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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/dist/lib/browser/index.mjs +2651 -1060
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +13 -15
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  9. package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
  11. package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
  12. package/dist/types/src/components/Toolbar/Toolbar.d.ts +29 -0
  13. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  14. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +25 -0
  15. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  16. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  17. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  18. package/dist/types/src/components/index.d.ts +1 -0
  19. package/dist/types/src/components/index.d.ts.map +1 -1
  20. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  21. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  22. package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
  23. package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
  24. package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
  25. package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
  26. package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
  27. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  28. package/dist/types/src/extensions/automerge/index.d.ts +0 -1
  29. package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
  30. package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
  31. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  32. package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
  33. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  34. package/dist/types/src/extensions/awareness.d.ts +2 -2
  35. package/dist/types/src/extensions/awareness.d.ts.map +1 -1
  36. package/dist/types/src/extensions/basic.d.ts +4 -5
  37. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  38. package/dist/types/src/extensions/comments.d.ts +40 -19
  39. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  40. package/dist/types/src/extensions/cursor.d.ts +3 -3
  41. package/dist/types/src/extensions/cursor.d.ts.map +1 -1
  42. package/dist/types/src/extensions/index.d.ts +1 -7
  43. package/dist/types/src/extensions/index.d.ts.map +1 -1
  44. package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
  45. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  46. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  47. package/dist/types/src/extensions/markdown/formatting.d.ts +57 -0
  48. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
  49. package/dist/types/src/extensions/markdown/formatting.test.d.ts +3 -0
  50. package/dist/types/src/extensions/markdown/formatting.test.d.ts.map +1 -0
  51. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  52. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  53. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  54. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  55. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  56. package/dist/types/src/extensions/markdown/index.d.ts +8 -0
  57. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  58. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  59. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  60. package/dist/types/src/extensions/markdown/tasklist.d.ts +3 -0
  61. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  62. package/dist/types/src/extensions/mention.d.ts.map +1 -1
  63. package/dist/types/src/extensions/outliner.d.ts +4 -0
  64. package/dist/types/src/extensions/outliner.d.ts.map +1 -0
  65. package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
  66. package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
  67. package/dist/types/src/hooks/defs.d.ts +25 -0
  68. package/dist/types/src/hooks/defs.d.ts.map +1 -0
  69. package/dist/types/src/hooks/index.d.ts +4 -2
  70. package/dist/types/src/hooks/index.d.ts.map +1 -1
  71. package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
  72. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
  73. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  74. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  75. package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
  76. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
  77. package/dist/types/src/hooks/useTextModel.d.ts +14 -27
  78. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  79. package/dist/types/src/index.d.ts +1 -1
  80. package/dist/types/src/index.d.ts.map +1 -1
  81. package/dist/types/src/styles/markdown.d.ts +2 -2
  82. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  83. package/dist/types/src/themes/default.d.ts.map +1 -1
  84. package/dist/types/src/util.d.ts +2 -0
  85. package/dist/types/src/util.d.ts.map +1 -1
  86. package/package.json +47 -37
  87. package/src/components/TextEditor/MarkdownEditor.stories.tsx +75 -84
  88. package/src/components/TextEditor/TextEditor.tsx +111 -75
  89. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  90. package/src/components/TextEditor/hooks.stories.tsx +111 -0
  91. package/src/components/Toolbar/Toolbar.stories.tsx +102 -0
  92. package/src/components/Toolbar/Toolbar.tsx +245 -0
  93. package/src/components/Toolbar/index.ts +5 -0
  94. package/src/components/index.ts +1 -0
  95. package/src/extensions/autocomplete.ts +6 -3
  96. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  97. package/src/extensions/automerge/automerge.test.ts +13 -0
  98. package/src/extensions/automerge/automerge.ts +54 -45
  99. package/src/extensions/automerge/cursor.ts +3 -3
  100. package/src/extensions/automerge/defs.ts +13 -34
  101. package/src/extensions/automerge/index.ts +0 -1
  102. package/src/extensions/automerge/semaphore.ts +33 -32
  103. package/src/extensions/automerge/update-automerge.ts +14 -8
  104. package/src/extensions/automerge/update-codemirror.ts +5 -5
  105. package/src/extensions/awareness.ts +22 -18
  106. package/src/extensions/basic.ts +17 -10
  107. package/src/extensions/blast.ts +4 -1
  108. package/src/extensions/comments.ts +328 -167
  109. package/src/extensions/cursor.ts +7 -3
  110. package/src/extensions/index.ts +1 -7
  111. package/src/extensions/markdown/bundle.ts +23 -28
  112. package/src/extensions/markdown/code.ts +150 -0
  113. package/src/extensions/markdown/formatting.test.ts +481 -0
  114. package/src/extensions/markdown/formatting.ts +1198 -0
  115. package/src/extensions/markdown/heading.ts +59 -0
  116. package/src/extensions/markdown/highlight.ts +9 -15
  117. package/src/extensions/markdown/hr.ts +71 -0
  118. package/src/extensions/markdown/index.ts +8 -0
  119. package/src/extensions/{link.ts → markdown/link.ts} +16 -7
  120. package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -33
  121. package/src/extensions/mention.ts +9 -2
  122. package/src/extensions/outliner.ts +12 -0
  123. package/src/hooks/awareness-provider.ts +24 -20
  124. package/src/hooks/defs.ts +42 -0
  125. package/src/hooks/index.ts +5 -2
  126. package/src/hooks/useActionHandler.ts +93 -0
  127. package/src/hooks/useEditorView.ts +29 -0
  128. package/src/hooks/useTextEditor.ts +153 -0
  129. package/src/hooks/useTextModel.ts +66 -56
  130. package/src/index.ts +1 -1
  131. package/src/styles/markdown.ts +9 -10
  132. package/src/themes/default.ts +11 -8
  133. package/src/util.ts +26 -3
  134. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
  135. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
  136. package/dist/types/src/components/TextEditor/comments.stories.d.ts +0 -26
  137. package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
  138. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  139. package/dist/types/src/extensions/code.d.ts.map +0 -1
  140. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  141. package/dist/types/src/extensions/image.d.ts.map +0 -1
  142. package/dist/types/src/extensions/link.d.ts.map +0 -1
  143. package/dist/types/src/extensions/table.d.ts.map +0 -1
  144. package/dist/types/src/extensions/tasklist.d.ts +0 -7
  145. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  146. package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
  147. package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
  148. package/dist/types/src/extensions/yjs/index.d.ts +0 -2
  149. package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
  150. package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
  151. package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
  152. package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
  153. package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
  154. package/dist/types/src/hooks/automerge.d.ts +0 -3
  155. package/dist/types/src/hooks/automerge.d.ts.map +0 -1
  156. package/dist/types/src/hooks/yjs.d.ts +0 -27
  157. package/dist/types/src/hooks/yjs.d.ts.map +0 -1
  158. package/dist/types/src/testing/index.d.ts +0 -3
  159. package/dist/types/src/testing/index.d.ts.map +0 -1
  160. package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
  161. package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
  162. package/dist/types/src/testing/proto/index.d.ts +0 -2
  163. package/dist/types/src/testing/proto/index.d.ts.map +0 -1
  164. package/dist/types/src/testing/replicator.d.ts +0 -45
  165. package/dist/types/src/testing/replicator.d.ts.map +0 -1
  166. package/src/components/TextEditor/codemirror.stories.ts +0 -115
  167. package/src/components/TextEditor/comments.stories.tsx +0 -315
  168. package/src/components/TextEditor/yjs.stories.tsx +0 -56
  169. package/src/extensions/code.ts +0 -106
  170. package/src/extensions/hr.ts +0 -73
  171. package/src/extensions/yjs/cursor.ts +0 -22
  172. package/src/extensions/yjs/index.ts +0 -5
  173. package/src/extensions/yjs/yjs.test.ts +0 -35
  174. package/src/extensions/yjs/yjs.ts +0 -16
  175. package/src/hooks/automerge.ts +0 -52
  176. package/src/hooks/yjs.ts +0 -156
  177. package/src/testing/index.ts +0 -6
  178. package/src/testing/proto/gen/schema.ts +0 -49
  179. package/src/testing/proto/index.ts +0 -5
  180. package/src/testing/proto/schema.proto +0 -15
  181. package/src/testing/replicator.ts +0 -184
  182. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  183. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  184. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  185. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  186. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  187. /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
  188. /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
@@ -0,0 +1,245 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import {
6
+ type Icon,
7
+ CaretRight,
8
+ ChatText,
9
+ Code,
10
+ Link,
11
+ ListBullets,
12
+ ListChecks,
13
+ ListNumbers,
14
+ Paragraph,
15
+ TextStrikethrough,
16
+ Table,
17
+ TextB,
18
+ TextItalic,
19
+ } from '@phosphor-icons/react';
20
+ import { createContext } from '@radix-ui/react-context';
21
+ import React, { type PropsWithChildren } from 'react';
22
+
23
+ import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
24
+ import { getSize, mx } from '@dxos/react-ui-theme';
25
+
26
+ import { type Formatting } from '../../extensions';
27
+
28
+ // TODO(burdon): Revert to string to make extensible?
29
+ export type ActionType =
30
+ | 'blockquote'
31
+ | 'strong'
32
+ | 'codeblock'
33
+ | 'comment'
34
+ | 'heading'
35
+ | 'image'
36
+ | 'emphasis'
37
+ | 'code'
38
+ | 'link'
39
+ | 'list-bullet'
40
+ | 'list-ordered'
41
+ | 'list-tasks'
42
+ | 'mention'
43
+ | 'prompt'
44
+ | 'strikethrough'
45
+ | 'table';
46
+
47
+ export type Action = {
48
+ type: ActionType;
49
+ data?: any;
50
+ };
51
+
52
+ export type ToolbarProps = PropsWithChildren<{
53
+ state: Formatting | null;
54
+ onAction?: (action: Action) => void;
55
+ }>;
56
+
57
+ const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
58
+
59
+ const ToolbarRoot = ({ children, onAction, state }: ToolbarProps) => {
60
+ return (
61
+ <ToolbarContextProvider onAction={onAction} state={state}>
62
+ <DensityProvider density='fine'>
63
+ <div role='toolbar' className='flex w-full shrink-0 p-1 gap-2 items-center whitespace-nowrap overflow-hidden'>
64
+ {children}
65
+ </div>
66
+ </DensityProvider>
67
+ </ToolbarContextProvider>
68
+ );
69
+ };
70
+
71
+ type ToolbarButtonProps = {
72
+ Icon: Icon;
73
+ disable?: (state: Formatting) => boolean;
74
+ getState?: (state: Formatting) => boolean;
75
+ onClick: (state: Formatting | null) => Action | undefined;
76
+ } & NonNullable<Pick<ButtonProps, 'title'>>;
77
+
78
+ const ToolbarButton = ({ Icon, onClick, title, getState, disable }: ToolbarButtonProps) => {
79
+ const { onAction, state } = useToolbarContext('ToolbarButton');
80
+ const active = getState && state ? getState(state) : false;
81
+ const disabled = disable && state ? disable(state) : false;
82
+
83
+ const handleClick = (event: React.MouseEvent) => {
84
+ const action = onClick(state);
85
+ if (action) {
86
+ onAction?.(action);
87
+ event.preventDefault();
88
+ }
89
+ };
90
+
91
+ return (
92
+ <Button
93
+ variant='ghost'
94
+ classNames={mx('p-2', active && 'ring-[1px]')}
95
+ onMouseDown={handleClick}
96
+ title={title}
97
+ disabled={disabled}
98
+ >
99
+ <Icon className={getSize(5)} />
100
+ </Button>
101
+ );
102
+ };
103
+
104
+ const ToolbarSeparator = () => <div className='grow' />;
105
+
106
+ const MarkdownHeading = () => {
107
+ const { onAction, state } = useToolbarContext('MarkdownFormatting');
108
+ const blockType = state ? state.blockType : 'paragraph';
109
+ const header = blockType && /heading(\d)/.exec(blockType);
110
+ const value = header ? header[1] : blockType === 'paragraph' || !blockType ? '0' : null;
111
+ return (
112
+ <Select.Root
113
+ disabled={value === null}
114
+ value={value ?? '0'}
115
+ onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}
116
+ >
117
+ <Select.TriggerButton>
118
+ <Paragraph className={getSize(5)} />
119
+ </Select.TriggerButton>
120
+ <Select.Portal>
121
+ <Select.Content>
122
+ <Select.Viewport>
123
+ <Select.Option value='0'>Paragraph</Select.Option>
124
+ {[1, 2, 3, 4, 5, 6].map((level) => (
125
+ <Select.Option key={level} value={String(level)}>
126
+ Heading {level}
127
+ </Select.Option>
128
+ ))}
129
+ </Select.Viewport>
130
+ </Select.Content>
131
+ </Select.Portal>
132
+ </Select.Root>
133
+ );
134
+ };
135
+
136
+ const MarkdownStyles = () => (
137
+ <div role='none'>
138
+ <ToolbarButton
139
+ Icon={TextB}
140
+ title='String'
141
+ disable={(s) => s.blockType === 'codeblock'}
142
+ getState={(s) => s.strong}
143
+ onClick={(s) => ({ type: 'strong', data: s ? !s.strong : null })}
144
+ />
145
+ <ToolbarButton
146
+ Icon={TextItalic}
147
+ title='Emphasis'
148
+ disable={(s) => s.blockType === 'codeblock'}
149
+ getState={(s) => s.emphasis}
150
+ onClick={(s) => ({ type: 'emphasis', data: s ? !s.emphasis : null })}
151
+ />
152
+ <ToolbarButton
153
+ Icon={TextStrikethrough}
154
+ title='Strike-through'
155
+ disable={(s) => s.blockType === 'codeblock'}
156
+ getState={(s) => s.strikethrough}
157
+ onClick={(s) => ({ type: 'strikethrough', data: s ? !s.strikethrough : null })}
158
+ />
159
+ <ToolbarButton
160
+ Icon={Code}
161
+ title='Inline code'
162
+ disable={(s) => s.blockType === 'codeblock'}
163
+ getState={(s) => s.code}
164
+ onClick={(s) => ({ type: 'code', data: s ? !s.code : null })}
165
+ />
166
+ </div>
167
+ );
168
+
169
+ const MarkdownLists = () => (
170
+ <div role='none'>
171
+ <ToolbarButton
172
+ Icon={ListBullets}
173
+ title='Bullet list'
174
+ getState={(s) => s.listStyle === 'bullet'}
175
+ onClick={(s) => ({ type: 'list-bullet', data: s ? s.listStyle !== 'bullet' : null })}
176
+ />
177
+ <ToolbarButton
178
+ Icon={ListNumbers}
179
+ title='Numbered list'
180
+ getState={(s) => s.listStyle === 'ordered'}
181
+ onClick={(s) => ({ type: 'list-ordered', data: s ? s.listStyle !== 'ordered' : null })}
182
+ />
183
+ <ToolbarButton
184
+ Icon={ListChecks}
185
+ title='Task list'
186
+ getState={(s) => s.listStyle === 'task'}
187
+ onClick={(s) => ({ type: 'list-tasks', data: s ? s.listStyle !== 'task' : null })}
188
+ />
189
+ </div>
190
+ );
191
+
192
+ const MarkdownBlocks = () => (
193
+ <div role='none'>
194
+ <ToolbarButton
195
+ Icon={CaretRight}
196
+ title='Block quote'
197
+ getState={(s) => s.blockquote}
198
+ onClick={(s) => ({ type: 'blockquote', data: s ? !s.blockquote : null })}
199
+ />
200
+ <ToolbarButton
201
+ Icon={Code}
202
+ title='Code block'
203
+ getState={(s) => s.blockType === 'codeblock'}
204
+ onClick={(s) => ({ type: 'codeblock', data: s ? s.blockType !== 'codeblock' : null })}
205
+ />
206
+ <ToolbarButton Icon={Table} title='Table' onClick={() => ({ type: 'table' })} />
207
+ </div>
208
+ );
209
+
210
+ const MarkdownLinks = () => (
211
+ <div role='none'>
212
+ <ToolbarButton
213
+ Icon={Link}
214
+ title='Link'
215
+ getState={(s) => s.link}
216
+ onClick={(s) => ({ type: 'link', data: s ? !s.link : null })}
217
+ />
218
+ {/* <ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} /> */}
219
+ {/* <ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} /> */}
220
+ </div>
221
+ );
222
+
223
+ const MarkdownStandard = () => (
224
+ <>
225
+ <MarkdownHeading />
226
+ <MarkdownStyles />
227
+ <MarkdownLists />
228
+ <MarkdownBlocks />
229
+ <MarkdownLinks />
230
+ </>
231
+ );
232
+
233
+ const MarkdownExtended = () => (
234
+ <Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
235
+ );
236
+
237
+ export const Toolbar = {
238
+ Root: ToolbarRoot,
239
+ Button: ToolbarButton,
240
+ Separator: ToolbarSeparator,
241
+ Markdown: MarkdownStandard,
242
+ Extended: MarkdownExtended,
243
+ };
244
+
245
+ export { useToolbarContext };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ export * from './Toolbar';
@@ -3,3 +3,4 @@
3
3
  //
4
4
 
5
5
  export * from './TextEditor';
6
+ export * from './Toolbar';
@@ -35,10 +35,13 @@ export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
35
35
  // https://codemirror.net/examples/autocompletion
36
36
  // https://codemirror.net/docs/ref/#autocomplete.autocompletion
37
37
  autocompletion({
38
- defaultKeymap: false,
38
+ activateOnTyping: true,
39
39
 
40
- // Don't start unless key press.
41
- activateOnTyping: false,
40
+ // closeOnBlur: false,
41
+ // defaultKeymap: false,
42
+
43
+ // TODO(burdon): Styles/fragments.
44
+ tooltipClass: () => 'shadow rounded',
42
45
  }),
43
46
 
44
47
  // TODO(burdon): Option to create new page?
@@ -0,0 +1,76 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { EditorState } from '@codemirror/state';
6
+ import { EditorView } from '@codemirror/view';
7
+ import { render, screen } from '@testing-library/react';
8
+ import chai, { expect } from 'chai';
9
+ import chaiDom from 'chai-dom';
10
+ import get from 'lodash.get';
11
+ import React, { type FC, useEffect, useRef, useState } from 'react';
12
+ import { describe, test } from 'vitest';
13
+
14
+ import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
15
+
16
+ import { automerge } from './automerge';
17
+
18
+ type TestObject = {
19
+ text: string;
20
+ };
21
+
22
+ const path = ['text'];
23
+
24
+ class Generator {
25
+ constructor(private readonly _handle: DocHandle<TestObject>) {}
26
+ update(text: string) {
27
+ this._handle.change((doc: TestObject) => {
28
+ doc.text = text;
29
+ });
30
+ }
31
+ }
32
+
33
+ const Test: FC<{ handle: DocHandle<TestObject>; generator: Generator }> = ({ handle, generator }) => {
34
+ const ref = useRef<HTMLDivElement>(null);
35
+ const [view, setView] = useState<EditorView>();
36
+ useEffect(() => {
37
+ const extensions = [
38
+ automerge({ handle, path }),
39
+ EditorView.updateListener.of((update) => {
40
+ if (view.state.doc.toString() === 'hello!') {
41
+ // Update editor.
42
+ update.view.dispatch({ changes: { from: view.state.doc.length - 1, insert: ' world' } });
43
+ }
44
+ }),
45
+ ];
46
+
47
+ const view = new EditorView({
48
+ state: EditorState.create({ doc: get(handle.docSync()!, path), extensions }),
49
+ parent: ref.current!,
50
+ });
51
+
52
+ setView(view);
53
+ }, []);
54
+
55
+ useEffect(() => {}, [view]);
56
+
57
+ return <div ref={ref} />;
58
+ };
59
+
60
+ chai.use(chaiDom);
61
+
62
+ describe('Automerge', () => {
63
+ test('basic sync', () => {
64
+ const repo = new Repo({ network: [] });
65
+ const handle = repo.create<TestObject>();
66
+ const generator = new Generator(handle);
67
+ render(<Test handle={handle} generator={generator} />);
68
+ expect(screen.getByRole('textbox')).to.have.text('');
69
+
70
+ generator.update('hello!');
71
+ expect(screen.getByRole('textbox')).to.have.text('hello world!');
72
+ });
73
+
74
+ // TODO(burdon): Test history/undo.
75
+ // TODO(burdon): https://testing-library.com/docs/react-testing-library/example-intro/
76
+ });
@@ -0,0 +1,13 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { describe, test } from '@dxos/test';
8
+
9
+ describe('Test', () => {
10
+ test('checks sanity', async () => {
11
+ expect(true).to.be.true;
12
+ });
13
+ });
@@ -4,91 +4,100 @@
4
4
  // Ref: https://github.com/automerge/automerge-codemirror
5
5
  //
6
6
 
7
- import { Facet, StateField, type Extension, type Transaction } from '@codemirror/state';
8
- import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '@codemirror/view';
7
+ import { invertedEffects } from '@codemirror/commands';
8
+ import { StateField, type Extension, type StateEffect } from '@codemirror/state';
9
+ import { EditorView, ViewPlugin } from '@codemirror/view';
9
10
 
10
- import { next as A } from '@dxos/automerge/automerge';
11
- import { type Prop } from '@dxos/automerge/automerge';
11
+ import { next as A, type Prop } from '@dxos/automerge/automerge';
12
12
 
13
13
  import { cursorConverter } from './cursor';
14
- import { effectType, type IDocHandle, isReconcileTx, type Value } from './defs';
14
+ import { updateHeadsEffect, type IDocHandle, isReconcile, type State } from './defs';
15
15
  import { PatchSemaphore } from './semaphore';
16
16
  import { Cursor } from '../cursor';
17
17
 
18
- const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
19
- combine: (values) => values.at(-1)!, // Take last.
20
- });
21
-
22
18
  export type AutomergeOptions = {
23
19
  handle: IDocHandle;
24
20
  path: Prop[];
25
21
  };
26
22
 
27
23
  export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
28
- // TODO(burdon): Rename.
29
- const stateField: StateField<Value> = StateField.define({
24
+ const syncState = StateField.define<State>({
30
25
  create: () => ({
26
+ path: path.slice(),
31
27
  lastHeads: A.getHeads(handle.docSync()!),
32
28
  unreconciledTransactions: [],
33
- path: path.slice(),
34
29
  }),
35
- update: (value: Value, tr: Transaction) => {
36
- const result: Value = {
30
+
31
+ update: (value, tr) => {
32
+ const result: State = {
33
+ path: path.slice(),
37
34
  lastHeads: value.lastHeads,
38
35
  unreconciledTransactions: value.unreconciledTransactions.slice(),
39
- path: path.slice(),
40
36
  };
37
+
41
38
  let clearUnreconciled = false;
42
39
  for (const effect of tr.effects) {
43
- if (effect.is(effectType)) {
40
+ if (effect.is(updateHeadsEffect)) {
44
41
  result.lastHeads = effect.value.newHeads;
45
42
  clearUnreconciled = true;
46
43
  }
47
44
  }
45
+
48
46
  if (clearUnreconciled) {
49
47
  result.unreconciledTransactions = [];
50
48
  } else {
51
- if (!isReconcileTx(tr)) {
49
+ if (!isReconcile(tr)) {
52
50
  result.unreconciledTransactions.push(tr);
53
51
  }
54
52
  }
53
+
55
54
  return result;
56
55
  },
57
56
  });
58
57
 
59
- const semaphore = new PatchSemaphore(stateField);
58
+ const semaphore = new PatchSemaphore(handle, syncState);
60
59
 
61
- const viewPlugin = ViewPlugin.fromClass(
62
- class AutomergeCodemirrorViewPlugin implements PluginValue {
63
- constructor(private readonly _view: EditorView) {
64
- handle.addListener('change', this._handleChange);
65
- }
60
+ return [
61
+ Cursor.converter.of(cursorConverter(handle, path)),
62
+ syncState,
66
63
 
67
- update(update: ViewUpdate) {
68
- if (update.transactions.length > 0 && update.transactions.some((tr) => !isReconcileTx(tr))) {
69
- // TODO(burdon): This is a hack to ensure that the view is updated after the transaction is applied.
70
- queueMicrotask(() => {
71
- this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
72
- });
64
+ // Reconcile external updates.
65
+ ViewPlugin.fromClass(
66
+ class {
67
+ constructor(private readonly _view: EditorView) {
68
+ handle.addListener('change', this._handleChange.bind(this));
73
69
  }
70
+
71
+ destroy() {
72
+ handle.removeListener('change', this._handleChange.bind(this));
73
+ }
74
+
75
+ _handleChange() {
76
+ semaphore.reconcile(this._view);
77
+ }
78
+ },
79
+ ),
80
+
81
+ // Reconcile local updates.
82
+ EditorView.updateListener.of(({ view, changes }) => {
83
+ if (!changes.empty) {
84
+ semaphore.reconcile(view);
74
85
  }
86
+ }),
75
87
 
76
- destroy() {
77
- handle.addListener('change', this._handleChange);
88
+ // TODO(burdon): Record undo transactions.
89
+ // See https://github.com/yjs/y-codemirror.next/tree/main
90
+ // https://codemirror.net/examples/inverted-effect
91
+ invertedEffects.of((tr) => {
92
+ // Each change results it three transactions: insert, delete, insert.
93
+ const effects: StateEffect<any>[] = [];
94
+ if (!tr.changes.empty) {
95
+ tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
96
+ // effects.push(effectType.of({});
97
+ });
78
98
  }
79
99
 
80
- private _handleChange = () => {
81
- this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
82
- };
83
- },
84
- );
85
-
86
- return {
87
- extension: [
88
- Cursor.converter.of(cursorConverter(handle, path)),
89
- semaphoreFacet.of(semaphore),
90
- stateField,
91
- viewPlugin,
92
- ],
93
- };
100
+ return effects;
101
+ }),
102
+ ];
94
103
  };
@@ -5,7 +5,7 @@
5
5
  import get from 'lodash.get';
6
6
 
7
7
  import type { Prop } from '@dxos/automerge/automerge';
8
- import { next as automerge } from '@dxos/automerge/automerge';
8
+ import { next as A } from '@dxos/automerge/automerge';
9
9
 
10
10
  import { type IDocHandle } from './defs';
11
11
  import { type CursorConverter } from '../cursor';
@@ -24,7 +24,7 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
24
24
  }
25
25
 
26
26
  // NOTE: Slice is needed because getCursor mutates the array.
27
- return automerge.getCursor(doc, path.slice(), pos);
27
+ return A.getCursor(doc, path.slice(), pos);
28
28
  },
29
29
 
30
30
  fromCursor: (cursor) => {
@@ -47,6 +47,6 @@ export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConvert
47
47
  }
48
48
 
49
49
  // NOTE: Slice is needed because getCursor mutates the array.
50
- return automerge.getCursorPosition(doc, path.slice(), cursor);
50
+ return A.getCursorPosition(doc, path.slice(), cursor);
51
51
  },
52
52
  });
@@ -1,55 +1,34 @@
1
1
  //
2
2
  // Copyright 2023 DXOS.org
3
+ // Copyright 2024 Automerge
3
4
  // Ref: https://github.com/automerge/automerge-codemirror
4
5
  //
5
6
 
6
- import {
7
- Annotation,
8
- StateEffect,
9
- type StateField,
10
- type EditorState,
11
- type Transaction,
12
- type TransactionSpec,
13
- } from '@codemirror/state';
7
+ import { Annotation, StateEffect, type StateField, type EditorState, type Transaction } from '@codemirror/state';
14
8
 
15
9
  import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
16
10
 
17
- export type Value = {
18
- lastHeads: Heads;
11
+ export type State = {
19
12
  path: Prop[];
13
+ lastHeads: Heads;
20
14
  unreconciledTransactions: Transaction[];
21
15
  };
22
16
 
23
- type UpdateHeads = {
17
+ export const getPath = (state: EditorState, field: StateField<State>): Prop[] => state.field(field).path;
18
+ export const getLastHeads = (state: EditorState, field: StateField<State>): Heads => state.field(field).lastHeads;
19
+
20
+ export type UpdateHeads = {
24
21
  newHeads: Heads;
25
22
  };
26
23
 
27
- export const effectType = StateEffect.define<UpdateHeads>({});
28
-
29
- export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => effectType.of({ newHeads });
30
-
31
- export const getLastHeads = (state: EditorState, field: StateField<Value>): Heads => state.field(field).lastHeads;
32
-
33
- export const getPath = (state: EditorState, field: StateField<Value>): Prop[] => state.field(field).path;
24
+ export const updateHeadsEffect = StateEffect.define<UpdateHeads>({});
34
25
 
35
- export const reconcileAnnotationType = Annotation.define<unknown>();
26
+ export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => updateHeadsEffect.of({ newHeads });
36
27
 
37
- export const isReconcileTx = (tr: Transaction): boolean => !!tr.annotation(reconcileAnnotationType);
28
+ export const reconcileAnnotation = Annotation.define<boolean>();
38
29
 
39
- export const makeReconcile = (tr: TransactionSpec) => {
40
- if (tr.annotations != null) {
41
- if (tr.annotations instanceof Array) {
42
- tr.annotations = [...tr.annotations, reconcileAnnotationType.of({})];
43
- } else {
44
- tr.annotations = [tr.annotations, reconcileAnnotationType.of({})];
45
- }
46
- } else {
47
- tr.annotations = [reconcileAnnotationType.of({})];
48
- }
49
- // return {
50
- // ...tr,
51
- // annotations: reconcileAnnotationType.of({})
52
- // }
30
+ export const isReconcile = (tr: Transaction): boolean => {
31
+ return !!tr.annotation(reconcileAnnotation);
53
32
  };
54
33
 
55
34
  export type IDocHandle<T = any> = {
@@ -4,4 +4,3 @@
4
4
 
5
5
  export { automerge } from './automerge';
6
6
  export { type IDocHandle } from './defs';
7
- export { cursorConverter } from './cursor';