From 91a63fd0edfcd0e5392fa6943cc1b854c5f0dada Mon Sep 17 00:00:00 2001 From: Richard Yu Date: Mon, 25 Apr 2022 18:52:36 +0800 Subject: [PATCH] add at top or bottom option --- CHANGELOG.md | 5 +++++ src/common/funcs.ts | 10 +++++++++ src/components/Target.vue | 19 +++++++++++++++++ src/main.ts | 3 +-- src/stores/target.ts | 45 ++++++++++++++++++++++++++++++--------- 5 files changed, 70 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88b4cbf..90c8ce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v0.0.6 + +- fix: remove copy ref to contents context command, not that useful. +- fix: add copy to current page and choose at top or bottom. + ## v0.0.5 - fix: optimize cut content action using moveBlock diff --git a/src/common/funcs.ts b/src/common/funcs.ts index bf19143..6fb92eb 100644 --- a/src/common/funcs.ts +++ b/src/common/funcs.ts @@ -35,6 +35,16 @@ export const getSettings = ( return key ? (merged[key] ? merged[key] : defaultValue) : merged; }; +export const getFirstBlock = async function ( + pageName: string +): Promise { + const blocks = await logseq.Editor.getPageBlocksTree(pageName); + if (blocks.length === 0) { + return null; + } + return blocks[0]; +}; + export const getLastBlock = async function ( pageName: string ): Promise { diff --git a/src/components/Target.vue b/src/components/Target.vue index fe4d59a..a8a32e5 100644 --- a/src/components/Target.vue +++ b/src/components/Target.vue @@ -50,6 +50,11 @@ const pageFilter = computed(() => {
Contents
+
+ + Current page + +
A journal
@@ -85,6 +90,20 @@ const pageFilter = computed(() => { /> + + +
+ Bottom +
+
+ Top +
+
+
+ { return false; } const config = await logseq.App.getUserConfigs(); - const { to, action, journal, after, page } = destination; + const { to, action, journal, after, page, at } = destination; let targetPage; let isJournal = false; switch (to) { @@ -27,6 +32,16 @@ const processBlock = async (block: BlockEntity, destination: Destination) => { case 'contents': targetPage = 'contents'; break; + case 'current_page': + let currentPage = await logseq.Editor.getCurrentPage(); + if (!currentPage) { + const currentBlock = await logseq.Editor.getCurrentBlock(); + if (currentBlock) { + currentPage = await logseq.Editor.getPage(currentBlock.page.id); + } + } + targetPage = currentPage.name; + break; case 'page': targetPage = page; @@ -45,7 +60,15 @@ const processBlock = async (block: BlockEntity, destination: Destination) => { } await createPageIfNotExist(targetPage, isJournal); - const lastBlock = await getLastBlock(targetPage); + let atBlock; + + if (at === 'bottom') { + atBlock = await getLastBlock(targetPage); + } else { + atBlock = await getFirstBlock(targetPage); + } + + await getLastBlock(targetPage); let targetBlock; let newBlockContent; @@ -58,19 +81,20 @@ const processBlock = async (block: BlockEntity, destination: Destination) => { break; } - if (lastBlock) { + if (atBlock) { if (['copy_ref', 'copy_content'].includes(action)) { - if (lastBlock.content) { + if (atBlock.content) { targetBlock = await logseq.Editor.insertBlock( - lastBlock.uuid, + atBlock.uuid, newBlockContent, { + before: at !== 'bottom', sibling: true, } ); } else { - await logseq.Editor.updateBlock(lastBlock.uuid, newBlockContent); - targetBlock = lastBlock; + await logseq.Editor.updateBlock(atBlock.uuid, newBlockContent); + targetBlock = atBlock; } } else if (['cut_content', 'cut_content_and_keep_ref'].includes(action)) { if (action === 'cut_content_and_keep_ref') { @@ -79,8 +103,8 @@ const processBlock = async (block: BlockEntity, destination: Destination) => { sibling: true, }); } - await logseq.Editor.moveBlock(block.uuid, lastBlock.uuid, { - before: false, + await logseq.Editor.moveBlock(block.uuid, atBlock.uuid, { + before: at !== 'bottom', children: false, }); targetBlock = await logseq.Editor.getBlock(block.uuid); @@ -98,6 +122,7 @@ export const useTargetStore = defineStore('target', { visible: false, destination: { to: 'today', + at: 'bottom', action: 'copy_ref', journal: null, after: 'stay',