@dxos/react-ui-editor 0.3.11-main.5a4849d → 0.3.11-main.5cbcf4e
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 +2506 -1566
- 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 +16 -14
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +20 -17
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +6 -6
- 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/hooks.stories.d.ts +23 -0
- 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 +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- 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/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 +24 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +3 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/semaphore.d.ts +15 -0
- 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/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +41 -20
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +25 -0
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/index.d.ts +4 -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 +31 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- 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 +5 -1
- 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 -31
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- 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/markdown.d.ts +2 -2
- package/dist/types/src/styles/markdown.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/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +48 -36
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +84 -91
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +135 -95
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +65 -6
- 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 +23 -19
- package/src/extensions/automerge/automerge.spec.tsx +76 -0
- package/src/extensions/automerge/automerge.test.ts +13 -0
- package/src/extensions/automerge/automerge.ts +103 -0
- package/src/extensions/automerge/cursor.ts +52 -0
- package/src/extensions/automerge/defs.ts +41 -0
- package/src/extensions/automerge/index.ts +6 -0
- package/src/extensions/automerge/semaphore.ts +81 -0
- package/src/extensions/automerge/update-automerge.ts +49 -0
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +17 -9
- package/src/extensions/awareness.ts +313 -0
- package/src/extensions/basic.ts +17 -10
- package/src/extensions/blast.ts +4 -1
- package/src/extensions/comments.ts +336 -255
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/index.ts +4 -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 +11 -26
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/image.ts +87 -0
- package/src/extensions/markdown/index.ts +8 -0
- package/src/extensions/{link.ts → markdown/link.ts} +90 -83
- package/src/extensions/{table.ts → markdown/table.ts} +10 -4
- 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 +124 -0
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +6 -1
- 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 +48 -115
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/markdown.ts +9 -12
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +59 -83
- package/src/util.ts +41 -0
- 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/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/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.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/util.d.ts +0 -5
- 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 +0 -19
- 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 +0 -27
- package/dist/types/src/hooks/yjs/space-provider.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts +0 -12
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/hooks/yjs/yjs.test.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 -114
- package/src/extensions/code.ts +0 -99
- package/src/extensions/hr.ts +0 -74
- package/src/extensions/image.ts +0 -69
- package/src/extensions/mermaid.ts +0 -11
- package/src/extensions/tasklist.ts +0 -121
- package/src/extensions/util.ts +0 -18
- package/src/hooks/automerge/handle.ts +0 -14
- package/src/hooks/automerge/index.ts +0 -6
- package/src/hooks/automerge/plugin.ts +0 -132
- package/src/hooks/automerge/semaphore.ts +0 -78
- package/src/hooks/automerge/update-automerge.ts +0 -44
- package/src/hooks/yjs/index.ts +0 -6
- package/src/hooks/yjs/space-provider.ts +0 -126
- package/src/hooks/yjs/yjs.stories.tsx +0 -56
- package/src/hooks/yjs/yjs.test.ts +0 -59
- 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 -189
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /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/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// TODO(burdon): Additional copyright?
|
|
6
|
-
|
|
7
|
-
import { type EditorView } from '@codemirror/view';
|
|
8
|
-
|
|
9
|
-
import { next as automerge } from '@dxos/automerge/automerge';
|
|
10
|
-
|
|
11
|
-
import { type IDocHandle } from './handle';
|
|
12
|
-
import { type Field, isReconcileTx, getPath, reconcileAnnotationType, updateHeads, getLastHeads } from './plugin';
|
|
13
|
-
import { updateAutomerge } from './update-automerge';
|
|
14
|
-
import { updateCodeMirror } from './update-codemirror';
|
|
15
|
-
|
|
16
|
-
type Doc<T> = automerge.Doc<T>;
|
|
17
|
-
type Heads = automerge.Heads;
|
|
18
|
-
|
|
19
|
-
type ChangeFn = (atHeads: Heads, change: (doc: Doc<unknown>) => void) => Heads | undefined;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* TODO(burdon): Comment.
|
|
23
|
-
*/
|
|
24
|
-
export class PatchSemaphore {
|
|
25
|
-
_field!: Field;
|
|
26
|
-
_inReconcile = false;
|
|
27
|
-
_queue: Array<ChangeFn> = [];
|
|
28
|
-
|
|
29
|
-
constructor(field?: Field) {
|
|
30
|
-
if (field !== undefined) {
|
|
31
|
-
this._field = field;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
reconcile = (handle: IDocHandle, view: EditorView) => {
|
|
36
|
-
if (this._inReconcile) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
this._inReconcile = true;
|
|
40
|
-
|
|
41
|
-
const path = getPath(view.state, this._field);
|
|
42
|
-
const oldHeads = getLastHeads(view.state, this._field);
|
|
43
|
-
let selection = view.state.selection;
|
|
44
|
-
|
|
45
|
-
const transactions = view.state.field(this._field).unreconciledTransactions.filter((tx) => !isReconcileTx(tx));
|
|
46
|
-
|
|
47
|
-
// First undo all the unreconciled transactions.
|
|
48
|
-
const toInvert = transactions.slice().reverse();
|
|
49
|
-
for (const tx of toInvert) {
|
|
50
|
-
const inverted = tx.changes.invert(tx.startState.doc);
|
|
51
|
-
selection = selection.map(inverted);
|
|
52
|
-
view.dispatch({
|
|
53
|
-
changes: inverted,
|
|
54
|
-
annotations: reconcileAnnotationType.of(true),
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Apply the unreconciled transactions to the document.
|
|
59
|
-
let newHeads = updateAutomerge(this._field, handle, transactions, view.state);
|
|
60
|
-
|
|
61
|
-
// NOTE: null and undefined each come from automerge and repo respectively.
|
|
62
|
-
if (newHeads === null || newHeads === undefined) {
|
|
63
|
-
// TODO: @alexjg this is the call that's resetting the editor state on click
|
|
64
|
-
newHeads = automerge.getHeads(handle.docSync()!);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Now get the diff between the updated state of the document and the heads and apply that to the codemirror doc.
|
|
68
|
-
const diff = automerge.equals(oldHeads, newHeads) ? [] : automerge.diff(handle.docSync()!, oldHeads, newHeads);
|
|
69
|
-
updateCodeMirror(view, selection, path, diff);
|
|
70
|
-
|
|
71
|
-
view.dispatch({
|
|
72
|
-
effects: updateHeads(newHeads),
|
|
73
|
-
annotations: reconcileAnnotationType.of({}),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
this._inReconcile = false;
|
|
77
|
-
};
|
|
78
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// TODO(burdon): Additional copyright?
|
|
6
|
-
|
|
7
|
-
import { type EditorState, type Text, type Transaction } from '@codemirror/state';
|
|
8
|
-
|
|
9
|
-
import { next as am, type Heads } from '@dxos/automerge/automerge';
|
|
10
|
-
|
|
11
|
-
import { type IDocHandle } from './handle';
|
|
12
|
-
import { type Field } from './plugin';
|
|
13
|
-
|
|
14
|
-
export const updateAutomerge = (
|
|
15
|
-
field: Field,
|
|
16
|
-
handle: IDocHandle,
|
|
17
|
-
transactions: Transaction[],
|
|
18
|
-
state: EditorState,
|
|
19
|
-
): Heads | undefined => {
|
|
20
|
-
const { lastHeads, path } = state.field(field);
|
|
21
|
-
|
|
22
|
-
// We don't want to call `automerge.updateAt` if there are no changes.
|
|
23
|
-
// Otherwise, later on `automerge.diff` will return empty patches that result in a no-op but still mess up the selection.
|
|
24
|
-
let hasChanges = false;
|
|
25
|
-
for (const tr of transactions) {
|
|
26
|
-
tr.changes.iterChanges(() => {
|
|
27
|
-
hasChanges = true;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (!hasChanges) {
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const newHeads = handle.changeAt(lastHeads, (doc: am.Doc<unknown>) => {
|
|
36
|
-
for (const tr of transactions) {
|
|
37
|
-
tr.changes.iterChanges((fromA: number, toA: number, _fromB: number, _toB: number, inserted: Text) => {
|
|
38
|
-
am.splice(doc, path, fromA, toA - fromA, inserted.toString());
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return newHeads ?? undefined;
|
|
44
|
-
};
|
package/src/hooks/yjs/index.ts
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as decoding from 'lib0/decoding';
|
|
6
|
-
import * as encoding from 'lib0/encoding';
|
|
7
|
-
import { Observable } from 'lib0/observable';
|
|
8
|
-
import * as awarenessProtocol from 'y-protocols/awareness';
|
|
9
|
-
import { type Doc } from 'yjs';
|
|
10
|
-
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { type Space } from '@dxos/react-client/echo';
|
|
13
|
-
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
14
|
-
|
|
15
|
-
type Awareness = awarenessProtocol.Awareness;
|
|
16
|
-
|
|
17
|
-
// Based on https://github.com/yjs/y-webrtc/blob/88baab2/src/y-webrtc.js.
|
|
18
|
-
|
|
19
|
-
const messageAwareness = 1;
|
|
20
|
-
const messageQueryAwareness = 3;
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Yjs awareness provider on top of a DXOS space.
|
|
24
|
-
*/
|
|
25
|
-
export class SpaceAwarenessProvider extends Observable<any> {
|
|
26
|
-
private readonly _space: Space;
|
|
27
|
-
private readonly _awareness: Awareness;
|
|
28
|
-
private readonly _clientId: number;
|
|
29
|
-
private readonly _channel: string;
|
|
30
|
-
|
|
31
|
-
constructor({ space, doc, channel, awareness }: { space: Space; doc: Doc; channel: string; awareness?: Awareness }) {
|
|
32
|
-
super();
|
|
33
|
-
this._space = space;
|
|
34
|
-
this._awareness = awareness ?? new awarenessProtocol.Awareness(doc);
|
|
35
|
-
this._channel = channel;
|
|
36
|
-
this._clientId = doc.clientID;
|
|
37
|
-
|
|
38
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
39
|
-
this._space.listen(this._channel, this._handleSpaceMessage.bind(this));
|
|
40
|
-
|
|
41
|
-
if (typeof window !== 'undefined') {
|
|
42
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
43
|
-
} else if (typeof process !== 'undefined') {
|
|
44
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Post queryAwareness.
|
|
48
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
49
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
50
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessQuery));
|
|
51
|
-
|
|
52
|
-
// Post local awareness state.
|
|
53
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
54
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
55
|
-
encoding.writeVarUint8Array(
|
|
56
|
-
encoderAwarenessState,
|
|
57
|
-
awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this._clientId]),
|
|
58
|
-
);
|
|
59
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessState));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get awareness(): Awareness {
|
|
63
|
-
return this._awareness;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
67
|
-
log('awareness update', { added, updated, removed, origin });
|
|
68
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
69
|
-
const encoderAwareness = encoding.createEncoder();
|
|
70
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
71
|
-
encoding.writeVarUint8Array(
|
|
72
|
-
encoderAwareness,
|
|
73
|
-
awarenessProtocol.encodeAwarenessUpdate(this._awareness, changedClients),
|
|
74
|
-
);
|
|
75
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwareness));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private _handleSpaceMessage({ payload }: GossipMessage) {
|
|
79
|
-
log('space message', payload);
|
|
80
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
81
|
-
const encoder = this._readMessage(data);
|
|
82
|
-
if (encoder) {
|
|
83
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoder));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
private _readMessage(message: Uint8Array) {
|
|
88
|
-
const decoder = decoding.createDecoder(message);
|
|
89
|
-
const encoder = encoding.createEncoder();
|
|
90
|
-
const messageType = decoding.readVarUint(decoder);
|
|
91
|
-
|
|
92
|
-
let sendReply = false;
|
|
93
|
-
switch (messageType) {
|
|
94
|
-
case messageAwareness: {
|
|
95
|
-
awarenessProtocol.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
case messageQueryAwareness: {
|
|
100
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
101
|
-
encoding.writeVarUint8Array(
|
|
102
|
-
encoder,
|
|
103
|
-
awarenessProtocol.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
104
|
-
);
|
|
105
|
-
sendReply = true;
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
default: {
|
|
110
|
-
console.error('Invalid message:', messageType);
|
|
111
|
-
return encoder;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (!sendReply) {
|
|
116
|
-
// Nothing has been written, no answer created.
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return encoder;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private _handleBeforeUnload() {
|
|
124
|
-
awarenessProtocol.removeAwarenessStates(this._awareness, [this._clientId], 'window unload');
|
|
125
|
-
}
|
|
126
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import '@dxosTheme';
|
|
6
|
-
|
|
7
|
-
import React, { useState } from 'react';
|
|
8
|
-
|
|
9
|
-
import { PublicKey } from '@dxos/react-client';
|
|
10
|
-
import { TextKind } from '@dxos/react-client/echo';
|
|
11
|
-
import { ClientRepeater, textGenerator, useDataGenerator } from '@dxos/react-client/testing';
|
|
12
|
-
import { useId, Input } from '@dxos/react-ui';
|
|
13
|
-
import { withTheme } from '@dxos/storybook-utils';
|
|
14
|
-
|
|
15
|
-
import { MarkdownEditor } from '../../components';
|
|
16
|
-
import { Replicator, useYjsModel } from '../../testing';
|
|
17
|
-
|
|
18
|
-
export default {
|
|
19
|
-
title: 'react-ui-editor/YJS',
|
|
20
|
-
component: MarkdownEditor,
|
|
21
|
-
decorators: [withTheme],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const replicator = new Replicator(TextKind.PLAIN);
|
|
25
|
-
|
|
26
|
-
const Story = () => {
|
|
27
|
-
const [generate, setGenerate] = useState(false);
|
|
28
|
-
const generateId = useId('generate');
|
|
29
|
-
|
|
30
|
-
const [id] = useState(PublicKey.random().toHex());
|
|
31
|
-
const model = useYjsModel({ id, replicator });
|
|
32
|
-
|
|
33
|
-
useDataGenerator({
|
|
34
|
-
generator: generate ? textGenerator : undefined,
|
|
35
|
-
options: { text: typeof model?.content !== 'string' ? (model?.content as any) : undefined },
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
return (
|
|
39
|
-
<main className='flex flex-col flex-1 p-4 gap-4'>
|
|
40
|
-
<div id={generateId} className='flex gap-2'>
|
|
41
|
-
<Input.Root>
|
|
42
|
-
<Input.Checkbox checked={generate} onCheckedChange={(checked) => setGenerate(!!checked)} />
|
|
43
|
-
<Input.Label>Start</Input.Label>
|
|
44
|
-
</Input.Root>
|
|
45
|
-
</div>
|
|
46
|
-
<div className='border'>
|
|
47
|
-
<MarkdownEditor model={model} />
|
|
48
|
-
</div>
|
|
49
|
-
</main>
|
|
50
|
-
);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export const Default = {
|
|
54
|
-
// TODO(wittjosiah): Decorator for doing this without clients being initialized?
|
|
55
|
-
render: () => <ClientRepeater count={2} Component={Story} />,
|
|
56
|
-
};
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { effect } from '@preact/signals-react';
|
|
6
|
-
import { expect } from 'chai';
|
|
7
|
-
import * as Y from 'yjs';
|
|
8
|
-
|
|
9
|
-
import { Trigger } from '@dxos/async';
|
|
10
|
-
import { Expando, setGlobalAutomergePreference, TextObject } from '@dxos/echo-schema';
|
|
11
|
-
import { registerSignalFactory } from '@dxos/echo-signals';
|
|
12
|
-
import { describe, test } from '@dxos/test';
|
|
13
|
-
import { type YText } from '@dxos/text-model';
|
|
14
|
-
|
|
15
|
-
describe('YJS', () => {
|
|
16
|
-
// https://docs.yjs.dev/api/shared-types/y.text
|
|
17
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
18
|
-
test('absolute position', () => {
|
|
19
|
-
const obj = new TextObject('hello world');
|
|
20
|
-
const index = obj.text.indexOf('world');
|
|
21
|
-
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
22
|
-
|
|
23
|
-
{
|
|
24
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
25
|
-
expect(cursor!.index).to.equal(index);
|
|
26
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
(obj.content as YText).insert(index, 'cruel ');
|
|
30
|
-
expect(obj.text).to.equal('hello cruel world');
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
34
|
-
expect(cursor!.index).not.to.equal(index);
|
|
35
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// TODO(burdon): Test deleting end character.
|
|
40
|
-
|
|
41
|
-
test.skip('effect', async () => {
|
|
42
|
-
setGlobalAutomergePreference(true);
|
|
43
|
-
registerSignalFactory();
|
|
44
|
-
const obj = new Expando({ comments: [] });
|
|
45
|
-
|
|
46
|
-
const trigger = new Trigger();
|
|
47
|
-
const unsubscribe = effect(() => {
|
|
48
|
-
// TODO(burdon): BUG: doesn't work with YJS.
|
|
49
|
-
console.log(obj.comments);
|
|
50
|
-
if (obj.comments.length > 0) {
|
|
51
|
-
trigger.wake();
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
obj.comments.push({ message: 'hello' });
|
|
56
|
-
await trigger.wait();
|
|
57
|
-
unsubscribe();
|
|
58
|
-
});
|
|
59
|
-
});
|
package/src/testing/index.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @generated @dxos/echo-typegen src/testing/proto/schema.proto
|
|
3
|
-
**/
|
|
4
|
-
|
|
5
|
-
import * as dxos_echo_schema from "@dxos/echo-schema";
|
|
6
|
-
|
|
7
|
-
export const types = new dxos_echo_schema.TypeCollection();
|
|
8
|
-
|
|
9
|
-
export type EditorDocumentProps = {
|
|
10
|
-
content: dxos_echo_schema.Text;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export class EditorDocument extends dxos_echo_schema.TypedObject<EditorDocumentProps> {
|
|
14
|
-
declare static readonly schema: dxos_echo_schema.Schema;
|
|
15
|
-
|
|
16
|
-
static filter(
|
|
17
|
-
filter?:
|
|
18
|
-
| Partial<EditorDocumentProps>
|
|
19
|
-
| dxos_echo_schema.OperatorFilter<EditorDocument>
|
|
20
|
-
): dxos_echo_schema.Filter<EditorDocument> {
|
|
21
|
-
return dxos_echo_schema.Filter.typename(
|
|
22
|
-
"dxos.testing.editor.EditorDocument",
|
|
23
|
-
filter
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
initValues?: Partial<EditorDocumentProps>,
|
|
29
|
-
opts?: dxos_echo_schema.TypedObjectOptions
|
|
30
|
-
) {
|
|
31
|
-
super({ ...initValues }, { schema: EditorDocument.schema, ...opts });
|
|
32
|
-
}
|
|
33
|
-
declare content: dxos_echo_schema.Text;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
types.registerPrototype(EditorDocument, {
|
|
37
|
-
typename: "dxos.testing.editor.EditorDocument",
|
|
38
|
-
props: [
|
|
39
|
-
{
|
|
40
|
-
id: "content",
|
|
41
|
-
type: 5,
|
|
42
|
-
repeated: false,
|
|
43
|
-
refName: "dxos.schema.Text",
|
|
44
|
-
refModelType: "dxos.org/model/text",
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
types.link();
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
// TODO(wittjosiah): Remove. Use DXOS protocols to reduce duplicate functionality weighing down apps.
|
|
6
|
-
import * as decoding from 'lib0/decoding';
|
|
7
|
-
import * as encoding from 'lib0/encoding';
|
|
8
|
-
import { Observable } from 'lib0/observable';
|
|
9
|
-
import { useMemo } from 'react';
|
|
10
|
-
import * as awarenessProtocol from 'y-protocols/awareness';
|
|
11
|
-
import * as syncProtocol from 'y-protocols/sync';
|
|
12
|
-
import { Doc } from 'yjs';
|
|
13
|
-
|
|
14
|
-
import { Event } from '@dxos/async';
|
|
15
|
-
import { log } from '@dxos/log';
|
|
16
|
-
import { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
|
|
17
|
-
|
|
18
|
-
import { cursorColor, type EditorModel } from '../hooks';
|
|
19
|
-
|
|
20
|
-
type Awareness = awarenessProtocol.Awareness;
|
|
21
|
-
|
|
22
|
-
const messageSync = 0;
|
|
23
|
-
const messageAwareness = 1;
|
|
24
|
-
const messageQueryAwareness = 3;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Yjs awareness provider in-memory.
|
|
28
|
-
*/
|
|
29
|
-
export class AwarenessProvider extends Observable<any> {
|
|
30
|
-
private readonly _remoteUpdate: Event<Uint8Array>;
|
|
31
|
-
private readonly _awareness: Awareness;
|
|
32
|
-
private readonly _doc: Doc;
|
|
33
|
-
|
|
34
|
-
constructor({ update, doc, awareness }: { update: Event<Uint8Array>; doc: Doc; awareness?: Awareness }) {
|
|
35
|
-
super();
|
|
36
|
-
this._remoteUpdate = update;
|
|
37
|
-
this._doc = doc;
|
|
38
|
-
this._awareness = awareness ?? new awarenessProtocol.Awareness(doc);
|
|
39
|
-
|
|
40
|
-
this._doc.on('update', this._handleDocUpdate.bind(this));
|
|
41
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
42
|
-
this._remoteUpdate.on(this._handleRemoteUpdate.bind(this));
|
|
43
|
-
|
|
44
|
-
if (typeof window !== 'undefined') {
|
|
45
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
46
|
-
} else if (typeof process !== 'undefined') {
|
|
47
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Post queryAwareness.
|
|
51
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
52
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
53
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessQuery));
|
|
54
|
-
|
|
55
|
-
// Post local awareness state.
|
|
56
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
57
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
58
|
-
encoding.writeVarUint8Array(
|
|
59
|
-
encoderAwarenessState,
|
|
60
|
-
awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this._doc.clientID]),
|
|
61
|
-
);
|
|
62
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessState));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
get awareness(): Awareness {
|
|
66
|
-
return this._awareness;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
private _handleDocUpdate(update: Uint8Array, origin: any) {
|
|
70
|
-
log('doc update', { update, origin });
|
|
71
|
-
const encoder = encoding.createEncoder();
|
|
72
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
73
|
-
syncProtocol.writeUpdate(encoder, update);
|
|
74
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
78
|
-
log('awareness update', { added, updated, removed, origin });
|
|
79
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
80
|
-
const encoderAwareness = encoding.createEncoder();
|
|
81
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
82
|
-
encoding.writeVarUint8Array(
|
|
83
|
-
encoderAwareness,
|
|
84
|
-
awarenessProtocol.encodeAwarenessUpdate(this._awareness, changedClients),
|
|
85
|
-
);
|
|
86
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoderAwareness));
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private _handleRemoteUpdate(payload: Uint8Array) {
|
|
90
|
-
log('remote update', { payload });
|
|
91
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
92
|
-
const encoder = this._readMessage(data);
|
|
93
|
-
if (encoder) {
|
|
94
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private _readMessage(message: Uint8Array) {
|
|
99
|
-
const decoder = decoding.createDecoder(message);
|
|
100
|
-
const encoder = encoding.createEncoder();
|
|
101
|
-
const messageType = decoding.readVarUint(decoder);
|
|
102
|
-
|
|
103
|
-
let sendReply = false;
|
|
104
|
-
switch (messageType) {
|
|
105
|
-
case messageSync: {
|
|
106
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
107
|
-
syncProtocol.readSyncMessage(decoder, encoder, this._doc, this);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
case messageAwareness: {
|
|
112
|
-
awarenessProtocol.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
113
|
-
break;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
case messageQueryAwareness: {
|
|
117
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
118
|
-
encoding.writeVarUint8Array(
|
|
119
|
-
encoder,
|
|
120
|
-
awarenessProtocol.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
121
|
-
);
|
|
122
|
-
sendReply = true;
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
default: {
|
|
127
|
-
console.error('Invalid message:', messageType);
|
|
128
|
-
return encoder;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (!sendReply) {
|
|
133
|
-
// Nothing has been written, no answer created.
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return encoder;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private _handleBeforeUnload() {
|
|
141
|
-
awarenessProtocol.removeAwarenessStates(this._awareness, [this._doc.clientID], 'window unload');
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* This mocks the replication across the swarm.
|
|
147
|
-
*/
|
|
148
|
-
export class Replicator {
|
|
149
|
-
private readonly _update = new Event<Uint8Array>();
|
|
150
|
-
private _peers: EditorModel[] = [];
|
|
151
|
-
|
|
152
|
-
constructor(private readonly _kind: TextKind) {}
|
|
153
|
-
|
|
154
|
-
setPeers(peers: EditorModel[]) {
|
|
155
|
-
this._peers = peers;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
createPeer = (id: string, doc: Doc = new Doc()): EditorModel => {
|
|
159
|
-
const provider = new AwarenessProvider({ update: this._update, doc });
|
|
160
|
-
provider.awareness.setLocalStateField('user', {
|
|
161
|
-
name: 'Anonymous ' + Math.floor(Math.random() * 100),
|
|
162
|
-
color: cursorColor.color,
|
|
163
|
-
colorLight: cursorColor.light,
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
const field = 'content';
|
|
167
|
-
const content = this._kind === TextKind.PLAIN ? doc.getText(field) : doc.getXmlFragment(field);
|
|
168
|
-
const model: EditorModel = {
|
|
169
|
-
id: doc.guid,
|
|
170
|
-
text: () => content.toString(),
|
|
171
|
-
content,
|
|
172
|
-
awareness: provider.awareness,
|
|
173
|
-
peer: { id },
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
this._peers.push(model);
|
|
177
|
-
return model;
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export type UseYjsModelOptions = {
|
|
182
|
-
replicator: Replicator;
|
|
183
|
-
id: string;
|
|
184
|
-
doc?: Doc;
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
export const useYjsModel = ({ replicator, id, doc }: UseYjsModelOptions): EditorModel => {
|
|
188
|
-
return useMemo(() => replicator.createPeer(id, doc), [doc]);
|
|
189
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|