@dxos/react-ui-editor 0.3.11-main.a996413 → 0.3.11-main.afa73e0

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 (186) hide show
  1. package/dist/lib/browser/index.mjs +2014 -1648
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +16 -13
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -3
  9. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  10. package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -1
  11. package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
  12. package/dist/types/src/components/TextEditor/comments.stories.d.ts +26 -0
  13. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -0
  14. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  15. package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
  16. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
  17. package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
  18. package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
  19. package/dist/types/src/extensions/automerge/defs.d.ts +24 -0
  20. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
  21. package/dist/types/src/extensions/automerge/index.d.ts +3 -0
  22. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
  23. package/dist/types/src/extensions/automerge/semaphore.d.ts +14 -0
  24. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
  25. package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
  26. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
  27. package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
  28. package/dist/types/src/extensions/awareness.d.ts +46 -0
  29. package/dist/types/src/extensions/awareness.d.ts.map +1 -0
  30. package/dist/types/src/extensions/basic.d.ts +4 -5
  31. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  32. package/dist/types/src/extensions/comments.d.ts +32 -19
  33. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  34. package/dist/types/src/extensions/cursor.d.ts +25 -0
  35. package/dist/types/src/extensions/cursor.d.ts.map +1 -0
  36. package/dist/types/src/extensions/index.d.ts +4 -7
  37. package/dist/types/src/extensions/index.d.ts.map +1 -1
  38. package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
  39. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  40. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  41. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  42. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  43. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  44. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  45. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  46. package/dist/types/src/extensions/markdown/index.d.ts +7 -0
  47. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  48. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  49. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  50. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  51. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  52. package/dist/types/src/extensions/outliner.d.ts +4 -0
  53. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  54. package/dist/types/src/hooks/awareness-provider.d.ts +31 -0
  55. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
  56. package/dist/types/src/hooks/defs.d.ts +25 -0
  57. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  58. package/dist/types/src/hooks/index.d.ts +3 -1
  59. package/dist/types/src/hooks/index.d.ts.map +1 -1
  60. package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
  61. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  62. package/dist/types/src/hooks/useTextModel.d.ts +2 -31
  63. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  64. package/dist/types/src/index.d.ts +3 -0
  65. package/dist/types/src/index.d.ts.map +1 -1
  66. package/dist/types/src/styles/cursors.d.ts.map +1 -0
  67. package/dist/types/src/styles/index.d.ts +1 -0
  68. package/dist/types/src/styles/index.d.ts.map +1 -1
  69. package/dist/types/src/styles/markdown.d.ts +2 -2
  70. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  71. package/dist/types/src/styles/tokens.d.ts +1 -0
  72. package/dist/types/src/styles/tokens.d.ts.map +1 -1
  73. package/dist/types/src/themes/default.d.ts.map +1 -1
  74. package/dist/types/src/util.d.ts +7 -0
  75. package/dist/types/src/util.d.ts.map +1 -0
  76. package/package.json +34 -35
  77. package/src/components/TextEditor/MarkdownEditor.stories.tsx +82 -83
  78. package/src/components/TextEditor/TextEditor.stories.tsx +13 -34
  79. package/src/components/TextEditor/TextEditor.tsx +122 -93
  80. package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
  81. package/src/components/TextEditor/comments.stories.tsx +357 -0
  82. package/src/extensions/autocomplete.ts +23 -19
  83. package/src/extensions/automerge/automerge.ts +88 -0
  84. package/src/extensions/automerge/cursor.ts +52 -0
  85. package/src/extensions/automerge/defs.ts +41 -0
  86. package/src/extensions/automerge/index.ts +6 -0
  87. package/src/{hooks → extensions}/automerge/semaphore.ts +34 -29
  88. package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
  89. package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
  90. package/src/extensions/awareness.ts +310 -0
  91. package/src/extensions/basic.ts +22 -17
  92. package/src/extensions/comments.ts +252 -229
  93. package/src/extensions/cursor.ts +50 -0
  94. package/src/extensions/index.ts +4 -7
  95. package/src/extensions/markdown/bundle.ts +20 -30
  96. package/src/extensions/markdown/code.ts +127 -0
  97. package/src/extensions/markdown/heading.ts +59 -0
  98. package/src/extensions/markdown/highlight.ts +11 -26
  99. package/src/extensions/markdown/hr.ts +71 -0
  100. package/src/extensions/markdown/image.ts +87 -0
  101. package/src/extensions/markdown/index.ts +7 -0
  102. package/src/extensions/{link.ts → markdown/link.ts} +85 -88
  103. package/src/extensions/{table.ts → markdown/table.ts} +10 -4
  104. package/src/extensions/markdown/tasklist.ts +110 -0
  105. package/src/extensions/mention.ts +9 -2
  106. package/src/extensions/outliner.ts +12 -0
  107. package/src/hooks/awareness-provider.ts +124 -0
  108. package/src/hooks/defs.ts +42 -0
  109. package/src/hooks/index.ts +4 -1
  110. package/src/hooks/useTextEditor.ts +29 -0
  111. package/src/hooks/useTextModel.ts +30 -116
  112. package/src/index.ts +3 -0
  113. package/src/styles/index.ts +1 -0
  114. package/src/styles/markdown.ts +9 -12
  115. package/src/styles/tokens.ts +3 -0
  116. package/src/themes/default.ts +49 -78
  117. package/src/util.ts +41 -0
  118. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  119. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  120. package/dist/types/src/extensions/code.d.ts.map +0 -1
  121. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  122. package/dist/types/src/extensions/image.d.ts.map +0 -1
  123. package/dist/types/src/extensions/link.d.ts.map +0 -1
  124. package/dist/types/src/extensions/mermaid.d.ts +0 -3
  125. package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
  126. package/dist/types/src/extensions/table.d.ts.map +0 -1
  127. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  128. package/dist/types/src/extensions/util.d.ts +0 -5
  129. package/dist/types/src/extensions/util.d.ts.map +0 -1
  130. package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
  131. package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
  132. package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
  133. package/dist/types/src/hooks/automerge/index.d.ts +0 -3
  134. package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
  135. package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
  136. package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
  137. package/dist/types/src/hooks/automerge/semaphore.d.ts +0 -19
  138. package/dist/types/src/hooks/automerge/semaphore.d.ts.map +0 -1
  139. package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
  140. package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
  141. package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +0 -1
  142. package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
  143. package/dist/types/src/hooks/yjs/index.d.ts +0 -3
  144. package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
  145. package/dist/types/src/hooks/yjs/space-provider.d.ts +0 -27
  146. package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
  147. package/dist/types/src/hooks/yjs/yjs.stories.d.ts +0 -12
  148. package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
  149. package/dist/types/src/hooks/yjs/yjs.test.d.ts +0 -2
  150. package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
  151. package/dist/types/src/testing/index.d.ts +0 -3
  152. package/dist/types/src/testing/index.d.ts.map +0 -1
  153. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  154. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  155. package/dist/types/src/testing/proto/index.d.ts +0 -2
  156. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  157. package/dist/types/src/testing/replicator.d.ts +0 -45
  158. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  159. package/src/components/TextEditor/codemirror.stories.ts +0 -114
  160. package/src/extensions/code.ts +0 -99
  161. package/src/extensions/hr.ts +0 -74
  162. package/src/extensions/image.ts +0 -69
  163. package/src/extensions/mermaid.ts +0 -11
  164. package/src/extensions/tasklist.ts +0 -121
  165. package/src/extensions/util.ts +0 -18
  166. package/src/hooks/automerge/handle.ts +0 -14
  167. package/src/hooks/automerge/index.ts +0 -6
  168. package/src/hooks/automerge/plugin.ts +0 -132
  169. package/src/hooks/yjs/index.ts +0 -6
  170. package/src/hooks/yjs/space-provider.ts +0 -126
  171. package/src/hooks/yjs/yjs.stories.tsx +0 -56
  172. package/src/hooks/yjs/yjs.test.ts +0 -59
  173. package/src/testing/index.ts +0 -6
  174. package/src/testing/proto/gen/schema.ts +0 -49
  175. package/src/testing/proto/index.ts +0 -5
  176. package/src/testing/proto/schema.proto +0 -15
  177. package/src/testing/replicator.ts +0 -189
  178. /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
  179. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  180. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  181. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  182. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  183. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  184. /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
  185. /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
  186. /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
@@ -1,50 +1,53 @@
1
1
  //
2
2
  // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Automerge
4
+ // Ref: https://github.com/automerge/automerge-codemirror
3
5
  //
4
6
 
5
- // TODO(burdon): Additional copyright?
6
-
7
+ import { type StateField } from '@codemirror/state';
7
8
  import { type EditorView } from '@codemirror/view';
8
9
 
9
10
  import { next as automerge } from '@dxos/automerge/automerge';
10
11
 
11
- import { type IDocHandle } from './handle';
12
- import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
12
+ import {
13
+ getLastHeads,
14
+ getPath,
15
+ isReconcileTx,
16
+ reconcileAnnotationType,
17
+ updateHeads,
18
+ type IDocHandle,
19
+ type State,
20
+ } from './defs';
13
21
  import { updateAutomerge } from './update-automerge';
14
22
  import { updateCodeMirror } from './update-codemirror';
15
23
 
16
- type Doc<T> = automerge.Doc<T>;
17
- type Heads = automerge.Heads;
18
-
19
- type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
20
-
21
24
  /**
22
- * TODO(burdon): Comment.
25
+ * Implements three-way merge (on each mutation/keystroke).
23
26
  */
24
27
  export class PatchSemaphore {
25
- _field!: Field;
26
- _inReconcile = false;
27
- _queue: Array<ChangeFn> = [];
28
+ private _inReconcile = false;
28
29
 
29
- constructor(field?: Field) {
30
- if (field !== undefined) {
31
- this._field = field;
32
- }
33
- }
30
+ // prettier-ignore
31
+ constructor(
32
+ private readonly _handle: IDocHandle,
33
+ private readonly _state: StateField<State>
34
+ ) {}
34
35
 
35
- reconcile = (handle: IDocHandle, view: EditorView) => {
36
+ // NOTE: Cannot destruct view.state.
37
+ reconcile(view: EditorView) {
36
38
  if (this._inReconcile) {
37
39
  return;
38
40
  }
39
41
  this._inReconcile = true;
40
42
 
41
- const path = getPath(view.state, this._field);
42
- const oldHeads = getLastHeads(view.state, this._field);
43
- let selection = view.state.selection;
43
+ const path = getPath(view.state, this._state);
44
44
 
45
- const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
45
+ // Get the heads before the unreconciled transactions are applied.
46
+ const oldHeads = getLastHeads(view.state, this._state);
47
+ let selection = view.state.selection;
46
48
 
47
49
  // First undo all the unreconciled transactions.
50
+ const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
48
51
  const toInvert = transactions.slice().reverse();
49
52
  for (const tx of toInvert) {
50
53
  const inverted = tx.changes.invert(tx.startState.doc);
@@ -56,23 +59,25 @@ export class PatchSemaphore {
56
59
  }
57
60
 
58
61
  // Apply the unreconciled transactions to the document.
59
- let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
60
-
61
62
  // NOTE: null and undefined each come from automerge and repo respectively.
63
+ let newHeads = updateAutomerge(this._state, this._handle, transactions, view.state);
62
64
  if (newHeads === null || newHeads === undefined) {
63
- // TODO: @alexjg this is the call that's resetting the editor state on click
64
- newHeads = automerge.getHeads(handle.docSync()!);
65
+ // TODO(alexjg): this is the call that's resetting the editor state on click.
66
+ newHeads = automerge.getHeads(this._handle.docSync()!);
65
67
  }
66
68
 
67
69
  // Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
68
- const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
70
+ const diff = automerge.equals(oldHeads, newHeads)
71
+ ? []
72
+ : automerge.diff(this._handle.docSync()!, oldHeads, newHeads);
69
73
  updateCodeMirror(view, selection, path, diff);
70
74
 
75
+ // Update automerge state.
71
76
  view.dispatch({
72
77
  effects: updateHeads(newHeads),
73
78
  annotations: reconcileAnnotationType.of({}),
74
79
  });
75
80
 
76
81
  this._inReconcile = false;
77
- };
82
+ }
78
83
  }
@@ -1,18 +1,17 @@
1
1
  //
2
2
  // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Automerge
4
+ // Ref: https://github.com/automerge/automerge-codemirror
3
5
  //
4
6
 
5
- // TODO(burdon): Additional copyright?
6
-
7
- import { type EditorState, type Text, type Transaction } from '@codemirror/state';
7
+ import { type EditorState, type StateField, type Text, type Transaction } from '@codemirror/state';
8
8
 
9
9
  import { next as am, type Heads } from '@dxos/automerge/automerge';
10
10
 
11
- import { type IDocHandle } from './handle';
12
- import { type Field } from './plugin';
11
+ import { type IDocHandle, type State } from './defs';
13
12
 
14
13
  export const updateAutomerge = (
15
- field: Field,
14
+ field: StateField<State>,
16
15
  handle: IDocHandle,
17
16
  transactions: Transaction[],
18
17
  state: EditorState,
@@ -1,9 +1,9 @@
1
1
  //
2
2
  // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Automerge
4
+ // Ref: https://github.com/automerge/automerge-codemirror
3
5
  //
4
6
 
5
- // TODO(burdon): Additional copyright?
6
-
7
7
  import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
8
8
  import { type EditorView } from '@codemirror/view';
9
9
 
@@ -16,7 +16,7 @@ import {
16
16
  type SpliceTextPatch,
17
17
  } from '@dxos/automerge/automerge';
18
18
 
19
- import { reconcileAnnotationType } from './plugin';
19
+ import { reconcileAnnotationType } from './defs';
20
20
 
21
21
  export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
22
22
  for (const patch of patches) {
@@ -56,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
56
56
  if (index == null) {
57
57
  return [];
58
58
  }
59
+
59
60
  const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
60
61
  return [{ from: index, to: index, insert: text }];
61
62
  };
@@ -65,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
65
66
  if (index == null) {
66
67
  return [];
67
68
  }
69
+
68
70
  return [{ from: index, insert: patch.value }];
69
71
  };
70
72
 
@@ -73,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
73
75
  if (index == null) {
74
76
  return [];
75
77
  }
78
+
76
79
  const length = patch.length || 1;
77
80
  return [{ from: index, to: index + length }];
78
81
  };
@@ -82,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
82
85
  if (index == null) {
83
86
  return [];
84
87
  }
88
+
85
89
  const length = state.doc.length;
86
90
  if (typeof patch.value !== 'string') {
87
91
  return []; // TODO(dmaretskyi): How to handle non string values?
88
92
  }
93
+
89
94
  return [{ from: 0, to: length, insert: patch.value as any }];
90
95
  };
91
96
 
92
- // If the path of the patch is of the form [path, <index>] then we know this is
93
- // a path to a character within the sequence given by path
97
+ // If the path of the patch is of the form [path, <index>] then we know this is a path to a character
98
+ // within the sequence given by path.
94
99
  const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
95
100
  if (candidatePath.length !== textPath.length + 1) {
96
101
  return null;
97
102
  }
103
+
98
104
  for (let i = 0; i < textPath.length; i++) {
99
105
  if (textPath[i] !== candidatePath[i]) {
100
106
  return null;
101
107
  }
102
108
  }
109
+
103
110
  const index = candidatePath[candidatePath.length - 1];
104
111
  if (typeof index === 'number') {
105
112
  return index;
106
113
  }
114
+
107
115
  return null;
108
116
  };
@@ -0,0 +1,310 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Annotation, Facet, type Extension, RangeSet, type Range } 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 } from './cursor';
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 = Facet.define<AwarenessProvider, AwarenessProvider>({
42
+ combine: (providers) => providers[0] ?? dummyProvider,
43
+ });
44
+
45
+ // TODO(dmaretskyi): Specify the users that actually changed. Currently, we recalculate positions for every user.
46
+ const RemoteSelectionChangedAnnotation = Annotation.define();
47
+
48
+ export type AwarenessPosition = {
49
+ anchor?: string;
50
+ head?: string;
51
+ };
52
+
53
+ export type AwarenessInfo = {
54
+ displayName?: string;
55
+ // TODO(burdon): Rename light/dark.
56
+ color?: string;
57
+ lightColor?: string;
58
+ };
59
+
60
+ export type AwarenessState = {
61
+ position?: AwarenessPosition;
62
+ peerId: string;
63
+ info: AwarenessInfo;
64
+ };
65
+
66
+ /**
67
+ * Extension provides presence information about other peers.
68
+ */
69
+ export const awareness = (provider = dummyProvider): Extension => {
70
+ return [
71
+ AwarenessProvider.of(provider),
72
+ ViewPlugin.fromClass(RemoteSelectionsDecorator, {
73
+ decorations: (value) => value.decorations,
74
+ }),
75
+ styles,
76
+ ];
77
+ };
78
+
79
+ /**
80
+ * Generates selection decorations from remote peers.
81
+ */
82
+ export class RemoteSelectionsDecorator implements PluginValue {
83
+ public decorations: DecorationSet = RangeSet.of([]);
84
+
85
+ private readonly _ctx = new Context();
86
+
87
+ private _cursorConverter: CursorConverter;
88
+ private _provider: AwarenessProvider;
89
+ private _lastAnchor?: number = undefined;
90
+ private _lastHead?: number = undefined;
91
+
92
+ constructor(view: EditorView) {
93
+ this._cursorConverter = view.state.facet(Cursor.converter);
94
+ this._provider = view.state.facet(AwarenessProvider);
95
+ this._provider.open();
96
+ this._provider.remoteStateChange.on(this._ctx, () => {
97
+ view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
98
+ });
99
+ }
100
+
101
+ destroy() {
102
+ void this._ctx.dispose();
103
+ this._provider.close();
104
+ }
105
+
106
+ update(update: ViewUpdate) {
107
+ this._updateLocalSelection(update);
108
+ this._updateRemoteSelections(update);
109
+ }
110
+
111
+ private _updateLocalSelection(update: ViewUpdate) {
112
+ const hasFocus = update.view.hasFocus && update.view.dom.ownerDocument.hasFocus();
113
+ const { anchor = undefined, head = undefined } = hasFocus ? update.state.selection.main : {};
114
+ if (this._lastAnchor === anchor && this._lastHead === head) {
115
+ return;
116
+ }
117
+
118
+ this._lastAnchor = anchor;
119
+ this._lastHead = head;
120
+
121
+ this._provider.update(
122
+ anchor !== undefined && head !== undefined
123
+ ? {
124
+ anchor: this._cursorConverter.toCursor(anchor),
125
+ head: this._cursorConverter.toCursor(head),
126
+ }
127
+ : undefined,
128
+ );
129
+ }
130
+
131
+ private _updateRemoteSelections(update: ViewUpdate) {
132
+ const decorations: Range<Decoration>[] = [];
133
+ const awarenessStates = this._provider.getRemoteStates();
134
+ for (const state of awarenessStates) {
135
+ const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
136
+ const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
137
+ if (anchor == null || head == null) {
138
+ continue;
139
+ }
140
+
141
+ const start = Math.min(Math.min(anchor, head), update.view.state.doc.length);
142
+ const end = Math.min(Math.max(anchor, head), update.view.state.doc.length);
143
+
144
+ const startLine = update.view.state.doc.lineAt(start);
145
+ const endLine = update.view.state.doc.lineAt(end);
146
+
147
+ // TODO(burdon): Factor out styles.
148
+ const color = state.info.color ?? '#30bced';
149
+ const lightColor = state.info.lightColor ?? color + '33';
150
+
151
+ if (startLine.number === endLine.number) {
152
+ // Selected content in a single line.
153
+ decorations.push({
154
+ from: start,
155
+ to: end,
156
+ value: Decoration.mark({
157
+ attributes: { style: `background-color: ${lightColor}` },
158
+ class: 'cm-collab-selection',
159
+ }),
160
+ });
161
+ } else {
162
+ // Selected content in multiple lines; first, render text-selection in the first line.
163
+ decorations.push({
164
+ from: start,
165
+ to: startLine.from + startLine.length,
166
+ value: Decoration.mark({
167
+ attributes: { style: `background-color: ${color}` },
168
+ class: 'cm-collab-selection',
169
+ }),
170
+ });
171
+
172
+ // Render text-selection in the last line.
173
+ decorations.push({
174
+ from: endLine.from,
175
+ to: end,
176
+ value: Decoration.mark({
177
+ attributes: { style: `background-color: ${color}` },
178
+ class: 'cm-collab-selection',
179
+ }),
180
+ });
181
+
182
+ for (let i = startLine.number + 1; i < endLine.number; i++) {
183
+ const linePos = update.view.state.doc.line(i).from;
184
+ decorations.push({
185
+ from: linePos,
186
+ to: linePos,
187
+ value: Decoration.line({
188
+ attributes: { style: `background-color: ${color}`, class: 'cm-collab-selectionLine' },
189
+ }),
190
+ });
191
+ }
192
+ }
193
+
194
+ decorations.push({
195
+ from: head,
196
+ to: head,
197
+ value: Decoration.widget({
198
+ side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
199
+ block: false,
200
+ widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', color),
201
+ }),
202
+ });
203
+ }
204
+
205
+ this.decorations = Decoration.set(decorations, true);
206
+ }
207
+ }
208
+
209
+ class RemoteCaretWidget extends WidgetType {
210
+ constructor(private readonly _name: string, private readonly _color: string) {
211
+ super();
212
+ }
213
+
214
+ override toDOM(): HTMLElement {
215
+ const span = document.createElement('span');
216
+ span.className = 'cm-collab-selectionCaret';
217
+ span.style.backgroundColor = this._color;
218
+ span.style.borderColor = this._color;
219
+
220
+ const dot = document.createElement('div');
221
+ dot.className = 'cm-collab-selectionCaretDot';
222
+
223
+ const info = document.createElement('div');
224
+ info.className = 'cm-collab-selectionInfo';
225
+ info.innerText = this._name;
226
+
227
+ span.appendChild(document.createTextNode('\u2060'));
228
+ span.appendChild(dot);
229
+ span.appendChild(document.createTextNode('\u2060'));
230
+ span.appendChild(info);
231
+ span.appendChild(document.createTextNode('\u2060'));
232
+
233
+ return span;
234
+ }
235
+
236
+ override updateDOM() {
237
+ return false;
238
+ }
239
+
240
+ override eq(widget: this) {
241
+ return widget._color === this._color;
242
+ }
243
+
244
+ override get estimatedHeight() {
245
+ return -1;
246
+ }
247
+
248
+ override ignoreEvent() {
249
+ return true;
250
+ }
251
+ }
252
+
253
+ const styles = EditorView.baseTheme({
254
+ '.cm-collab-selection': {},
255
+ '.cm-collab-selectionLine': {
256
+ padding: 0,
257
+ margin: '0px 2px 0px 4px',
258
+ },
259
+ '.cm-collab-selectionCaret': {
260
+ position: 'relative',
261
+ borderLeft: '1px solid black',
262
+ borderRight: '1px solid black',
263
+ marginLeft: '-1px',
264
+ marginRight: '-1px',
265
+ boxSizing: 'border-box',
266
+ display: 'inline',
267
+ cursor: 'pointer',
268
+ },
269
+ '.cm-collab-selectionCaretDot': {
270
+ borderRadius: '50%',
271
+ position: 'absolute',
272
+ width: '.5em',
273
+ height: '.5em',
274
+ top: '-.25em',
275
+ left: '-.25em',
276
+ backgroundColor: 'inherit',
277
+ transition: 'transform .3s ease-in-out',
278
+ boxSizing: 'border-box',
279
+ },
280
+ '.cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot': {
281
+ transform: 'scale(0)',
282
+ transformOrigin: 'center',
283
+ },
284
+ '.cm-collab-selectionInfo': {
285
+ position: 'absolute',
286
+ transform: 'translate(-50%, 0)',
287
+ top: '-20px',
288
+ left: 0,
289
+ fontSize: '.75em',
290
+ fontFamily: 'sans-serif',
291
+ fontStyle: 'normal',
292
+ fontWeight: 'normal',
293
+ lineHeight: 'normal',
294
+ userSelect: 'none',
295
+ color: 'white',
296
+ padding: '2px',
297
+ zIndex: 101,
298
+ transition: 'opacity .3s ease-in-out',
299
+ backgroundColor: 'inherit',
300
+ borderRadius: '2px',
301
+ // These should be separate.
302
+ opacity: 0,
303
+ transitionDelay: '0s',
304
+ whiteSpace: 'nowrap',
305
+ },
306
+ '.cm-collab-selectionCaret:hover > .cm-collab-selectionInfo': {
307
+ opacity: 1,
308
+ transitionDelay: '0s',
309
+ },
310
+ });
@@ -3,30 +3,35 @@
3
3
  //
4
4
 
5
5
  import { closeBrackets } from '@codemirror/autocomplete';
6
+ import { history } from '@codemirror/commands';
6
7
  import { bracketMatching, defaultHighlightStyle, syntaxHighlighting } from '@codemirror/language';
7
8
  import { type Extension } from '@codemirror/state';
8
9
  import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
9
- import { drawSelection, EditorView, placeholder } from '@codemirror/view';
10
+ import { drawSelection, EditorView, highlightActiveLine, placeholder } from '@codemirror/view';
10
11
 
11
- import type { ThemeMode } from '@dxos/react-ui';
12
+ import { type ThemeMode } from '@dxos/react-ui';
13
+
14
+ import { type TextEditorProps } from '../components';
12
15
 
13
16
  export type BasicBundleOptions = {
14
- readonly?: boolean;
15
17
  themeMode?: ThemeMode;
16
- placeholder?: string;
17
- };
18
+ } & Pick<TextEditorProps, 'placeholder' | 'lineWrapping'>;
18
19
 
19
- export const basicBundle = ({ readonly, themeMode, placeholder: _placeholder }: BasicBundleOptions): Extension[] =>
20
- [
21
- EditorView.lineWrapping,
20
+ export const basicBundle = ({
21
+ themeMode,
22
+ placeholder: _placeholder,
23
+ lineWrapping = true,
24
+ }: BasicBundleOptions): Extension[] => [
25
+ lineWrapping ? EditorView.lineWrapping : [],
22
26
 
23
- // TODO(burdon): Compare with minimalSetup.
24
- // https://codemirror.net/docs/ref/#codemirror.minimalSetup
25
- bracketMatching(),
26
- closeBrackets(),
27
- drawSelection(),
28
- _placeholder && placeholder(_placeholder),
27
+ // https://codemirror.net/docs/ref/#codemirror.minimalSetup
28
+ bracketMatching(),
29
+ closeBrackets(),
30
+ drawSelection(),
31
+ highlightActiveLine(),
32
+ history(),
33
+ _placeholder ? placeholder(_placeholder) : [],
29
34
 
30
- // TODO(burdon): Is this required?
31
- themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
32
- ].filter(Boolean) as Extension[];
35
+ // https://github.com/codemirror/theme-one-dark
36
+ themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
37
+ ];