Skip to content

Commit dab3b5a

Browse files
committed
slate chat -- shift+enter to submit; alt+enter to switch to editing markdown.
1 parent fc072d2 commit dab3b5a

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,22 @@ export default function MultiMarkdownInput({
123123
padding: "5px 15px",
124124
height: height ?? "100%",
125125
}}
126+
saveDebounceMs={0}
126127
actions={{
127128
set_value: (value) => {
128129
onChange?.(value);
129130
},
131+
shiftEnter:
132+
onShiftEnter == null
133+
? undefined
134+
: (value) => {
135+
onChange?.(value);
136+
onShiftEnter();
137+
},
138+
altEnter: (value) => {
139+
onChange?.(value);
140+
setMode("markdown");
141+
},
130142
}}
131143
font_size={fontSize}
132144
autoFocus={autoFocus}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ interface Props {
119119
onBlur?: () => void;
120120
autoFocus?: boolean;
121121
hideSearch?: boolean;
122+
saveDebounceMs?: number;
122123
}
123124

124125
export const EditableMarkdown: React.FC<Props> = React.memo(
@@ -141,6 +142,7 @@ export const EditableMarkdown: React.FC<Props> = React.memo(
141142
onBlur,
142143
autoFocus,
143144
hideSearch,
145+
saveDebounceMs,
144146
}) => {
145147
if (disableWindowing == null) {
146148
disableWindowing = !USE_WINDOWING;
@@ -322,10 +324,18 @@ export const EditableMarkdown: React.FC<Props> = React.memo(
322324
// We don't want to do saveValue too much, since it presumably can be slow,
323325
// especially if the document is large. By debouncing, we only do this when
324326
// the user pauses typing for a moment. Also, this avoids making too many commits.
325-
const saveValueDebounce = useMemo(
326-
() => debounce(() => editor.saveValue(), SAVE_DEBOUNCE_MS),
327-
[]
328-
);
327+
// For tiny documents, user can make this small or even 0 to not dbounce.
328+
const saveValueDebounce =
329+
saveDebounceMs != null && !saveDebounceMs
330+
? () => editor.saveValue()
331+
: useMemo(
332+
() =>
333+
debounce(
334+
() => editor.saveValue(),
335+
saveDebounceMs ?? SAVE_DEBOUNCE_MS
336+
),
337+
[]
338+
);
329339

330340
function onKeyDown(e) {
331341
if (read_only) {

src/packages/frontend/editors/slate/keyboard/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ register(
6969
return true;
7070
}
7171
);
72+

src/packages/frontend/editors/slate/keyboard/shift-enter.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ import { register } from "./register";
1111
import { hardbreak } from "../elements/break";
1212
import { isWhitespaceParagraph, isWhitespaceText } from "../padding";
1313

14-
register({ key: "Enter", shift: true }, ({ editor }) => {
14+
register({ key: "Enter", shift: true }, ({ editor, extra }) => {
15+
// Configured editor so shift+enter does some action, e.g., "submit chat".
16+
// In this case, we do that instead of the various things below involving
17+
// newlines, which can instead be done with control+enter.
18+
const shiftEnter = extra?.actions?.shiftEnter;
19+
if (shiftEnter != null) {
20+
shiftEnter(editor.getMarkdownValue());
21+
return true;
22+
}
23+
return softBreak({ editor });
24+
});
25+
26+
function softBreak({ editor }) {
1527
// In a table, the only option is to insert a <br/>.
1628
const fragment = editor.getFragment();
1729
if (isElementOfType(fragment?.[0], "table")) {
@@ -50,4 +62,6 @@ register({ key: "Enter", shift: true }, ({ editor }) => {
5062
Transforms.insertNodes(editor, [hardbreak()]);
5163
Transforms.move(editor, { distance: 1 });
5264
return true;
53-
});
65+
}
66+
67+
register({ key: "Enter", ctrl: true }, softBreak);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ register(
1010
{ key: "Enter", alt: true },
1111
{ key: "Enter", meta: true },
1212
],
13-
({ editor }) => {
13+
({ editor, extra }) => {
14+
const altEnter = extra?.actions?.altEnter;
15+
if (altEnter != null) {
16+
altEnter(editor.getMarkdownValue());
17+
return true;
18+
}
1419
editor.inverseSearch(true);
1520
return true;
1621
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ export interface Actions {
4646
change_font_size?: (delta?: number, id?: string, zoom?: number) => void;
4747
undo?: (id: string) => void;
4848
redo?: (id: string) => void;
49+
shiftEnter?: (value: string) => void;
50+
altEnter?: (value: string) => void;
4951
}

0 commit comments

Comments
 (0)