@@ -4,7 +4,7 @@ import { createEmptyHistoryState } from '@lexical/react/LexicalHistoryPlugin.js'
4
4
import { $isHeadingNode , HeadingTagType } from '@lexical/rich-text'
5
5
import { $setBlocksType } from '@lexical/selection'
6
6
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'
8
8
import {
9
9
$createParagraphNode ,
10
10
$getRoot ,
@@ -215,6 +215,18 @@ const markdownSignal$ = Signal<string>((r) => {
215
215
r . link ( initialMarkdown$ , markdown$ )
216
216
} )
217
217
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
+
218
230
// import configuration
219
231
/** @internal */
220
232
export const importVisitors$ = Cell < MdastImportVisitor < Mdast . Nodes > [ ] > ( [ ] )
@@ -294,6 +306,7 @@ export const addExportVisitor$ = Appender(exportVisitors$)
294
306
*/
295
307
export const addToMarkdownExtension$ = Appender ( toMarkdownExtensions$ )
296
308
309
+ export const muteChange$ = Cell ( false )
297
310
/**
298
311
* Sets a new markdown value for the editor, replacing the current one.
299
312
* @group Core
@@ -308,16 +321,22 @@ export const setMarkdown$ = Signal<string>((r) => {
308
321
} )
309
322
) ,
310
323
( [ 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 )
314
329
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 )
319
338
}
320
- } )
339
+ )
321
340
}
322
341
)
323
342
} )
@@ -876,7 +895,7 @@ export const corePlugin = realmPlugin<{
876
895
[ readOnly$ ] : params ?. readOnly
877
896
} )
878
897
879
- realm . singletonSub ( markdownSignal $, params ?. onChange )
898
+ realm . singletonSub ( mutableMarkdownSignal $, params ?. onChange )
880
899
realm . singletonSub ( onBlur$ , params ?. onBlur )
881
900
realm . singletonSub ( markdownErrorSignal$ , params ?. onError )
882
901
}
0 commit comments