@dxos/react-ui-editor 0.3.10-main.5515dfe → 0.3.10-main.560f82b

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 (108) hide show
  1. package/dist/lib/browser/index.mjs +1049 -285
  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 +22 -1
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +3 -1
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts +3 -2
  9. package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/extensions/basic.d.ts +2 -1
  11. package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +1 -1
  12. package/dist/types/src/components/TextEditor/extensions/blast.d.ts +25 -0
  13. package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +1 -0
  14. package/dist/types/src/components/TextEditor/extensions/comments.d.ts +18 -0
  15. package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -0
  16. package/dist/types/src/components/TextEditor/extensions/demo.d.ts +11 -0
  17. package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -0
  18. package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +1 -1
  19. package/dist/types/src/components/TextEditor/extensions/image.d.ts +4 -0
  20. package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +1 -0
  21. package/dist/types/src/components/TextEditor/extensions/index.d.ts +6 -0
  22. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
  23. package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +1 -1
  24. package/dist/types/src/components/TextEditor/extensions/listener.d.ts +4 -2
  25. package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +1 -1
  26. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +2 -1
  27. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -1
  28. package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts +6 -2
  29. package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +1 -1
  30. package/dist/types/src/components/TextEditor/extensions/mention.d.ts +6 -0
  31. package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +1 -0
  32. package/dist/types/src/components/TextEditor/extensions/table.d.ts +8 -0
  33. package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +1 -0
  34. package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts +2 -4
  35. package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +1 -1
  36. package/dist/types/src/components/TextEditor/themes/default.d.ts +8 -0
  37. package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -1
  38. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.d.ts +3 -0
  39. package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +1 -0
  40. package/dist/types/src/hooks/automerge/automerge.stories.d.ts +16 -0
  41. package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -0
  42. package/dist/types/src/hooks/automerge/handle.d.ts.map +1 -0
  43. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/index.d.ts +1 -1
  44. package/dist/types/src/hooks/automerge/index.d.ts.map +1 -0
  45. package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -0
  46. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +5 -0
  47. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +1 -0
  48. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +6 -0
  49. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +1 -0
  50. package/dist/types/src/hooks/automerge/translation/index.d.ts +3 -0
  51. package/dist/types/src/hooks/automerge/translation/index.d.ts.map +1 -0
  52. package/dist/types/src/hooks/index.d.ts +0 -1
  53. package/dist/types/src/hooks/index.d.ts.map +1 -1
  54. package/dist/types/src/hooks/useTextModel.d.ts +3 -4
  55. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  56. package/dist/types/src/{components/TextEditor → hooks/yjs}/yjs.stories.d.ts +1 -1
  57. package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +1 -0
  58. package/dist/types/src/styles/markdown.d.ts +3 -2
  59. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  60. package/dist/types/src/testing/replicator.d.ts.map +1 -1
  61. package/package.json +20 -19
  62. package/src/components/TextEditor/MarkdownEditor.stories.tsx +137 -31
  63. package/src/components/TextEditor/TextEditor.tsx +52 -25
  64. package/src/components/TextEditor/extensions/autocomplete.ts +9 -22
  65. package/src/components/TextEditor/extensions/basic.ts +2 -1
  66. package/src/components/TextEditor/extensions/blast.ts +375 -0
  67. package/src/components/TextEditor/extensions/comments.ts +190 -0
  68. package/src/components/TextEditor/extensions/demo.ts +69 -0
  69. package/src/components/TextEditor/extensions/experimental.tsx +1 -0
  70. package/src/components/TextEditor/extensions/image.ts +68 -0
  71. package/src/components/TextEditor/extensions/index.ts +6 -0
  72. package/src/components/TextEditor/extensions/link.ts +25 -25
  73. package/src/components/TextEditor/extensions/listener.ts +4 -3
  74. package/src/components/TextEditor/extensions/markdown/bundle.ts +18 -18
  75. package/src/components/TextEditor/extensions/markdown/highlight.ts +118 -103
  76. package/src/components/TextEditor/extensions/mention.ts +31 -0
  77. package/src/components/TextEditor/extensions/table.ts +123 -0
  78. package/src/components/TextEditor/extensions/tasklist.ts +89 -81
  79. package/src/components/TextEditor/themes/default.ts +69 -2
  80. package/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.ts +12 -9
  81. package/src/{automerge → hooks/automerge}/automerge.stories.tsx +5 -3
  82. package/src/{automerge/automerge-plugin → hooks/automerge}/plugin.ts +1 -0
  83. package/src/{automerge/automerge-plugin/amToCodemirror.ts → hooks/automerge/translation/automerge-to-codemirror.ts} +10 -2
  84. package/src/{automerge/automerge-plugin/codeMirrorToAm.ts → hooks/automerge/translation/codemirror-to-automerge.ts} +7 -4
  85. package/src/hooks/automerge/translation/index.ts +6 -0
  86. package/src/hooks/index.ts +0 -1
  87. package/src/hooks/useTextModel.ts +49 -41
  88. package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +1 -1
  89. package/src/styles/markdown.ts +14 -8
  90. package/src/testing/replicator.ts +6 -7
  91. package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +0 -1
  92. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts +0 -6
  93. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts.map +0 -1
  94. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts +0 -7
  95. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +0 -1
  96. package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +0 -1
  97. package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +0 -1
  98. package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +0 -1
  99. package/dist/types/src/automerge/automerge.stories.d.ts +0 -9
  100. package/dist/types/src/automerge/automerge.stories.d.ts.map +0 -1
  101. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  102. package/dist/types/src/hooks/useCollaboration.d.ts +0 -9
  103. package/dist/types/src/hooks/useCollaboration.d.ts.map +0 -1
  104. package/src/hooks/useCollaboration.ts +0 -45
  105. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/handle.d.ts +0 -0
  106. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/plugin.d.ts +0 -0
  107. package/src/{automerge/automerge-plugin → hooks/automerge}/handle.ts +0 -0
  108. package/src/{automerge/automerge-plugin → hooks/automerge}/index.ts +1 -1
@@ -0,0 +1,123 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { type EditorState, type RangeSet, RangeSetBuilder, StateField, type Transaction } from '@codemirror/state';
7
+ import { Decoration, EditorView, WidgetType } from '@codemirror/view';
8
+
9
+ // TODO(burdon): Snippet to create basic table.
10
+ // https://codemirror.net/docs/ref/#autocomplete.snippet
11
+
12
+ export type TableOptions = {};
13
+
14
+ /**
15
+ * GFM tables.
16
+ * https://github.github.com/gfm/#tables-extension
17
+ */
18
+ export const table = (options: TableOptions = {}) => {
19
+ return StateField.define<RangeSet<any>>({
20
+ create: (state) => update(state, options),
21
+ update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
22
+ provide: (field) => EditorView.decorations.from(field),
23
+ });
24
+ };
25
+
26
+ type Table = {
27
+ from: number;
28
+ to?: number;
29
+ header?: string[];
30
+ rows?: string[][];
31
+ };
32
+
33
+ const update = (state: EditorState, options: TableOptions) => {
34
+ const builder = new RangeSetBuilder();
35
+ if (state.readOnly) {
36
+ const tables: Table[] = [];
37
+ const getTable = () => tables[tables.length - 1];
38
+ const getRow = () => {
39
+ const table = getTable();
40
+ return table.rows?.[table.rows.length - 1];
41
+ };
42
+
43
+ // Parse table.
44
+ // TODO(burdon): Use remark?: https://www.npmjs.com/package/remark-gfm
45
+ syntaxTree(state).iterate({
46
+ enter: (node) => {
47
+ // console.log(node.name);
48
+ // Check if cursor is inside text.
49
+ switch (node.name) {
50
+ case 'Table': {
51
+ tables.push({ from: node.from });
52
+ break;
53
+ }
54
+ case 'TableHeader': {
55
+ getTable().header = [];
56
+ break;
57
+ }
58
+ case 'TableRow': {
59
+ (getTable().rows ??= []).push([]);
60
+ break;
61
+ }
62
+ case 'TableCell': {
63
+ const row = getRow();
64
+ if (row) {
65
+ row.push(state.sliceDoc(node.from, node.to));
66
+ } else {
67
+ getTable().header?.push(state.sliceDoc(node.from, node.to));
68
+ }
69
+ break;
70
+ }
71
+ case 'TableDelimiter': {
72
+ getTable().to = node.to;
73
+ break;
74
+ }
75
+ }
76
+ },
77
+ });
78
+
79
+ tables.forEach((table) =>
80
+ builder.add(table.from, table.to!, Decoration.replace({ widget: new TableWidget(table) })),
81
+ );
82
+ }
83
+
84
+ return builder.finish();
85
+ };
86
+
87
+ class TableWidget extends WidgetType {
88
+ constructor(readonly _table: Table) {
89
+ super();
90
+ }
91
+
92
+ override eq(other: WidgetType) {
93
+ return this._table.from === (other as any as TableWidget)?._table?.from;
94
+ }
95
+
96
+ toDOM(view: EditorView) {
97
+ const table = document.createElement('table');
98
+
99
+ {
100
+ const header = table.appendChild(document.createElement('thead'));
101
+ const row = header.appendChild(document.createElement('tr'));
102
+ this._table.header?.forEach((cell) => {
103
+ const th = document.createElement('th');
104
+ th.setAttribute('class', 'cm-table-head');
105
+ row.appendChild(th).textContent = cell;
106
+ });
107
+ }
108
+
109
+ {
110
+ const body = table.appendChild(document.createElement('tbody'));
111
+ this._table.rows?.forEach((row) => {
112
+ const tr = body.appendChild(document.createElement('tr'));
113
+ row.forEach((cell) => {
114
+ const td = document.createElement('td');
115
+ td.setAttribute('class', 'cm-table-cell');
116
+ tr.appendChild(td).textContent = cell;
117
+ });
118
+ });
119
+ }
120
+
121
+ return table;
122
+ }
123
+ }
@@ -2,112 +2,120 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { syntaxTree } from '@codemirror/language';
6
5
  import {
7
- type EditorState,
8
- type Extension,
9
- type RangeSet,
10
- RangeSetBuilder,
11
- StateField,
12
- type Transaction,
13
- } from '@codemirror/state';
14
- import { EditorView, Decoration, WidgetType } from '@codemirror/view';
6
+ EditorView,
7
+ Decoration,
8
+ WidgetType,
9
+ MatchDecorator,
10
+ ViewPlugin,
11
+ type DecorationSet,
12
+ type ViewUpdate,
13
+ } from '@codemirror/view';
15
14
 
16
15
  class CheckboxWidget extends WidgetType {
17
- constructor(private readonly _pos: number, private readonly _getCursor: () => number, private _checked: boolean) {
16
+ constructor(
17
+ private readonly _pos: number,
18
+ private _checked: boolean,
19
+ private readonly _indent: number,
20
+ private readonly _onCheck?: (check: boolean) => void,
21
+ ) {
18
22
  super();
19
23
  }
20
24
 
21
25
  override eq(other: CheckboxWidget) {
22
- // Uniqueness based on position; also checks state.
23
- // TODO(burdon): Is this efficient? Check life-cycle.
24
- return this._pos === other._pos && this._checked === other._checked;
26
+ return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
25
27
  }
26
28
 
27
29
  toDOM(view: EditorView) {
28
- // console.log('toDom', this._pos);
29
30
  const wrap = document.createElement('span');
30
- wrap.setAttribute('aria-hidden', 'true');
31
31
  wrap.className = 'cm-task-item';
32
+ wrap.setAttribute('aria-hidden', 'true');
33
+ wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
32
34
 
33
- const box = wrap.appendChild(document.createElement('input'));
34
- box.type = 'checkbox';
35
- box.checked = this._checked;
36
- box.onclick = (event) => {
37
- this._checked = !isChecked(view.state, this._pos);
38
- view.dispatch({
39
- changes: {
40
- from: this._pos + 1,
41
- to: this._pos + 2,
42
- insert: this._checked ? 'x' : ' ',
43
- },
44
- // TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
45
- selection: {
46
- anchor: this._pos + 4,
47
- // anchor: this._getCursor(),
48
- },
49
- });
50
- return true;
51
- };
35
+ const input = wrap.appendChild(document.createElement('input'));
36
+ input.type = 'checkbox';
37
+ input.checked = this._checked;
38
+ if (!this._onCheck) {
39
+ input.setAttribute('disabled', 'true');
40
+ }
41
+ if (this._onCheck) {
42
+ input.onchange = (event: Event) => {
43
+ this._onCheck?.((event.target as any).checked);
44
+ return true;
45
+ };
46
+ }
52
47
 
53
48
  return wrap;
54
49
  }
55
50
 
56
- // TODO(burdon): Remove listener?
57
- // override destroy() {
58
- // console.log('destroy', this._pos);
59
- // }
60
-
61
51
  override ignoreEvent() {
62
52
  return false;
63
53
  }
64
54
  }
65
55
 
66
- const isChecked = (state: EditorState, pos: number) => {
67
- return state.sliceDoc(pos, pos + 3).toLowerCase() === '[x]';
68
- };
56
+ // TODO(burdon): Reconcile with theme.
57
+ const styles = EditorView.baseTheme({
58
+ '& .cm-task-item': {
59
+ paddingLeft: '4px',
60
+ paddingRight: '8px',
61
+ },
62
+ });
69
63
 
70
- export const statefield = (): Extension => {
71
- let lastPosition: number = 0;
72
- const listener = EditorView.updateListener.of((update) => {
73
- lastPosition = update.startState.selection.main.head;
64
+ export type TasklistOptions = {};
65
+
66
+ export const tasklist = (options: TasklistOptions = {}) => {
67
+ // TODO(burdon): Matcher isn't as precise as syntax tree.
68
+ // Allows for indents to be greater than AST would allow.
69
+ const taskMatcher = new MatchDecorator({
70
+ regexp: /^(\s*)- \[([ xX])\]\s/g,
71
+ decoration: (match, view, pos) => {
72
+ const indent = Math.floor(match[1].length / 2);
73
+ const checked = match[2] === 'x' || match[2] === 'X';
74
+ return Decoration.replace({
75
+ widget: new CheckboxWidget(
76
+ pos,
77
+ checked,
78
+ indent,
79
+ view.state.readOnly
80
+ ? undefined
81
+ : (checked) => {
82
+ const idx = pos + match[0].indexOf('[') + 1;
83
+ view.dispatch({
84
+ changes: {
85
+ from: idx,
86
+ to: idx + 1,
87
+ insert: checked ? 'x' : ' ',
88
+ },
89
+ // TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
90
+ selection: {
91
+ anchor: idx + 3,
92
+ },
93
+ });
94
+ },
95
+ ),
96
+ });
97
+ },
74
98
  });
75
99
 
76
- const checkbox = (state: EditorState) => {
77
- const builder = new RangeSetBuilder();
78
- syntaxTree(state).iterate({
79
- enter: (node) => {
80
- // console.log(node.name, node.from, node.to);
81
- if (node.name === 'TaskMarker') {
82
- builder.add(
83
- node.from,
84
- node.to,
85
- Decoration.replace({
86
- widget: new CheckboxWidget(node.from, () => lastPosition, isChecked(state, node.from)),
87
- }),
88
- );
89
- }
90
- },
91
- });
100
+ const tasks = ViewPlugin.fromClass(
101
+ class {
102
+ tasks: DecorationSet;
103
+ constructor(view: EditorView) {
104
+ this.tasks = taskMatcher.createDeco(view);
105
+ }
92
106
 
93
- return builder.finish();
94
- };
107
+ update(update: ViewUpdate) {
108
+ this.tasks = taskMatcher.updateDeco(update, this.tasks);
109
+ }
110
+ },
111
+ {
112
+ decorations: (instance) => instance.tasks,
113
+ provide: (plugin) =>
114
+ EditorView.atomicRanges.of((view) => {
115
+ return view.plugin(plugin)?.tasks || Decoration.none;
116
+ }),
117
+ },
118
+ );
95
119
 
96
- return [
97
- listener,
98
- StateField.define<RangeSet<any>>({
99
- create: (state) => checkbox(state),
100
- update: (_: RangeSet<any>, tr: Transaction) => checkbox(tr.state),
101
- provide: (field) => EditorView.decorations.from(field),
102
- }),
103
- ];
120
+ return [tasks, styles];
104
121
  };
105
-
106
- // TODO(burdon): Reconcile with theme.
107
- export const styles = EditorView.baseTheme({
108
- '& .cm-task-item': {
109
- padding: '0 4px',
110
- },
111
- });
112
-
113
- export const tasklist = () => [statefield(), styles];
@@ -10,6 +10,11 @@ import { tokens } from '../../../styles';
10
10
  /**
11
11
  * Minimal styles.
12
12
  * https://codemirror.net/examples/styling
13
+ *
14
+ * Examples:
15
+ * - https://github.com/codemirror/view/blob/main/src/theme.ts
16
+ * - https://github.com/codemirror/theme-one-dark/blob/main/src/one-dark.ts
17
+ *
13
18
  * NOTE: Use one of '&', '&light', and '&dark' prefix to scope instance.
14
19
  * NOTE: `light` and `dark` selectors are preprocessed by CodeMirror and can only be in the base theme.
15
20
  */
@@ -136,8 +141,55 @@ export const defaultTheme: {
136
141
  '& .cm-tooltip': {
137
142
  border: 'none',
138
143
  },
139
- // '& .cm-tooltip-below': {},
140
- // '& .cm-tooltip-autocomplete': {},
144
+ '& .cm-tooltip-below': {},
145
+
146
+ //
147
+ // autocomplete
148
+ //
149
+ '& .cm-tooltip-autocomplete': {
150
+ marginTop: '4px',
151
+ marginLeft: '-3px',
152
+ },
153
+ '& .cm-tooltip-autocomplete ul li': {},
154
+ '& .cm-tooltip-autocomplete ul li[aria-selected]': {},
155
+ '& .cm-completionIcon': {
156
+ display: 'none',
157
+ },
158
+ '& .cm-completionLabel': {
159
+ fontFamily: get(tokens, 'fontFamily.body', []).join(','),
160
+ },
161
+ '& .cm-completionMatchedText': {
162
+ textDecoration: 'none',
163
+ },
164
+
165
+ //
166
+ // widgets
167
+ //
168
+ '& .cm-widgetBuffer': {
169
+ display: 'none',
170
+ height: 0,
171
+ },
172
+
173
+ //
174
+ // table
175
+ //
176
+ '& .cm-table-head': {
177
+ padding: '2px 16px 2px 0px',
178
+ borderBottom: `1px solid ${get(tokens, 'extend.colors.neutral.500')}`,
179
+ fontWeight: 100,
180
+ textAlign: 'left',
181
+ color: get(tokens, 'extend.colors.neutral.500'),
182
+ },
183
+ '& .cm-table-cell': {
184
+ padding: '2px 16px 2px 0px',
185
+ },
186
+
187
+ //
188
+ // image
189
+ //
190
+ '& .cm-image': {
191
+ margin: '0.5rem 0',
192
+ },
141
193
 
142
194
  //
143
195
  // font size
@@ -159,6 +211,18 @@ export const textTheme: {
159
211
  '& .cm-scroller': {
160
212
  fontFamily: get(tokens, 'fontFamily.body', []).join(','),
161
213
  },
214
+ '& .cm-placeholder': {
215
+ fontFamily: get(tokens, 'fontFamily.body', []).join(','),
216
+ },
217
+ };
218
+
219
+ export const markdownTheme: {
220
+ [selector: string]: StyleSpec;
221
+ } = {
222
+ // NOTE: Must leave base font family as is (i.e., monospace) due to fenced code blocks.
223
+ '& .cm-placeholder': {
224
+ fontFamily: get(tokens, 'fontFamily.body', []).join(','),
225
+ },
162
226
  };
163
227
 
164
228
  export const codeTheme: {
@@ -167,4 +231,7 @@ export const codeTheme: {
167
231
  '& .cm-scroller': {
168
232
  fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
169
233
  },
234
+ '& .cm-placeholder': {
235
+ fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
236
+ },
170
237
  };
@@ -2,20 +2,24 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { type EditorView } from '@codemirror/view';
6
8
 
7
9
  import { next as automerge } from '@dxos/automerge/automerge';
8
10
 
9
- import amToCodemirror from './amToCodemirror';
10
- import codeMirrorToAm from './codeMirrorToAm';
11
11
  import { type IDocHandle } from './handle';
12
12
  import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
13
+ import { automergeToCodemirror, codemirrorToAutomerge } from './translation';
13
14
 
14
15
  type Doc<T> = automerge.Doc<T>;
15
16
  type Heads = automerge.Heads;
16
17
 
17
18
  type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
18
19
 
20
+ /**
21
+ * TODO(burdon): Comment.
22
+ */
19
23
  export class PatchSemaphore {
20
24
  _field!: Field;
21
25
  _inReconcile = false;
@@ -39,7 +43,7 @@ export class PatchSemaphore {
39
43
 
40
44
  const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
41
45
 
42
- // First undo all the unreconciled transactions
46
+ // First undo all the unreconciled transactions.
43
47
  const toInvert = transactions.slice().reverse();
44
48
  for (const tx of toInvert) {
45
49
  const inverted = tx.changes.invert(tx.startState.doc);
@@ -50,19 +54,18 @@ export class PatchSemaphore {
50
54
  });
51
55
  }
52
56
 
53
- // now apply the unreconciled transactions to the document
54
- let newHeads = codeMirrorToAm(this._field, handle, transactions, view.state);
57
+ // Apply the unreconciled transactions to the document.
58
+ let newHeads = codemirrorToAutomerge(this._field, handle, transactions, view.state);
55
59
 
56
- // NOTE: null and undefined each come from automerge and repo respectively
60
+ // NOTE: null and undefined each come from automerge and repo respectively.
57
61
  if (newHeads === null || newHeads === undefined) {
58
62
  // TODO: @alexjg this is the call that's resetting the editor state on click
59
63
  newHeads = automerge.getHeads(handle.docSync()!);
60
64
  }
61
65
 
62
- // now get the diff between the updated state of the document and the heads
63
- // and apply that to the codemirror doc
66
+ // Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
64
67
  const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
65
- amToCodemirror(view, selection, path, diff);
68
+ automergeToCodemirror(view, selection, path, diff);
66
69
 
67
70
  view.dispatch({
68
71
  effects: updateHeads(newHeads),
@@ -12,7 +12,8 @@ import React, { useEffect, useRef, useState } from 'react';
12
12
  import { type Prop } from '@dxos/automerge/automerge';
13
13
  import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
14
14
 
15
- import { type IDocHandle, automergePlugin } from './automerge-plugin';
15
+ import { type IDocHandle } from './handle';
16
+ import { automergePlugin } from './plugin';
16
17
 
17
18
  type EditorProps = {
18
19
  handle: IDocHandle;
@@ -81,9 +82,10 @@ const Story = () => {
81
82
  };
82
83
 
83
84
  export default {
84
- title: 'Automerge',
85
+ title: 'react-ui-editor/Automerge',
86
+ component: Editor,
85
87
  };
86
88
 
87
- export const AutomergeRepo = {
89
+ export const Default = {
88
90
  render: () => <Story />,
89
91
  };
@@ -78,6 +78,7 @@ export const automergePlugin = (handle: IDocHandle, path: Prop[]): AutomergePlug
78
78
  return result;
79
79
  },
80
80
  });
81
+
81
82
  const semaphore = new PatchSemaphore(stateField);
82
83
 
83
84
  const viewPlugin = ViewPlugin.fromClass(
@@ -2,6 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
6
8
  import { type EditorView } from '@codemirror/view';
7
9
 
@@ -14,9 +16,14 @@ import {
14
16
  type SpliceTextPatch,
15
17
  } from '@dxos/automerge/automerge';
16
18
 
17
- import { reconcileAnnotationType } from './plugin';
19
+ import { reconcileAnnotationType } from '../plugin';
18
20
 
19
- export default (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
21
+ export const automergeToCodemirror = (
22
+ view: EditorView,
23
+ selection: EditorSelection,
24
+ target: Prop[],
25
+ patches: Patch[],
26
+ ) => {
20
27
  for (const patch of patches) {
21
28
  const changeSpec = handlePatch(patch, target, view.state);
22
29
  if (changeSpec != null) {
@@ -28,6 +35,7 @@ export default (view: EditorView, selection: EditorSelection, target: Prop[], pa
28
35
  });
29
36
  }
30
37
  }
38
+
31
39
  view.dispatch({
32
40
  selection,
33
41
  annotations: reconcileAnnotationType.of({}),
@@ -2,14 +2,16 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ // TODO(burdon): Additional copyright?
6
+
5
7
  import { type EditorState, type Text, type Transaction } from '@codemirror/state';
6
8
 
7
9
  import { next as am, type Heads } from '@dxos/automerge/automerge';
8
10
 
9
- import { type IDocHandle } from './handle';
10
- import { type Field } from './plugin';
11
+ import { type IDocHandle } from '../handle';
12
+ import { type Field } from '../plugin';
11
13
 
12
- export default (
14
+ export const codemirrorToAutomerge = (
13
15
  field: Field,
14
16
  handle: IDocHandle,
15
17
  transactions: Transaction[],
@@ -18,7 +20,7 @@ export default (
18
20
  const { lastHeads, path } = state.field(field);
19
21
 
20
22
  // We don't want to call `automerge.updateAt` if there are no changes.
21
- // Otherwise later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
23
+ // Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
22
24
  let hasChanges = false;
23
25
  for (const tr of transactions) {
24
26
  tr.changes.iterChanges(() => {
@@ -37,5 +39,6 @@ export default (
37
39
  });
38
40
  }
39
41
  });
42
+
40
43
  return newHeads ?? undefined;
41
44
  };
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './automerge-to-codemirror';
6
+ export * from './codemirror-to-automerge';
@@ -2,6 +2,5 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './useCollaboration';
6
5
  export * from './useTextModel';
7
6
  export * from './yjs';