@dxos/react-ui-editor 0.3.10 → 0.3.11-main.3f845e1
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 +1928 -1717
- 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 -9
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +2 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts +38 -8
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/hr.d.ts +2 -0
- package/dist/types/src/components/TextEditor/extensions/hr.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts +3 -2
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts +3 -0
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/extensions/{demo.d.ts → typewriter.d.ts} +2 -3
- package/dist/types/src/components/TextEditor/extensions/typewriter.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/util.d.ts +5 -0
- package/dist/types/src/components/TextEditor/extensions/util.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/themes/default.d.ts +5 -13
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts +2 -3
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -1
- package/dist/types/src/hooks/automerge/{PatchSemaphore.d.ts → semaphore.d.ts} +1 -1
- package/dist/types/src/hooks/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/update-automerge.d.ts +6 -0
- package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge/update-codemirror.d.ts +5 -0
- package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -6
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts +2 -0
- package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/package.json +24 -20
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +128 -67
- package/src/components/TextEditor/TextEditor.stories.tsx +1 -1
- package/src/components/TextEditor/TextEditor.tsx +26 -22
- package/src/components/TextEditor/extensions/code.ts +12 -12
- package/src/components/TextEditor/extensions/comments.ts +307 -92
- package/src/components/TextEditor/extensions/hr.ts +74 -0
- package/src/components/TextEditor/extensions/image.ts +3 -1
- package/src/components/TextEditor/extensions/index.ts +3 -2
- package/src/components/TextEditor/extensions/markdown/highlight.ts +5 -8
- package/src/components/TextEditor/extensions/mermaid.ts +11 -0
- package/src/components/TextEditor/extensions/table.ts +14 -1
- package/src/components/TextEditor/extensions/{tooltip.tsx → tooltip.ts} +1 -1
- package/src/components/TextEditor/extensions/{demo.ts → typewriter.ts} +1 -2
- package/src/components/TextEditor/extensions/util.ts +18 -0
- package/src/components/TextEditor/themes/default.ts +61 -38
- package/src/hooks/automerge/automerge.stories.tsx +2 -3
- package/src/hooks/automerge/plugin.ts +1 -1
- package/src/hooks/automerge/{PatchSemaphore.ts → semaphore.ts} +4 -3
- package/src/hooks/automerge/{translation/codemirror-to-automerge.ts → update-automerge.ts} +3 -3
- package/src/hooks/automerge/{translation/automerge-to-codemirror.ts → update-codemirror.ts} +2 -7
- package/src/hooks/useTextModel.ts +59 -22
- package/src/hooks/yjs/yjs.test.ts +59 -0
- package/src/index.ts +1 -0
- package/src/testing/replicator.ts +0 -1
- package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts +0 -10
- package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts +0 -24
- package/dist/types/src/components/TextEditor/extensions/highlight.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +0 -5
- package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/translation/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/translation/index.d.ts.map +0 -1
- package/src/components/TextEditor/extensions/experimental.tsx +0 -114
- package/src/components/TextEditor/extensions/highlight.ts +0 -128
- package/src/hooks/automerge/translation/index.ts +0 -6
|
@@ -21,28 +21,32 @@ const CODE_REGEX = /```[\s\S]*?```/gs;
|
|
|
21
21
|
|
|
22
22
|
// TODO(burdon): Reconcile with theme.
|
|
23
23
|
const styles = EditorView.baseTheme({
|
|
24
|
-
'& .cm-
|
|
25
|
-
|
|
26
|
-
paddingInline: '4px !important',
|
|
24
|
+
'& .cm-codeblock': {
|
|
25
|
+
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
27
26
|
},
|
|
28
|
-
'&light .cm-
|
|
27
|
+
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
29
28
|
background: get(tokens, 'extend.colors.neutral.50'),
|
|
30
29
|
},
|
|
31
|
-
'&dark .cm-
|
|
30
|
+
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
32
31
|
background: get(tokens, 'extend.colors.neutral.850'),
|
|
33
32
|
},
|
|
34
|
-
'& .cm-
|
|
33
|
+
'& [aria-readonly="true"] .cm-codeblock': {
|
|
34
|
+
display: 'block',
|
|
35
|
+
paddingInline: '4px !important',
|
|
36
|
+
},
|
|
37
|
+
'& [aria-readonly="true"] .cm-codeblock-first': {
|
|
35
38
|
paddingTop: '4px !important',
|
|
36
39
|
borderTopLeftRadius: '4px',
|
|
37
40
|
borderTopRightRadius: '4px',
|
|
38
41
|
},
|
|
39
|
-
'& .cm-
|
|
42
|
+
'& [aria-readonly="true"] .cm-codeblock-last': {
|
|
40
43
|
paddingBottom: '4px !important',
|
|
41
44
|
borderBottomLeftRadius: '4px',
|
|
42
45
|
borderBottomRightRadius: '4px',
|
|
43
46
|
},
|
|
44
47
|
});
|
|
45
48
|
|
|
49
|
+
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
46
50
|
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
47
51
|
const start = lines.findIndex((line) => line.from >= from);
|
|
48
52
|
const end = lines.findIndex((line) => line.to >= to);
|
|
@@ -62,11 +66,7 @@ export const code = () => {
|
|
|
62
66
|
const block = blocks[i];
|
|
63
67
|
decorations.push(
|
|
64
68
|
Decoration.line({
|
|
65
|
-
class: mx(
|
|
66
|
-
'cm-code-block',
|
|
67
|
-
i === range[0] && 'cm-code-block-first',
|
|
68
|
-
i === range[1] && 'cm-code-block-last',
|
|
69
|
-
),
|
|
69
|
+
class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
|
|
70
70
|
}).range(block.from),
|
|
71
71
|
);
|
|
72
72
|
}
|
|
@@ -2,21 +2,32 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Extension } from '@codemirror/state';
|
|
5
|
+
import { type Extension, StateEffect, StateField } from '@codemirror/state';
|
|
6
6
|
import {
|
|
7
|
+
hoverTooltip,
|
|
7
8
|
keymap,
|
|
9
|
+
type Command,
|
|
8
10
|
type DecorationSet,
|
|
9
|
-
type Rect,
|
|
10
11
|
type ViewUpdate,
|
|
11
12
|
Decoration,
|
|
12
13
|
EditorView,
|
|
13
14
|
MatchDecorator,
|
|
14
15
|
ViewPlugin,
|
|
15
16
|
WidgetType,
|
|
17
|
+
type Rect,
|
|
16
18
|
} from '@codemirror/view';
|
|
19
|
+
import sortBy from 'lodash.sortby';
|
|
17
20
|
|
|
18
21
|
import { debounce } from '@dxos/async';
|
|
19
22
|
import { invariant } from '@dxos/invariant';
|
|
23
|
+
import { log } from '@dxos/log';
|
|
24
|
+
import { nonNullable } from '@dxos/util';
|
|
25
|
+
|
|
26
|
+
import { callbackWrapper } from './util';
|
|
27
|
+
import { type CommentRange, type EditorModel, modelState, type Range } from '../../../hooks';
|
|
28
|
+
|
|
29
|
+
// TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
|
|
30
|
+
// TODO(burdon): Consider breaking into separate plugin (since not standalone)? Like mermaid?
|
|
20
31
|
|
|
21
32
|
// TODO(burdon): Reconcile with theme.
|
|
22
33
|
const styles = EditorView.baseTheme({
|
|
@@ -29,64 +40,226 @@ const styles = EditorView.baseTheme({
|
|
|
29
40
|
'& .cm-bookmark-selected': {
|
|
30
41
|
backgroundColor: 'orange',
|
|
31
42
|
},
|
|
43
|
+
'& .cm-comment': {
|
|
44
|
+
backgroundColor: 'yellow',
|
|
45
|
+
},
|
|
46
|
+
'& .cm-comment-active': {
|
|
47
|
+
backgroundColor: 'orange',
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const marks = {
|
|
52
|
+
highlight: Decoration.mark({ class: 'cm-comment' }),
|
|
53
|
+
highlightActive: Decoration.mark({ class: 'cm-comment-active' }),
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// TODO(burdon): Should this be part of state?
|
|
57
|
+
type CommentSelected = {
|
|
58
|
+
active?: string;
|
|
59
|
+
closest?: string;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// TODO(burdon): Rename.
|
|
63
|
+
type ExtendedCommentRange = Range &
|
|
64
|
+
CommentRange & {
|
|
65
|
+
// TODO(burdon): Not part of state; just required for callback.
|
|
66
|
+
location?: Rect | null;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export type CommentsState = CommentSelected & {
|
|
70
|
+
ranges: ExtendedCommentRange[];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const setCommentRange = StateEffect.define<{ model: EditorModel; comments: CommentRange[] }>();
|
|
74
|
+
|
|
75
|
+
const setSelection = StateEffect.define<CommentSelected>();
|
|
76
|
+
|
|
77
|
+
const setCommentState = StateEffect.define<CommentsState>();
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* State field (reducer) that tracks comment ranges.
|
|
81
|
+
* The ranges are tracked as codemirror ranges (i.e., not relative YJS/Automerge positions), and
|
|
82
|
+
* therefore must be updated when the document changes.
|
|
83
|
+
*/
|
|
84
|
+
const commentsStateField = StateField.define<CommentsState>({
|
|
85
|
+
create: () => ({ ranges: [] }),
|
|
86
|
+
update: (value, tr) => {
|
|
87
|
+
for (const effect of tr.effects) {
|
|
88
|
+
// Update selection.
|
|
89
|
+
if (effect.is(setSelection)) {
|
|
90
|
+
return { ...value, ...effect.value };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Update range from store.
|
|
94
|
+
if (effect.is(setCommentRange)) {
|
|
95
|
+
const { model, comments } = effect.value;
|
|
96
|
+
const ranges: ExtendedCommentRange[] = comments.map((comment) => {
|
|
97
|
+
const range = model.getRangeFromCursor!(comment.cursor)!;
|
|
98
|
+
return { ...comment, ...range };
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return { ...value, ranges };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Update entire state.
|
|
105
|
+
if (effect.is(setCommentState)) {
|
|
106
|
+
return effect.value;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return value;
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Decorate ranges.
|
|
116
|
+
*/
|
|
117
|
+
const highlightDecorations = EditorView.decorations.compute([commentsStateField], (state) => {
|
|
118
|
+
const { active, ranges } = state.field(commentsStateField);
|
|
119
|
+
const decorations =
|
|
120
|
+
sortBy(ranges ?? [], (range) => range.from)
|
|
121
|
+
?.flatMap((selection) => {
|
|
122
|
+
// TODO(burdon): Invalid range (e.g., deleted).
|
|
123
|
+
if (selection.from === selection.to) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const range = { from: selection.from, to: selection.to };
|
|
128
|
+
if (selection.id === active) {
|
|
129
|
+
return marks.highlightActive.range(range.from, range.to);
|
|
130
|
+
} else {
|
|
131
|
+
return marks.highlight.range(range.from, range.to);
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
.filter(nonNullable) ?? [];
|
|
135
|
+
|
|
136
|
+
return Decoration.set(decorations);
|
|
32
137
|
});
|
|
33
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Optional bookmark widget at the start/end of the selection.
|
|
141
|
+
* May correspond to a markdown bookmark.
|
|
142
|
+
*/
|
|
34
143
|
class BookmarkWidget extends WidgetType {
|
|
35
|
-
constructor(
|
|
144
|
+
constructor(
|
|
145
|
+
private readonly _anchor: number,
|
|
146
|
+
private readonly _id: string,
|
|
147
|
+
private readonly _handleClick?: () => void,
|
|
148
|
+
) {
|
|
36
149
|
super();
|
|
37
150
|
invariant(this._id);
|
|
38
151
|
}
|
|
39
152
|
|
|
40
153
|
override eq(other: BookmarkWidget) {
|
|
41
|
-
return this._id === other._id;
|
|
154
|
+
return this._anchor === other._anchor && this._id === other._id;
|
|
42
155
|
}
|
|
43
156
|
|
|
44
157
|
override toDOM() {
|
|
45
158
|
const span = document.createElement('span');
|
|
46
159
|
span.className = 'cm-bookmark';
|
|
47
|
-
span.textContent = '
|
|
48
|
-
|
|
160
|
+
span.textContent = '※';
|
|
161
|
+
if (this._handleClick) {
|
|
162
|
+
span.onclick = () => this._handleClick!();
|
|
163
|
+
}
|
|
49
164
|
return span;
|
|
50
165
|
}
|
|
51
166
|
}
|
|
52
167
|
|
|
53
|
-
type CommentsInfo = {
|
|
54
|
-
active?: string;
|
|
55
|
-
items?: { id: string; pos: number; location: Rect | null }[];
|
|
56
|
-
};
|
|
57
|
-
|
|
58
168
|
export type CommentsOptions = {
|
|
59
169
|
key?: string;
|
|
60
|
-
|
|
61
|
-
|
|
170
|
+
footnote?: boolean;
|
|
171
|
+
/**
|
|
172
|
+
* Called to create a new thread and return the thread id.
|
|
173
|
+
*/
|
|
174
|
+
onCreate?: (range: string) => string | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Called to notify which thread is currently closest to the cursor.
|
|
177
|
+
*/
|
|
178
|
+
onSelect?: (state: CommentsState) => void;
|
|
179
|
+
/**
|
|
180
|
+
* Called to render tooltip.
|
|
181
|
+
*/
|
|
182
|
+
onHover?: (el: Element) => void;
|
|
62
183
|
};
|
|
63
184
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
185
|
+
/**
|
|
186
|
+
* Comment threads.
|
|
187
|
+
* 1). Updates the EditorModel to store relative selections for a set of comments threads.
|
|
188
|
+
* Since the selections are relative, they do not need to be updated when the document is edited.
|
|
189
|
+
* 2). Implements a StateField to track absolute selections corresponding to the comments (i.e., when the document is edited).
|
|
190
|
+
* 3). Creates decoration marks to apply classes to each selection.
|
|
191
|
+
* 4). Tracks the current cursor position to:
|
|
192
|
+
* a). Update the decoration to show if the cursor is within a current selection.
|
|
193
|
+
* b). Calls a handler to indicate which is the closest selection (e.g., to update the thread sidebar).
|
|
194
|
+
* 5). Optionally, inserts a markdown footnote when creating a comment thread.
|
|
195
|
+
* 6). Optionally, implements a hoverTooltip to show hints when creating a selection range.
|
|
196
|
+
*/
|
|
67
197
|
export const comments = (options: CommentsOptions = {}): Extension => {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
198
|
+
const handleSelect = debounce((state: CommentsState) => options.onSelect?.(state), 200);
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Create comment thread action.
|
|
202
|
+
*/
|
|
203
|
+
const createCommentThread: Command = (view) => {
|
|
204
|
+
invariant(options.onCreate);
|
|
205
|
+
const { head, from, to } = view.state.selection.main;
|
|
206
|
+
if (from === to) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const model = view.state.field(modelState);
|
|
211
|
+
const relPos = model?.getCursorFromRange?.({ from, to });
|
|
212
|
+
if (relPos) {
|
|
213
|
+
// Create thread via callback.
|
|
214
|
+
const id = callbackWrapper(options.onCreate)(relPos);
|
|
215
|
+
if (id) {
|
|
216
|
+
// Update range.
|
|
217
|
+
view.dispatch({
|
|
218
|
+
effects: setSelection.of({ active: id }),
|
|
219
|
+
selection: { anchor: from },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
// Insert footnote.
|
|
223
|
+
if (options.footnote) {
|
|
224
|
+
const tag = `[^${id}]`;
|
|
225
|
+
view.dispatch({
|
|
226
|
+
changes: { from: head, insert: tag },
|
|
227
|
+
selection: { anchor: head + tag.length },
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return false;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Bookmark widget decoration.
|
|
240
|
+
*/
|
|
241
|
+
const bookmarksViewPlugin = ViewPlugin.fromClass(
|
|
242
|
+
class BookmarkViewPlugin {
|
|
243
|
+
// Match markdown footnotes (option).
|
|
244
|
+
// https://www.markdownguide.org/extended-syntax/#footnotes
|
|
245
|
+
static bookmarkMatcher = new MatchDecorator({
|
|
246
|
+
regexp: /\[\^(\w+)\]/g,
|
|
247
|
+
decoration: (match, view, pos) => {
|
|
248
|
+
const id = match[1];
|
|
249
|
+
return Decoration.replace({
|
|
250
|
+
id,
|
|
251
|
+
widget: new BookmarkWidget(pos, id),
|
|
252
|
+
});
|
|
253
|
+
},
|
|
77
254
|
});
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
255
|
|
|
81
|
-
const bookmarks = ViewPlugin.fromClass(
|
|
82
|
-
class {
|
|
83
256
|
bookmarks: DecorationSet;
|
|
84
257
|
constructor(view: EditorView) {
|
|
85
|
-
this.bookmarks = bookmarkMatcher.createDeco(view);
|
|
258
|
+
this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.createDeco(view);
|
|
86
259
|
}
|
|
87
260
|
|
|
88
261
|
update(update: ViewUpdate) {
|
|
89
|
-
this.bookmarks = bookmarkMatcher.updateDeco(update, this.bookmarks);
|
|
262
|
+
this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.updateDeco(update, this.bookmarks);
|
|
90
263
|
}
|
|
91
264
|
},
|
|
92
265
|
{
|
|
@@ -98,76 +271,118 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
98
271
|
},
|
|
99
272
|
);
|
|
100
273
|
|
|
101
|
-
const handleUpdate = debounce((data: CommentsInfo) => {
|
|
102
|
-
options.onUpdate?.(data);
|
|
103
|
-
}, 200);
|
|
104
|
-
|
|
105
274
|
return [
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
// Insert footnote.
|
|
111
|
-
const { from, to, head } = view.state.selection.main;
|
|
112
|
-
const id = options.onCreate?.(from, to);
|
|
113
|
-
if (id) {
|
|
114
|
-
const tag = `[^${id}]`;
|
|
115
|
-
// TODO(burdon): Add selection.
|
|
116
|
-
view.dispatch({
|
|
117
|
-
changes: { from: head, insert: tag },
|
|
118
|
-
selection: { anchor: head + tag.length },
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
275
|
+
bookmarksViewPlugin,
|
|
276
|
+
commentsStateField,
|
|
277
|
+
highlightDecorations,
|
|
278
|
+
styles,
|
|
123
279
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
280
|
+
//
|
|
281
|
+
// Keymap.
|
|
282
|
+
//
|
|
283
|
+
options.onCreate
|
|
284
|
+
? keymap.of([
|
|
285
|
+
{
|
|
286
|
+
key: options?.key ?? "meta-'",
|
|
287
|
+
run: createCommentThread,
|
|
288
|
+
},
|
|
289
|
+
])
|
|
290
|
+
: [],
|
|
128
291
|
|
|
129
|
-
|
|
292
|
+
//
|
|
293
|
+
// Hover tooltip (for key shortcut hints, etc.)
|
|
294
|
+
// TODO(burdon): Factor out to generic hints extension for current selection/line.
|
|
295
|
+
//
|
|
296
|
+
options.onHover
|
|
297
|
+
? hoverTooltip(
|
|
298
|
+
(view, pos) => {
|
|
299
|
+
const selection = view.state.selection.main;
|
|
300
|
+
if (selection && pos >= selection.from && pos <= selection.to) {
|
|
301
|
+
return {
|
|
302
|
+
pos: selection.from,
|
|
303
|
+
end: selection.to,
|
|
304
|
+
above: true,
|
|
305
|
+
create: () => {
|
|
306
|
+
// TODO(burdon): Dispatch to react callback to render (or use SSR)?
|
|
307
|
+
const el = document.createElement('div');
|
|
308
|
+
options.onHover?.(el);
|
|
309
|
+
return { dom: el, offset: { x: 0, y: 8 } };
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
130
313
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
314
|
+
return null;
|
|
315
|
+
},
|
|
316
|
+
{ hideOnChange: true, hoverTime: 1000 },
|
|
317
|
+
)
|
|
318
|
+
: [],
|
|
137
319
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
320
|
+
//
|
|
321
|
+
// Monitor cursor movement and text updates.
|
|
322
|
+
// TODO(burdon): Is there a better (finer grained) way to do this?
|
|
323
|
+
//
|
|
324
|
+
EditorView.updateListener.of(({ view, state, startState, changes }) => {
|
|
325
|
+
//
|
|
326
|
+
// Test if need to recompute indexed range if document changes before the end of the range.
|
|
327
|
+
//
|
|
328
|
+
{
|
|
329
|
+
let mod = false;
|
|
330
|
+
const model = state.field(modelState);
|
|
331
|
+
const { active, ranges } = state.field(commentsStateField);
|
|
332
|
+
changes.iterChanges((from, to, from2, to2) => {
|
|
333
|
+
invariant(model);
|
|
334
|
+
ranges.forEach((range) => {
|
|
335
|
+
if (from2 === to2) {
|
|
336
|
+
const newRange = model.getRangeFromCursor!(range.cursor);
|
|
337
|
+
if (newRange.to - newRange.from === 0) {
|
|
338
|
+
// TODO(burdon): Delete range if empty.
|
|
339
|
+
log.info('deleted comment', { thread: range.id });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (from <= range.to) {
|
|
344
|
+
const newRange = model.getRangeFromCursor!(range.cursor);
|
|
345
|
+
Object.assign(range, newRange);
|
|
346
|
+
mod = true;
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
if (mod) {
|
|
352
|
+
view.dispatch({ effects: setCommentState.of({ active, ranges }) });
|
|
153
353
|
}
|
|
154
|
-
}
|
|
354
|
+
}
|
|
155
355
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
),
|
|
356
|
+
//
|
|
357
|
+
// Track the current selection.
|
|
358
|
+
//
|
|
359
|
+
{
|
|
360
|
+
const { head } = state.selection.main;
|
|
361
|
+
|
|
362
|
+
let min = Infinity;
|
|
363
|
+
const { active, closest, ranges } = state.field(commentsStateField);
|
|
364
|
+
const selected: CommentSelected = { active: undefined, closest: undefined };
|
|
365
|
+
ranges.forEach((comment) => {
|
|
366
|
+
const d = Math.min(Math.abs(head - comment.from), Math.abs(head - comment.to));
|
|
367
|
+
if (head >= comment.from && head <= comment.to) {
|
|
368
|
+
selected.active = comment.id;
|
|
369
|
+
}
|
|
370
|
+
if (d < min) {
|
|
371
|
+
selected.closest = comment.id;
|
|
372
|
+
min = d;
|
|
373
|
+
}
|
|
167
374
|
});
|
|
375
|
+
|
|
376
|
+
if (selected.active !== active || selected.closest !== closest) {
|
|
377
|
+
view.dispatch({ effects: setSelection.of(selected) });
|
|
378
|
+
|
|
379
|
+
// Update callback.
|
|
380
|
+
handleSelect({
|
|
381
|
+
...selected,
|
|
382
|
+
ranges: ranges.map((range) => ({ ...range, location: view.coordsAtPos(range.from) })),
|
|
383
|
+
});
|
|
384
|
+
}
|
|
168
385
|
}
|
|
169
386
|
}),
|
|
170
|
-
|
|
171
|
-
styles,
|
|
172
387
|
];
|
|
173
388
|
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
Decoration,
|
|
7
|
+
type DecorationSet,
|
|
8
|
+
EditorView,
|
|
9
|
+
MatchDecorator,
|
|
10
|
+
ViewPlugin,
|
|
11
|
+
type ViewUpdate,
|
|
12
|
+
WidgetType,
|
|
13
|
+
} from '@codemirror/view';
|
|
14
|
+
import get from 'lodash.get';
|
|
15
|
+
|
|
16
|
+
import { tokens } from '../../../styles';
|
|
17
|
+
|
|
18
|
+
// TODO(burdon): Reconcile with theme.
|
|
19
|
+
const styles = EditorView.baseTheme({
|
|
20
|
+
'& .cm-hr': {
|
|
21
|
+
// TODO(burdon): ???
|
|
22
|
+
// Note that block-level decorations should not have vertical margins,
|
|
23
|
+
borderBottom: `1px solid ${get(tokens, 'extend.colors.neutral.200')}`,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
class HorizontalRuleWidget extends WidgetType {
|
|
28
|
+
constructor(readonly _pos: number) {
|
|
29
|
+
super();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
override eq(other: WidgetType) {
|
|
33
|
+
return this._pos === (other as any as HorizontalRuleWidget)._pos;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
toDOM(view: EditorView) {
|
|
37
|
+
// TODO(burdon): Create <hr> element? Does this clash with markdown parser?
|
|
38
|
+
const el = document.createElement('div');
|
|
39
|
+
el.className = 'cm-hr';
|
|
40
|
+
return el;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const placeholderMatcher = new MatchDecorator({
|
|
45
|
+
regexp: /^---$/g,
|
|
46
|
+
decoration: (match, view, pos) =>
|
|
47
|
+
Decoration.replace({
|
|
48
|
+
widget: new HorizontalRuleWidget(pos),
|
|
49
|
+
}),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const hr = () => [
|
|
53
|
+
styles,
|
|
54
|
+
ViewPlugin.fromClass(
|
|
55
|
+
class {
|
|
56
|
+
rules: DecorationSet;
|
|
57
|
+
constructor(view: EditorView) {
|
|
58
|
+
this.rules = placeholderMatcher.createDeco(view);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
update(update: ViewUpdate) {
|
|
62
|
+
this.rules = placeholderMatcher.updateDeco(update, this.rules);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
decorations: (instance) => instance.rules,
|
|
67
|
+
// TODO(burdon): Is this really atomic (cursor can move into ---).
|
|
68
|
+
provide: (plugin) =>
|
|
69
|
+
EditorView.atomicRanges.of((view) => {
|
|
70
|
+
return view.plugin(plugin)?.rules || Decoration.none;
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
),
|
|
74
|
+
];
|
|
@@ -38,6 +38,7 @@ const update = (state: EditorState, options: ImageOptions) => {
|
|
|
38
38
|
hide ? node.from : node.to,
|
|
39
39
|
node.to,
|
|
40
40
|
Decoration.replace({
|
|
41
|
+
block: true, // Prevent cursor from entering.
|
|
41
42
|
widget: new ImageWidget(url),
|
|
42
43
|
}),
|
|
43
44
|
);
|
|
@@ -54,8 +55,9 @@ class ImageWidget extends WidgetType {
|
|
|
54
55
|
super();
|
|
55
56
|
}
|
|
56
57
|
|
|
58
|
+
// TODO(burdon): Does this need to be unique for each position?
|
|
57
59
|
override eq(other: WidgetType) {
|
|
58
|
-
return this._url === (other as any as ImageWidget)
|
|
60
|
+
return this._url === (other as any as ImageWidget)._url;
|
|
59
61
|
}
|
|
60
62
|
|
|
61
63
|
toDOM(view: EditorView) {
|
|
@@ -6,14 +6,15 @@ export * from './autocomplete';
|
|
|
6
6
|
export * from './basic';
|
|
7
7
|
export * from './blast';
|
|
8
8
|
export * from './code';
|
|
9
|
-
export * from './demo';
|
|
10
9
|
export * from './comments';
|
|
11
|
-
export * from './
|
|
10
|
+
export * from './hr';
|
|
12
11
|
export * from './image';
|
|
13
12
|
export * from './link';
|
|
14
13
|
export * from './listener';
|
|
15
14
|
export * from './markdown';
|
|
16
15
|
export * from './mention';
|
|
16
|
+
export * from './mermaid';
|
|
17
17
|
export * from './table';
|
|
18
18
|
export * from './tasklist';
|
|
19
19
|
export * from './tooltip';
|
|
20
|
+
export * from './typewriter';
|
|
@@ -8,15 +8,12 @@ import { tags, styleTags, Tag } from '@lezer/highlight';
|
|
|
8
8
|
import { type MarkdownConfig, Table } from '@lezer/markdown';
|
|
9
9
|
import get from 'lodash.get';
|
|
10
10
|
|
|
11
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
12
|
-
|
|
13
11
|
import {
|
|
14
12
|
blockquote,
|
|
15
13
|
bold,
|
|
16
14
|
code,
|
|
17
15
|
codeMark,
|
|
18
16
|
heading,
|
|
19
|
-
horizontalRule,
|
|
20
17
|
inlineUrl,
|
|
21
18
|
italic,
|
|
22
19
|
mark,
|
|
@@ -35,7 +32,7 @@ export const markdownTags = {
|
|
|
35
32
|
CodeText: Tag.define(),
|
|
36
33
|
EmphasisMark: Tag.define(),
|
|
37
34
|
HeaderMark: Tag.define(),
|
|
38
|
-
HorizontalRule: Tag.define(),
|
|
35
|
+
// HorizontalRule: Tag.define(),
|
|
39
36
|
InlineCode: Tag.define(),
|
|
40
37
|
LinkLabel: Tag.define(),
|
|
41
38
|
LinkReference: Tag.define(),
|
|
@@ -182,10 +179,10 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
|
|
|
182
179
|
},
|
|
183
180
|
|
|
184
181
|
// TODO(burdon): Replace with extension.
|
|
185
|
-
{
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
},
|
|
182
|
+
// {
|
|
183
|
+
// tag: [markdownTags.HorizontalRule],
|
|
184
|
+
// class: mx(horizontalRule, readonly && '-indent-[100rem]'),
|
|
185
|
+
// },
|
|
189
186
|
|
|
190
187
|
// TODO(burdon): Only if being edited.
|
|
191
188
|
{
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension } from '@codemirror/state';
|
|
6
|
+
|
|
7
|
+
// TODO(burdon): How will this interact with the code extension?
|
|
8
|
+
// TODO(burdon): Move to mermaid-plugin (which should contribute this extension).
|
|
9
|
+
export const mermaid = (): Extension => {
|
|
10
|
+
return [];
|
|
11
|
+
};
|