Skip to content

Commit 6831ac6

Browse files
committed
work in progress trying to fix issue 6803
- this might be the fix; it seems logical, even if I can't reproduce it.
1 parent 9dd6266 commit 6831ac6

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

src/packages/frontend/editors/slate/elements/codemirror.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export const SlateCodeMirror: React.FC<Props> = React.memo(
129129
...cmOptions?.extraKeys,
130130
"Shift-Enter": () => {
131131
Transforms.move(editor, { distance: 1, unit: "line" });
132-
ReactEditor.focus(editor);
132+
ReactEditor.focus(editor, false, true);
133133
onShiftEnter?.();
134134
},
135135
// We make it so doing select all when not everything is
@@ -147,7 +147,7 @@ export const SlateCodeMirror: React.FC<Props> = React.memo(
147147
if (cmRef.current == null) return;
148148
$(cmRef.current.getWrapperElement()).css(css);
149149
},
150-
[cmRef]
150+
[cmRef],
151151
);
152152

153153
const focusEditor = useCallback(
@@ -169,7 +169,7 @@ export const SlateCodeMirror: React.FC<Props> = React.memo(
169169
ReactEditor.blur(editor);
170170
}
171171
},
172-
[collapsed, options.theme]
172+
[collapsed, options.theme],
173173
);
174174

175175
useEffect(() => {
@@ -326,7 +326,7 @@ export const SlateCodeMirror: React.FC<Props> = React.memo(
326326
{addonAfter}
327327
</div>
328328
);
329-
}
329+
},
330330
);
331331

332332
// TODO: vim version of this...
@@ -342,7 +342,7 @@ function cursorHandlers(editor, isInline: boolean | undefined) {
342342
if (cur_line === n && cur_ch === line_length) {
343343
//Transforms.move(editor, { distance: 1, unit: "line" });
344344
moveCursorDown(editor, true);
345-
ReactEditor.focus(editor);
345+
ReactEditor.focus(editor, false, true);
346346
return true;
347347
} else {
348348
return false;
@@ -358,7 +358,7 @@ function cursorHandlers(editor, isInline: boolean | undefined) {
358358
if (!isInline) {
359359
moveCursorToBeginningOfBlock(editor);
360360
}
361-
ReactEditor.focus(editor);
361+
ReactEditor.focus(editor, false, true);
362362
} else {
363363
commands.goLineUp(cm);
364364
}
@@ -367,7 +367,7 @@ function cursorHandlers(editor, isInline: boolean | undefined) {
367367
const cur = cm.getCursor();
368368
if (cur?.line === cm.firstLine() && cur?.ch == 0) {
369369
Transforms.move(editor, { distance: 1, unit: "line", reverse: true });
370-
ReactEditor.focus(editor);
370+
ReactEditor.focus(editor, false, true);
371371
} else {
372372
commands.goCharLeft(cm);
373373
}

src/packages/frontend/editors/slate/slate-react/plugin/react-editor.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const ReactEditor = {
100100
}
101101

102102
throw new Error(
103-
`Unable to find the path for Slate node: ${JSON.stringify(node)}`
103+
`Unable to find the path for Slate node: ${JSON.stringify(node)}`,
104104
);
105105
},
106106

@@ -137,7 +137,12 @@ export const ReactEditor = {
137137
* Focus the editor.
138138
*/
139139

140-
focus(editor: ReactEditor, force: boolean = false): void {
140+
focus(
141+
editor: ReactEditor,
142+
force: boolean = false,
143+
preserveSelection: boolean = false,
144+
): void {
145+
console.trace("focus!", { force });
141146
const { selection } = editor;
142147
const el = ReactEditor.toDOMNode(editor, editor);
143148
IS_FOCUSED.set(editor, true);
@@ -151,8 +156,12 @@ export const ReactEditor = {
151156
el.blur();
152157
el.focus({ preventScroll: true });
153158
}
154-
if (selection != null) {
155-
// I've changed the focus method to preserve the selection if there is one.
159+
if (selection != null && preserveSelection) {
160+
console.log("setting selection", { selection });
161+
// I've changed the focus method to optionally preserve the selection if there is one.
162+
// However, doing this when not needed may be the cause of
163+
// https://github.com/sagemathinc/cocalc/issues/6803
164+
// hence the preserveSelection parameter.
156165
// Often when the editor not focused there is no selection. However,
157166
// in some cases, e.g., "set the selection, then focus", like we
158167
// do when using commands to move the cursors out of editing a void element,
@@ -186,7 +195,7 @@ export const ReactEditor = {
186195
hasDOMNode(
187196
editor: ReactEditor,
188197
target: DOMNode,
189-
options: { editable?: boolean } = {}
198+
options: { editable?: boolean } = {},
190199
): boolean {
191200
const { editable = false } = options;
192201
const editorEl = ReactEditor.toDOMNode(editor, editor);
@@ -245,7 +254,7 @@ export const ReactEditor = {
245254
} catch (err) {
246255
console.warn(
247256
"SLATE: problem in setFragementData (invalid selection; so ignoring)",
248-
err
257+
err,
249258
);
250259
}
251260
},
@@ -267,7 +276,7 @@ export const ReactEditor = {
267276

268277
if (!domNode) {
269278
throw new Error(
270-
`Cannot resolve a DOM node from Slate node: ${JSON.stringify(node)}`
279+
`Cannot resolve a DOM node from Slate node: ${JSON.stringify(node)}`,
271280
);
272281
}
273282

@@ -319,7 +328,7 @@ export const ReactEditor = {
319328

320329
if (!domPoint) {
321330
throw new Error(
322-
`Cannot resolve a DOM point from Slate point: ${JSON.stringify(point)}`
331+
`Cannot resolve a DOM point from Slate point: ${JSON.stringify(point)}`,
323332
);
324333
}
325334

@@ -398,7 +407,7 @@ export const ReactEditor = {
398407

399408
if (x == null || y == null) {
400409
throw new Error(
401-
`Cannot resolve a Slate range from a DOM event: ${event}`
410+
`Cannot resolve a Slate range from a DOM event: ${event}`,
402411
);
403412
}
404413

@@ -446,7 +455,7 @@ export const ReactEditor = {
446455

447456
if (!domRange) {
448457
throw new Error(
449-
`Cannot resolve a Slate range from a DOM event: ${event}`
458+
`Cannot resolve a Slate range from a DOM event: ${event}`,
450459
);
451460
}
452461

@@ -521,7 +530,7 @@ export const ReactEditor = {
521530

522531
if (!textNode) {
523532
throw new Error(
524-
`Cannot resolve a Slate point from DOM point: ${domPoint}`
533+
`Cannot resolve a Slate point from DOM point: ${domPoint}`,
525534
);
526535
}
527536

@@ -539,7 +548,7 @@ export const ReactEditor = {
539548

540549
toSlateRange(
541550
editor: ReactEditor,
542-
domRange: DOMRange | DOMStaticRange | DOMSelection
551+
domRange: DOMRange | DOMStaticRange | DOMSelection,
543552
): Range {
544553
const el =
545554
domRange instanceof Selection
@@ -574,7 +583,7 @@ export const ReactEditor = {
574583
focusOffset == null
575584
) {
576585
throw new Error(
577-
`Cannot resolve a Slate range from DOM range: ${domRange}`
586+
`Cannot resolve a Slate range from DOM range: ${domRange}`,
578587
);
579588
}
580589

0 commit comments

Comments
 (0)