@dxos/react-ui-editor 0.3.11-main.68db74e → 0.3.11-main.6901f09

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 (188) hide show
  1. package/dist/lib/browser/index.mjs +2651 -1060
  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 +13 -15
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  9. package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
  11. package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
  12. package/dist/types/src/components/Toolbar/Toolbar.d.ts +29 -0
  13. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  14. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +25 -0
  15. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  16. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  17. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  18. package/dist/types/src/components/index.d.ts +1 -0
  19. package/dist/types/src/components/index.d.ts.map +1 -1
  20. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  21. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  22. package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
  23. package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
  24. package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
  25. package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
  26. package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
  27. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  28. package/dist/types/src/extensions/automerge/index.d.ts +0 -1
  29. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
  30. package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
  31. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  32. package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
  33. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  34. package/dist/types/src/extensions/awareness.d.ts +2 -2
  35. package/dist/types/src/extensions/awareness.d.ts.map +1 -1
  36. package/dist/types/src/extensions/basic.d.ts +4 -5
  37. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  38. package/dist/types/src/extensions/comments.d.ts +40 -19
  39. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  40. package/dist/types/src/extensions/cursor.d.ts +3 -3
  41. package/dist/types/src/extensions/cursor.d.ts.map +1 -1
  42. package/dist/types/src/extensions/index.d.ts +1 -7
  43. package/dist/types/src/extensions/index.d.ts.map +1 -1
  44. package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
  45. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  46. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  47. package/dist/types/src/extensions/markdown/formatting.d.ts +57 -0
  48. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
  49. package/dist/types/src/extensions/markdown/formatting.test.d.ts +3 -0
  50. package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -0
  51. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  52. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  53. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  54. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  55. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  56. package/dist/types/src/extensions/markdown/index.d.ts +8 -0
  57. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  58. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  59. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  60. package/dist/types/src/extensions/markdown/tasklist.d.ts +3 -0
  61. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  62. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  63. package/dist/types/src/extensions/outliner.d.ts +4 -0
  64. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  65. package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
  66. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
  67. package/dist/types/src/hooks/defs.d.ts +25 -0
  68. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  69. package/dist/types/src/hooks/index.d.ts +4 -2
  70. package/dist/types/src/hooks/index.d.ts.map +1 -1
  71. package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
  72. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
  73. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  74. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  75. package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
  76. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  77. package/dist/types/src/hooks/useTextModel.d.ts +14 -27
  78. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  79. package/dist/types/src/index.d.ts +1 -1
  80. package/dist/types/src/index.d.ts.map +1 -1
  81. package/dist/types/src/styles/markdown.d.ts +2 -2
  82. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  83. package/dist/types/src/themes/default.d.ts.map +1 -1
  84. package/dist/types/src/util.d.ts +2 -0
  85. package/dist/types/src/util.d.ts.map +1 -1
  86. package/package.json +47 -37
  87. package/src/components/TextEditor/MarkdownEditor.stories.tsx +75 -84
  88. package/src/components/TextEditor/TextEditor.tsx +111 -75
  89. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  90. package/src/components/TextEditor/hooks.stories.tsx +111 -0
  91. package/src/components/Toolbar/Toolbar.stories.tsx +102 -0
  92. package/src/components/Toolbar/Toolbar.tsx +245 -0
  93. package/src/components/Toolbar/index.ts +5 -0
  94. package/src/components/index.ts +1 -0
  95. package/src/extensions/autocomplete.ts +6 -3
  96. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  97. package/src/extensions/automerge/automerge.test.ts +13 -0
  98. package/src/extensions/automerge/automerge.ts +54 -45
  99. package/src/extensions/automerge/cursor.ts +3 -3
  100. package/src/extensions/automerge/defs.ts +13 -34
  101. package/src/extensions/automerge/index.ts +0 -1
  102. package/src/extensions/automerge/semaphore.ts +33 -32
  103. package/src/extensions/automerge/update-automerge.ts +14 -8
  104. package/src/extensions/automerge/update-codemirror.ts +5 -5
  105. package/src/extensions/awareness.ts +22 -18
  106. package/src/extensions/basic.ts +17 -10
  107. package/src/extensions/blast.ts +4 -1
  108. package/src/extensions/comments.ts +328 -167
  109. package/src/extensions/cursor.ts +7 -3
  110. package/src/extensions/index.ts +1 -7
  111. package/src/extensions/markdown/bundle.ts +23 -28
  112. package/src/extensions/markdown/code.ts +150 -0
  113. package/src/extensions/markdown/formatting.test.ts +481 -0
  114. package/src/extensions/markdown/formatting.ts +1198 -0
  115. package/src/extensions/markdown/heading.ts +59 -0
  116. package/src/extensions/markdown/highlight.ts +9 -15
  117. package/src/extensions/markdown/hr.ts +71 -0
  118. package/src/extensions/markdown/index.ts +8 -0
  119. package/src/extensions/{link.ts → markdown/link.ts} +16 -7
  120. package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -33
  121. package/src/extensions/mention.ts +9 -2
  122. package/src/extensions/outliner.ts +12 -0
  123. package/src/hooks/awareness-provider.ts +24 -20
  124. package/src/hooks/defs.ts +42 -0
  125. package/src/hooks/index.ts +5 -2
  126. package/src/hooks/useActionHandler.ts +93 -0
  127. package/src/hooks/useEditorView.ts +29 -0
  128. package/src/hooks/useTextEditor.ts +153 -0
  129. package/src/hooks/useTextModel.ts +66 -56
  130. package/src/index.ts +1 -1
  131. package/src/styles/markdown.ts +9 -10
  132. package/src/themes/default.ts +11 -8
  133. package/src/util.ts +26 -3
  134. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  135. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  136. package/dist/types/src/components/TextEditor/comments.stories.d.ts +0 -26
  137. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
  138. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  139. package/dist/types/src/extensions/code.d.ts.map +0 -1
  140. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  141. package/dist/types/src/extensions/image.d.ts.map +0 -1
  142. package/dist/types/src/extensions/link.d.ts.map +0 -1
  143. package/dist/types/src/extensions/table.d.ts.map +0 -1
  144. package/dist/types/src/extensions/tasklist.d.ts +0 -7
  145. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  146. package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
  147. package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
  148. package/dist/types/src/extensions/yjs/index.d.ts +0 -2
  149. package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
  150. package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
  151. package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
  152. package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
  153. package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
  154. package/dist/types/src/hooks/automerge.d.ts +0 -3
  155. package/dist/types/src/hooks/automerge.d.ts.map +0 -1
  156. package/dist/types/src/hooks/yjs.d.ts +0 -27
  157. package/dist/types/src/hooks/yjs.d.ts.map +0 -1
  158. package/dist/types/src/testing/index.d.ts +0 -3
  159. package/dist/types/src/testing/index.d.ts.map +0 -1
  160. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  161. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  162. package/dist/types/src/testing/proto/index.d.ts +0 -2
  163. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  164. package/dist/types/src/testing/replicator.d.ts +0 -45
  165. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  166. package/src/components/TextEditor/codemirror.stories.ts +0 -115
  167. package/src/components/TextEditor/comments.stories.tsx +0 -315
  168. package/src/components/TextEditor/yjs.stories.tsx +0 -56
  169. package/src/extensions/code.ts +0 -106
  170. package/src/extensions/hr.ts +0 -73
  171. package/src/extensions/yjs/cursor.ts +0 -22
  172. package/src/extensions/yjs/index.ts +0 -5
  173. package/src/extensions/yjs/yjs.test.ts +0 -35
  174. package/src/extensions/yjs/yjs.ts +0 -16
  175. package/src/hooks/automerge.ts +0 -52
  176. package/src/hooks/yjs.ts +0 -156
  177. package/src/testing/index.ts +0 -6
  178. package/src/testing/proto/gen/schema.ts +0 -49
  179. package/src/testing/proto/index.ts +0 -5
  180. package/src/testing/proto/schema.proto +0 -15
  181. package/src/testing/replicator.ts +0 -184
  182. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  183. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  184. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  185. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  186. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  187. /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
  188. /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
@@ -0,0 +1,59 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, type EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';
8
+
9
+ const buildDecorations = (view: EditorView): DecorationSet => {
10
+ const builder = new RangeSetBuilder<Decoration>();
11
+ const { state } = view;
12
+ const cursor = state.selection.main.head;
13
+
14
+ for (const { from, to } of view.visibleRanges) {
15
+ syntaxTree(state).iterate({
16
+ enter: (node) => {
17
+ switch (node.name) {
18
+ case 'ATXHeading1':
19
+ case 'ATXHeading2':
20
+ case 'ATXHeading3':
21
+ case 'ATXHeading4':
22
+ case 'ATXHeading5':
23
+ case 'ATXHeading6': {
24
+ const mark = node.node.getChild('HeaderMark');
25
+ if (mark) {
26
+ if (!view.hasFocus || view.state.readOnly || cursor < node.from || cursor > node.to) {
27
+ builder.add(mark.from, mark.to + 1, Decoration.replace({}));
28
+ }
29
+ }
30
+ }
31
+ }
32
+ },
33
+ from,
34
+ to,
35
+ });
36
+ }
37
+
38
+ return builder.finish();
39
+ };
40
+
41
+ export const heading = () => {
42
+ return [
43
+ ViewPlugin.fromClass(
44
+ class {
45
+ decorations: DecorationSet;
46
+ constructor(view: EditorView) {
47
+ this.decorations = buildDecorations(view);
48
+ }
49
+
50
+ update(update: ViewUpdate) {
51
+ this.decorations = buildDecorations(update.view);
52
+ }
53
+ },
54
+ {
55
+ decorations: (value) => value.decorations,
56
+ },
57
+ ),
58
+ ];
59
+ };
@@ -128,13 +128,13 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
128
128
  // Markdown marks.
129
129
  {
130
130
  tag: [markdownTags.CodeMark, markdownTags.HeaderMark, markdownTags.QuoteMark, markdownTags.EmphasisMark],
131
- class: readonly ? 'hidden' : mark,
131
+ class: mark,
132
132
  },
133
133
 
134
134
  // E.g., code block language (after ```).
135
135
  {
136
136
  tag: [tags.function(tags.variableName), tags.labelName],
137
- class: readonly ? 'hidden' : codeMark,
137
+ class: codeMark,
138
138
  },
139
139
 
140
140
  {
@@ -143,12 +143,12 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
143
143
  },
144
144
 
145
145
  // Headings.
146
- { tag: tags.heading1, class: heading(1, readonly) },
147
- { tag: tags.heading2, class: heading(2, readonly) },
148
- { tag: tags.heading3, class: heading(3, readonly) },
149
- { tag: tags.heading4, class: heading(4, readonly) },
150
- { tag: tags.heading5, class: heading(5, readonly) },
151
- { tag: tags.heading6, class: heading(6, readonly) },
146
+ { tag: tags.heading1, class: heading(1) },
147
+ { tag: tags.heading2, class: heading(2) },
148
+ { tag: tags.heading3, class: heading(3) },
149
+ { tag: tags.heading4, class: heading(4) },
150
+ { tag: tags.heading5, class: heading(5) },
151
+ { tag: tags.heading6, class: heading(6) },
152
152
 
153
153
  // Emphasis.
154
154
  { tag: tags.emphasis, class: italic },
@@ -165,7 +165,7 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
165
165
  // However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
166
166
  // when a language is specified. In this case, the syntax highlighting extensions will colorize
167
167
  // the code, but all other CSS properties will be inherited.
168
- // IMPORTANT: Therefore, the fenced code block will use the base editor font.
168
+ // IMPORTANT: Therefore, the fenced code block will use the base editor font unless changed by an extension.
169
169
  {
170
170
  tag: [markdownTags.CodeText, markdownTags.InlineCode],
171
171
  class: code,
@@ -180,12 +180,6 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
180
180
  tag: [markdownTags.TableCell],
181
181
  class: 'font-mono',
182
182
  },
183
-
184
- // Main content, paragraphs, etc.
185
- // {
186
- // tag: [tags.content],
187
- // fontFamily: getToken('fontFamily.body', []).join(','),
188
- // },
189
183
  ],
190
184
  {
191
185
  scope: markdownLanguage,
@@ -0,0 +1,71 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
7
+ import { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate, WidgetType } from '@codemirror/view';
8
+
9
+ import { getToken } from '../../styles';
10
+
11
+ // TODO(burdon): Reconcile with theme.
12
+ const styles = EditorView.baseTheme({
13
+ '& .cm-hr': {
14
+ // Note that block-level decorations should not have vertical margins,
15
+ borderTop: `1px solid ${getToken('extend.colors.neutral.200')}`,
16
+ },
17
+ });
18
+
19
+ class HorizontalRuleWidget extends WidgetType {
20
+ override toDOM() {
21
+ const el = document.createElement('div');
22
+ el.className = 'cm-hr';
23
+ return el;
24
+ }
25
+ }
26
+
27
+ const decoration = Decoration.replace({ widget: new HorizontalRuleWidget() });
28
+
29
+ const buildDecorations = (view: EditorView): DecorationSet => {
30
+ const builder = new RangeSetBuilder<Decoration>();
31
+ const { state } = view;
32
+ const cursor = state.selection.main.head;
33
+
34
+ for (const { from, to } of view.visibleRanges) {
35
+ syntaxTree(state).iterate({
36
+ enter: (node) => {
37
+ if (node.name === 'HorizontalRule') {
38
+ // Check if cursor is inside text.
39
+ if (cursor <= node.from || cursor >= node.to) {
40
+ builder.add(node.from, node.to, decoration);
41
+ }
42
+ }
43
+ },
44
+ from,
45
+ to,
46
+ });
47
+ }
48
+
49
+ return builder.finish();
50
+ };
51
+
52
+ export const hr = () => {
53
+ return [
54
+ ViewPlugin.fromClass(
55
+ class {
56
+ decorations: DecorationSet;
57
+ constructor(view: EditorView) {
58
+ this.decorations = buildDecorations(view);
59
+ }
60
+
61
+ update(update: ViewUpdate) {
62
+ this.decorations = buildDecorations(update.view);
63
+ }
64
+ },
65
+ {
66
+ decorations: (value) => value.decorations,
67
+ },
68
+ ),
69
+ styles,
70
+ ];
71
+ };
@@ -3,4 +3,12 @@
3
3
  //
4
4
 
5
5
  export * from './bundle';
6
+ export * from './code';
7
+ export * from './formatting';
8
+ export * from './heading';
6
9
  export * from './highlight';
10
+ export * from './hr';
11
+ export * from './image';
12
+ export * from './link';
13
+ export * from './table';
14
+ export * from './tasklist';
@@ -6,13 +6,13 @@
6
6
  import { syntaxTree } from '@codemirror/language';
7
7
  import { type Extension, RangeSetBuilder } from '@codemirror/state';
8
8
  import {
9
+ hoverTooltip,
9
10
  Decoration,
11
+ type DecorationSet,
10
12
  type EditorView,
11
- WidgetType,
12
- hoverTooltip,
13
13
  ViewPlugin,
14
14
  type ViewUpdate,
15
- type DecorationSet,
15
+ WidgetType,
16
16
  } from '@codemirror/view';
17
17
  import { type SyntaxNode } from '@lezer/common';
18
18
 
@@ -22,6 +22,7 @@ import { tooltipContent } from '@dxos/react-ui-theme';
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
24
24
 
25
+ // TODO(burdon): Callback to dynamically lookup title of linked pages.
25
26
  export type LinkOptions = {
26
27
  link?: boolean;
27
28
  onHover?: (el: Element, url: string) => void;
@@ -37,7 +38,6 @@ export const link = (options: LinkOptions = {}): Extension => {
37
38
  ViewPlugin.fromClass(
38
39
  class {
39
40
  decorations: DecorationSet;
40
-
41
41
  constructor(view: EditorView) {
42
42
  this.decorations = buildDecorations(view, options);
43
43
  }
@@ -49,7 +49,7 @@ export const link = (options: LinkOptions = {}): Extension => {
49
49
  }
50
50
  },
51
51
  {
52
- decorations: (v) => v.decorations,
52
+ decorations: (value) => value.decorations,
53
53
  },
54
54
  ),
55
55
  ];
@@ -65,10 +65,12 @@ export const link = (options: LinkOptions = {}): Extension => {
65
65
  for (let i = 0, node: SyntaxNode | null = syntax; !link && node && i < 5; node = node.parent, i++) {
66
66
  link = node.name === 'Link' ? node : null;
67
67
  }
68
+
68
69
  const url = link && link.getChild('URL');
69
70
  if (!url || !link) {
70
71
  return null;
71
72
  }
73
+
72
74
  const urlText = view.state.sliceDoc(url.from, url.to);
73
75
  return {
74
76
  pos: link.from,
@@ -89,7 +91,10 @@ export const link = (options: LinkOptions = {}): Extension => {
89
91
  };
90
92
 
91
93
  class LinkButton extends WidgetType {
92
- constructor(private readonly _url: string, private readonly _onAttach: LinkOptions['onRender']) {
94
+ constructor(
95
+ private readonly _url: string,
96
+ private readonly _onAttach: LinkOptions['onRender'],
97
+ ) {
93
98
  super();
94
99
  }
95
100
 
@@ -105,7 +110,10 @@ class LinkButton extends WidgetType {
105
110
  }
106
111
 
107
112
  class LinkText extends WidgetType {
108
- constructor(private readonly _text: string, private readonly _url?: string) {
113
+ constructor(
114
+ private readonly _text: string,
115
+ private readonly _url?: string,
116
+ ) {
109
117
  super();
110
118
  }
111
119
 
@@ -158,6 +166,7 @@ const buildDecorations = (view: EditorView, options: LinkOptions): DecorationSet
158
166
  return false;
159
167
  }
160
168
 
169
+ // TODO(burdon): Don't expand if moving cursor up/down.
161
170
  if (!view.hasFocus || state.readOnly || cursor < node.from || cursor > node.to) {
162
171
  builder.add(
163
172
  node.from,
@@ -6,6 +6,19 @@ import { syntaxTree } from '@codemirror/language';
6
6
  import { RangeSetBuilder } from '@codemirror/state';
7
7
  import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
8
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
+
9
22
  class CheckboxWidget extends WidgetType {
10
23
  constructor(private _checked: boolean) {
11
24
  super();
@@ -17,6 +30,7 @@ class CheckboxWidget extends WidgetType {
17
30
 
18
31
  override toDOM(view: EditorView) {
19
32
  const input = document.createElement('input');
33
+ input.className = 'cm-task-checkbox';
20
34
  input.type = 'checkbox';
21
35
  input.checked = this._checked;
22
36
  if (view.state.readOnly) {
@@ -42,35 +56,8 @@ class CheckboxWidget extends WidgetType {
42
56
  }
43
57
  }
44
58
 
45
- const checkedDeco = Decoration.replace({ widget: new CheckboxWidget(true) });
46
- const uncheckedDeco = Decoration.replace({ widget: new CheckboxWidget(false) });
47
-
48
- export type TasklistOptions = {};
49
-
50
- export const tasklist = (options: TasklistOptions = {}) => {
51
- return ViewPlugin.fromClass(
52
- class {
53
- decorations: DecorationSet;
54
-
55
- constructor(view: EditorView) {
56
- this.decorations = buildDecorations(view);
57
- }
58
-
59
- update(update: ViewUpdate) {
60
- if (update.docChanged || update.viewportChanged || update.selectionSet) {
61
- this.decorations = buildDecorations(update.view);
62
- }
63
- }
64
- },
65
- {
66
- decorations: (v) => v.decorations,
67
- provide: (plugin) =>
68
- EditorView.atomicRanges.of((view) => {
69
- return view.plugin(plugin)?.decorations || Decoration.none;
70
- }),
71
- },
72
- );
73
- };
59
+ const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
60
+ const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
74
61
 
75
62
  const buildDecorations = (view: EditorView): DecorationSet => {
76
63
  const builder = new RangeSetBuilder<Decoration>();
@@ -80,10 +67,13 @@ const buildDecorations = (view: EditorView): DecorationSet => {
80
67
  for (const { from, to } of view.visibleRanges) {
81
68
  syntaxTree(state).iterate({
82
69
  enter: (node) => {
83
- // Check if cursor is inside text.
84
- if (node.name === 'TaskMarker' && (cursor < node.from || cursor > node.to)) {
85
- const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
86
- builder.add(node.from, node.to, checked ? checkedDeco : uncheckedDeco);
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
+ }
87
77
  }
88
78
  },
89
79
  from,
@@ -93,3 +83,28 @@ const buildDecorations = (view: EditorView): DecorationSet => {
93
83
 
94
84
  return builder.finish();
95
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
+ };
@@ -29,9 +29,10 @@ export type AwarenessProviderParams = {
29
29
  info: AwarenessInfo;
30
30
  };
31
31
 
32
+ /**
33
+ * Receives and broadcasts profile and cursor position.
34
+ */
32
35
  export class SpaceAwarenessProvider implements AwarenessProvider {
33
- public readonly remoteStateChange = new Event<void>();
34
-
35
36
  private readonly _remoteStates = new Map<string, AwarenessState>();
36
37
 
37
38
  private readonly _space: Space;
@@ -39,9 +40,11 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
39
40
  private readonly _peerId: string;
40
41
  private readonly _info: AwarenessInfo;
41
42
 
42
- private _localState?: AwarenessState;
43
- private _postTask?: DeferredTask;
44
43
  private _ctx?: Context;
44
+ private _postTask?: DeferredTask;
45
+ private _localState?: AwarenessState;
46
+
47
+ public readonly remoteStateChange = new Event<void>();
45
48
 
46
49
  constructor(params: AwarenessProviderParams) {
47
50
  this._space = params.space;
@@ -63,19 +66,20 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
63
66
  }
64
67
  });
65
68
 
66
- const unsubscribe = this._space.listen(this._channel, (message: GossipMessage) => {
67
- switch (message.payload.kind) {
68
- case 'query': {
69
- this._handleQueryMessage();
70
- break;
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
+ }
71
80
  }
72
- case 'post': {
73
- this._handlePostMessage(message.payload);
74
- break;
75
- }
76
- }
77
- });
78
- this._ctx.onDispose(unsubscribe);
81
+ }),
82
+ );
79
83
 
80
84
  void this._space
81
85
  .postMessage(this._channel, {
@@ -92,6 +96,10 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
92
96
  this._postTask = undefined;
93
97
  }
94
98
 
99
+ getRemoteStates(): AwarenessState[] {
100
+ return Array.from(this._remoteStates.values());
101
+ }
102
+
95
103
  update(position: AwarenessPosition | undefined): void {
96
104
  invariant(this._postTask);
97
105
  this._localState = {
@@ -103,10 +111,6 @@ export class SpaceAwarenessProvider implements AwarenessProvider {
103
111
  this._postTask.schedule();
104
112
  }
105
113
 
106
- getRemoteStates(): AwarenessState[] {
107
- return Array.from(this._remoteStates.values());
108
- }
109
-
110
114
  private _handleQueryMessage() {
111
115
  invariant(this._postTask);
112
116
  this._postTask.schedule();
@@ -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,7 +2,10 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './automerge';
6
5
  export * from './awareness-provider';
6
+ export * from './defs';
7
+
8
+ export * from './useActionHandler';
9
+ export * from './useEditorView';
10
+ export * from './useTextEditor';
7
11
  export * from './useTextModel';
8
- export * from './yjs';
@@ -0,0 +1,93 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import type { EditorView } from '@codemirror/view';
6
+
7
+ import type { ToolbarProps } from '../components';
8
+ import {
9
+ createComment,
10
+ insertTable,
11
+ setHeading,
12
+ setStyle,
13
+ toggleStyle,
14
+ addLink,
15
+ removeLink,
16
+ Inline,
17
+ List,
18
+ addList,
19
+ removeList,
20
+ toggleList,
21
+ addBlockquote,
22
+ removeBlockquote,
23
+ addCodeblock,
24
+ removeCodeblock,
25
+ toggleBlockquote,
26
+ } from '../extensions';
27
+
28
+ export const useActionHandler = (view?: EditorView | null): ToolbarProps['onAction'] => {
29
+ return (action) => {
30
+ if (!view) {
31
+ return;
32
+ }
33
+
34
+ let inlineType, listType;
35
+ switch (action.type) {
36
+ case 'heading':
37
+ setHeading(parseInt(action.data))(view);
38
+ break;
39
+
40
+ case 'strong':
41
+ case 'emphasis':
42
+ case 'strikethrough':
43
+ case 'code':
44
+ inlineType =
45
+ action.type === 'strong'
46
+ ? Inline.Strong
47
+ : action.type === 'emphasis'
48
+ ? Inline.Emphasis
49
+ : action.type === 'strikethrough'
50
+ ? Inline.Strikethrough
51
+ : Inline.Code;
52
+ (typeof action.data === 'boolean' ? setStyle(inlineType, action.data) : toggleStyle(inlineType))(view);
53
+ break;
54
+
55
+ case 'list-ordered':
56
+ case 'list-bullet':
57
+ case 'list-tasks':
58
+ listType =
59
+ action.type === 'list-ordered' ? List.Ordered : action.type === 'list-bullet' ? List.Bullet : List.Task;
60
+ (action.data === false
61
+ ? removeList(listType)
62
+ : action.data === true
63
+ ? addList(listType)
64
+ : toggleList(listType))(view);
65
+ break;
66
+
67
+ case 'blockquote':
68
+ (action.data === false ? removeBlockquote : action.data === true ? addBlockquote : toggleBlockquote)(view);
69
+ break;
70
+ case 'codeblock':
71
+ (action.data === false ? removeCodeblock : addCodeblock)(view);
72
+ break;
73
+ case 'table':
74
+ insertTable(view);
75
+ break;
76
+
77
+ case 'link':
78
+ (action.data === false ? removeLink : addLink)(view);
79
+ break;
80
+
81
+ case 'comment':
82
+ createComment(view);
83
+ break;
84
+ }
85
+
86
+ // TODO(burdon): Hack otherwise remains on heading selector.
87
+ setTimeout(() => {
88
+ if (!view.hasFocus) {
89
+ view.focus();
90
+ }
91
+ });
92
+ };
93
+ };