Skip to content

Commit 3b0cf16

Browse files
committed
feat(commands): forced block swap
1 parent 2234b3b commit 3b0cf16

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/app.ts

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { BlockEntity, SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user'
1+
import { BlockEntity, PageEntity, SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user'
22

33
import { splitByParagraphsCommand, toggleAutoHeadingCommand } from './commands'
4-
import { getChosenBlocks, p } from './utils'
4+
import { getChosenBlocks, p, scrollToBlock, sleep } from './utils'
55
import { log } from 'console'
66

77

@@ -207,6 +207,57 @@ async function main() {
207207
await logseq.Editor.editBlock((nextBlock as BlockEntity).uuid, {pos: 0})
208208
} )
209209

210+
211+
// Movements
212+
logseq.App.registerCommandPalette({
213+
label: '🪚 Move block (⤒) on top of siblings', key: 'move-block-1-on-top',
214+
keybinding: {mac: 'mod+alt+shift+up', binding: 'ctrl+alt+shift+up', mode: 'global'},
215+
}, async (e) => {
216+
const [blocks] = await getChosenBlocks()
217+
const [first] = blocks
218+
if (!first)
219+
return
220+
221+
// already on top
222+
if (first.parent.id === first.left.id || first.left.id === first.page.id)
223+
return
224+
225+
let topmost = first
226+
while (true) {
227+
const prev = await logseq.Editor.getPreviousSiblingBlock(topmost.uuid)
228+
if (!prev)
229+
break
230+
topmost = prev
231+
}
232+
233+
await logseq.Editor.moveBlock(first.uuid, topmost.uuid, {before: true, children: false})
234+
await scrollToBlock(first)
235+
} )
236+
237+
logseq.App.registerCommandPalette({
238+
label: '🪚 Move block (⤓) on bottom of siblings', key: 'move-block-2-on-bottom',
239+
keybinding: {mac: 'mod+alt+shift+down', binding: 'ctrl+alt+shift+down', mode: 'global'},
240+
}, async (e) => {
241+
const [blocks] = await getChosenBlocks()
242+
const [first] = blocks
243+
if (!first)
244+
return
245+
246+
let bottommost = first
247+
while (true) {
248+
const next = await logseq.Editor.getNextSiblingBlock(bottommost.uuid)
249+
if (!next)
250+
break
251+
bottommost = next
252+
}
253+
254+
// already on bottom
255+
if (first.id === bottommost.id)
256+
return
257+
258+
await logseq.Editor.moveBlock(first.uuid, bottommost.uuid, {before: false, children: false})
259+
await scrollToBlock(first)
260+
} )
210261
await postInit()
211262
}
212263

src/utils/logseq.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BlockEntity } from '@logseq/libs/dist/LSPlugin.user'
1+
import { BlockEntity, PageEntity } from '@logseq/libs/dist/LSPlugin.user'
22

33
import { f, indexOfNth, p, sleep } from './other'
44

@@ -221,3 +221,19 @@ export class PropertiesUtils {
221221
return propertyNames
222222
}
223223
}
224+
225+
// scroll to block if it has disappeared from view
226+
export async function scrollToBlock(block: BlockEntity) {
227+
const position = (await logseq.Editor.getEditingCursorPosition())?.pos // editing mode?
228+
const view = await logseq.App.queryElementRect(`.ls-block[blockid="${block.uuid}"]`)
229+
if (view && (view.top < 0 || view.bottom > top!.window.innerHeight)) {
230+
const page = await logseq.Editor.getPage(block.page.id) as PageEntity
231+
logseq.Editor.scrollToBlockInPage(page.name, block.uuid)
232+
233+
// .scrollToBlockInPage exists editing mode — return to it if necessary
234+
if (position) {
235+
await sleep(250)
236+
await logseq.Editor.editBlock(block.uuid, {pos: position})
237+
}
238+
}
239+
}

0 commit comments

Comments
 (0)