@dxos/react-ui-editor 0.3.10-main.3cfd481 → 0.3.10-main.4fbcffb
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 +345 -125
- 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 +3 -0
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +1 -0
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts +3 -2
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts +18 -0
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts +10 -0
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/index.d.ts +2 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts +4 -2
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts +1 -4
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts +3 -0
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/package.json +20 -19
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +41 -8
- package/src/components/TextEditor/TextEditor.tsx +32 -11
- package/src/components/TextEditor/extensions/autocomplete.ts +5 -3
- package/src/components/TextEditor/extensions/comments.ts +190 -0
- package/src/components/TextEditor/extensions/demo.ts +66 -0
- package/src/components/TextEditor/extensions/experimental.tsx +1 -0
- package/src/components/TextEditor/extensions/index.ts +2 -0
- package/src/components/TextEditor/extensions/link.ts +10 -2
- package/src/components/TextEditor/extensions/listener.ts +3 -2
- package/src/components/TextEditor/extensions/markdown/bundle.ts +6 -14
- package/src/components/TextEditor/extensions/markdown/highlight.ts +2 -1
- package/src/components/TextEditor/extensions/tasklist.ts +73 -79
- package/src/components/TextEditor/themes/default.ts +15 -0
- package/src/hooks/useTextModel.ts +11 -8
- package/src/testing/replicator.ts +5 -5
|
@@ -23,14 +23,16 @@ import {
|
|
|
23
23
|
} from '@codemirror/autocomplete';
|
|
24
24
|
import { keymap } from '@codemirror/view';
|
|
25
25
|
|
|
26
|
+
export type AutocompleteResult = Completion;
|
|
27
|
+
|
|
26
28
|
export type AutocompleteOptions = {
|
|
27
|
-
|
|
29
|
+
onSearch: (text: string) => Completion[];
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
33
|
* Autocomplete extension.
|
|
32
34
|
*/
|
|
33
|
-
export const autocomplete = ({
|
|
35
|
+
export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
|
|
34
36
|
return [
|
|
35
37
|
// https://codemirror.net/docs/ref/#view.keymap
|
|
36
38
|
// https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
|
|
@@ -57,7 +59,7 @@ export const autocomplete = ({ getOptions }: AutocompleteOptions) => {
|
|
|
57
59
|
// TODO(burdon): Option to convert to links?
|
|
58
60
|
return {
|
|
59
61
|
from: word.from,
|
|
60
|
-
options:
|
|
62
|
+
options: onSearch(word.text.toLowerCase()),
|
|
61
63
|
// options: [
|
|
62
64
|
// { label: 'apple', type: 'keyword' },
|
|
63
65
|
// { label: 'amazon', type: 'keyword' },
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
keymap,
|
|
8
|
+
type DecorationSet,
|
|
9
|
+
type Rect,
|
|
10
|
+
type ViewUpdate,
|
|
11
|
+
Decoration,
|
|
12
|
+
EditorView,
|
|
13
|
+
MatchDecorator,
|
|
14
|
+
ViewPlugin,
|
|
15
|
+
WidgetType,
|
|
16
|
+
} from '@codemirror/view';
|
|
17
|
+
|
|
18
|
+
import { debounce } from '@dxos/async';
|
|
19
|
+
import { invariant } from '@dxos/invariant';
|
|
20
|
+
|
|
21
|
+
// 1. TODO(burdon): Make atomic (for tasklist also).
|
|
22
|
+
// - https://discuss.codemirror.net/t/easily-track-remove-content-with-decorations/4606
|
|
23
|
+
// - https://discuss.codemirror.net/t/creating-atomic-replace-decorations/2961
|
|
24
|
+
// - https://codemirror.net/docs/ref/#state.EditorState%5EtransactionFilter (transaction filter: move, delete).
|
|
25
|
+
// 2. TODO(burdon): Create/track threads in composer (change global state). Select/follow. Scroll with page.
|
|
26
|
+
// - Separate from chat/search.
|
|
27
|
+
// 3. TODO(burdon): Support multiple threads in sidebar?
|
|
28
|
+
// 4. TODO(burdon): Anchor AI thread when creating section.
|
|
29
|
+
// 5. TODO(burdon): Button to resolve thread to close comments.
|
|
30
|
+
|
|
31
|
+
// 6. TODO(burdon): Perf: Update existing rangeset.
|
|
32
|
+
// https://discuss.codemirror.net/t/rangeset-with-metadata-and-different-decorations/3874
|
|
33
|
+
|
|
34
|
+
// TODO(burdon): Import note (performance):
|
|
35
|
+
// Easily track & remove content with decorations
|
|
36
|
+
// https://discuss.codemirror.net/t/easily-track-remove-content-with-decorations/4606
|
|
37
|
+
|
|
38
|
+
class BookmarkWidget extends WidgetType {
|
|
39
|
+
constructor(private readonly _pos: number, private readonly _id: string) {
|
|
40
|
+
super();
|
|
41
|
+
invariant(this._id);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
override eq(other: BookmarkWidget) {
|
|
45
|
+
return this._id === other._id;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
override toDOM() {
|
|
49
|
+
const span = document.createElement('span');
|
|
50
|
+
span.className = 'cm-bookmark';
|
|
51
|
+
// TODO(burdon): Call out to react?
|
|
52
|
+
// https://emojifinder.com/comment
|
|
53
|
+
span.textContent = '💬';
|
|
54
|
+
return span;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// TODO(burdon): Reconcile with theme.
|
|
59
|
+
const styles = EditorView.baseTheme({
|
|
60
|
+
'& .cm-bookmark': {
|
|
61
|
+
cursor: 'pointer',
|
|
62
|
+
margin: '4px',
|
|
63
|
+
padding: '4px',
|
|
64
|
+
backgroundColor: 'yellow',
|
|
65
|
+
},
|
|
66
|
+
'& .cm-bookmark-selected': {
|
|
67
|
+
backgroundColor: 'orange',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
type CommentsInfo = {
|
|
72
|
+
active?: string;
|
|
73
|
+
items: { id: string; pos: number; location: Rect | null }[];
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export type CommentsOptions = {
|
|
77
|
+
key?: string;
|
|
78
|
+
onCreate?: () => string | void;
|
|
79
|
+
onUpdate?: (info: CommentsInfo) => void;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// https://www.markdownguide.org/extended-syntax/#footnotes
|
|
83
|
+
// TODO(burdon): Record span in automerge model?
|
|
84
|
+
// TODO(burdon): Extended markdown: https://www.markdownguide.org/extended-syntax
|
|
85
|
+
export const comments = (options: CommentsOptions = {}): Extension => {
|
|
86
|
+
let active: string | undefined;
|
|
87
|
+
|
|
88
|
+
const bookmarkMatcher = new MatchDecorator({
|
|
89
|
+
regexp: /\[\^(\w+)\]/g,
|
|
90
|
+
decoration: (match, view, pos) => {
|
|
91
|
+
const id = match[1];
|
|
92
|
+
return Decoration.replace({
|
|
93
|
+
id,
|
|
94
|
+
widget: new BookmarkWidget(pos, id),
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const bookmarks = ViewPlugin.fromClass(
|
|
100
|
+
class {
|
|
101
|
+
bookmarks: DecorationSet;
|
|
102
|
+
constructor(view: EditorView) {
|
|
103
|
+
this.bookmarks = bookmarkMatcher.createDeco(view);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
update(update: ViewUpdate) {
|
|
107
|
+
this.bookmarks = bookmarkMatcher.updateDeco(update, this.bookmarks);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
decorations: (instance) => instance.bookmarks,
|
|
112
|
+
provide: (plugin) =>
|
|
113
|
+
EditorView.atomicRanges.of((view) => {
|
|
114
|
+
return view.plugin(plugin)?.bookmarks || Decoration.none;
|
|
115
|
+
}),
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const doUpdate = debounce((data: CommentsInfo) => {
|
|
120
|
+
options.onUpdate?.(data);
|
|
121
|
+
}, 200);
|
|
122
|
+
|
|
123
|
+
return [
|
|
124
|
+
keymap.of([
|
|
125
|
+
{
|
|
126
|
+
key: options?.key ?? 'shift-meta-c',
|
|
127
|
+
run: (view) => {
|
|
128
|
+
// Insert footnote.
|
|
129
|
+
const id = options.onCreate?.();
|
|
130
|
+
if (id) {
|
|
131
|
+
const pos = view.state.selection.main.head;
|
|
132
|
+
const tag = `[^${id}]`;
|
|
133
|
+
view.dispatch({
|
|
134
|
+
changes: { from: pos, insert: tag },
|
|
135
|
+
selection: { anchor: pos + tag.length },
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return false;
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
]),
|
|
145
|
+
|
|
146
|
+
bookmarks,
|
|
147
|
+
|
|
148
|
+
// Monitor cursor movement.
|
|
149
|
+
EditorView.updateListener.of((update) => {
|
|
150
|
+
active = undefined;
|
|
151
|
+
if (!options.onUpdate) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const view = update.view;
|
|
156
|
+
const pos = view.state.selection.main.head;
|
|
157
|
+
|
|
158
|
+
const decorations: { from: number; to: number; value: Decoration }[] = [];
|
|
159
|
+
const rangeSet = view.plugin(bookmarks)?.bookmarks;
|
|
160
|
+
let closest = Infinity;
|
|
161
|
+
// TODO(burdon): Always shows entire document?
|
|
162
|
+
const { from, to } = view.visibleRanges?.[0] ?? { from: 0, to: view.state.doc.length };
|
|
163
|
+
rangeSet?.between(from, to, (from, to, value) => {
|
|
164
|
+
decorations.push({ from, to, value });
|
|
165
|
+
const d = Math.min(Math.abs(pos - from), Math.abs(pos - to));
|
|
166
|
+
if (d < closest) {
|
|
167
|
+
// TODO(burdon): Update class of selected.
|
|
168
|
+
active = value.spec.id;
|
|
169
|
+
closest = d;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (decorations.length) {
|
|
174
|
+
doUpdate({
|
|
175
|
+
active,
|
|
176
|
+
items: decorations.map(
|
|
177
|
+
({
|
|
178
|
+
from,
|
|
179
|
+
value: {
|
|
180
|
+
spec: { id },
|
|
181
|
+
},
|
|
182
|
+
}) => ({ id: id as string, pos: from, location: view.coordsAtPos(from) }),
|
|
183
|
+
),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}),
|
|
187
|
+
|
|
188
|
+
styles,
|
|
189
|
+
];
|
|
190
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { keymap } from '@codemirror/view';
|
|
6
|
+
|
|
7
|
+
export type DemoOptions = {
|
|
8
|
+
delay?: number;
|
|
9
|
+
items?: string[];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const defaultItems = ['hello world!', 'this is a test.', 'this is [DXOS](https://dxos.org)'];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Demo script.
|
|
16
|
+
* Configurable plugin that let's user cycle through pre-configured input script.
|
|
17
|
+
*/
|
|
18
|
+
export const demo = ({ delay = 75, items = defaultItems }: DemoOptions = {}) => {
|
|
19
|
+
let t: any;
|
|
20
|
+
let idx = 0;
|
|
21
|
+
|
|
22
|
+
return [
|
|
23
|
+
keymap.of([
|
|
24
|
+
{
|
|
25
|
+
// Reset.
|
|
26
|
+
key: "alt-meta-'",
|
|
27
|
+
run: (view) => {
|
|
28
|
+
clearTimeout(t);
|
|
29
|
+
idx = 0;
|
|
30
|
+
return true;
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
// Next prompt.
|
|
35
|
+
// TODO(burdon): Press 1-9 to select prompt?
|
|
36
|
+
key: "shift-meta-'",
|
|
37
|
+
run: (view) => {
|
|
38
|
+
clearTimeout(t);
|
|
39
|
+
// TODO(burdon): Add space if needed.
|
|
40
|
+
const text = items[idx++];
|
|
41
|
+
if (idx === items?.length) {
|
|
42
|
+
idx = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let i = 0;
|
|
46
|
+
const insert = (d = 0) => {
|
|
47
|
+
t = setTimeout(() => {
|
|
48
|
+
const pos = view.state.selection.main.head;
|
|
49
|
+
view.dispatch({
|
|
50
|
+
changes: { from: pos, insert: text[i++] },
|
|
51
|
+
selection: { anchor: pos + 1 },
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (i < text.length) {
|
|
55
|
+
insert(Math.random() * delay * (text[i] === ' ' ? 2 : 1));
|
|
56
|
+
}
|
|
57
|
+
}, d);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
insert();
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
]),
|
|
65
|
+
];
|
|
66
|
+
};
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
// TODO(burdon): Add-on: dialog.
|
|
20
20
|
// TODO(burdon): Comments: https://codemirror.net/5/doc/manual.html#setBookmark
|
|
21
21
|
// TODO(burdon): Split view: https://codemirror.net/examples/split
|
|
22
|
+
// TODO(burdon): https://codemirror.net/5/demo/simplemode.html
|
|
22
23
|
|
|
23
24
|
// https://codemirror.net/examples/autocompletion
|
|
24
25
|
// https://codemirror.net/docs/ref/#autocomplete.autocompletion
|
|
@@ -77,6 +77,11 @@ class LinkText extends WidgetType {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Range sets provide a data structure that can hold a collection of tagged,
|
|
82
|
+
* possibly overlapping ranges in such a way that they can efficiently be mapped though document changes.
|
|
83
|
+
* https://codemirror.net/docs/ref/#state
|
|
84
|
+
*/
|
|
80
85
|
const update = (state: EditorState, options: LinkOptions) => {
|
|
81
86
|
const builder = new RangeSetBuilder();
|
|
82
87
|
const cursor = state.selection.main.head;
|
|
@@ -89,6 +94,9 @@ const update = (state: EditorState, options: LinkOptions) => {
|
|
|
89
94
|
|
|
90
95
|
const urlNode = node.node.getChild('URL');
|
|
91
96
|
const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : '';
|
|
97
|
+
if (!url) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
92
100
|
|
|
93
101
|
if (cursor < node.from || cursor > node.to) {
|
|
94
102
|
builder.add(
|
|
@@ -109,9 +117,9 @@ const update = (state: EditorState, options: LinkOptions) => {
|
|
|
109
117
|
}),
|
|
110
118
|
);
|
|
111
119
|
}
|
|
112
|
-
}
|
|
113
120
|
|
|
114
|
-
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
115
123
|
},
|
|
116
124
|
});
|
|
117
125
|
|
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
import { StateField } from '@codemirror/state';
|
|
6
6
|
|
|
7
|
-
export type
|
|
7
|
+
export type ListenerOptions = { onChange: (text: string) => void };
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Based on https://github.com/codemirror/dev/issues/44#issuecomment-789093799
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
// TODO(burdon): Expose options.
|
|
13
|
+
export const listener = ({ onChange }: ListenerOptions) =>
|
|
13
14
|
StateField.define({
|
|
14
15
|
create: () => null,
|
|
15
16
|
update: (_value, transaction) => {
|
|
@@ -3,17 +3,10 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
|
|
6
|
-
import { defaultKeymap, history,
|
|
6
|
+
import { defaultKeymap, history, indentWithTab } from '@codemirror/commands';
|
|
7
7
|
import { markdownLanguage, markdown } from '@codemirror/lang-markdown';
|
|
8
|
-
import {
|
|
9
|
-
bracketMatching,
|
|
10
|
-
defaultHighlightStyle,
|
|
11
|
-
foldKeymap,
|
|
12
|
-
indentOnInput,
|
|
13
|
-
syntaxHighlighting,
|
|
14
|
-
} from '@codemirror/language';
|
|
8
|
+
import { bracketMatching, defaultHighlightStyle, indentOnInput, syntaxHighlighting } from '@codemirror/language';
|
|
15
9
|
import { languages } from '@codemirror/language-data';
|
|
16
|
-
import { lintKeymap } from '@codemirror/lint';
|
|
17
10
|
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
|
|
18
11
|
import { EditorState, type Extension } from '@codemirror/state';
|
|
19
12
|
import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
|
|
@@ -50,7 +43,6 @@ export const markdownBundle = ({ themeMode, placeholder: _placeholder }: Markdow
|
|
|
50
43
|
EditorState.allowMultipleSelections.of(true),
|
|
51
44
|
EditorView.lineWrapping,
|
|
52
45
|
|
|
53
|
-
// autocompletion(),
|
|
54
46
|
crosshairCursor(),
|
|
55
47
|
dropCursor(),
|
|
56
48
|
drawSelection(),
|
|
@@ -62,13 +54,13 @@ export const markdownBundle = ({ themeMode, placeholder: _placeholder }: Markdow
|
|
|
62
54
|
indentOnInput(),
|
|
63
55
|
rectangularSelection(),
|
|
64
56
|
|
|
57
|
+
// TODO(burdon): Review.
|
|
65
58
|
keymap.of([
|
|
66
59
|
...closeBracketsKeymap,
|
|
67
|
-
// ...completionKeymap,
|
|
68
60
|
...defaultKeymap,
|
|
69
|
-
...foldKeymap,
|
|
70
|
-
...historyKeymap,
|
|
71
|
-
...lintKeymap,
|
|
61
|
+
// ...foldKeymap,
|
|
62
|
+
// ...historyKeymap,
|
|
63
|
+
// ...lintKeymap,
|
|
72
64
|
...searchKeymap,
|
|
73
65
|
indentWithTab,
|
|
74
66
|
]),
|
|
@@ -134,10 +134,11 @@ export const markdownHighlightStyle = HighlightStyle.define(
|
|
|
134
134
|
class: codeMark,
|
|
135
135
|
},
|
|
136
136
|
|
|
137
|
-
// The `markdown` extension configures extensions for `lezer` to parse markdown tokens (incl. below).
|
|
137
|
+
// NOTE: The `markdown` extension configures extensions for `lezer` to parse markdown tokens (incl. below).
|
|
138
138
|
// However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
|
|
139
139
|
// when a language is specified. In this case, the syntax highlighting extensions will colorize
|
|
140
140
|
// the code, but all other CSS properties will be inherited.
|
|
141
|
+
// IMPORTANT: Therefore, the fenced code block will use the base editor font.
|
|
141
142
|
{
|
|
142
143
|
tag: [markdownTags.CodeText, markdownTags.InlineCode],
|
|
143
144
|
class: code,
|
|
@@ -2,112 +2,106 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { syntaxTree } from '@codemirror/language';
|
|
6
5
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
EditorView,
|
|
7
|
+
Decoration,
|
|
8
|
+
WidgetType,
|
|
9
|
+
MatchDecorator,
|
|
10
|
+
ViewPlugin,
|
|
11
|
+
type DecorationSet,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
} from '@codemirror/view';
|
|
15
14
|
|
|
16
15
|
class CheckboxWidget extends WidgetType {
|
|
17
|
-
constructor(
|
|
16
|
+
constructor(
|
|
17
|
+
private readonly _pos: number,
|
|
18
|
+
private _checked: boolean,
|
|
19
|
+
private readonly _indent: number,
|
|
20
|
+
private readonly _onCheck: (check: boolean) => void,
|
|
21
|
+
) {
|
|
18
22
|
super();
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
override eq(other: CheckboxWidget) {
|
|
22
|
-
|
|
23
|
-
// TODO(burdon): Is this efficient? Check life-cycle.
|
|
24
|
-
return this._pos === other._pos && this._checked === other._checked;
|
|
26
|
+
return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
toDOM(view: EditorView) {
|
|
28
|
-
// console.log('toDom', this._pos);
|
|
29
30
|
const wrap = document.createElement('span');
|
|
30
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
31
31
|
wrap.className = 'cm-task-item';
|
|
32
|
+
wrap.setAttribute('aria-hidden', 'true');
|
|
33
|
+
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
32
34
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this.
|
|
38
|
-
view.dispatch({
|
|
39
|
-
changes: {
|
|
40
|
-
from: this._pos + 1,
|
|
41
|
-
to: this._pos + 2,
|
|
42
|
-
insert: this._checked ? 'x' : ' ',
|
|
43
|
-
},
|
|
44
|
-
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
45
|
-
selection: {
|
|
46
|
-
anchor: this._pos + 4,
|
|
47
|
-
// anchor: this._getCursor(),
|
|
48
|
-
},
|
|
49
|
-
});
|
|
35
|
+
const input = wrap.appendChild(document.createElement('input'));
|
|
36
|
+
input.type = 'checkbox';
|
|
37
|
+
input.checked = this._checked;
|
|
38
|
+
input.onchange = (event: Event) => {
|
|
39
|
+
this._onCheck((event.target as any).checked);
|
|
50
40
|
return true;
|
|
51
41
|
};
|
|
52
42
|
|
|
53
43
|
return wrap;
|
|
54
44
|
}
|
|
55
45
|
|
|
56
|
-
// TODO(burdon): Remove listener?
|
|
57
|
-
// override destroy() {
|
|
58
|
-
// console.log('destroy', this._pos);
|
|
59
|
-
// }
|
|
60
|
-
|
|
61
46
|
override ignoreEvent() {
|
|
62
47
|
return false;
|
|
63
48
|
}
|
|
64
49
|
}
|
|
65
50
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
51
|
+
// TODO(burdon): Reconcile with theme.
|
|
52
|
+
const styles = EditorView.baseTheme({
|
|
53
|
+
'& .cm-task-item': {
|
|
54
|
+
paddingLeft: '4px',
|
|
55
|
+
paddingRight: '8px',
|
|
56
|
+
},
|
|
57
|
+
});
|
|
69
58
|
|
|
70
|
-
export const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
59
|
+
export const tasklist = () => {
|
|
60
|
+
// TODO(burdon): Matcher isn't as precise as syntax tree.
|
|
61
|
+
// Allows for indents to be greater than AST would allow.
|
|
62
|
+
const taskMatcher = new MatchDecorator({
|
|
63
|
+
regexp: /^(\s*)- \[([ xX])\]\s/g,
|
|
64
|
+
decoration: (match, view, pos) => {
|
|
65
|
+
const indent = Math.floor(match[1].length / 2);
|
|
66
|
+
const checked = match[2] === 'x' || match[2] === 'X';
|
|
67
|
+
return Decoration.replace({
|
|
68
|
+
widget: new CheckboxWidget(pos, checked, indent, (checked) => {
|
|
69
|
+
const idx = pos + match[0].indexOf('[') + 1;
|
|
70
|
+
view.dispatch({
|
|
71
|
+
changes: {
|
|
72
|
+
from: idx,
|
|
73
|
+
to: idx + 1,
|
|
74
|
+
insert: checked ? 'x' : ' ',
|
|
75
|
+
},
|
|
76
|
+
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
77
|
+
selection: {
|
|
78
|
+
anchor: idx + 3,
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
},
|
|
74
84
|
});
|
|
75
85
|
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
builder.add(
|
|
83
|
-
node.from,
|
|
84
|
-
node.to,
|
|
85
|
-
Decoration.replace({
|
|
86
|
-
widget: new CheckboxWidget(node.from, () => lastPosition, isChecked(state, node.from)),
|
|
87
|
-
}),
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
});
|
|
86
|
+
const tasks = ViewPlugin.fromClass(
|
|
87
|
+
class {
|
|
88
|
+
tasks: DecorationSet;
|
|
89
|
+
constructor(view: EditorView) {
|
|
90
|
+
this.tasks = taskMatcher.createDeco(view);
|
|
91
|
+
}
|
|
92
92
|
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
update(update: ViewUpdate) {
|
|
94
|
+
this.tasks = taskMatcher.updateDeco(update, this.tasks);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
decorations: (instance) => instance.tasks,
|
|
99
|
+
provide: (plugin) =>
|
|
100
|
+
EditorView.atomicRanges.of((view) => {
|
|
101
|
+
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
102
|
+
}),
|
|
103
|
+
},
|
|
104
|
+
);
|
|
95
105
|
|
|
96
|
-
return [
|
|
97
|
-
listener,
|
|
98
|
-
StateField.define<RangeSet<any>>({
|
|
99
|
-
create: (state) => checkbox(state),
|
|
100
|
-
update: (_: RangeSet<any>, tr: Transaction) => checkbox(tr.state),
|
|
101
|
-
provide: (field) => EditorView.decorations.from(field),
|
|
102
|
-
}),
|
|
103
|
-
];
|
|
106
|
+
return [tasks, styles];
|
|
104
107
|
};
|
|
105
|
-
|
|
106
|
-
// TODO(burdon): Reconcile with theme.
|
|
107
|
-
export const styles = EditorView.baseTheme({
|
|
108
|
-
'& .cm-task-item': {
|
|
109
|
-
padding: '0 4px',
|
|
110
|
-
},
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
export const tasklist = () => [statefield(), styles];
|
|
@@ -159,6 +159,18 @@ export const textTheme: {
|
|
|
159
159
|
'& .cm-scroller': {
|
|
160
160
|
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
161
161
|
},
|
|
162
|
+
'& .cm-placeholder': {
|
|
163
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
export const markdownTheme: {
|
|
168
|
+
[selector: string]: StyleSpec;
|
|
169
|
+
} = {
|
|
170
|
+
// NOTE: Must leave base font family as is (i.e., monospace) due to fenced code blocks.
|
|
171
|
+
'& .cm-placeholder': {
|
|
172
|
+
fontFamily: get(tokens, 'fontFamily.body', []).join(','),
|
|
173
|
+
},
|
|
162
174
|
};
|
|
163
175
|
|
|
164
176
|
export const codeTheme: {
|
|
@@ -167,4 +179,7 @@ export const codeTheme: {
|
|
|
167
179
|
'& .cm-scroller': {
|
|
168
180
|
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
169
181
|
},
|
|
182
|
+
'& .cm-placeholder': {
|
|
183
|
+
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
184
|
+
},
|
|
170
185
|
};
|
|
@@ -29,6 +29,7 @@ type Awareness = awarenessProtocol.Awareness;
|
|
|
29
29
|
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
30
30
|
export type EditorModel = {
|
|
31
31
|
id: string;
|
|
32
|
+
// TODO(burdon): Remove.
|
|
32
33
|
content: string | YText | YXmlFragment | DocAccessor;
|
|
33
34
|
text: () => string;
|
|
34
35
|
extension?: Extension;
|
|
@@ -57,28 +58,30 @@ export const useTextModel = ({ identity, space, text }: UseTextModelOptions): Ed
|
|
|
57
58
|
};
|
|
58
59
|
|
|
59
60
|
const createModel = (options: UseTextModelOptions) => {
|
|
60
|
-
const {
|
|
61
|
-
|
|
61
|
+
const { text } = options;
|
|
62
62
|
if (isActualAutomergeObject(text)) {
|
|
63
63
|
return createAutomergeModel(options);
|
|
64
64
|
} else {
|
|
65
|
-
if (!
|
|
65
|
+
if (!text?.doc) {
|
|
66
66
|
return undefined;
|
|
67
67
|
}
|
|
68
|
+
|
|
68
69
|
return createYjsModel(options);
|
|
69
70
|
}
|
|
70
71
|
};
|
|
71
72
|
|
|
72
73
|
const createYjsModel = ({ identity, space, text }: UseTextModelOptions): EditorModel => {
|
|
73
|
-
invariant(
|
|
74
|
-
const provider =
|
|
74
|
+
invariant(text?.doc && text?.content);
|
|
75
|
+
const provider = space
|
|
76
|
+
? new SpaceAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` })
|
|
77
|
+
: undefined;
|
|
75
78
|
|
|
76
79
|
return {
|
|
77
80
|
id: text.doc.guid,
|
|
78
81
|
content: text.content,
|
|
79
82
|
text: () => text.content!.toString(),
|
|
80
|
-
extension: yCollab(text.content as YText, provider
|
|
81
|
-
awareness: provider
|
|
83
|
+
extension: yCollab(text.content as YText, provider?.awareness),
|
|
84
|
+
awareness: provider?.awareness,
|
|
82
85
|
peer: identity
|
|
83
86
|
? {
|
|
84
87
|
id: identity.identityKey.toHex(),
|
|
@@ -88,7 +91,7 @@ const createYjsModel = ({ identity, space, text }: UseTextModelOptions): EditorM
|
|
|
88
91
|
};
|
|
89
92
|
};
|
|
90
93
|
|
|
91
|
-
const createAutomergeModel = ({ identity,
|
|
94
|
+
const createAutomergeModel = ({ identity, text }: UseTextModelOptions): EditorModel => {
|
|
92
95
|
const obj = text as any as AutomergeTextCompat;
|
|
93
96
|
const doc = getRawDoc(obj, [obj.field]);
|
|
94
97
|
|