@@ -247,6 +247,21 @@ export async function ensureChildrenIncluded(node: BlockEntity): Promise<BlockEn
247247}
248248
249249export 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-
386363export 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+
413401export 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 */
457445export async function insertBatchBlockBefore (
458446 srcBlock : BlockEntity ,
0 commit comments