@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
@@ -4,20 +4,20 @@
4
4
  //
5
5
 
6
6
  import { syntaxTree } from '@codemirror/language';
7
+ import { type Extension, RangeSetBuilder } from '@codemirror/state';
7
8
  import {
8
- type EditorState,
9
- type Extension,
10
- type RangeSet,
11
- RangeSetBuilder,
12
- StateField,
13
- type Transaction,
14
- } from '@codemirror/state';
15
- import { Decoration, EditorView, WidgetType, hoverTooltip } from '@codemirror/view';
9
+ hoverTooltip,
10
+ Decoration,
11
+ type DecorationSet,
12
+ type EditorView,
13
+ ViewPlugin,
14
+ type ViewUpdate,
15
+ WidgetType,
16
+ } from '@codemirror/view';
17
+ import { type SyntaxNode } from '@lezer/common';
16
18
 
17
19
  import { tooltipContent } from '@dxos/react-ui-theme';
18
20
 
19
- const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)/;
20
-
21
21
  // Adapted from:
22
22
  // https://codemirror.net/try/?c=aW1wb3J0IHsgYmFzaWNTZXR1cCwgRWRpdG9yVmlldyB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7IG1hcmtkb3duIH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1tYXJrZG93biIKaW1wb3J0IHsgc3ludGF4VHJlZSB9IGZyb20gIkBjb2RlbWlycm9yL2xhbmd1YWdlIjsKaW1wb3J0IHsgRWRpdG9yU3RhdGUsIFJhbmdlU2V0QnVpbGRlciwgU3RhdGVGaWVsZCB9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIjsKaW1wb3J0IHsgRGVjb3JhdGlvbiwgV2lkZ2V0VHlwZSB9IGZyb20gIkBjb2RlbWlycm9yL3ZpZXciOwoKY2xhc3MgTGluayBleHRlbmRzIFdpZGdldFR5cGUgewogIGNvbnN0cnVjdG9yKHRleHQsIHVybCkgewogICAgc3VwZXIoKQogICAgdGhpcy50ZXh0ID0gdGV4dAogICAgdGhpcy51cmwgPSB1cmwKICB9CiAgZXEob3RoZXIpIHsgcmV0dXJuIHRoaXMudGV4dCA9PSBvdGhlci50ZXh0ICYmIHRoaXMudXJsID09IG90aGVyLnVybCB9CiAgdG9ET00oKSB7CiAgICBsZXQgbGluayA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoImEiKQogICAgbGluay50ZXh0Q29udGVudCA9IHRoaXMudGV4dAogICAgbGluay5ocmVmID0gdGhpcy51cmwKICAgIHJldHVybiBsaW5rOwogIH0KfQoKbGV0IGRlY29yYXRpb25zRmllbGQgPSBTdGF0ZUZpZWxkLmRlZmluZSh7CiAgY3JlYXRlKCkgewogICAgcmV0dXJuIERlY29yYXRpb24ubm9uZTsKICB9LAogIHVwZGF0ZShfLCB0cikgewogICAgY29uc3QgYnVpbGRlciA9IG5ldyBSYW5nZVNldEJ1aWxkZXIoKTsKICAgIGxldCBjdXJzb3IgPSB0ci5zdGF0ZS5zZWxlY3Rpb24ubWFpbi5oZWFkOwogICAgc3ludGF4VHJlZSh0ci5zdGF0ZSkuaXRlcmF0ZSh7CiAgICAgIGVudGVyOiAobm9kZSkgPT4gewogICAgICAgIGlmICgoY3Vyc29yIDwgbm9kZS5mcm9tIHx8IGN1cnNvciA+IG5vZGUudG8pICYmIG5vZGUubmFtZSA9PSAiTGluayIpIHsKICAgICAgICAgIGxldCBtYXJrcyA9IG5vZGUubm9kZS5nZXRDaGlsZHJlbigiTGlua01hcmsiKTsKICAgICAgICAgIGxldCB0ZXh0ID0gbWFya3MubGVuZ3RoID49IDIgPyB0ci5zdGF0ZS5zbGljZURvYyhtYXJrc1swXS50bywgbWFya3NbMV0uZnJvbSkgOiAiIgoKICAgICAgICAgIGxldCB1cmxOb2RlID0gbm9kZS5ub2RlLmdldENoaWxkKCJVUkwiKTsKICAgICAgICAgIGxldCB1cmwgPSB1cmxOb2RlID8gdHIuc3RhdGUuc2xpY2VEb2ModXJsTm9kZS5mcm9tLCB1cmxOb2RlLnRvKSA6ICIiCiAgICAgICAgICBidWlsZGVyLmFkZChub2RlLmZyb20sIG5vZGUudG8sIERlY29yYXRpb24ucmVwbGFjZSh7CiAgICAgICAgICAgIHdpZGdldDogbmV3IExpbmsodGV4dCwgdXJsKSwKICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSwKICAgIH0pOwogICAgcmV0dXJuIGJ1aWxkZXIuZmluaXNoKCk7CiAgfSwKICBwcm92aWRlOiAoZikgPT4gRWRpdG9yVmlldy5kZWNvcmF0aW9ucy5mcm9tKGYpLAp9KTsKCmxldCB2aWV3ID0gbmV3IEVkaXRvclZpZXcoewogIGRvYzogIiMgSGVsbG8gV29ybGRcblxuW1Rlc3RdKGh0dHBzOi8vZXhhbXBsZS5jb20pXG5cblxuIiwKICBleHRlbnNpb25zOiBbZGVjb3JhdGlvbnNGaWVsZCwgbWFya2Rvd24oKV0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5LAp9KTsKCnZpZXcuZm9jdXMoKTs=
23
23
  // https://discuss.codemirror.net/t/link-widget-is-clicked-only-if-editor-is-out-of-focus/7433
@@ -34,11 +34,23 @@ export type LinkOptions = {
34
34
  */
35
35
  export const link = (options: LinkOptions = {}): Extension => {
36
36
  const extensions: Extension[] = [
37
- StateField.define<RangeSet<any>>({
38
- create: (state) => update(state, options),
39
- update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
40
- provide: (field) => EditorView.decorations.from(field),
41
- }),
37
+ ViewPlugin.fromClass(
38
+ class {
39
+ decorations: DecorationSet;
40
+ constructor(view: EditorView) {
41
+ this.decorations = buildDecorations(view, options);
42
+ }
43
+
44
+ update(update: ViewUpdate) {
45
+ if (update.docChanged || update.viewportChanged || update.selectionSet) {
46
+ this.decorations = buildDecorations(update.view, options);
47
+ }
48
+ }
49
+ },
50
+ {
51
+ decorations: (value) => value.decorations,
52
+ },
53
+ ),
42
54
  ];
43
55
 
44
56
  if (options.onHover) {
@@ -46,41 +58,25 @@ export const link = (options: LinkOptions = {}): Extension => {
46
58
  // https://codemirror.net/examples/tooltip
47
59
  // https://codemirror.net/docs/ref/#view.hoverTooltip
48
60
  // https://github.com/codemirror/view/blob/main/src/tooltip.ts
49
- hoverTooltip((view, pos) => {
50
- const { from, text } = view.state.doc.lineAt(pos);
51
- const p = pos - from;
52
-
53
- let idx = 0;
54
- let match;
55
- do {
56
- match = text.substring(idx).match(markdownLinkRegexp);
57
- if (!match) {
58
- match = undefined;
59
- break;
60
- }
61
-
62
- idx = text.indexOf(match[0]);
63
- if (p >= idx && p < idx + match[0].length) {
64
- break;
65
- }
66
-
67
- idx += match[0].length;
68
- } while (true);
69
-
70
- if (!match) {
61
+ hoverTooltip((view, pos, side) => {
62
+ const syntax = syntaxTree(view.state).resolveInner(pos, side);
63
+ let link = null;
64
+ for (let i = 0, node: SyntaxNode | null = syntax; !link && node && i < 5; node = node.parent, i++) {
65
+ link = node.name === 'Link' ? node : null;
66
+ }
67
+ const url = link && link.getChild('URL');
68
+ if (!url || !link) {
71
69
  return null;
72
70
  }
73
-
74
- const [, , url] = match ?? [];
75
-
71
+ const urlText = view.state.sliceDoc(url.from, url.to);
76
72
  return {
77
- pos: idx + from,
78
- end: idx + from + match[0].length,
73
+ pos: link.from,
74
+ end: link.to,
79
75
  above: true,
80
76
  create: () => {
81
77
  const el = document.createElement('div');
82
78
  el.className = tooltipContent({}, 'pli-2 plb-1');
83
- options.onHover!(el, url);
79
+ options.onHover!(el, urlText);
84
80
  return { dom: el, offset: { x: 0, y: 4 } };
85
81
  },
86
82
  };
@@ -92,19 +88,15 @@ export const link = (options: LinkOptions = {}): Extension => {
92
88
  };
93
89
 
94
90
  class LinkButton extends WidgetType {
95
- constructor(
96
- private readonly _pos: number,
97
- private readonly _url: string,
98
- private readonly _onAttach: LinkOptions['onRender'],
99
- ) {
91
+ constructor(private readonly _url: string, private readonly _onAttach: LinkOptions['onRender']) {
100
92
  super();
101
93
  }
102
94
 
103
- override eq(other: LinkButton) {
104
- return this._pos === other._pos && this._url === other._url;
95
+ override eq(other: this) {
96
+ return this._url === other._url;
105
97
  }
106
98
 
107
- toDOM(view: EditorView) {
99
+ override toDOM(view: EditorView) {
108
100
  const el = document.createElement('span');
109
101
  this._onAttach!(el, this._url);
110
102
  return el;
@@ -116,11 +108,11 @@ class LinkText extends WidgetType {
116
108
  super();
117
109
  }
118
110
 
119
- override eq(other: LinkText) {
111
+ override eq(other: this) {
120
112
  return this._url === other._url;
121
113
  }
122
114
 
123
- toDOM(view: EditorView) {
115
+ override toDOM(view: EditorView) {
124
116
  const link = document.createElement('a');
125
117
  link.setAttribute('class', 'cm-link');
126
118
  link.textContent = this._text;
@@ -147,46 +139,51 @@ class LinkText extends WidgetType {
147
139
  * possibly overlapping ranges in such a way that they can efficiently be mapped though document changes.
148
140
  * https://codemirror.net/docs/ref/#state
149
141
  */
150
- const update = (state: EditorState, options: LinkOptions) => {
151
- const builder = new RangeSetBuilder();
142
+ const buildDecorations = (view: EditorView, options: LinkOptions): DecorationSet => {
143
+ const builder = new RangeSetBuilder<Decoration>();
144
+ const { state } = view;
152
145
  const cursor = state.selection.main.head;
153
146
 
154
- syntaxTree(state).iterate({
155
- enter: (node) => {
156
- // Check if cursor is inside text.
157
- if (node.name === 'Link') {
158
- const marks = node.node.getChildren('LinkMark');
159
- const text = marks.length >= 2 ? state.sliceDoc(marks[0].to, marks[1].from) : '';
160
- const urlNode = node.node.getChild('URL');
161
- const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : '';
162
- if (!url) {
163
- return false;
164
- }
147
+ for (const { from, to } of view.visibleRanges) {
148
+ syntaxTree(state).iterate({
149
+ enter: (node) => {
150
+ // Check if cursor is inside text.
151
+ if (node.name === 'Link') {
152
+ const marks = node.node.getChildren('LinkMark');
153
+ const text = marks.length >= 2 ? state.sliceDoc(marks[0].to, marks[1].from) : '';
154
+ const urlNode = node.node.getChild('URL');
155
+ const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : '';
156
+ if (!url) {
157
+ return false;
158
+ }
165
159
 
166
- if (state.readOnly || cursor < node.from || cursor > node.to) {
167
- builder.add(
168
- node.from,
169
- node.to,
170
- Decoration.replace({
171
- widget: new LinkText(text, options.link ? url : undefined),
172
- }),
173
- );
174
- }
160
+ if (!view.hasFocus || state.readOnly || cursor < node.from || cursor > node.to) {
161
+ builder.add(
162
+ node.from,
163
+ node.to,
164
+ Decoration.replace({
165
+ widget: new LinkText(text, options.link ? url : undefined),
166
+ }),
167
+ );
168
+ }
175
169
 
176
- if (options.onRender) {
177
- builder.add(
178
- node.to,
179
- node.to,
180
- Decoration.replace({
181
- widget: new LinkButton(node.from, url, options.onRender),
182
- }),
183
- );
184
- }
170
+ if (options.onRender) {
171
+ builder.add(
172
+ node.to,
173
+ node.to,
174
+ Decoration.replace({
175
+ widget: new LinkButton(url, options.onRender),
176
+ }),
177
+ );
178
+ }
185
179
 
186
- return false;
187
- }
188
- },
189
- });
180
+ return false;
181
+ }
182
+ },
183
+ from,
184
+ to,
185
+ });
186
+ }
190
187
 
191
188
  return builder.finish();
192
189
  };
@@ -81,7 +81,6 @@ const update = (state: EditorState, options: TableOptions) => {
81
81
  table.from,
82
82
  table.to,
83
83
  Decoration.replace({
84
- block: true,
85
84
  widget: new TableWidget(table),
86
85
  }),
87
86
  );
@@ -98,11 +97,14 @@ class TableWidget extends WidgetType {
98
97
  super();
99
98
  }
100
99
 
101
- override eq(other: WidgetType) {
102
- return this._table.from === (other as any as TableWidget)?._table?.from;
100
+ override eq(other: this) {
101
+ return (
102
+ this._table.header?.join() === other._table.header?.join() &&
103
+ this._table.rows?.join() === other._table.rows?.join()
104
+ );
103
105
  }
104
106
 
105
- toDOM(view: EditorView) {
107
+ override toDOM(view: EditorView) {
106
108
  const table = document.createElement('table');
107
109
 
108
110
  {
@@ -129,4 +131,8 @@ class TableWidget extends WidgetType {
129
131
 
130
132
  return table;
131
133
  }
134
+
135
+ override ignoreEvent(e: Event) {
136
+ return !/^mouse/.test(e.type);
137
+ }
132
138
  }
@@ -0,0 +1,110 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
8
+
9
+ import { getToken } from '../../styles';
10
+
11
+ // TODO(burdon): Reconcile with theme.
12
+ const styles = EditorView.baseTheme({
13
+ '& .cm-task': {
14
+ color: getToken('extend.colors.blue.500'),
15
+ },
16
+ '& .cm-task-checkbox': {
17
+ marginLeft: '4px',
18
+ marginRight: '4px',
19
+ },
20
+ });
21
+
22
+ class CheckboxWidget extends WidgetType {
23
+ constructor(private _checked: boolean) {
24
+ super();
25
+ }
26
+
27
+ override eq(other: this) {
28
+ return this._checked === other._checked;
29
+ }
30
+
31
+ override toDOM(view: EditorView) {
32
+ const input = document.createElement('input');
33
+ input.className = 'cm-task-checkbox';
34
+ input.type = 'checkbox';
35
+ input.checked = this._checked;
36
+ if (view.state.readOnly) {
37
+ input.setAttribute('disabled', 'true');
38
+ } else {
39
+ input.onmousedown = (event: Event) => {
40
+ const pos = view.posAtDOM(input);
41
+ const text = view.state.sliceDoc(pos, pos + 3);
42
+ if (text === (this._checked ? '[x]' : '[ ]')) {
43
+ view.dispatch({
44
+ changes: { from: pos + 1, to: pos + 2, insert: this._checked ? ' ' : 'x' },
45
+ });
46
+ event.preventDefault();
47
+ }
48
+ };
49
+ }
50
+
51
+ return input;
52
+ }
53
+
54
+ override ignoreEvent() {
55
+ return false;
56
+ }
57
+ }
58
+
59
+ const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
60
+ const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
61
+
62
+ const buildDecorations = (view: EditorView): DecorationSet => {
63
+ const builder = new RangeSetBuilder<Decoration>();
64
+ const { state } = view;
65
+ const cursor = state.selection.main.head;
66
+
67
+ for (const { from, to } of view.visibleRanges) {
68
+ syntaxTree(state).iterate({
69
+ enter: (node) => {
70
+ if (node.name === 'TaskMarker') {
71
+ // Check if cursor is inside text.
72
+ if (cursor < node.from || cursor > node.to) {
73
+ const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
74
+ builder.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
75
+ builder.add(node.from, node.to, checked ? checkedDecoration : uncheckedDecoration);
76
+ }
77
+ }
78
+ },
79
+ from,
80
+ to,
81
+ });
82
+ }
83
+
84
+ return builder.finish();
85
+ };
86
+
87
+ export type TasklistOptions = {};
88
+
89
+ export const tasklist = (options: TasklistOptions = {}) => {
90
+ return [
91
+ ViewPlugin.fromClass(
92
+ class {
93
+ decorations: DecorationSet;
94
+ constructor(view: EditorView) {
95
+ this.decorations = buildDecorations(view);
96
+ }
97
+
98
+ update(update: ViewUpdate) {
99
+ if (update.docChanged || update.viewportChanged || update.selectionSet) {
100
+ this.decorations = buildDecorations(update.view);
101
+ }
102
+ }
103
+ },
104
+ {
105
+ decorations: (value) => value.decorations,
106
+ },
107
+ ),
108
+ styles,
109
+ ];
110
+ };
@@ -9,18 +9,25 @@ export type MentionOptions = {
9
9
  onSearch: (text: string) => string[];
10
10
  };
11
11
 
12
+ // TODO(burdon): Can only have a single autocompletion. Merge configuration with autocomplete.
12
13
  export const mention = ({ onSearch }: MentionOptions): Extension => {
13
14
  return autocompletion({
15
+ // TODO(burdon): Not working.
16
+ activateOnTyping: true,
17
+ // activateOnTypingDelay: 100,
18
+ // selectOnOpen: true,
14
19
  closeOnBlur: false, // For debugging.
20
+ // defaultKeymap: false,
21
+ icons: false,
15
22
  override: [
16
23
  (context: CompletionContext): CompletionResult | null => {
24
+ console.log(context);
25
+
17
26
  const match = context.matchBefore(/@(\w+)?/);
18
27
  if (!match || (match.from === match.to && !context.explicit)) {
19
28
  return null;
20
29
  }
21
30
 
22
- console.log(match);
23
-
24
31
  return {
25
32
  from: match.from,
26
33
  options: onSearch(match.text.slice(1).toLowerCase()).map((value) => ({ label: `@${value}` })),
@@ -0,0 +1,12 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Extension } from '@codemirror/state';
6
+
7
+ export type OutlinerOptions = {};
8
+
9
+ // TODO(burdon): Experimental component: multiple hierarchical draggable editor sections.
10
+ export const outliner = (options: OutlinerOptions = {}): Extension => {
11
+ return [];
12
+ };
@@ -0,0 +1,124 @@
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 Space } from '@dxos/react-client/echo';
10
+ import { type GossipMessage } from '@dxos/react-client/mesh';
11
+
12
+ import { type AwarenessInfo, type AwarenessPosition, type AwarenessProvider, type AwarenessState } from '../extensions';
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 AwarenessProviderParams = {
26
+ space: Space;
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 _space: Space;
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(params: AwarenessProviderParams) {
50
+ this._space = params.space;
51
+ this._channel = params.channel;
52
+ this._peerId = params.peerId;
53
+ this._info = params.info;
54
+ }
55
+
56
+ open() {
57
+ this._ctx = new Context();
58
+ this._postTask = new DeferredTask(this._ctx, async () => {
59
+ if (this._localState) {
60
+ await this._space.postMessage(this._channel, {
61
+ kind: 'post',
62
+ state: this._localState,
63
+ } satisfies ProtocolMessage);
64
+
65
+ await sleep(DEBOUNCE_INTERVAL);
66
+ }
67
+ });
68
+
69
+ this._ctx.onDispose(
70
+ this._space.listen(this._channel, (message: GossipMessage) => {
71
+ switch (message.payload.kind) {
72
+ case 'query': {
73
+ this._handleQueryMessage();
74
+ break;
75
+ }
76
+ case 'post': {
77
+ this._handlePostMessage(message.payload);
78
+ break;
79
+ }
80
+ }
81
+ }),
82
+ );
83
+
84
+ void this._space
85
+ .postMessage(this._channel, {
86
+ kind: 'query',
87
+ } satisfies ProtocolMessage)
88
+ .catch((err) => {
89
+ log.debug('failed to query awareness', { err });
90
+ });
91
+ }
92
+
93
+ close() {
94
+ void this._ctx?.dispose();
95
+ this._ctx = undefined;
96
+ this._postTask = undefined;
97
+ }
98
+
99
+ getRemoteStates(): AwarenessState[] {
100
+ return Array.from(this._remoteStates.values());
101
+ }
102
+
103
+ update(position: AwarenessPosition | undefined): void {
104
+ invariant(this._postTask);
105
+ this._localState = {
106
+ peerId: this._peerId,
107
+ position,
108
+ info: this._info,
109
+ };
110
+
111
+ this._postTask.schedule();
112
+ }
113
+
114
+ private _handleQueryMessage() {
115
+ invariant(this._postTask);
116
+ this._postTask.schedule();
117
+ }
118
+
119
+ private _handlePostMessage(message: ProtocolMessage) {
120
+ invariant(message.kind === 'post');
121
+ this._remoteStates.set(message.state.peerId, message.state);
122
+ this.remoteStateChange.emit();
123
+ }
124
+ }
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Extension, StateField } from '@codemirror/state';
6
+
7
+ import { type DocAccessor } from '@dxos/echo-schema';
8
+
9
+ // Runtime data structure.
10
+ export type Range = {
11
+ from: number;
12
+ to: number;
13
+ };
14
+
15
+ // Persistent data structure.
16
+ export type Comment = {
17
+ id: string;
18
+ cursor?: string;
19
+ };
20
+
21
+ export type EditorModel = {
22
+ id: string;
23
+ text: () => string;
24
+ extension?: Extension;
25
+
26
+ // TODO(burdon): Remove.
27
+ content: string | DocAccessor;
28
+
29
+ // TODO(burdon): Remove.
30
+ peer?: {
31
+ id: string;
32
+ name?: string;
33
+ };
34
+ };
35
+
36
+ /**
37
+ * State field makes the model available to other extensions.
38
+ */
39
+ export const modelState = StateField.define<EditorModel | undefined>({
40
+ create: () => undefined,
41
+ update: (model) => model,
42
+ });
@@ -2,5 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ export * from './awareness-provider';
6
+ export * from './defs';
7
+
8
+ export * from './useTextEditor';
5
9
  export * from './useTextModel';
6
- export * from './yjs';
@@ -0,0 +1,29 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type EditorView } from '@codemirror/view';
6
+ import { type RefCallback, useState } from 'react';
7
+
8
+ /**
9
+ * Hook for accessing the editor view.
10
+ *
11
+ * ```tsx
12
+ * const Test = () => {
13
+ * const [editorRef, editorView] = useTextEditor();
14
+ * useEffect(() => {
15
+ * editorView?.focus();
16
+ * }, [editorView]);
17
+ * return <TextEditor ref={editorRef} />;
18
+ * };
19
+ * ```
20
+ */
21
+ export const useTextEditor = (): [RefCallback<EditorView | null>, EditorView | null] => {
22
+ const [view, setView] = useState<EditorView | null>(null);
23
+ return [
24
+ (ref: EditorView | null) => {
25
+ setView(ref);
26
+ },
27
+ view,
28
+ ];
29
+ };