@dxos/react-ui-editor 0.3.11-main.baa8c4d → 0.3.11-main.bc6b68b

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 (221) hide show
  1. package/dist/lib/browser/index.mjs +1882 -1531
  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 +14 -14
  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/components/TextEditor/index.d.ts +0 -2
  15. package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
  16. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -0
  17. package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
  18. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
  19. package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
  20. package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
  21. package/dist/types/src/extensions/automerge/defs.d.ts +24 -0
  22. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
  23. package/dist/types/src/extensions/automerge/index.d.ts +3 -0
  24. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
  25. package/dist/types/src/extensions/automerge/semaphore.d.ts +14 -0
  26. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
  27. package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
  28. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
  29. package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
  30. package/dist/types/src/extensions/awareness.d.ts +46 -0
  31. package/dist/types/src/extensions/awareness.d.ts.map +1 -0
  32. package/dist/types/src/extensions/basic.d.ts +8 -0
  33. package/dist/types/src/extensions/basic.d.ts.map +1 -0
  34. package/dist/types/src/extensions/blast.d.ts.map +1 -0
  35. package/dist/types/src/extensions/code.d.ts.map +1 -0
  36. package/dist/types/src/{components/TextEditor/extensions → extensions}/comments.d.ts +33 -18
  37. package/dist/types/src/extensions/comments.d.ts.map +1 -0
  38. package/dist/types/src/extensions/cursor.d.ts +25 -0
  39. package/dist/types/src/extensions/cursor.d.ts.map +1 -0
  40. package/dist/types/src/extensions/hr.d.ts.map +1 -0
  41. package/dist/types/src/extensions/image.d.ts.map +1 -0
  42. package/dist/types/src/{components/TextEditor/extensions → extensions}/index.d.ts +4 -2
  43. package/dist/types/src/extensions/index.d.ts.map +1 -0
  44. package/dist/types/src/{components/TextEditor/extensions → extensions}/link.d.ts +1 -0
  45. package/dist/types/src/extensions/link.d.ts.map +1 -0
  46. package/dist/types/src/extensions/listener.d.ts +10 -0
  47. package/dist/types/src/extensions/listener.d.ts.map +1 -0
  48. package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/bundle.d.ts +2 -5
  49. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
  50. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
  51. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
  52. package/dist/types/src/extensions/mention.d.ts.map +1 -0
  53. package/dist/types/src/extensions/outliner.d.ts +4 -0
  54. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  55. package/dist/types/src/extensions/table.d.ts.map +1 -0
  56. package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
  57. package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
  58. package/dist/types/src/hooks/awareness-provider.d.ts +31 -0
  59. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
  60. package/dist/types/src/hooks/defs.d.ts +25 -0
  61. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  62. package/dist/types/src/hooks/index.d.ts +3 -1
  63. package/dist/types/src/hooks/index.d.ts.map +1 -1
  64. package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
  65. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  66. package/dist/types/src/hooks/useTextModel.d.ts +2 -31
  67. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  68. package/dist/types/src/index.d.ts +5 -0
  69. package/dist/types/src/index.d.ts.map +1 -1
  70. package/dist/types/src/styles/cursors.d.ts.map +1 -0
  71. package/dist/types/src/styles/index.d.ts +1 -0
  72. package/dist/types/src/styles/index.d.ts.map +1 -1
  73. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  74. package/dist/types/src/styles/tokens.d.ts +1 -0
  75. package/dist/types/src/styles/tokens.d.ts.map +1 -1
  76. package/dist/types/src/{components/TextEditor/themes → themes}/default.d.ts +1 -1
  77. package/dist/types/src/themes/default.d.ts.map +1 -0
  78. package/dist/types/src/themes/index.d.ts.map +1 -0
  79. package/dist/types/src/util.d.ts +7 -0
  80. package/dist/types/src/util.d.ts.map +1 -0
  81. package/package.json +33 -34
  82. package/src/components/TextEditor/MarkdownEditor.stories.tsx +86 -91
  83. package/src/components/TextEditor/TextEditor.stories.tsx +13 -34
  84. package/src/components/TextEditor/TextEditor.tsx +124 -95
  85. package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
  86. package/src/components/TextEditor/comments.stories.tsx +357 -0
  87. package/src/components/TextEditor/index.ts +0 -3
  88. package/src/{components/TextEditor/extensions → extensions}/autocomplete.ts +23 -19
  89. package/src/extensions/automerge/automerge.ts +88 -0
  90. package/src/extensions/automerge/cursor.ts +52 -0
  91. package/src/extensions/automerge/defs.ts +41 -0
  92. package/src/extensions/automerge/index.ts +6 -0
  93. package/src/{hooks → extensions}/automerge/semaphore.ts +34 -29
  94. package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
  95. package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
  96. package/src/extensions/awareness.ts +310 -0
  97. package/src/extensions/basic.ts +37 -0
  98. package/src/{components/TextEditor/extensions → extensions}/code.ts +28 -23
  99. package/src/extensions/comments.ts +428 -0
  100. package/src/extensions/cursor.ts +50 -0
  101. package/src/extensions/hr.ts +72 -0
  102. package/src/extensions/image.ts +87 -0
  103. package/src/{components/TextEditor/extensions → extensions}/index.ts +4 -2
  104. package/src/{components/TextEditor/extensions → extensions}/link.ts +116 -62
  105. package/src/extensions/listener.ts +35 -0
  106. package/src/{components/TextEditor/extensions → extensions}/markdown/bundle.ts +18 -24
  107. package/src/{components/TextEditor/extensions → extensions}/markdown/highlight.ts +4 -13
  108. package/src/{components/TextEditor/extensions → extensions}/mention.ts +9 -2
  109. package/src/extensions/outliner.ts +12 -0
  110. package/src/{components/TextEditor/extensions → extensions}/table.ts +10 -4
  111. package/src/extensions/tasklist.ts +111 -0
  112. package/src/{components/TextEditor/extensions → extensions}/typewriter.ts +1 -1
  113. package/src/hooks/awareness-provider.ts +124 -0
  114. package/src/hooks/defs.ts +42 -0
  115. package/src/hooks/index.ts +4 -1
  116. package/src/hooks/useTextEditor.ts +29 -0
  117. package/src/hooks/useTextModel.ts +30 -115
  118. package/src/index.ts +5 -0
  119. package/src/styles/index.ts +1 -0
  120. package/src/styles/markdown.ts +0 -2
  121. package/src/styles/tokens.ts +3 -0
  122. package/src/{components/TextEditor/themes → themes}/default.ts +52 -79
  123. package/src/util.ts +41 -0
  124. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  125. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  126. package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +0 -1
  127. package/dist/types/src/components/TextEditor/extensions/basic.d.ts +0 -9
  128. package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +0 -1
  129. package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +0 -1
  130. package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +0 -1
  131. package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +0 -1
  132. package/dist/types/src/components/TextEditor/extensions/hr.d.ts.map +0 -1
  133. package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +0 -1
  134. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
  135. package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +0 -1
  136. package/dist/types/src/components/TextEditor/extensions/listener.d.ts +0 -9
  137. package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +0 -1
  138. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
  139. package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +0 -1
  140. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
  141. package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +0 -1
  142. package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts +0 -3
  143. package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts.map +0 -1
  144. package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +0 -1
  145. package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +0 -1
  146. package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts +0 -12
  147. package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +0 -1
  148. package/dist/types/src/components/TextEditor/extensions/typewriter.d.ts.map +0 -1
  149. package/dist/types/src/components/TextEditor/extensions/util.d.ts +0 -5
  150. package/dist/types/src/components/TextEditor/extensions/util.d.ts.map +0 -1
  151. package/dist/types/src/components/TextEditor/themes/default.d.ts.map +0 -1
  152. package/dist/types/src/components/TextEditor/themes/index.d.ts.map +0 -1
  153. package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
  154. package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
  155. package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
  156. package/dist/types/src/hooks/automerge/index.d.ts +0 -3
  157. package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
  158. package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
  159. package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
  160. package/dist/types/src/hooks/automerge/semaphore.d.ts +0 -19
  161. package/dist/types/src/hooks/automerge/semaphore.d.ts.map +0 -1
  162. package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
  163. package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
  164. package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +0 -1
  165. package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
  166. package/dist/types/src/hooks/yjs/index.d.ts +0 -3
  167. package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
  168. package/dist/types/src/hooks/yjs/space-provider.d.ts +0 -27
  169. package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
  170. package/dist/types/src/hooks/yjs/yjs.stories.d.ts +0 -12
  171. package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
  172. package/dist/types/src/hooks/yjs/yjs.test.d.ts +0 -2
  173. package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
  174. package/dist/types/src/testing/index.d.ts +0 -3
  175. package/dist/types/src/testing/index.d.ts.map +0 -1
  176. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  177. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  178. package/dist/types/src/testing/proto/index.d.ts +0 -2
  179. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  180. package/dist/types/src/testing/replicator.d.ts +0 -45
  181. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  182. package/src/components/TextEditor/codemirror.stories.ts +0 -114
  183. package/src/components/TextEditor/extensions/basic.ts +0 -32
  184. package/src/components/TextEditor/extensions/comments.ts +0 -388
  185. package/src/components/TextEditor/extensions/hr.ts +0 -74
  186. package/src/components/TextEditor/extensions/image.ts +0 -69
  187. package/src/components/TextEditor/extensions/listener.ts +0 -23
  188. package/src/components/TextEditor/extensions/mermaid.ts +0 -11
  189. package/src/components/TextEditor/extensions/tasklist.ts +0 -121
  190. package/src/components/TextEditor/extensions/tooltip.ts +0 -61
  191. package/src/components/TextEditor/extensions/util.ts +0 -18
  192. package/src/hooks/automerge/handle.ts +0 -14
  193. package/src/hooks/automerge/index.ts +0 -6
  194. package/src/hooks/automerge/plugin.ts +0 -134
  195. package/src/hooks/yjs/index.ts +0 -6
  196. package/src/hooks/yjs/space-provider.ts +0 -126
  197. package/src/hooks/yjs/yjs.stories.tsx +0 -56
  198. package/src/hooks/yjs/yjs.test.ts +0 -59
  199. package/src/testing/index.ts +0 -6
  200. package/src/testing/proto/gen/schema.ts +0 -49
  201. package/src/testing/proto/index.ts +0 -5
  202. package/src/testing/proto/schema.proto +0 -15
  203. package/src/testing/replicator.ts +0 -189
  204. /package/dist/types/src/{components/TextEditor/extensions → extensions}/autocomplete.d.ts +0 -0
  205. /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
  206. /package/dist/types/src/{components/TextEditor/extensions → extensions}/blast.d.ts +0 -0
  207. /package/dist/types/src/{components/TextEditor/extensions → extensions}/code.d.ts +0 -0
  208. /package/dist/types/src/{components/TextEditor/extensions → extensions}/hr.d.ts +0 -0
  209. /package/dist/types/src/{components/TextEditor/extensions → extensions}/image.d.ts +0 -0
  210. /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/highlight.d.ts +0 -0
  211. /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/index.d.ts +0 -0
  212. /package/dist/types/src/{components/TextEditor/extensions → extensions}/mention.d.ts +0 -0
  213. /package/dist/types/src/{components/TextEditor/extensions → extensions}/table.d.ts +0 -0
  214. /package/dist/types/src/{components/TextEditor/extensions → extensions}/tasklist.d.ts +0 -0
  215. /package/dist/types/src/{components/TextEditor/extensions → extensions}/typewriter.d.ts +0 -0
  216. /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
  217. /package/dist/types/src/{components/TextEditor/themes → themes}/index.d.ts +0 -0
  218. /package/src/{components/TextEditor/extensions → extensions}/blast.ts +0 -0
  219. /package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +0 -0
  220. /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
  221. /package/src/{components/TextEditor/themes → themes}/index.ts +0 -0
@@ -5,774 +5,953 @@ import { Doc, YText, YXmlFragment } from "@dxos/text-model";
5
5
  // packages/ui/react-ui-editor/src/components/TextEditor/index.ts
6
6
  import { tags as tags2 } from "@lezer/highlight";
7
7
 
8
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/autocomplete.ts
8
+ // packages/ui/react-ui-editor/src/components/TextEditor/TextEditor.tsx
9
+ import { EditorState as EditorState2 } from "@codemirror/state";
10
+ import { EditorView as EditorView13 } from "@codemirror/view";
11
+ import { useFocusableGroup } from "@fluentui/react-tabster";
12
+ import { vim } from "@replit/codemirror-vim";
13
+ import defaultsDeep2 from "lodash.defaultsdeep";
14
+ import React, { forwardRef, useCallback, useEffect as useEffect2, useImperativeHandle, useRef, useState } from "react";
15
+ import { log as log2 } from "@dxos/log";
16
+ import { useThemeContext } from "@dxos/react-ui";
17
+ import { attentionSurface, mx as mx3 } from "@dxos/react-ui-theme";
18
+
19
+ // packages/ui/react-ui-editor/src/extensions/autocomplete.ts
9
20
  import { autocompletion, completionKeymap } from "@codemirror/autocomplete";
21
+ import { markdownLanguage } from "@codemirror/lang-markdown";
10
22
  import { keymap } from "@codemirror/view";
11
23
  var autocomplete = ({ onSearch }) => {
12
24
  return [
13
25
  // https://codemirror.net/docs/ref/#view.keymap
14
26
  // https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
27
+ // TODO(burdon): Set custom keymap.
15
28
  keymap.of(completionKeymap),
16
29
  // https://codemirror.net/examples/autocompletion
17
30
  // https://codemirror.net/docs/ref/#autocomplete.autocompletion
18
31
  autocompletion({
19
- // TODO(burdon): Set custom keymap.
32
+ activateOnTyping: true,
33
+ // closeOnBlur: false,
20
34
  // defaultKeymap: false,
21
- // Don't start unless key press.
22
- activateOnTyping: false,
23
- // TODO(burdon): Option to create new page?
24
- // TODO(burdon): Optional decoration via addToOptions
25
- override: [
26
- (context) => {
27
- const match = context.matchBefore(/\w*/);
28
- if (!match || match.from === match.to && !context.explicit) {
29
- return null;
30
- }
31
- return {
32
- from: match.from,
33
- options: onSearch(match.text.toLowerCase())
34
- };
35
+ // TODO(burdon): Styles/fragments.
36
+ tooltipClass: () => "shadow rounded"
37
+ }),
38
+ // TODO(burdon): Option to create new page?
39
+ // TODO(burdon): Optional decoration via addToOptions
40
+ markdownLanguage.data.of({
41
+ autocomplete: (context) => {
42
+ const match = context.matchBefore(/\w*/);
43
+ if (!match || match.from === match.to && !context.explicit) {
44
+ return null;
35
45
  }
36
- ]
46
+ return {
47
+ from: match.from,
48
+ options: onSearch(match.text.toLowerCase())
49
+ };
50
+ }
37
51
  })
38
52
  ];
39
53
  };
40
54
 
41
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/basic.ts
42
- import { closeBrackets } from "@codemirror/autocomplete";
43
- import { bracketMatching, defaultHighlightStyle, syntaxHighlighting } from "@codemirror/language";
44
- import { oneDarkHighlightStyle } from "@codemirror/theme-one-dark";
45
- import { drawSelection, EditorView, placeholder } from "@codemirror/view";
46
- var basicBundle = ({ readonly, themeMode, placeholder: _placeholder }) => [
47
- EditorView.lineWrapping,
48
- // TODO(burdon): Compare with minimalSetup.
49
- // https://codemirror.net/docs/ref/#codemirror.minimalSetup
50
- bracketMatching(),
51
- closeBrackets(),
52
- drawSelection(),
53
- _placeholder && placeholder(_placeholder),
54
- // TODO(burdon): Is this required?
55
- themeMode === "dark" ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle)
56
- ].filter(Boolean);
57
-
58
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/blast.ts
59
- import { EditorView as EditorView2, keymap as keymap2 } from "@codemirror/view";
60
- import defaultsDeep from "lodash.defaultsdeep";
55
+ // packages/ui/react-ui-editor/src/extensions/automerge/automerge.ts
56
+ import { StateField } from "@codemirror/state";
57
+ import { EditorView, ViewPlugin } from "@codemirror/view";
58
+ import { next as A } from "@dxos/automerge/automerge";
61
59
  import { invariant } from "@dxos/invariant";
62
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/components/TextEditor/extensions/blast.ts";
63
- var defaultOptions = {
64
- effect: 2,
65
- maxParticles: 200,
66
- particleGravity: 0.08,
67
- particleAlphaFadeout: 0.996,
68
- particleSize: {
69
- min: 2,
70
- max: 8
71
- },
72
- particleNumRange: {
73
- min: 5,
74
- max: 10
75
- },
76
- particleVelocityRange: {
77
- x: [
78
- -1,
79
- 1
80
- ],
81
- y: [
82
- -3.5,
83
- -1.5
84
- ]
60
+
61
+ // packages/ui/react-ui-editor/src/extensions/automerge/cursor.ts
62
+ import get from "lodash.get";
63
+ import { next as automerge } from "@dxos/automerge/automerge";
64
+ var cursorConverter = (handle, path) => ({
65
+ // TODO(burdon): Handle assoc to associate with a previous character.
66
+ toCursor: (pos) => {
67
+ const doc = handle.docSync();
68
+ if (!doc) {
69
+ return "";
70
+ }
71
+ const value = get(doc, path);
72
+ if (typeof value === "string" && value.length <= pos) {
73
+ return "end";
74
+ }
75
+ return automerge.getCursor(doc, path.slice(), pos);
85
76
  },
86
- particleShrinkRate: 0.95,
87
- shakeIntensity: 5
88
- };
89
- var blast = (options = defaultOptions) => {
90
- let blaster;
91
- let last = 0;
92
- const getPoint = (view, pos) => {
93
- const { left: x = 0, top: y = 0 } = view.coordsForChar(pos) ?? {};
94
- const element = document.elementFromPoint(x, y);
95
- const { offsetLeft: left = 0, offsetTop: top = 0 } = view.scrollDOM.parentElement ?? {};
96
- const point = {
97
- x: x - left,
98
- y: y - top
99
- };
100
- return {
101
- element,
102
- point
103
- };
104
- };
105
- return [
106
- // Cursor moved.
107
- EditorView2.updateListener.of((update4) => {
108
- if (blaster?.node !== update4.view.scrollDOM) {
109
- if (blaster) {
110
- blaster.destroy();
111
- }
112
- blaster = new Blaster(update4.view.scrollDOM, defaultsDeep({
113
- particleGravity: 0.2,
114
- particleShrinkRate: 0.995,
115
- color: () => [
116
- 100 + Math.random() * 100,
117
- 0,
118
- 0
119
- ]
120
- }, options, defaultOptions));
121
- blaster.initialize();
122
- blaster.start();
77
+ fromCursor: (cursor) => {
78
+ if (cursor === "") {
79
+ return 0;
80
+ }
81
+ const doc = handle.docSync();
82
+ if (!doc) {
83
+ return 0;
84
+ }
85
+ if (cursor === "end") {
86
+ const value = get(doc, path);
87
+ if (typeof value === "string") {
88
+ return value.length;
123
89
  } else {
124
- blaster.resize();
125
- }
126
- const current = update4.state.selection.main.head;
127
- if (current !== last) {
128
- last = current;
129
- const { element, point } = getPoint(update4.view, current - 1);
130
- if (element && point) {
131
- blaster.spawn({
132
- element,
133
- point
134
- });
135
- }
136
- }
137
- }),
138
- keymap2.of([
139
- {
140
- any: (_, event) => {
141
- if (blaster) {
142
- if (event.key === "Enter" && event.shiftKey) {
143
- blaster.shake({
144
- time: 0.8
145
- });
146
- return true;
147
- }
148
- }
149
- return false;
150
- }
151
- }
152
- ])
153
- ];
154
- };
155
- var Blaster = class {
156
- constructor(_node, _options) {
157
- this._node = _node;
158
- this._options = _options;
159
- this._running = false;
160
- this._shakeTime = 0;
161
- this._shakeTimeMax = 0;
162
- this._particles = [];
163
- this._particlePointer = 0;
164
- this._lastPoint = {
165
- x: 0,
166
- y: 0
167
- };
168
- this.shake = throttle(({ time }) => {
169
- this._shakeTime = this._shakeTimeMax || time;
170
- this._shakeTimeMax = time;
171
- }, 100);
172
- this.spawn = throttle(({ element, point }) => {
173
- const color = getRGBComponents(element, this._options.color);
174
- const numParticles = random(this._options.particleNumRange.min, this._options.particleNumRange.max);
175
- const dir = this._lastPoint.x === point.x ? 0 : this._lastPoint.x < point.x ? 1 : -1;
176
- this._lastPoint = point;
177
- for (let i = numParticles; i--; i > 0) {
178
- this._particles[this._particlePointer] = this._effect.create(point.x - dir * 16, point.y, color);
179
- this._particlePointer = (this._particlePointer + 1) % this._options.maxParticles;
90
+ return 0;
180
91
  }
181
- }, 100);
182
- this._effect = this._options.effect === 1 ? new Effect1(_options) : new Effect2(_options);
183
- }
184
- get node() {
185
- return this._node;
186
- }
187
- initialize() {
188
- invariant(!this._canvas && !this._ctx, void 0, {
189
- F: __dxlog_file,
190
- L: 138,
191
- S: this,
192
- A: [
193
- "!this._canvas && !this._ctx",
194
- ""
195
- ]
196
- });
197
- this._canvas = document.createElement("canvas");
198
- this._canvas.id = "code-blast-canvas";
199
- this._canvas.style.position = "absolute";
200
- this._canvas.style.zIndex = "0";
201
- this._canvas.style.pointerEvents = "none";
202
- this._ctx = this._canvas.getContext("2d");
203
- const parent = this._node.parentElement?.parentElement;
204
- parent?.appendChild(this._canvas);
205
- this.resize();
206
- }
207
- destroy() {
208
- this.stop();
209
- if (this._canvas) {
210
- this._canvas.remove();
211
- this._canvas = void 0;
212
- this._ctx = void 0;
213
92
  }
93
+ return automerge.getCursorPosition(doc, path.slice(), cursor);
214
94
  }
215
- resize() {
216
- if (this._node.parentElement && this._canvas) {
217
- const { offsetLeft: x, offsetTop: y, offsetWidth: width, offsetHeight: height } = this._node.parentElement;
218
- this._canvas.style.top = `${y}px`;
219
- this._canvas.style.left = `${x}px`;
220
- this._canvas.width = width;
221
- this._canvas.height = height;
222
- }
223
- }
224
- start() {
225
- invariant(this._canvas && this._ctx, void 0, {
226
- F: __dxlog_file,
227
- L: 177,
228
- S: this,
229
- A: [
230
- "this._canvas && this._ctx",
231
- ""
232
- ]
95
+ });
96
+
97
+ // packages/ui/react-ui-editor/src/extensions/automerge/defs.ts
98
+ import { Annotation, StateEffect } from "@codemirror/state";
99
+ var effectType = StateEffect.define({});
100
+ var reconcileAnnotationType = Annotation.define();
101
+ var getPath = (state, field) => state.field(field).path;
102
+ var getLastHeads = (state, field) => state.field(field).lastHeads;
103
+ var updateHeads = (newHeads) => effectType.of({
104
+ newHeads
105
+ });
106
+ var isReconcileTx = (tr) => !!tr.annotation(reconcileAnnotationType);
107
+
108
+ // packages/ui/react-ui-editor/src/extensions/automerge/semaphore.ts
109
+ import { next as automerge2 } from "@dxos/automerge/automerge";
110
+
111
+ // packages/ui/react-ui-editor/src/extensions/automerge/update-automerge.ts
112
+ import { next as am } from "@dxos/automerge/automerge";
113
+ var updateAutomerge = (field, handle, transactions, state) => {
114
+ const { lastHeads, path } = state.field(field);
115
+ let hasChanges = false;
116
+ for (const tr of transactions) {
117
+ tr.changes.iterChanges(() => {
118
+ hasChanges = true;
233
119
  });
234
- this._running = true;
235
- this.loop();
236
120
  }
237
- stop() {
238
- this._running = false;
239
- this._node.style.transform = "translate(0px, 0px)";
121
+ if (!hasChanges) {
122
+ return void 0;
240
123
  }
241
- loop() {
242
- if (!this._running || !this._canvas || !this._ctx) {
243
- return;
124
+ const newHeads = handle.changeAt(lastHeads, (doc) => {
125
+ for (const tr of transactions) {
126
+ tr.changes.iterChanges((fromA, toA, _fromB, _toB, inserted) => {
127
+ am.splice(doc, path, fromA, toA - fromA, inserted.toString());
128
+ });
244
129
  }
245
- this._ctx.clearRect(0, 0, this._canvas.width ?? 0, this._canvas.height ?? 0);
246
- const now = (/* @__PURE__ */ new Date()).getTime();
247
- this._lastTime ??= now;
248
- const dt = (now - this._lastTime) / 1e3;
249
- this._lastTime = now;
250
- if (this._shakeTime > 0) {
251
- this._shakeTime -= dt;
252
- const magnitude = this._shakeTime / this._shakeTimeMax * this._options.shakeIntensity;
253
- const shakeX = random(-magnitude, magnitude);
254
- const shakeY = random(-magnitude, magnitude);
255
- this._node.style.transform = `translate(${shakeX}px,${shakeY}px)`;
256
- }
257
- this.drawParticles();
258
- requestAnimationFrame(this.loop.bind(this));
259
- }
260
- drawParticles() {
261
- for (let i = this._particles.length; i--; i > 0) {
262
- const particle = this._particles[i];
263
- if (!particle) {
264
- continue;
265
- }
266
- if (particle.alpha < 0.01 || particle.size <= 0.5) {
267
- continue;
268
- }
269
- this._effect.update(this._ctx, particle);
130
+ });
131
+ return newHeads ?? void 0;
132
+ };
133
+
134
+ // packages/ui/react-ui-editor/src/extensions/automerge/update-codemirror.ts
135
+ import { ChangeSet } from "@codemirror/state";
136
+ var updateCodeMirror = (view, selection, target, patches) => {
137
+ for (const patch of patches) {
138
+ const changeSpec = handlePatch(patch, target, view.state);
139
+ if (changeSpec != null) {
140
+ const changeSet = ChangeSet.of(changeSpec, view.state.doc.length, "\n");
141
+ selection = selection.map(changeSet, 1);
142
+ view.dispatch({
143
+ changes: changeSet,
144
+ annotations: reconcileAnnotationType.of({})
145
+ });
270
146
  }
271
147
  }
148
+ view.dispatch({
149
+ selection,
150
+ annotations: reconcileAnnotationType.of({})
151
+ });
272
152
  };
273
- var Effect = class {
274
- constructor(_options) {
275
- this._options = _options;
153
+ var handlePatch = (patch, target, state) => {
154
+ if (patch.action === "insert") {
155
+ return handleInsert(target, patch);
156
+ } else if (patch.action === "splice") {
157
+ return handleSplice(target, patch);
158
+ } else if (patch.action === "del") {
159
+ return handleDel(target, patch);
160
+ } else if (patch.action === "put") {
161
+ return handlePut(target, patch, state);
162
+ } else {
163
+ return null;
276
164
  }
277
165
  };
278
- var Effect1 = class extends Effect {
279
- create(x, y, color) {
280
- return {
281
- x,
282
- y: y + 10,
283
- vx: random(this._options.particleVelocityRange.x[0], this._options.particleVelocityRange.x[1]),
284
- vy: random(this._options.particleVelocityRange.y[0], this._options.particleVelocityRange.y[1]),
285
- size: random(this._options.particleSize.min, this._options.particleSize.max),
286
- color,
287
- alpha: 1
288
- };
289
- }
290
- update(ctx, particle) {
291
- particle.vy += this._options.particleGravity;
292
- particle.x += particle.vx;
293
- particle.y += particle.vy;
294
- particle.alpha *= this._options.particleAlphaFadeout;
295
- ctx.fillStyle = `rgba(${particle.color[0]},${particle.color[1]},${particle.color[2]},${particle.alpha})`;
296
- ctx.fillRect(Math.round(particle.x - 1), Math.round(particle.y - 1), particle.size, particle.size);
166
+ var handleInsert = (target, patch) => {
167
+ const index = charPath(target, patch.path);
168
+ if (index == null) {
169
+ return [];
297
170
  }
171
+ const text = patch.values.map((v) => v ? v.toString() : "").join("");
172
+ return [
173
+ {
174
+ from: index,
175
+ to: index,
176
+ insert: text
177
+ }
178
+ ];
298
179
  };
299
- var Effect2 = class extends Effect {
300
- create(x, y, color) {
301
- return {
302
- x,
303
- y: y + 10,
304
- vx: random(this._options.particleVelocityRange.x[0], this._options.particleVelocityRange.x[1]),
305
- vy: random(this._options.particleVelocityRange.y[0], this._options.particleVelocityRange.y[1]),
306
- size: random(this._options.particleSize.min, this._options.particleSize.max),
307
- color,
308
- alpha: 1,
309
- theta: random(0, 360) * Math.PI / 180,
310
- drag: 0.92,
311
- wander: 0.15
312
- };
313
- }
314
- update(ctx, particle) {
315
- particle.vy += this._options.particleGravity;
316
- particle.x += particle.vx;
317
- particle.y += particle.vy;
318
- particle.vx *= particle.drag;
319
- particle.vy *= particle.drag;
320
- particle.theta += random(-0.5, 0.5);
321
- particle.vx += Math.sin(particle.theta) * 0.1;
322
- particle.vy += Math.cos(particle.theta) * 0.1;
323
- particle.size *= this._options.particleShrinkRate;
324
- particle.alpha *= this._options.particleAlphaFadeout;
325
- ctx.fillStyle = `rgba(${particle.color[0]},${particle.color[1]},${particle.color[2]},${particle.alpha})`;
326
- ctx.beginPath();
327
- ctx.arc(Math.round(particle.x - 1), Math.round(particle.y - 1), particle.size, 0, 2 * Math.PI);
328
- ctx.fill();
180
+ var handleSplice = (target, patch) => {
181
+ const index = charPath(target, patch.path);
182
+ if (index == null) {
183
+ return [];
329
184
  }
330
- };
331
- function throttle(callback, limit) {
332
- let wait = false;
333
- return function(...args) {
334
- if (!wait) {
335
- callback.apply(this, args);
336
- wait = true;
337
- setTimeout(() => {
338
- wait = false;
339
- }, limit);
185
+ return [
186
+ {
187
+ from: index,
188
+ insert: patch.value
340
189
  }
341
- };
342
- }
343
- var getRGBComponents = (node, color) => {
344
- if (typeof color === "function") {
345
- return color();
190
+ ];
191
+ };
192
+ var handleDel = (target, patch) => {
193
+ const index = charPath(target, patch.path);
194
+ if (index == null) {
195
+ return [];
346
196
  }
347
- const bgColor = getComputedStyle(node).color;
348
- if (bgColor) {
349
- const x = bgColor.match(/(\d+), (\d+), (\d+)/)?.slice(1);
350
- if (x) {
351
- return x;
197
+ const length = patch.length || 1;
198
+ return [
199
+ {
200
+ from: index,
201
+ to: index + length
352
202
  }
203
+ ];
204
+ };
205
+ var handlePut = (target, patch, state) => {
206
+ const index = charPath(target, [
207
+ ...patch.path,
208
+ 0
209
+ ]);
210
+ if (index == null) {
211
+ return [];
212
+ }
213
+ const length = state.doc.length;
214
+ if (typeof patch.value !== "string") {
215
+ return [];
353
216
  }
354
217
  return [
355
- 50,
356
- 50,
357
- 50
218
+ {
219
+ from: 0,
220
+ to: length,
221
+ insert: patch.value
222
+ }
358
223
  ];
359
224
  };
360
- var random = (min, max) => {
361
- if (!max) {
362
- max = min;
363
- min = 0;
225
+ var charPath = (textPath, candidatePath) => {
226
+ if (candidatePath.length !== textPath.length + 1) {
227
+ return null;
364
228
  }
365
- return min + ~~(Math.random() * (max - min + 1));
229
+ for (let i = 0; i < textPath.length; i++) {
230
+ if (textPath[i] !== candidatePath[i]) {
231
+ return null;
232
+ }
233
+ }
234
+ const index = candidatePath[candidatePath.length - 1];
235
+ if (typeof index === "number") {
236
+ return index;
237
+ }
238
+ return null;
366
239
  };
367
240
 
368
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/code.ts
369
- import { ViewPlugin, EditorView as EditorView3, Decoration } from "@codemirror/view";
370
- import get from "lodash.get";
371
- import { mx as mx2 } from "@dxos/react-ui-theme";
241
+ // packages/ui/react-ui-editor/src/extensions/automerge/semaphore.ts
242
+ var PatchSemaphore = class {
243
+ // prettier-ignore
244
+ constructor(_handle, _state) {
245
+ this._handle = _handle;
246
+ this._state = _state;
247
+ this._inReconcile = false;
248
+ }
249
+ // NOTE: Cannot destruct view.state.
250
+ reconcile(view) {
251
+ if (this._inReconcile) {
252
+ return;
253
+ }
254
+ this._inReconcile = true;
255
+ const path = getPath(view.state, this._state);
256
+ const oldHeads = getLastHeads(view.state, this._state);
257
+ let selection = view.state.selection;
258
+ const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
259
+ const toInvert = transactions.slice().reverse();
260
+ for (const tx of toInvert) {
261
+ const inverted = tx.changes.invert(tx.startState.doc);
262
+ selection = selection.map(inverted);
263
+ view.dispatch({
264
+ changes: inverted,
265
+ annotations: reconcileAnnotationType.of(true)
266
+ });
267
+ }
268
+ let newHeads = updateAutomerge(this._state, this._handle, transactions, view.state);
269
+ if (newHeads === null || newHeads === void 0) {
270
+ newHeads = automerge2.getHeads(this._handle.docSync());
271
+ }
272
+ const diff = automerge2.equals(oldHeads, newHeads) ? [] : automerge2.diff(this._handle.docSync(), oldHeads, newHeads);
273
+ updateCodeMirror(view, selection, path, diff);
274
+ view.dispatch({
275
+ effects: updateHeads(newHeads),
276
+ annotations: reconcileAnnotationType.of({})
277
+ });
278
+ this._inReconcile = false;
279
+ }
280
+ };
372
281
 
373
- // packages/ui/react-ui-editor/src/styles/markdown.ts
374
- import { mx } from "@dxos/react-ui-theme";
375
- var headings = {
376
- 1: [
377
- "mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl",
378
- "-ml-[10px]"
379
- ],
380
- 2: [
381
- "mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl",
382
- "-ml-[8px]"
383
- ],
384
- 3: [
385
- "mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl",
386
- "-ml-[7px]"
387
- ],
388
- 4: [
389
- "mbs-4 mbe-2 font-medium text-inherit no-underline text-xl",
390
- "-ml-[6px]"
391
- ],
392
- 5: [
393
- "mbs-4 mbe-2 font-medium text-inherit no-underline text-lg",
394
- "-ml-[5px]"
395
- ],
396
- 6: [
397
- "mbs-4 mbe-2 font-medium text-inherit no-underline",
398
- "-ml-[4px]"
399
- ]
282
+ // packages/ui/react-ui-editor/src/extensions/cursor.ts
283
+ import { Facet } from "@codemirror/state";
284
+ var defaultCursorConverter = {
285
+ toCursor: (position) => position.toString(),
286
+ fromCursor: (cursor) => parseInt(cursor)
400
287
  };
401
- var heading = (level, readonly) => {
402
- const [style, offset] = headings[level];
403
- return mx(style, readonly && offset);
288
+ var Cursor = class _Cursor {
289
+ static {
290
+ this.converter = Facet.define({
291
+ combine: (providers) => providers[0] ?? defaultCursorConverter
292
+ });
293
+ }
294
+ static {
295
+ this.getCursorFromRange = (state, range) => {
296
+ const cursorConverter2 = state.facet(_Cursor.converter);
297
+ const from = cursorConverter2.toCursor(range.from);
298
+ const to = cursorConverter2.toCursor(range.to, -1);
299
+ return [
300
+ from,
301
+ to
302
+ ].join(":");
303
+ };
304
+ }
305
+ static {
306
+ this.getRangeFromCursor = (state, cursor) => {
307
+ const cursorConverter2 = state.facet(_Cursor.converter);
308
+ const parts = cursor.split(":");
309
+ const from = cursorConverter2.fromCursor(parts[0]);
310
+ const to = cursorConverter2.fromCursor(parts[1]);
311
+ return from !== void 0 && to !== void 0 ? {
312
+ from,
313
+ to
314
+ } : void 0;
315
+ };
316
+ }
404
317
  };
405
- var light = "text-neutral-200 dark:text-neutral-800";
406
- var mark = mx("!font-normal !no-underline !text-inherit opacity-40", light);
407
- var bold = "font-bold";
408
- var italic = "italic";
409
- var strikethrough = "line-through";
410
- var code = "font-mono !no-underline text-neutral-700 dark:text-neutral-300";
411
- var codeMark = "font-mono text-primary-500";
412
- var inlineUrl = mx(code, "px-1");
413
- var blockquote = mx("pl-1 mr-1 border-is-4 border-primary-500/70 dark:border-primary-500/30", light);
414
-
415
- // packages/ui/react-ui-editor/src/styles/tokens.ts
416
- import { tailwindConfig } from "@dxos/react-ui-theme";
417
- var tokens = tailwindConfig({}).theme;
418
318
 
419
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/code.ts
420
- var CODE_REGEX = /```[\s\S]*?```/gs;
421
- var styles = EditorView3.baseTheme({
422
- "& .cm-codeblock": {
423
- fontFamily: get(tokens, "fontFamily.mono", []).join(",")
424
- },
425
- '&light [aria-readonly="true"] .cm-codeblock': {
426
- background: get(tokens, "extend.colors.neutral.50")
427
- },
428
- '&dark [aria-readonly="true"] .cm-codeblock': {
429
- background: get(tokens, "extend.colors.neutral.850")
430
- },
431
- '& [aria-readonly="true"] .cm-codeblock': {
432
- display: "block",
433
- paddingInline: "4px !important"
434
- },
435
- '& [aria-readonly="true"] .cm-codeblock-first': {
436
- paddingTop: "4px !important",
437
- borderTopLeftRadius: "4px",
438
- borderTopRightRadius: "4px"
439
- },
440
- '& [aria-readonly="true"] .cm-codeblock-last': {
441
- paddingBottom: "4px !important",
442
- borderBottomLeftRadius: "4px",
443
- borderBottomRightRadius: "4px"
444
- }
445
- });
446
- var getLineRange = (lines, from, to) => {
447
- const start = lines.findIndex((line) => line.from >= from);
448
- const end = lines.findIndex((line) => line.to >= to);
449
- return [
450
- start,
451
- end
452
- ];
453
- };
454
- var code2 = () => {
455
- const getDecorations = (view) => {
456
- const decorations = [];
457
- if (view.state.readOnly) {
458
- const text = view.state.doc.sliceString(0);
459
- const matches = text.matchAll(CODE_REGEX);
460
- const blocks = view.viewportLineBlocks;
461
- for (const match of matches) {
462
- const range = getLineRange(blocks, match.index, match.index + match[0].length);
463
- for (let i = range[0]; i <= range[1]; i++) {
464
- const block = blocks[i];
465
- decorations.push(Decoration.line({
466
- class: mx2("cm-codeblock", i === range[0] && "cm-codeblock-first", i === range[1] && "cm-codeblock-last")
467
- }).range(block.from));
319
+ // packages/ui/react-ui-editor/src/extensions/automerge/automerge.ts
320
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/extensions/automerge/automerge.ts";
321
+ var automerge3 = ({ handle, path }) => {
322
+ const state = StateField.define({
323
+ create: () => ({
324
+ path: path.slice(),
325
+ lastHeads: A.getHeads(handle.docSync()),
326
+ unreconciledTransactions: []
327
+ }),
328
+ update: (value, tr) => {
329
+ const result = {
330
+ path: path.slice(),
331
+ lastHeads: value.lastHeads,
332
+ unreconciledTransactions: value.unreconciledTransactions.slice()
333
+ };
334
+ let clearUnreconciled = false;
335
+ for (const effect of tr.effects) {
336
+ if (effect.is(effectType)) {
337
+ result.lastHeads = effect.value.newHeads;
338
+ clearUnreconciled = true;
339
+ }
340
+ }
341
+ if (clearUnreconciled) {
342
+ result.unreconciledTransactions = [];
343
+ } else {
344
+ if (!isReconcileTx(tr)) {
345
+ result.unreconciledTransactions.push(tr);
468
346
  }
469
347
  }
348
+ return result;
470
349
  }
471
- return Decoration.set(decorations);
472
- };
350
+ });
351
+ const semaphore = new PatchSemaphore(handle, state);
473
352
  return [
353
+ Cursor.converter.of(cursorConverter(handle, path)),
354
+ // Reconcile external updates.
474
355
  ViewPlugin.fromClass(class {
475
- constructor(view) {
476
- this.decorations = getDecorations(view);
356
+ constructor(_view) {
357
+ this._view = _view;
358
+ invariant(this._view, void 0, {
359
+ F: __dxlog_file,
360
+ L: 66,
361
+ S: this,
362
+ A: [
363
+ "this._view",
364
+ ""
365
+ ]
366
+ });
367
+ handle.addListener("change", this._handleChange.bind(this));
477
368
  }
478
- update(update4) {
479
- if (update4.docChanged || update4.viewportChanged) {
480
- this.decorations = getDecorations(update4.view);
481
- }
369
+ destroy() {
370
+ handle.removeListener("change", this._handleChange.bind(this));
371
+ }
372
+ _handleChange() {
373
+ semaphore.reconcile(this._view);
482
374
  }
483
- }, {
484
- decorations: (value) => value.decorations
485
375
  }),
486
- styles
376
+ // Reconcile local updates.
377
+ EditorView.updateListener.of(({ view, changes }) => {
378
+ if (!changes.empty) {
379
+ semaphore.reconcile(view);
380
+ }
381
+ }),
382
+ state
487
383
  ];
488
384
  };
489
385
 
490
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/comments.ts
491
- import { StateEffect as StateEffect2, StateField as StateField3 } from "@codemirror/state";
492
- import { hoverTooltip, keymap as keymap3, Decoration as Decoration2, EditorView as EditorView4, MatchDecorator, ViewPlugin as ViewPlugin3, WidgetType } from "@codemirror/view";
493
- import sortBy from "lodash.sortby";
494
- import { debounce } from "@dxos/async";
495
- import { invariant as invariant3 } from "@dxos/invariant";
496
- import { log as log3 } from "@dxos/log";
497
- import { nonNullable } from "@dxos/util";
498
-
499
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/util.ts
500
- import { log } from "@dxos/log";
501
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/components/TextEditor/extensions/util.ts";
502
- var callbackWrapper = (fn) => (...args) => {
503
- try {
504
- return fn(...args);
505
- } catch (error) {
506
- log.catch(error, void 0, {
507
- F: __dxlog_file2,
508
- L: 16,
509
- S: void 0,
510
- C: (f, a) => f(...a)
511
- });
386
+ // packages/ui/react-ui-editor/src/extensions/awareness.ts
387
+ import { Annotation as Annotation2, Facet as Facet2, RangeSet } from "@codemirror/state";
388
+ import { Decoration, EditorView as EditorView2, ViewPlugin as ViewPlugin2, WidgetType } from "@codemirror/view";
389
+ import { Event } from "@dxos/async";
390
+ import { Context } from "@dxos/context";
391
+ var dummyProvider = {
392
+ remoteStateChange: new Event(),
393
+ open: () => {
394
+ },
395
+ close: () => {
396
+ },
397
+ getRemoteStates: () => [],
398
+ update: () => {
512
399
  }
513
400
  };
514
-
515
- // packages/ui/react-ui-editor/src/hooks/useTextModel.ts
516
- import { StateField as StateField2 } from "@codemirror/state";
517
- import get2 from "lodash.get";
518
- import { useEffect, useState } from "react";
519
- import { yCollab } from "y-codemirror.next";
520
- import * as Y from "yjs";
521
- import { invariant as invariant2 } from "@dxos/invariant";
522
- import { getRawDoc, isActualAutomergeObject } from "@dxos/react-client/echo";
523
- import { arrayToString, stringToArray } from "@dxos/util";
524
-
525
- // packages/ui/react-ui-editor/src/hooks/automerge/plugin.ts
526
- import { Annotation, Facet, StateEffect, StateField } from "@codemirror/state";
527
- import { ViewPlugin as ViewPlugin2 } from "@codemirror/view";
528
- import * as automerge2 from "@dxos/automerge/automerge";
529
-
530
- // packages/ui/react-ui-editor/src/hooks/automerge/semaphore.ts
531
- import { next as automerge } from "@dxos/automerge/automerge";
532
-
533
- // packages/ui/react-ui-editor/src/hooks/automerge/update-automerge.ts
534
- import { next as am } from "@dxos/automerge/automerge";
535
- var updateAutomerge = (field, handle, transactions, state) => {
536
- const { lastHeads, path } = state.field(field);
537
- let hasChanges = false;
538
- for (const tr of transactions) {
539
- tr.changes.iterChanges(() => {
540
- hasChanges = true;
401
+ var AwarenessProvider = Facet2.define({
402
+ combine: (providers) => providers[0] ?? dummyProvider
403
+ });
404
+ var RemoteSelectionChangedAnnotation = Annotation2.define();
405
+ var awareness = (provider = dummyProvider) => {
406
+ return [
407
+ AwarenessProvider.of(provider),
408
+ ViewPlugin2.fromClass(RemoteSelectionsDecorator, {
409
+ decorations: (value) => value.decorations
410
+ }),
411
+ styles
412
+ ];
413
+ };
414
+ var RemoteSelectionsDecorator = class {
415
+ constructor(view) {
416
+ this.decorations = RangeSet.of([]);
417
+ this._ctx = new Context();
418
+ this._lastAnchor = void 0;
419
+ this._lastHead = void 0;
420
+ this._cursorConverter = view.state.facet(Cursor.converter);
421
+ this._provider = view.state.facet(AwarenessProvider);
422
+ this._provider.open();
423
+ this._provider.remoteStateChange.on(this._ctx, () => {
424
+ view.dispatch({
425
+ annotations: [
426
+ RemoteSelectionChangedAnnotation.of([])
427
+ ]
428
+ });
541
429
  });
542
430
  }
543
- if (!hasChanges) {
544
- return void 0;
431
+ destroy() {
432
+ void this._ctx.dispose();
433
+ this._provider.close();
545
434
  }
546
- const newHeads = handle.changeAt(lastHeads, (doc) => {
547
- for (const tr of transactions) {
548
- tr.changes.iterChanges((fromA, toA, _fromB, _toB, inserted) => {
549
- am.splice(doc, path, fromA, toA - fromA, inserted.toString());
550
- });
435
+ update(update2) {
436
+ this._updateLocalSelection(update2);
437
+ this._updateRemoteSelections(update2);
438
+ }
439
+ _updateLocalSelection(update2) {
440
+ const hasFocus = update2.view.hasFocus && update2.view.dom.ownerDocument.hasFocus();
441
+ const { anchor = void 0, head = void 0 } = hasFocus ? update2.state.selection.main : {};
442
+ if (this._lastAnchor === anchor && this._lastHead === head) {
443
+ return;
551
444
  }
552
- });
553
- return newHeads ?? void 0;
554
- };
555
-
556
- // packages/ui/react-ui-editor/src/hooks/automerge/update-codemirror.ts
557
- import { ChangeSet } from "@codemirror/state";
558
- var updateCodeMirror = (view, selection, target, patches) => {
559
- for (const patch of patches) {
560
- const changeSpec = handlePatch(patch, target, view.state);
561
- if (changeSpec != null) {
562
- const changeSet = ChangeSet.of(changeSpec, view.state.doc.length, "\n");
563
- selection = selection.map(changeSet, 1);
564
- view.dispatch({
565
- changes: changeSet,
566
- annotations: reconcileAnnotationType.of({})
445
+ this._lastAnchor = anchor;
446
+ this._lastHead = head;
447
+ this._provider.update(anchor !== void 0 && head !== void 0 ? {
448
+ anchor: this._cursorConverter.toCursor(anchor),
449
+ head: this._cursorConverter.toCursor(head)
450
+ } : void 0);
451
+ }
452
+ _updateRemoteSelections(update2) {
453
+ const decorations = [];
454
+ const awarenessStates = this._provider.getRemoteStates();
455
+ for (const state of awarenessStates) {
456
+ const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
457
+ const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
458
+ if (anchor == null || head == null) {
459
+ continue;
460
+ }
461
+ const start = Math.min(Math.min(anchor, head), update2.view.state.doc.length);
462
+ const end = Math.min(Math.max(anchor, head), update2.view.state.doc.length);
463
+ const startLine = update2.view.state.doc.lineAt(start);
464
+ const endLine = update2.view.state.doc.lineAt(end);
465
+ const color = state.info.color ?? "#30bced";
466
+ const lightColor = state.info.lightColor ?? color + "33";
467
+ if (startLine.number === endLine.number) {
468
+ decorations.push({
469
+ from: start,
470
+ to: end,
471
+ value: Decoration.mark({
472
+ attributes: {
473
+ style: `background-color: ${lightColor}`
474
+ },
475
+ class: "cm-collab-selection"
476
+ })
477
+ });
478
+ } else {
479
+ decorations.push({
480
+ from: start,
481
+ to: startLine.from + startLine.length,
482
+ value: Decoration.mark({
483
+ attributes: {
484
+ style: `background-color: ${color}`
485
+ },
486
+ class: "cm-collab-selection"
487
+ })
488
+ });
489
+ decorations.push({
490
+ from: endLine.from,
491
+ to: end,
492
+ value: Decoration.mark({
493
+ attributes: {
494
+ style: `background-color: ${color}`
495
+ },
496
+ class: "cm-collab-selection"
497
+ })
498
+ });
499
+ for (let i = startLine.number + 1; i < endLine.number; i++) {
500
+ const linePos = update2.view.state.doc.line(i).from;
501
+ decorations.push({
502
+ from: linePos,
503
+ to: linePos,
504
+ value: Decoration.line({
505
+ attributes: {
506
+ style: `background-color: ${color}`,
507
+ class: "cm-collab-selectionLine"
508
+ }
509
+ })
510
+ });
511
+ }
512
+ }
513
+ decorations.push({
514
+ from: head,
515
+ to: head,
516
+ value: Decoration.widget({
517
+ side: head - anchor > 0 ? -1 : 1,
518
+ block: false,
519
+ widget: new RemoteCaretWidget(state.info.displayName ?? "Anonymous", color)
520
+ })
567
521
  });
568
522
  }
523
+ this.decorations = Decoration.set(decorations, true);
569
524
  }
570
- view.dispatch({
571
- selection,
572
- annotations: reconcileAnnotationType.of({})
573
- });
574
525
  };
575
- var handlePatch = (patch, target, state) => {
576
- if (patch.action === "insert") {
577
- return handleInsert(target, patch);
578
- } else if (patch.action === "splice") {
579
- return handleSplice(target, patch);
580
- } else if (patch.action === "del") {
581
- return handleDel(target, patch);
582
- } else if (patch.action === "put") {
583
- return handlePut(target, patch, state);
584
- } else {
585
- return null;
526
+ var RemoteCaretWidget = class extends WidgetType {
527
+ constructor(_name, _color) {
528
+ super();
529
+ this._name = _name;
530
+ this._color = _color;
531
+ }
532
+ toDOM() {
533
+ const span = document.createElement("span");
534
+ span.className = "cm-collab-selectionCaret";
535
+ span.style.backgroundColor = this._color;
536
+ span.style.borderColor = this._color;
537
+ const dot = document.createElement("div");
538
+ dot.className = "cm-collab-selectionCaretDot";
539
+ const info = document.createElement("div");
540
+ info.className = "cm-collab-selectionInfo";
541
+ info.innerText = this._name;
542
+ span.appendChild(document.createTextNode("\u2060"));
543
+ span.appendChild(dot);
544
+ span.appendChild(document.createTextNode("\u2060"));
545
+ span.appendChild(info);
546
+ span.appendChild(document.createTextNode("\u2060"));
547
+ return span;
548
+ }
549
+ updateDOM() {
550
+ return false;
551
+ }
552
+ eq(widget) {
553
+ return widget._color === this._color;
554
+ }
555
+ get estimatedHeight() {
556
+ return -1;
557
+ }
558
+ ignoreEvent() {
559
+ return true;
586
560
  }
587
561
  };
588
- var handleInsert = (target, patch) => {
589
- const index = charPath(target, patch.path);
590
- if (index == null) {
591
- return [];
562
+ var styles = EditorView2.baseTheme({
563
+ ".cm-collab-selection": {},
564
+ ".cm-collab-selectionLine": {
565
+ padding: 0,
566
+ margin: "0px 2px 0px 4px"
567
+ },
568
+ ".cm-collab-selectionCaret": {
569
+ position: "relative",
570
+ borderLeft: "1px solid black",
571
+ borderRight: "1px solid black",
572
+ marginLeft: "-1px",
573
+ marginRight: "-1px",
574
+ boxSizing: "border-box",
575
+ display: "inline",
576
+ cursor: "pointer"
577
+ },
578
+ ".cm-collab-selectionCaretDot": {
579
+ borderRadius: "50%",
580
+ position: "absolute",
581
+ width: ".5em",
582
+ height: ".5em",
583
+ top: "-.25em",
584
+ left: "-.25em",
585
+ backgroundColor: "inherit",
586
+ transition: "transform .3s ease-in-out",
587
+ boxSizing: "border-box"
588
+ },
589
+ ".cm-collab-selectionCaret:hover > .cm-collab-selectionCaretDot": {
590
+ transform: "scale(0)",
591
+ transformOrigin: "center"
592
+ },
593
+ ".cm-collab-selectionInfo": {
594
+ position: "absolute",
595
+ transform: "translate(-50%, 0)",
596
+ top: "-20px",
597
+ left: 0,
598
+ fontSize: ".75em",
599
+ fontFamily: "sans-serif",
600
+ fontStyle: "normal",
601
+ fontWeight: "normal",
602
+ lineHeight: "normal",
603
+ userSelect: "none",
604
+ color: "white",
605
+ padding: "2px",
606
+ zIndex: 101,
607
+ transition: "opacity .3s ease-in-out",
608
+ backgroundColor: "inherit",
609
+ borderRadius: "2px",
610
+ // These should be separate.
611
+ opacity: 0,
612
+ transitionDelay: "0s",
613
+ whiteSpace: "nowrap"
614
+ },
615
+ ".cm-collab-selectionCaret:hover > .cm-collab-selectionInfo": {
616
+ opacity: 1,
617
+ transitionDelay: "0s"
592
618
  }
593
- const text = patch.values.map((v) => v ? v.toString() : "").join("");
619
+ });
620
+
621
+ // packages/ui/react-ui-editor/src/extensions/basic.ts
622
+ import { closeBrackets } from "@codemirror/autocomplete";
623
+ import { history } from "@codemirror/commands";
624
+ import { bracketMatching, defaultHighlightStyle, syntaxHighlighting } from "@codemirror/language";
625
+ import { oneDarkHighlightStyle } from "@codemirror/theme-one-dark";
626
+ import { drawSelection, EditorView as EditorView3, highlightActiveLine, placeholder } from "@codemirror/view";
627
+ var basicBundle = ({ themeMode, placeholder: _placeholder, lineWrapping = true }) => [
628
+ lineWrapping ? EditorView3.lineWrapping : [],
629
+ // https://codemirror.net/docs/ref/#codemirror.minimalSetup
630
+ bracketMatching(),
631
+ closeBrackets(),
632
+ drawSelection(),
633
+ highlightActiveLine(),
634
+ history(),
635
+ _placeholder ? placeholder(_placeholder) : [],
636
+ // https://github.com/codemirror/theme-one-dark
637
+ themeMode === "dark" ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle)
638
+ ];
639
+
640
+ // packages/ui/react-ui-editor/src/extensions/blast.ts
641
+ import { EditorView as EditorView4, keymap as keymap2 } from "@codemirror/view";
642
+ import defaultsDeep from "lodash.defaultsdeep";
643
+ import { invariant as invariant2 } from "@dxos/invariant";
644
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/extensions/blast.ts";
645
+ var defaultOptions = {
646
+ effect: 2,
647
+ maxParticles: 200,
648
+ particleGravity: 0.08,
649
+ particleAlphaFadeout: 0.996,
650
+ particleSize: {
651
+ min: 2,
652
+ max: 8
653
+ },
654
+ particleNumRange: {
655
+ min: 5,
656
+ max: 10
657
+ },
658
+ particleVelocityRange: {
659
+ x: [
660
+ -1,
661
+ 1
662
+ ],
663
+ y: [
664
+ -3.5,
665
+ -1.5
666
+ ]
667
+ },
668
+ particleShrinkRate: 0.95,
669
+ shakeIntensity: 5
670
+ };
671
+ var blast = (options = defaultOptions) => {
672
+ let blaster;
673
+ let last = 0;
674
+ const getPoint = (view, pos) => {
675
+ const { left: x = 0, top: y = 0 } = view.coordsForChar(pos) ?? {};
676
+ const element = document.elementFromPoint(x, y);
677
+ const { offsetLeft: left = 0, offsetTop: top = 0 } = view.scrollDOM.parentElement ?? {};
678
+ const point = {
679
+ x: x - left,
680
+ y: y - top
681
+ };
682
+ return {
683
+ element,
684
+ point
685
+ };
686
+ };
594
687
  return [
595
- {
596
- from: index,
597
- to: index,
598
- insert: text
599
- }
688
+ // Cursor moved.
689
+ EditorView4.updateListener.of((update2) => {
690
+ if (blaster?.node !== update2.view.scrollDOM) {
691
+ if (blaster) {
692
+ blaster.destroy();
693
+ }
694
+ blaster = new Blaster(update2.view.scrollDOM, defaultsDeep({
695
+ particleGravity: 0.2,
696
+ particleShrinkRate: 0.995,
697
+ color: () => [
698
+ 100 + Math.random() * 100,
699
+ 0,
700
+ 0
701
+ ]
702
+ }, options, defaultOptions));
703
+ blaster.initialize();
704
+ blaster.start();
705
+ } else {
706
+ blaster.resize();
707
+ }
708
+ const current = update2.state.selection.main.head;
709
+ if (current !== last) {
710
+ last = current;
711
+ const { element, point } = getPoint(update2.view, current - 1);
712
+ if (element && point) {
713
+ blaster.spawn({
714
+ element,
715
+ point
716
+ });
717
+ }
718
+ }
719
+ }),
720
+ keymap2.of([
721
+ {
722
+ any: (_, event) => {
723
+ if (blaster) {
724
+ if (event.key === "Enter" && event.shiftKey) {
725
+ blaster.shake({
726
+ time: 0.8
727
+ });
728
+ return true;
729
+ }
730
+ }
731
+ return false;
732
+ }
733
+ }
734
+ ])
600
735
  ];
601
736
  };
602
- var handleSplice = (target, patch) => {
603
- const index = charPath(target, patch.path);
604
- if (index == null) {
605
- return [];
737
+ var Blaster = class {
738
+ constructor(_node, _options) {
739
+ this._node = _node;
740
+ this._options = _options;
741
+ this._running = false;
742
+ this._shakeTime = 0;
743
+ this._shakeTimeMax = 0;
744
+ this._particles = [];
745
+ this._particlePointer = 0;
746
+ this._lastPoint = {
747
+ x: 0,
748
+ y: 0
749
+ };
750
+ this.shake = throttle(({ time }) => {
751
+ this._shakeTime = this._shakeTimeMax || time;
752
+ this._shakeTimeMax = time;
753
+ }, 100);
754
+ this.spawn = throttle(({ element, point }) => {
755
+ const color = getRGBComponents(element, this._options.color);
756
+ const numParticles = random(this._options.particleNumRange.min, this._options.particleNumRange.max);
757
+ const dir = this._lastPoint.x === point.x ? 0 : this._lastPoint.x < point.x ? 1 : -1;
758
+ this._lastPoint = point;
759
+ for (let i = numParticles; i--; i > 0) {
760
+ this._particles[this._particlePointer] = this._effect.create(point.x - dir * 16, point.y, color);
761
+ this._particlePointer = (this._particlePointer + 1) % this._options.maxParticles;
762
+ }
763
+ }, 100);
764
+ this._effect = this._options.effect === 1 ? new Effect1(_options) : new Effect2(_options);
606
765
  }
607
- return [
608
- {
609
- from: index,
610
- insert: patch.value
766
+ get node() {
767
+ return this._node;
768
+ }
769
+ initialize() {
770
+ invariant2(!this._canvas && !this._ctx, void 0, {
771
+ F: __dxlog_file2,
772
+ L: 138,
773
+ S: this,
774
+ A: [
775
+ "!this._canvas && !this._ctx",
776
+ ""
777
+ ]
778
+ });
779
+ this._canvas = document.createElement("canvas");
780
+ this._canvas.id = "code-blast-canvas";
781
+ this._canvas.style.position = "absolute";
782
+ this._canvas.style.zIndex = "0";
783
+ this._canvas.style.pointerEvents = "none";
784
+ this._ctx = this._canvas.getContext("2d");
785
+ const parent = this._node.parentElement?.parentElement;
786
+ parent?.appendChild(this._canvas);
787
+ this.resize();
788
+ }
789
+ destroy() {
790
+ this.stop();
791
+ if (this._canvas) {
792
+ this._canvas.remove();
793
+ this._canvas = void 0;
794
+ this._ctx = void 0;
795
+ }
796
+ }
797
+ resize() {
798
+ if (this._node.parentElement && this._canvas) {
799
+ const { offsetLeft: x, offsetTop: y, offsetWidth: width, offsetHeight: height } = this._node.parentElement;
800
+ this._canvas.style.top = `${y}px`;
801
+ this._canvas.style.left = `${x}px`;
802
+ this._canvas.width = width;
803
+ this._canvas.height = height;
804
+ }
805
+ }
806
+ start() {
807
+ invariant2(this._canvas && this._ctx, void 0, {
808
+ F: __dxlog_file2,
809
+ L: 177,
810
+ S: this,
811
+ A: [
812
+ "this._canvas && this._ctx",
813
+ ""
814
+ ]
815
+ });
816
+ this._running = true;
817
+ this.loop();
818
+ }
819
+ stop() {
820
+ this._running = false;
821
+ this._node.style.transform = "translate(0px, 0px)";
822
+ }
823
+ loop() {
824
+ if (!this._running || !this._canvas || !this._ctx) {
825
+ return;
826
+ }
827
+ this._ctx.clearRect(0, 0, this._canvas.width ?? 0, this._canvas.height ?? 0);
828
+ const now = (/* @__PURE__ */ new Date()).getTime();
829
+ this._lastTime ??= now;
830
+ const dt = (now - this._lastTime) / 1e3;
831
+ this._lastTime = now;
832
+ if (this._shakeTime > 0) {
833
+ this._shakeTime -= dt;
834
+ const magnitude = this._shakeTime / this._shakeTimeMax * this._options.shakeIntensity;
835
+ const shakeX = random(-magnitude, magnitude);
836
+ const shakeY = random(-magnitude, magnitude);
837
+ this._node.style.transform = `translate(${shakeX}px,${shakeY}px)`;
838
+ }
839
+ this.drawParticles();
840
+ requestAnimationFrame(this.loop.bind(this));
841
+ }
842
+ drawParticles() {
843
+ for (let i = this._particles.length; i--; i > 0) {
844
+ const particle = this._particles[i];
845
+ if (!particle) {
846
+ continue;
847
+ }
848
+ if (particle.alpha < 0.01 || particle.size <= 0.5) {
849
+ continue;
850
+ }
851
+ this._effect.update(this._ctx, particle);
611
852
  }
612
- ];
853
+ }
613
854
  };
614
- var handleDel = (target, patch) => {
615
- const index = charPath(target, patch.path);
616
- if (index == null) {
617
- return [];
855
+ var Effect = class {
856
+ constructor(_options) {
857
+ this._options = _options;
618
858
  }
619
- const length = patch.length || 1;
620
- return [
621
- {
622
- from: index,
623
- to: index + length
624
- }
625
- ];
626
859
  };
627
- var handlePut = (target, patch, state) => {
628
- const index = charPath(target, [
629
- ...patch.path,
630
- 0
631
- ]);
632
- if (index == null) {
633
- return [];
860
+ var Effect1 = class extends Effect {
861
+ create(x, y, color) {
862
+ return {
863
+ x,
864
+ y: y + 10,
865
+ vx: random(this._options.particleVelocityRange.x[0], this._options.particleVelocityRange.x[1]),
866
+ vy: random(this._options.particleVelocityRange.y[0], this._options.particleVelocityRange.y[1]),
867
+ size: random(this._options.particleSize.min, this._options.particleSize.max),
868
+ color,
869
+ alpha: 1
870
+ };
634
871
  }
635
- const length = state.doc.length;
636
- if (typeof patch.value !== "string") {
637
- return [];
872
+ update(ctx, particle) {
873
+ particle.vy += this._options.particleGravity;
874
+ particle.x += particle.vx;
875
+ particle.y += particle.vy;
876
+ particle.alpha *= this._options.particleAlphaFadeout;
877
+ ctx.fillStyle = `rgba(${particle.color[0]},${particle.color[1]},${particle.color[2]},${particle.alpha})`;
878
+ ctx.fillRect(Math.round(particle.x - 1), Math.round(particle.y - 1), particle.size, particle.size);
638
879
  }
639
- return [
640
- {
641
- from: 0,
642
- to: length,
643
- insert: patch.value
644
- }
645
- ];
646
880
  };
647
- var charPath = (textPath, candidatePath) => {
648
- if (candidatePath.length !== textPath.length + 1) {
649
- return null;
881
+ var Effect2 = class extends Effect {
882
+ create(x, y, color) {
883
+ return {
884
+ x,
885
+ y: y + 10,
886
+ vx: random(this._options.particleVelocityRange.x[0], this._options.particleVelocityRange.x[1]),
887
+ vy: random(this._options.particleVelocityRange.y[0], this._options.particleVelocityRange.y[1]),
888
+ size: random(this._options.particleSize.min, this._options.particleSize.max),
889
+ color,
890
+ alpha: 1,
891
+ theta: random(0, 360) * Math.PI / 180,
892
+ drag: 0.92,
893
+ wander: 0.15
894
+ };
650
895
  }
651
- for (let i = 0; i < textPath.length; i++) {
652
- if (textPath[i] !== candidatePath[i]) {
653
- return null;
896
+ update(ctx, particle) {
897
+ particle.vy += this._options.particleGravity;
898
+ particle.x += particle.vx;
899
+ particle.y += particle.vy;
900
+ particle.vx *= particle.drag;
901
+ particle.vy *= particle.drag;
902
+ particle.theta += random(-0.5, 0.5);
903
+ particle.vx += Math.sin(particle.theta) * 0.1;
904
+ particle.vy += Math.cos(particle.theta) * 0.1;
905
+ particle.size *= this._options.particleShrinkRate;
906
+ particle.alpha *= this._options.particleAlphaFadeout;
907
+ ctx.fillStyle = `rgba(${particle.color[0]},${particle.color[1]},${particle.color[2]},${particle.alpha})`;
908
+ ctx.beginPath();
909
+ ctx.arc(Math.round(particle.x - 1), Math.round(particle.y - 1), particle.size, 0, 2 * Math.PI);
910
+ ctx.fill();
911
+ }
912
+ };
913
+ function throttle(callback, limit) {
914
+ let wait = false;
915
+ return function(...args) {
916
+ if (!wait) {
917
+ callback.apply(this, args);
918
+ wait = true;
919
+ setTimeout(() => {
920
+ wait = false;
921
+ }, limit);
654
922
  }
923
+ };
924
+ }
925
+ var getRGBComponents = (node, color) => {
926
+ if (typeof color === "function") {
927
+ return color();
655
928
  }
656
- const index = candidatePath[candidatePath.length - 1];
657
- if (typeof index === "number") {
658
- return index;
929
+ const bgColor = getComputedStyle(node).color;
930
+ if (bgColor) {
931
+ const x = bgColor.match(/(\d+), (\d+), (\d+)/)?.slice(1);
932
+ if (x) {
933
+ return x;
934
+ }
659
935
  }
660
- return null;
936
+ return [
937
+ 50,
938
+ 50,
939
+ 50
940
+ ];
661
941
  };
662
-
663
- // packages/ui/react-ui-editor/src/hooks/automerge/semaphore.ts
664
- var PatchSemaphore = class {
665
- constructor(field) {
666
- this._inReconcile = false;
667
- this._queue = [];
668
- this.reconcile = (handle, view) => {
669
- if (this._inReconcile) {
670
- return;
671
- }
672
- this._inReconcile = true;
673
- const path = getPath(view.state, this._field);
674
- const oldHeads = getLastHeads(view.state, this._field);
675
- let selection = view.state.selection;
676
- const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
677
- const toInvert = transactions.slice().reverse();
678
- for (const tx of toInvert) {
679
- const inverted = tx.changes.invert(tx.startState.doc);
680
- selection = selection.map(inverted);
681
- view.dispatch({
682
- changes: inverted,
683
- annotations: reconcileAnnotationType.of(true)
684
- });
685
- }
686
- let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
687
- if (newHeads === null || newHeads === void 0) {
688
- newHeads = automerge.getHeads(handle.docSync());
689
- }
690
- const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync(), oldHeads, newHeads);
691
- updateCodeMirror(view, selection, path, diff);
692
- view.dispatch({
693
- effects: updateHeads(newHeads),
694
- annotations: reconcileAnnotationType.of({})
695
- });
696
- this._inReconcile = false;
697
- };
698
- if (field !== void 0) {
699
- this._field = field;
700
- }
942
+ var random = (min, max) => {
943
+ if (!max) {
944
+ max = min;
945
+ min = 0;
701
946
  }
947
+ return min + ~~(Math.random() * (max - min + 1));
702
948
  };
703
949
 
704
- // packages/ui/react-ui-editor/src/hooks/automerge/plugin.ts
705
- var effectType = StateEffect.define({});
706
- var updateHeads = (newHeads) => effectType.of({
707
- newHeads
708
- });
709
- var getLastHeads = (state, field) => state.field(field).lastHeads;
710
- var getPath = (state, field) => state.field(field).path;
711
- var semaphoreFacet = Facet.define({
712
- combine: (values) => values.at(-1)
713
- });
714
- var automergePlugin = (handle, path) => {
715
- const stateField = StateField.define({
716
- create: () => ({
717
- lastHeads: automerge2.getHeads(handle.docSync()),
718
- unreconciledTransactions: [],
719
- path: path.slice()
720
- }),
721
- update: (value, tr) => {
722
- const result = {
723
- lastHeads: value.lastHeads,
724
- unreconciledTransactions: value.unreconciledTransactions.slice(),
725
- path: path.slice()
726
- };
727
- let clearUnreconciled = false;
728
- for (const effect of tr.effects) {
729
- if (effect.is(effectType)) {
730
- result.lastHeads = effect.value.newHeads;
731
- clearUnreconciled = true;
732
- }
733
- }
734
- if (clearUnreconciled) {
735
- result.unreconciledTransactions = [];
736
- } else {
737
- if (!isReconcileTx(tr)) {
738
- result.unreconciledTransactions.push(tr);
739
- }
740
- }
741
- return result;
742
- }
743
- });
744
- const semaphore = new PatchSemaphore(stateField);
745
- const viewPlugin = ViewPlugin2.fromClass(class AutomergeCodemirrorViewPlugin {
746
- constructor(view) {
747
- this._handleChange = () => {
748
- this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
749
- };
750
- this._view = view;
751
- handle.addListener("change", this._handleChange);
752
- }
753
- update(update4) {
754
- if (update4.transactions.length > 0 && update4.transactions.some((t) => !isReconcileTx(t))) {
755
- queueMicrotask(() => {
756
- this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
757
- });
758
- }
759
- }
760
- destroy() {
761
- handle.addListener("change", this._handleChange);
762
- }
763
- });
764
- return {
765
- extension: [
766
- stateField,
767
- semaphoreFacet.of(semaphore),
768
- viewPlugin
769
- ]
770
- };
771
- };
772
- var reconcileAnnotationType = Annotation.define();
773
- var isReconcileTx = (tr) => !!tr.annotation(reconcileAnnotationType);
950
+ // packages/ui/react-ui-editor/src/extensions/code.ts
951
+ import { ViewPlugin as ViewPlugin3, EditorView as EditorView5, Decoration as Decoration2 } from "@codemirror/view";
952
+ import { mx as mx2 } from "@dxos/react-ui-theme";
774
953
 
775
- // packages/ui/react-ui-editor/src/hooks/yjs/cursors.ts
954
+ // packages/ui/react-ui-editor/src/styles/cursors.ts
776
955
  import * as random2 from "lib0/random";
777
956
  var cursorColors = [
778
957
  {
@@ -810,258 +989,243 @@ var cursorColors = [
810
989
  ];
811
990
  var cursorColor = cursorColors[random2.uint32() % cursorColors.length];
812
991
 
813
- // packages/ui/react-ui-editor/src/hooks/yjs/space-provider.ts
814
- import * as decoding from "lib0/decoding";
815
- import * as encoding from "lib0/encoding";
816
- import { Observable } from "lib0/observable";
817
- import * as awarenessProtocol from "y-protocols/awareness";
818
- import { log as log2 } from "@dxos/log";
819
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/hooks/yjs/space-provider.ts";
820
- var messageAwareness = 1;
821
- var messageQueryAwareness = 3;
822
- var SpaceAwarenessProvider = class extends Observable {
823
- constructor({ space, doc, channel, awareness }) {
824
- super();
825
- this._space = space;
826
- this._awareness = awareness ?? new awarenessProtocol.Awareness(doc);
827
- this._channel = channel;
828
- this._clientId = doc.clientID;
829
- this._awareness.on("update", this._handleAwarenessUpdate.bind(this));
830
- this._space.listen(this._channel, this._handleSpaceMessage.bind(this));
831
- if (typeof window !== "undefined") {
832
- window.addEventListener("beforeunload", this._handleBeforeUnload.bind(this));
833
- } else if (typeof process !== "undefined") {
834
- process.on("exit", this._handleBeforeUnload.bind(this));
835
- }
836
- const encoderAwarenessQuery = encoding.createEncoder();
837
- encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
838
- void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessQuery));
839
- const encoderAwarenessState = encoding.createEncoder();
840
- encoding.writeVarUint(encoderAwarenessState, messageAwareness);
841
- encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [
842
- this._clientId
843
- ]));
844
- void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessState));
845
- }
846
- get awareness() {
847
- return this._awareness;
848
- }
849
- _handleAwarenessUpdate({ added, updated, removed }, origin) {
850
- log2("awareness update", {
851
- added,
852
- updated,
853
- removed,
854
- origin
855
- }, {
856
- F: __dxlog_file3,
857
- L: 67,
858
- S: this,
859
- C: (f, a) => f(...a)
860
- });
861
- const changedClients = added.concat(updated).concat(removed);
862
- const encoderAwareness = encoding.createEncoder();
863
- encoding.writeVarUint(encoderAwareness, messageAwareness);
864
- encoding.writeVarUint8Array(encoderAwareness, awarenessProtocol.encodeAwarenessUpdate(this._awareness, changedClients));
865
- void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwareness));
866
- }
867
- _handleSpaceMessage({ payload }) {
868
- log2("space message", payload, {
869
- F: __dxlog_file3,
870
- L: 79,
871
- S: this,
872
- C: (f, a) => f(...a)
873
- });
874
- const data = new Uint8Array(Array.from(Object.values(payload)));
875
- const encoder = this._readMessage(data);
876
- if (encoder) {
877
- void this._space.postMessage(this._channel, encoding.toUint8Array(encoder));
878
- }
992
+ // packages/ui/react-ui-editor/src/styles/markdown.ts
993
+ import { mx } from "@dxos/react-ui-theme";
994
+ var headings = {
995
+ 1: [
996
+ "mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl",
997
+ "-ml-[10px]"
998
+ ],
999
+ 2: [
1000
+ "mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl",
1001
+ "-ml-[8px]"
1002
+ ],
1003
+ 3: [
1004
+ "mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl",
1005
+ "-ml-[7px]"
1006
+ ],
1007
+ 4: [
1008
+ "mbs-4 mbe-2 font-medium text-inherit no-underline text-xl",
1009
+ "-ml-[6px]"
1010
+ ],
1011
+ 5: [
1012
+ "mbs-4 mbe-2 font-medium text-inherit no-underline text-lg",
1013
+ "-ml-[5px]"
1014
+ ],
1015
+ 6: [
1016
+ "mbs-4 mbe-2 font-medium text-inherit no-underline",
1017
+ "-ml-[4px]"
1018
+ ]
1019
+ };
1020
+ var heading = (level, readonly) => {
1021
+ const [style, offset] = headings[level];
1022
+ return mx(style, readonly && offset);
1023
+ };
1024
+ var light = "text-neutral-200 dark:text-neutral-800";
1025
+ var mark = mx("!font-normal !no-underline !text-inherit opacity-40", light);
1026
+ var bold = "font-bold";
1027
+ var italic = "italic";
1028
+ var strikethrough = "line-through";
1029
+ var code = "font-mono !no-underline text-neutral-700 dark:text-neutral-300";
1030
+ var codeMark = "font-mono text-primary-500";
1031
+ var inlineUrl = mx(code, "px-1");
1032
+ var blockquote = mx("pl-1 mr-1 border-is-4 border-primary-500/70 dark:border-primary-500/30", light);
1033
+
1034
+ // packages/ui/react-ui-editor/src/styles/tokens.ts
1035
+ import get2 from "lodash.get";
1036
+ import { tailwindConfig } from "@dxos/react-ui-theme";
1037
+ var tokens = tailwindConfig({}).theme;
1038
+ var getToken = (path, defaultValue = void 0) => get2(tokens, path, defaultValue);
1039
+
1040
+ // packages/ui/react-ui-editor/src/extensions/code.ts
1041
+ var CODE_REGEX = /```[\s\S]*?```/gs;
1042
+ var styles2 = EditorView5.baseTheme({
1043
+ "& .cm-codeblock": {
1044
+ fontFamily: getToken("fontFamily.mono", []).join(",")
1045
+ },
1046
+ '&light [aria-readonly="true"] .cm-codeblock': {
1047
+ background: getToken("extend.colors.neutral.50")
1048
+ },
1049
+ '&dark [aria-readonly="true"] .cm-codeblock': {
1050
+ background: getToken("extend.colors.neutral.850")
1051
+ },
1052
+ '& [aria-readonly="true"] .cm-codeblock': {
1053
+ display: "block",
1054
+ paddingInline: "4px !important"
1055
+ },
1056
+ '& [aria-readonly="true"] .cm-codeblock-first': {
1057
+ paddingTop: "4px !important",
1058
+ borderTopLeftRadius: "4px",
1059
+ borderTopRightRadius: "4px"
1060
+ },
1061
+ '& [aria-readonly="true"] .cm-codeblock-last': {
1062
+ paddingBottom: "4px !important",
1063
+ borderBottomLeftRadius: "4px",
1064
+ borderBottomRightRadius: "4px"
879
1065
  }
880
- _readMessage(message) {
881
- const decoder = decoding.createDecoder(message);
882
- const encoder = encoding.createEncoder();
883
- const messageType = decoding.readVarUint(decoder);
884
- let sendReply = false;
885
- switch (messageType) {
886
- case messageAwareness: {
887
- awarenessProtocol.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
888
- break;
1066
+ });
1067
+ var getLineRange = (lines, from, to) => {
1068
+ const start = lines.findIndex((line) => line.from >= from);
1069
+ const end = lines.findIndex((line) => line.to >= to);
1070
+ return [
1071
+ start,
1072
+ end
1073
+ ];
1074
+ };
1075
+ var code2 = () => {
1076
+ const buildDecorations5 = (view) => {
1077
+ const decorations = [];
1078
+ const text = view.state.doc.sliceString(0);
1079
+ const matches = text.matchAll(CODE_REGEX);
1080
+ const blocks = view.viewportLineBlocks;
1081
+ for (const match of matches) {
1082
+ const range = getLineRange(blocks, match.index, match.index + match[0].length);
1083
+ for (let i = range[0]; i <= range[1]; i++) {
1084
+ const block = blocks[i];
1085
+ decorations.push(Decoration2.line({
1086
+ class: mx2("cm-codeblock", i === range[0] && "cm-codeblock-first", i === range[1] && "cm-codeblock-last")
1087
+ }).range(block.from));
889
1088
  }
890
- case messageQueryAwareness: {
891
- encoding.writeVarUint(encoder, messageAwareness);
892
- encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())));
893
- sendReply = true;
894
- break;
1089
+ }
1090
+ return Decoration2.set(decorations);
1091
+ };
1092
+ return [
1093
+ ViewPlugin3.fromClass(class {
1094
+ constructor(view) {
1095
+ this.hasFocus = false;
1096
+ this.decorations = buildDecorations5(view);
895
1097
  }
896
- default: {
897
- console.error("Invalid message:", messageType);
898
- return encoder;
1098
+ update(update2) {
1099
+ if (
1100
+ // TODO(burdon): Generalize for other extensions.
1101
+ this.hasFocus !== update2.view.hasFocus || update2.startState.readOnly !== update2.view.state.readOnly || update2.docChanged || update2.viewportChanged
1102
+ ) {
1103
+ this.hasFocus = update2.view.hasFocus;
1104
+ this.decorations = buildDecorations5(update2.view);
1105
+ }
899
1106
  }
900
- }
901
- if (!sendReply) {
902
- return null;
903
- }
904
- return encoder;
905
- }
906
- _handleBeforeUnload() {
907
- awarenessProtocol.removeAwarenessStates(this._awareness, [
908
- this._clientId
909
- ], "window unload");
910
- }
1107
+ }, {
1108
+ decorations: (value) => value.decorations
1109
+ }),
1110
+ styles2
1111
+ ];
911
1112
  };
912
1113
 
913
- // packages/ui/react-ui-editor/src/hooks/useTextModel.ts
914
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/hooks/useTextModel.ts";
915
- var modelState = StateField2.define({
916
- create: () => void 0,
917
- update: (model) => model
918
- });
919
- var useTextModel = (props) => {
920
- const { identity, space, text } = props;
921
- const [model, setModel] = useState(() => createModel(props));
922
- useEffect(() => setModel(createModel(props)), [
923
- identity,
924
- space,
925
- text
926
- ]);
927
- return model;
1114
+ // packages/ui/react-ui-editor/src/extensions/comments.ts
1115
+ import { StateEffect as StateEffect2, StateField as StateField2 } from "@codemirror/state";
1116
+ import { hoverTooltip, keymap as keymap3, Decoration as Decoration3, EditorView as EditorView6 } from "@codemirror/view";
1117
+ import sortBy from "lodash.sortby";
1118
+ import { useEffect } from "react";
1119
+ import { debounce } from "@dxos/async";
1120
+ import { invariant as invariant3 } from "@dxos/invariant";
1121
+ import { nonNullable } from "@dxos/util";
1122
+
1123
+ // packages/ui/react-ui-editor/src/util.ts
1124
+ import { log } from "@dxos/log";
1125
+ var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/util.ts";
1126
+ var callbackWrapper = (fn) => (...args) => {
1127
+ try {
1128
+ return fn(...args);
1129
+ } catch (err) {
1130
+ log.catch(err, void 0, {
1131
+ F: __dxlog_file3,
1132
+ L: 18,
1133
+ S: void 0,
1134
+ C: (f, a) => f(...a)
1135
+ });
1136
+ }
928
1137
  };
929
- var createModel = (props) => {
930
- const { text } = props;
931
- if (isActualAutomergeObject(text)) {
932
- return createAutomergeModel(props);
933
- } else {
934
- if (!text?.doc) {
1138
+ var logChanges = (trs) => {
1139
+ const changes = trs.flatMap((tr) => {
1140
+ if (tr.changes.empty) {
935
1141
  return void 0;
936
1142
  }
937
- return createYjsModel(props);
1143
+ const changes2 = [];
1144
+ tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => changes2.push(JSON.stringify({
1145
+ fromA,
1146
+ toA,
1147
+ fromB,
1148
+ toB,
1149
+ inserted: inserted.toString()
1150
+ })));
1151
+ return changes2;
1152
+ }).filter(Boolean);
1153
+ if (changes.length) {
1154
+ console.log("changes", changes);
938
1155
  }
939
1156
  };
940
- var createYjsModel = ({ identity, space, text }) => {
941
- invariant2(text?.doc && text?.content, void 0, {
942
- F: __dxlog_file4,
943
- L: 97,
944
- S: void 0,
945
- A: [
946
- "text?.doc && text?.content",
947
- ""
948
- ]
949
- });
950
- const provider = space ? new SpaceAwarenessProvider({
951
- space,
952
- doc: text.doc,
953
- channel: `yjs.awareness.${text.id}`
954
- }) : void 0;
955
- const model = {
956
- id: text.doc.guid,
957
- content: text.content,
958
- text: () => text.content.toString(),
959
- // https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
960
- getCursorFromRange: (range) => {
961
- const from = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content, range.from));
962
- const to = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content, range.to, -1));
963
- return [
964
- arrayToString(from),
965
- arrayToString(to)
966
- ].join(":");
967
- },
968
- getRangeFromCursor: (cursor) => {
969
- const parts = cursor.split(":");
970
- const from = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(parts[0])), text.doc);
971
- const to = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(parts[1])), text.doc);
972
- return {
973
- from: from.index,
974
- to: to.index
975
- };
976
- },
977
- extension: [
978
- yCollab(text.content, provider?.awareness),
979
- modelState.init(() => model)
980
- ],
981
- awareness: provider?.awareness,
982
- peer: identity ? {
983
- id: identity.identityKey.toHex(),
984
- name: identity.profile?.displayName
985
- } : void 0
986
- };
987
- return model;
988
- };
989
- var createAutomergeModel = ({ identity, text }) => {
990
- const obj = text;
991
- const doc = getRawDoc(obj, [
992
- obj.field
993
- ]);
994
- const model = {
995
- id: obj.id,
996
- content: doc,
997
- text: () => get2(doc.handle.docSync(), doc.path),
998
- // TODO(burdon): https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
999
- extension: [
1000
- automergePlugin(doc.handle, doc.path),
1001
- modelState.init(() => model)
1002
- ],
1003
- peer: identity ? {
1004
- id: identity.identityKey.toHex(),
1005
- name: identity.profile?.displayName
1006
- } : void 0
1007
- };
1008
- return model;
1009
- };
1010
1157
 
1011
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/comments.ts
1012
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/components/TextEditor/extensions/comments.ts";
1013
- var styles2 = EditorView4.baseTheme({
1014
- "& .cm-bookmark": {
1015
- cursor: "pointer",
1016
- margin: "4px",
1017
- padding: "4px",
1018
- backgroundColor: "yellow"
1019
- },
1020
- "& .cm-bookmark-selected": {
1021
- backgroundColor: "orange"
1022
- },
1158
+ // packages/ui/react-ui-editor/src/extensions/comments.ts
1159
+ var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/extensions/comments.ts";
1160
+ var styles3 = EditorView6.baseTheme({
1023
1161
  "& .cm-comment": {
1024
- backgroundColor: "yellow"
1162
+ backgroundColor: getToken("extend.colors.yellow.50")
1025
1163
  },
1026
- "& .cm-comment-active": {
1027
- backgroundColor: "orange"
1164
+ "& .cm-comment-current": {
1165
+ backgroundColor: getToken("extend.colors.yellow.100")
1028
1166
  }
1029
1167
  });
1030
1168
  var marks = {
1031
- highlight: Decoration2.mark({
1169
+ highlight: Decoration3.mark({
1032
1170
  class: "cm-comment"
1033
1171
  }),
1034
- highlightActive: Decoration2.mark({
1035
- class: "cm-comment-active"
1172
+ highlightActive: Decoration3.mark({
1173
+ class: "cm-comment-current"
1036
1174
  })
1037
1175
  };
1038
- var setCommentRange = StateEffect2.define();
1176
+ var setFocus = (view, id, center = true) => {
1177
+ const comment = view.state.field(commentsState).comments.find((range2) => range2.comment.id === id);
1178
+ if (!comment?.comment.cursor) {
1179
+ return;
1180
+ }
1181
+ const range = Cursor.getRangeFromCursor(view.state, comment.comment.cursor);
1182
+ if (range) {
1183
+ view.dispatch({
1184
+ selection: {
1185
+ anchor: range.from
1186
+ },
1187
+ effects: [
1188
+ //
1189
+ EditorView6.scrollIntoView(range.from, center ? {
1190
+ y: "center"
1191
+ } : void 0),
1192
+ setSelection.of({
1193
+ current: id
1194
+ })
1195
+ ]
1196
+ });
1197
+ }
1198
+ };
1199
+ var setComments = StateEffect2.define();
1039
1200
  var setSelection = StateEffect2.define();
1040
1201
  var setCommentState = StateEffect2.define();
1041
- var commentsStateField = StateField3.define({
1202
+ var commentsState = StateField2.define({
1042
1203
  create: () => ({
1043
- ranges: []
1204
+ comments: [],
1205
+ selection: {}
1044
1206
  }),
1045
1207
  update: (value, tr) => {
1046
1208
  for (const effect of tr.effects) {
1047
1209
  if (effect.is(setSelection)) {
1048
1210
  return {
1049
1211
  ...value,
1050
- ...effect.value
1212
+ selection: effect.value
1051
1213
  };
1052
1214
  }
1053
- if (effect.is(setCommentRange)) {
1054
- const { model, comments: comments2 } = effect.value;
1055
- const ranges = comments2.map((comment) => {
1056
- const range = model.getRangeFromCursor(comment.cursor);
1057
- return {
1058
- ...comment,
1059
- ...range
1215
+ if (effect.is(setComments)) {
1216
+ const comments2 = effect.value.map((comment) => {
1217
+ if (!comment.cursor) {
1218
+ return void 0;
1219
+ }
1220
+ const range = Cursor.getRangeFromCursor(tr.state, comment.cursor);
1221
+ return range && {
1222
+ comment,
1223
+ range
1060
1224
  };
1061
- });
1225
+ }).filter(nonNullable);
1062
1226
  return {
1063
1227
  ...value,
1064
- ranges
1228
+ comments: comments2
1065
1229
  };
1066
1230
  }
1067
1231
  if (effect.is(setCommentState)) {
@@ -1071,143 +1235,134 @@ var commentsStateField = StateField3.define({
1071
1235
  return value;
1072
1236
  }
1073
1237
  });
1074
- var highlightDecorations = EditorView4.decorations.compute([
1075
- commentsStateField
1238
+ var highlightDecorations = EditorView6.decorations.compute([
1239
+ commentsState
1076
1240
  ], (state) => {
1077
- const { active, ranges } = state.field(commentsStateField);
1078
- const decorations = sortBy(ranges ?? [], (range) => range.from)?.flatMap((selection) => {
1079
- if (selection.from === selection.to) {
1241
+ const { selection: { current }, comments: comments2 } = state.field(commentsState);
1242
+ const decorations = sortBy(comments2 ?? [], (range) => range.range.from)?.flatMap((comment) => {
1243
+ const range = comment.range;
1244
+ if (!range || range.from === range.to) {
1245
+ console.warn("Invalid range:", range);
1080
1246
  return void 0;
1081
1247
  }
1082
- const range = {
1083
- from: selection.from,
1084
- to: selection.to
1085
- };
1086
- if (selection.id === active) {
1248
+ if (comment.comment.id === current) {
1087
1249
  return marks.highlightActive.range(range.from, range.to);
1088
1250
  } else {
1089
1251
  return marks.highlight.range(range.from, range.to);
1090
1252
  }
1091
- }).filter(nonNullable) ?? [];
1092
- return Decoration2.set(decorations);
1253
+ }).filter(nonNullable);
1254
+ return Decoration3.set(decorations);
1093
1255
  });
1094
- var BookmarkWidget = class extends WidgetType {
1095
- constructor(_anchor, _id, _handleClick) {
1096
- super();
1097
- this._anchor = _anchor;
1098
- this._id = _id;
1099
- this._handleClick = _handleClick;
1100
- invariant3(this._id, void 0, {
1101
- F: __dxlog_file5,
1102
- L: 150,
1103
- S: this,
1104
- A: [
1105
- "this._id",
1106
- ""
1107
- ]
1108
- });
1109
- }
1110
- eq(other) {
1111
- return this._anchor === other._anchor && this._id === other._id;
1112
- }
1113
- toDOM() {
1114
- const span = document.createElement("span");
1115
- span.className = "cm-bookmark";
1116
- span.textContent = "\u203B";
1117
- if (this._handleClick) {
1118
- span.onclick = () => this._handleClick();
1256
+ var trackPastedComments = (onUpdate) => {
1257
+ let tracked = null;
1258
+ const handleTrack = (event, view) => {
1259
+ const comments2 = view.state.field(commentsState);
1260
+ const { main } = view.state.selection;
1261
+ const selectedRanges = comments2.comments.filter(({ range }) => range.from >= main.from && range.to <= main.to && range.from < range.to);
1262
+ if (!selectedRanges.length) {
1263
+ tracked = null;
1264
+ } else {
1265
+ tracked = {
1266
+ text: view.state.doc.slice(main.from, main.to),
1267
+ comments: selectedRanges.map(({ comment, range }) => ({
1268
+ id: comment.id,
1269
+ from: range.from - main.from,
1270
+ to: range.to - main.from
1271
+ }))
1272
+ };
1119
1273
  }
1120
- return span;
1121
- }
1274
+ };
1275
+ return [
1276
+ EditorView6.domEventHandlers({
1277
+ cut: handleTrack,
1278
+ copy: handleTrack
1279
+ }),
1280
+ // Handle paste.
1281
+ EditorView6.updateListener.of(({ view, state, transactions }) => {
1282
+ if (tracked) {
1283
+ const paste = transactions.find((tr) => tr.isUserEvent("input.paste"));
1284
+ if (paste) {
1285
+ let found = -1;
1286
+ paste.changes.iterChanges((fromA, toA, fromB, toB, text) => {
1287
+ if (text.eq(tracked.text)) {
1288
+ for (let i = transactions.indexOf(paste) + 1; i < transactions.length; i++) {
1289
+ fromB = transactions[i].changes.mapPos(fromB);
1290
+ }
1291
+ found = fromB;
1292
+ }
1293
+ });
1294
+ if (found > -1) {
1295
+ for (const moved of tracked.comments) {
1296
+ const range = {
1297
+ from: found + moved.from,
1298
+ to: found + moved.to
1299
+ };
1300
+ const cursor = Cursor.getCursorFromRange(state, range);
1301
+ onUpdate(moved.id, cursor);
1302
+ }
1303
+ }
1304
+ tracked = null;
1305
+ }
1306
+ }
1307
+ })
1308
+ ];
1122
1309
  };
1123
1310
  var comments = (options = {}) => {
1311
+ const { key: shortcut = "meta-'" } = options;
1124
1312
  const handleSelect = debounce((state) => options.onSelect?.(state), 200);
1125
1313
  const createCommentThread = (view) => {
1126
1314
  invariant3(options.onCreate, void 0, {
1127
- F: __dxlog_file5,
1128
- L: 204,
1315
+ F: __dxlog_file4,
1316
+ L: 259,
1129
1317
  S: void 0,
1130
1318
  A: [
1131
1319
  "options.onCreate",
1132
1320
  ""
1133
1321
  ]
1134
1322
  });
1135
- const { head, from, to } = view.state.selection.main;
1323
+ const { from, to } = view.state.selection.main;
1136
1324
  if (from === to) {
1137
1325
  return false;
1138
1326
  }
1139
- const model = view.state.field(modelState);
1140
- const relPos = model?.getCursorFromRange?.({
1327
+ if (to === view.state.doc.length) {
1328
+ view.dispatch({
1329
+ changes: {
1330
+ from: to,
1331
+ insert: "\n"
1332
+ }
1333
+ });
1334
+ }
1335
+ const cursor = Cursor.getCursorFromRange(view.state, {
1141
1336
  from,
1142
1337
  to
1143
1338
  });
1144
- if (relPos) {
1145
- const id = callbackWrapper(options.onCreate)(relPos);
1339
+ if (cursor) {
1340
+ const id = options.onCreate?.(cursor, view.coordsAtPos(from));
1146
1341
  if (id) {
1147
1342
  view.dispatch({
1148
1343
  effects: setSelection.of({
1149
- active: id
1344
+ current: id
1150
1345
  }),
1151
1346
  selection: {
1152
1347
  anchor: from
1153
1348
  }
1154
1349
  });
1155
- if (options.footnote) {
1156
- const tag = `[^${id}]`;
1157
- view.dispatch({
1158
- changes: {
1159
- from: head,
1160
- insert: tag
1161
- },
1162
- selection: {
1163
- anchor: head + tag.length
1164
- }
1165
- });
1166
- }
1167
1350
  return true;
1168
1351
  }
1169
1352
  }
1170
1353
  return false;
1171
1354
  };
1172
- const bookmarksViewPlugin = ViewPlugin3.fromClass(class BookmarkViewPlugin {
1173
- static {
1174
- // Match markdown footnotes (option).
1175
- // https://www.markdownguide.org/extended-syntax/#footnotes
1176
- this.bookmarkMatcher = new MatchDecorator({
1177
- regexp: /\[\^(\w+)\]/g,
1178
- decoration: (match, view, pos) => {
1179
- const id = match[1];
1180
- return Decoration2.replace({
1181
- id,
1182
- widget: new BookmarkWidget(pos, id)
1183
- });
1184
- }
1185
- });
1186
- }
1187
- constructor(view) {
1188
- this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.createDeco(view);
1189
- }
1190
- update(update4) {
1191
- this.bookmarks = BookmarkViewPlugin.bookmarkMatcher.updateDeco(update4, this.bookmarks);
1192
- }
1193
- }, {
1194
- decorations: (instance) => instance.bookmarks,
1195
- provide: (plugin) => EditorView4.atomicRanges.of((view) => {
1196
- return view.plugin(plugin)?.bookmarks || Decoration2.none;
1197
- })
1198
- });
1199
1355
  return [
1200
- bookmarksViewPlugin,
1201
- commentsStateField,
1356
+ commentsState,
1202
1357
  highlightDecorations,
1203
- styles2,
1358
+ styles3,
1204
1359
  //
1205
1360
  // Keymap.
1206
1361
  //
1207
1362
  options.onCreate ? keymap3.of([
1208
1363
  {
1209
- key: options?.key ?? "meta-'",
1210
- run: createCommentThread
1364
+ key: shortcut,
1365
+ run: callbackWrapper(createCommentThread)
1211
1366
  }
1212
1367
  ]) : [],
1213
1368
  //
@@ -1223,7 +1378,7 @@ var comments = (options = {}) => {
1223
1378
  above: true,
1224
1379
  create: () => {
1225
1380
  const el = document.createElement("div");
1226
- options.onHover?.(el);
1381
+ options.onHover(el, shortcut);
1227
1382
  return {
1228
1383
  dom: el,
1229
1384
  offset: {
@@ -1236,178 +1391,206 @@ var comments = (options = {}) => {
1236
1391
  }
1237
1392
  return null;
1238
1393
  }, {
1239
- hideOnChange: true,
1394
+ // TODO(burdon): Hide on change triggered immediately?
1395
+ // hideOnChange: true,
1240
1396
  hoverTime: 1e3
1241
1397
  }) : [],
1242
1398
  //
1243
- // Monitor cursor movement and text updates.
1244
- // TODO(burdon): Is there a better (finer grained) way to do this?
1399
+ // Track deleted ranges and update ranges for decorations.
1245
1400
  //
1246
- EditorView4.updateListener.of(({ view, state, startState, changes }) => {
1247
- {
1248
- let mod = false;
1249
- const model = state.field(modelState);
1250
- const { active, ranges } = state.field(commentsStateField);
1251
- changes.iterChanges((from, to, from2, to2) => {
1252
- invariant3(model, void 0, {
1253
- F: __dxlog_file5,
1254
- L: 333,
1255
- S: void 0,
1256
- A: [
1257
- "model",
1258
- ""
1259
- ]
1260
- });
1261
- ranges.forEach((range) => {
1262
- if (from2 === to2) {
1263
- const newRange = model.getRangeFromCursor(range.cursor);
1264
- if (newRange.to - newRange.from === 0) {
1265
- log3.info("deleted comment", {
1266
- thread: range.id
1267
- }, {
1268
- F: __dxlog_file5,
1269
- L: 339,
1270
- S: void 0,
1271
- C: (f, a) => f(...a)
1272
- });
1273
- }
1401
+ EditorView6.updateListener.of(({ view, state, changes }) => {
1402
+ let mod = false;
1403
+ const { comments: comments2, ...value } = state.field(commentsState);
1404
+ changes.iterChanges((from, to, from2, to2) => {
1405
+ comments2.forEach(({ comment, range }) => {
1406
+ if (from2 === to2) {
1407
+ const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor);
1408
+ if (!newRange || newRange.to - newRange.from === 0) {
1409
+ options.onDelete?.(comment.id);
1274
1410
  }
1275
- if (from <= range.to) {
1276
- const newRange = model.getRangeFromCursor(range.cursor);
1277
- Object.assign(range, newRange);
1278
- mod = true;
1279
- }
1280
- });
1411
+ }
1412
+ if (from <= range.to) {
1413
+ const newRange = Cursor.getRangeFromCursor(view.state, comment.cursor);
1414
+ Object.assign(range, newRange);
1415
+ mod = true;
1416
+ }
1417
+ });
1418
+ });
1419
+ if (mod) {
1420
+ view.dispatch({
1421
+ effects: setCommentState.of({
1422
+ comments: comments2,
1423
+ ...value
1424
+ })
1281
1425
  });
1282
- if (mod) {
1283
- view.dispatch({
1284
- effects: setCommentState.of({
1285
- active,
1286
- ranges
1287
- })
1288
- });
1289
- }
1290
1426
  }
1291
- {
1292
- const { head } = state.selection.main;
1293
- let min = Infinity;
1294
- const { active, closest, ranges } = state.field(commentsStateField);
1295
- const selected = {
1296
- active: void 0,
1297
- closest: void 0
1298
- };
1299
- ranges.forEach((comment) => {
1300
- const d = Math.min(Math.abs(head - comment.from), Math.abs(head - comment.to));
1301
- if (head >= comment.from && head <= comment.to) {
1302
- selected.active = comment.id;
1303
- }
1427
+ }),
1428
+ //
1429
+ // Track selection/proximity.
1430
+ //
1431
+ EditorView6.updateListener.of(({ view, state }) => {
1432
+ let min = Infinity;
1433
+ const { selection: { current, closest }, comments: comments2 } = state.field(commentsState);
1434
+ const { head } = state.selection.main;
1435
+ const selection = {};
1436
+ comments2.forEach(({ comment, range }) => {
1437
+ if (head >= range.from && head <= range.to) {
1438
+ selection.current = comment.id;
1439
+ selection.closest = void 0;
1440
+ }
1441
+ if (!selection.current) {
1442
+ const d = Math.min(Math.abs(head - range.from), Math.abs(head - range.to));
1304
1443
  if (d < min) {
1305
- selected.closest = comment.id;
1444
+ selection.closest = comment.id;
1306
1445
  min = d;
1307
1446
  }
1308
- });
1309
- if (selected.active !== active || selected.closest !== closest) {
1310
- view.dispatch({
1311
- effects: setSelection.of(selected)
1312
- });
1313
- handleSelect({
1314
- ...selected,
1315
- ranges: ranges.map((range) => ({
1316
- ...range,
1317
- location: view.coordsAtPos(range.from)
1318
- }))
1319
- });
1320
1447
  }
1448
+ });
1449
+ if (selection.current !== current || selection.closest !== closest) {
1450
+ view.dispatch({
1451
+ effects: setSelection.of(selection)
1452
+ });
1453
+ handleSelect({
1454
+ selection,
1455
+ comments: comments2.map(({ comment, range }) => ({
1456
+ comment,
1457
+ range,
1458
+ location: view.coordsAtPos(range.from)
1459
+ }))
1460
+ });
1321
1461
  }
1322
- })
1462
+ }),
1463
+ options.onUpdate ? trackPastedComments(options.onUpdate) : []
1323
1464
  ];
1324
1465
  };
1466
+ var useComments = (view, comments2 = []) => {
1467
+ useEffect(() => {
1468
+ if (view) {
1469
+ view.dispatch({
1470
+ effects: setComments.of(comments2)
1471
+ });
1472
+ }
1473
+ }, [
1474
+ view,
1475
+ comments2
1476
+ ]);
1477
+ };
1325
1478
 
1326
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/hr.ts
1327
- import { Decoration as Decoration3, EditorView as EditorView5, MatchDecorator as MatchDecorator2, ViewPlugin as ViewPlugin4, WidgetType as WidgetType2 } from "@codemirror/view";
1328
- import get3 from "lodash.get";
1329
- var styles3 = EditorView5.baseTheme({
1479
+ // packages/ui/react-ui-editor/src/extensions/hr.ts
1480
+ import { syntaxTree } from "@codemirror/language";
1481
+ import { RangeSetBuilder } from "@codemirror/state";
1482
+ import { Decoration as Decoration4, EditorView as EditorView7, ViewPlugin as ViewPlugin4, WidgetType as WidgetType2 } from "@codemirror/view";
1483
+ var styles4 = EditorView7.baseTheme({
1330
1484
  "& .cm-hr": {
1331
- // TODO(burdon): ???
1332
1485
  // Note that block-level decorations should not have vertical margins,
1333
- borderBottom: `1px solid ${get3(tokens, "extend.colors.neutral.200")}`
1486
+ borderTop: `1px solid ${getToken("extend.colors.neutral.200")}`
1334
1487
  }
1335
1488
  });
1336
1489
  var HorizontalRuleWidget = class extends WidgetType2 {
1337
- constructor(_pos) {
1338
- super();
1339
- this._pos = _pos;
1340
- }
1341
- eq(other) {
1342
- return this._pos === other._pos;
1343
- }
1344
- toDOM(view) {
1490
+ toDOM() {
1345
1491
  const el = document.createElement("div");
1346
1492
  el.className = "cm-hr";
1347
1493
  return el;
1348
1494
  }
1349
1495
  };
1350
- var placeholderMatcher = new MatchDecorator2({
1351
- regexp: /^---$/g,
1352
- decoration: (match, view, pos) => Decoration3.replace({
1353
- widget: new HorizontalRuleWidget(pos)
1354
- })
1496
+ var decoration = Decoration4.replace({
1497
+ widget: new HorizontalRuleWidget()
1355
1498
  });
1356
- var hr = () => [
1357
- styles3,
1358
- ViewPlugin4.fromClass(class {
1359
- constructor(view) {
1360
- this.rules = placeholderMatcher.createDeco(view);
1361
- }
1362
- update(update4) {
1363
- this.rules = placeholderMatcher.updateDeco(update4, this.rules);
1364
- }
1365
- }, {
1366
- decorations: (instance) => instance.rules,
1367
- // TODO(burdon): Is this really atomic (cursor can move into ---).
1368
- provide: (plugin) => EditorView5.atomicRanges.of((view) => {
1369
- return view.plugin(plugin)?.rules || Decoration3.none;
1499
+ var buildDecorations = (view) => {
1500
+ const builder = new RangeSetBuilder();
1501
+ const { state } = view;
1502
+ const cursor = state.selection.main.head;
1503
+ for (const { from, to } of view.visibleRanges) {
1504
+ syntaxTree(state).iterate({
1505
+ enter: (node) => {
1506
+ if (node.name === "HorizontalRule") {
1507
+ if (cursor <= node.from || cursor >= node.to) {
1508
+ builder.add(node.from, node.to, decoration);
1509
+ }
1510
+ }
1511
+ },
1512
+ from,
1513
+ to
1514
+ });
1515
+ }
1516
+ return builder.finish();
1517
+ };
1518
+ var hr = () => {
1519
+ return [
1520
+ styles4,
1521
+ ViewPlugin4.fromClass(class {
1522
+ constructor(view) {
1523
+ this.decorations = buildDecorations(view);
1524
+ }
1525
+ update(update2) {
1526
+ this.decorations = buildDecorations(update2.view);
1527
+ }
1528
+ }, {
1529
+ decorations: (v) => v.decorations
1370
1530
  })
1371
- })
1372
- ];
1531
+ ];
1532
+ };
1373
1533
 
1374
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/image.ts
1375
- import { syntaxTree } from "@codemirror/language";
1376
- import { RangeSetBuilder, StateField as StateField4 } from "@codemirror/state";
1377
- import { Decoration as Decoration4, EditorView as EditorView6, WidgetType as WidgetType3 } from "@codemirror/view";
1534
+ // packages/ui/react-ui-editor/src/extensions/image.ts
1535
+ import { syntaxTree as syntaxTree2 } from "@codemirror/language";
1536
+ import { StateField as StateField3 } from "@codemirror/state";
1537
+ import { Decoration as Decoration5, EditorView as EditorView8, WidgetType as WidgetType3 } from "@codemirror/view";
1378
1538
  var image = (options = {}) => {
1379
- return StateField4.define({
1380
- create: (state) => update(state, options),
1381
- update: (_, tr) => update(tr.state, options),
1382
- provide: (field) => EditorView6.decorations.from(field)
1539
+ return StateField3.define({
1540
+ create: (state) => {
1541
+ return Decoration5.set(buildDecorations2(0, state.doc.length, state));
1542
+ },
1543
+ update: (value, tr) => {
1544
+ if (!tr.docChanged && !tr.selection) {
1545
+ return value;
1546
+ }
1547
+ const cursor = tr.state.selection.main.head;
1548
+ const oldCursor = tr.changes.mapPos(tr.startState.selection.main.head);
1549
+ let from = Math.min(cursor, oldCursor);
1550
+ let to = Math.max(cursor, oldCursor);
1551
+ tr.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
1552
+ from = Math.min(from, fromB);
1553
+ to = Math.max(to, toB);
1554
+ });
1555
+ from = tr.state.doc.lineAt(from).from;
1556
+ to = tr.state.doc.lineAt(to).to;
1557
+ return value.map(tr.changes).update({
1558
+ filterFrom: from,
1559
+ filterTo: to,
1560
+ filter: () => false,
1561
+ add: buildDecorations2(from, to, tr.state)
1562
+ });
1563
+ },
1564
+ provide: (field) => EditorView8.decorations.from(field)
1383
1565
  });
1384
1566
  };
1385
- var update = (state, options) => {
1386
- const builder = new RangeSetBuilder();
1567
+ var buildDecorations2 = (from, to, state) => {
1568
+ const decorations = [];
1387
1569
  const cursor = state.selection.main.head;
1388
- syntaxTree(state).iterate({
1570
+ syntaxTree2(state).iterate({
1389
1571
  enter: (node) => {
1390
1572
  if (node.name === "Image") {
1391
1573
  const urlNode = node.node.getChild("URL");
1392
1574
  if (urlNode) {
1393
1575
  const hide = state.readOnly || cursor < node.from || cursor > node.to;
1394
1576
  const url = state.sliceDoc(urlNode.from, urlNode.to);
1395
- builder.add(hide ? node.from : node.to, node.to, Decoration4.replace({
1577
+ decorations.push(Decoration5.replace({
1396
1578
  block: true,
1397
1579
  widget: new ImageWidget(url)
1398
- }));
1580
+ }).range(hide ? node.from : node.to, node.to));
1399
1581
  }
1400
1582
  }
1401
- }
1583
+ },
1584
+ from,
1585
+ to
1402
1586
  });
1403
- return builder.finish();
1587
+ return decorations;
1404
1588
  };
1405
1589
  var ImageWidget = class extends WidgetType3 {
1406
1590
  constructor(_url) {
1407
1591
  super();
1408
1592
  this._url = _url;
1409
1593
  }
1410
- // TODO(burdon): Does this need to be unique for each position?
1411
1594
  eq(other) {
1412
1595
  return this._url === other._url;
1413
1596
  }
@@ -1415,30 +1598,77 @@ var ImageWidget = class extends WidgetType3 {
1415
1598
  const img = document.createElement("img");
1416
1599
  img.setAttribute("src", this._url);
1417
1600
  img.setAttribute("class", "cm-image");
1601
+ img.onload = () => img.classList.add("cm-loaded-image");
1418
1602
  return img;
1419
1603
  }
1420
- };
1421
-
1422
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/link.ts
1423
- import { syntaxTree as syntaxTree2 } from "@codemirror/language";
1424
- import { RangeSetBuilder as RangeSetBuilder2, StateField as StateField5 } from "@codemirror/state";
1425
- import { Decoration as Decoration5, EditorView as EditorView7, WidgetType as WidgetType4 } from "@codemirror/view";
1426
- var link = (options = {}) => {
1427
- return StateField5.define({
1428
- create: (state) => update2(state, options),
1429
- update: (_, tr) => update2(tr.state, options),
1430
- provide: (field) => EditorView7.decorations.from(field)
1431
- });
1604
+ };
1605
+
1606
+ // packages/ui/react-ui-editor/src/extensions/link.ts
1607
+ import { syntaxTree as syntaxTree3 } from "@codemirror/language";
1608
+ import { RangeSetBuilder as RangeSetBuilder2 } from "@codemirror/state";
1609
+ import { hoverTooltip as hoverTooltip2, Decoration as Decoration6, ViewPlugin as ViewPlugin5, WidgetType as WidgetType4 } from "@codemirror/view";
1610
+ import { tooltipContent } from "@dxos/react-ui-theme";
1611
+ var link = (options = {}) => {
1612
+ const extensions = [
1613
+ ViewPlugin5.fromClass(class {
1614
+ constructor(view) {
1615
+ this.decorations = buildDecorations3(view, options);
1616
+ }
1617
+ update(update2) {
1618
+ if (update2.docChanged || update2.viewportChanged || update2.selectionSet) {
1619
+ this.decorations = buildDecorations3(update2.view, options);
1620
+ }
1621
+ }
1622
+ }, {
1623
+ decorations: (v) => v.decorations
1624
+ })
1625
+ ];
1626
+ if (options.onHover) {
1627
+ extensions.push(
1628
+ // https://codemirror.net/examples/tooltip
1629
+ // https://codemirror.net/docs/ref/#view.hoverTooltip
1630
+ // https://github.com/codemirror/view/blob/main/src/tooltip.ts
1631
+ hoverTooltip2((view, pos, side) => {
1632
+ const syntax = syntaxTree3(view.state).resolveInner(pos, side);
1633
+ let link2 = null;
1634
+ for (let i = 0, node = syntax; !link2 && node && i < 5; node = node.parent, i++) {
1635
+ link2 = node.name === "Link" ? node : null;
1636
+ }
1637
+ const url = link2 && link2.getChild("URL");
1638
+ if (!url || !link2) {
1639
+ return null;
1640
+ }
1641
+ const urlText = view.state.sliceDoc(url.from, url.to);
1642
+ return {
1643
+ pos: link2.from,
1644
+ end: link2.to,
1645
+ above: true,
1646
+ create: () => {
1647
+ const el = document.createElement("div");
1648
+ el.className = tooltipContent({}, "pli-2 plb-1");
1649
+ options.onHover(el, urlText);
1650
+ return {
1651
+ dom: el,
1652
+ offset: {
1653
+ x: 0,
1654
+ y: 4
1655
+ }
1656
+ };
1657
+ }
1658
+ };
1659
+ })
1660
+ );
1661
+ }
1662
+ return extensions;
1432
1663
  };
1433
1664
  var LinkButton = class extends WidgetType4 {
1434
- constructor(_pos, _url, _onAttach) {
1665
+ constructor(_url, _onAttach) {
1435
1666
  super();
1436
- this._pos = _pos;
1437
1667
  this._url = _url;
1438
1668
  this._onAttach = _onAttach;
1439
1669
  }
1440
1670
  eq(other) {
1441
- return this._pos === other._pos && this._url === other._url;
1671
+ return this._url === other._url;
1442
1672
  }
1443
1673
  toDOM(view) {
1444
1674
  const el = document.createElement("span");
@@ -1447,14 +1677,13 @@ var LinkButton = class extends WidgetType4 {
1447
1677
  }
1448
1678
  };
1449
1679
  var LinkText = class extends WidgetType4 {
1450
- constructor(_pos, _text, _url) {
1680
+ constructor(_text, _url) {
1451
1681
  super();
1452
- this._pos = _pos;
1453
1682
  this._text = _text;
1454
1683
  this._url = _url;
1455
1684
  }
1456
1685
  eq(other) {
1457
- return this._pos === other._pos && this._url === other._url;
1686
+ return this._url === other._url;
1458
1687
  }
1459
1688
  toDOM(view) {
1460
1689
  const link2 = document.createElement("a");
@@ -1468,7 +1697,7 @@ var LinkText = class extends WidgetType4 {
1468
1697
  link2.onclick = () => {
1469
1698
  view.dispatch({
1470
1699
  selection: {
1471
- anchor: this._pos
1700
+ anchor: view.posAtDOM(link2)
1472
1701
  }
1473
1702
  });
1474
1703
  };
@@ -1476,74 +1705,77 @@ var LinkText = class extends WidgetType4 {
1476
1705
  return link2;
1477
1706
  }
1478
1707
  };
1479
- var update2 = (state, options) => {
1708
+ var buildDecorations3 = (view, options) => {
1480
1709
  const builder = new RangeSetBuilder2();
1710
+ const { state } = view;
1481
1711
  const cursor = state.selection.main.head;
1482
- syntaxTree2(state).iterate({
1483
- enter: (node) => {
1484
- if (node.name === "Link") {
1485
- const marks2 = node.node.getChildren("LinkMark");
1486
- const text = marks2.length >= 2 ? state.sliceDoc(marks2[0].to, marks2[1].from) : "";
1487
- const urlNode = node.node.getChild("URL");
1488
- const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : "";
1489
- if (!url) {
1712
+ for (const { from, to } of view.visibleRanges) {
1713
+ syntaxTree3(state).iterate({
1714
+ enter: (node) => {
1715
+ if (node.name === "Link") {
1716
+ const marks2 = node.node.getChildren("LinkMark");
1717
+ const text = marks2.length >= 2 ? state.sliceDoc(marks2[0].to, marks2[1].from) : "";
1718
+ const urlNode = node.node.getChild("URL");
1719
+ const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : "";
1720
+ if (!url) {
1721
+ return false;
1722
+ }
1723
+ if (!view.hasFocus || state.readOnly || cursor < node.from || cursor > node.to) {
1724
+ builder.add(node.from, node.to, Decoration6.replace({
1725
+ widget: new LinkText(text, options.link ? url : void 0)
1726
+ }));
1727
+ }
1728
+ if (options.onRender) {
1729
+ builder.add(node.to, node.to, Decoration6.replace({
1730
+ widget: new LinkButton(url, options.onRender)
1731
+ }));
1732
+ }
1490
1733
  return false;
1491
1734
  }
1492
- if (state.readOnly || cursor < node.from || cursor > node.to) {
1493
- builder.add(node.from, node.to, Decoration5.replace({
1494
- widget: new LinkText(node.from, text, options.link ? url : void 0)
1495
- }));
1496
- }
1497
- if (options.onRender) {
1498
- builder.add(node.to, node.to, Decoration5.replace({
1499
- widget: new LinkButton(node.from, url, options.onRender)
1500
- }));
1501
- }
1502
- return false;
1503
- }
1504
- }
1505
- });
1735
+ },
1736
+ from,
1737
+ to
1738
+ });
1739
+ }
1506
1740
  return builder.finish();
1507
1741
  };
1508
1742
 
1509
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/listener.ts
1510
- import { StateField as StateField6 } from "@codemirror/state";
1511
- var listener = ({ onChange }) => {
1512
- return StateField6.define({
1513
- create: () => null,
1514
- update: (_value, transaction) => {
1515
- if (transaction.docChanged && onChange) {
1516
- onChange(transaction.newDoc.toString());
1517
- }
1518
- return null;
1519
- }
1520
- });
1743
+ // packages/ui/react-ui-editor/src/extensions/listener.ts
1744
+ import { EditorView as EditorView9 } from "@codemirror/view";
1745
+ var listener = ({ onFocus, onChange }) => {
1746
+ const extensions = [];
1747
+ onFocus && extensions.push(EditorView9.focusChangeEffect.of((_, focused) => {
1748
+ onFocus(focused);
1749
+ return null;
1750
+ }));
1751
+ onChange && extensions.push(EditorView9.updateListener.of((update2) => {
1752
+ onChange(update2.state.doc.toString());
1753
+ }));
1754
+ return extensions;
1521
1755
  };
1522
1756
 
1523
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/markdown/bundle.ts
1757
+ // packages/ui/react-ui-editor/src/extensions/markdown/bundle.ts
1524
1758
  import { closeBrackets as closeBrackets2, closeBracketsKeymap } from "@codemirror/autocomplete";
1525
- import { history, historyKeymap, indentWithTab, standardKeymap } from "@codemirror/commands";
1526
- import { markdownLanguage as markdownLanguage2, markdown } from "@codemirror/lang-markdown";
1759
+ import { history as history2, historyKeymap, indentWithTab, standardKeymap } from "@codemirror/commands";
1760
+ import { markdownLanguage as markdownLanguage3, markdown } from "@codemirror/lang-markdown";
1527
1761
  import { bracketMatching as bracketMatching2, defaultHighlightStyle as defaultHighlightStyle2, indentOnInput, syntaxHighlighting as syntaxHighlighting2 } from "@codemirror/language";
1528
1762
  import { languages } from "@codemirror/language-data";
1529
1763
  import { searchKeymap } from "@codemirror/search";
1530
1764
  import { EditorState } from "@codemirror/state";
1531
1765
  import { oneDarkHighlightStyle as oneDarkHighlightStyle2 } from "@codemirror/theme-one-dark";
1532
- import { crosshairCursor, drawSelection as drawSelection2, dropCursor, EditorView as EditorView8, highlightActiveLine, keymap as keymap4, placeholder as placeholder2 } from "@codemirror/view";
1766
+ import { crosshairCursor, drawSelection as drawSelection2, dropCursor, EditorView as EditorView10, highlightActiveLine as highlightActiveLine2, keymap as keymap4, placeholder as placeholder2 } from "@codemirror/view";
1533
1767
 
1534
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/markdown/highlight.ts
1535
- import { markdownLanguage } from "@codemirror/lang-markdown";
1768
+ // packages/ui/react-ui-editor/src/extensions/markdown/highlight.ts
1769
+ import { markdownLanguage as markdownLanguage2 } from "@codemirror/lang-markdown";
1536
1770
  import { HighlightStyle } from "@codemirror/language";
1537
1771
  import { tags, styleTags, Tag } from "@lezer/highlight";
1538
1772
  import { Table } from "@lezer/markdown";
1539
- import get4 from "lodash.get";
1540
1773
  var markdownTags = {
1541
1774
  Blockquote: Tag.define(),
1542
1775
  CodeMark: Tag.define(),
1543
1776
  CodeText: Tag.define(),
1544
1777
  EmphasisMark: Tag.define(),
1545
1778
  HeaderMark: Tag.define(),
1546
- // HorizontalRule: Tag.define(),
1547
1779
  InlineCode: Tag.define(),
1548
1780
  LinkLabel: Tag.define(),
1549
1781
  LinkReference: Tag.define(),
@@ -1707,12 +1939,6 @@ var markdownHighlightStyle = (readonly) => {
1707
1939
  ],
1708
1940
  class: blockquote
1709
1941
  },
1710
- // TODO(burdon): Replace with extension.
1711
- // {
1712
- // tag: [markdownTags.HorizontalRule],
1713
- // class: mx(horizontalRule, readonly && '-indent-[100rem]'),
1714
- // },
1715
- // TODO(burdon): Only if being edited.
1716
1942
  {
1717
1943
  tag: [
1718
1944
  markdownTags.TableCell
@@ -1720,29 +1946,29 @@ var markdownHighlightStyle = (readonly) => {
1720
1946
  class: "font-mono"
1721
1947
  }
1722
1948
  ], {
1723
- scope: markdownLanguage,
1949
+ scope: markdownLanguage2,
1724
1950
  all: {
1725
- fontFamily: get4(tokens, "fontFamily.body", []).join(",")
1951
+ fontFamily: getToken("fontFamily.body", []).join(",")
1726
1952
  }
1727
1953
  });
1728
1954
  };
1729
1955
 
1730
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/markdown/bundle.ts
1956
+ // packages/ui/react-ui-editor/src/extensions/markdown/bundle.ts
1731
1957
  var markdownBundle = ({ readonly, themeMode, placeholder: _placeholder }) => {
1732
1958
  return [
1733
1959
  EditorState.allowMultipleSelections.of(true),
1734
1960
  EditorState.tabSize.of(2),
1735
- EditorView8.lineWrapping,
1736
- customKeymap(),
1961
+ EditorView10.lineWrapping,
1962
+ // https://github.com/codemirror/basic-setup
1737
1963
  bracketMatching2(),
1738
1964
  closeBrackets2(),
1739
1965
  crosshairCursor(),
1740
1966
  dropCursor(),
1741
1967
  drawSelection2(),
1742
- highlightActiveLine(),
1743
- history(),
1968
+ highlightActiveLine2(),
1969
+ history2(),
1744
1970
  indentOnInput(),
1745
- _placeholder && placeholder2(_placeholder),
1971
+ _placeholder ? placeholder2(_placeholder) : [],
1746
1972
  // Main extension.
1747
1973
  // https://github.com/codemirror/lang-markdown
1748
1974
  // https://codemirror.net/5/mode/markdown/index.html (demo).
@@ -1752,7 +1978,7 @@ var markdownBundle = ({ readonly, themeMode, placeholder: _placeholder }) => {
1752
1978
  // NOTE: This extends the parser; it doesn't affect rendering.
1753
1979
  // https://github.github.com/gfm
1754
1980
  // https://github.com/lezer-parser/markdown?tab=readme-ov-file#github-flavored-markdown
1755
- base: markdownLanguage2,
1981
+ base: markdownLanguage3,
1756
1982
  // Languages for syntax highlighting fenced code blocks.
1757
1983
  codeLanguages: languages,
1758
1984
  // Parser extensions.
@@ -1764,34 +1990,40 @@ var markdownBundle = ({ readonly, themeMode, placeholder: _placeholder }) => {
1764
1990
  // https://github.com/codemirror/theme-one-dark
1765
1991
  themeMode === "dark" ? syntaxHighlighting2(oneDarkHighlightStyle2) : syntaxHighlighting2(defaultHighlightStyle2),
1766
1992
  // Custom styles.
1767
- syntaxHighlighting2(markdownHighlightStyle(readonly))
1768
- ].filter(Boolean);
1769
- };
1770
- var customKeymap = () => keymap4.of([
1771
- // https://codemirror.net/docs/ref/#commands.indentWithTab
1772
- indentWithTab,
1773
- // https://codemirror.net/docs/ref/#commands.standardKeymap
1774
- ...standardKeymap,
1775
- // https://codemirror.net/docs/ref/#commands.historyKeymap
1776
- ...historyKeymap,
1777
- // https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
1778
- ...closeBracketsKeymap,
1779
- // https://codemirror.net/docs/ref/#search.searchKeymap
1780
- ...searchKeymap
1781
- ]);
1993
+ syntaxHighlighting2(markdownHighlightStyle(readonly)),
1994
+ keymap4.of([
1995
+ // https://codemirror.net/docs/ref/#commands.indentWithTab
1996
+ indentWithTab,
1997
+ // https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
1998
+ ...closeBracketsKeymap,
1999
+ // https://codemirror.net/docs/ref/#commands.historyKeymap
2000
+ ...historyKeymap,
2001
+ // https://codemirror.net/docs/ref/#search.searchKeymap
2002
+ ...searchKeymap,
2003
+ // https://codemirror.net/docs/ref/#commands.standardKeymap
2004
+ ...standardKeymap
2005
+ ])
2006
+ ];
2007
+ };
1782
2008
 
1783
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/mention.ts
2009
+ // packages/ui/react-ui-editor/src/extensions/mention.ts
1784
2010
  import { autocompletion as autocompletion2 } from "@codemirror/autocomplete";
1785
2011
  var mention = ({ onSearch }) => {
1786
2012
  return autocompletion2({
2013
+ // TODO(burdon): Not working.
2014
+ activateOnTyping: true,
2015
+ // activateOnTypingDelay: 100,
2016
+ // selectOnOpen: true,
1787
2017
  closeOnBlur: false,
2018
+ // defaultKeymap: false,
2019
+ icons: false,
1788
2020
  override: [
1789
2021
  (context) => {
2022
+ console.log(context);
1790
2023
  const match = context.matchBefore(/@(\w+)?/);
1791
2024
  if (!match || match.from === match.to && !context.explicit) {
1792
2025
  return null;
1793
2026
  }
1794
- console.log(match);
1795
2027
  return {
1796
2028
  from: match.from,
1797
2029
  options: onSearch(match.text.slice(1).toLowerCase()).map((value) => ({
@@ -1803,23 +2035,23 @@ var mention = ({ onSearch }) => {
1803
2035
  });
1804
2036
  };
1805
2037
 
1806
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/mermaid.ts
1807
- var mermaid = () => {
2038
+ // packages/ui/react-ui-editor/src/extensions/outliner.ts
2039
+ var outliner = (options = {}) => {
1808
2040
  return [];
1809
2041
  };
1810
2042
 
1811
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/table.ts
1812
- import { syntaxTree as syntaxTree3 } from "@codemirror/language";
1813
- import { RangeSetBuilder as RangeSetBuilder3, StateField as StateField7 } from "@codemirror/state";
1814
- import { Decoration as Decoration6, EditorView as EditorView9, WidgetType as WidgetType5 } from "@codemirror/view";
2043
+ // packages/ui/react-ui-editor/src/extensions/table.ts
2044
+ import { syntaxTree as syntaxTree4 } from "@codemirror/language";
2045
+ import { RangeSetBuilder as RangeSetBuilder3, StateField as StateField4 } from "@codemirror/state";
2046
+ import { Decoration as Decoration7, EditorView as EditorView11, WidgetType as WidgetType5 } from "@codemirror/view";
1815
2047
  var table = (options = {}) => {
1816
- return StateField7.define({
1817
- create: (state) => update3(state, options),
1818
- update: (_, tr) => update3(tr.state, options),
1819
- provide: (field) => EditorView9.decorations.from(field)
2048
+ return StateField4.define({
2049
+ create: (state) => update(state, options),
2050
+ update: (_, tr) => update(tr.state, options),
2051
+ provide: (field) => EditorView11.decorations.from(field)
1820
2052
  });
1821
2053
  };
1822
- var update3 = (state, options) => {
2054
+ var update = (state, options) => {
1823
2055
  const builder = new RangeSetBuilder3();
1824
2056
  const cursor = state.selection.main.head;
1825
2057
  const tables = [];
@@ -1828,7 +2060,7 @@ var update3 = (state, options) => {
1828
2060
  const table2 = getTable();
1829
2061
  return table2.rows?.[table2.rows.length - 1];
1830
2062
  };
1831
- syntaxTree3(state).iterate({
2063
+ syntaxTree4(state).iterate({
1832
2064
  enter: (node) => {
1833
2065
  switch (node.name) {
1834
2066
  case "Table": {
@@ -1860,11 +2092,10 @@ var update3 = (state, options) => {
1860
2092
  });
1861
2093
  tables.forEach((table2) => {
1862
2094
  const hide = state.readOnly || cursor < table2.from || cursor > table2.to;
1863
- hide && builder.add(table2.from, table2.to, Decoration6.replace({
1864
- block: true,
2095
+ hide && builder.add(table2.from, table2.to, Decoration7.replace({
1865
2096
  widget: new TableWidget(table2)
1866
2097
  }));
1867
- builder.add(table2.from, table2.to, Decoration6.mark({
2098
+ builder.add(table2.from, table2.to, Decoration7.mark({
1868
2099
  class: "cm-table"
1869
2100
  }));
1870
2101
  });
@@ -1876,7 +2107,7 @@ var TableWidget = class extends WidgetType5 {
1876
2107
  this._table = _table;
1877
2108
  }
1878
2109
  eq(other) {
1879
- return this._table.from === other?._table?.from;
2110
+ return this._table.header?.join() === other._table.header?.join() && this._table.rows?.join() === other._table.rows?.join();
1880
2111
  }
1881
2112
  toDOM(view) {
1882
2113
  const table2 = document.createElement("table");
@@ -1902,138 +2133,109 @@ var TableWidget = class extends WidgetType5 {
1902
2133
  }
1903
2134
  return table2;
1904
2135
  }
2136
+ ignoreEvent(e) {
2137
+ return !/^mouse/.test(e.type);
2138
+ }
1905
2139
  };
1906
2140
 
1907
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/tasklist.ts
1908
- import { EditorView as EditorView10, Decoration as Decoration7, WidgetType as WidgetType6, MatchDecorator as MatchDecorator3, ViewPlugin as ViewPlugin5 } from "@codemirror/view";
1909
- var styles4 = EditorView10.baseTheme({
1910
- "& .cm-task-item": {
1911
- paddingLeft: "4px",
1912
- paddingRight: "8px"
2141
+ // packages/ui/react-ui-editor/src/extensions/tasklist.ts
2142
+ import { syntaxTree as syntaxTree5 } from "@codemirror/language";
2143
+ import { RangeSetBuilder as RangeSetBuilder4 } from "@codemirror/state";
2144
+ import { EditorView as EditorView12, Decoration as Decoration8, WidgetType as WidgetType6, ViewPlugin as ViewPlugin6 } from "@codemirror/view";
2145
+ var styles5 = EditorView12.baseTheme({
2146
+ "& .cm-task": {
2147
+ color: getToken("extend.colors.blue.500")
2148
+ },
2149
+ "& .cm-task-checkbox": {
2150
+ marginLeft: "4px",
2151
+ marginRight: "4px"
1913
2152
  }
1914
2153
  });
1915
2154
  var CheckboxWidget = class extends WidgetType6 {
1916
- constructor(_pos, _checked, _indent, _onCheck) {
2155
+ constructor(_checked) {
1917
2156
  super();
1918
- this._pos = _pos;
1919
2157
  this._checked = _checked;
1920
- this._indent = _indent;
1921
- this._onCheck = _onCheck;
1922
2158
  }
1923
2159
  eq(other) {
1924
- return this._pos === other._pos && this._checked === other._checked && this._indent === other._indent;
2160
+ return this._checked === other._checked;
1925
2161
  }
1926
2162
  toDOM(view) {
1927
- const wrap = document.createElement("span");
1928
- wrap.className = "cm-task-item";
1929
- wrap.setAttribute("aria-hidden", "true");
1930
- wrap.style.setProperty("margin-left", this._indent * 24 + "px");
1931
- const input = wrap.appendChild(document.createElement("input"));
2163
+ const input = document.createElement("input");
2164
+ input.className = "cm-task-checkbox";
1932
2165
  input.type = "checkbox";
1933
2166
  input.checked = this._checked;
1934
- if (!this._onCheck) {
2167
+ if (view.state.readOnly) {
1935
2168
  input.setAttribute("disabled", "true");
1936
- }
1937
- if (this._onCheck) {
1938
- input.onchange = (event) => {
1939
- this._onCheck?.(event.target.checked);
1940
- return true;
2169
+ } else {
2170
+ input.onmousedown = (event) => {
2171
+ const pos = view.posAtDOM(input);
2172
+ const text = view.state.sliceDoc(pos, pos + 3);
2173
+ if (text === (this._checked ? "[x]" : "[ ]")) {
2174
+ view.dispatch({
2175
+ changes: {
2176
+ from: pos + 1,
2177
+ to: pos + 2,
2178
+ insert: this._checked ? " " : "x"
2179
+ }
2180
+ });
2181
+ event.preventDefault();
2182
+ }
1941
2183
  };
1942
2184
  }
1943
- return wrap;
2185
+ return input;
1944
2186
  }
1945
2187
  ignoreEvent() {
1946
2188
  return false;
1947
2189
  }
1948
2190
  };
2191
+ var checkedDecoration = Decoration8.replace({
2192
+ widget: new CheckboxWidget(true)
2193
+ });
2194
+ var uncheckedDecoration = Decoration8.replace({
2195
+ widget: new CheckboxWidget(false)
2196
+ });
2197
+ var buildDecorations4 = (view) => {
2198
+ const builder = new RangeSetBuilder4();
2199
+ const { state } = view;
2200
+ const cursor = state.selection.main.head;
2201
+ for (const { from, to } of view.visibleRanges) {
2202
+ syntaxTree5(state).iterate({
2203
+ enter: (node) => {
2204
+ if (node.name === "TaskMarker") {
2205
+ if (cursor < node.from || cursor > node.to) {
2206
+ const checked = state.doc.sliceString(node.from + 1, node.to - 1) === "x";
2207
+ builder.add(node.from - 2, node.from - 1, Decoration8.mark({
2208
+ class: "cm-task"
2209
+ }));
2210
+ builder.add(node.from, node.to, checked ? checkedDecoration : uncheckedDecoration);
2211
+ }
2212
+ }
2213
+ },
2214
+ from,
2215
+ to
2216
+ });
2217
+ }
2218
+ return builder.finish();
2219
+ };
1949
2220
  var tasklist = (options = {}) => {
1950
- const taskMatcher = new MatchDecorator3({
1951
- regexp: /^(\s*)- \[([ xX])\]\s/g,
1952
- decoration: (match, view, pos) => {
1953
- const indent = Math.floor(match[1].length / 2);
1954
- const checked = match[2] === "x" || match[2] === "X";
1955
- return Decoration7.replace({
1956
- widget: new CheckboxWidget(pos, checked, indent, view.state.readOnly ? void 0 : (checked2) => {
1957
- const idx = pos + match[0].indexOf("[") + 1;
1958
- view.dispatch({
1959
- changes: {
1960
- from: idx,
1961
- to: idx + 1,
1962
- insert: checked2 ? "x" : " "
1963
- },
1964
- // TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
1965
- selection: {
1966
- anchor: idx + 3
1967
- }
1968
- });
1969
- })
1970
- });
1971
- }
1972
- });
1973
- const tasks = ViewPlugin5.fromClass(class {
1974
- constructor(view) {
1975
- this.tasks = taskMatcher.createDeco(view);
1976
- }
1977
- update(update4) {
1978
- this.tasks = taskMatcher.updateDeco(update4, this.tasks);
1979
- }
1980
- }, {
1981
- decorations: (value) => value.tasks,
1982
- provide: (plugin) => EditorView10.atomicRanges.of((view) => {
1983
- return view.plugin(plugin)?.tasks || Decoration7.none;
1984
- })
1985
- });
1986
2221
  return [
1987
- tasks,
1988
- styles4
2222
+ ViewPlugin6.fromClass(class {
2223
+ constructor(view) {
2224
+ this.decorations = buildDecorations4(view);
2225
+ }
2226
+ update(update2) {
2227
+ if (update2.docChanged || update2.viewportChanged || update2.selectionSet) {
2228
+ this.decorations = buildDecorations4(update2.view);
2229
+ }
2230
+ }
2231
+ }, {
2232
+ decorations: (v) => v.decorations
2233
+ }),
2234
+ styles5
1989
2235
  ];
1990
2236
  };
1991
2237
 
1992
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/tooltip.ts
1993
- import { hoverTooltip as hoverTooltip2 } from "@codemirror/view";
1994
- import { tooltipContent } from "@dxos/react-ui-theme";
1995
- var markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)/;
1996
- var tooltip = ({ onHover, regexp = markdownLinkRegexp }) => hoverTooltip2((view, pos) => {
1997
- const { from, text } = view.state.doc.lineAt(pos);
1998
- const p = pos - from;
1999
- let idx = 0;
2000
- let match;
2001
- do {
2002
- match = text.substring(idx).match(regexp);
2003
- if (!match) {
2004
- match = void 0;
2005
- break;
2006
- }
2007
- idx = text.indexOf(match[0]);
2008
- if (p >= idx && p < idx + match[0].length) {
2009
- break;
2010
- }
2011
- idx += match[0].length;
2012
- } while (true);
2013
- if (!match) {
2014
- return null;
2015
- }
2016
- const [, , url] = match ?? [];
2017
- return {
2018
- pos: idx + from,
2019
- end: idx + from + match[0].length,
2020
- above: true,
2021
- create: () => {
2022
- const el = document.createElement("div");
2023
- el.className = tooltipContent({}, "pli-2 plb-1");
2024
- onHover(el, url);
2025
- return {
2026
- dom: el,
2027
- offset: {
2028
- x: 0,
2029
- y: 4
2030
- }
2031
- };
2032
- }
2033
- };
2034
- });
2035
-
2036
- // packages/ui/react-ui-editor/src/components/TextEditor/extensions/typewriter.ts
2238
+ // packages/ui/react-ui-editor/src/extensions/typewriter.ts
2037
2239
  import { keymap as keymap5 } from "@codemirror/view";
2038
2240
  var defaultItems = [
2039
2241
  "hello world!",
@@ -2090,8 +2292,8 @@ var typewriter = ({ delay = 75, items = defaultItems } = {}) => {
2090
2292
  ];
2091
2293
  };
2092
2294
 
2093
- // packages/ui/react-ui-editor/src/components/TextEditor/themes/default.ts
2094
- import get5 from "lodash.get";
2295
+ // packages/ui/react-ui-editor/src/themes/default.ts
2296
+ import get3 from "lodash.get";
2095
2297
  var defaultTheme = {
2096
2298
  //
2097
2299
  // Main layout:
@@ -2120,21 +2322,21 @@ var defaultTheme = {
2120
2322
  "&.cm-focused": {
2121
2323
  outline: "none"
2122
2324
  },
2123
- "& .cm-scroller": {
2325
+ ".cm-scroller": {
2124
2326
  overflow: "visible",
2125
- fontFamily: get5(tokens, "fontFamily.mono", []).join(",")
2327
+ fontFamily: get3(tokens, "fontFamily.mono", []).join(",")
2126
2328
  },
2127
- "& .cm-content": {
2329
+ ".cm-content": {
2128
2330
  padding: 0,
2129
2331
  // NOTE: Base font size (otherwise defined by HTML tag, which might be different for storybook).
2130
2332
  fontSize: "16px"
2131
2333
  },
2132
2334
  "&light .cm-content": {
2133
- color: get5(tokens, "extend.colors.neutral.900", "black"),
2335
+ color: get3(tokens, "extend.colors.neutral.900", "black"),
2134
2336
  caretColor: "black"
2135
2337
  },
2136
2338
  "&dark .cm-content": {
2137
- color: get5(tokens, "extend.colors.neutral.100", "white"),
2339
+ color: get3(tokens, "extend.colors.neutral.100", "white"),
2138
2340
  caretColor: "white"
2139
2341
  },
2140
2342
  //
@@ -2146,158 +2348,120 @@ var defaultTheme = {
2146
2348
  "&dark .cm-cursor, &dark .cm-dropCursor": {
2147
2349
  borderLeft: "2px solid white"
2148
2350
  },
2149
- "& .cm-placeholder": {
2351
+ ".cm-placeholder": {
2150
2352
  fontWeight: 100
2151
2353
  },
2152
2354
  //
2153
2355
  // line
2154
2356
  //
2155
- "& .cm-line": {
2357
+ ".cm-line": {
2156
2358
  paddingInline: 0
2157
2359
  },
2158
- "& .cm-activeLine": {
2360
+ ".cm-activeLine": {
2159
2361
  background: "inherit"
2160
2362
  },
2161
2363
  //
2162
2364
  // Selection
2163
2365
  //
2164
2366
  "&light .cm-selectionBackground": {
2165
- background: get5(tokens, "extend.colors.primary.100")
2367
+ background: get3(tokens, "extend.colors.primary.100")
2166
2368
  },
2167
2369
  "&light.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground": {
2168
- background: get5(tokens, "extend.colors.primary.200")
2370
+ background: get3(tokens, "extend.colors.primary.200")
2169
2371
  },
2170
2372
  "&dark .cm-selectionBackground": {
2171
- background: get5(tokens, "extend.colors.primary.700")
2373
+ background: get3(tokens, "extend.colors.primary.700")
2172
2374
  },
2173
2375
  "&dark.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground": {
2174
- background: get5(tokens, "extend.colors.primary.600")
2376
+ background: get3(tokens, "extend.colors.primary.600")
2175
2377
  },
2176
2378
  //
2177
2379
  // Search
2178
2380
  //
2179
2381
  "&light .cm-searchMatch": {
2180
- backgroundColor: get5(tokens, "extend.colors.yellow.100")
2382
+ backgroundColor: get3(tokens, "extend.colors.yellow.100")
2181
2383
  },
2182
2384
  "&dark .cm-searchMatch": {
2183
- backgroundColor: get5(tokens, "extend.colors.yellow.700")
2184
- },
2185
- //
2186
- // collaboration
2187
- // TODO(burdon): Review classnames (YJS dependent?) Note: e2e tests depend on this class.
2188
- //
2189
- "&light .cm-ySelection, &light .cm-yLineSelection": {
2190
- mixBlendMode: "multiply"
2191
- },
2192
- "&dark .cm-ySelection, &dark .cm-yLineSelection": {
2193
- mixBlendMode: "screen"
2194
- },
2195
- "& .cm-ySelectionInfo": {
2196
- padding: "2px 4px",
2197
- marginBlockStart: "-4px"
2198
- },
2199
- "& .cm-ySelection, & .cm-selectionMatch": {
2200
- paddingBlockStart: ".15em",
2201
- paddingBlockEnd: ".15em"
2202
- },
2203
- "& .cm-ySelectionCaret": {
2204
- display: "inline-block",
2205
- insetBlockStart: ".1em",
2206
- blockSize: "1.4em",
2207
- verticalAlign: "top"
2208
- },
2209
- "& .cm-yLineSelection": {
2210
- margin: 0
2385
+ backgroundColor: get3(tokens, "extend.colors.yellow.700")
2211
2386
  },
2212
2387
  //
2213
2388
  // link
2214
2389
  //
2215
- "& .cm-link": {
2216
- color: get5(tokens, "extend.colors.primary.500"),
2390
+ ".cm-link": {
2391
+ color: get3(tokens, "extend.colors.primary.500"),
2217
2392
  textDecorationLine: "underline",
2218
2393
  textDecorationThickness: "1px",
2219
2394
  textUnderlineOffset: "2px",
2220
2395
  borderRadius: ".125rem",
2221
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2396
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2222
2397
  },
2223
2398
  //
2224
2399
  // tooltip
2225
2400
  //
2226
- "& .cm-tooltip": {
2401
+ ".cm-tooltip": {
2227
2402
  border: "none"
2228
2403
  },
2229
- "& .cm-tooltip-below": {},
2404
+ ".cm-tooltip-below": {},
2230
2405
  //
2231
2406
  // autocomplete
2232
2407
  //
2233
- "& .cm-tooltip-autocomplete": {
2408
+ ".cm-tooltip-autocomplete": {
2234
2409
  marginTop: "4px",
2235
2410
  marginLeft: "-3px"
2236
2411
  },
2237
- "& .cm-tooltip-autocomplete ul li": {},
2238
- "& .cm-tooltip-autocomplete ul li[aria-selected]": {},
2239
- "& .cm-completionIcon": {
2412
+ ".cm-tooltip-autocomplete ul li": {},
2413
+ ".cm-tooltip-autocomplete ul li[aria-selected]": {},
2414
+ ".cm-completionIcon": {
2240
2415
  display: "none"
2241
2416
  },
2242
- "& .cm-completionLabel": {
2243
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2417
+ ".cm-completionLabel": {
2418
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2244
2419
  },
2245
- "& .cm-completionMatchedText": {
2420
+ ".cm-completionMatchedText": {
2246
2421
  textDecoration: "none"
2247
2422
  },
2248
2423
  //
2249
- // widgets
2250
- //
2251
- "& .cm-widgetBuffer": {
2252
- display: "none",
2253
- height: 0
2254
- },
2255
- //
2256
2424
  // table
2257
2425
  //
2258
- "& .cm-table *": {
2259
- fontFamily: `${get5(tokens, "fontFamily.mono", []).join(",")} !important`,
2426
+ ".cm-table *": {
2427
+ fontFamily: `${get3(tokens, "fontFamily.mono", []).join(",")} !important`,
2260
2428
  textDecoration: "none !important"
2261
2429
  },
2262
- "& .cm-table-head": {
2430
+ ".cm-table-head": {
2263
2431
  padding: "2px 16px 2px 0px",
2264
- borderBottom: `1px solid ${get5(tokens, "extend.colors.neutral.500")}`,
2432
+ borderBottom: `1px solid ${get3(tokens, "extend.colors.neutral.500")}`,
2265
2433
  fontWeight: 100,
2266
2434
  textAlign: "left",
2267
- color: get5(tokens, "extend.colors.neutral.500")
2435
+ color: get3(tokens, "extend.colors.neutral.500")
2268
2436
  },
2269
- "& .cm-table-cell": {
2437
+ ".cm-table-cell": {
2270
2438
  padding: "2px 16px 2px 0px"
2271
2439
  },
2272
2440
  //
2273
2441
  // image
2274
2442
  //
2275
- "& .cm-image": {
2276
- margin: "0.5rem 0"
2443
+ ".cm-image": {
2444
+ display: "block",
2445
+ height: "0"
2446
+ },
2447
+ ".cm-image.cm-loaded-image": {
2448
+ height: "auto",
2449
+ borderTop: "0.5rem solid transparent",
2450
+ borderBottom: "0.5rem solid transparent"
2277
2451
  },
2278
2452
  //
2279
2453
  // font size
2280
- // TODO(thure): This appears to be the best or only way to set selection caret heights, but it's far more verbose than it needs to be.
2454
+ // TODO(thure): This appears to be the best or only way to set selection caret heights,
2455
+ // but it's far more verbose than it needs to be.
2281
2456
  //
2282
- ...Object.keys(get5(tokens, "extend.fontSize", {})).reduce((acc, fontSize) => {
2283
- const height = get5(tokens, [
2284
- "extend",
2285
- "fontSize",
2286
- fontSize,
2287
- 1,
2288
- "lineHeight"
2289
- ]);
2290
- acc[`& .text-${fontSize} + .cm-ySelectionCaret`] = {
2291
- height
2292
- };
2293
- acc[`& .text-${fontSize} + .cm-ySelection + .cm-ySelectionCaret`] = {
2294
- height
2295
- };
2296
- acc[`& .text-${fontSize} + .cm-widgetBuffer + .cm-ySelectionCaret`] = {
2297
- height
2298
- };
2299
- return acc;
2300
- }, {}),
2457
+ // ...Object.keys(get(tokens, 'extend.fontSize', {})).reduce((acc: Record<string, any>, fontSize) => {
2458
+ // const height = get(tokens, ['extend', 'fontSize', fontSize, 1, 'lineHeight']);
2459
+ //
2460
+ // acc[`& .text-${fontSize} + .cm-ySelectionCaret`] = { height };
2461
+ // acc[`& .text-${fontSize} + .cm-ySelection + .cm-ySelectionCaret`] = { height };
2462
+ // acc[`& .text-${fontSize} + .cm-widgetBuffer + .cm-ySelectionCaret`] = { height };
2463
+ // return acc;
2464
+ // }, {}),
2301
2465
  /**
2302
2466
  * Panels
2303
2467
  * TODO(burdon): Needs styling attention (esp. dark mode).
@@ -2314,153 +2478,140 @@ var defaultTheme = {
2314
2478
  * </div>
2315
2479
  * </div
2316
2480
  */
2317
- "& .cm-panels": {
2318
- border: `1px solid ${get5(tokens, "extend.colors.neutral.200")}`
2481
+ ".cm-panels": {
2482
+ border: `1px solid ${get3(tokens, "extend.colors.neutral.200")}`
2319
2483
  },
2320
- "& .cm-panel": {
2321
- background: get5(tokens, "extend.colors.neutral.50"),
2322
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2484
+ ".cm-panel": {
2485
+ background: get3(tokens, "extend.colors.neutral.50"),
2486
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2323
2487
  },
2324
- "& .cm-button": {
2488
+ ".cm-button": {
2325
2489
  margin: "4px",
2326
- fontFamily: get5(tokens, "fontFamily.body", []).join(","),
2327
- background: get5(tokens, "extend.colors.neutral.100"),
2490
+ fontFamily: get3(tokens, "fontFamily.body", []).join(","),
2491
+ background: get3(tokens, "extend.colors.neutral.100"),
2328
2492
  backgroundImage: "none",
2329
2493
  border: "none",
2330
2494
  "&:hover": {
2331
- background: get5(tokens, "extend.colors.neutral.200")
2495
+ background: get3(tokens, "extend.colors.neutral.200")
2332
2496
  },
2333
2497
  "&:active": {
2334
- background: get5(tokens, "extend.colors.neutral.300"),
2498
+ background: get3(tokens, "extend.colors.neutral.300"),
2335
2499
  backgroundImage: "none"
2336
2500
  }
2337
2501
  },
2338
- "& .cm-panel input[type=checkbox]": {
2502
+ ".cm-panel input[type=checkbox]": {
2339
2503
  marginRight: "0.4rem !important"
2340
2504
  }
2341
2505
  };
2342
2506
  var textTheme = {
2343
- "& .cm-scroller": {
2344
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2507
+ ".cm-scroller": {
2508
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2345
2509
  },
2346
- "& .cm-placeholder": {
2347
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2510
+ ".cm-placeholder": {
2511
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2348
2512
  }
2349
2513
  };
2350
2514
  var markdownTheme = {
2351
- // NOTE: Must leave base font family as is (i.e., monospace) due to fenced code blocks.
2352
- "& .cm-placeholder": {
2353
- fontFamily: get5(tokens, "fontFamily.body", []).join(",")
2515
+ ".cm-scroller": {
2516
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2517
+ },
2518
+ ".cm-placeholder": {
2519
+ fontFamily: get3(tokens, "fontFamily.body", []).join(",")
2354
2520
  }
2355
2521
  };
2356
2522
  var codeTheme = {
2357
- "& .cm-scroller": {
2358
- fontFamily: get5(tokens, "fontFamily.mono", []).join(",")
2523
+ ".cm-scroller": {
2524
+ fontFamily: get3(tokens, "fontFamily.mono", []).join(",")
2359
2525
  },
2360
- "& .cm-placeholder": {
2361
- fontFamily: get5(tokens, "fontFamily.mono", []).join(",")
2526
+ ".cm-placeholder": {
2527
+ fontFamily: get3(tokens, "fontFamily.mono", []).join(",")
2362
2528
  }
2363
2529
  };
2364
2530
 
2365
2531
  // packages/ui/react-ui-editor/src/components/TextEditor/TextEditor.tsx
2366
- import { EditorState as EditorState2 } from "@codemirror/state";
2367
- import { EditorView as EditorView11 } from "@codemirror/view";
2368
- import { useFocusableGroup } from "@fluentui/react-tabster";
2369
- import { vim } from "@replit/codemirror-vim";
2370
- import defaultsDeep2 from "lodash.defaultsdeep";
2371
- import React, { forwardRef, useCallback, useEffect as useEffect2, useImperativeHandle, useState as useState2 } from "react";
2372
- import { generateName } from "@dxos/display-name";
2373
- import { useThemeContext } from "@dxos/react-ui";
2374
- import { getColorForValue, inputSurface, mx as mx3 } from "@dxos/react-ui-theme";
2532
+ var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/components/TextEditor/TextEditor.tsx";
2375
2533
  var EditorModes = [
2376
2534
  "default",
2377
2535
  "vim"
2378
2536
  ];
2379
- var BaseTextEditor = /* @__PURE__ */ forwardRef(({ model, comments: comments2, readonly, editorMode, extensions = [], slots = defaultSlots }, forwardedRef) => {
2380
- const { themeMode } = useThemeContext();
2537
+ var BaseTextEditor = /* @__PURE__ */ forwardRef(({ model, readonly, autoFocus, scrollTo, selection, editorMode, theme, slots = defaultSlots, extensions = [], debug }, forwardedRef) => {
2381
2538
  const tabsterDOMAttribute = useFocusableGroup({
2382
2539
  tabBehavior: "limited"
2383
2540
  });
2384
- const [root, setRoot] = useState2(null);
2385
- const [{ state = void 0, view = void 0 } = {}, setEditor] = useState2();
2386
- useImperativeHandle(forwardedRef, () => ({
2387
- root,
2388
- state,
2541
+ const { themeMode } = useThemeContext();
2542
+ const rootRef = useRef(null);
2543
+ const [view, setView] = useState(null);
2544
+ useImperativeHandle(forwardedRef, () => view, [
2389
2545
  view
2390
- }), [
2391
- view,
2392
- state,
2393
- root
2394
- ]);
2395
- const { awareness, peer } = model;
2396
- useEffect2(() => {
2397
- if (awareness && peer) {
2398
- awareness.setLocalStateField("user", {
2399
- name: peer.name ?? generateName(peer.id),
2400
- color: getColorForValue({
2401
- value: peer.id,
2402
- type: "color"
2403
- }),
2404
- colorLight: getColorForValue({
2405
- value: peer.id,
2406
- themeMode,
2407
- type: "highlight"
2408
- })
2409
- });
2410
- }
2411
- }, [
2412
- awareness,
2413
- peer,
2414
- themeMode
2415
2546
  ]);
2416
2547
  useEffect2(() => {
2417
- if (view && comments2?.length) {
2418
- view.dispatch({
2419
- effects: setCommentRange.of({
2420
- model,
2421
- comments: comments2
2422
- })
2423
- });
2548
+ if (autoFocus) {
2549
+ view?.focus();
2424
2550
  }
2425
2551
  }, [
2426
2552
  view,
2427
- comments2
2553
+ autoFocus
2428
2554
  ]);
2429
2555
  useEffect2(() => {
2430
- if (!root) {
2556
+ if (!model || !rootRef.current) {
2431
2557
  return;
2432
2558
  }
2433
- const state2 = EditorState2.create({
2559
+ const state = EditorState2.create({
2434
2560
  doc: model.text(),
2561
+ selection,
2435
2562
  extensions: [
2436
- readonly && EditorState2.readOnly.of(readonly),
2437
- // TODO(burdon): Factor out VIM mode? (manage via MarkdownPlugin).
2438
- editorMode === "vim" && vim(),
2563
+ // TODO(burdon): Doesn't catch errors in keymap functions.
2564
+ EditorView13.exceptionSink.of((err) => {
2565
+ log2.catch(err, void 0, {
2566
+ F: __dxlog_file5,
2567
+ L: 123,
2568
+ S: void 0,
2569
+ C: (f, a) => f(...a)
2570
+ });
2571
+ }),
2439
2572
  // Theme.
2440
- // TODO(burdon): Make theme configurable.
2441
- EditorView11.baseTheme(defaultTheme),
2442
- EditorView11.theme(slots?.editor?.theme ?? {}),
2443
- EditorView11.darkTheme.of(themeMode === "dark"),
2573
+ // TODO(burdon): Make configurable.
2574
+ EditorView13.baseTheme(defaultTheme),
2575
+ EditorView13.theme(theme ?? {}),
2576
+ EditorView13.darkTheme.of(themeMode === "dark"),
2577
+ EditorView13.editorAttributes.of({
2578
+ class: slots.editor?.className ?? ""
2579
+ }),
2580
+ EditorView13.contentAttributes.of({
2581
+ class: slots.content?.className ?? ""
2582
+ }),
2583
+ // State.
2584
+ EditorState2.readOnly.of(!!readonly),
2444
2585
  // Storage and replication.
2586
+ // NOTE: This must come before user extensions.
2445
2587
  model.extension,
2588
+ // TODO(burdon): Factor out (requires special handling for Escape/focus).
2589
+ editorMode === "vim" && vim(),
2446
2590
  // Custom.
2447
2591
  ...extensions
2448
2592
  ].filter(Boolean)
2449
2593
  });
2450
- view?.destroy();
2451
- setEditor({
2452
- state: state2,
2453
- view: new EditorView11({
2454
- state: state2,
2455
- parent: root
2456
- })
2594
+ const newView = new EditorView13({
2595
+ state,
2596
+ parent: rootRef.current,
2597
+ scrollTo,
2598
+ // NOTE: Uncomment to debug/monitor all transactions.
2599
+ // https://codemirror.net/docs/ref/#view.EditorView.dispatch
2600
+ dispatchTransactions: (trs, view2) => {
2601
+ if (debug) {
2602
+ logChanges(trs);
2603
+ }
2604
+ view2.update(trs);
2605
+ }
2457
2606
  });
2607
+ view?.destroy();
2608
+ setView(newView);
2458
2609
  return () => {
2459
- view?.destroy();
2460
- setEditor(void 0);
2610
+ newView?.destroy();
2611
+ setView(null);
2461
2612
  };
2462
2613
  }, [
2463
- root,
2614
+ rootRef,
2464
2615
  model,
2465
2616
  readonly,
2466
2617
  editorMode,
@@ -2470,11 +2621,13 @@ var BaseTextEditor = /* @__PURE__ */ forwardRef(({ model, comments: comments2, r
2470
2621
  const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
2471
2622
  switch (key) {
2472
2623
  case "Enter": {
2473
- view?.contentDOM.focus();
2624
+ view?.focus();
2474
2625
  break;
2475
2626
  }
2476
2627
  case "Escape": {
2477
- editorMode === "vim" && (altKey || shiftKey || metaKey || ctrlKey) && root?.focus();
2628
+ if (editorMode === "vim" && (altKey || shiftKey || metaKey || ctrlKey)) {
2629
+ rootRef.current?.focus();
2630
+ }
2478
2631
  break;
2479
2632
  }
2480
2633
  }
@@ -2484,107 +2637,305 @@ var BaseTextEditor = /* @__PURE__ */ forwardRef(({ model, comments: comments2, r
2484
2637
  ]);
2485
2638
  return /* @__PURE__ */ React.createElement("div", {
2486
2639
  key: model.id,
2487
- ref: setRoot,
2640
+ role: "none",
2488
2641
  tabIndex: 0,
2489
- ...slots?.root,
2642
+ onKeyUp: handleKeyUp,
2643
+ ...slots.root,
2490
2644
  ...editorMode !== "vim" && tabsterDOMAttribute,
2491
- onKeyUp: handleKeyUp
2645
+ ref: rootRef
2492
2646
  });
2493
2647
  });
2494
- var TextEditor = /* @__PURE__ */ forwardRef(({ readonly, extensions = [], slots: _slots, ...props }, forwardedRef) => {
2648
+ var TextEditor = /* @__PURE__ */ forwardRef(({ readonly, placeholder: placeholder3, lineWrapping, theme = textTheme, slots, extensions = [], ...props }, forwardedRef) => {
2495
2649
  const { themeMode } = useThemeContext();
2496
- const slots = defaultsDeep2({}, _slots, defaultTextSlots);
2650
+ const updatedSlots = defaultsDeep2({}, slots, defaultTextSlots);
2497
2651
  return /* @__PURE__ */ React.createElement(BaseTextEditor, {
2498
2652
  ref: forwardedRef,
2499
2653
  readonly,
2500
2654
  extensions: [
2501
2655
  basicBundle({
2502
- readonly,
2503
2656
  themeMode,
2504
- placeholder: slots?.editor?.placeholder
2657
+ placeholder: placeholder3,
2658
+ lineWrapping
2505
2659
  }),
2506
2660
  ...extensions
2507
2661
  ],
2508
- slots,
2662
+ theme,
2663
+ slots: updatedSlots,
2509
2664
  ...props
2510
2665
  });
2511
2666
  });
2512
- var MarkdownEditor = /* @__PURE__ */ forwardRef(({ readonly, extensions = [], slots: _slots, ...props }, forwardedRef) => {
2667
+ var MarkdownEditor = /* @__PURE__ */ forwardRef(({ readonly, placeholder: placeholder3, theme = markdownTheme, slots, extensions = [], ...props }, forwardedRef) => {
2513
2668
  const { themeMode } = useThemeContext();
2514
- const slots = defaultsDeep2({}, _slots, defaultMarkdownSlots);
2669
+ const updatedSlots = defaultsDeep2({}, slots, defaultMarkdownSlots);
2515
2670
  return /* @__PURE__ */ React.createElement(BaseTextEditor, {
2516
2671
  ref: forwardedRef,
2517
2672
  readonly,
2518
2673
  extensions: [
2519
2674
  markdownBundle({
2520
- readonly,
2521
2675
  themeMode,
2522
- placeholder: slots?.editor?.placeholder
2676
+ readonly,
2677
+ placeholder: placeholder3
2523
2678
  }),
2524
2679
  ...extensions
2525
2680
  ],
2526
- slots,
2681
+ theme,
2682
+ slots: updatedSlots,
2527
2683
  ...props
2528
2684
  });
2529
2685
  });
2530
2686
  var defaultSlots = {
2531
2687
  root: {
2532
- className: mx3("p-2", inputSurface)
2688
+ // TODO(burdon): Add focusRing by default/as property?
2689
+ className: mx3("flex flex-col grow overflow-y-auto", attentionSurface)
2690
+ },
2691
+ editor: {
2692
+ className: "h-full p-2"
2533
2693
  }
2534
2694
  };
2535
2695
  var defaultTextSlots = {
2536
- ...defaultSlots,
2537
- editor: {
2538
- theme: textTheme
2539
- }
2696
+ ...defaultSlots
2540
2697
  };
2541
2698
  var defaultMarkdownSlots = {
2542
- ...defaultSlots,
2543
- editor: {
2544
- theme: markdownTheme
2699
+ ...defaultSlots
2700
+ };
2701
+
2702
+ // packages/ui/react-ui-editor/src/hooks/awareness-provider.ts
2703
+ import { DeferredTask, Event as Event2, sleep } from "@dxos/async";
2704
+ import { Context as Context2 } from "@dxos/context";
2705
+ import { invariant as invariant4 } from "@dxos/invariant";
2706
+ import { log as log3 } from "@dxos/log";
2707
+ var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/hooks/awareness-provider.ts";
2708
+ var DEBOUNCE_INTERVAL = 100;
2709
+ var SpaceAwarenessProvider = class {
2710
+ constructor(params) {
2711
+ this._remoteStates = /* @__PURE__ */ new Map();
2712
+ this.remoteStateChange = new Event2();
2713
+ this._space = params.space;
2714
+ this._channel = params.channel;
2715
+ this._peerId = params.peerId;
2716
+ this._info = params.info;
2717
+ }
2718
+ open() {
2719
+ this._ctx = new Context2();
2720
+ this._postTask = new DeferredTask(this._ctx, async () => {
2721
+ if (this._localState) {
2722
+ await this._space.postMessage(this._channel, {
2723
+ kind: "post",
2724
+ state: this._localState
2725
+ });
2726
+ await sleep(DEBOUNCE_INTERVAL);
2727
+ }
2728
+ });
2729
+ this._ctx.onDispose(this._space.listen(this._channel, (message) => {
2730
+ switch (message.payload.kind) {
2731
+ case "query": {
2732
+ this._handleQueryMessage();
2733
+ break;
2734
+ }
2735
+ case "post": {
2736
+ this._handlePostMessage(message.payload);
2737
+ break;
2738
+ }
2739
+ }
2740
+ }));
2741
+ void this._space.postMessage(this._channel, {
2742
+ kind: "query"
2743
+ }).catch((err) => {
2744
+ log3.debug("failed to query awareness", {
2745
+ err
2746
+ }, {
2747
+ F: __dxlog_file6,
2748
+ L: 89,
2749
+ S: this,
2750
+ C: (f, a) => f(...a)
2751
+ });
2752
+ });
2753
+ }
2754
+ close() {
2755
+ void this._ctx?.dispose();
2756
+ this._ctx = void 0;
2757
+ this._postTask = void 0;
2758
+ }
2759
+ getRemoteStates() {
2760
+ return Array.from(this._remoteStates.values());
2761
+ }
2762
+ update(position) {
2763
+ invariant4(this._postTask, void 0, {
2764
+ F: __dxlog_file6,
2765
+ L: 104,
2766
+ S: this,
2767
+ A: [
2768
+ "this._postTask",
2769
+ ""
2770
+ ]
2771
+ });
2772
+ this._localState = {
2773
+ peerId: this._peerId,
2774
+ position,
2775
+ info: this._info
2776
+ };
2777
+ this._postTask.schedule();
2778
+ }
2779
+ _handleQueryMessage() {
2780
+ invariant4(this._postTask, void 0, {
2781
+ F: __dxlog_file6,
2782
+ L: 115,
2783
+ S: this,
2784
+ A: [
2785
+ "this._postTask",
2786
+ ""
2787
+ ]
2788
+ });
2789
+ this._postTask.schedule();
2790
+ }
2791
+ _handlePostMessage(message) {
2792
+ invariant4(message.kind === "post", void 0, {
2793
+ F: __dxlog_file6,
2794
+ L: 120,
2795
+ S: this,
2796
+ A: [
2797
+ "message.kind === 'post'",
2798
+ ""
2799
+ ]
2800
+ });
2801
+ this._remoteStates.set(message.state.peerId, message.state);
2802
+ this.remoteStateChange.emit();
2545
2803
  }
2546
2804
  };
2805
+
2806
+ // packages/ui/react-ui-editor/src/hooks/defs.ts
2807
+ import { StateField as StateField5 } from "@codemirror/state";
2808
+ var modelState = StateField5.define({
2809
+ create: () => void 0,
2810
+ update: (model) => model
2811
+ });
2812
+
2813
+ // packages/ui/react-ui-editor/src/hooks/useTextEditor.ts
2814
+ import { useState as useState2 } from "react";
2815
+ var useTextEditor = () => {
2816
+ const [view, setView] = useState2(null);
2817
+ return [
2818
+ (ref) => {
2819
+ setView(ref);
2820
+ },
2821
+ view
2822
+ ];
2823
+ };
2824
+
2825
+ // packages/ui/react-ui-editor/src/hooks/useTextModel.ts
2826
+ import get4 from "lodash.get";
2827
+ import { useEffect as useEffect3, useState as useState3 } from "react";
2828
+ import { generateName } from "@dxos/display-name";
2829
+ import { getRawDoc } from "@dxos/echo-schema";
2830
+ import { invariant as invariant5 } from "@dxos/invariant";
2831
+ import { isAutomergeObject } from "@dxos/react-client/echo";
2832
+ import { isNotNullOrUndefined } from "@dxos/util";
2833
+ var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-editor/src/hooks/useTextModel.ts";
2834
+ var useTextModel = (props) => {
2835
+ const { identity, space, text } = props;
2836
+ const [model, setModel] = useState3();
2837
+ useEffect3(() => setModel(createModel(props)), [
2838
+ identity,
2839
+ space,
2840
+ text
2841
+ ]);
2842
+ return model;
2843
+ };
2844
+ var createModel = ({ space, identity, text }) => {
2845
+ invariant5(isAutomergeObject(text), void 0, {
2846
+ F: __dxlog_file7,
2847
+ L: 35,
2848
+ S: void 0,
2849
+ A: [
2850
+ "isAutomergeObject(text)",
2851
+ ""
2852
+ ]
2853
+ });
2854
+ const obj = text;
2855
+ const doc = getRawDoc(obj, [
2856
+ obj.field
2857
+ ]);
2858
+ const awarenessProvider = space && new SpaceAwarenessProvider({
2859
+ space,
2860
+ channel: `automerge.awareness.${obj.id}`,
2861
+ info: {
2862
+ displayName: identity ? identity.profile?.displayName ?? generateName(identity.identityKey.toHex()) : void 0,
2863
+ color: cursorColor.color,
2864
+ lightColor: cursorColor.light
2865
+ },
2866
+ peerId: identity?.identityKey.toHex() ?? "Anonymous"
2867
+ });
2868
+ const model = {
2869
+ id: obj.id,
2870
+ content: doc,
2871
+ text: () => get4(doc.handle.docSync(), doc.path),
2872
+ extension: [
2873
+ modelState.init(() => model),
2874
+ automerge3({
2875
+ handle: doc.handle,
2876
+ path: doc.path
2877
+ }),
2878
+ awarenessProvider && AwarenessProvider.of(awarenessProvider),
2879
+ awareness()
2880
+ ].filter(isNotNullOrUndefined),
2881
+ peer: identity ? {
2882
+ id: identity.identityKey.toHex(),
2883
+ name: identity.profile?.displayName
2884
+ } : void 0
2885
+ };
2886
+ return model;
2887
+ };
2547
2888
  export {
2889
+ AwarenessProvider,
2548
2890
  BaseTextEditor,
2891
+ Cursor,
2549
2892
  Doc,
2550
2893
  EditorModes,
2551
2894
  MarkdownEditor,
2895
+ RemoteSelectionsDecorator,
2552
2896
  SpaceAwarenessProvider,
2553
2897
  TextEditor,
2554
2898
  TextKind,
2555
2899
  YText,
2556
2900
  YXmlFragment,
2557
2901
  autocomplete,
2902
+ automerge3 as automerge,
2903
+ awareness,
2558
2904
  basicBundle,
2559
2905
  blast,
2906
+ callbackWrapper,
2560
2907
  code2 as code,
2561
2908
  codeTheme,
2562
2909
  comments,
2563
- cursorColor,
2564
2910
  defaultMarkdownSlots,
2565
2911
  defaultOptions,
2566
2912
  defaultSlots,
2567
2913
  defaultTextSlots,
2568
2914
  defaultTheme,
2915
+ getToken,
2569
2916
  hr,
2570
2917
  image,
2571
2918
  link,
2572
2919
  listener,
2920
+ logChanges,
2573
2921
  markdownBundle,
2574
2922
  markdownHighlightStyle,
2575
2923
  markdownTags,
2576
2924
  markdownTagsExtensions,
2577
2925
  markdownTheme,
2578
2926
  mention,
2579
- mermaid,
2580
2927
  modelState,
2581
- setCommentRange,
2928
+ outliner,
2929
+ setComments,
2930
+ setFocus,
2931
+ setSelection,
2582
2932
  table,
2583
2933
  tags2 as tags,
2584
2934
  tasklist,
2585
2935
  textTheme,
2586
- tooltip,
2587
2936
  typewriter,
2937
+ useComments,
2938
+ useTextEditor,
2588
2939
  useTextModel
2589
2940
  };
2590
2941
  //# sourceMappingURL=index.mjs.map