Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/keyboard/spacebar.ts
1697 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
What happens when you hit the space bar.
8
9
We also make shift+space do autoformat for the following reason:
10
"We also **allow** shift+space for autoformat, since it is easy to accidentally
11
type! E.g., trying typing "> " quickly a few times on a US keyboard, and you'll
12
find that > is shift+., and you often don't lift off the shift before hitting space."
13
and to implement it include { key: " ", shift: true } in the list below.
14
However, I think overall it's more valuable to allow shift+space to insert a space
15
without autoformat, since there must be an easy way to do this.
16
17
To insert a space without formatting, use some other modifier that your browser or
18
OS doesn't take. On MacOS that includes option+space for now.
19
TODO: instead, anytime autoformat happens, there could be an indicator about it in the
20
toolbar, with a button to undo it (leaving the space). This would be general for
21
return as well.
22
23
IMPORTANT: we also explicitly do this same insertText action in
24
frontend/editors/slate/slate-react/components/editable.tsx
25
to handle virtual keyboards. See https://github.com/sagemathinc/cocalc/issues/8536
26
*/
27
28
import { toggleCheckbox } from "../elements/checkbox/editable";
29
import { register } from "./register";
30
31
register([{ key: " " }, { key: " ", shift: true }], ({ editor }) => {
32
if (toggleCheckbox(editor)) {
33
return true;
34
}
35
36
// @ts-ignore - that second argument below is "unsanctioned"; it controls
37
// whether autoformat should happen.
38
editor.insertText(" ", true);
39
return true;
40
});
41
42