Skip to content

Commit eb74334

Browse files
committed
save debounce wasn't properly honored in some cases for slate editor
1 parent 4bbdcca commit eb74334

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/packages/frontend/editors/markdown-input/multimode.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ interface Props {
8585
submitMentionsRef?: SubmitMentionsRef;
8686
extraHelp?: ReactNode;
8787
hideHelp?: boolean;
88-
saveDebounceMs?: number; // debounce how frequently get updates from onChange; if saveDebounceMs=0 get them on every change. Default is the global SAVE_DEBOUNCE_MS const.
88+
// debounce how frequently get updates from onChange; if saveDebounceMs=0 get them on every change. Default is the global SAVE_DEBOUNCE_MS const.
89+
// can be a little more frequent in case of shift or alt enter, or blur.
90+
saveDebounceMs?: number;
8991
onBlur?: () => void;
9092
onFocus?: () => void;
9193
minimal?: boolean;

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -496,24 +496,31 @@ export const EditableMarkdown: React.FC<Props> = React.memo((props: Props) => {
496496
}
497497
}, [value]);
498498

499-
function setSyncstringFromSlate() {
500-
if (actions.set_value == null) {
501-
// no way to save the value out (e.g., just beginning to test
502-
// using the component).
503-
return;
504-
}
505-
if (!editor.hasUnsavedChanges()) {
506-
// there are no changes to save
507-
return;
508-
}
499+
const setSyncstringFromSlate = useMemo(() => {
500+
const f = () => {
501+
if (actions.set_value == null) {
502+
// no way to save the value out (e.g., just beginning to test
503+
// using the component).
504+
return;
505+
}
506+
if (!editor.hasUnsavedChanges()) {
507+
// there are no changes to save
508+
return;
509+
}
509510

510-
const markdown = editor.getMarkdownValue();
511-
actions.set_value(markdown);
512-
actions.syncstring_commit?.();
511+
const markdown = editor.getMarkdownValue();
512+
actions.set_value(markdown);
513+
actions.syncstring_commit?.();
513514

514-
// Record that the syncstring's value is now equal to ours:
515-
editor.resetHasUnsavedChanges();
516-
}
515+
// Record that the syncstring's value is now equal to ours:
516+
editor.resetHasUnsavedChanges();
517+
};
518+
if (saveDebounceMs) {
519+
return debounce(f, saveDebounceMs);
520+
} else {
521+
return f;
522+
}
523+
}, []);
517524

518525
// We don't want to do saveValue too much, since it presumably can be slow,
519526
// especially if the document is large. By debouncing, we only do this when

src/packages/frontend/editors/slate/sync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function slatePointToMarkdown(
3939
try {
4040
[node] = Editor.node(editor, point);
4141
} catch (err) {
42-
console.warn(`slate -- invalid point ${JSON.stringify(point)} -- ${err}`);
42+
// console.warn(`slate -- invalid point ${JSON.stringify(point)} -- ${err}`);
4343
// There is no guarantee that point is valid when this is called.
4444
return { index: -1, markdown: "" };
4545
}

0 commit comments

Comments
 (0)