@dxos/react-ui-editor 0.3.9-main.b7e6a67 → 0.3.9-main.c414ce0
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 +973 -499
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts +2 -2
- package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts +2 -2
- package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-plugin/handle.d.ts +9 -0
- package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +1 -0
- package/dist/types/src/automerge/automerge-plugin/index.d.ts +2 -2
- package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge-plugin/plugin.d.ts +6 -5
- package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +1 -1
- package/dist/types/src/automerge/automerge.stories.d.ts +2 -1
- package/dist/types/src/automerge/automerge.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +43 -0
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +15 -0
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -0
- package/dist/types/{stories/html/code-mirror.stories.d.ts → src/components/TextEditor/codemirror.stories.d.ts} +2 -2
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts +8 -0
- package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts +3 -0
- package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts +10 -0
- package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/change.d.ts +7 -0
- package/dist/types/src/components/TextEditor/extensions/change.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts +11 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts +8 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts +10 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts +4 -0
- package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts +5 -0
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +8 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts +3 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +1 -0
- package/dist/types/src/components/{Markdown/markdownTags.d.ts → TextEditor/extensions/markdown/tags.d.ts} +1 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts +21 -0
- package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/index.d.ts +4 -0
- package/dist/types/src/components/TextEditor/index.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +11 -0
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -3
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +4 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useCollaboration.d.ts +9 -0
- package/dist/types/src/hooks/useCollaboration.d.ts.map +1 -0
- package/dist/types/src/{model.d.ts → hooks/useTextModel.d.ts} +3 -15
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -0
- package/dist/types/src/hooks/yjs/cursors.d.ts.map +1 -0
- package/dist/types/src/hooks/yjs/index.d.ts.map +1 -0
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +4 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/dist/types/src/testing/replicator.d.ts +1 -1
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/package.json +34 -27
- package/src/automerge/automerge-plugin/PatchSemaphore.ts +5 -5
- package/src/automerge/automerge-plugin/codeMirrorToAm.ts +10 -7
- package/src/automerge/automerge-plugin/handle.ts +14 -0
- package/src/automerge/automerge-plugin/index.ts +2 -2
- package/src/automerge/automerge-plugin/plugin.ts +39 -10
- package/src/automerge/automerge.stories.tsx +32 -48
- package/src/components/TextEditor/TextEditor.stories.tsx +79 -0
- package/src/components/TextEditor/TextEditor.tsx +188 -0
- package/src/components/TextEditor/codemirror.stories.ts +114 -0
- package/src/components/TextEditor/extensions/basic/bundle.ts +26 -0
- package/src/components/TextEditor/extensions/basic/index.ts +6 -0
- package/src/components/TextEditor/extensions/basic/theme.ts +49 -0
- package/src/components/TextEditor/extensions/change.ts +22 -0
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.ts +99 -0
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.tsx +61 -0
- package/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.tsx +119 -0
- package/src/components/TextEditor/extensions/hyperlink/index.ts +7 -0
- package/src/components/TextEditor/extensions/index.ts +8 -0
- package/src/components/TextEditor/extensions/markdown/bundle.ts +99 -0
- package/src/components/TextEditor/extensions/markdown/index.ts +6 -0
- package/src/components/{Markdown/markdownTheme.ts → TextEditor/extensions/markdown/theme.ts} +42 -27
- package/src/components/TextEditor/index.ts +8 -0
- package/src/components/TextEditor/yjs.stories.tsx +55 -0
- package/src/components/index.ts +1 -3
- package/src/hooks/index.ts +7 -0
- package/src/hooks/useCollaboration.ts +45 -0
- package/src/{model.ts → hooks/useTextModel.ts} +42 -17
- package/src/index.ts +3 -3
- package/src/styles/tokens.ts +6 -0
- package/src/testing/replicator.ts +1 -3
- package/dist/types/src/automerge/demo.d.ts +0 -25
- package/dist/types/src/automerge/demo.d.ts.map +0 -1
- package/dist/types/src/components/Editor/Editor.d.ts +0 -15
- package/dist/types/src/components/Editor/Editor.d.ts.map +0 -1
- package/dist/types/src/components/Editor/Editor.stories.d.ts +0 -28
- package/dist/types/src/components/Editor/Editor.stories.d.ts.map +0 -1
- package/dist/types/src/components/Editor/index.d.ts +0 -2
- package/dist/types/src/components/Editor/index.d.ts.map +0 -1
- package/dist/types/src/components/Markdown/Markdown.d.ts +0 -19
- package/dist/types/src/components/Markdown/Markdown.d.ts.map +0 -1
- package/dist/types/src/components/Markdown/Markdown.stories.d.ts +0 -19
- package/dist/types/src/components/Markdown/Markdown.stories.d.ts.map +0 -1
- package/dist/types/src/components/Markdown/index.d.ts +0 -2
- package/dist/types/src/components/Markdown/index.d.ts.map +0 -1
- package/dist/types/src/components/Markdown/markdownTags.d.ts.map +0 -1
- package/dist/types/src/components/Markdown/markdownTheme.d.ts +0 -107
- package/dist/types/src/components/Markdown/markdownTheme.d.ts.map +0 -1
- package/dist/types/src/components/RichText/RichText.d.ts +0 -20
- package/dist/types/src/components/RichText/RichText.d.ts.map +0 -1
- package/dist/types/src/components/RichText/RichText.stories.d.ts +0 -26
- package/dist/types/src/components/RichText/RichText.stories.d.ts.map +0 -1
- package/dist/types/src/components/RichText/index.d.ts +0 -2
- package/dist/types/src/components/RichText/index.d.ts.map +0 -1
- package/dist/types/src/model.d.ts.map +0 -1
- package/dist/types/src/yjs/cursors.d.ts.map +0 -1
- package/dist/types/src/yjs/index.d.ts.map +0 -1
- package/dist/types/src/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/stories/html/code-mirror.stories.d.ts.map +0 -1
- package/src/automerge/demo.ts +0 -106
- package/src/components/Editor/Editor.stories.tsx +0 -80
- package/src/components/Editor/Editor.tsx +0 -33
- package/src/components/Editor/index.ts +0 -5
- package/src/components/Markdown/Markdown.stories.tsx +0 -56
- package/src/components/Markdown/Markdown.tsx +0 -206
- package/src/components/Markdown/index.ts +0 -5
- package/src/components/RichText/RichText.stories.tsx +0 -68
- package/src/components/RichText/RichText.tsx +0 -181
- package/src/components/RichText/index.ts +0 -5
- /package/dist/types/src/{yjs → hooks/yjs}/cursors.d.ts +0 -0
- /package/dist/types/src/{yjs → hooks/yjs}/index.d.ts +0 -0
- /package/dist/types/src/{yjs → hooks/yjs}/space-provider.d.ts +0 -0
- /package/src/components/{Markdown/markdownTags.ts → TextEditor/extensions/markdown/tags.ts} +0 -0
- /package/src/{yjs → hooks/yjs}/cursors.ts +0 -0
- /package/src/{yjs → hooks/yjs}/index.ts +0 -0
- /package/src/{yjs → hooks/yjs}/space-provider.ts +0 -0
|
@@ -8,8 +8,8 @@ import { next as automerge } from '@dxos/automerge/automerge';
|
|
|
8
8
|
|
|
9
9
|
import amToCodemirror from './amToCodemirror';
|
|
10
10
|
import codeMirrorToAm from './codeMirrorToAm';
|
|
11
|
+
import { type IDocHandle } from './handle';
|
|
11
12
|
import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
|
|
12
|
-
import { type Peer } from '../demo';
|
|
13
13
|
|
|
14
14
|
type Doc<T> = automerge.Doc<T>;
|
|
15
15
|
type Heads = automerge.Heads;
|
|
@@ -27,8 +27,8 @@ export class PatchSemaphore {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
reconcile = (handle:
|
|
31
|
-
if (
|
|
30
|
+
reconcile = (handle: IDocHandle, view: EditorView) => {
|
|
31
|
+
if (this._inReconcile) {
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
34
|
this._inReconcile = true;
|
|
@@ -56,12 +56,12 @@ export class PatchSemaphore {
|
|
|
56
56
|
// NOTE: null and undefined each come from automerge and repo respectively
|
|
57
57
|
if (newHeads === null || newHeads === undefined) {
|
|
58
58
|
// TODO: @alexjg this is the call that's resetting the editor state on click
|
|
59
|
-
newHeads = automerge.getHeads(handle.
|
|
59
|
+
newHeads = automerge.getHeads(handle.docSync()!);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
// now get the diff between the updated state of the document and the heads
|
|
63
63
|
// and apply that to the codemirror doc
|
|
64
|
-
const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.
|
|
64
|
+
const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
|
|
65
65
|
amToCodemirror(view, selection, path, diff);
|
|
66
66
|
|
|
67
67
|
view.dispatch({
|
|
@@ -6,21 +6,24 @@ import { type EditorState, type Text, type Transaction } from '@codemirror/state
|
|
|
6
6
|
|
|
7
7
|
import { next as am, type Heads } from '@dxos/automerge/automerge';
|
|
8
8
|
|
|
9
|
+
import { type IDocHandle } from './handle';
|
|
9
10
|
import { type Field } from './plugin';
|
|
10
|
-
import { type Peer } from '../demo';
|
|
11
11
|
|
|
12
|
-
export default (
|
|
12
|
+
export default (
|
|
13
|
+
field: Field,
|
|
14
|
+
handle: IDocHandle,
|
|
15
|
+
transactions: Transaction[],
|
|
16
|
+
state: EditorState,
|
|
17
|
+
): Heads | undefined => {
|
|
13
18
|
const { lastHeads, path } = state.field(field);
|
|
14
19
|
|
|
15
20
|
// We don't want to call `automerge.updateAt` if there are no changes.
|
|
16
21
|
// Otherwise later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
17
22
|
let hasChanges = false;
|
|
18
23
|
for (const tr of transactions) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
+
tr.changes.iterChanges(() => {
|
|
25
|
+
hasChanges = true;
|
|
26
|
+
});
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
if (!hasChanges) {
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as A from '@dxos/automerge/automerge';
|
|
6
|
+
|
|
7
|
+
export type IDocHandle<T = any> = {
|
|
8
|
+
docSync(): A.Doc<T> | undefined;
|
|
9
|
+
change(callback: A.ChangeFn<T>, options?: A.ChangeOptions<T>): void;
|
|
10
|
+
changeAt(heads: A.Heads, callback: A.ChangeFn<T>, options?: A.ChangeOptions<T>): string[] | undefined;
|
|
11
|
+
|
|
12
|
+
addListener(event: 'change', listener: () => void): void;
|
|
13
|
+
removeListener(event: 'change', listener: () => void): void;
|
|
14
|
+
};
|
|
@@ -12,13 +12,13 @@ import {
|
|
|
12
12
|
type Transaction,
|
|
13
13
|
type TransactionSpec,
|
|
14
14
|
} from '@codemirror/state';
|
|
15
|
-
import { type EditorView } from '@codemirror/view';
|
|
15
|
+
import { ViewPlugin, type EditorView, type PluginValue, type ViewUpdate } from '@codemirror/view';
|
|
16
16
|
|
|
17
17
|
import * as automerge from '@dxos/automerge/automerge';
|
|
18
|
-
import { type
|
|
18
|
+
import { type Heads, type Prop } from '@dxos/automerge/automerge';
|
|
19
19
|
|
|
20
20
|
import { PatchSemaphore } from './PatchSemaphore';
|
|
21
|
-
import { type
|
|
21
|
+
import { type IDocHandle } from './handle';
|
|
22
22
|
|
|
23
23
|
export type Value = {
|
|
24
24
|
lastHeads: Heads;
|
|
@@ -44,10 +44,14 @@ const semaphoreFacet = Facet.define<PatchSemaphore, PatchSemaphore>({
|
|
|
44
44
|
combine: (values) => values.at(-1)!, // Take last.
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
export
|
|
47
|
+
export type AutomergePlugin = {
|
|
48
|
+
extension: Extension;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const automergePlugin = (handle: IDocHandle, path: Prop[]): AutomergePlugin => {
|
|
48
52
|
const stateField: StateField<Value> = StateField.define({
|
|
49
53
|
create: () => ({
|
|
50
|
-
lastHeads: automerge.getHeads(
|
|
54
|
+
lastHeads: automerge.getHeads(handle.docSync()!),
|
|
51
55
|
unreconciledTransactions: [],
|
|
52
56
|
path: path.slice(),
|
|
53
57
|
}),
|
|
@@ -76,7 +80,36 @@ export const plugin = <T>(doc: Doc<T>, path: Prop[]): Extension => {
|
|
|
76
80
|
});
|
|
77
81
|
const semaphore = new PatchSemaphore(stateField);
|
|
78
82
|
|
|
79
|
-
|
|
83
|
+
const viewPlugin = ViewPlugin.fromClass(
|
|
84
|
+
class AutomergeCodemirrorViewPlugin implements PluginValue {
|
|
85
|
+
private _view: EditorView;
|
|
86
|
+
|
|
87
|
+
constructor(view: EditorView) {
|
|
88
|
+
this._view = view;
|
|
89
|
+
handle.addListener('change', this._handleChange);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
update(update: ViewUpdate) {
|
|
93
|
+
if (update.transactions.length > 0 && update.transactions.some((t) => !isReconcileTx(t))) {
|
|
94
|
+
queueMicrotask(() => {
|
|
95
|
+
this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
destroy() {
|
|
101
|
+
handle.addListener('change', this._handleChange);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private _handleChange = () => {
|
|
105
|
+
this._view.state.facet(semaphoreFacet).reconcile(handle, this._view);
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
extension: [stateField, semaphoreFacet.of(semaphore), viewPlugin],
|
|
112
|
+
};
|
|
80
113
|
};
|
|
81
114
|
|
|
82
115
|
export const reconcileAnnotationType = Annotation.define<unknown>();
|
|
@@ -98,7 +131,3 @@ export const makeReconcile = (tr: TransactionSpec) => {
|
|
|
98
131
|
// annotations: reconcileAnnotationType.of({})
|
|
99
132
|
// }
|
|
100
133
|
};
|
|
101
|
-
|
|
102
|
-
export const reconcile = (handle: Peer, view: EditorView) => {
|
|
103
|
-
view.state.facet(semaphoreFacet).reconcile(handle, view);
|
|
104
|
-
};
|
|
@@ -2,18 +2,20 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import '@preact/signals-react'; // Register react integration
|
|
6
|
+
import { BroadcastChannelNetworkAdapter } from '@automerge/automerge-repo-network-broadcastchannel';
|
|
6
7
|
import { EditorView } from '@codemirror/view';
|
|
8
|
+
import { basicSetup } from 'codemirror';
|
|
9
|
+
import get from 'lodash.get';
|
|
7
10
|
import React, { useEffect, useRef, useState } from 'react';
|
|
8
11
|
|
|
9
|
-
import { type Prop
|
|
12
|
+
import { type Prop } from '@dxos/automerge/automerge';
|
|
13
|
+
import { type DocHandle, Repo } from '@dxos/automerge/automerge-repo';
|
|
10
14
|
|
|
11
|
-
import {
|
|
12
|
-
import { reconcile } from './automerge-plugin/plugin';
|
|
13
|
-
import { Peer } from './demo';
|
|
15
|
+
import { type IDocHandle, automergePlugin } from './automerge-plugin';
|
|
14
16
|
|
|
15
17
|
type EditorProps = {
|
|
16
|
-
handle:
|
|
18
|
+
handle: IDocHandle;
|
|
17
19
|
path: Prop[];
|
|
18
20
|
};
|
|
19
21
|
|
|
@@ -22,27 +24,13 @@ const Editor = ({ handle, path }: EditorProps) => {
|
|
|
22
24
|
const editorRoot = useRef<EditorView>();
|
|
23
25
|
|
|
24
26
|
useEffect(() => {
|
|
25
|
-
const doc = handle.doc;
|
|
26
|
-
const source = doc.text; // this should use path
|
|
27
|
-
const plugin = amgPlugin(doc, path);
|
|
28
27
|
const view = (editorRoot.current = new EditorView({
|
|
29
|
-
doc:
|
|
30
|
-
extensions: [basicSetup,
|
|
31
|
-
dispatch: (transaction) => {
|
|
32
|
-
view.update([transaction]);
|
|
33
|
-
reconcile(handle, view);
|
|
34
|
-
},
|
|
28
|
+
doc: get(handle.docSync()!, path),
|
|
29
|
+
extensions: [basicSetup, automergePlugin(handle, path)],
|
|
35
30
|
parent: containerRef.current as any,
|
|
36
31
|
}));
|
|
37
32
|
|
|
38
|
-
const handleChange = () => {
|
|
39
|
-
reconcile(handle, view);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
handle.changeEvent.on(handleChange);
|
|
43
|
-
|
|
44
33
|
return () => {
|
|
45
|
-
handle.changeEvent.off(handleChange);
|
|
46
34
|
view.destroy();
|
|
47
35
|
};
|
|
48
36
|
}, []);
|
|
@@ -51,31 +39,29 @@ const Editor = ({ handle, path }: EditorProps) => {
|
|
|
51
39
|
};
|
|
52
40
|
|
|
53
41
|
const Story = () => {
|
|
54
|
-
const [object1, setObject1] = useState<
|
|
55
|
-
const [object2, setObject2] = useState<
|
|
56
|
-
const [stats1, setStats1] = useState<any>({});
|
|
57
|
-
const [stats2, setStats2] = useState<any>({});
|
|
42
|
+
const [object1, setObject1] = useState<DocHandle<any> | null>(null);
|
|
43
|
+
const [object2, setObject2] = useState<DocHandle<any> | null>(null);
|
|
58
44
|
|
|
59
45
|
useEffect(() => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
46
|
+
queueMicrotask(async () => {
|
|
47
|
+
const repo1 = new Repo({
|
|
48
|
+
network: [new BroadcastChannelNetworkAdapter()],
|
|
49
|
+
});
|
|
50
|
+
const repo2 = new Repo({
|
|
51
|
+
network: [new BroadcastChannelNetworkAdapter()],
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const object1 = repo1.create();
|
|
55
|
+
object1.change((doc: any) => {
|
|
56
|
+
doc.text = 'Hello world!';
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const object2 = repo2.find(object1.url);
|
|
60
|
+
await object2.whenReady();
|
|
61
|
+
|
|
62
|
+
setObject1(object1);
|
|
63
|
+
setObject2(object2);
|
|
64
|
+
});
|
|
79
65
|
}, []);
|
|
80
66
|
|
|
81
67
|
if (!object1 || !object2) {
|
|
@@ -86,11 +72,9 @@ const Story = () => {
|
|
|
86
72
|
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', width: '100vw' }}>
|
|
87
73
|
<div>
|
|
88
74
|
<Editor handle={object1} path={['text']} />
|
|
89
|
-
<div style={{ whiteSpace: 'pre' }}>{JSON.stringify(stats1, null, 2)}</div>
|
|
90
75
|
</div>
|
|
91
76
|
<div>
|
|
92
77
|
<Editor handle={object2} path={['text']} />
|
|
93
|
-
<div style={{ whiteSpace: 'pre' }}>{JSON.stringify(stats2, null, 2)}</div>
|
|
94
78
|
</div>
|
|
95
79
|
</div>
|
|
96
80
|
);
|
|
@@ -100,6 +84,6 @@ export default {
|
|
|
100
84
|
title: 'Automerge',
|
|
101
85
|
};
|
|
102
86
|
|
|
103
|
-
export const
|
|
87
|
+
export const AutomergeRepo = {
|
|
104
88
|
render: () => <Story />,
|
|
105
89
|
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxosTheme';
|
|
6
|
+
import { markdown } from '@codemirror/lang-markdown';
|
|
7
|
+
import { ArrowCircleUp } from '@phosphor-icons/react';
|
|
8
|
+
import React, { StrictMode, useState } from 'react';
|
|
9
|
+
import { createRoot } from 'react-dom/client';
|
|
10
|
+
|
|
11
|
+
import { TextObject } from '@dxos/echo-schema';
|
|
12
|
+
import { fixedInsetFlexLayout, getSize, groupSurface, inputSurface, mx } from '@dxos/react-ui-theme';
|
|
13
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
14
|
+
|
|
15
|
+
import { TextEditor } from './TextEditor';
|
|
16
|
+
import { createHyperlinkTooltip, hyperlinkDecoration } from './extensions';
|
|
17
|
+
import { useTextModel } from '../../hooks';
|
|
18
|
+
|
|
19
|
+
const text = [
|
|
20
|
+
'',
|
|
21
|
+
'',
|
|
22
|
+
'This is all about [DXOS](https://dxos.org); read more [here](https://docs.dxos.org/guide/getting-started.html).',
|
|
23
|
+
'',
|
|
24
|
+
'',
|
|
25
|
+
].join('\n');
|
|
26
|
+
|
|
27
|
+
const hyperLinkTooltip = () =>
|
|
28
|
+
createHyperlinkTooltip((el, url) => {
|
|
29
|
+
const web = new URL(url);
|
|
30
|
+
createRoot(el).render(
|
|
31
|
+
<StrictMode>
|
|
32
|
+
<div className='flex gap-1 items-center'>
|
|
33
|
+
<ArrowCircleUp className={mx(getSize(6), 'text-blue-500')} />
|
|
34
|
+
<p className='pr-1'>{web.origin}</p>
|
|
35
|
+
</div>
|
|
36
|
+
</StrictMode>,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const Story = ({ automerge }: { automerge?: boolean }) => {
|
|
41
|
+
const [item] = useState({ text: new TextObject(text, undefined, undefined, { useAutomergeBackend: !!automerge }) });
|
|
42
|
+
const model = useTextModel({ text: item.text });
|
|
43
|
+
if (!model) {
|
|
44
|
+
return <></>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
49
|
+
<div className='flex justify-center p-8'>
|
|
50
|
+
<div className='w-[800px]'>
|
|
51
|
+
<TextEditor
|
|
52
|
+
model={model}
|
|
53
|
+
extensions={[
|
|
54
|
+
markdown(),
|
|
55
|
+
hyperlinkDecoration({ link: false }),
|
|
56
|
+
hyperLinkTooltip(),
|
|
57
|
+
// EditorView.domEventHandlers({
|
|
58
|
+
// mousedown: (e, view) => {},
|
|
59
|
+
// }),
|
|
60
|
+
]}
|
|
61
|
+
slots={{ root: { className: mx(inputSurface, 'p-2') } }}
|
|
62
|
+
/>
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export default {
|
|
70
|
+
component: TextEditor,
|
|
71
|
+
decorators: [withTheme],
|
|
72
|
+
render: Story,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const Default = {};
|
|
76
|
+
|
|
77
|
+
export const Automerge = {
|
|
78
|
+
render: () => <Story automerge />,
|
|
79
|
+
};
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { EditorState, type Extension } from '@codemirror/state';
|
|
6
|
+
import { EditorView } from '@codemirror/view';
|
|
7
|
+
import { useFocusableGroup } from '@fluentui/react-tabster';
|
|
8
|
+
import { vim } from '@replit/codemirror-vim';
|
|
9
|
+
import defaultsDeep from 'lodash.defaultsdeep';
|
|
10
|
+
import get from 'lodash.get';
|
|
11
|
+
import React, {
|
|
12
|
+
type KeyboardEvent,
|
|
13
|
+
forwardRef,
|
|
14
|
+
useCallback,
|
|
15
|
+
useEffect,
|
|
16
|
+
useImperativeHandle,
|
|
17
|
+
useState,
|
|
18
|
+
type ComponentProps,
|
|
19
|
+
} from 'react';
|
|
20
|
+
|
|
21
|
+
import { isDocAccessor } from '@dxos/echo-schema';
|
|
22
|
+
import { useThemeContext } from '@dxos/react-ui';
|
|
23
|
+
|
|
24
|
+
import { basicBundle, basicTheme, markdownBundle, markdownTheme } from './extensions';
|
|
25
|
+
import { type EditorModel, useCollaboration } from '../../hooks';
|
|
26
|
+
import type { ThemeStyles } from '../../styles';
|
|
27
|
+
|
|
28
|
+
export const EditorModes = ['default', 'vim'] as const;
|
|
29
|
+
export type EditorMode = (typeof EditorModes)[number];
|
|
30
|
+
|
|
31
|
+
export type CursorInfo = {
|
|
32
|
+
from: number;
|
|
33
|
+
to: number;
|
|
34
|
+
line: number;
|
|
35
|
+
lines: number;
|
|
36
|
+
after?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type TextEditorRef = {
|
|
40
|
+
editor: HTMLDivElement | null;
|
|
41
|
+
state?: EditorState;
|
|
42
|
+
view?: EditorView;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type TextEditorSlots = {
|
|
46
|
+
root?: Omit<ComponentProps<'div'>, 'ref'>;
|
|
47
|
+
editor?: {
|
|
48
|
+
className?: string;
|
|
49
|
+
placeholder?: string;
|
|
50
|
+
spellCheck?: boolean;
|
|
51
|
+
tabIndex?: number;
|
|
52
|
+
theme?: ThemeStyles;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type TextEditorProps = {
|
|
57
|
+
model: EditorModel;
|
|
58
|
+
extensions?: Extension[];
|
|
59
|
+
slots?: TextEditorSlots;
|
|
60
|
+
editorMode?: EditorMode;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Base text editor.
|
|
65
|
+
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
66
|
+
*/
|
|
67
|
+
export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
68
|
+
({ model, extensions = [], slots, editorMode }, forwardedRef) => {
|
|
69
|
+
const { themeMode } = useThemeContext();
|
|
70
|
+
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
71
|
+
|
|
72
|
+
const [parent, setParent] = useState<HTMLDivElement | null>(null);
|
|
73
|
+
const [state, setState] = useState<EditorState>();
|
|
74
|
+
const [view, setView] = useState<EditorView>();
|
|
75
|
+
useImperativeHandle(
|
|
76
|
+
forwardedRef,
|
|
77
|
+
() => ({
|
|
78
|
+
editor: parent,
|
|
79
|
+
state,
|
|
80
|
+
view,
|
|
81
|
+
}),
|
|
82
|
+
[view, state, parent],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// TODO(burdon): Pass in extension?
|
|
86
|
+
const collaboration = useCollaboration(model, themeMode);
|
|
87
|
+
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
if (!parent) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const initialText = isDocAccessor(model.content)
|
|
94
|
+
? get(model.content.handle.docSync(), model.content.path)
|
|
95
|
+
: model.content?.toString();
|
|
96
|
+
|
|
97
|
+
const state = EditorState.create({
|
|
98
|
+
doc: initialText,
|
|
99
|
+
extensions: [
|
|
100
|
+
// TODO(burdon): Factor out VIM mode?
|
|
101
|
+
editorMode === 'vim' && vim(),
|
|
102
|
+
|
|
103
|
+
// Replication.
|
|
104
|
+
collaboration,
|
|
105
|
+
|
|
106
|
+
// Theme.
|
|
107
|
+
slots?.editor?.theme && EditorView.theme(slots?.editor?.theme),
|
|
108
|
+
|
|
109
|
+
// Custom.
|
|
110
|
+
...extensions,
|
|
111
|
+
].filter(Boolean) as Extension[],
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
setState(state);
|
|
115
|
+
|
|
116
|
+
// NOTE: This repaints the editor.
|
|
117
|
+
// If the new state is derived from the old state, it will likely not be visible other than the cursor resetting.
|
|
118
|
+
// Ideally this should not be hit except when changing between text objects.
|
|
119
|
+
view?.destroy();
|
|
120
|
+
setView(new EditorView({ state, parent }));
|
|
121
|
+
|
|
122
|
+
return () => {
|
|
123
|
+
view?.destroy();
|
|
124
|
+
setView(undefined);
|
|
125
|
+
setState(undefined);
|
|
126
|
+
};
|
|
127
|
+
}, [parent, model.content, themeMode, editorMode]);
|
|
128
|
+
|
|
129
|
+
const handleKeyUp = useCallback(
|
|
130
|
+
(event: KeyboardEvent) => {
|
|
131
|
+
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
132
|
+
switch (key) {
|
|
133
|
+
case 'Enter': {
|
|
134
|
+
view?.contentDOM.focus();
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
case 'Escape': {
|
|
139
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && parent?.focus();
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
[view, editorMode],
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<div
|
|
149
|
+
key={model.id}
|
|
150
|
+
ref={setParent}
|
|
151
|
+
tabIndex={0}
|
|
152
|
+
{...slots?.root}
|
|
153
|
+
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
154
|
+
onKeyUp={handleKeyUp}
|
|
155
|
+
/>
|
|
156
|
+
);
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
export const TextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
161
|
+
({ extensions: _extensions, slots, ...props }, forwardedRef) => {
|
|
162
|
+
const { themeMode } = useThemeContext();
|
|
163
|
+
const extensions = [...(_extensions ?? []), basicBundle({ themeMode, placeholder: slots?.editor?.placeholder })];
|
|
164
|
+
return (
|
|
165
|
+
<BaseTextEditor
|
|
166
|
+
ref={forwardedRef}
|
|
167
|
+
extensions={extensions}
|
|
168
|
+
slots={defaultsDeep({}, slots, { editor: { theme: basicTheme } })}
|
|
169
|
+
{...props}
|
|
170
|
+
/>
|
|
171
|
+
);
|
|
172
|
+
},
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
export const MarkdownEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
176
|
+
({ extensions: _extensions, slots, ...props }, forwardedRef) => {
|
|
177
|
+
const { themeMode } = useThemeContext();
|
|
178
|
+
const extensions = [...(_extensions ?? []), markdownBundle({ themeMode })];
|
|
179
|
+
return (
|
|
180
|
+
<BaseTextEditor
|
|
181
|
+
ref={forwardedRef}
|
|
182
|
+
extensions={extensions}
|
|
183
|
+
slots={defaultsDeep({}, slots, { editor: { theme: markdownTheme } })}
|
|
184
|
+
{...props}
|
|
185
|
+
/>
|
|
186
|
+
);
|
|
187
|
+
},
|
|
188
|
+
);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
|
|
6
|
+
import { EditorState } from '@codemirror/state';
|
|
7
|
+
import { EditorView } from '@codemirror/view';
|
|
8
|
+
import { type StoryObj } from '@storybook/html';
|
|
9
|
+
import { basicSetup } from 'codemirror';
|
|
10
|
+
import { yCollab } from 'y-codemirror.next';
|
|
11
|
+
|
|
12
|
+
import { EventSubscriptions } from '@dxos/async';
|
|
13
|
+
import { generateName } from '@dxos/display-name';
|
|
14
|
+
import { Client, type PublicKey } from '@dxos/react-client';
|
|
15
|
+
import { type Space, TextObject } from '@dxos/react-client/echo';
|
|
16
|
+
import { type Identity } from '@dxos/react-client/halo';
|
|
17
|
+
import { joinCommonSpace, TestBuilder, textGenerator } from '@dxos/react-client/testing';
|
|
18
|
+
import { YText } from '@dxos/text-model';
|
|
19
|
+
|
|
20
|
+
import { cursorColor, SpaceAwarenessProvider } from '../../hooks';
|
|
21
|
+
import { EditorDocument, types as schema } from '../../testing';
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
title: 'CodeMirror',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const testBuilder = new TestBuilder();
|
|
28
|
+
const views: Record<number, EditorView> = {};
|
|
29
|
+
const subscriptions = new EventSubscriptions();
|
|
30
|
+
|
|
31
|
+
const updateEditor = async (id: number, editor: HTMLDivElement, identity: Identity, space: Space, text: TextObject) => {
|
|
32
|
+
const doc = text.doc;
|
|
33
|
+
const ytext = text.content;
|
|
34
|
+
if (!(ytext instanceof YText) || !doc) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { awareness } = new SpaceAwarenessProvider({ space, doc, channel: `yjs.awareness.${text.id}` });
|
|
39
|
+
awareness.setLocalStateField('user', {
|
|
40
|
+
name: identity.profile?.displayName ?? generateName(identity.identityKey.toHex()),
|
|
41
|
+
// TODO(wittjosiah): Pick colours from theme based on identity key.
|
|
42
|
+
color: cursorColor.color,
|
|
43
|
+
colorLight: cursorColor.light,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const state = EditorState.create({
|
|
47
|
+
doc: ytext.toString(),
|
|
48
|
+
extensions: [basicSetup, markdown({ base: markdownLanguage }), yCollab(ytext, awareness)],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const view = views[id];
|
|
52
|
+
if (view) {
|
|
53
|
+
view.setState(state);
|
|
54
|
+
} else {
|
|
55
|
+
views[id] = new EditorView({ state, parent: editor });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (id === 0) {
|
|
59
|
+
subscriptions.clear();
|
|
60
|
+
subscriptions.add(textGenerator({ text: ytext }));
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const setupEditor = async (id: number, client: Client, spaceKey: PublicKey, editor: HTMLDivElement) => {
|
|
65
|
+
const space = await client.spaces.get(spaceKey)!;
|
|
66
|
+
const query = space.db.query(EditorDocument.filter());
|
|
67
|
+
query.subscribe(({ objects }) => {
|
|
68
|
+
const text = objects[0]?.content;
|
|
69
|
+
void (text && updateEditor(id, editor, client.halo.identity.get()!, space, text));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const text = query.objects[0]?.content;
|
|
73
|
+
void (text && updateEditor(id, editor, client.halo.identity.get()!, space, text));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const setupSpace = async (count: number) => {
|
|
77
|
+
const clients = [...Array(count)].map(() => new Client({ services: testBuilder.createLocal() }));
|
|
78
|
+
await Promise.all(clients.map((client) => client.initialize()));
|
|
79
|
+
await Promise.all(clients.map((client) => client.halo.createIdentity()));
|
|
80
|
+
clients.map((client) => client.spaces.addSchema(schema));
|
|
81
|
+
|
|
82
|
+
const space = await clients[0].spaces.create();
|
|
83
|
+
const text = new TextObject('Hello, Storybook!');
|
|
84
|
+
const document = new EditorDocument({ content: text });
|
|
85
|
+
await space.db.add(document);
|
|
86
|
+
|
|
87
|
+
await joinCommonSpace(clients, space.key);
|
|
88
|
+
|
|
89
|
+
return { clients, spaceKey: space.key };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const count = 2;
|
|
93
|
+
|
|
94
|
+
export const Default: StoryObj<{ id: number; client: Client; spaceKey: PublicKey }> = {
|
|
95
|
+
render: ({ id, client, spaceKey }) => {
|
|
96
|
+
const editor = document.createElement('div');
|
|
97
|
+
editor.setAttribute('style', 'min-width: 0; flex: 1; padding: 1rem;');
|
|
98
|
+
void setupEditor(id, client, spaceKey, editor);
|
|
99
|
+
return editor;
|
|
100
|
+
},
|
|
101
|
+
decorators: [
|
|
102
|
+
(story) => {
|
|
103
|
+
const container = document.createElement('div');
|
|
104
|
+
container.setAttribute('style', 'display: flex; justify-content: space-evenly');
|
|
105
|
+
void setupSpace(count).then(({ clients, spaceKey }) => {
|
|
106
|
+
clients.forEach((client, id) => {
|
|
107
|
+
// TODO(wittjosiah): According to the docs these types should be compatible.
|
|
108
|
+
container.appendChild(story({ args: { id, client, spaceKey } }) as HTMLElement);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
return container;
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
};
|