@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.
Files changed (93) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +21 -0
  3. package/package.json +121 -0
  4. package/src/defaults.ts +34 -0
  5. package/src/extensions/annotations.ts +55 -0
  6. package/src/extensions/autocomplete/autocomplete.ts +151 -0
  7. package/src/extensions/autocomplete/index.ts +8 -0
  8. package/src/extensions/autocomplete/match.ts +46 -0
  9. package/src/extensions/autocomplete/placeholder.ts +117 -0
  10. package/src/extensions/autocomplete/typeahead.ts +87 -0
  11. package/src/extensions/automerge/automerge.test.tsx +76 -0
  12. package/src/extensions/automerge/automerge.ts +105 -0
  13. package/src/extensions/automerge/cursor.ts +28 -0
  14. package/src/extensions/automerge/defs.ts +31 -0
  15. package/src/extensions/automerge/index.ts +5 -0
  16. package/src/extensions/automerge/sync.ts +79 -0
  17. package/src/extensions/automerge/update-automerge.ts +50 -0
  18. package/src/extensions/automerge/update-codemirror.ts +115 -0
  19. package/src/extensions/autoscroll.ts +165 -0
  20. package/src/extensions/awareness/awareness-provider.ts +127 -0
  21. package/src/extensions/awareness/awareness.ts +315 -0
  22. package/src/extensions/awareness/index.ts +6 -0
  23. package/src/extensions/blast.ts +363 -0
  24. package/src/extensions/blocks.ts +131 -0
  25. package/src/extensions/bookmarks.ts +77 -0
  26. package/src/extensions/comments.ts +579 -0
  27. package/src/extensions/debug.ts +15 -0
  28. package/src/extensions/dnd.ts +39 -0
  29. package/src/extensions/factories.ts +284 -0
  30. package/src/extensions/focus.ts +36 -0
  31. package/src/extensions/folding.ts +63 -0
  32. package/src/extensions/hashtag.ts +68 -0
  33. package/src/extensions/index.ts +34 -0
  34. package/src/extensions/json.ts +57 -0
  35. package/src/extensions/listener.ts +32 -0
  36. package/src/extensions/markdown/action.ts +117 -0
  37. package/src/extensions/markdown/bundle.ts +105 -0
  38. package/src/extensions/markdown/changes.test.ts +26 -0
  39. package/src/extensions/markdown/changes.ts +149 -0
  40. package/src/extensions/markdown/debug.ts +44 -0
  41. package/src/extensions/markdown/decorate.ts +622 -0
  42. package/src/extensions/markdown/formatting.test.ts +498 -0
  43. package/src/extensions/markdown/formatting.ts +1265 -0
  44. package/src/extensions/markdown/highlight.ts +183 -0
  45. package/src/extensions/markdown/image.ts +118 -0
  46. package/src/extensions/markdown/index.ts +13 -0
  47. package/src/extensions/markdown/link.ts +50 -0
  48. package/src/extensions/markdown/parser.test.ts +75 -0
  49. package/src/extensions/markdown/styles.ts +135 -0
  50. package/src/extensions/markdown/table.ts +150 -0
  51. package/src/extensions/mention.ts +41 -0
  52. package/src/extensions/modal.ts +24 -0
  53. package/src/extensions/modes.ts +41 -0
  54. package/src/extensions/outliner/commands.ts +270 -0
  55. package/src/extensions/outliner/editor.test.ts +33 -0
  56. package/src/extensions/outliner/editor.ts +184 -0
  57. package/src/extensions/outliner/index.ts +7 -0
  58. package/src/extensions/outliner/menu.ts +128 -0
  59. package/src/extensions/outliner/outliner.test.ts +100 -0
  60. package/src/extensions/outliner/outliner.ts +167 -0
  61. package/src/extensions/outliner/selection.ts +50 -0
  62. package/src/extensions/outliner/tree.test.ts +168 -0
  63. package/src/extensions/outliner/tree.ts +317 -0
  64. package/src/extensions/preview/index.ts +5 -0
  65. package/src/extensions/preview/preview.ts +193 -0
  66. package/src/extensions/replacer.test.ts +75 -0
  67. package/src/extensions/replacer.ts +93 -0
  68. package/src/extensions/scrolling.ts +189 -0
  69. package/src/extensions/selection.ts +100 -0
  70. package/src/extensions/state.ts +7 -0
  71. package/src/extensions/submit.ts +62 -0
  72. package/src/extensions/tags/extended-markdown.test.ts +263 -0
  73. package/src/extensions/tags/extended-markdown.ts +78 -0
  74. package/src/extensions/tags/index.ts +7 -0
  75. package/src/extensions/tags/streamer.ts +243 -0
  76. package/src/extensions/tags/xml-tags.ts +507 -0
  77. package/src/extensions/tags/xml-util.test.ts +48 -0
  78. package/src/extensions/tags/xml-util.ts +93 -0
  79. package/src/extensions/typewriter.ts +68 -0
  80. package/src/index.ts +14 -0
  81. package/src/styles/index.ts +7 -0
  82. package/src/styles/markdown.ts +26 -0
  83. package/src/styles/theme.ts +293 -0
  84. package/src/styles/tokens.ts +17 -0
  85. package/src/types/index.ts +5 -0
  86. package/src/types/types.ts +32 -0
  87. package/src/util/cursor.ts +56 -0
  88. package/src/util/debug.ts +56 -0
  89. package/src/util/decorations.ts +21 -0
  90. package/src/util/dom.ts +36 -0
  91. package/src/util/facet.ts +13 -0
  92. package/src/util/index.ts +10 -0
  93. package/src/util/util.ts +29 -0
@@ -0,0 +1,127 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { DeferredTask, Event, sleep } from '@dxos/async';
6
+ import { Context } from '@dxos/context';
7
+ import { invariant } from '@dxos/invariant';
8
+ import { log } from '@dxos/log';
9
+ import { type Messenger } from '@dxos/protocols';
10
+ import { type GossipMessage } from '@dxos/protocols/proto/dxos/mesh/teleport/gossip';
11
+
12
+ import { type AwarenessInfo, type AwarenessPosition, type AwarenessProvider, type AwarenessState } from './awareness';
13
+
14
+ type ProtocolMessage =
15
+ | {
16
+ kind: 'query';
17
+ }
18
+ | {
19
+ kind: 'post';
20
+ state: AwarenessState;
21
+ };
22
+
23
+ const DEBOUNCE_INTERVAL = 100; // ms
24
+
25
+ export type AwarenessProviderProps = {
26
+ messenger: Messenger;
27
+ channel: string;
28
+ peerId: string;
29
+ info: AwarenessInfo;
30
+ };
31
+
32
+ /**
33
+ * Receives and broadcasts profile and cursor position.
34
+ */
35
+ export class SpaceAwarenessProvider implements AwarenessProvider {
36
+ private readonly _remoteStates = new Map<string, AwarenessState>();
37
+
38
+ private readonly _messenger: Messenger;
39
+ private readonly _channel: string;
40
+ private readonly _peerId: string;
41
+ private readonly _info: AwarenessInfo;
42
+
43
+ private _ctx?: Context;
44
+ private _postTask?: DeferredTask;
45
+ private _localState?: AwarenessState;
46
+
47
+ public readonly remoteStateChange = new Event<void>();
48
+
49
+ constructor({ messenger, channel, peerId, info }: AwarenessProviderProps) {
50
+ this._messenger = messenger;
51
+ this._channel = channel;
52
+ this._peerId = peerId;
53
+ this._info = info;
54
+ }
55
+
56
+ open(): void {
57
+ this._ctx = new Context();
58
+ this._postTask = new DeferredTask(this._ctx, async () => {
59
+ if (this._localState) {
60
+ await this._messenger.postMessage(this._channel, {
61
+ kind: 'post',
62
+ state: this._localState,
63
+ } satisfies ProtocolMessage);
64
+
65
+ // TODO(burdon): Replace with throttle.
66
+ // TODO(burdon): Send heads?
67
+ await sleep(DEBOUNCE_INTERVAL);
68
+ }
69
+ });
70
+
71
+ this._ctx.onDispose(
72
+ this._messenger.listen(this._channel, (message: GossipMessage) => {
73
+ switch (message.payload.kind) {
74
+ case 'query': {
75
+ this._handleQueryMessage();
76
+ break;
77
+ }
78
+ case 'post': {
79
+ this._handlePostMessage(message.payload);
80
+ break;
81
+ }
82
+ }
83
+ }),
84
+ );
85
+
86
+ void this._messenger
87
+ .postMessage(this._channel, {
88
+ kind: 'query',
89
+ } satisfies ProtocolMessage)
90
+ .catch((err) => {
91
+ log.debug('failed to query awareness', { err });
92
+ });
93
+ }
94
+
95
+ close(): void {
96
+ void this._ctx?.dispose();
97
+ this._ctx = undefined;
98
+ this._postTask = undefined;
99
+ }
100
+
101
+ getRemoteStates(): AwarenessState[] {
102
+ return Array.from(this._remoteStates.values());
103
+ }
104
+
105
+ update(position: AwarenessPosition | undefined): void {
106
+ invariant(this._postTask);
107
+ this._localState = {
108
+ peerId: this._peerId,
109
+ position,
110
+ info: this._info,
111
+ };
112
+
113
+ this._postTask.schedule();
114
+ }
115
+
116
+ private _handleQueryMessage(): void {
117
+ invariant(this._postTask);
118
+ this._postTask.schedule();
119
+ }
120
+
121
+ private _handlePostMessage(message: ProtocolMessage): void {
122
+ invariant(message.kind === 'post');
123
+ // TODO(wittjosiah): Is it helpful or confusing to show cursors for self on other devices?
124
+ this._remoteStates.set(message.state.peerId, message.state);
125
+ this.remoteStateChange.emit();
126
+ }
127
+ }
@@ -0,0 +1,315 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Annotation, type Extension, type Range, RangeSet } from '@codemirror/state';
6
+ import {
7
+ Decoration,
8
+ type DecorationSet,
9
+ EditorView,
10
+ type PluginValue,
11
+ ViewPlugin,
12
+ type ViewUpdate,
13
+ WidgetType,
14
+ } from '@codemirror/view';
15
+
16
+ import { Event } from '@dxos/async';
17
+ import { Context } from '@dxos/context';
18
+
19
+ import { Cursor, type CursorConverter, singleValueFacet } from '../../util';
20
+
21
+ export interface AwarenessProvider {
22
+ remoteStateChange: Event<void>;
23
+
24
+ open(): void;
25
+ close(): void;
26
+
27
+ getRemoteStates(): AwarenessState[];
28
+ update(position: AwarenessPosition | undefined): void;
29
+ }
30
+
31
+ const dummyProvider: AwarenessProvider = {
32
+ remoteStateChange: new Event(),
33
+
34
+ open: () => {},
35
+ close: () => {},
36
+
37
+ getRemoteStates: () => [],
38
+ update: () => {},
39
+ };
40
+
41
+ export const awarenessProvider = singleValueFacet<AwarenessProvider>(dummyProvider);
42
+
43
+ // TODO(dmaretskyi): Specify the users that actually changed. Currently, we recalculate positions for every user.
44
+ const RemoteSelectionChangedAnnotation = Annotation.define();
45
+
46
+ export type AwarenessPosition = {
47
+ anchor?: string;
48
+ head?: string;
49
+ };
50
+
51
+ export type AwarenessInfo = {
52
+ displayName: string;
53
+ darkColor: string;
54
+ lightColor: string;
55
+ };
56
+
57
+ export type AwarenessState = {
58
+ position?: AwarenessPosition;
59
+ peerId: string;
60
+ info: AwarenessInfo;
61
+ };
62
+
63
+ /**
64
+ * Extension provides presence information about other peers.
65
+ */
66
+ export const awareness = (provider = dummyProvider): Extension => {
67
+ return [
68
+ awarenessProvider.of(provider),
69
+ ViewPlugin.fromClass(RemoteSelectionsDecorator, {
70
+ decorations: (value) => value.decorations,
71
+ }),
72
+ styles,
73
+ ];
74
+ };
75
+
76
+ /**
77
+ * Generates selection decorations from remote peers.
78
+ */
79
+ export class RemoteSelectionsDecorator implements PluginValue {
80
+ private readonly _ctx = new Context();
81
+ private readonly _cursorConverter: CursorConverter;
82
+ private readonly _provider: AwarenessProvider;
83
+
84
+ private _lastAnchor?: number;
85
+ private _lastHead?: number;
86
+
87
+ public decorations: DecorationSet = RangeSet.of([]);
88
+
89
+ constructor(view: EditorView) {
90
+ this._cursorConverter = view.state.facet(Cursor.converter);
91
+ this._provider = view.state.facet(awarenessProvider);
92
+ this._provider.open();
93
+ this._provider.remoteStateChange.on(this._ctx, () => {
94
+ view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
95
+ });
96
+ }
97
+
98
+ destroy(): void {
99
+ void this._ctx.dispose();
100
+ this._provider.close();
101
+ }
102
+
103
+ update(update: ViewUpdate): void {
104
+ this._updateLocalSelection(update.view);
105
+ this._updateRemoteSelections(update.view);
106
+ }
107
+
108
+ private _updateLocalSelection(view: EditorView): void {
109
+ const hasFocus = view.hasFocus && view.dom.ownerDocument.hasFocus();
110
+ const { anchor = undefined, head = undefined } = hasFocus ? view.state.selection.main : {};
111
+ if (this._lastAnchor === anchor && this._lastHead === head) {
112
+ return;
113
+ }
114
+
115
+ this._lastAnchor = anchor;
116
+ this._lastHead = head;
117
+
118
+ this._provider.update(
119
+ anchor !== undefined && head !== undefined
120
+ ? {
121
+ anchor: this._cursorConverter.toCursor(anchor),
122
+ head: this._cursorConverter.toCursor(head, -1),
123
+ }
124
+ : undefined,
125
+ );
126
+ }
127
+
128
+ private _updateRemoteSelections(view: EditorView): void {
129
+ const decorations: Range<Decoration>[] = [
130
+ // TODO(burdon): Factor out for testing.
131
+ // {
132
+ // from: 0,
133
+ // to: 0,
134
+ // value: Decoration.widget({ side: 0, block: false, widget: new RemoteCaretWidget('Test', 'red') }),
135
+ // },
136
+ ];
137
+
138
+ const awarenessStates = this._provider.getRemoteStates();
139
+ for (const state of awarenessStates) {
140
+ const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
141
+ const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
142
+ if (anchor == null || head == null) {
143
+ continue;
144
+ }
145
+
146
+ const start = Math.min(Math.min(anchor, head), view.state.doc.length);
147
+ const end = Math.min(Math.max(anchor, head), view.state.doc.length);
148
+
149
+ const startLine = view.state.doc.lineAt(start);
150
+ const endLine = view.state.doc.lineAt(end);
151
+
152
+ const darkColor = state.info.darkColor;
153
+ const lightColor = state.info.lightColor;
154
+
155
+ if (startLine.number === endLine.number) {
156
+ // Selected content in a single line.
157
+ decorations.push({
158
+ from: start,
159
+ to: end,
160
+ value: Decoration.mark({
161
+ attributes: { style: `background-color: ${lightColor}` },
162
+ class: 'cm-collab-selection',
163
+ }),
164
+ });
165
+ } else {
166
+ // Selected content in multiple lines; first, render text-selection in the first line.
167
+ decorations.push({
168
+ from: start,
169
+ to: startLine.from + startLine.length,
170
+ value: Decoration.mark({
171
+ attributes: { style: `background-color: ${lightColor}` },
172
+ class: 'cm-collab-selection',
173
+ }),
174
+ });
175
+
176
+ // Render text-selection in the last line.
177
+ decorations.push({
178
+ from: endLine.from,
179
+ to: end,
180
+ value: Decoration.mark({
181
+ attributes: { style: `background-color: ${lightColor}` },
182
+ class: 'cm-collab-selection',
183
+ }),
184
+ });
185
+
186
+ for (let i = startLine.number + 1; i < endLine.number; i++) {
187
+ const linePos = view.state.doc.line(i).from;
188
+ decorations.push({
189
+ from: linePos,
190
+ to: linePos,
191
+ value: Decoration.line({
192
+ attributes: { style: `background-color: ${lightColor}`, class: 'cm-collab-selectionLine' },
193
+ }),
194
+ });
195
+ }
196
+ }
197
+
198
+ decorations.push({
199
+ from: head,
200
+ to: head,
201
+ value: Decoration.widget({
202
+ side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
203
+ block: false,
204
+ widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', darkColor),
205
+ }),
206
+ });
207
+ }
208
+
209
+ this.decorations = Decoration.set(decorations, true);
210
+ }
211
+ }
212
+
213
+ class RemoteCaretWidget extends WidgetType {
214
+ constructor(
215
+ private readonly _name: string,
216
+ private readonly _color: string,
217
+ ) {
218
+ super();
219
+ }
220
+
221
+ override toDOM(): HTMLElement {
222
+ const span = document.createElement('span');
223
+ span.className = 'cm-collab-selectionCaret';
224
+ span.style.backgroundColor = this._color;
225
+ span.style.borderColor = this._color;
226
+
227
+ const dot = document.createElement('div');
228
+ dot.className = 'cm-collab-selectionCaretDot';
229
+
230
+ const info = document.createElement('div');
231
+ info.className = 'cm-collab-selectionInfo';
232
+ info.innerText = this._name;
233
+
234
+ span.appendChild(document.createTextNode('\u2060'));
235
+ span.appendChild(dot);
236
+ span.appendChild(document.createTextNode('\u2060'));
237
+ span.appendChild(info);
238
+ span.appendChild(document.createTextNode('\u2060'));
239
+ return span;
240
+ }
241
+
242
+ override updateDOM(): boolean {
243
+ return false;
244
+ }
245
+
246
+ override eq(widget: this): boolean {
247
+ return widget._color === this._color;
248
+ }
249
+
250
+ override get estimatedHeight() {
251
+ return -1;
252
+ }
253
+
254
+ override ignoreEvent(): boolean {
255
+ return true;
256
+ }
257
+ }
258
+
259
+ const styles = EditorView.theme({
260
+ '.cm-collab-selection': {},
261
+ '.cm-collab-selectionLine': {
262
+ padding: 0,
263
+ margin: '0px 2px 0px 4px',
264
+ },
265
+ '.cm-collab-selectionCaret': {
266
+ position: 'relative',
267
+ borderLeft: '1px solid black',
268
+ borderRight: '1px solid black',
269
+ marginLeft: '-1px',
270
+ marginRight: '-1px',
271
+ boxSizing: 'border-box',
272
+ display: 'inline',
273
+ cursor: 'pointer',
274
+ },
275
+ '.cm-collab-selectionCaretDot': {
276
+ borderRadius: '50%',
277
+ position: 'absolute',
278
+ width: '.5em',
279
+ height: '.5em',
280
+ top: '-.25em',
281
+ left: '-.25em',
282
+ backgroundColor: 'inherit',
283
+ transition: 'transform .3s ease-in-out',
284
+ boxSizing: 'border-box',
285
+ },
286
+ '.cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot': {
287
+ transform: 'scale(0)',
288
+ transformOrigin: 'center',
289
+ },
290
+ '.cm-collab-selectionInfo': {
291
+ position: 'absolute',
292
+ transform: 'translate(-50%, 0)',
293
+ top: '-20px',
294
+ left: 0,
295
+ fontSize: '.75em',
296
+ fontFamily: 'sans-serif',
297
+ fontStyle: 'normal',
298
+ fontWeight: 'normal',
299
+ lineHeight: 'normal',
300
+ userSelect: 'none',
301
+ color: 'white',
302
+ padding: '2px 6px',
303
+ zIndex: 101,
304
+ transition: 'opacity .3s ease-in-out',
305
+ backgroundColor: 'inherit',
306
+ borderRadius: '2px',
307
+ opacity: 0,
308
+ transitionDelay: '0s',
309
+ whiteSpace: 'nowrap',
310
+ },
311
+ '.cm-collab-selectionCaret:hover > .cm-collab-selectionInfo': {
312
+ opacity: 1,
313
+ transitionDelay: '0s',
314
+ },
315
+ });
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './awareness';
6
+ export * from './awareness-provider';