Skip to content

Commit 0380f9f

Browse files
kvapsclaude
andcommitted
fix: handle root edits when markdown has no H1 line
trySurgicalEdit detects root via mind.nodeData reference (more reliable than meta.kind which can fall through to 'block' after refreshMetadata). editNodeText falls back to inserting `# topic` at the document start when root.startLine is missing, so renaming root in an empty document produces a real heading instead of leaving the change unsaved. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
1 parent 3655829 commit 0380f9f

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/components/MindMap.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,14 @@ function trySurgicalEdit(op: unknown): boolean {
166166
167167
if (o.name === 'finishEdit') {
168168
const meta = o.obj?.metadata as NodeMeta | undefined
169-
if (!meta || meta.startLine < 0) return false
169+
if (!meta) return false
170+
const isRoot = !!mind && o.obj === mind.nodeData
171+
if (isRoot && meta.startLine < 0) {
172+
const forced: NodeMeta = { ...meta, kind: 'root' }
173+
if (typeof o.obj?.topic !== 'string') return false
174+
return commitMarkdown(editNodeText(store.markdown, forced, o.obj.topic))
175+
}
176+
if (meta.startLine < 0) return false
170177
if (typeof o.obj?.topic !== 'string') return false
171178
return commitMarkdown(editNodeText(store.markdown, meta, o.obj.topic))
172179
}

src/utils/surgical.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ function escapeNewlinesForListItem(text: string, indent: number): string {
1818
}
1919

2020
export function editNodeText(markdown: string, meta: NodeMeta, newText: string): string {
21-
if (meta.startLine < 0) return markdown
21+
if (meta.startLine < 0) {
22+
if (meta.kind !== 'root') return markdown
23+
const head = `# ${newText.split('\n')[0]}`
24+
const rest = markdown.trim()
25+
if (!rest) return head + '\n'
26+
return head + '\n\n' + rest + (rest.endsWith('\n') ? '' : '\n')
27+
}
2228
const lines = markdown.split('\n')
2329
const firstLine = lines[meta.startLine]
2430
if (firstLine === undefined) return markdown

0 commit comments

Comments
 (0)