@@ -11,18 +11,16 @@ const EXPENSIVE_DEBUG = false;
11
11
import { delay } from "awaiting" ;
12
12
import { Map } from "immutable" ;
13
13
import { debounce , isEqual , throttle } from "lodash" ;
14
- import { MutableRefObject , RefObject } from "react" ;
15
-
16
14
import {
17
- CSS ,
18
- React ,
15
+ MutableRefObject ,
16
+ RefObject ,
19
17
useCallback ,
20
18
useEffect ,
21
- useIsMountedRef ,
22
19
useMemo ,
23
20
useRef ,
24
21
useState ,
25
- } from "@cocalc/frontend/app-framework" ;
22
+ } from "react" ;
23
+ import { CSS , React , useIsMountedRef } from "@cocalc/frontend/app-framework" ;
26
24
import { SubmitMentionsRef } from "@cocalc/frontend/chat/types" ;
27
25
import { useMentionableUsers } from "@cocalc/frontend/editors/markdown-input/mentionable-users" ;
28
26
import { submit_mentions } from "@cocalc/frontend/editors/markdown-input/mentions" ;
@@ -496,6 +494,8 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
496
494
}
497
495
} , [ value ] ) ;
498
496
497
+ const lastSetValueRef = useRef < string | null > ( null ) ;
498
+
499
499
const setSyncstringFromSlateNOW = ( ) => {
500
500
if ( actions . set_value == null ) {
501
501
// no way to save the value out (e.g., just beginning to test
@@ -508,6 +508,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
508
508
}
509
509
510
510
const markdown = editor . getMarkdownValue ( ) ;
511
+ lastSetValueRef . current = markdown ;
511
512
actions . set_value ( markdown ) ;
512
513
actions . syncstring_commit ?.( ) ;
513
514
@@ -581,6 +582,15 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
581
582
} , [ is_current ] ) ;
582
583
583
584
const setEditorToValue = ( value ) => {
585
+ // console.log("setEditorToValue", { value, ed: editor.getMarkdownValue() });
586
+ if ( lastSetValueRef . current == value ) {
587
+ // this always happens once right after calling setSyncstringFromSlateNOW
588
+ // and it can randomly undo the last thing done, so don't do that!
589
+ // Also, this is an excellent optimization to do as well.
590
+ lastSetValueRef . current = null ;
591
+ // console.log("setEditorToValue: skip");
592
+ return ;
593
+ }
584
594
if ( value == null ) return ;
585
595
if ( value == editor . getMarkdownValue ( ) ) {
586
596
// nothing to do, and in fact doing something
@@ -647,6 +657,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
647
657
// also so we don't update the source editor (and other browsers)
648
658
// with a view with things like loan $'s escaped.'
649
659
editor . syncCausedUpdate = true ;
660
+ // console.log("setEditorToValue: applying operations...", { operations });
650
661
preserveScrollPosition ( editor , operations ) ;
651
662
applyOperations ( editor , operations ) ;
652
663
// console.log("time to set via diff", new Date() - t);
@@ -662,6 +673,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
662
673
663
674
try {
664
675
if ( editor . selection != null ) {
676
+ // console.log("setEditorToValue: restore selection", editor.selection);
665
677
const { anchor, focus } = editor . selection ;
666
678
Editor . node ( editor , anchor ) ;
667
679
Editor . node ( editor , focus ) ;
0 commit comments