Skip to content

Commit

Permalink
add at top or bottom option
Browse files Browse the repository at this point in the history
  • Loading branch information
vipzhicheng committed Apr 25, 2022
1 parent 21a7c49 commit 91a63fd
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/common/funcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export const getSettings = (
return key ? (merged[key] ? merged[key] : defaultValue) : merged;
};

export const getFirstBlock = async function (
pageName: string
): Promise<null | BlockEntity> {
const blocks = await logseq.Editor.getPageBlocksTree(pageName);
if (blocks.length === 0) {
return null;
}
return blocks[0];
};

export const getLastBlock = async function (
pageName: string
): Promise<null | BlockEntity> {
Expand Down
19 changes: 19 additions & 0 deletions src/components/Target.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const pageFilter = computed(() => {
<div>
<n-radio key="contents" value="contents"> Contents </n-radio>
</div>
<div>
<n-radio key="current_page" value="current_page">
Current page
</n-radio>
</div>
<div>
<n-radio key="journal" value="journal"> A journal </n-radio>
</div>
Expand Down Expand Up @@ -85,6 +90,20 @@ const pageFilter = computed(() => {
/>
</n-form-item>

<n-form-item label="At:" path="at">
<n-radio-group
v-model:value="targetStore.destination.at"
name="radiogroup"
>
<div>
<n-radio key="bottom" value="bottom"> Bottom </n-radio>
</div>
<div>
<n-radio key="top" value="top"> Top </n-radio>
</div>
</n-radio-group>
</n-form-item>

<n-form-item label="Action:" path="action">
<n-radio-group
v-model:value="targetStore.destination.action"
Expand Down
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import '@logseq/libs';
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
import copyRefToContents from './operations/copyRefToContents';
import copyRefToJournal from './operations/copyRefToJournal';
import moveTo from './operations/moveTo';
import { useTargetStore } from '@/stores/target';
Expand All @@ -14,7 +13,7 @@ import { initSettings, setHotkeys } from './common/funcs';
async function main() {
initSettings();
copyRefToJournal();
copyRefToContents();
// copyRefToContents();

const app = createApp(App);
app.use(createPinia());
Expand Down
45 changes: 35 additions & 10 deletions src/stores/target.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BlockEntity } from '@logseq/libs/dist/LSPlugin';
import { defineStore } from 'pinia';
import { format } from 'date-fns';
import { createPageIfNotExist, getLastBlock } from '@/common/funcs';
import {
createPageIfNotExist,
getFirstBlock,
getLastBlock,
} from '@/common/funcs';

type Destination = {
to: string;
at: string;
action: string;
journal: string | null;
after: string;
Expand All @@ -16,7 +21,7 @@ const processBlock = async (block: BlockEntity, destination: Destination) => {
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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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') {
Expand All @@ -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);
Expand All @@ -98,6 +122,7 @@ export const useTargetStore = defineStore('target', {
visible: false,
destination: {
to: 'today',
at: 'bottom',
action: 'copy_ref',
journal: null,
after: 'stay',
Expand Down

0 comments on commit 91a63fd

Please sign in to comment.