@dxos/react-ui-editor 0.3.10-next.ef70620 → 0.3.11-main.0e40963

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 (169) hide show
  1. package/dist/lib/browser/index.mjs +2226 -971
  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 +63 -0
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -0
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +9 -4
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +12 -4
  9. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/index.d.ts +1 -1
  11. package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
  12. package/dist/types/src/extensions/autocomplete.d.ts +10 -0
  13. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -0
  14. package/dist/types/src/{components/TextEditor/extensions/basic/bundle.d.ts → extensions/basic.d.ts} +3 -2
  15. package/dist/types/src/extensions/basic.d.ts.map +1 -0
  16. package/dist/types/src/extensions/blast.d.ts +25 -0
  17. package/dist/types/src/extensions/blast.d.ts.map +1 -0
  18. package/dist/types/src/extensions/code.d.ts +2 -0
  19. package/dist/types/src/extensions/code.d.ts.map +1 -0
  20. package/dist/types/src/extensions/comments.d.ts +48 -0
  21. package/dist/types/src/extensions/comments.d.ts.map +1 -0
  22. package/dist/types/src/extensions/hr.d.ts +2 -0
  23. package/dist/types/src/extensions/hr.d.ts.map +1 -0
  24. package/dist/types/src/extensions/image.d.ts +4 -0
  25. package/dist/types/src/extensions/image.d.ts.map +1 -0
  26. package/dist/types/src/extensions/index.d.ts +16 -0
  27. package/dist/types/src/extensions/index.d.ts.map +1 -0
  28. package/dist/types/src/extensions/link.d.ts +12 -0
  29. package/dist/types/src/extensions/link.d.ts.map +1 -0
  30. package/dist/types/src/extensions/listener.d.ts +10 -0
  31. package/dist/types/src/extensions/listener.d.ts.map +1 -0
  32. package/dist/types/src/extensions/markdown/bundle.d.ts +17 -0
  33. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
  34. package/dist/types/src/extensions/markdown/highlight.d.ts +36 -0
  35. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
  36. package/dist/types/src/extensions/markdown/index.d.ts +3 -0
  37. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
  38. package/dist/types/src/extensions/mention.d.ts +6 -0
  39. package/dist/types/src/extensions/mention.d.ts.map +1 -0
  40. package/dist/types/src/extensions/mermaid.d.ts +3 -0
  41. package/dist/types/src/extensions/mermaid.d.ts.map +1 -0
  42. package/dist/types/src/extensions/table.d.ts +8 -0
  43. package/dist/types/src/extensions/table.d.ts.map +1 -0
  44. package/dist/types/src/extensions/tasklist.d.ts +3 -0
  45. package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
  46. package/dist/types/src/extensions/typewriter.d.ts +10 -0
  47. package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
  48. package/dist/types/src/extensions/util.d.ts +5 -0
  49. package/dist/types/src/extensions/util.d.ts.map +1 -0
  50. package/dist/types/src/hooks/automerge/automerge.stories.d.ts +15 -0
  51. package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -0
  52. package/dist/types/src/hooks/automerge/handle.d.ts.map +1 -0
  53. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/index.d.ts +1 -1
  54. package/dist/types/src/hooks/automerge/index.d.ts.map +1 -0
  55. package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -0
  56. package/dist/types/src/{automerge/automerge-plugin/PatchSemaphore.d.ts → hooks/automerge/semaphore.d.ts} +4 -1
  57. package/dist/types/src/hooks/automerge/semaphore.d.ts.map +1 -0
  58. package/dist/types/src/{automerge/automerge-plugin/codeMirrorToAm.d.ts → hooks/automerge/update-automerge.d.ts} +2 -3
  59. package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +1 -0
  60. package/dist/types/src/hooks/automerge/update-codemirror.d.ts +5 -0
  61. package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +1 -0
  62. package/dist/types/src/hooks/index.d.ts +0 -1
  63. package/dist/types/src/hooks/index.d.ts.map +1 -1
  64. package/dist/types/src/hooks/useTextModel.d.ts +19 -5
  65. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  66. package/dist/types/src/{components/TextEditor → hooks/yjs}/yjs.stories.d.ts +2 -1
  67. package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +1 -0
  68. package/dist/types/src/hooks/yjs/yjs.test.d.ts +2 -0
  69. package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +1 -0
  70. package/dist/types/src/index.d.ts +3 -0
  71. package/dist/types/src/index.d.ts.map +1 -1
  72. package/dist/types/src/styles/markdown.d.ts +14 -11
  73. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  74. package/dist/types/src/testing/replicator.d.ts.map +1 -1
  75. package/dist/types/src/themes/default.d.ts +17 -0
  76. package/dist/types/src/themes/default.d.ts.map +1 -0
  77. package/dist/types/src/themes/index.d.ts +2 -0
  78. package/dist/types/src/themes/index.d.ts.map +1 -0
  79. package/package.json +28 -33
  80. package/src/components/TextEditor/MarkdownEditor.stories.tsx +426 -0
  81. package/src/components/TextEditor/TextEditor.stories.tsx +57 -43
  82. package/src/components/TextEditor/TextEditor.tsx +98 -52
  83. package/src/components/TextEditor/index.ts +1 -1
  84. package/src/extensions/autocomplete.ts +59 -0
  85. package/src/{components/TextEditor/extensions/basic/bundle.ts → extensions/basic.ts} +8 -4
  86. package/src/extensions/blast.ts +373 -0
  87. package/src/extensions/code.ts +99 -0
  88. package/src/extensions/comments.ts +391 -0
  89. package/src/extensions/hr.ts +74 -0
  90. package/src/extensions/image.ts +69 -0
  91. package/src/extensions/index.ts +19 -0
  92. package/src/extensions/link.ts +192 -0
  93. package/src/extensions/listener.ts +35 -0
  94. package/src/extensions/markdown/bundle.ts +106 -0
  95. package/src/extensions/markdown/highlight.ts +204 -0
  96. package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +1 -1
  97. package/src/extensions/mention.ts +31 -0
  98. package/src/extensions/mermaid.ts +11 -0
  99. package/src/extensions/table.ts +132 -0
  100. package/src/extensions/tasklist.ts +121 -0
  101. package/src/extensions/typewriter.ts +68 -0
  102. package/src/extensions/util.ts +18 -0
  103. package/src/{automerge → hooks/automerge}/automerge.stories.tsx +7 -5
  104. package/src/{automerge/automerge-plugin → hooks/automerge}/plugin.ts +2 -1
  105. package/src/{automerge/automerge-plugin/PatchSemaphore.ts → hooks/automerge/semaphore.ts} +13 -9
  106. package/src/{automerge/automerge-plugin/codeMirrorToAm.ts → hooks/automerge/update-automerge.ts} +5 -2
  107. package/src/{automerge/automerge-plugin/amToCodemirror.ts → hooks/automerge/update-codemirror.ts} +4 -1
  108. package/src/hooks/index.ts +0 -1
  109. package/src/hooks/useTextModel.ts +102 -42
  110. package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +2 -1
  111. package/src/hooks/yjs/yjs.test.ts +59 -0
  112. package/src/index.ts +3 -0
  113. package/src/styles/markdown.ts +33 -24
  114. package/src/testing/replicator.ts +6 -6
  115. package/src/themes/default.ts +302 -0
  116. package/src/themes/index.ts +5 -0
  117. package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +0 -1
  118. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts +0 -6
  119. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts.map +0 -1
  120. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +0 -1
  121. package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +0 -1
  122. package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +0 -1
  123. package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +0 -1
  124. package/dist/types/src/automerge/automerge.stories.d.ts +0 -9
  125. package/dist/types/src/automerge/automerge.stories.d.ts.map +0 -1
  126. package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts.map +0 -1
  127. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts +0 -3
  128. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts.map +0 -1
  129. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts +0 -10
  130. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts.map +0 -1
  131. package/dist/types/src/components/TextEditor/extensions/change.d.ts +0 -7
  132. package/dist/types/src/components/TextEditor/extensions/change.d.ts.map +0 -1
  133. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts +0 -11
  134. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts.map +0 -1
  135. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts +0 -8
  136. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts.map +0 -1
  137. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts +0 -10
  138. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts.map +0 -1
  139. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts +0 -4
  140. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts.map +0 -1
  141. package/dist/types/src/components/TextEditor/extensions/index.d.ts +0 -5
  142. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
  143. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +0 -8
  144. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
  145. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts +0 -3
  146. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
  147. package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts +0 -17
  148. package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts.map +0 -1
  149. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts +0 -21
  150. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts.map +0 -1
  151. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  152. package/dist/types/src/hooks/useCollaboration.d.ts +0 -9
  153. package/dist/types/src/hooks/useCollaboration.d.ts.map +0 -1
  154. package/src/components/TextEditor/extensions/basic/index.ts +0 -6
  155. package/src/components/TextEditor/extensions/basic/theme.ts +0 -49
  156. package/src/components/TextEditor/extensions/change.ts +0 -22
  157. package/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.ts +0 -99
  158. package/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.tsx +0 -61
  159. package/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.tsx +0 -119
  160. package/src/components/TextEditor/extensions/hyperlink/index.ts +0 -7
  161. package/src/components/TextEditor/extensions/index.ts +0 -8
  162. package/src/components/TextEditor/extensions/markdown/bundle.ts +0 -99
  163. package/src/components/TextEditor/extensions/markdown/tags.ts +0 -38
  164. package/src/components/TextEditor/extensions/markdown/theme.ts +0 -265
  165. package/src/hooks/useCollaboration.ts +0 -45
  166. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/handle.d.ts +0 -0
  167. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/plugin.d.ts +0 -0
  168. package/src/{automerge/automerge-plugin → hooks/automerge}/handle.ts +0 -0
  169. package/src/{automerge/automerge-plugin → hooks/automerge}/index.ts +1 -1
@@ -0,0 +1,132 @@
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
+ // TODO(burdon): Advanced formatting (left/right/center).
13
+ // TODO(burdon): Editor to auto balance columns.
14
+
15
+ export type TableOptions = {};
16
+
17
+ /**
18
+ * GFM tables.
19
+ * https://github.github.com/gfm/#tables-extension
20
+ */
21
+ export const table = (options: TableOptions = {}) => {
22
+ return StateField.define<RangeSet<any>>({
23
+ create: (state) => update(state, options),
24
+ update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
25
+ provide: (field) => EditorView.decorations.from(field),
26
+ });
27
+ };
28
+
29
+ type Table = {
30
+ from: number;
31
+ to: number;
32
+ header?: string[];
33
+ rows?: string[][];
34
+ };
35
+
36
+ const update = (state: EditorState, options: TableOptions) => {
37
+ const builder = new RangeSetBuilder();
38
+ const cursor = state.selection.main.head;
39
+
40
+ const tables: Table[] = [];
41
+ const getTable = () => tables[tables.length - 1];
42
+ const getRow = () => {
43
+ const table = getTable();
44
+ return table.rows?.[table.rows.length - 1];
45
+ };
46
+
47
+ // Parse table.
48
+ syntaxTree(state).iterate({
49
+ enter: (node) => {
50
+ // Check if cursor is inside text.
51
+ switch (node.name) {
52
+ case 'Table': {
53
+ tables.push({ from: node.from, to: node.to });
54
+ break;
55
+ }
56
+ case 'TableHeader': {
57
+ getTable().header = [];
58
+ break;
59
+ }
60
+ case 'TableRow': {
61
+ (getTable().rows ??= []).push([]);
62
+ break;
63
+ }
64
+ case 'TableCell': {
65
+ const row = getRow();
66
+ if (row) {
67
+ row.push(state.sliceDoc(node.from, node.to));
68
+ } else {
69
+ getTable().header?.push(state.sliceDoc(node.from, node.to));
70
+ }
71
+ break;
72
+ }
73
+ }
74
+ },
75
+ });
76
+
77
+ tables.forEach((table) => {
78
+ const hide = state.readOnly || cursor < table.from || cursor > table.to;
79
+ hide &&
80
+ builder.add(
81
+ table.from,
82
+ table.to,
83
+ Decoration.replace({
84
+ block: true,
85
+ widget: new TableWidget(table),
86
+ }),
87
+ );
88
+
89
+ // Add class for styling.
90
+ builder.add(table.from, table.to, Decoration.mark({ class: 'cm-table' }));
91
+ });
92
+
93
+ return builder.finish();
94
+ };
95
+
96
+ class TableWidget extends WidgetType {
97
+ constructor(readonly _table: Table) {
98
+ super();
99
+ }
100
+
101
+ override eq(other: WidgetType) {
102
+ return this._table.from === (other as any as TableWidget)?._table?.from;
103
+ }
104
+
105
+ toDOM(view: EditorView) {
106
+ const table = document.createElement('table');
107
+
108
+ {
109
+ const header = table.appendChild(document.createElement('thead'));
110
+ const tr = header.appendChild(document.createElement('tr'));
111
+ this._table.header?.forEach((cell) => {
112
+ const th = document.createElement('th');
113
+ th.setAttribute('class', 'cm-table-head');
114
+ tr.appendChild(th).textContent = cell;
115
+ });
116
+ }
117
+
118
+ {
119
+ const body = table.appendChild(document.createElement('tbody'));
120
+ this._table.rows?.forEach((row) => {
121
+ const tr = body.appendChild(document.createElement('tr'));
122
+ row.forEach((cell) => {
123
+ const td = document.createElement('td');
124
+ td.setAttribute('class', 'cm-table-cell');
125
+ tr.appendChild(td).textContent = cell;
126
+ });
127
+ });
128
+ }
129
+
130
+ return table;
131
+ }
132
+ }
@@ -0,0 +1,121 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import {
6
+ EditorView,
7
+ Decoration,
8
+ WidgetType,
9
+ MatchDecorator,
10
+ ViewPlugin,
11
+ type DecorationSet,
12
+ type ViewUpdate,
13
+ } from '@codemirror/view';
14
+
15
+ // TODO(burdon): Reconcile with theme.
16
+ const styles = EditorView.baseTheme({
17
+ '& .cm-task-item': {
18
+ paddingLeft: '4px',
19
+ paddingRight: '8px',
20
+ },
21
+ });
22
+
23
+ class CheckboxWidget extends WidgetType {
24
+ constructor(
25
+ private readonly _pos: number,
26
+ private _checked: boolean,
27
+ private readonly _indent: number,
28
+ private readonly _onCheck?: (check: boolean) => void,
29
+ ) {
30
+ super();
31
+ }
32
+
33
+ override eq(other: CheckboxWidget) {
34
+ return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
35
+ }
36
+
37
+ toDOM(view: EditorView) {
38
+ const wrap = document.createElement('span');
39
+ wrap.className = 'cm-task-item';
40
+ wrap.setAttribute('aria-hidden', 'true');
41
+ wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
42
+
43
+ const input = wrap.appendChild(document.createElement('input'));
44
+ input.type = 'checkbox';
45
+ input.checked = this._checked;
46
+ if (!this._onCheck) {
47
+ input.setAttribute('disabled', 'true');
48
+ }
49
+ if (this._onCheck) {
50
+ input.onchange = (event: Event) => {
51
+ this._onCheck?.((event.target as any).checked);
52
+ return true;
53
+ };
54
+ }
55
+
56
+ return wrap;
57
+ }
58
+
59
+ override ignoreEvent() {
60
+ return false;
61
+ }
62
+ }
63
+
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
+ },
98
+ });
99
+
100
+ const tasks = ViewPlugin.fromClass(
101
+ class {
102
+ tasks: DecorationSet;
103
+ constructor(view: EditorView) {
104
+ this.tasks = taskMatcher.createDeco(view);
105
+ }
106
+
107
+ update(update: ViewUpdate) {
108
+ this.tasks = taskMatcher.updateDeco(update, this.tasks);
109
+ }
110
+ },
111
+ {
112
+ decorations: (value) => value.tasks,
113
+ provide: (plugin) =>
114
+ EditorView.atomicRanges.of((view) => {
115
+ return view.plugin(plugin)?.tasks || Decoration.none;
116
+ }),
117
+ },
118
+ );
119
+
120
+ return [tasks, styles];
121
+ };
@@ -0,0 +1,68 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Extension } from '@codemirror/state';
6
+ import { keymap } from '@codemirror/view';
7
+
8
+ // TODO(burdon): Review https://github.com/sergeche/codemirror-movie?tab=readme-ov-file
9
+
10
+ export type DemoOptions = {
11
+ delay?: number;
12
+ items?: string[];
13
+ };
14
+
15
+ const defaultItems = ['hello world!', 'this is a test.', 'this is [DXOS](https://dxos.org)'];
16
+
17
+ /**
18
+ * Configurable plugin that let's user cycle through pre-configured input script.
19
+ */
20
+ export const typewriter = ({ delay = 75, items = defaultItems }: DemoOptions = {}): Extension => {
21
+ let t: any;
22
+ let idx = 0; // TODO(burdon): Make global.
23
+
24
+ return [
25
+ keymap.of([
26
+ {
27
+ // Reset.
28
+ key: "alt-meta-'",
29
+ run: (view) => {
30
+ clearTimeout(t);
31
+ idx = 0;
32
+ return true;
33
+ },
34
+ },
35
+ {
36
+ // Next prompt.
37
+ // TODO(burdon): Press 1-9 to select prompt?
38
+ key: "shift-meta-'",
39
+ run: (view) => {
40
+ clearTimeout(t);
41
+ // TODO(burdon): Add space if needed.
42
+ const text = items[idx++];
43
+ if (idx === items?.length) {
44
+ idx = 0;
45
+ }
46
+
47
+ let i = 0;
48
+ const insert = (d = 0) => {
49
+ t = setTimeout(() => {
50
+ const pos = view.state.selection.main.head;
51
+ view.dispatch({
52
+ changes: { from: pos, insert: text[i++] },
53
+ selection: { anchor: pos + 1 },
54
+ });
55
+
56
+ if (i < text.length) {
57
+ insert(Math.random() * delay * (text[i] === ' ' ? 2 : 1));
58
+ }
59
+ }, d);
60
+ };
61
+
62
+ insert();
63
+ return true;
64
+ },
65
+ },
66
+ ]),
67
+ ];
68
+ };
@@ -0,0 +1,18 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { log } from '@dxos/log';
6
+
7
+ /**
8
+ * CodeMirror callbacks swallow errors so wrap handlers.
9
+ */
10
+ // TODO(burdon): Figure out how to systematize this.
11
+ export const callbackWrapper = <T extends Function>(fn: T): T =>
12
+ ((...args: any[]) => {
13
+ try {
14
+ return fn(...args);
15
+ } catch (error) {
16
+ log.catch(error);
17
+ }
18
+ }) as unknown as T;
@@ -9,10 +9,12 @@ import { basicSetup } from 'codemirror';
9
9
  import get from 'lodash.get';
10
10
  import React, { useEffect, useRef, useState } from 'react';
11
11
 
12
+ // TODO(burdon): Why separate imports?
12
13
  import { type Prop } from '@dxos/automerge/automerge';
13
14
  import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
14
15
 
15
- import { type IDocHandle, automergePlugin } from './automerge-plugin';
16
+ import { type IDocHandle } from './handle';
17
+ import { automergePlugin } from './plugin';
16
18
 
17
19
  type EditorProps = {
18
20
  handle: IDocHandle;
@@ -81,9 +83,9 @@ const Story = () => {
81
83
  };
82
84
 
83
85
  export default {
84
- title: 'Automerge',
85
- };
86
-
87
- export const AutomergeRepo = {
86
+ title: 'react-ui-editor/Automerge',
87
+ component: Editor,
88
88
  render: () => <Story />,
89
89
  };
90
+
91
+ export const Default = {};
@@ -17,8 +17,8 @@ import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '
17
17
  import * as automerge from '@dxos/automerge/automerge';
18
18
  import { type Heads, type Prop } from '@dxos/automerge/automerge';
19
19
 
20
- import { PatchSemaphore } from './PatchSemaphore';
21
20
  import { type IDocHandle } from './handle';
21
+ import { PatchSemaphore } from './semaphore';
22
22
 
23
23
  export type Value = {
24
24
  lastHeads: Heads;
@@ -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,20 +2,25 @@
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 { updateAutomerge } from './update-automerge';
14
+ import { updateCodeMirror } from './update-codemirror';
13
15
 
14
16
  type Doc<T> = automerge.Doc<T>;
15
17
  type Heads = automerge.Heads;
16
18
 
17
19
  type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
18
20
 
21
+ /**
22
+ * TODO(burdon): Comment.
23
+ */
19
24
  export class PatchSemaphore {
20
25
  _field!: Field;
21
26
  _inReconcile = false;
@@ -39,7 +44,7 @@ export class PatchSemaphore {
39
44
 
40
45
  const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
41
46
 
42
- // First undo all the unreconciled transactions
47
+ // First undo all the unreconciled transactions.
43
48
  const toInvert = transactions.slice().reverse();
44
49
  for (const tx of toInvert) {
45
50
  const inverted = tx.changes.invert(tx.startState.doc);
@@ -50,19 +55,18 @@ export class PatchSemaphore {
50
55
  });
51
56
  }
52
57
 
53
- // now apply the unreconciled transactions to the document
54
- let newHeads = codeMirrorToAm(this._field, handle, transactions, view.state);
58
+ // Apply the unreconciled transactions to the document.
59
+ let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
55
60
 
56
- // NOTE: null and undefined each come from automerge and repo respectively
61
+ // NOTE: null and undefined each come from automerge and repo respectively.
57
62
  if (newHeads === null || newHeads === undefined) {
58
63
  // TODO: @alexjg this is the call that's resetting the editor state on click
59
64
  newHeads = automerge.getHeads(handle.docSync()!);
60
65
  }
61
66
 
62
- // now get the diff between the updated state of the document and the heads
63
- // and apply that to the codemirror doc
67
+ // Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
64
68
  const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
65
- amToCodemirror(view, selection, path, diff);
69
+ updateCodeMirror(view, selection, path, diff);
66
70
 
67
71
  view.dispatch({
68
72
  effects: updateHeads(newHeads),
@@ -2,6 +2,8 @@
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';
@@ -9,7 +11,7 @@ import { next as am, type Heads } from '@dxos/automerge/automerge';
9
11
  import { type IDocHandle } from './handle';
10
12
  import { type Field } from './plugin';
11
13
 
12
- export default (
14
+ export const updateAutomerge = (
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
  };
@@ -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
 
@@ -16,7 +18,7 @@ import {
16
18
 
17
19
  import { reconcileAnnotationType } from './plugin';
18
20
 
19
- export default (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
21
+ export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
20
22
  for (const patch of patches) {
21
23
  const changeSpec = handlePatch(patch, target, view.state);
22
24
  if (changeSpec != null) {
@@ -28,6 +30,7 @@ export default (view: EditorView, selection: EditorSelection, target: Prop[], pa
28
30
  });
29
31
  }
30
32
  }
33
+
31
34
  view.dispatch({
32
35
  selection,
33
36
  annotations: reconcileAnnotationType.of({}),
@@ -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';