Skip to content

Commit 041c5c1

Browse files
committed
refactor: move code & add utils
1 parent a0052c6 commit 041c5c1

File tree

3 files changed

+65
-40
lines changed

3 files changed

+65
-40
lines changed

src/commands.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ export async function toggleAutoHeadingCommand(opts: {togglingBasedOnFirstBlock:
4141
}
4242
}
4343

44+
export async function transformSelectedBlocksCommand(
45+
blocks: BlockEntity[],
46+
transformCallback: (blocks: BlockEntity[]) => BlockEntity[],
47+
isSelectedState: boolean,
48+
) {
49+
// CASE: all transformed blocks relates to one root block
50+
if (blocks.length === 1) {
51+
const tree = await ensureChildrenIncluded(blocks[0])
52+
if (!tree.children || tree.children.length === 0)
53+
return // nothing to transform
54+
55+
const newRoot = await transformBlocksTreeByReplacing(tree, transformCallback)
56+
if (newRoot) { // successfully replaced
57+
if (isSelectedState)
58+
await logseq.Editor.selectBlock(newRoot.uuid)
59+
else
60+
await logseq.Editor.editBlock(newRoot.uuid)
61+
62+
return
63+
}
64+
65+
// fallback to array of blocks
66+
blocks = tree.children as BlockEntity[]
67+
}
68+
69+
70+
// CASE: selected blocks from different parents
71+
transformSelectedBlocksWithMovements(blocks, transformCallback)
72+
}
4473

4574
export async function sortBlocksCommand(contextBlockUUID: string | null = null) {
4675
let blocks: BlockEntity[]

src/utils/logseq.ts

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ export async function ensureChildrenIncluded(node: BlockEntity): Promise<BlockEn
247247
}
248248

249249
export async function replaceChildrenBlocksInTree(
250+
export async function getBlocksWithReferences(root: BlockEntity): Promise<BlockEntity[]> {
251+
const blocksWithPersistedID = findPropertyInTree(root as IBatchBlock, PropertiesUtils.idProperty)
252+
const blocksAndItsReferences = (await Promise.all(
253+
blocksWithPersistedID.map(async (b): Promise<[BlockEntity, Number[]]> => {
254+
const block = b as BlockEntity
255+
const references = await findBlockReferences(block.uuid)
256+
return [block, references]
257+
})
258+
))
259+
const blocksWithReferences = blocksAndItsReferences.filter(([b, rs]) => (rs.length !== 0))
260+
return blocksWithReferences.map(([b, rs]) => {
261+
b._references = rs
262+
return b
263+
})
264+
}
250265
root: BlockEntity,
251266
transformChildrenCallback: (blocks: BlockEntity[]) => BlockEntity[],
252267
): Promise<BlockEntity | null> {
@@ -257,18 +272,10 @@ export async function replaceChildrenBlocksInTree(
257272
// METHOD: blocks removal to replace whole tree
258273
// but it is important to check if any block in the tree has references
259274
// (Logseq replaces references with it's text)
260-
const blocksWithPersistedID = findPropertyInTree(root as IBatchBlock, PropertiesUtils.idProperty)
261-
const blocksAndItsReferences = (await Promise.all(
262-
blocksWithPersistedID.map(async (b): Promise<[IBatchBlock, Number[]]> => {
263-
const references = await findBlockReferences((b as BlockEntity).uuid)
264-
return [b, references]
265-
})
266-
))
267-
const blocksWithReferences = blocksAndItsReferences.filter(([b, rs]) => (rs.length !== 0))
275+
const blocksWithReferences = await getBlocksWithReferences(root)
268276
if (blocksWithReferences.length !== 0)
269277
return null // blocks removal cannot be used
270278

271-
272279
const transformedBlocks = transformChildrenCallback(root.children as BlockEntity[])
273280

274281
// root is the first block in page
@@ -353,36 +360,6 @@ export async function transformSelectedBlocksWithMovements(
353360
}
354361
}
355362

356-
export async function transformSelectedBlocksCommand(
357-
blocks: BlockEntity[],
358-
transformCallback: (blocks: BlockEntity[]) => BlockEntity[],
359-
isSelectedState: boolean,
360-
) {
361-
// CASE: all sorting blocks relates to one root block
362-
if (blocks.length === 1) {
363-
const tree = await ensureChildrenIncluded(blocks[0])
364-
if (!tree.children || tree.children.length === 0)
365-
return // nothing to transform
366-
367-
const newRoot = await replaceChildrenBlocksInTree(tree, transformCallback)
368-
if (newRoot) { // successfully replaced
369-
if (isSelectedState)
370-
await logseq.Editor.selectBlock(newRoot.uuid)
371-
else
372-
await logseq.Editor.editBlock(newRoot.uuid)
373-
374-
return
375-
}
376-
377-
// fallback to array of blocks
378-
blocks = tree.children as BlockEntity[]
379-
}
380-
381-
382-
// CASE: selected blocks from different parents
383-
transformSelectedBlocksWithMovements(blocks, transformCallback)
384-
}
385-
386363
export async function walkBlockTreeAsync(
387364
root: IBatchBlock,
388365
callback: (b: IBatchBlock, lvl: number) => Promise<string | void>,
@@ -410,6 +387,17 @@ export function walkBlockTree(
410387
}
411388
}
412389

390+
export function reduceBlockTree(
391+
root: IBatchBlock,
392+
callback: (b: IBatchBlock, lvl: number, children: string[]) => string,
393+
level: number = 0,
394+
): string {
395+
const children = (root.children || []).map(
396+
(b) => reduceBlockTree(b as IBatchBlock, callback, level + 1)
397+
)
398+
return callback(root, level, children) ?? ''
399+
}
400+
413401
export function findPropertyInTree(tree: IBatchBlock, propertyName: string): IBatchBlock[] {
414402
const found: IBatchBlock[] = []
415403
walkBlockTree(tree, (node, level) => {
@@ -452,7 +440,7 @@ export function filterOutChildBlocks(blocks: BlockEntity[]): BlockEntity[] {
452440
}
453441

454442
/**
455-
* Reason: logseq bug — before: true doesn't work for batch inserting
443+
* Reason: logseq bug — `before: true` doesn't work for batch inserting
456444
*/
457445
export async function insertBatchBlockBefore(
458446
srcBlock: BlockEntity,

src/utils/other.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,11 @@ export function unique<X>(items: Array<X>, keyFunction: (item: X) => any) {
6666
return keyFunction(r[i - 1]) !== keyFunction(b)
6767
})
6868
}
69+
70+
71+
export function reduceTextWithLength(text: string, length: number, suffix = '...') {
72+
if (text.length <= length)
73+
return text
74+
return text.substring(0, length).trimEnd() + suffix
75+
}
76+

0 commit comments

Comments
 (0)