@dxos/react-ui-editor 0.3.11-main.870164f → 0.3.11-main.8be6c8a
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 +1609 -1139
- 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 +9 -5
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +10 -10
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +2 -2
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/yjs → components/TextEditor}/yjs.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +4 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{hooks → extensions}/automerge/semaphore.d.ts +4 -4
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +5 -0
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/update-codemirror.d.ts.map +1 -0
- package/dist/types/src/extensions/awareness.d.ts +46 -0
- package/dist/types/src/extensions/awareness.d.ts.map +1 -0
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/image.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +3 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +3 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +8 -7
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +8 -8
- package/dist/types/src/hooks/yjs.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/cursors.d.ts.map +1 -0
- package/dist/types/src/styles/index.d.ts +1 -0
- package/dist/types/src/styles/index.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +1 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts +2 -2
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util/cursor.d.ts +17 -0
- package/dist/types/src/util/cursor.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/package.json +22 -21
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +13 -11
- package/src/components/TextEditor/TextEditor.stories.tsx +3 -4
- package/src/components/TextEditor/TextEditor.tsx +60 -46
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/extensions/autocomplete.ts +18 -17
- package/src/{hooks/automerge/plugin.ts → extensions/automerge/automerge.ts} +20 -58
- package/src/extensions/automerge/cursor.ts +51 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +7 -0
- package/src/{hooks → extensions}/automerge/semaphore.ts +14 -12
- package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
- package/src/extensions/awareness.ts +316 -0
- package/src/extensions/code.ts +4 -5
- package/src/extensions/comments.ts +30 -15
- package/src/extensions/hr.ts +2 -3
- package/src/extensions/image.ts +38 -20
- package/src/extensions/index.ts +3 -1
- package/src/extensions/link.ts +82 -84
- package/src/extensions/markdown/highlight.ts +3 -4
- package/src/extensions/table.ts +9 -3
- package/src/extensions/tasklist.ts +1 -3
- package/src/extensions/yjs/cursor.ts +21 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +1 -25
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTextModel.ts +20 -94
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +71 -19
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/tokens.ts +3 -0
- package/src/testing/replicator.ts +10 -15
- package/src/themes/default.ts +39 -33
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -0
- package/dist/types/src/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/extensions/util.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/handle.d.ts +0 -9
- package/dist/types/src/hooks/automerge/handle.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/index.d.ts +0 -3
- package/dist/types/src/hooks/automerge/index.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/plugin.d.ts +0 -26
- package/dist/types/src/hooks/automerge/plugin.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/semaphore.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-automerge.d.ts +0 -6
- package/dist/types/src/hooks/automerge/update-automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge/update-codemirror.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/cursors.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/index.d.ts +0 -3
- package/dist/types/src/hooks/yjs/index.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -11
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/yjs/index.ts +0 -6
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/yjs/yjs.test.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/dist/types/src/{extensions → util}/util.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{extensions → util}/util.ts +0 -0
|
@@ -15,6 +15,7 @@ import React, {
|
|
|
15
15
|
useEffect,
|
|
16
16
|
useImperativeHandle,
|
|
17
17
|
useState,
|
|
18
|
+
useRef,
|
|
18
19
|
} from 'react';
|
|
19
20
|
|
|
20
21
|
import { generateName } from '@dxos/display-name';
|
|
@@ -34,15 +35,10 @@ export type CursorInfo = {
|
|
|
34
35
|
to: number;
|
|
35
36
|
line: number;
|
|
36
37
|
lines: number;
|
|
38
|
+
length: number;
|
|
37
39
|
after?: string;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
|
-
export type TextEditorRef = {
|
|
41
|
-
root: HTMLDivElement | null;
|
|
42
|
-
state?: EditorState;
|
|
43
|
-
view?: EditorView;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
42
|
export type TextEditorSlots = {
|
|
47
43
|
root?: Omit<ComponentProps<'div'>, 'ref'>;
|
|
48
44
|
editor?: {
|
|
@@ -56,6 +52,8 @@ export type TextEditorSlots = {
|
|
|
56
52
|
|
|
57
53
|
export type TextEditorProps = {
|
|
58
54
|
model: EditorModel;
|
|
55
|
+
focus?: boolean;
|
|
56
|
+
selection?: { anchor: number; head?: number };
|
|
59
57
|
readonly?: boolean; // TODO(burdon): Move into model.
|
|
60
58
|
comments?: CommentRange[]; // TODO(burdon): Move into extension.
|
|
61
59
|
extensions?: Extension[];
|
|
@@ -65,19 +63,28 @@ export type TextEditorProps = {
|
|
|
65
63
|
|
|
66
64
|
/**
|
|
67
65
|
* Base text editor.
|
|
68
|
-
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
69
66
|
*/
|
|
70
|
-
export const BaseTextEditor = forwardRef<
|
|
71
|
-
(
|
|
72
|
-
|
|
67
|
+
export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
68
|
+
(
|
|
69
|
+
{ model, focus, selection, readonly, comments, extensions = [], editorMode, slots = defaultSlots },
|
|
70
|
+
forwardedRef,
|
|
71
|
+
) => {
|
|
73
72
|
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
74
|
-
const
|
|
75
|
-
const [{ state = undefined, view = undefined } = {}, setEditor] = useState<
|
|
76
|
-
{ state?: EditorState; view?: EditorView } | undefined
|
|
77
|
-
>();
|
|
78
|
-
useImperativeHandle(forwardedRef, () => ({ root, state, view }), [view, state, root]);
|
|
73
|
+
const { themeMode } = useThemeContext();
|
|
79
74
|
|
|
80
|
-
//
|
|
75
|
+
// The editor view ref should only be used as an escape hatch.
|
|
76
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
77
|
+
const [view, setView] = useState<EditorView | null>(null);
|
|
78
|
+
useImperativeHandle<EditorView | null, EditorView | null>(forwardedRef, () => view, [view]);
|
|
79
|
+
|
|
80
|
+
// Focus.
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (view && focus) {
|
|
83
|
+
view.focus();
|
|
84
|
+
}
|
|
85
|
+
}, [view, focus]);
|
|
86
|
+
|
|
87
|
+
// TODO(burdon): Factor out as extension.
|
|
81
88
|
const { awareness, peer } = model;
|
|
82
89
|
useEffect(() => {
|
|
83
90
|
if (awareness && peer) {
|
|
@@ -99,7 +106,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
99
106
|
}, [view, comments]);
|
|
100
107
|
|
|
101
108
|
useEffect(() => {
|
|
102
|
-
if (!
|
|
109
|
+
if (!model || !rootRef.current) {
|
|
103
110
|
return;
|
|
104
111
|
}
|
|
105
112
|
|
|
@@ -107,6 +114,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
107
114
|
// https://codemirror.net/docs/ref/#state.EditorStateConfig
|
|
108
115
|
const state = EditorState.create({
|
|
109
116
|
doc: model.text(),
|
|
117
|
+
selection,
|
|
110
118
|
extensions: [
|
|
111
119
|
readonly && EditorState.readOnly.of(readonly),
|
|
112
120
|
|
|
@@ -131,36 +139,35 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
131
139
|
// If the new state is derived from the old state, it will likely not be visible other than the cursor resetting.
|
|
132
140
|
// Ideally this should not happen except when changing between text objects.
|
|
133
141
|
view?.destroy();
|
|
134
|
-
|
|
142
|
+
const newView = new EditorView({
|
|
143
|
+
parent: rootRef.current,
|
|
135
144
|
state,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// dispatch: (transaction, view) => {
|
|
142
|
-
// view.update([transaction]);
|
|
143
|
-
// },
|
|
144
|
-
}),
|
|
145
|
+
// NOTE: Uncomment to spy on all transactions.
|
|
146
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
147
|
+
// dispatch: (transaction, view) => {
|
|
148
|
+
// view.update([transaction]);
|
|
149
|
+
// },
|
|
145
150
|
});
|
|
146
151
|
|
|
152
|
+
setView(newView);
|
|
147
153
|
return () => {
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
newView?.destroy();
|
|
155
|
+
setView(null);
|
|
150
156
|
};
|
|
151
|
-
}, [
|
|
157
|
+
}, [rootRef, model, readonly, editorMode, themeMode]);
|
|
152
158
|
|
|
153
159
|
const handleKeyUp = useCallback(
|
|
154
160
|
(event: KeyboardEvent) => {
|
|
155
161
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
156
162
|
switch (key) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
163
|
+
// TODO(burdon): ???
|
|
164
|
+
// case 'Enter': {
|
|
165
|
+
// view?.contentDOM.focus();
|
|
166
|
+
// break;
|
|
167
|
+
// }
|
|
161
168
|
|
|
162
169
|
case 'Escape': {
|
|
163
|
-
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) &&
|
|
170
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && rootRef.current?.focus();
|
|
164
171
|
break;
|
|
165
172
|
}
|
|
166
173
|
}
|
|
@@ -171,7 +178,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
171
178
|
return (
|
|
172
179
|
<div
|
|
173
180
|
key={model.id}
|
|
174
|
-
ref={
|
|
181
|
+
ref={rootRef}
|
|
175
182
|
tabIndex={0}
|
|
176
183
|
{...slots?.root}
|
|
177
184
|
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
@@ -181,32 +188,39 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
181
188
|
},
|
|
182
189
|
);
|
|
183
190
|
|
|
184
|
-
export const TextEditor = forwardRef<
|
|
185
|
-
({ readonly, extensions = [], slots
|
|
191
|
+
export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
192
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
186
193
|
const { themeMode } = useThemeContext();
|
|
187
|
-
const
|
|
194
|
+
const updatedSlots = defaultsDeep({}, slots, defaultTextSlots);
|
|
188
195
|
return (
|
|
189
196
|
<BaseTextEditor
|
|
190
197
|
ref={forwardedRef}
|
|
191
198
|
readonly={readonly}
|
|
192
|
-
extensions={[
|
|
193
|
-
|
|
199
|
+
extensions={[
|
|
200
|
+
basicBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
201
|
+
...extensions,
|
|
202
|
+
]}
|
|
203
|
+
slots={updatedSlots}
|
|
194
204
|
{...props}
|
|
195
205
|
/>
|
|
196
206
|
);
|
|
197
207
|
},
|
|
198
208
|
);
|
|
199
209
|
|
|
200
|
-
|
|
201
|
-
|
|
210
|
+
// TODO(burdon): Remove (Just provide bundle, slots).
|
|
211
|
+
export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
212
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
202
213
|
const { themeMode } = useThemeContext();
|
|
203
|
-
const
|
|
214
|
+
const updatedSlots = defaultsDeep({}, slots, defaultMarkdownSlots);
|
|
204
215
|
return (
|
|
205
216
|
<BaseTextEditor
|
|
206
217
|
ref={forwardedRef}
|
|
207
218
|
readonly={readonly}
|
|
208
|
-
extensions={[
|
|
209
|
-
|
|
219
|
+
extensions={[
|
|
220
|
+
markdownBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
221
|
+
...extensions,
|
|
222
|
+
]}
|
|
223
|
+
slots={updatedSlots}
|
|
210
224
|
{...props}
|
|
211
225
|
/>
|
|
212
226
|
);
|
|
@@ -2,19 +2,29 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
|
|
6
7
|
import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-network-broadcastchannel';
|
|
7
8
|
import { EditorView } from '@codemirror/view';
|
|
9
|
+
import '@preact/signals-react'; // Register react integration
|
|
8
10
|
import { basicSetup } from 'codemirror';
|
|
9
11
|
import get from 'lodash.get';
|
|
10
12
|
import React, { useEffect, useRef, useState } from 'react';
|
|
11
13
|
|
|
12
|
-
// TODO(burdon): Why separate imports?
|
|
13
14
|
import { type Prop } from '@dxos/automerge/automerge';
|
|
14
|
-
import { type DocHandle
|
|
15
|
+
import { Repo, type DocHandle } from '@dxos/automerge/automerge-repo';
|
|
16
|
+
import { Filter, setGlobalAutomergePreference } from '@dxos/echo-schema';
|
|
17
|
+
import { type PublicKey } from '@dxos/keys';
|
|
18
|
+
import { Expando, TextObject, useSpace } from '@dxos/react-client/echo';
|
|
19
|
+
import { useIdentity } from '@dxos/react-client/halo';
|
|
20
|
+
import { ClientRepeater } from '@dxos/react-client/testing';
|
|
21
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
15
22
|
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
23
|
+
import { MarkdownEditor } from './TextEditor';
|
|
24
|
+
import { type IDocHandle, automerge, awareness } from '../../extensions';
|
|
25
|
+
import { useTextModel } from '../../hooks';
|
|
26
|
+
|
|
27
|
+
// TODO(burdon): Move to components.
|
|
18
28
|
|
|
19
29
|
type EditorProps = {
|
|
20
30
|
handle: IDocHandle;
|
|
@@ -28,7 +38,7 @@ const Editor = ({ handle, path }: EditorProps) => {
|
|
|
28
38
|
useEffect(() => {
|
|
29
39
|
const view = (editorRoot.current = new EditorView({
|
|
30
40
|
doc: get(handle.docSync()!, path),
|
|
31
|
-
extensions: [basicSetup,
|
|
41
|
+
extensions: [basicSetup, automerge({ handle, path }), awareness()],
|
|
32
42
|
parent: containerRef.current as any,
|
|
33
43
|
}));
|
|
34
44
|
|
|
@@ -89,3 +99,53 @@ export default {
|
|
|
89
99
|
};
|
|
90
100
|
|
|
91
101
|
export const Default = {};
|
|
102
|
+
|
|
103
|
+
const EchoStory = ({ id, spaceKey }: { id: number; spaceKey: PublicKey }) => {
|
|
104
|
+
const identity = useIdentity();
|
|
105
|
+
const space = useSpace(spaceKey);
|
|
106
|
+
// TODO(dmaretskyi): useQuery doesn't work.
|
|
107
|
+
const [obj] = space?.db.query(Filter.from({ type: 'test' })).objects ?? [];
|
|
108
|
+
|
|
109
|
+
const model = useTextModel({
|
|
110
|
+
text: obj?.content,
|
|
111
|
+
identity,
|
|
112
|
+
space,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!model) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
// <div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
121
|
+
<div className='flex justify-center overflow-y-scroll'>
|
|
122
|
+
<div className='flex flex-col w-[800px] py-16'>
|
|
123
|
+
<MarkdownEditor model={model} />
|
|
124
|
+
<div className='flex shrink-0 h-[300px]'></div>
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
// </div>
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const WithEcho = {
|
|
132
|
+
render: () => {
|
|
133
|
+
setGlobalAutomergePreference(true);
|
|
134
|
+
return (
|
|
135
|
+
<ClientRepeater
|
|
136
|
+
count={2}
|
|
137
|
+
createSpace
|
|
138
|
+
onCreateSpace={async (space) => {
|
|
139
|
+
space.db.add(
|
|
140
|
+
new Expando({
|
|
141
|
+
type: 'test',
|
|
142
|
+
content: new TextObject('Hello world!'),
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}}
|
|
146
|
+
Component={EchoStory}
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
},
|
|
150
|
+
decorators: [withTheme],
|
|
151
|
+
};
|
|
@@ -17,7 +17,8 @@ import { type Identity } from '@dxos/react-client/halo';
|
|
|
17
17
|
import { joinCommonSpace, TestBuilder, textGenerator } from '@dxos/react-client/testing';
|
|
18
18
|
import { YText } from '@dxos/text-model';
|
|
19
19
|
|
|
20
|
-
import {
|
|
20
|
+
import { YAwarenessProvider } from '../../hooks';
|
|
21
|
+
import { cursorColor } from '../../styles';
|
|
21
22
|
import { EditorDocument, types as schema } from '../../testing';
|
|
22
23
|
|
|
23
24
|
export default {
|
|
@@ -35,7 +36,7 @@ const updateEditor = async (id: number, editor: HTMLDivElement, identity: Identi
|
|
|
35
36
|
return;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
const { awareness } = new
|
|
39
|
+
const { awareness } = new YAwarenessProvider({ space, doc, channel: `yjs.awareness.${text.id}` });
|
|
39
40
|
awareness.setLocalStateField('user', {
|
|
40
41
|
name: identity.profile?.displayName ?? generateName(identity.identityKey.toHex()),
|
|
41
42
|
// TODO(wittjosiah): Pick colours from theme based on identity key.
|
|
@@ -12,7 +12,7 @@ import { ClientRepeater, textGenerator, useDataGenerator } from '@dxos/react-cli
|
|
|
12
12
|
import { useId, Input } from '@dxos/react-ui';
|
|
13
13
|
import { withTheme } from '@dxos/storybook-utils';
|
|
14
14
|
|
|
15
|
-
import { MarkdownEditor } from '
|
|
15
|
+
import { MarkdownEditor } from './TextEditor';
|
|
16
16
|
import { Replicator, useYjsModel } from '../../testing';
|
|
17
17
|
|
|
18
18
|
export default {
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type CompletionContext,
|
|
14
14
|
type CompletionResult,
|
|
15
15
|
} from '@codemirror/autocomplete';
|
|
16
|
+
import { markdownLanguage } from '@codemirror/lang-markdown';
|
|
16
17
|
import { keymap } from '@codemirror/view';
|
|
17
18
|
|
|
18
19
|
export type AutocompleteResult = Completion;
|
|
@@ -28,32 +29,32 @@ export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
|
|
|
28
29
|
return [
|
|
29
30
|
// https://codemirror.net/docs/ref/#view.keymap
|
|
30
31
|
// https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
|
|
32
|
+
// TODO(burdon): Set custom keymap.
|
|
31
33
|
keymap.of(completionKeymap),
|
|
32
34
|
|
|
33
35
|
// https://codemirror.net/examples/autocompletion
|
|
34
36
|
// https://codemirror.net/docs/ref/#autocomplete.autocompletion
|
|
35
37
|
autocompletion({
|
|
36
|
-
|
|
37
|
-
// defaultKeymap: false,
|
|
38
|
+
defaultKeymap: false,
|
|
38
39
|
|
|
39
40
|
// Don't start unless key press.
|
|
40
41
|
activateOnTyping: false,
|
|
42
|
+
}),
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
],
|
|
44
|
+
// TODO(burdon): Option to create new page?
|
|
45
|
+
// TODO(burdon): Optional decoration via addToOptions
|
|
46
|
+
markdownLanguage.data.of({
|
|
47
|
+
autocomplete: (context: CompletionContext): CompletionResult | null => {
|
|
48
|
+
const match = context.matchBefore(/\w*/);
|
|
49
|
+
if (!match || (match.from === match.to && !context.explicit)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
from: match.from,
|
|
55
|
+
options: onSearch(match.text.toLowerCase()),
|
|
56
|
+
};
|
|
57
|
+
},
|
|
57
58
|
}),
|
|
58
59
|
];
|
|
59
60
|
};
|
|
@@ -1,57 +1,34 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
import {
|
|
6
|
-
Annotation,
|
|
7
|
-
type EditorState,
|
|
8
|
-
type Extension,
|
|
9
|
-
Facet,
|
|
10
|
-
StateEffect,
|
|
11
|
-
StateField,
|
|
12
|
-
type Transaction,
|
|
13
|
-
type TransactionSpec,
|
|
14
|
-
} from '@codemirror/state';
|
|
7
|
+
import { Facet, StateField, type Extension, type Transaction } from '@codemirror/state';
|
|
15
8
|
import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '@codemirror/view';
|
|
16
9
|
|
|
17
|
-
import
|
|
18
|
-
import { type
|
|
10
|
+
import { next as A } from '@dxos/automerge/automerge';
|
|
11
|
+
import { type Prop } from '@dxos/automerge/automerge';
|
|
19
12
|
|
|
20
|
-
import {
|
|
13
|
+
import { cursorConverter } from './cursor';
|
|
14
|
+
import { effectType, type IDocHandle, isReconcileTx, type Value } from './defs';
|
|
21
15
|
import { PatchSemaphore } from './semaphore';
|
|
22
|
-
|
|
23
|
-
export type Value = {
|
|
24
|
-
lastHeads: Heads;
|
|
25
|
-
path: Prop[];
|
|
26
|
-
unreconciledTransactions: Transaction[];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
type UpdateHeads = {
|
|
30
|
-
newHeads: Heads;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const effectType = StateEffect.define<UpdateHeads>({});
|
|
34
|
-
|
|
35
|
-
export const updateHeads = (newHeads: Heads): StateEffect<UpdateHeads> => effectType.of({ newHeads });
|
|
36
|
-
|
|
37
|
-
export const getLastHeads = (state: EditorState, field: Field): Heads => state.field(field).lastHeads;
|
|
38
|
-
|
|
39
|
-
export const getPath = (state: EditorState, field: Field): Prop[] => state.field(field).path;
|
|
40
|
-
|
|
41
|
-
export type Field = StateField<Value>;
|
|
16
|
+
import { CursorConverter } from '../../util';
|
|
42
17
|
|
|
43
18
|
const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
44
19
|
combine: (values) => values.at(-1)!, // Take last.
|
|
45
20
|
});
|
|
46
21
|
|
|
47
|
-
export type
|
|
48
|
-
|
|
22
|
+
export type AutomergeOptions = {
|
|
23
|
+
handle: IDocHandle;
|
|
24
|
+
path: Prop[];
|
|
49
25
|
};
|
|
50
26
|
|
|
51
|
-
export const
|
|
27
|
+
export const automerge = ({ handle, path }: AutomergeOptions): Extension => {
|
|
28
|
+
// TODO(burdon): Rename.
|
|
52
29
|
const stateField: StateField<Value> = StateField.define({
|
|
53
30
|
create: () => ({
|
|
54
|
-
lastHeads:
|
|
31
|
+
lastHeads: A.getHeads(handle.docSync()!),
|
|
55
32
|
unreconciledTransactions: [],
|
|
56
33
|
path: path.slice(),
|
|
57
34
|
}),
|
|
@@ -107,26 +84,11 @@ export const automergePlugin = (handle: IDocHandle, path: Prop[]): AutomergePlug
|
|
|
107
84
|
);
|
|
108
85
|
|
|
109
86
|
return {
|
|
110
|
-
extension: [
|
|
87
|
+
extension: [
|
|
88
|
+
CursorConverter.of(cursorConverter(handle, path)),
|
|
89
|
+
semaphoreFacet.of(semaphore),
|
|
90
|
+
stateField,
|
|
91
|
+
viewPlugin,
|
|
92
|
+
],
|
|
111
93
|
};
|
|
112
94
|
};
|
|
113
|
-
|
|
114
|
-
export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
115
|
-
|
|
116
|
-
export const isReconcileTx = (tr: Transaction): boolean => !!tr.annotation(reconcileAnnotationType);
|
|
117
|
-
|
|
118
|
-
export const makeReconcile = (tr: TransactionSpec) => {
|
|
119
|
-
if (tr.annotations != null) {
|
|
120
|
-
if (tr.annotations instanceof Array) {
|
|
121
|
-
tr.annotations = [...tr.annotations, reconcileAnnotationType.of({})];
|
|
122
|
-
} else {
|
|
123
|
-
tr.annotations = [tr.annotations, reconcileAnnotationType.of({})];
|
|
124
|
-
}
|
|
125
|
-
} else {
|
|
126
|
-
tr.annotations = [reconcileAnnotationType.of({})];
|
|
127
|
-
}
|
|
128
|
-
// return {
|
|
129
|
-
// ...tr,
|
|
130
|
-
// annotations: reconcileAnnotationType.of({})
|
|
131
|
-
// }
|
|
132
|
-
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import get from 'lodash.get';
|
|
6
|
+
|
|
7
|
+
import type { Prop } from '@dxos/automerge/automerge';
|
|
8
|
+
import { next as automerge } from '@dxos/automerge/automerge';
|
|
9
|
+
|
|
10
|
+
import { type IDocHandle } from './defs';
|
|
11
|
+
import { type CursorConverter } from '../../util';
|
|
12
|
+
|
|
13
|
+
export const cursorConverter = (handle: IDocHandle, path: Prop[]): CursorConverter => ({
|
|
14
|
+
// TODO(burdon): Handle assoc to associate with a previous character.
|
|
15
|
+
toCursor: (pos) => {
|
|
16
|
+
const doc = handle.docSync();
|
|
17
|
+
if (!doc) {
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const value = get(doc, path);
|
|
22
|
+
if (typeof value === 'string' && value.length <= pos) {
|
|
23
|
+
return 'end';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// NOTE: Slice is needed because getCursor mutates the array.
|
|
27
|
+
return automerge.getCursor(doc, path.slice(), pos);
|
|
28
|
+
},
|
|
29
|
+
fromCursor: (cursor) => {
|
|
30
|
+
if (cursor === '') {
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const doc = handle.docSync();
|
|
35
|
+
if (!doc) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (cursor === 'end') {
|
|
40
|
+
const value = get(doc, path);
|
|
41
|
+
if (typeof value === 'string') {
|
|
42
|
+
return value.length;
|
|
43
|
+
} else {
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// NOTE: Slice is needed because getCursor mutates the array.
|
|
49
|
+
return automerge.getCursorPosition(doc, path.slice(), cursor);
|
|
50
|
+
},
|
|
51
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Annotation,
|
|
8
|
+
StateEffect,
|
|
9
|
+
type StateField,
|
|
10
|
+
type EditorState,
|
|
11
|
+
type Transaction,
|
|
12
|
+
type TransactionSpec,
|
|
13
|
+
} from '@codemirror/state';
|
|
14
|
+
|
|
15
|
+
import { type ChangeFn, type ChangeOptions, type Doc, type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
16
|
+
|
|
17
|
+
export type Value = {
|
|
18
|
+
lastHeads: Heads;
|
|
19
|
+
path: Prop[];
|
|
20
|
+
unreconciledTransactions: Transaction[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type UpdateHeads = {
|
|
24
|
+
newHeads: Heads;
|
|
25
|
+
};
|
|
26
|
+
|
|
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;
|
|
34
|
+
|
|
35
|
+
export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
36
|
+
|
|
37
|
+
export const isReconcileTx = (tr: Transaction): boolean => !!tr.annotation(reconcileAnnotationType);
|
|
38
|
+
|
|
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
|
+
// }
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type IDocHandle<T = any> = {
|
|
56
|
+
docSync(): Doc<T> | undefined;
|
|
57
|
+
change(callback: ChangeFn<T>, options?: ChangeOptions<T>): void;
|
|
58
|
+
changeAt(heads: Heads, callback: ChangeFn<T>, options?: ChangeOptions<T>): string[] | undefined;
|
|
59
|
+
|
|
60
|
+
addListener(event: 'change', listener: () => void): void;
|
|
61
|
+
removeListener(event: 'change', listener: () => void): void;
|
|
62
|
+
};
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
import { type StateField } from '@codemirror/state';
|
|
7
8
|
import { type EditorView } from '@codemirror/view';
|
|
8
9
|
|
|
9
10
|
import { next as automerge } from '@dxos/automerge/automerge';
|
|
10
11
|
|
|
11
|
-
import {
|
|
12
|
-
|
|
12
|
+
import {
|
|
13
|
+
getLastHeads,
|
|
14
|
+
getPath,
|
|
15
|
+
isReconcileTx,
|
|
16
|
+
reconcileAnnotationType,
|
|
17
|
+
updateHeads,
|
|
18
|
+
type IDocHandle,
|
|
19
|
+
type Value,
|
|
20
|
+
} from './defs';
|
|
13
21
|
import { updateAutomerge } from './update-automerge';
|
|
14
22
|
import { updateCodeMirror } from './update-codemirror';
|
|
15
23
|
|
|
@@ -22,15 +30,10 @@ type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads |
|
|
|
22
30
|
* TODO(burdon): Comment.
|
|
23
31
|
*/
|
|
24
32
|
export class PatchSemaphore {
|
|
25
|
-
_field!: Field;
|
|
26
33
|
_inReconcile = false;
|
|
27
34
|
_queue: Array<ChangeFn> = [];
|
|
28
35
|
|
|
29
|
-
constructor(
|
|
30
|
-
if (field !== undefined) {
|
|
31
|
-
this._field = field;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
36
|
+
constructor(private readonly _field: StateField<Value>) {}
|
|
34
37
|
|
|
35
38
|
reconcile = (handle: IDocHandle, view: EditorView) => {
|
|
36
39
|
if (this._inReconcile) {
|
|
@@ -42,9 +45,8 @@ export class PatchSemaphore {
|
|
|
42
45
|
const oldHeads = getLastHeads(view.state, this._field);
|
|
43
46
|
let selection = view.state.selection;
|
|
44
47
|
|
|
45
|
-
const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
|
|
46
|
-
|
|
47
48
|
// First undo all the unreconciled transactions.
|
|
49
|
+
const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
|
|
48
50
|
const toInvert = transactions.slice().reverse();
|
|
49
51
|
for (const tx of toInvert) {
|
|
50
52
|
const inverted = tx.changes.invert(tx.startState.doc);
|