Skip to content

Commit ba7aad7

Browse files
committed
feat(commands): revrese & shuffle
1 parent d938448 commit ba7aad7

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

src/app.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BlockEntity, SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user'
22

3-
import { sortBlocksCommand, splitByParagraphsCommand, toggleAutoHeadingCommand } from './commands'
3+
import { reverseBlocksCommand, shuffleBlocksCommand, sortBlocksCommand, splitByParagraphsCommand, toggleAutoHeadingCommand } from './commands'
44
import { getChosenBlocks, p, scrollToBlock } from './utils'
55

66

@@ -283,14 +283,26 @@ async function main() {
283283

284284
// Transformations
285285
logseq.App.registerCommandPalette({
286-
label: '🪚 Sort blocks', key: 'sort-blocks',
286+
label: '🪚 Sort blocks', key: 'transform-1-sort-blocks',
287287
// @ts-expect-error
288288
keybinding: {},
289289
}, (e) => sortBlocksCommand() )
290+
logseq.Editor.registerBlockContextMenuItem(
291+
'🪚 Sort blocks', async (e) => sortBlocksCommand(e.uuid) )
290292

293+
logseq.App.registerCommandPalette({
294+
label: '🪚 Reverse blocks', key: 'transform-2-reverse-blocks',
295+
// @ts-expect-error
296+
keybinding: {},
297+
}, (e) => reverseBlocksCommand() )
291298
logseq.Editor.registerBlockContextMenuItem(
292-
'🪚 Sort blocks',
293-
async (e) => sortBlocksCommand(e.uuid) )
299+
'🪚 Reverse blocks', async (e) => reverseBlocksCommand(e.uuid) )
300+
301+
logseq.App.registerCommandPalette({
302+
label: '🪚 Shuffle blocks', key: 'transform-3-shuffle-blocks',
303+
// @ts-expect-error
304+
keybinding: {},
305+
}, (e) => shuffleBlocksCommand() )
294306

295307
await postInit()
296308
}

src/commands.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,58 @@ export async function sortBlocksCommand(contextBlockUUID: string | null = null)
6464
transformSelectedBlocksCommand(blocks, sortBlocks, isSelectedState)
6565
}
6666

67+
export async function reverseBlocksCommand(contextBlockUUID: string | null = null) {
68+
let blocks: BlockEntity[]
69+
let isSelectedState = true
70+
if (contextBlockUUID)
71+
blocks = [(await logseq.Editor.getBlock(contextBlockUUID))!]
72+
else
73+
[blocks, isSelectedState] = await getChosenBlocks()
74+
75+
if (blocks.length === 0) {
76+
await logseq.UI.showMsg(
77+
`[:div
78+
[:b "🪚 Reverse Blocks Command"]
79+
[:p "Select some blocks to use the command"]]`,
80+
'warning',
81+
{timeout: 10000},
82+
)
83+
return
84+
}
85+
86+
const reverseBlocks = (blocks) => Array
87+
.from(blocks as BlockEntity[])
88+
.reverse()
89+
90+
transformSelectedBlocksCommand(blocks, reverseBlocks, isSelectedState)
91+
}
92+
93+
export async function shuffleBlocksCommand(contextBlockUUID: string | null = null) {
94+
let blocks: BlockEntity[]
95+
let isSelectedState = true
96+
if (contextBlockUUID)
97+
blocks = [(await logseq.Editor.getBlock(contextBlockUUID))!]
98+
else
99+
[blocks, isSelectedState] = await getChosenBlocks()
100+
101+
if (blocks.length === 0) {
102+
await logseq.UI.showMsg(
103+
`[:div
104+
[:b "🪚 Shuffle Blocks Command"]
105+
[:p "Select some blocks to use the command"]]`,
106+
'warning',
107+
{timeout: 10000},
108+
)
109+
return
110+
}
111+
112+
const shuffleBlocks = (blocks) => Array
113+
.from(blocks as BlockEntity[])
114+
.sort(() => Math.random() - 0.5)
115+
116+
transformSelectedBlocksCommand(blocks, shuffleBlocks, isSelectedState)
117+
}
118+
67119

68120
// paragraphs → lines → sentences
69121

0 commit comments

Comments
 (0)