Skip to content

Commit

Permalink
Merge branch 'hyrijk-master'
Browse files Browse the repository at this point in the history
* hyrijk-master:
  feat: refactor with `moveBlock` api #1 hyrijk#6 hyrijk#9
  • Loading branch information
qbosen committed May 25, 2022
2 parents 727703c + 786a72b commit 3e450cb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 65 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, isEmbedPage: boolean = false, isPublicPage:
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 @@ -29,26 +24,32 @@ async function main(blockId: string, isEmbedPage: boolean = false, isPublicPage:

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) {
// properties 似乎是部分更新
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();

// 删除原本的属性
Expand Down Expand Up @@ -79,18 +80,6 @@ logseq
})
.catch(console.error);

async function insertBatchBlock(
srcBlock: BlockIdentity,
blocks: BlockEntity[]
) {
const batchBlocks = toBatchBlocks(blocks);
debug("insertBatchBlock", srcBlock, blocks, batchBlocks);
await logseq.Editor.insertBatchBlock(srcBlock, batchBlocks, {
sibling: true,
before: false,
});
}

async function createPageIfNotExist(pageName: string, isPublicPage: boolean, pageProperties: any) {
let page = await logseq.Editor.getPage(pageName);
pageProperties = toRawProperties(pageProperties);
Expand Down Expand Up @@ -128,13 +117,6 @@ async function createPageIfNotExist(pageName: string, isPublicPage: boolean, pag
}
}

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
54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "logseq-plugin-block-to-page",
"title": "Block to page",
"version": "2.0.0",
"description": "Turn block into page",
"main": "dist/index.html",
"scripts": {
"dev": "parcel ./index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps index.html"
},
"keywords": [
"logseq"
],
"author": "qbosen",
"devDependencies": {
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.2.3",
"parcel": "2.0.0-beta.2",
"semantic-release": "^17.4.7",
"typescript": "^4.4.3"
},
"dependencies": {
"@logseq/libs": "^0.0.1-alpha.27"
},
"logseq": {
"id": "logseq-block-to-page-hack",
"icon": "./icon.png"
}
"name": "logseq-plugin-block-to-page",
"title": "Block to page",
"version": "2.0.0",
"description": "Turn block into page",
"main": "dist/index.html",
"scripts": {
"dev": "parcel ./index.html --public-url ./",
"build": "parcel build --public-url . --no-source-maps index.html"
},
"keywords": [
"logseq"
],
"author": "qbosen",
"devDependencies": {
"@semantic-release/git": "^9.0.0",
"@semantic-release/github": "^7.2.3",
"parcel": "2.0.0-beta.2",
"semantic-release": "^17.4.7",
"typescript": "^4.4.3"
},
"dependencies": {
"@logseq/libs": "^0.0.1-alpha.27"
},
"logseq": {
"id": "logseq-block-to-page-hack",
"icon": "./icon.png"
}
}

0 comments on commit 3e450cb

Please sign in to comment.