Skip to content

Commit edfc280

Browse files
committed
fix: setMarkdown should not trigger onChange
Following the logic of the regular DOM elements
1 parent 2d1d254 commit edfc280

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/plugins/core/index.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createEmptyHistoryState } from '@lexical/react/LexicalHistoryPlugin.js'
44
import { $isHeadingNode, HeadingTagType } from '@lexical/rich-text'
55
import { $setBlocksType } from '@lexical/selection'
66
import { $findMatchingParent, $insertNodeToNearestRoot, $wrapNodeInElement } from '@lexical/utils'
7-
import { Cell, NodeRef, Realm, Signal, filter, scan, withLatestFrom } from '@mdxeditor/gurx'
7+
import { Cell, NodeRef, Realm, Signal, filter, map, scan, withLatestFrom } from '@mdxeditor/gurx'
88
import {
99
$createParagraphNode,
1010
$getRoot,
@@ -215,6 +215,18 @@ const markdownSignal$ = Signal<string>((r) => {
215215
r.link(initialMarkdown$, markdown$)
216216
})
217217

218+
const mutableMarkdownSignal$ = Signal<string>((r) => {
219+
r.link(
220+
r.pipe(
221+
markdownSignal$,
222+
withLatestFrom(muteChange$),
223+
filter(([, muted]) => !muted),
224+
map(([value]) => value)
225+
),
226+
mutableMarkdownSignal$
227+
)
228+
})
229+
218230
// import configuration
219231
/** @internal */
220232
export const importVisitors$ = Cell<MdastImportVisitor<Mdast.Nodes>[]>([])
@@ -294,6 +306,7 @@ export const addExportVisitor$ = Appender(exportVisitors$)
294306
*/
295307
export const addToMarkdownExtension$ = Appender(toMarkdownExtensions$)
296308

309+
export const muteChange$ = Cell(false)
297310
/**
298311
* Sets a new markdown value for the editor, replacing the current one.
299312
* @group Core
@@ -308,16 +321,22 @@ export const setMarkdown$ = Signal<string>((r) => {
308321
})
309322
),
310323
([theNewMarkdownValue, , editor, inFocus]) => {
311-
editor?.update(() => {
312-
$getRoot().clear()
313-
tryImportingMarkdown(r, $getRoot(), theNewMarkdownValue)
324+
r.pub(muteChange$, true)
325+
editor?.update(
326+
() => {
327+
$getRoot().clear()
328+
tryImportingMarkdown(r, $getRoot(), theNewMarkdownValue)
314329

315-
if (!inFocus) {
316-
$setSelection(null)
317-
} else {
318-
editor.focus()
330+
if (!inFocus) {
331+
$setSelection(null)
332+
} else {
333+
editor.focus()
334+
}
335+
},
336+
{
337+
onUpdate: () => r.pub(muteChange$, false)
319338
}
320-
})
339+
)
321340
}
322341
)
323342
})
@@ -876,7 +895,7 @@ export const corePlugin = realmPlugin<{
876895
[readOnly$]: params?.readOnly
877896
})
878897

879-
realm.singletonSub(markdownSignal$, params?.onChange)
898+
realm.singletonSub(mutableMarkdownSignal$, params?.onChange)
880899
realm.singletonSub(onBlur$, params?.onBlur)
881900
realm.singletonSub(markdownErrorSignal$, params?.onError)
882901
}

0 commit comments

Comments
 (0)