Skip to content

Commit def7ba6

Browse files
committed
fix #6803 -- cursor jumping
- I'm optimstic finally. - This is also an optimization
1 parent a71f678 commit def7ba6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/packages/frontend/editors/slate/editable-markdown.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ const EXPENSIVE_DEBUG = false;
1111
import { delay } from "awaiting";
1212
import { Map } from "immutable";
1313
import { debounce, isEqual, throttle } from "lodash";
14-
import { MutableRefObject, RefObject } from "react";
15-
1614
import {
17-
CSS,
18-
React,
15+
MutableRefObject,
16+
RefObject,
1917
useCallback,
2018
useEffect,
21-
useIsMountedRef,
2219
useMemo,
2320
useRef,
2421
useState,
25-
} from "@cocalc/frontend/app-framework";
22+
} from "react";
23+
import { CSS, React, useIsMountedRef } from "@cocalc/frontend/app-framework";
2624
import { SubmitMentionsRef } from "@cocalc/frontend/chat/types";
2725
import { useMentionableUsers } from "@cocalc/frontend/editors/markdown-input/mentionable-users";
2826
import { submit_mentions } from "@cocalc/frontend/editors/markdown-input/mentions";
@@ -496,6 +494,8 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
496494
}
497495
}, [value]);
498496

497+
const lastSetValueRef = useRef<string | null>(null);
498+
499499
const setSyncstringFromSlateNOW = () => {
500500
if (actions.set_value == null) {
501501
// 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) => {
508508
}
509509

510510
const markdown = editor.getMarkdownValue();
511+
lastSetValueRef.current = markdown;
511512
actions.set_value(markdown);
512513
actions.syncstring_commit?.();
513514

@@ -581,6 +582,15 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
581582
}, [is_current]);
582583

583584
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+
}
584594
if (value == null) return;
585595
if (value == editor.getMarkdownValue()) {
586596
// nothing to do, and in fact doing something
@@ -647,6 +657,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
647657
// also so we don't update the source editor (and other browsers)
648658
// with a view with things like loan $'s escaped.'
649659
editor.syncCausedUpdate = true;
660+
// console.log("setEditorToValue: applying operations...", { operations });
650661
preserveScrollPosition(editor, operations);
651662
applyOperations(editor, operations);
652663
// console.log("time to set via diff", new Date() - t);
@@ -662,6 +673,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
662673

663674
try {
664675
if (editor.selection != null) {
676+
// console.log("setEditorToValue: restore selection", editor.selection);
665677
const { anchor, focus } = editor.selection;
666678
Editor.node(editor, anchor);
667679
Editor.node(editor, focus);

0 commit comments

Comments
 (0)