Skip to content

Commit

Permalink
Merge pull request #10 from hyrijk/feature/move-block
Browse files Browse the repository at this point in the history
feat: refactor with `moveBlock` api #1 #6 #9
  • Loading branch information
hyrijk authored Mar 27, 2022
2 parents da93ff6 + 0f73829 commit 4c4de2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
58 changes: 20 additions & 38 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ async function main(blockId: string) {
if (block === null || block.children?.length === 0) {
return;
}
if (mayBeReferenced(block.children as BlockEntity[])) {
// https://github.com/hyrijk/logseq-plugin-block-to-page/issues/1
logseq.App.showMsg("some sub block may be referenced", "error");
return;
}

const pageRegx = /^\[\[(.*)\]\]$/;
const firstLine = block.content.split("\n")[0].trim();
Expand All @@ -28,25 +23,32 @@ async function main(blockId: string) {

const srcBlock = await getLastBlock(pageName);
if (srcBlock) {
// page.format 为空
if (srcBlock.format !== block.format) {
logseq.App.showMsg("page format not same", "error");
return Promise.reject("page format not same");
}

await removeBlocks(block.children as BlockEntity[]);
if (newBlockContent) {
await logseq.Editor.updateBlock(block.uuid, newBlockContent);
// propteties param not working...
// and then remove block property will undo updateBlock...
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;
}
}
await insertBatchBlock(srcBlock.uuid, block.children as BlockEntity[]);

// remove first line.
if (srcBlock.content === "") {
// insertBatchBlock `before` param not working...
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) {
Expand All @@ -66,19 +68,6 @@ logseq
})
.catch(console.error);

async function insertBatchBlock(
srcBlock: BlockIdentity,
blocks: BlockEntity[]
) {
const batchBlocks = toBatchBlocks(blocks);

debug("insertBatchBlock", srcBlock, batchBlocks);
await logseq.Editor.insertBatchBlock(srcBlock, batchBlocks, {
sibling: true,
before: false,
});
}

async function createPageIfNotExist(pageName: string) {
let page = await logseq.Editor.getPage(pageName);
if (!page) {
Expand Down Expand Up @@ -108,13 +97,6 @@ async function createPageIfNotExist(pageName: string) {
}
}

async function removeBlocks(blocks: BlockEntity[]) {
for (let i = 0; i < blocks.length; i++) {
const child = blocks[i];
await logseq.Editor.removeBlock(child.uuid);
}
}

async function getLastBlock(pageName: string): Promise<null | BlockEntity> {
const blocks = await logseq.Editor.getPageBlocksTree(pageName);
if (blocks.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-plugin-block-to-page",
"version": "0.0.5",
"version": "1.3.0",
"description": "turn block into page",
"main": "dist/index.html",
"scripts": {
Expand All @@ -25,4 +25,4 @@
"id": "logseq-block-to-page",
"icon": "./icon.png"
}
}
}

0 comments on commit 4c4de2a

Please sign in to comment.