@dxos/react-ui-editor 0.3.11-main.ec7a2b3 → 0.3.11-main.ee2b64c
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.
- package/dist/lib/browser/index.mjs +1702 -1100
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +13 -15
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +14 -6
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/{yjs.stories.d.ts → hooks.stories.d.ts} +13 -2
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
- package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +9 -11
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +0 -1
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +8 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/awareness.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +35 -18
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +1 -7
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/bundle.d.ts +3 -6
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts +13 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
- package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts +8 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -1
- package/dist/types/src/extensions/outliner.d.ts +4 -0
- package/dist/types/src/extensions/outliner.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +4 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
- package/dist/types/src/hooks/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +39 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +14 -27
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts +2 -2
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +48 -38
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +73 -82
- package/src/components/TextEditor/TextEditor.tsx +111 -75
- package/src/components/TextEditor/automerge.stories.tsx +1 -2
- package/src/components/TextEditor/hooks.stories.tsx +106 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
- package/src/components/Toolbar/Toolbar.tsx +166 -0
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +54 -45
- package/src/extensions/automerge/cursor.ts +3 -3
- package/src/extensions/automerge/defs.ts +13 -34
- package/src/extensions/automerge/index.ts +0 -1
- package/src/extensions/automerge/semaphore.ts +33 -32
- package/src/extensions/automerge/update-automerge.ts +14 -8
- package/src/extensions/automerge/update-codemirror.ts +5 -5
- package/src/extensions/awareness.ts +22 -18
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +327 -166
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +1 -7
- package/src/extensions/markdown/bundle.ts +23 -28
- package/src/extensions/markdown/code.ts +150 -0
- package/src/extensions/markdown/formatting.ts +188 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -15
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +16 -7
- package/src/extensions/markdown/tasklist.ts +110 -0
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +5 -2
- package/src/hooks/useActionHandler.ts +62 -0
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +153 -0
- package/src/hooks/useTextModel.ts +66 -56
- package/src/index.ts +1 -1
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +11 -8
- package/src/util.ts +26 -3
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts +0 -26
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- package/dist/types/src/extensions/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/comments.stories.tsx +0 -315
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -106
- package/src/extensions/hr.ts +0 -73
- package/src/extensions/tasklist.ts +0 -119
- package/src/extensions/yjs/cursor.ts +0 -22
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -156
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
- /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type Icon,
|
|
7
|
+
At,
|
|
8
|
+
CaretRight,
|
|
9
|
+
ChatText,
|
|
10
|
+
Code,
|
|
11
|
+
Image,
|
|
12
|
+
Link,
|
|
13
|
+
ListBullets,
|
|
14
|
+
ListChecks,
|
|
15
|
+
ListNumbers,
|
|
16
|
+
TextStrikethrough,
|
|
17
|
+
Table,
|
|
18
|
+
TextB,
|
|
19
|
+
TextItalic,
|
|
20
|
+
} from '@phosphor-icons/react';
|
|
21
|
+
import { createContext } from '@radix-ui/react-context';
|
|
22
|
+
import React, { type PropsWithChildren } from 'react';
|
|
23
|
+
|
|
24
|
+
import { Button, type ButtonProps, DensityProvider, Select } from '@dxos/react-ui';
|
|
25
|
+
import { getSize } from '@dxos/react-ui-theme';
|
|
26
|
+
|
|
27
|
+
// TODO(burdon): Revert to string to make extensible?
|
|
28
|
+
export type ActionType =
|
|
29
|
+
| 'blockquote'
|
|
30
|
+
| 'bold'
|
|
31
|
+
| 'codeblock'
|
|
32
|
+
| 'comment'
|
|
33
|
+
| 'heading'
|
|
34
|
+
| 'image'
|
|
35
|
+
| 'italic'
|
|
36
|
+
| 'link'
|
|
37
|
+
| 'list'
|
|
38
|
+
| 'list-ordered'
|
|
39
|
+
| 'list-tasks'
|
|
40
|
+
| 'mention'
|
|
41
|
+
| 'prompt'
|
|
42
|
+
| 'strikethrough'
|
|
43
|
+
| 'table';
|
|
44
|
+
|
|
45
|
+
export type Action = {
|
|
46
|
+
type: ActionType;
|
|
47
|
+
data?: any;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type ToolbarProps = PropsWithChildren<{
|
|
51
|
+
onAction?: (action: Action) => void;
|
|
52
|
+
}>;
|
|
53
|
+
|
|
54
|
+
const [ToolbarContextProvider, useToolbarContext] = createContext<ToolbarProps>('Toolbar');
|
|
55
|
+
|
|
56
|
+
const ToolbarRoot = ({ children, onAction }: ToolbarProps) => {
|
|
57
|
+
return (
|
|
58
|
+
<ToolbarContextProvider onAction={onAction}>
|
|
59
|
+
<DensityProvider density='fine'>
|
|
60
|
+
<div role='toolbar' className='flex w-full shrink-0 p-2 gap-4 items-center whitespace-nowrap overflow-hidden'>
|
|
61
|
+
{children}
|
|
62
|
+
</div>
|
|
63
|
+
</DensityProvider>
|
|
64
|
+
</ToolbarContextProvider>
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
type ToolbarButtonProps = {
|
|
69
|
+
Icon: Icon;
|
|
70
|
+
onClick: () => Action | undefined;
|
|
71
|
+
} & NonNullable<Pick<ButtonProps, 'title'>>;
|
|
72
|
+
|
|
73
|
+
const ToolbarButton = ({ Icon, onClick, title }: ToolbarButtonProps) => {
|
|
74
|
+
const { onAction } = useToolbarContext('ToolbarButton');
|
|
75
|
+
const handleClick = () => {
|
|
76
|
+
const action = onClick();
|
|
77
|
+
if (action) {
|
|
78
|
+
onAction?.(action);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<Button variant='ghost' classNames='p-2' onClick={handleClick} title={title}>
|
|
84
|
+
<Icon className={getSize(5)} />
|
|
85
|
+
</Button>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const ToolbarSeparator = () => <div className='grow' />;
|
|
90
|
+
|
|
91
|
+
const MarkdownHeading = () => {
|
|
92
|
+
const { onAction } = useToolbarContext('MarkdownFormatting');
|
|
93
|
+
return (
|
|
94
|
+
<Select.Root defaultValue='0' onValueChange={(value) => onAction?.({ type: 'heading', data: parseInt(value) })}>
|
|
95
|
+
<Select.TriggerButton classNames='w-[8rem]' />
|
|
96
|
+
<Select.Portal>
|
|
97
|
+
<Select.Content>
|
|
98
|
+
<Select.Viewport>
|
|
99
|
+
<Select.Option value='0'>Paragraph</Select.Option>
|
|
100
|
+
{[1, 2, 3, 4, 5, 6].map((level) => (
|
|
101
|
+
<Select.Option key={level} value={String(level)}>
|
|
102
|
+
Heading {level}
|
|
103
|
+
</Select.Option>
|
|
104
|
+
))}
|
|
105
|
+
</Select.Viewport>
|
|
106
|
+
</Select.Content>
|
|
107
|
+
</Select.Portal>
|
|
108
|
+
</Select.Root>
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const MarkdownStyles = () => (
|
|
113
|
+
<div role='none'>
|
|
114
|
+
<ToolbarButton Icon={TextB} title='Bold' onClick={() => ({ type: 'bold' })} />
|
|
115
|
+
<ToolbarButton Icon={TextItalic} title='Italic' onClick={() => ({ type: 'italic' })} />
|
|
116
|
+
<ToolbarButton Icon={TextStrikethrough} title='Strike-through' onClick={() => ({ type: 'strikethrough' })} />
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const MarkdownLists = () => (
|
|
121
|
+
<div role='none'>
|
|
122
|
+
<ToolbarButton Icon={ListBullets} title='Bullet list' onClick={() => ({ type: 'list' })} />
|
|
123
|
+
<ToolbarButton Icon={ListNumbers} title='Numbered list' onClick={() => ({ type: 'list-ordered' })} />
|
|
124
|
+
<ToolbarButton Icon={ListChecks} title='Task list' onClick={() => ({ type: 'list-tasks' })} />
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const MarkdownBlocks = () => (
|
|
129
|
+
<div role='none'>
|
|
130
|
+
<ToolbarButton Icon={CaretRight} title='Block quite' onClick={() => ({ type: 'blockquote' })} />
|
|
131
|
+
<ToolbarButton Icon={Code} title='Code block' onClick={() => ({ type: 'codeblock' })} />
|
|
132
|
+
<ToolbarButton Icon={Table} title='Table' onClick={() => ({ type: 'table' })} />
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const MarkdownLinks = () => (
|
|
137
|
+
<div role='none'>
|
|
138
|
+
<ToolbarButton Icon={Link} title='Link' onClick={() => ({ type: 'link' })} />
|
|
139
|
+
<ToolbarButton Icon={At} title='Mention' onClick={() => ({ type: 'mention' })} />
|
|
140
|
+
<ToolbarButton Icon={Image} title='Image' onClick={() => ({ type: 'image' })} />
|
|
141
|
+
</div>
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const MarkdownStandard = () => (
|
|
145
|
+
<>
|
|
146
|
+
<MarkdownHeading />
|
|
147
|
+
<MarkdownStyles />
|
|
148
|
+
<MarkdownLists />
|
|
149
|
+
<MarkdownBlocks />
|
|
150
|
+
<MarkdownLinks />
|
|
151
|
+
</>
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const MarkdownExtended = () => (
|
|
155
|
+
<Toolbar.Button Icon={ChatText} title='Create comment' onClick={() => ({ type: 'comment' })} />
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
export const Toolbar = {
|
|
159
|
+
Root: ToolbarRoot,
|
|
160
|
+
Button: ToolbarButton,
|
|
161
|
+
Separator: ToolbarSeparator,
|
|
162
|
+
Markdown: MarkdownStandard,
|
|
163
|
+
Extended: MarkdownExtended,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export { useToolbarContext };
|
package/src/components/index.ts
CHANGED
|
@@ -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
|
-
|
|
38
|
+
activateOnTyping: true,
|
|
39
39
|
|
|
40
|
-
//
|
|
41
|
-
|
|
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
|
+
});
|
|
@@ -4,91 +4,100 @@
|
|
|
4
4
|
// Ref: https://github.com/automerge/automerge-codemirror
|
|
5
5
|
//
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
36
|
-
|
|
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(
|
|
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 (!
|
|
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(
|
|
58
|
+
const semaphore = new PatchSemaphore(handle, syncState);
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
handle.addListener('change', this._handleChange);
|
|
65
|
-
}
|
|
60
|
+
return [
|
|
61
|
+
Cursor.converter.of(cursorConverter(handle, path)),
|
|
62
|
+
syncState,
|
|
66
63
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
77
|
-
|
|
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
|
-
|
|
81
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
18
|
-
lastHeads: Heads;
|
|
11
|
+
export type State = {
|
|
19
12
|
path: Prop[];
|
|
13
|
+
lastHeads: Heads;
|
|
20
14
|
unreconciledTransactions: Transaction[];
|
|
21
15
|
};
|
|
22
16
|
|
|
23
|
-
|
|
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
|
|
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
|
|
26
|
+
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => updateHeadsEffect.of({ newHeads });
|
|
36
27
|
|
|
37
|
-
export const
|
|
28
|
+
export const reconcileAnnotation = Annotation.define<boolean>();
|
|
38
29
|
|
|
39
|
-
export const
|
|
40
|
-
|
|
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> = {
|
|
@@ -7,74 +7,75 @@
|
|
|
7
7
|
import { type StateField } from '@codemirror/state';
|
|
8
8
|
import { type EditorView } from '@codemirror/view';
|
|
9
9
|
|
|
10
|
-
import { next as
|
|
10
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
13
|
getLastHeads,
|
|
14
14
|
getPath,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
isReconcile,
|
|
16
|
+
reconcileAnnotation,
|
|
17
17
|
updateHeads,
|
|
18
18
|
type IDocHandle,
|
|
19
|
-
type
|
|
19
|
+
type State,
|
|
20
20
|
} from './defs';
|
|
21
21
|
import { updateAutomerge } from './update-automerge';
|
|
22
22
|
import { updateCodeMirror } from './update-codemirror';
|
|
23
23
|
|
|
24
|
-
type Doc<T> = automerge.Doc<T>;
|
|
25
|
-
type Heads = automerge.Heads;
|
|
26
|
-
|
|
27
|
-
type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
|
|
28
|
-
|
|
29
24
|
/**
|
|
30
|
-
*
|
|
25
|
+
* Implements three-way merge (on each mutation).
|
|
31
26
|
*/
|
|
32
27
|
export class PatchSemaphore {
|
|
33
|
-
_inReconcile = false;
|
|
34
|
-
_queue: Array<ChangeFn> = [];
|
|
28
|
+
private _inReconcile = false;
|
|
35
29
|
|
|
36
|
-
|
|
30
|
+
// prettier-ignore
|
|
31
|
+
constructor(
|
|
32
|
+
private readonly _handle: IDocHandle,
|
|
33
|
+
private readonly _state: StateField<State>
|
|
34
|
+
) {}
|
|
37
35
|
|
|
38
|
-
reconcile
|
|
39
|
-
if (this._inReconcile) {
|
|
40
|
-
|
|
36
|
+
reconcile(view: EditorView) {
|
|
37
|
+
if (!this._inReconcile) {
|
|
38
|
+
this._inReconcile = true;
|
|
39
|
+
this.doReconcile(view);
|
|
40
|
+
this._inReconcile = false;
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private doReconcile(view: EditorView) {
|
|
45
|
+
const path = getPath(view.state, this._state);
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
const oldHeads = getLastHeads(view.state, this.
|
|
47
|
+
// Get the heads before the unreconciled transactions are applied.
|
|
48
|
+
const oldHeads = getLastHeads(view.state, this._state);
|
|
46
49
|
let selection = view.state.selection;
|
|
47
50
|
|
|
48
51
|
// First undo all the unreconciled transactions.
|
|
49
|
-
const transactions = view.state.field(this.
|
|
52
|
+
const transactions = view.state.field(this._state).unreconciledTransactions.filter((tx) => !isReconcile(tx));
|
|
50
53
|
const toInvert = transactions.slice().reverse();
|
|
51
|
-
for (const
|
|
52
|
-
const inverted =
|
|
54
|
+
for (const tr of toInvert) {
|
|
55
|
+
const inverted = tr.changes.invert(tr.startState.doc);
|
|
53
56
|
selection = selection.map(inverted);
|
|
54
57
|
view.dispatch({
|
|
55
58
|
changes: inverted,
|
|
56
|
-
annotations:
|
|
59
|
+
annotations: reconcileAnnotation.of(true),
|
|
57
60
|
});
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
// Apply the unreconciled transactions to the document.
|
|
61
|
-
let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
|
|
62
|
-
|
|
63
64
|
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
65
|
+
let newHeads = updateAutomerge(this._state, this._handle, transactions, view.state);
|
|
64
66
|
if (newHeads === null || newHeads === undefined) {
|
|
65
|
-
// TODO:
|
|
66
|
-
newHeads =
|
|
67
|
+
// TODO(alexjg): this is the call that's resetting the editor state on click.
|
|
68
|
+
newHeads = A.getHeads(this._handle.docSync()!);
|
|
67
69
|
}
|
|
68
70
|
|
|
69
71
|
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
70
|
-
const diff =
|
|
72
|
+
const diff = A.equals(oldHeads, newHeads) ? [] : A.diff(this._handle.docSync()!, oldHeads, newHeads);
|
|
71
73
|
updateCodeMirror(view, selection, path, diff);
|
|
72
74
|
|
|
75
|
+
// Update automerge state.
|
|
73
76
|
view.dispatch({
|
|
74
77
|
effects: updateHeads(newHeads),
|
|
75
|
-
annotations:
|
|
78
|
+
annotations: reconcileAnnotation.of(false),
|
|
76
79
|
});
|
|
77
|
-
|
|
78
|
-
this._inReconcile = false;
|
|
79
|
-
};
|
|
80
|
+
}
|
|
80
81
|
}
|