@dxos/react-ui-editor 0.3.11-main.e8c477a → 0.3.11-main.ea1c9ce
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +1754 -1305
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +16 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts +23 -0
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +0 -1
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +37 -20
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/{util → extensions}/cursor.d.ts +10 -2
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/index.d.ts +2 -7
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts +13 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
- package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +8 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +4 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -32
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts +2 -2
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +48 -37
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +84 -90
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +135 -95
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- package/src/components/TextEditor/hooks.stories.tsx +106 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
- package/src/components/Toolbar/Toolbar.tsx +166 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +55 -46
- package/src/extensions/automerge/cursor.ts +5 -4
- package/src/extensions/automerge/defs.ts +13 -34
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +33 -32
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +46 -49
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +320 -264
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/index.ts +2 -7
- package/src/extensions/markdown/bundle.ts +23 -28
- package/src/extensions/markdown/code.ts +150 -0
- package/src/extensions/markdown/formatting.ts +188 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -23
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/{image.ts → markdown/image.ts} +2 -2
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +20 -11
- package/src/extensions/{table.ts → markdown/table.ts} +2 -2
- package/src/extensions/markdown/tasklist.ts +110 -0
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +5 -2
- package/src/hooks/useActionHandler.ts +62 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +153 -0
- package/src/hooks/useTextModel.ts +67 -60
- package/src/index.ts +1 -0
- package/src/styles/markdown.ts +9 -12
- package/src/themes/default.ts +24 -54
- package/src/util.ts +41 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- package/dist/types/src/extensions/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/dist/types/src/util/cursor.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -3
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/util.d.ts +0 -5
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -98
- package/src/extensions/hr.ts +0 -73
- package/src/extensions/tasklist.ts +0 -119
- package/src/extensions/yjs/cursor.ts +0 -21
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -178
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- package/src/util/util.ts +0 -18
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type EditorState, Facet } from '@codemirror/state';
|
|
6
|
+
|
|
7
|
+
import type { Range } from '../hooks';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts indexes into the text document into stable peer-independent cursors.
|
|
11
|
+
*
|
|
12
|
+
* See:
|
|
13
|
+
* - https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
14
|
+
* - https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
15
|
+
*
|
|
16
|
+
* @param {assoc} number Negative values will associate the cursor with the previous character
|
|
17
|
+
* while positive - with the next one.
|
|
18
|
+
*/
|
|
19
|
+
export interface CursorConverter {
|
|
20
|
+
toCursor(position: number, assoc?: -1 | 1 | undefined): string;
|
|
21
|
+
fromCursor(cursor: string): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const defaultCursorConverter: CursorConverter = {
|
|
25
|
+
toCursor: (position) => position.toString(),
|
|
26
|
+
fromCursor: (cursor) => parseInt(cursor),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export class Cursor {
|
|
30
|
+
static readonly converter = Facet.define<CursorConverter, CursorConverter>({
|
|
31
|
+
combine: (providers) => providers[0] ?? defaultCursorConverter,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
static readonly getCursorFromRange = (state: EditorState, range: Range) => {
|
|
35
|
+
const cursorConverter = state.facet(Cursor.converter);
|
|
36
|
+
|
|
37
|
+
const from = cursorConverter.toCursor(range.from);
|
|
38
|
+
const to = cursorConverter.toCursor(range.to, -1);
|
|
39
|
+
return [from, to].join(':');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
static readonly getRangeFromCursor = (state: EditorState, cursor: string) => {
|
|
43
|
+
const cursorConverter = state.facet(Cursor.converter);
|
|
44
|
+
|
|
45
|
+
const parts = cursor.split(':');
|
|
46
|
+
const from = cursorConverter.fromCursor(parts[0]);
|
|
47
|
+
const to = cursorConverter.fromCursor(parts[1]);
|
|
48
|
+
return from !== undefined && to !== undefined ? { from, to } : undefined;
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/extensions/index.ts
CHANGED
|
@@ -7,15 +7,10 @@ export * from './automerge';
|
|
|
7
7
|
export * from './awareness';
|
|
8
8
|
export * from './basic';
|
|
9
9
|
export * from './blast';
|
|
10
|
-
export * from './code';
|
|
11
10
|
export * from './comments';
|
|
12
|
-
export * from './
|
|
13
|
-
export * from './image';
|
|
14
|
-
export * from './link';
|
|
11
|
+
export * from './cursor';
|
|
15
12
|
export * from './listener';
|
|
16
13
|
export * from './markdown';
|
|
17
14
|
export * from './mention';
|
|
18
|
-
export * from './
|
|
19
|
-
export * from './tasklist';
|
|
15
|
+
export * from './outliner';
|
|
20
16
|
export * from './typewriter';
|
|
21
|
-
export * from './yjs';
|
|
@@ -21,36 +21,33 @@ import {
|
|
|
21
21
|
} from '@codemirror/view';
|
|
22
22
|
|
|
23
23
|
import type { ThemeMode } from '@dxos/react-ui';
|
|
24
|
+
import { isNotFalsy } from '@dxos/util';
|
|
24
25
|
|
|
25
26
|
import { markdownHighlightStyle, markdownTagsExtensions } from './highlight';
|
|
27
|
+
import { type TextEditorProps } from '../../components';
|
|
26
28
|
|
|
27
29
|
export type MarkdownBundleOptions = {
|
|
28
|
-
readonly?: boolean;
|
|
29
30
|
themeMode?: ThemeMode;
|
|
30
|
-
|
|
31
|
-
};
|
|
31
|
+
} & Pick<TextEditorProps, 'placeholder' | 'lineWrapping'>;
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Markdown bundle.
|
|
35
35
|
* Refs:
|
|
36
|
-
* https://github.com/codemirror/basic-setup
|
|
37
|
-
* https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts#L50
|
|
38
36
|
* https://codemirror.net/docs/community
|
|
39
37
|
* https://codemirror.net/docs/ref/#codemirror.basicSetup
|
|
40
38
|
*/
|
|
41
39
|
// TODO(burdon): Add Composer here: https://codemirror.net/docs/community
|
|
42
|
-
export const
|
|
43
|
-
readonly,
|
|
40
|
+
export const createMarkdownExtensions = ({
|
|
44
41
|
themeMode,
|
|
45
42
|
placeholder: _placeholder,
|
|
46
|
-
|
|
43
|
+
lineWrapping = true,
|
|
44
|
+
}: MarkdownBundleOptions = {}): Extension[] => {
|
|
47
45
|
return [
|
|
46
|
+
lineWrapping && EditorView.lineWrapping,
|
|
48
47
|
EditorState.allowMultipleSelections.of(true),
|
|
49
48
|
EditorState.tabSize.of(2),
|
|
50
|
-
EditorView.lineWrapping,
|
|
51
|
-
|
|
52
|
-
customKeymap(),
|
|
53
49
|
|
|
50
|
+
// https://github.com/codemirror/basic-setup
|
|
54
51
|
bracketMatching(),
|
|
55
52
|
closeBrackets(),
|
|
56
53
|
crosshairCursor(),
|
|
@@ -86,21 +83,19 @@ export const markdownBundle = ({
|
|
|
86
83
|
themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
|
|
87
84
|
|
|
88
85
|
// Custom styles.
|
|
89
|
-
syntaxHighlighting(markdownHighlightStyle(
|
|
90
|
-
].filter(Boolean) as Extension[];
|
|
91
|
-
};
|
|
86
|
+
syntaxHighlighting(markdownHighlightStyle()),
|
|
92
87
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
88
|
+
keymap.of([
|
|
89
|
+
// https://codemirror.net/docs/ref/#commands.indentWithTab
|
|
90
|
+
indentWithTab,
|
|
91
|
+
// https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
|
|
92
|
+
...closeBracketsKeymap,
|
|
93
|
+
// https://codemirror.net/docs/ref/#commands.historyKeymap
|
|
94
|
+
...historyKeymap,
|
|
95
|
+
// https://codemirror.net/docs/ref/#search.searchKeymap
|
|
96
|
+
...searchKeymap,
|
|
97
|
+
// https://codemirror.net/docs/ref/#commands.standardKeymap
|
|
98
|
+
...standardKeymap,
|
|
99
|
+
]),
|
|
100
|
+
].filter(isNotFalsy);
|
|
101
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import {
|
|
8
|
+
ViewPlugin,
|
|
9
|
+
type DecorationSet,
|
|
10
|
+
EditorView,
|
|
11
|
+
type ViewUpdate,
|
|
12
|
+
Decoration,
|
|
13
|
+
type BlockInfo,
|
|
14
|
+
WidgetType,
|
|
15
|
+
} from '@codemirror/view';
|
|
16
|
+
|
|
17
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
18
|
+
|
|
19
|
+
import { getToken } from '../../styles';
|
|
20
|
+
|
|
21
|
+
// TODO(burdon): Reconcile with theme.
|
|
22
|
+
const styles = EditorView.baseTheme({
|
|
23
|
+
'& .cm-code': {
|
|
24
|
+
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
25
|
+
},
|
|
26
|
+
'& .cm-codeblock': {
|
|
27
|
+
width: '100%',
|
|
28
|
+
paddingInline: '1rem !important',
|
|
29
|
+
position: 'relative',
|
|
30
|
+
'&::after': {
|
|
31
|
+
content: '""',
|
|
32
|
+
position: 'absolute',
|
|
33
|
+
inset: 0,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
'&light .cm-codeblock': {
|
|
37
|
+
'&::after': {
|
|
38
|
+
background: getToken('extend.semanticColors.input.light'),
|
|
39
|
+
mixBlendMode: 'darken',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
'&dark .cm-codeblock': {
|
|
43
|
+
'&::after': {
|
|
44
|
+
background: getToken('extend.semanticColors.input.dark'),
|
|
45
|
+
mixBlendMode: 'lighten',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
'& .cm-codeblock-first': {
|
|
49
|
+
display: 'inline-block',
|
|
50
|
+
'&::after': {
|
|
51
|
+
borderTopLeftRadius: '.5rem',
|
|
52
|
+
borderTopRightRadius: '.5rem',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
'& .cm-codeblock-last': {
|
|
56
|
+
display: 'inline-block',
|
|
57
|
+
'&::after': {
|
|
58
|
+
borderBottomLeftRadius: '.5rem',
|
|
59
|
+
borderBottomRightRadius: '.5rem',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
65
|
+
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
66
|
+
const start = lines.findIndex((line) => line.from >= from);
|
|
67
|
+
const end = lines.findIndex((line) => line.to >= to);
|
|
68
|
+
return [start, end];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// TODO(burdon): Add copy to clipboard widget.
|
|
72
|
+
class LineWidget extends WidgetType {
|
|
73
|
+
constructor(private readonly _className: string) {
|
|
74
|
+
super();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
override toDOM() {
|
|
78
|
+
const el = document.createElement('span');
|
|
79
|
+
el.innerHTML = ' ';
|
|
80
|
+
el.className = mx('cm-code cm-codeblock', this._className);
|
|
81
|
+
return el;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const top = new LineWidget('cm-codeblock-first');
|
|
86
|
+
const bottom = new LineWidget('cm-codeblock-last');
|
|
87
|
+
|
|
88
|
+
// TODO(burdon): Selection isn't visible unless multiline (e.g., can't highlight word).
|
|
89
|
+
|
|
90
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
91
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
92
|
+
const { state } = view;
|
|
93
|
+
const blocks = view.viewportLineBlocks;
|
|
94
|
+
const cursor = state.selection.main.head;
|
|
95
|
+
|
|
96
|
+
for (const { from, to } of view.visibleRanges) {
|
|
97
|
+
syntaxTree(state).iterate({
|
|
98
|
+
enter: (node) => {
|
|
99
|
+
if (node.name === 'FencedCode') {
|
|
100
|
+
// FencedCode > CodeMark > [CodeInfo] > CodeText > CodeMark
|
|
101
|
+
const edit = !view.state.readOnly && cursor >= node.from && cursor <= node.to;
|
|
102
|
+
const range = getLineRange(blocks, node.from, node.to);
|
|
103
|
+
for (let i = range[0]; i <= range[1]; i++) {
|
|
104
|
+
const block = blocks[i];
|
|
105
|
+
if (!edit && (i === range[0] || i === range[1])) {
|
|
106
|
+
builder.add(block.from, block.to, Decoration.replace({ widget: i === range[0] ? top : bottom }));
|
|
107
|
+
} else {
|
|
108
|
+
builder.add(
|
|
109
|
+
block.from,
|
|
110
|
+
block.from,
|
|
111
|
+
Decoration.line({
|
|
112
|
+
class: mx(
|
|
113
|
+
'cm-code cm-codeblock',
|
|
114
|
+
i === range[0] && 'cm-codeblock-first',
|
|
115
|
+
i === range[1] && 'cm-codeblock-last',
|
|
116
|
+
),
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
from,
|
|
124
|
+
to,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return builder.finish();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const code = () => {
|
|
132
|
+
return [
|
|
133
|
+
ViewPlugin.fromClass(
|
|
134
|
+
class {
|
|
135
|
+
decorations: DecorationSet;
|
|
136
|
+
constructor(view: EditorView) {
|
|
137
|
+
this.decorations = buildDecorations(view);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
update(update: ViewUpdate) {
|
|
141
|
+
this.decorations = buildDecorations(update.view);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
decorations: (value) => value.decorations,
|
|
146
|
+
},
|
|
147
|
+
),
|
|
148
|
+
styles,
|
|
149
|
+
];
|
|
150
|
+
};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { snippet } from '@codemirror/autocomplete';
|
|
6
|
+
import { syntaxTree } from '@codemirror/language';
|
|
7
|
+
import { type Extension, RangeSetBuilder } from '@codemirror/state';
|
|
8
|
+
import {
|
|
9
|
+
type Command,
|
|
10
|
+
Decoration,
|
|
11
|
+
type DecorationSet,
|
|
12
|
+
type EditorView,
|
|
13
|
+
keymap,
|
|
14
|
+
ViewPlugin,
|
|
15
|
+
type ViewUpdate,
|
|
16
|
+
} from '@codemirror/view';
|
|
17
|
+
import { type SyntaxNodeRef, type SyntaxNode } from '@lezer/common';
|
|
18
|
+
|
|
19
|
+
export type FormattingOptions = {};
|
|
20
|
+
|
|
21
|
+
export const setHeading =
|
|
22
|
+
(level: number): Command =>
|
|
23
|
+
(view: EditorView) => {
|
|
24
|
+
const {
|
|
25
|
+
selection: { ranges },
|
|
26
|
+
doc,
|
|
27
|
+
} = view.state;
|
|
28
|
+
const changes = [];
|
|
29
|
+
for (const range of ranges) {
|
|
30
|
+
const { number } = doc.lineAt(range.anchor);
|
|
31
|
+
const { from, to } = doc.line(number);
|
|
32
|
+
|
|
33
|
+
// Check heading doesn't already exist.
|
|
34
|
+
const line = doc.sliceString(from, to);
|
|
35
|
+
const [_, marks, spaces] = line.match(/(#+)(\s+)/) ?? [];
|
|
36
|
+
const current = marks?.length ?? 0;
|
|
37
|
+
if (level !== current) {
|
|
38
|
+
changes.push({
|
|
39
|
+
from,
|
|
40
|
+
to: from + current + (spaces?.length ?? 0),
|
|
41
|
+
insert: '#'.repeat(level) + (level > 0 ? ' ' : ''),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (changes.length) {
|
|
47
|
+
view.dispatch({ changes });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const toggleStyle =
|
|
54
|
+
(mark: string): Command =>
|
|
55
|
+
(view) => {
|
|
56
|
+
const { ranges } = view.state.selection;
|
|
57
|
+
for (const range of ranges) {
|
|
58
|
+
if (range.from === range.to) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// TODO(burdon): Detect if already styled (or nested).
|
|
63
|
+
view.dispatch({
|
|
64
|
+
changes: [
|
|
65
|
+
{
|
|
66
|
+
from: range.from,
|
|
67
|
+
insert: mark,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
from: range.to,
|
|
71
|
+
insert: mark,
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// TODO(burdon): Define and trigger snippets for codeblock, table, etc.
|
|
81
|
+
const snippets = {
|
|
82
|
+
codeblock: snippet(['```#{lang}', '\t#{}', '```'].join('\n')),
|
|
83
|
+
table: snippet(
|
|
84
|
+
['| #{col1} | #{col2} |', '| ---- | ---- |', '| #{val1} | #{val2} |', '| #{val3} | #{val4} |'].join('\n'),
|
|
85
|
+
),
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const insertCodeblock = (view: EditorView) => {
|
|
89
|
+
const {
|
|
90
|
+
selection: { main },
|
|
91
|
+
doc,
|
|
92
|
+
} = view.state;
|
|
93
|
+
const { number } = doc.lineAt(main.anchor);
|
|
94
|
+
const { from } = doc.line(number);
|
|
95
|
+
snippets.codeblock(view, null, from, from);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const insertTable = (view: EditorView) => {
|
|
99
|
+
const {
|
|
100
|
+
selection: { main },
|
|
101
|
+
doc,
|
|
102
|
+
} = view.state;
|
|
103
|
+
const { number } = doc.lineAt(main.anchor);
|
|
104
|
+
const { from } = doc.line(number);
|
|
105
|
+
snippets.table(view, null, from, from);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const toggleBold = toggleStyle('**');
|
|
109
|
+
export const toggleItalic = toggleStyle('_');
|
|
110
|
+
export const toggleStrikethrough = toggleStyle('~~');
|
|
111
|
+
|
|
112
|
+
export const toggleList = (view: EditorView) => {};
|
|
113
|
+
|
|
114
|
+
export const formatting = (options: FormattingOptions = {}): Extension => {
|
|
115
|
+
return [
|
|
116
|
+
keymap.of([
|
|
117
|
+
{
|
|
118
|
+
key: 'meta-b',
|
|
119
|
+
run: toggleBold,
|
|
120
|
+
},
|
|
121
|
+
]),
|
|
122
|
+
styling(),
|
|
123
|
+
];
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// https://github.github.com/gfm
|
|
127
|
+
// https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax
|
|
128
|
+
|
|
129
|
+
const styling = (): Extension => {
|
|
130
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
131
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
132
|
+
const { state } = view;
|
|
133
|
+
const cursor = state.selection.main.head;
|
|
134
|
+
|
|
135
|
+
// TODO(burdon): Bug if '***foo***' (since StrongEmphasis is nested inside EmphasisMark).
|
|
136
|
+
// Ranges must be added sorted by `from` position and `startSide`.
|
|
137
|
+
const replace = (node: SyntaxNodeRef, marks: SyntaxNode[]) => {
|
|
138
|
+
if (cursor <= node.from || cursor >= node.to) {
|
|
139
|
+
for (const mark of marks) {
|
|
140
|
+
builder.add(mark.from, mark.to, Decoration.replace({}));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
for (const { from, to } of view.visibleRanges) {
|
|
146
|
+
syntaxTree(state).iterate({
|
|
147
|
+
enter: (node) => {
|
|
148
|
+
switch (node.name) {
|
|
149
|
+
case 'Emphasis':
|
|
150
|
+
case 'StrongEmphasis': {
|
|
151
|
+
const marks = node.node.getChildren('EmphasisMark');
|
|
152
|
+
replace(node, marks);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
case 'Strikethrough': {
|
|
157
|
+
const marks = node.node.getChildren('StrikethroughMark');
|
|
158
|
+
replace(node, marks);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
from,
|
|
164
|
+
to,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return builder.finish();
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
return [
|
|
172
|
+
ViewPlugin.fromClass(
|
|
173
|
+
class {
|
|
174
|
+
decorations: DecorationSet;
|
|
175
|
+
constructor(view: EditorView) {
|
|
176
|
+
this.decorations = buildDecorations(view);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
update(update: ViewUpdate) {
|
|
180
|
+
this.decorations = buildDecorations(update.view);
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
decorations: (value) => value.decorations,
|
|
185
|
+
},
|
|
186
|
+
),
|
|
187
|
+
];
|
|
188
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, type EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
10
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
11
|
+
const { state } = view;
|
|
12
|
+
const cursor = state.selection.main.head;
|
|
13
|
+
|
|
14
|
+
for (const { from, to } of view.visibleRanges) {
|
|
15
|
+
syntaxTree(state).iterate({
|
|
16
|
+
enter: (node) => {
|
|
17
|
+
switch (node.name) {
|
|
18
|
+
case 'ATXHeading1':
|
|
19
|
+
case 'ATXHeading2':
|
|
20
|
+
case 'ATXHeading3':
|
|
21
|
+
case 'ATXHeading4':
|
|
22
|
+
case 'ATXHeading5':
|
|
23
|
+
case 'ATXHeading6': {
|
|
24
|
+
const mark = node.node.getChild('HeaderMark');
|
|
25
|
+
if (mark) {
|
|
26
|
+
if (!view.hasFocus || view.state.readOnly || cursor < node.from || cursor > node.to) {
|
|
27
|
+
builder.add(mark.from, mark.to + 1, Decoration.replace({}));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
from,
|
|
34
|
+
to,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return builder.finish();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const heading = () => {
|
|
42
|
+
return [
|
|
43
|
+
ViewPlugin.fromClass(
|
|
44
|
+
class {
|
|
45
|
+
decorations: DecorationSet;
|
|
46
|
+
constructor(view: EditorView) {
|
|
47
|
+
this.decorations = buildDecorations(view);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
update(update: ViewUpdate) {
|
|
51
|
+
this.decorations = buildDecorations(update.view);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
decorations: (value) => value.decorations,
|
|
56
|
+
},
|
|
57
|
+
),
|
|
58
|
+
];
|
|
59
|
+
};
|
|
@@ -31,7 +31,6 @@ export const markdownTags = {
|
|
|
31
31
|
CodeText: Tag.define(),
|
|
32
32
|
EmphasisMark: Tag.define(),
|
|
33
33
|
HeaderMark: Tag.define(),
|
|
34
|
-
// HorizontalRule: Tag.define(),
|
|
35
34
|
InlineCode: Tag.define(),
|
|
36
35
|
LinkLabel: Tag.define(),
|
|
37
36
|
LinkReference: Tag.define(),
|
|
@@ -129,13 +128,13 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
129
128
|
// Markdown marks.
|
|
130
129
|
{
|
|
131
130
|
tag: [markdownTags.CodeMark, markdownTags.HeaderMark, markdownTags.QuoteMark, markdownTags.EmphasisMark],
|
|
132
|
-
class:
|
|
131
|
+
class: mark,
|
|
133
132
|
},
|
|
134
133
|
|
|
135
134
|
// E.g., code block language (after ```).
|
|
136
135
|
{
|
|
137
136
|
tag: [tags.function(tags.variableName), tags.labelName],
|
|
138
|
-
class:
|
|
137
|
+
class: codeMark,
|
|
139
138
|
},
|
|
140
139
|
|
|
141
140
|
{
|
|
@@ -144,12 +143,12 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
144
143
|
},
|
|
145
144
|
|
|
146
145
|
// Headings.
|
|
147
|
-
{ tag: tags.heading1, class: heading(1
|
|
148
|
-
{ tag: tags.heading2, class: heading(2
|
|
149
|
-
{ tag: tags.heading3, class: heading(3
|
|
150
|
-
{ tag: tags.heading4, class: heading(4
|
|
151
|
-
{ tag: tags.heading5, class: heading(5
|
|
152
|
-
{ tag: tags.heading6, class: heading(6
|
|
146
|
+
{ tag: tags.heading1, class: heading(1) },
|
|
147
|
+
{ tag: tags.heading2, class: heading(2) },
|
|
148
|
+
{ tag: tags.heading3, class: heading(3) },
|
|
149
|
+
{ tag: tags.heading4, class: heading(4) },
|
|
150
|
+
{ tag: tags.heading5, class: heading(5) },
|
|
151
|
+
{ tag: tags.heading6, class: heading(6) },
|
|
153
152
|
|
|
154
153
|
// Emphasis.
|
|
155
154
|
{ tag: tags.emphasis, class: italic },
|
|
@@ -166,7 +165,7 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
166
165
|
// However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
|
|
167
166
|
// when a language is specified. In this case, the syntax highlighting extensions will colorize
|
|
168
167
|
// the code, but all other CSS properties will be inherited.
|
|
169
|
-
// IMPORTANT: Therefore, the fenced code block will use the base editor font.
|
|
168
|
+
// IMPORTANT: Therefore, the fenced code block will use the base editor font unless changed by an extension.
|
|
170
169
|
{
|
|
171
170
|
tag: [markdownTags.CodeText, markdownTags.InlineCode],
|
|
172
171
|
class: code,
|
|
@@ -177,23 +176,10 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
177
176
|
class: blockquote,
|
|
178
177
|
},
|
|
179
178
|
|
|
180
|
-
// TODO(burdon): Replace with extension.
|
|
181
|
-
// {
|
|
182
|
-
// tag: [markdownTags.HorizontalRule],
|
|
183
|
-
// class: mx(horizontalRule, readonly && '-indent-[100rem]'),
|
|
184
|
-
// },
|
|
185
|
-
|
|
186
|
-
// TODO(burdon): Only if being edited.
|
|
187
179
|
{
|
|
188
180
|
tag: [markdownTags.TableCell],
|
|
189
181
|
class: 'font-mono',
|
|
190
182
|
},
|
|
191
|
-
|
|
192
|
-
// Main content, paragraphs, etc.
|
|
193
|
-
// {
|
|
194
|
-
// tag: [tags.content],
|
|
195
|
-
// fontFamily: getToken('fontFamily.body', []).join(','),
|
|
196
|
-
// },
|
|
197
183
|
],
|
|
198
184
|
{
|
|
199
185
|
scope: markdownLanguage,
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
import { getToken } from '../../styles';
|
|
10
|
+
|
|
11
|
+
// TODO(burdon): Reconcile with theme.
|
|
12
|
+
const styles = EditorView.baseTheme({
|
|
13
|
+
'& .cm-hr': {
|
|
14
|
+
// Note that block-level decorations should not have vertical margins,
|
|
15
|
+
borderTop: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
class HorizontalRuleWidget extends WidgetType {
|
|
20
|
+
override toDOM() {
|
|
21
|
+
const el = document.createElement('div');
|
|
22
|
+
el.className = 'cm-hr';
|
|
23
|
+
return el;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const decoration = Decoration.replace({ widget: new HorizontalRuleWidget() });
|
|
28
|
+
|
|
29
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
30
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
31
|
+
const { state } = view;
|
|
32
|
+
const cursor = state.selection.main.head;
|
|
33
|
+
|
|
34
|
+
for (const { from, to } of view.visibleRanges) {
|
|
35
|
+
syntaxTree(state).iterate({
|
|
36
|
+
enter: (node) => {
|
|
37
|
+
if (node.name === 'HorizontalRule') {
|
|
38
|
+
// Check if cursor is inside text.
|
|
39
|
+
if (cursor <= node.from || cursor >= node.to) {
|
|
40
|
+
builder.add(node.from, node.to, decoration);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
from,
|
|
45
|
+
to,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return builder.finish();
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const hr = () => {
|
|
53
|
+
return [
|
|
54
|
+
ViewPlugin.fromClass(
|
|
55
|
+
class {
|
|
56
|
+
decorations: DecorationSet;
|
|
57
|
+
constructor(view: EditorView) {
|
|
58
|
+
this.decorations = buildDecorations(view);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
update(update: ViewUpdate) {
|
|
62
|
+
this.decorations = buildDecorations(update.view);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
decorations: (value) => value.decorations,
|
|
67
|
+
},
|
|
68
|
+
),
|
|
69
|
+
styles,
|
|
70
|
+
];
|
|
71
|
+
};
|