Skip to content

Commit

Permalink
fix: prevent accidental frontmatter deletion
Browse files Browse the repository at this point in the history
Fixes #611
  • Loading branch information
petyosi committed Nov 10, 2024
1 parent fa62100 commit d72e836
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/examples/basics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ export function Frontmatter() {
return (
<MDXEditor
markdown={frontmatterMarkdown}
onChange={(value) => {
console.log(`new value: ${value}`)
}}
plugins={[frontmatterPlugin(), toolbarPlugin({ toolbarContents: () => <InsertFrontmatter /> })]}
/>
)
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,9 @@ export const createRootEditorSubscription$ = Appender(rootEditorSubscriptions$,
},
COMMAND_PRIORITY_CRITICAL
)
},
}

/*
// Fixes select all when frontmatter is present
(rootEditor) => {
return rootEditor.registerCommand<KeyboardEvent>(
Expand Down Expand Up @@ -581,7 +583,7 @@ export const createRootEditorSubscription$ = Appender(rootEditorSubscriptions$,
},
COMMAND_PRIORITY_CRITICAL
)
}
}*/
])
})

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/frontmatter/FrontmatterNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class FrontmatterNode extends DecoratorNode<JSX.Element> {
/>
)
}

isKeyboardSelectable(): boolean {
return false
}
}

/**
Expand Down
52 changes: 50 additions & 2 deletions src/plugins/frontmatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import {
rootEditor$
} from '../core'
import { Action, Cell, withLatestFrom } from '@mdxeditor/gurx'
import { $getRoot } from 'lexical'
import {
$getRoot,
$getSelection,
$isRangeSelection,
$setSelection,
COMMAND_PRIORITY_CRITICAL,
KEY_DOWN_COMMAND,
LexicalEditor
} from 'lexical'
import { frontmatterFromMarkdown, frontmatterToMarkdown } from 'mdast-util-frontmatter'
import { frontmatter } from 'micromark-extension-frontmatter'
import { $createFrontmatterNode, $isFrontmatterNode, FrontmatterNode } from './FrontmatterNode'
Expand Down Expand Up @@ -87,7 +95,47 @@ export const frontmatterPlugin = realmPlugin({
[addLexicalNode$]: FrontmatterNode,
[addImportVisitor$]: MdastFrontmatterVisitor,
[addExportVisitor$]: LexicalFrontmatterVisitor,
[addToMarkdownExtension$]: frontmatterToMarkdown('yaml')
[addToMarkdownExtension$]: frontmatterToMarkdown('yaml'),
[createRootEditorSubscription$]: (editor: LexicalEditor) => {
return editor.registerCommand<KeyboardEvent>(
KEY_DOWN_COMMAND,
(event) => {
let shouldPrevent = false

editor.read(() => {
const selection = $getSelection()

if ($isRangeSelection(selection)) {
if (selection.isCollapsed() && selection.anchor.offset === 0 && selection.focus.offset === 0 && event.key === 'Backspace') {
shouldPrevent = true
event.preventDefault()
} else {
const firstNode = selection.getNodes()[0]
if ($isFrontmatterNode(firstNode)) {
const yaml = firstNode.getYaml()
setTimeout(() => {
editor.update(
() => {
$getRoot().splice(0, 0, [$createFrontmatterNode(yaml)])
},
{ discrete: true }
)
})
}
}
}
})

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (shouldPrevent) {
return true
}

return false
},
COMMAND_PRIORITY_CRITICAL
)
}
})
}
})

0 comments on commit d72e836

Please sign in to comment.