-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.ts
110 lines (98 loc) · 2.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import "@logseq/libs";
import { BlockEntity, BlockIdentity } from "@logseq/libs/dist/LSPlugin.user";
import { toBatchBlocks, mayBeReferenced } from "./util";
async function main(blockId: string) {
const block = await logseq.Editor.getBlock(blockId, {
includeChildren: true,
});
if (block === null || block.children?.length === 0) {
return;
}
const pageRegx = /^\[\[(.*)\]\]$/;
const firstLine = block.content.split("\n")[0].trim();
const pageName = firstLine.replace(pageRegx, "$1");
let newBlockContent = "";
if (!pageRegx.test(firstLine)) {
newBlockContent = block.content.replace(firstLine, `[[${firstLine}]]`);
}
await createPageIfNotExist(pageName);
const srcBlock = await getLastBlock(pageName);
if (srcBlock) {
const children = block.children as BlockEntity[];
let targetUUID = srcBlock.uuid;
for (let i = 0; i < children.length; i++) {
try {
await logseq.Editor.moveBlock(children[i].uuid, targetUUID, {
children: false,
before: false,
});
targetUUID = children[i].uuid;
} catch (error) {
console.error("moveBlock error", error);
logseq.App.showMsg("move block error", "error");
return;
}
}
// remove first line.
if (srcBlock.content === "") {
await logseq.Editor.removeBlock(srcBlock.uuid);
}
if (newBlockContent) {
await logseq.Editor.updateBlock(block.uuid, newBlockContent);
// properties param not working...
// and then remove block property will undo updateBlock...
}
await logseq.Editor.exitEditingMode();
if (block.properties?.collapsed) {
await logseq.Editor.removeBlockProperty(block.uuid, "collapsed");
}
}
}
logseq
.ready(() => {
logseq.Editor.registerSlashCommand("Turn Into Page", async (e) => {
main(e.uuid);
});
logseq.Editor.registerBlockContextMenuItem("Turn into page", async (e) => {
main(e.uuid);
});
})
.catch(console.error);
async function createPageIfNotExist(pageName: string) {
let page = await logseq.Editor.getPage(pageName);
if (!page) {
await logseq.Editor.createPage(
pageName,
{},
{
createFirstBlock: true,
redirect: false,
}
);
} else {
debug("page already exist");
const lastBlock = await getLastBlock(pageName);
if (lastBlock === null) {
// 无法往空页面写入 block
await logseq.Editor.deletePage(pageName);
await logseq.Editor.createPage(
pageName,
{},
{
createFirstBlock: true,
redirect: false,
}
);
}
}
}
async function getLastBlock(pageName: string): Promise<null | BlockEntity> {
const blocks = await logseq.Editor.getPageBlocksTree(pageName);
if (blocks.length === 0) {
return null;
}
return blocks[blocks.length - 1];
}
function debug(...args: any) {
console.debug("block-to-page", ...args);
}