@dxos/react-ui-editor 0.3.11-main.00a28cb → 0.3.11-main.0999ea0
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 +1304 -1092
- 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 +12 -17
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +13 -5
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +9 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +14 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- 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/defs.d.ts +8 -10
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -6
- 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 +23 -15
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -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 +11 -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 +3 -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/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 +3 -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/useTextEditor.d.ts +17 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +2 -27
- package/dist/types/src/hooks/useTextModel.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 +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +45 -36
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +64 -75
- package/src/components/TextEditor/TextEditor.tsx +103 -71
- package/src/components/TextEditor/comments.stories.tsx +46 -44
- package/src/components/Toolbar/Toolbar.stories.tsx +86 -0
- package/src/components/Toolbar/Toolbar.tsx +115 -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 +50 -39
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +11 -36
- package/src/extensions/automerge/semaphore.ts +31 -28
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +17 -17
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/comments.ts +247 -179
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +0 -7
- package/src/extensions/markdown/bundle.ts +20 -30
- package/src/extensions/markdown/code.ts +142 -0
- package/src/extensions/markdown/formatting.ts +159 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -15
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +5 -2
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -34
- package/src/extensions/mention.ts +9 -2
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useActionHandler.ts +45 -0
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +49 -55
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +1 -3
- package/src/util.ts +26 -3
- 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 +0 -7
- 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/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -104
- package/src/extensions/hr.ts +0 -72
- package/src/extensions/yjs/cursor.ts +0 -22
- 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 -156
- 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/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/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
|
@@ -23,34 +23,26 @@ import {
|
|
|
23
23
|
import type { ThemeMode } from '@dxos/react-ui';
|
|
24
24
|
|
|
25
25
|
import { markdownHighlightStyle, markdownTagsExtensions } from './highlight';
|
|
26
|
+
import { type TextEditorProps } from '../../components';
|
|
26
27
|
|
|
27
28
|
export type MarkdownBundleOptions = {
|
|
28
|
-
readonly?: boolean;
|
|
29
29
|
themeMode?: ThemeMode;
|
|
30
|
-
|
|
31
|
-
};
|
|
30
|
+
} & Pick<TextEditorProps, 'placeholder'>;
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
33
|
* Markdown bundle.
|
|
35
34
|
* Refs:
|
|
36
|
-
* https://github.com/codemirror/basic-setup
|
|
37
|
-
* https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts#L50
|
|
38
35
|
* https://codemirror.net/docs/community
|
|
39
36
|
* https://codemirror.net/docs/ref/#codemirror.basicSetup
|
|
40
37
|
*/
|
|
41
38
|
// TODO(burdon): Add Composer here: https://codemirror.net/docs/community
|
|
42
|
-
export const markdownBundle = ({
|
|
43
|
-
readonly,
|
|
44
|
-
themeMode,
|
|
45
|
-
placeholder: _placeholder,
|
|
46
|
-
}: MarkdownBundleOptions): Extension[] => {
|
|
39
|
+
export const markdownBundle = ({ themeMode, placeholder: _placeholder }: MarkdownBundleOptions): Extension[] => {
|
|
47
40
|
return [
|
|
48
41
|
EditorState.allowMultipleSelections.of(true),
|
|
49
42
|
EditorState.tabSize.of(2),
|
|
50
43
|
EditorView.lineWrapping,
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
// https://github.com/codemirror/basic-setup
|
|
54
46
|
bracketMatching(),
|
|
55
47
|
closeBrackets(),
|
|
56
48
|
crosshairCursor(),
|
|
@@ -59,7 +51,7 @@ export const markdownBundle = ({
|
|
|
59
51
|
highlightActiveLine(),
|
|
60
52
|
history(),
|
|
61
53
|
indentOnInput(),
|
|
62
|
-
_placeholder
|
|
54
|
+
_placeholder ? placeholder(_placeholder) : [],
|
|
63
55
|
|
|
64
56
|
// Main extension.
|
|
65
57
|
// https://github.com/codemirror/lang-markdown
|
|
@@ -86,21 +78,19 @@ export const markdownBundle = ({
|
|
|
86
78
|
themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
|
|
87
79
|
|
|
88
80
|
// Custom styles.
|
|
89
|
-
syntaxHighlighting(markdownHighlightStyle(
|
|
90
|
-
].filter(Boolean) as Extension[];
|
|
91
|
-
};
|
|
81
|
+
syntaxHighlighting(markdownHighlightStyle()),
|
|
92
82
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
83
|
+
keymap.of([
|
|
84
|
+
// https://codemirror.net/docs/ref/#commands.indentWithTab
|
|
85
|
+
indentWithTab,
|
|
86
|
+
// https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
|
|
87
|
+
...closeBracketsKeymap,
|
|
88
|
+
// https://codemirror.net/docs/ref/#commands.historyKeymap
|
|
89
|
+
...historyKeymap,
|
|
90
|
+
// https://codemirror.net/docs/ref/#search.searchKeymap
|
|
91
|
+
...searchKeymap,
|
|
92
|
+
// https://codemirror.net/docs/ref/#commands.standardKeymap
|
|
93
|
+
...standardKeymap,
|
|
94
|
+
]),
|
|
95
|
+
];
|
|
96
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
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
|
+
paddingInline: '1rem !important',
|
|
25
|
+
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
26
|
+
},
|
|
27
|
+
'& .cm-codeblock': {
|
|
28
|
+
display: 'inline-block',
|
|
29
|
+
width: '100%',
|
|
30
|
+
position: 'relative',
|
|
31
|
+
'&::after': {
|
|
32
|
+
content: '""',
|
|
33
|
+
position: 'absolute',
|
|
34
|
+
inset: 0,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
'&light .cm-codeblock': {
|
|
38
|
+
'&::after': {
|
|
39
|
+
background: getToken('extend.semanticColors.input.light'),
|
|
40
|
+
mixBlendMode: 'darken',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
'&dark .cm-codeblock': {
|
|
44
|
+
'&::after': {
|
|
45
|
+
background: getToken('extend.semanticColors.input.dark'),
|
|
46
|
+
mixBlendMode: 'lighten',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
'& .cm-codeblock-first': {
|
|
50
|
+
'&::after': {
|
|
51
|
+
borderTopLeftRadius: '.5rem',
|
|
52
|
+
borderTopRightRadius: '.5rem',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
'& .cm-codeblock-last': {
|
|
56
|
+
'&::after': {
|
|
57
|
+
borderBottomLeftRadius: '.5rem',
|
|
58
|
+
borderBottomRightRadius: '.5rem',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
64
|
+
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
65
|
+
const start = lines.findIndex((line) => line.from >= from);
|
|
66
|
+
const end = lines.findIndex((line) => line.to >= to);
|
|
67
|
+
return [start, end];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
class LineWidget extends WidgetType {
|
|
71
|
+
constructor(private readonly _className: string) {
|
|
72
|
+
super();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
override toDOM() {
|
|
76
|
+
const el = document.createElement('span');
|
|
77
|
+
el.innerHTML = ' ';
|
|
78
|
+
el.className = mx('cm-code cm-codeblock', this._className);
|
|
79
|
+
return el;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const top = new LineWidget('cm-codeblock-first');
|
|
84
|
+
const bottom = new LineWidget('cm-codeblock-last');
|
|
85
|
+
|
|
86
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
87
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
88
|
+
const { state } = view;
|
|
89
|
+
const blocks = view.viewportLineBlocks;
|
|
90
|
+
const cursor = state.selection.main.head;
|
|
91
|
+
|
|
92
|
+
// TODO(burdon): Add copy to clipboard widget.
|
|
93
|
+
for (const { from, to } of view.visibleRanges) {
|
|
94
|
+
syntaxTree(state).iterate({
|
|
95
|
+
enter: (node) => {
|
|
96
|
+
if (node.name === 'FencedCode') {
|
|
97
|
+
const edit = !view.state.readOnly && cursor >= node.from && cursor <= node.to;
|
|
98
|
+
const range = getLineRange(blocks, node.from, node.to);
|
|
99
|
+
for (let i = range[0]; i <= range[1]; i++) {
|
|
100
|
+
const block = blocks[i];
|
|
101
|
+
if (!edit && (i === range[0] || i === range[1])) {
|
|
102
|
+
builder.add(block.from, block.to, Decoration.replace({ widget: i === range[0] ? top : bottom }));
|
|
103
|
+
} else {
|
|
104
|
+
builder.add(
|
|
105
|
+
block.from,
|
|
106
|
+
block.from,
|
|
107
|
+
Decoration.line({
|
|
108
|
+
class: mx('cm-code cm-codeblock'),
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
from,
|
|
116
|
+
to,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return builder.finish();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const code = () => {
|
|
124
|
+
return [
|
|
125
|
+
ViewPlugin.fromClass(
|
|
126
|
+
class {
|
|
127
|
+
decorations: DecorationSet;
|
|
128
|
+
constructor(view: EditorView) {
|
|
129
|
+
this.decorations = buildDecorations(view);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
update(update: ViewUpdate) {
|
|
133
|
+
this.decorations = buildDecorations(update.view);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
decorations: (value) => value.decorations,
|
|
138
|
+
},
|
|
139
|
+
),
|
|
140
|
+
styles,
|
|
141
|
+
];
|
|
142
|
+
};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type Extension, RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import {
|
|
8
|
+
type Command,
|
|
9
|
+
Decoration,
|
|
10
|
+
type DecorationSet,
|
|
11
|
+
type EditorView,
|
|
12
|
+
keymap,
|
|
13
|
+
ViewPlugin,
|
|
14
|
+
type ViewUpdate,
|
|
15
|
+
} from '@codemirror/view';
|
|
16
|
+
import { type SyntaxNodeRef, type SyntaxNode } from '@lezer/common';
|
|
17
|
+
|
|
18
|
+
export type FormattingOptions = {};
|
|
19
|
+
|
|
20
|
+
export const setHeading =
|
|
21
|
+
(level: number): Command =>
|
|
22
|
+
(view: EditorView) => {
|
|
23
|
+
const {
|
|
24
|
+
selection: { ranges },
|
|
25
|
+
doc,
|
|
26
|
+
} = view.state;
|
|
27
|
+
const changes = [];
|
|
28
|
+
for (const range of ranges) {
|
|
29
|
+
const { number } = doc.lineAt(range.anchor);
|
|
30
|
+
const { from } = doc.line(number);
|
|
31
|
+
|
|
32
|
+
// Check heading doesn't already exist.
|
|
33
|
+
const line = doc.sliceString(from, from + 7);
|
|
34
|
+
const [_, marks, spaces] = line.match(/(#+)(\s+)/) ?? [];
|
|
35
|
+
const current = marks?.length ?? 0;
|
|
36
|
+
if (level !== current) {
|
|
37
|
+
changes.push({
|
|
38
|
+
from,
|
|
39
|
+
to: from + current + (spaces?.length ?? 0),
|
|
40
|
+
insert: '#'.repeat(level) + (level > 0 ? ' ' : ''),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (changes.length) {
|
|
46
|
+
view.dispatch({ changes });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const toggleStyle =
|
|
53
|
+
(mark: string): Command =>
|
|
54
|
+
(view) => {
|
|
55
|
+
const { ranges } = view.state.selection;
|
|
56
|
+
for (const range of ranges) {
|
|
57
|
+
if (range.from === range.to) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// TODO(burdon): Detect if already styled (or nested).
|
|
62
|
+
view.dispatch({
|
|
63
|
+
changes: [
|
|
64
|
+
{
|
|
65
|
+
from: range.from,
|
|
66
|
+
insert: mark,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
from: range.to,
|
|
70
|
+
insert: mark,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const toggleBold = toggleStyle('**');
|
|
80
|
+
export const toggleItalic = toggleStyle('_');
|
|
81
|
+
export const toggleStrikethrough = toggleStyle('~~');
|
|
82
|
+
|
|
83
|
+
export const toggleList = (view: EditorView) => {};
|
|
84
|
+
|
|
85
|
+
export const formatting = (options: FormattingOptions = {}): Extension => {
|
|
86
|
+
return [
|
|
87
|
+
keymap.of([
|
|
88
|
+
{
|
|
89
|
+
key: 'meta-b',
|
|
90
|
+
run: toggleBold,
|
|
91
|
+
},
|
|
92
|
+
]),
|
|
93
|
+
styling(),
|
|
94
|
+
];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// https://github.github.com/gfm
|
|
98
|
+
// https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax
|
|
99
|
+
|
|
100
|
+
const styling = (): Extension => {
|
|
101
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
102
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
103
|
+
const { state } = view;
|
|
104
|
+
const cursor = state.selection.main.head;
|
|
105
|
+
|
|
106
|
+
// TODO(burdon): Bug if '***foo***' (since StrongEmphasis is nested inside EmphasisMark).
|
|
107
|
+
// Ranges must be added sorted by `from` position and `startSide`.
|
|
108
|
+
const replace = (node: SyntaxNodeRef, marks: SyntaxNode[]) => {
|
|
109
|
+
if (cursor <= node.from || cursor >= node.to) {
|
|
110
|
+
for (const mark of marks) {
|
|
111
|
+
builder.add(mark.from, mark.to, Decoration.replace({}));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
for (const { from, to } of view.visibleRanges) {
|
|
117
|
+
syntaxTree(state).iterate({
|
|
118
|
+
enter: (node) => {
|
|
119
|
+
switch (node.name) {
|
|
120
|
+
case 'Emphasis':
|
|
121
|
+
case 'StrongEmphasis': {
|
|
122
|
+
const marks = node.node.getChildren('EmphasisMark');
|
|
123
|
+
replace(node, marks);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
case 'Strikethrough': {
|
|
128
|
+
const marks = node.node.getChildren('StrikethroughMark');
|
|
129
|
+
replace(node, marks);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
from,
|
|
135
|
+
to,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return builder.finish();
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return [
|
|
143
|
+
ViewPlugin.fromClass(
|
|
144
|
+
class {
|
|
145
|
+
decorations: DecorationSet;
|
|
146
|
+
constructor(view: EditorView) {
|
|
147
|
+
this.decorations = buildDecorations(view);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
update(update: ViewUpdate) {
|
|
151
|
+
this.decorations = buildDecorations(update.view);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
decorations: (value) => value.decorations,
|
|
156
|
+
},
|
|
157
|
+
),
|
|
158
|
+
];
|
|
159
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -128,13 +128,13 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
128
128
|
// Markdown marks.
|
|
129
129
|
{
|
|
130
130
|
tag: [markdownTags.CodeMark, markdownTags.HeaderMark, markdownTags.QuoteMark, markdownTags.EmphasisMark],
|
|
131
|
-
class:
|
|
131
|
+
class: mark,
|
|
132
132
|
},
|
|
133
133
|
|
|
134
134
|
// E.g., code block language (after ```).
|
|
135
135
|
{
|
|
136
136
|
tag: [tags.function(tags.variableName), tags.labelName],
|
|
137
|
-
class:
|
|
137
|
+
class: codeMark,
|
|
138
138
|
},
|
|
139
139
|
|
|
140
140
|
{
|
|
@@ -143,12 +143,12 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
143
143
|
},
|
|
144
144
|
|
|
145
145
|
// Headings.
|
|
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
|
|
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) },
|
|
152
152
|
|
|
153
153
|
// Emphasis.
|
|
154
154
|
{ tag: tags.emphasis, class: italic },
|
|
@@ -165,7 +165,7 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
165
165
|
// However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
|
|
166
166
|
// when a language is specified. In this case, the syntax highlighting extensions will colorize
|
|
167
167
|
// the code, but all other CSS properties will be inherited.
|
|
168
|
-
// 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.
|
|
169
169
|
{
|
|
170
170
|
tag: [markdownTags.CodeText, markdownTags.InlineCode],
|
|
171
171
|
class: code,
|
|
@@ -180,12 +180,6 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
180
180
|
tag: [markdownTags.TableCell],
|
|
181
181
|
class: 'font-mono',
|
|
182
182
|
},
|
|
183
|
-
|
|
184
|
-
// Main content, paragraphs, etc.
|
|
185
|
-
// {
|
|
186
|
-
// tag: [tags.content],
|
|
187
|
-
// fontFamily: getToken('fontFamily.body', []).join(','),
|
|
188
|
-
// },
|
|
189
183
|
],
|
|
190
184
|
{
|
|
191
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
|
+
};
|
|
@@ -3,4 +3,12 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './bundle';
|
|
6
|
+
export * from './code';
|
|
7
|
+
export * from './formatting';
|
|
8
|
+
export * from './heading';
|
|
6
9
|
export * from './highlight';
|
|
10
|
+
export * from './hr';
|
|
11
|
+
export * from './image';
|
|
12
|
+
export * from './link';
|
|
13
|
+
export * from './table';
|
|
14
|
+
export * from './tasklist';
|
|
@@ -22,6 +22,7 @@ import { tooltipContent } from '@dxos/react-ui-theme';
|
|
|
22
22
|
// https://codemirror.net/try/?c=aW1wb3J0IHsgYmFzaWNTZXR1cCwgRWRpdG9yVmlldyB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7IG1hcmtkb3duIH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1tYXJrZG93biIKaW1wb3J0IHsgc3ludGF4VHJlZSB9IGZyb20gIkBjb2RlbWlycm9yL2xhbmd1YWdlIjsKaW1wb3J0IHsgRWRpdG9yU3RhdGUsIFJhbmdlU2V0QnVpbGRlciwgU3RhdGVGaWVsZCB9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIjsKaW1wb3J0IHsgRGVjb3JhdGlvbiwgV2lkZ2V0VHlwZSB9IGZyb20gIkBjb2RlbWlycm9yL3ZpZXciOwoKY2xhc3MgTGluayBleHRlbmRzIFdpZGdldFR5cGUgewogIGNvbnN0cnVjdG9yKHRleHQsIHVybCkgewogICAgc3VwZXIoKQogICAgdGhpcy50ZXh0ID0gdGV4dAogICAgdGhpcy51cmwgPSB1cmwKICB9CiAgZXEob3RoZXIpIHsgcmV0dXJuIHRoaXMudGV4dCA9PSBvdGhlci50ZXh0ICYmIHRoaXMudXJsID09IG90aGVyLnVybCB9CiAgdG9ET00oKSB7CiAgICBsZXQgbGluayA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoImEiKQogICAgbGluay50ZXh0Q29udGVudCA9IHRoaXMudGV4dAogICAgbGluay5ocmVmID0gdGhpcy51cmwKICAgIHJldHVybiBsaW5rOwogIH0KfQoKbGV0IGRlY29yYXRpb25zRmllbGQgPSBTdGF0ZUZpZWxkLmRlZmluZSh7CiAgY3JlYXRlKCkgewogICAgcmV0dXJuIERlY29yYXRpb24ubm9uZTsKICB9LAogIHVwZGF0ZShfLCB0cikgewogICAgY29uc3QgYnVpbGRlciA9IG5ldyBSYW5nZVNldEJ1aWxkZXIoKTsKICAgIGxldCBjdXJzb3IgPSB0ci5zdGF0ZS5zZWxlY3Rpb24ubWFpbi5oZWFkOwogICAgc3ludGF4VHJlZSh0ci5zdGF0ZSkuaXRlcmF0ZSh7CiAgICAgIGVudGVyOiAobm9kZSkgPT4gewogICAgICAgIGlmICgoY3Vyc29yIDwgbm9kZS5mcm9tIHx8IGN1cnNvciA+IG5vZGUudG8pICYmIG5vZGUubmFtZSA9PSAiTGluayIpIHsKICAgICAgICAgIGxldCBtYXJrcyA9IG5vZGUubm9kZS5nZXRDaGlsZHJlbigiTGlua01hcmsiKTsKICAgICAgICAgIGxldCB0ZXh0ID0gbWFya3MubGVuZ3RoID49IDIgPyB0ci5zdGF0ZS5zbGljZURvYyhtYXJrc1swXS50bywgbWFya3NbMV0uZnJvbSkgOiAiIgoKICAgICAgICAgIGxldCB1cmxOb2RlID0gbm9kZS5ub2RlLmdldENoaWxkKCJVUkwiKTsKICAgICAgICAgIGxldCB1cmwgPSB1cmxOb2RlID8gdHIuc3RhdGUuc2xpY2VEb2ModXJsTm9kZS5mcm9tLCB1cmxOb2RlLnRvKSA6ICIiCiAgICAgICAgICBidWlsZGVyLmFkZChub2RlLmZyb20sIG5vZGUudG8sIERlY29yYXRpb24ucmVwbGFjZSh7CiAgICAgICAgICAgIHdpZGdldDogbmV3IExpbmsodGV4dCwgdXJsKSwKICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSwKICAgIH0pOwogICAgcmV0dXJuIGJ1aWxkZXIuZmluaXNoKCk7CiAgfSwKICBwcm92aWRlOiAoZikgPT4gRWRpdG9yVmlldy5kZWNvcmF0aW9ucy5mcm9tKGYpLAp9KTsKCmxldCB2aWV3ID0gbmV3IEVkaXRvclZpZXcoewogIGRvYzogIiMgSGVsbG8gV29ybGRcblxuW1Rlc3RdKGh0dHBzOi8vZXhhbXBsZS5jb20pXG5cblxuIiwKICBleHRlbnNpb25zOiBbZGVjb3JhdGlvbnNGaWVsZCwgbWFya2Rvd24oKV0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5LAp9KTsKCnZpZXcuZm9jdXMoKTs=
|
|
23
23
|
// https://discuss.codemirror.net/t/link-widget-is-clicked-only-if-editor-is-out-of-focus/7433
|
|
24
24
|
|
|
25
|
+
// TODO(burdon): Callback to dynamically lookup title of linked pages.
|
|
25
26
|
export type LinkOptions = {
|
|
26
27
|
link?: boolean;
|
|
27
28
|
onHover?: (el: Element, url: string) => void;
|
|
@@ -37,7 +38,6 @@ export const link = (options: LinkOptions = {}): Extension => {
|
|
|
37
38
|
ViewPlugin.fromClass(
|
|
38
39
|
class {
|
|
39
40
|
decorations: DecorationSet;
|
|
40
|
-
|
|
41
41
|
constructor(view: EditorView) {
|
|
42
42
|
this.decorations = buildDecorations(view, options);
|
|
43
43
|
}
|
|
@@ -49,7 +49,7 @@ export const link = (options: LinkOptions = {}): Extension => {
|
|
|
49
49
|
}
|
|
50
50
|
},
|
|
51
51
|
{
|
|
52
|
-
decorations: (
|
|
52
|
+
decorations: (value) => value.decorations,
|
|
53
53
|
},
|
|
54
54
|
),
|
|
55
55
|
];
|
|
@@ -65,10 +65,12 @@ export const link = (options: LinkOptions = {}): Extension => {
|
|
|
65
65
|
for (let i = 0, node: SyntaxNode | null = syntax; !link && node && i < 5; node = node.parent, i++) {
|
|
66
66
|
link = node.name === 'Link' ? node : null;
|
|
67
67
|
}
|
|
68
|
+
|
|
68
69
|
const url = link && link.getChild('URL');
|
|
69
70
|
if (!url || !link) {
|
|
70
71
|
return null;
|
|
71
72
|
}
|
|
73
|
+
|
|
72
74
|
const urlText = view.state.sliceDoc(url.from, url.to);
|
|
73
75
|
return {
|
|
74
76
|
pos: link.from,
|
|
@@ -158,6 +160,7 @@ const buildDecorations = (view: EditorView, options: LinkOptions): DecorationSet
|
|
|
158
160
|
return false;
|
|
159
161
|
}
|
|
160
162
|
|
|
163
|
+
// TODO(burdon): Don't expand if moving cursor up/down.
|
|
161
164
|
if (!view.hasFocus || state.readOnly || cursor < node.from || cursor > node.to) {
|
|
162
165
|
builder.add(
|
|
163
166
|
node.from,
|