Skip to content

Commit

Permalink
fix: skip re-rendering if setMarkdown called with current value
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Nov 24, 2023
1 parent 51a8612 commit 9fe9680
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
COMMAND_PRIORITY_CRITICAL,
DecoratorNode,
ElementNode,
FOCUS_COMMAND,
FORMAT_TEXT_COMMAND,
KEY_DOWN_COMMAND,
Klass,
Expand Down Expand Up @@ -233,8 +234,14 @@ export const coreSystem = system((r) => {
const setMarkdown = r.node<string>()

r.sub(
r.pipe(setMarkdown, r.o.withLatestFrom(rootEditor, importVisitors, mdastExtensions, syntaxExtensions, inFocus)),
([theNewMarkdownValue, editor, importVisitors, mdastExtensions, syntaxExtensions, inFocus]) => {
r.pipe(
setMarkdown,
r.o.withLatestFrom(markdown, rootEditor, importVisitors, mdastExtensions, syntaxExtensions, inFocus),
r.o.filter(([newMarkdown, oldMarkdown]) => {
return newMarkdown.trim() !== oldMarkdown.trim()
})
),
([theNewMarkdownValue, , editor, importVisitors, mdastExtensions, syntaxExtensions, inFocus]) => {
editor?.update(() => {
$getRoot().clear()
importMarkdownToLexical({
Expand All @@ -247,6 +254,8 @@ export const coreSystem = system((r) => {

if (!inFocus) {
$setSelection(null)
} else {
editor.focus()
}
})
}
Expand Down Expand Up @@ -311,6 +320,17 @@ export const coreSystem = system((r) => {
)
})

r.pub(createRootEditorSubscription, (theEditor) => {
return theEditor.registerCommand(
FOCUS_COMMAND,
() => {
r.pub(inFocus, true)
return false
},
COMMAND_PRIORITY_CRITICAL
)
})

// Fixes select all when frontmatter is present
r.pub(createRootEditorSubscription, (theRootEditor) => {
return theRootEditor.registerCommand<KeyboardEvent>(
Expand Down

0 comments on commit 9fe9680

Please sign in to comment.