Skip to content

Commit b42f249

Browse files
committed
fix: empty & numbering bugs in inserting
1 parent b3731eb commit b42f249

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

src/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export function magicSplit(text: string): IBatchBlock[] {
418418
function createBlock(content: string, opts: { numbering: boolean } = { numbering: false }) {
419419
const properties: {[name: string]: string} = {}
420420
if (opts.numbering)
421-
properties['logseq.order-list-type'] = 'number'
421+
properties[PropertiesUtils.numberingProperty_] = 'number'
422422
return {content, children: [], properties}
423423
}
424424

src/utils/logseq.ts

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class PropertiesUtils {
6767
static readonly idProperty = 'id'
6868
static readonly headingProperty = 'heading'
6969
static readonly numberingProperty = 'logseq.orderListType'
70+
static readonly numberingProperty_ = 'logseq.order-list-type'
7071

7172
// source: https://github.com/logseq/logseq/blob/master/deps/graph-parser/src/logseq/graph_parser/property.cljs#L81
7273
// logseq.* prefix need to be checked separately
@@ -424,42 +425,68 @@ export async function insertBatchBlockBefore(
424425
// there is no batch way: use pseudo block
425426
// issue: https://github.com/logseq/logseq/issues/10729
426427
const first = ( await logseq.Editor.insertBlock(
427-
srcBlock.uuid, '', {before: true, sibling: true}) )!
428+
srcBlock.uuid, 'ø', {before: true, sibling: true}) )!
428429
const result = await logseq.Editor.insertBatchBlock(
429430
first.uuid, blocks, {before: false, sibling: true, ...opts})
430-
// due to logseq bug: empty first block overrides with next insertion
431-
// there is no reason to remove pseudo block
432-
// await logseq.Editor.removeBlock(first.uuid)
433-
// issue: https://github.com/logseq/logseq/issues/10729
431+
await logseq.Editor.removeBlock(first.uuid)
434432
return result
435433
}
436434

437435
const prev = await logseq.Editor.getPreviousSiblingBlock(srcBlock.uuid)
438436
if (prev) {
439-
// special handling for empty block
437+
// special handling for empty block & numbering
440438
// issue: https://github.com/logseq/logseq/issues/10729
441-
const emptyParent = !prev.content
442-
if (emptyParent)
443-
await logseq.Editor.updateBlock(prev.uuid, 'ø')
444-
const result = await logseq.Editor.insertBatchBlock(
439+
const empty = !PropertiesUtils.deleteAllProperties(prev.content)
440+
441+
let numbering = undefined
442+
let properties = {}
443+
if (prev.properties) {
444+
numbering = prev.properties[PropertiesUtils.numberingProperty]
445+
delete prev.properties[PropertiesUtils.numberingProperty]
446+
properties = PropertiesUtils.fromCamelCaseAll(prev.properties)
447+
}
448+
if (numbering)
449+
await logseq.Editor.removeBlockProperty(prev.uuid, PropertiesUtils.numberingProperty_)
450+
if (empty)
451+
await logseq.Editor.updateBlock(prev.uuid, 'ø', {properties})
452+
453+
const inserted = await logseq.Editor.insertBatchBlock(
445454
prev.uuid, blocks, {before: false, sibling: true, ...opts})
446-
if (emptyParent)
447-
await logseq.Editor.updateBlock(prev.uuid, '')
448-
return result
455+
456+
if (empty)
457+
await logseq.Editor.updateBlock(prev.uuid, '', {properties})
458+
if (numbering)
459+
await logseq.Editor.upsertBlockProperty(prev.uuid, PropertiesUtils.numberingProperty_, numbering)
460+
461+
return inserted
449462
}
450463

451464
// first block for parent
452-
const parent: BlockEntity | null | {uuid: string} = ( await logseq.Editor.getBlock(srcBlock.parent.id) )!
453-
let inserted = await logseq.Editor.insertBatchBlock(
454-
parent.uuid, blocks, {sibling: false, ...opts})
455-
if (!inserted) {
456-
const children = ( await logseq.Editor.getBlock(parent.uuid) )!.children!
457-
inserted = {uuid: children[0][1]}
465+
const parent = ( await logseq.Editor.getBlock(srcBlock.parent.id) )!
466+
467+
// special handling for empty block & numbering
468+
// issue: https://github.com/logseq/logseq/issues/10729
469+
const empty = !PropertiesUtils.deleteAllProperties(parent.content)
470+
471+
let numbering = undefined
472+
let properties = {}
473+
if (parent.properties) {
474+
numbering = parent.properties[PropertiesUtils.numberingProperty]
475+
delete parent.properties[PropertiesUtils.numberingProperty]
476+
properties = PropertiesUtils.fromCamelCaseAll(parent.properties)
458477
}
459-
if (inserted)
460-
await logseq.Editor.removeBlockProperty(
461-
inserted.uuid,
462-
PropertiesUtils.fromCamelCase(PropertiesUtils.numberingProperty),
463-
)
478+
if (numbering)
479+
await logseq.Editor.removeBlockProperty(parent.uuid, PropertiesUtils.numberingProperty_)
480+
if (empty)
481+
await logseq.Editor.updateBlock(parent.uuid, 'ø', {properties})
482+
483+
const inserted = await logseq.Editor.insertBatchBlock(
484+
parent.uuid, blocks, {sibling: false, ...opts})
485+
486+
if (empty)
487+
await logseq.Editor.updateBlock(parent.uuid, '', {properties})
488+
if (numbering)
489+
await logseq.Editor.upsertBlockProperty(parent.uuid, PropertiesUtils.numberingProperty_, numbering)
490+
464491
return inserted
465492
}

0 commit comments

Comments
 (0)