@dxos/ui-editor 0.0.0
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/LICENSE +8 -0
- package/README.md +21 -0
- package/package.json +121 -0
- package/src/defaults.ts +34 -0
- package/src/extensions/annotations.ts +55 -0
- package/src/extensions/autocomplete/autocomplete.ts +151 -0
- package/src/extensions/autocomplete/index.ts +8 -0
- package/src/extensions/autocomplete/match.ts +46 -0
- package/src/extensions/autocomplete/placeholder.ts +117 -0
- package/src/extensions/autocomplete/typeahead.ts +87 -0
- package/src/extensions/automerge/automerge.test.tsx +76 -0
- package/src/extensions/automerge/automerge.ts +105 -0
- package/src/extensions/automerge/cursor.ts +28 -0
- package/src/extensions/automerge/defs.ts +31 -0
- package/src/extensions/automerge/index.ts +5 -0
- package/src/extensions/automerge/sync.ts +79 -0
- package/src/extensions/automerge/update-automerge.ts +50 -0
- package/src/extensions/automerge/update-codemirror.ts +115 -0
- package/src/extensions/autoscroll.ts +165 -0
- package/src/extensions/awareness/awareness-provider.ts +127 -0
- package/src/extensions/awareness/awareness.ts +315 -0
- package/src/extensions/awareness/index.ts +6 -0
- package/src/extensions/blast.ts +363 -0
- package/src/extensions/blocks.ts +131 -0
- package/src/extensions/bookmarks.ts +77 -0
- package/src/extensions/comments.ts +579 -0
- package/src/extensions/debug.ts +15 -0
- package/src/extensions/dnd.ts +39 -0
- package/src/extensions/factories.ts +284 -0
- package/src/extensions/focus.ts +36 -0
- package/src/extensions/folding.ts +63 -0
- package/src/extensions/hashtag.ts +68 -0
- package/src/extensions/index.ts +34 -0
- package/src/extensions/json.ts +57 -0
- package/src/extensions/listener.ts +32 -0
- package/src/extensions/markdown/action.ts +117 -0
- package/src/extensions/markdown/bundle.ts +105 -0
- package/src/extensions/markdown/changes.test.ts +26 -0
- package/src/extensions/markdown/changes.ts +149 -0
- package/src/extensions/markdown/debug.ts +44 -0
- package/src/extensions/markdown/decorate.ts +622 -0
- package/src/extensions/markdown/formatting.test.ts +498 -0
- package/src/extensions/markdown/formatting.ts +1265 -0
- package/src/extensions/markdown/highlight.ts +183 -0
- package/src/extensions/markdown/image.ts +118 -0
- package/src/extensions/markdown/index.ts +13 -0
- package/src/extensions/markdown/link.ts +50 -0
- package/src/extensions/markdown/parser.test.ts +75 -0
- package/src/extensions/markdown/styles.ts +135 -0
- package/src/extensions/markdown/table.ts +150 -0
- package/src/extensions/mention.ts +41 -0
- package/src/extensions/modal.ts +24 -0
- package/src/extensions/modes.ts +41 -0
- package/src/extensions/outliner/commands.ts +270 -0
- package/src/extensions/outliner/editor.test.ts +33 -0
- package/src/extensions/outliner/editor.ts +184 -0
- package/src/extensions/outliner/index.ts +7 -0
- package/src/extensions/outliner/menu.ts +128 -0
- package/src/extensions/outliner/outliner.test.ts +100 -0
- package/src/extensions/outliner/outliner.ts +167 -0
- package/src/extensions/outliner/selection.ts +50 -0
- package/src/extensions/outliner/tree.test.ts +168 -0
- package/src/extensions/outliner/tree.ts +317 -0
- package/src/extensions/preview/index.ts +5 -0
- package/src/extensions/preview/preview.ts +193 -0
- package/src/extensions/replacer.test.ts +75 -0
- package/src/extensions/replacer.ts +93 -0
- package/src/extensions/scrolling.ts +189 -0
- package/src/extensions/selection.ts +100 -0
- package/src/extensions/state.ts +7 -0
- package/src/extensions/submit.ts +62 -0
- package/src/extensions/tags/extended-markdown.test.ts +263 -0
- package/src/extensions/tags/extended-markdown.ts +78 -0
- package/src/extensions/tags/index.ts +7 -0
- package/src/extensions/tags/streamer.ts +243 -0
- package/src/extensions/tags/xml-tags.ts +507 -0
- package/src/extensions/tags/xml-util.test.ts +48 -0
- package/src/extensions/tags/xml-util.ts +93 -0
- package/src/extensions/typewriter.ts +68 -0
- package/src/index.ts +14 -0
- package/src/styles/index.ts +7 -0
- package/src/styles/markdown.ts +26 -0
- package/src/styles/theme.ts +293 -0
- package/src/styles/tokens.ts +17 -0
- package/src/types/index.ts +5 -0
- package/src/types/types.ts +32 -0
- package/src/util/cursor.ts +56 -0
- package/src/util/debug.ts +56 -0
- package/src/util/decorations.ts +21 -0
- package/src/util/dom.ts +36 -0
- package/src/util/facet.ts +13 -0
- package/src/util/index.ts +10 -0
- package/src/util/util.ts +29 -0
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { invertedEffects } from '@codemirror/commands';
|
|
6
|
+
import { type ChangeDesc, type Extension, StateEffect, StateField, type Text } from '@codemirror/state';
|
|
7
|
+
import {
|
|
8
|
+
type Command,
|
|
9
|
+
Decoration,
|
|
10
|
+
EditorView,
|
|
11
|
+
type PluginValue,
|
|
12
|
+
type Rect,
|
|
13
|
+
ViewPlugin,
|
|
14
|
+
hoverTooltip,
|
|
15
|
+
keymap,
|
|
16
|
+
} from '@codemirror/view';
|
|
17
|
+
import sortBy from 'lodash.sortby';
|
|
18
|
+
|
|
19
|
+
import { type CleanupFn, debounce } from '@dxos/async';
|
|
20
|
+
import { log } from '@dxos/log';
|
|
21
|
+
import { isNonNullable } from '@dxos/util';
|
|
22
|
+
|
|
23
|
+
import { type Comment, type Range, type RenderCallback } from '../types';
|
|
24
|
+
import { Cursor, singleValueFacet, wrapWithCatch } from '../util';
|
|
25
|
+
|
|
26
|
+
import { documentId } from './selection';
|
|
27
|
+
|
|
28
|
+
//
|
|
29
|
+
// State management.
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
type CommentState = {
|
|
33
|
+
comment: Comment;
|
|
34
|
+
range: Range;
|
|
35
|
+
location?: Rect | null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type SelectionState = {
|
|
39
|
+
current?: string;
|
|
40
|
+
closest?: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type CommentsState = {
|
|
44
|
+
id?: string;
|
|
45
|
+
comments: CommentState[];
|
|
46
|
+
selection: SelectionState;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const setComments = StateEffect.define<{ id: string; comments: Comment[] }>();
|
|
50
|
+
|
|
51
|
+
export const setSelection = StateEffect.define<SelectionState>();
|
|
52
|
+
|
|
53
|
+
const setCommentState = StateEffect.define<CommentsState>();
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* State field (reducer) that tracks comment ranges.
|
|
57
|
+
* The ranges are tracked as Automerge cursors from which the absolute indexed ranges can be computed.
|
|
58
|
+
*/
|
|
59
|
+
export const commentsState = StateField.define<CommentsState>({
|
|
60
|
+
create: (state) => ({
|
|
61
|
+
id: state.facet(documentId),
|
|
62
|
+
comments: [],
|
|
63
|
+
selection: {},
|
|
64
|
+
}),
|
|
65
|
+
update: (value, tr) => {
|
|
66
|
+
for (const effect of tr.effects) {
|
|
67
|
+
// Update selection.
|
|
68
|
+
if (effect.is(setSelection)) {
|
|
69
|
+
return { ...value, selection: effect.value };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Update range from store.
|
|
73
|
+
if (effect.is(setComments)) {
|
|
74
|
+
const { comments } = effect.value;
|
|
75
|
+
const commentStates: CommentState[] = comments
|
|
76
|
+
.map((comment) => {
|
|
77
|
+
// Skip cut/deleted comments.
|
|
78
|
+
if (!comment.cursor) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const range = Cursor.getRangeFromCursor(tr.state, comment.cursor);
|
|
83
|
+
return range && { comment, range };
|
|
84
|
+
})
|
|
85
|
+
.filter(isNonNullable);
|
|
86
|
+
|
|
87
|
+
return { ...value, comments: commentStates };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Update entire state.
|
|
91
|
+
if (effect.is(setCommentState)) {
|
|
92
|
+
return effect.value;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return value;
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* NOTE: Matches search.
|
|
102
|
+
*/
|
|
103
|
+
const styles = EditorView.theme({
|
|
104
|
+
'.cm-comment, .cm-comment-current': {
|
|
105
|
+
padding: '3px 0',
|
|
106
|
+
color: 'var(--dx-cmCommentText)',
|
|
107
|
+
backgroundColor: 'var(--dx-cmCommentSurface)',
|
|
108
|
+
},
|
|
109
|
+
'.cm-comment > span, .cm-comment-current > span': {
|
|
110
|
+
boxDecorationBreak: 'clone',
|
|
111
|
+
boxShadow: '0 0 1px 3px var(--dx-cmCommentSurface)',
|
|
112
|
+
backgroundColor: 'var(--dx-cmCommentSurface)',
|
|
113
|
+
color: 'var(--dx-cmCommentText)',
|
|
114
|
+
cursor: 'pointer',
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const createCommentMark = (id: string, isCurrent: boolean) =>
|
|
119
|
+
Decoration.mark({
|
|
120
|
+
class: isCurrent ? 'cm-comment-current' : 'cm-comment',
|
|
121
|
+
attributes: {
|
|
122
|
+
'data-testid': 'cm-comment',
|
|
123
|
+
'data-comment-id': id,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Decorate ranges.
|
|
129
|
+
*/
|
|
130
|
+
const commentsDecorations = EditorView.decorations.compute([commentsState], (state) => {
|
|
131
|
+
const {
|
|
132
|
+
selection: { current },
|
|
133
|
+
comments,
|
|
134
|
+
} = state.field(commentsState);
|
|
135
|
+
|
|
136
|
+
const decorations = sortBy(comments ?? [], (range) => range.range.from)
|
|
137
|
+
?.flatMap((comment) => {
|
|
138
|
+
const range = comment.range;
|
|
139
|
+
if (!range) {
|
|
140
|
+
log.warn('Invalid range:', range);
|
|
141
|
+
return undefined;
|
|
142
|
+
} else if (range.from === range.to) {
|
|
143
|
+
// Skip empty ranges. This can happen when a comment is cut or deleted.
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const mark = createCommentMark(comment.comment.id, comment.comment.id === current);
|
|
148
|
+
return mark.range(range.from, range.to);
|
|
149
|
+
})
|
|
150
|
+
.filter(isNonNullable);
|
|
151
|
+
|
|
152
|
+
return Decoration.set(decorations);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
export const commentClickedEffect = StateEffect.define<string>();
|
|
156
|
+
|
|
157
|
+
const handleCommentClick = EditorView.domEventHandlers({
|
|
158
|
+
click: (event, view) => {
|
|
159
|
+
let target = event.target as HTMLElement;
|
|
160
|
+
const editorRoot = view.dom;
|
|
161
|
+
|
|
162
|
+
// Traverse up the DOM tree looking for an element with data-comment-id
|
|
163
|
+
// Stop if we reach the editor root or find the comment id
|
|
164
|
+
while (target && target !== editorRoot && !target.hasAttribute('data-comment-id')) {
|
|
165
|
+
target = target.parentElement as HTMLElement;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Check if we found a comment id and are still within the editor
|
|
169
|
+
if (target && target !== editorRoot) {
|
|
170
|
+
const commentId = target.getAttribute('data-comment-id');
|
|
171
|
+
if (commentId) {
|
|
172
|
+
view.dispatch({ effects: commentClickedEffect.of(commentId) });
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return false;
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
//
|
|
182
|
+
// Cut-and-paste.
|
|
183
|
+
//
|
|
184
|
+
|
|
185
|
+
type TrackedComment = { id: string; from: number; to: number };
|
|
186
|
+
|
|
187
|
+
const trackPastedComments = (onUpdate: NonNullable<CommentsOptions['onUpdate']>) => {
|
|
188
|
+
// Tracks indexed selections within text.
|
|
189
|
+
let tracked: { text: Text; comments: TrackedComment[] } | null = null;
|
|
190
|
+
|
|
191
|
+
// Track cut or copy (enables cut-and-paste and copy-delete-paste to restore comment selection).
|
|
192
|
+
const handleTrack = (event: Event, view: EditorView) => {
|
|
193
|
+
const comments = view.state.field(commentsState);
|
|
194
|
+
const { main } = view.state.selection;
|
|
195
|
+
const selectedRanges = comments.comments.filter(
|
|
196
|
+
({ range }) => range.from >= main.from && range.to <= main.to && range.from < range.to,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
if (!selectedRanges.length) {
|
|
200
|
+
tracked = null;
|
|
201
|
+
} else {
|
|
202
|
+
tracked = {
|
|
203
|
+
text: view.state.doc.slice(main.from, main.to),
|
|
204
|
+
comments: selectedRanges.map(({ comment, range }) => ({
|
|
205
|
+
id: comment.id,
|
|
206
|
+
from: range.from - main.from,
|
|
207
|
+
to: range.to - main.from,
|
|
208
|
+
})),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return [
|
|
214
|
+
EditorView.domEventHandlers({
|
|
215
|
+
cut: handleTrack,
|
|
216
|
+
copy: handleTrack,
|
|
217
|
+
}),
|
|
218
|
+
|
|
219
|
+
// Track deleted comments.
|
|
220
|
+
invertedEffects.of((tr) => {
|
|
221
|
+
const { comments } = tr.startState.field(commentsState);
|
|
222
|
+
const effects: StateEffect<any>[] = [];
|
|
223
|
+
tr.changes.iterChangedRanges((fromA, toA) => {
|
|
224
|
+
for (const {
|
|
225
|
+
comment: { id },
|
|
226
|
+
range: { from, to },
|
|
227
|
+
} of comments) {
|
|
228
|
+
if (from < to && from >= fromA && to <= toA) {
|
|
229
|
+
effects.push(restoreCommentEffect.of({ id, from, to }));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return effects;
|
|
235
|
+
}),
|
|
236
|
+
|
|
237
|
+
// Handle paste or the undo of comment deletion.
|
|
238
|
+
EditorView.updateListener.of((update) => {
|
|
239
|
+
const restore: TrackedComment[] = [];
|
|
240
|
+
|
|
241
|
+
for (let i = 0; i < update.transactions.length; i++) {
|
|
242
|
+
const tr = update.transactions[i];
|
|
243
|
+
for (let j = 0; j < restore.length; j++) {
|
|
244
|
+
restore[j] = mapTrackedComment(restore[j], tr.changes);
|
|
245
|
+
}
|
|
246
|
+
for (const effect of tr.effects) {
|
|
247
|
+
if (effect.is(restoreCommentEffect)) {
|
|
248
|
+
restore.push(effect.value);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (tracked) {
|
|
254
|
+
const paste = update.transactions.find((tr) => tr.isUserEvent('input.paste'));
|
|
255
|
+
if (paste) {
|
|
256
|
+
let found = -1;
|
|
257
|
+
paste.changes.iterChanges((fromA, toA, fromB, toB, text) => {
|
|
258
|
+
if (text.eq(tracked!.text)) {
|
|
259
|
+
for (let i = update.transactions.indexOf(paste!) + 1; i < update.transactions.length; i++) {
|
|
260
|
+
fromB = update.transactions[i].changes.mapPos(fromB);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
found = fromB;
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
if (found > -1) {
|
|
268
|
+
for (const moved of tracked.comments) {
|
|
269
|
+
restore.push({ id: moved.id, from: found + moved.from, to: found + moved.to });
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
tracked = null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
for (const comment of restore) {
|
|
278
|
+
const { comments } = update.startState.field(commentsState);
|
|
279
|
+
const exists = comments.some((c) => c.comment.id === comment.id && c.range.from < c.range.to);
|
|
280
|
+
if (!exists) {
|
|
281
|
+
const cursor = Cursor.getCursorFromRange(update.state, comment);
|
|
282
|
+
onUpdate({ id: comment.id, cursor });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}),
|
|
286
|
+
];
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const mapTrackedComment = (comment: TrackedComment, changes: ChangeDesc) => ({
|
|
290
|
+
id: comment.id,
|
|
291
|
+
from: changes.mapPos(comment.from, 1),
|
|
292
|
+
to: changes.mapPos(comment.to, 1),
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* These are attached to undone/redone transactions in the editor for the purpose of restoring comments
|
|
297
|
+
* that were deleted by the original changes.
|
|
298
|
+
*/
|
|
299
|
+
const restoreCommentEffect = StateEffect.define<TrackedComment>({ map: mapTrackedComment });
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Create comment thread action.
|
|
303
|
+
*/
|
|
304
|
+
export const createComment: Command = (view) => {
|
|
305
|
+
const options = view.state.facet(optionsFacet);
|
|
306
|
+
const { from, to } = view.state.selection.main;
|
|
307
|
+
if (from === to) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Don't allow selection at end of document.
|
|
312
|
+
if (to === view.state.doc.length) {
|
|
313
|
+
view.dispatch({
|
|
314
|
+
changes: {
|
|
315
|
+
from: to,
|
|
316
|
+
insert: '\n',
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const cursor = Cursor.getCursorFromRange(view.state, { from, to });
|
|
322
|
+
if (cursor) {
|
|
323
|
+
// Create thread via callback.
|
|
324
|
+
options.onCreate?.({ cursor, from, location: view.coordsAtPos(from) });
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return false;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
//
|
|
332
|
+
// Options
|
|
333
|
+
//
|
|
334
|
+
|
|
335
|
+
export type CommentsOptions = {
|
|
336
|
+
/**
|
|
337
|
+
* Document id.
|
|
338
|
+
*/
|
|
339
|
+
id?: string;
|
|
340
|
+
/**
|
|
341
|
+
* Key shortcut to create a new thread.
|
|
342
|
+
*/
|
|
343
|
+
key?: string;
|
|
344
|
+
/**
|
|
345
|
+
* Called to render tooltip.
|
|
346
|
+
*/
|
|
347
|
+
renderTooltip?: RenderCallback<{ shortcut: string }>;
|
|
348
|
+
/**
|
|
349
|
+
* Called to create a new thread and return the thread id.
|
|
350
|
+
*/
|
|
351
|
+
onCreate?: (params: { cursor: string; from: number; location?: Rect | null }) => void;
|
|
352
|
+
/**
|
|
353
|
+
* Selection cut/deleted.
|
|
354
|
+
*/
|
|
355
|
+
onDelete?: (params: { id: string }) => void;
|
|
356
|
+
/**
|
|
357
|
+
* Called when a comment is moved.
|
|
358
|
+
*/
|
|
359
|
+
onUpdate?: (params: { id: string; cursor: string }) => void;
|
|
360
|
+
/**
|
|
361
|
+
* Called to notify which thread is currently closest to the cursor.
|
|
362
|
+
*/
|
|
363
|
+
onSelect?: (state: CommentsState) => void;
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const optionsFacet = singleValueFacet<CommentsOptions>();
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Comment threads.
|
|
370
|
+
* 1). Updates the EditorModel to store relative selections for a set of comments threads.
|
|
371
|
+
* Since the selections are relative, they do not need to be updated when the document is edited.
|
|
372
|
+
* 2). Implements a StateField to track absolute selections corresponding to the comments (i.e., when the document is edited).
|
|
373
|
+
* 3). Creates decoration marks to apply classes to each selection.
|
|
374
|
+
* 4). Tracks the current cursor position to:
|
|
375
|
+
* a). Update the decoration to show if the cursor is within a current selection.
|
|
376
|
+
* b). Calls a handler to indicate which is the closest selection (e.g., to update the thread sidebar).
|
|
377
|
+
* 5). Optionally, implements a hoverTooltip to show hints when creating a selection range.
|
|
378
|
+
*/
|
|
379
|
+
export const comments = (options: CommentsOptions = {}): Extension => {
|
|
380
|
+
const { key: shortcut = "meta-'" } = options;
|
|
381
|
+
|
|
382
|
+
const handleSelect = debounce((state: CommentsState) => options.onSelect?.(state), 200);
|
|
383
|
+
|
|
384
|
+
return [
|
|
385
|
+
optionsFacet.of(options),
|
|
386
|
+
options.id ? documentId.of(options.id) : undefined,
|
|
387
|
+
commentsState,
|
|
388
|
+
commentsDecorations,
|
|
389
|
+
handleCommentClick,
|
|
390
|
+
styles,
|
|
391
|
+
|
|
392
|
+
//
|
|
393
|
+
// Keymap.
|
|
394
|
+
//
|
|
395
|
+
options.onCreate &&
|
|
396
|
+
keymap.of([
|
|
397
|
+
{
|
|
398
|
+
key: shortcut,
|
|
399
|
+
run: wrapWithCatch(createComment),
|
|
400
|
+
},
|
|
401
|
+
]),
|
|
402
|
+
|
|
403
|
+
//
|
|
404
|
+
// Hover tooltip (for key shortcut hints, etc.)
|
|
405
|
+
// TODO(burdon): Factor out to generic hints extension for current selection/line.
|
|
406
|
+
//
|
|
407
|
+
options.renderTooltip &&
|
|
408
|
+
hoverTooltip(
|
|
409
|
+
(view, pos) => {
|
|
410
|
+
const selection = view.state.selection.main;
|
|
411
|
+
if (selection && pos >= selection.from && pos <= selection.to) {
|
|
412
|
+
return {
|
|
413
|
+
pos: selection.from,
|
|
414
|
+
end: selection.to,
|
|
415
|
+
above: true,
|
|
416
|
+
create: () => {
|
|
417
|
+
const el = document.createElement('div');
|
|
418
|
+
options.renderTooltip!(el, { shortcut }, view);
|
|
419
|
+
return { dom: el, offset: { x: 0, y: 8 } };
|
|
420
|
+
},
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
return null;
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
// TODO(burdon): Hide on change triggered immediately?
|
|
428
|
+
// hideOnChange: true,
|
|
429
|
+
hoverTime: 1_000,
|
|
430
|
+
},
|
|
431
|
+
),
|
|
432
|
+
|
|
433
|
+
//
|
|
434
|
+
// Track deleted ranges and update ranges for decorations.
|
|
435
|
+
//
|
|
436
|
+
EditorView.updateListener.of(({ view, state, changes }) => {
|
|
437
|
+
let mod = false;
|
|
438
|
+
const { comments, ...value } = state.field(commentsState);
|
|
439
|
+
changes.iterChanges((from, to, from2, to2) => {
|
|
440
|
+
comments.forEach(({ comment, range }) => {
|
|
441
|
+
// Test if range deleted.
|
|
442
|
+
if (from2 === to2) {
|
|
443
|
+
const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor!);
|
|
444
|
+
if (!newRange || newRange.to - newRange.from === 0) {
|
|
445
|
+
options.onDelete?.({ id: comment.id });
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Update range.
|
|
450
|
+
if (from <= range.to) {
|
|
451
|
+
const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor!);
|
|
452
|
+
Object.assign(range, newRange);
|
|
453
|
+
mod = true;
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
if (mod) {
|
|
459
|
+
view.dispatch({ effects: setCommentState.of({ comments, ...value }) });
|
|
460
|
+
}
|
|
461
|
+
}),
|
|
462
|
+
|
|
463
|
+
//
|
|
464
|
+
// Track selection/proximity.
|
|
465
|
+
//
|
|
466
|
+
EditorView.updateListener.of(({ view, state }) => {
|
|
467
|
+
let min = Infinity;
|
|
468
|
+
const {
|
|
469
|
+
selection: { current, closest },
|
|
470
|
+
comments,
|
|
471
|
+
} = state.field(commentsState);
|
|
472
|
+
|
|
473
|
+
const { head } = state.selection.main;
|
|
474
|
+
const selection: SelectionState = {};
|
|
475
|
+
comments.forEach(({ comment, range }) => {
|
|
476
|
+
if (head >= range.from && head <= range.to) {
|
|
477
|
+
selection.current = comment.id;
|
|
478
|
+
selection.closest = undefined;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (!selection.current) {
|
|
482
|
+
const d = Math.min(Math.abs(head - range.from), Math.abs(head - range.to));
|
|
483
|
+
if (d < min) {
|
|
484
|
+
selection.closest = comment.id;
|
|
485
|
+
min = d;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
if (selection.current !== current || selection.closest !== closest) {
|
|
491
|
+
view.dispatch({ effects: setSelection.of(selection) });
|
|
492
|
+
|
|
493
|
+
// Update callback.
|
|
494
|
+
handleSelect({
|
|
495
|
+
selection,
|
|
496
|
+
id: state.facet(documentId),
|
|
497
|
+
comments: comments.map(({ comment, range }) => ({
|
|
498
|
+
comment,
|
|
499
|
+
range,
|
|
500
|
+
location: view.coordsAtPos(range.from),
|
|
501
|
+
})),
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}),
|
|
505
|
+
|
|
506
|
+
options.onUpdate && trackPastedComments(options.onUpdate),
|
|
507
|
+
].filter(isNonNullable);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
//
|
|
511
|
+
// Utils.
|
|
512
|
+
//
|
|
513
|
+
|
|
514
|
+
export const scrollThreadIntoView = (view: EditorView, id: string, center = true) => {
|
|
515
|
+
const comment = view.state.field(commentsState).comments.find((range) => range.comment.id === id);
|
|
516
|
+
if (!comment?.comment.cursor) {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const range = Cursor.getRangeFromCursor(view.state, comment.comment.cursor);
|
|
520
|
+
if (range) {
|
|
521
|
+
const currentSelection = view.state.selection.main;
|
|
522
|
+
const currentScrollPosition = view.scrollDOM.scrollTop;
|
|
523
|
+
const targetScrollPosition = view.coordsAtPos(range.from)?.top;
|
|
524
|
+
|
|
525
|
+
const needsScroll =
|
|
526
|
+
targetScrollPosition !== undefined &&
|
|
527
|
+
(targetScrollPosition < currentScrollPosition ||
|
|
528
|
+
targetScrollPosition > currentScrollPosition + view.scrollDOM.clientHeight);
|
|
529
|
+
|
|
530
|
+
const needsSelectionUpdate = currentSelection.from !== range.from || currentSelection.to !== range.from;
|
|
531
|
+
|
|
532
|
+
if (needsScroll || needsSelectionUpdate) {
|
|
533
|
+
view.dispatch({
|
|
534
|
+
selection: needsSelectionUpdate ? { anchor: range.from } : undefined,
|
|
535
|
+
effects: [
|
|
536
|
+
needsScroll ? EditorView.scrollIntoView(range.from, center ? { y: 'center' } : undefined) : [],
|
|
537
|
+
needsSelectionUpdate ? setSelection.of({ current: id }) : [],
|
|
538
|
+
].flat(),
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Manages external comment synchronization for the editor.
|
|
546
|
+
* This class subscribes to external comment updates and applies them to the editor view.
|
|
547
|
+
*/
|
|
548
|
+
class ExternalCommentSync implements PluginValue {
|
|
549
|
+
private readonly unsubscribe: () => void;
|
|
550
|
+
|
|
551
|
+
constructor(view: EditorView, id: string, subscribe: (sink: () => void) => CleanupFn, getComments: () => Comment[]) {
|
|
552
|
+
const updateComments = () => {
|
|
553
|
+
const comments = getComments();
|
|
554
|
+
if (id === view.state.facet(documentId)) {
|
|
555
|
+
queueMicrotask(() => view.dispatch({ effects: setComments.of({ id, comments }) }));
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
this.unsubscribe = subscribe(updateComments);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
destroy = () => {
|
|
563
|
+
this.unsubscribe();
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// TODO(burdon): Needs comment.
|
|
568
|
+
export const createExternalCommentSync = (
|
|
569
|
+
id: string,
|
|
570
|
+
subscribe: (sink: () => void) => CleanupFn,
|
|
571
|
+
getComments: () => Comment[],
|
|
572
|
+
): Extension =>
|
|
573
|
+
ViewPlugin.fromClass(
|
|
574
|
+
class {
|
|
575
|
+
constructor(view: EditorView) {
|
|
576
|
+
return new ExternalCommentSync(view, id, subscribe, getComments);
|
|
577
|
+
}
|
|
578
|
+
},
|
|
579
|
+
);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type EditorState, type Extension, type RangeSet, StateField, type Transaction } from '@codemirror/state';
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line no-console
|
|
9
|
+
export const debugNodeLogger = (log: (...args: any[]) => void = console.log): Extension => {
|
|
10
|
+
const logTokens = (state: EditorState) => syntaxTree(state).iterate({ enter: (node) => log(node.type) });
|
|
11
|
+
return StateField.define<any>({
|
|
12
|
+
create: (state) => logTokens(state),
|
|
13
|
+
update: (_: RangeSet<any>, tr: Transaction) => logTokens(tr.state),
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type { Extension } from '@codemirror/state';
|
|
6
|
+
import { EditorView, dropCursor } from '@codemirror/view';
|
|
7
|
+
|
|
8
|
+
export type DropOptions = {
|
|
9
|
+
onDrop?: (view: EditorView, event: { files: FileList }) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const dropFile = (options: DropOptions = {}): Extension => {
|
|
13
|
+
return [
|
|
14
|
+
styles,
|
|
15
|
+
dropCursor(),
|
|
16
|
+
EditorView.domEventHandlers({
|
|
17
|
+
drop: (event, view) => {
|
|
18
|
+
event.preventDefault();
|
|
19
|
+
const files = event.dataTransfer?.files;
|
|
20
|
+
const pos = view.posAtCoords(event);
|
|
21
|
+
if (files?.length && pos !== null) {
|
|
22
|
+
view.dispatch({ selection: { anchor: pos } });
|
|
23
|
+
options.onDrop?.(view, { files });
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const styles = EditorView.theme({
|
|
31
|
+
'.cm-dropCursor': {
|
|
32
|
+
borderLeft: '2px solid var(--dx-accentText)',
|
|
33
|
+
color: 'var(--dx-accentText)',
|
|
34
|
+
padding: '0 4px',
|
|
35
|
+
},
|
|
36
|
+
'.cm-dropCursor:after': {
|
|
37
|
+
content: '"←"',
|
|
38
|
+
},
|
|
39
|
+
});
|