@dxos/react-ui-editor 0.3.11-main.9c729f3 → 0.3.11-main.9de2c34
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 +939 -1067
- 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 -13
- 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/components/TextEditor/comments.stories.d.ts +26 -0
- package/dist/types/src/components/TextEditor/comments.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.map +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +7 -9
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +7 -12
- package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts +2 -2
- package/dist/types/src/extensions/awareness.d.ts +1 -1
- package/dist/types/src/extensions/awareness.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +4 -5
- package/dist/types/src/extensions/basic.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +32 -19
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/{util → extensions}/cursor.d.ts +10 -2
- package/dist/types/src/extensions/cursor.d.ts.map +1 -0
- 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 +2 -2
- 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/bundle.d.ts +2 -5
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- 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/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/hooks/awareness-provider.d.ts +7 -4
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -1
- package/dist/types/src/hooks/defs.d.ts +25 -0
- package/dist/types/src/hooks/defs.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +2 -2
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextEditor.d.ts +17 -0
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextModel.d.ts +2 -32
- 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/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 +33 -35
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +77 -80
- package/src/components/TextEditor/TextEditor.stories.tsx +12 -46
- package/src/components/TextEditor/TextEditor.tsx +122 -93
- package/src/components/TextEditor/comments.stories.tsx +357 -0
- package/src/extensions/autocomplete.ts +23 -19
- package/src/extensions/automerge/automerge.ts +40 -46
- package/src/extensions/automerge/cursor.ts +2 -1
- package/src/extensions/automerge/defs.ts +10 -31
- package/src/extensions/automerge/semaphore.ts +23 -20
- package/src/extensions/automerge/update-automerge.ts +2 -2
- package/src/extensions/awareness.ts +40 -46
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/code.ts +28 -23
- package/src/extensions/comments.ts +249 -242
- package/src/extensions/cursor.ts +50 -0
- package/src/extensions/hr.ts +50 -52
- package/src/extensions/image.ts +40 -22
- package/src/extensions/index.ts +2 -2
- package/src/extensions/link.ts +86 -88
- package/src/extensions/markdown/bundle.ts +18 -24
- package/src/extensions/markdown/highlight.ts +3 -12
- package/src/extensions/mention.ts +9 -2
- package/src/extensions/outliner.ts +12 -0
- package/src/extensions/table.ts +10 -4
- package/src/extensions/tasklist.ts +78 -88
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +3 -2
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +48 -60
- package/src/index.ts +3 -0
- package/src/styles/markdown.ts +0 -2
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +51 -78
- 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/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.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/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/dist/types/src/util/cursor.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -3
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/util.d.ts +0 -5
- package/dist/types/src/util/util.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/mermaid.ts +0 -11
- package/src/extensions/yjs/cursor.ts +0 -21
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -178
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- package/src/util/cursor.ts +0 -29
- package/src/util/index.ts +0 -6
- package/src/util/util.ts +0 -18
|
@@ -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 './TextEditor';
|
|
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,11 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Extension } from '@codemirror/state';
|
|
6
|
-
|
|
7
|
-
// TODO(burdon): How will this interact with the code extension?
|
|
8
|
-
// TODO(burdon): Move to mermaid-plugin (which should contribute this extension).
|
|
9
|
-
export const mermaid = (): Extension => {
|
|
10
|
-
return [];
|
|
11
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import * as Y from 'yjs';
|
|
6
|
-
|
|
7
|
-
import { arrayToString, stringToArray } from '@dxos/util';
|
|
8
|
-
|
|
9
|
-
import { type CursorConverter } from '../../util';
|
|
10
|
-
|
|
11
|
-
export const cursorConverter = (text: Y.Text): CursorConverter => ({
|
|
12
|
-
toCursor: (index, assoc) => {
|
|
13
|
-
return arrayToString(Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text, index, assoc)));
|
|
14
|
-
},
|
|
15
|
-
fromCursor: (cursor) => {
|
|
16
|
-
return (
|
|
17
|
-
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
18
|
-
0
|
|
19
|
-
);
|
|
20
|
-
},
|
|
21
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { expect } from 'chai';
|
|
6
|
-
import * as Y from 'yjs';
|
|
7
|
-
|
|
8
|
-
import { TextObject } from '@dxos/echo-schema';
|
|
9
|
-
import { describe, test } from '@dxos/test';
|
|
10
|
-
import { type YText } from '@dxos/text-model';
|
|
11
|
-
|
|
12
|
-
describe('YJS', () => {
|
|
13
|
-
// https://docs.yjs.dev/api/shared-types/y.text
|
|
14
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
15
|
-
test('absolute position', () => {
|
|
16
|
-
const obj = new TextObject('hello world');
|
|
17
|
-
const index = obj.text.indexOf('world');
|
|
18
|
-
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
19
|
-
|
|
20
|
-
{
|
|
21
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
22
|
-
expect(cursor!.index).to.equal(index);
|
|
23
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
(obj.content as YText).insert(index, 'cruel ');
|
|
27
|
-
expect(obj.text).to.equal('hello cruel world');
|
|
28
|
-
|
|
29
|
-
{
|
|
30
|
-
const cursor = Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(relPos), obj.doc!);
|
|
31
|
-
expect(cursor!.index).not.to.equal(index);
|
|
32
|
-
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { yCollab } from 'y-codemirror.next';
|
|
6
|
-
import type * as YP from 'y-protocols/awareness';
|
|
7
|
-
|
|
8
|
-
import type { YText } from '@dxos/text-model';
|
|
9
|
-
|
|
10
|
-
import { cursorConverter } from './cursor';
|
|
11
|
-
import { CursorConverter } from '../../util';
|
|
12
|
-
|
|
13
|
-
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
-
CursorConverter.of(cursorConverter(content)),
|
|
15
|
-
yCollab(content, awareness),
|
|
16
|
-
];
|
package/src/hooks/automerge.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import get from 'lodash.get';
|
|
6
|
-
|
|
7
|
-
import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
8
|
-
import { isNotNullOrUndefined } from '@dxos/util';
|
|
9
|
-
|
|
10
|
-
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
11
|
-
import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
|
|
12
|
-
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
13
|
-
import { cursorColor } from '../styles';
|
|
14
|
-
|
|
15
|
-
export const createAutomergeModel = ({ space, identity, text }: UseTextModelProps): EditorModel => {
|
|
16
|
-
const obj = text as any as AutomergeTextCompat;
|
|
17
|
-
const doc = getRawDoc(obj, [obj.field]);
|
|
18
|
-
|
|
19
|
-
const awarenessProvider =
|
|
20
|
-
space &&
|
|
21
|
-
new SpaceAwarenessProvider({
|
|
22
|
-
space,
|
|
23
|
-
channel: `automerge.awareness.${obj.id}`,
|
|
24
|
-
info: {
|
|
25
|
-
displayName: identity?.profile?.displayName ?? '',
|
|
26
|
-
color: cursorColor.color,
|
|
27
|
-
lightColor: cursorColor.light,
|
|
28
|
-
},
|
|
29
|
-
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
const model: EditorModel = {
|
|
33
|
-
id: obj.id,
|
|
34
|
-
content: doc,
|
|
35
|
-
text: () => get(doc.handle.docSync(), doc.path),
|
|
36
|
-
// TODO(burdon): https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
37
|
-
extension: [
|
|
38
|
-
modelState.init(() => model),
|
|
39
|
-
automerge({ handle: doc.handle, path: doc.path }),
|
|
40
|
-
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
41
|
-
awareness(),
|
|
42
|
-
].filter(isNotNullOrUndefined),
|
|
43
|
-
peer: identity
|
|
44
|
-
? {
|
|
45
|
-
id: identity.identityKey.toHex(),
|
|
46
|
-
name: identity.profile?.displayName,
|
|
47
|
-
}
|
|
48
|
-
: undefined,
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return model;
|
|
52
|
-
};
|
package/src/hooks/yjs.ts
DELETED
|
@@ -1,178 +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 YP from 'y-protocols/awareness';
|
|
9
|
-
import * as Y from 'yjs';
|
|
10
|
-
import { type Doc } from 'yjs';
|
|
11
|
-
|
|
12
|
-
import { invariant } from '@dxos/invariant';
|
|
13
|
-
import { log } from '@dxos/log';
|
|
14
|
-
import { type Space } from '@dxos/react-client/echo';
|
|
15
|
-
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
16
|
-
import type { YText } from '@dxos/text-model';
|
|
17
|
-
import { arrayToString, stringToArray } from '@dxos/util';
|
|
18
|
-
|
|
19
|
-
import { type EditorModel, modelState, type Range, type UseTextModelProps } from './useTextModel';
|
|
20
|
-
import { yjs } from '../extensions';
|
|
21
|
-
|
|
22
|
-
export const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
|
|
23
|
-
invariant(text?.doc && text?.content);
|
|
24
|
-
const provider = space
|
|
25
|
-
? new YAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` })
|
|
26
|
-
: undefined;
|
|
27
|
-
|
|
28
|
-
const model: EditorModel = {
|
|
29
|
-
id: text.doc.guid,
|
|
30
|
-
content: text.content,
|
|
31
|
-
text: () => text.content!.toString(),
|
|
32
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
33
|
-
// TODO(dmaretskyi): Refactor as cursor-converter.
|
|
34
|
-
getCursorFromRange: (range: Range) => {
|
|
35
|
-
const from = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.from));
|
|
36
|
-
const to = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.to, -1));
|
|
37
|
-
return [arrayToString(from), arrayToString(to)].join(':');
|
|
38
|
-
},
|
|
39
|
-
getRangeFromCursor: (cursor: string) => {
|
|
40
|
-
invariant(text.doc);
|
|
41
|
-
const parts = cursor.split(':');
|
|
42
|
-
const from = Y.createAbsolutePositionFromRelativePosition(
|
|
43
|
-
Y.decodeRelativePosition(stringToArray(parts[0])),
|
|
44
|
-
text.doc,
|
|
45
|
-
);
|
|
46
|
-
const to = Y.createAbsolutePositionFromRelativePosition(
|
|
47
|
-
Y.decodeRelativePosition(stringToArray(parts[1])),
|
|
48
|
-
text.doc,
|
|
49
|
-
);
|
|
50
|
-
return from && to ? { from: from.index, to: to.index } : undefined;
|
|
51
|
-
},
|
|
52
|
-
extension: [modelState.init(() => model), yjs(text.content as YText, provider?.awareness)],
|
|
53
|
-
awareness: provider?.awareness,
|
|
54
|
-
peer: identity
|
|
55
|
-
? {
|
|
56
|
-
id: identity.identityKey.toHex(),
|
|
57
|
-
name: identity.profile?.displayName,
|
|
58
|
-
}
|
|
59
|
-
: undefined,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
return model;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// Based on https://github.com/yjs/y-webrtc/blob/88baab2/src/y-webrtc.js.
|
|
66
|
-
|
|
67
|
-
const messageAwareness = 1;
|
|
68
|
-
const messageQueryAwareness = 3;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Yjs awareness provider on top of a DXOS space.
|
|
72
|
-
*/
|
|
73
|
-
export class YAwarenessProvider extends Observable<any> {
|
|
74
|
-
private readonly _space: Space;
|
|
75
|
-
private readonly _awareness: YP.Awareness;
|
|
76
|
-
private readonly _clientId: number;
|
|
77
|
-
private readonly _channel: string;
|
|
78
|
-
|
|
79
|
-
constructor({
|
|
80
|
-
space,
|
|
81
|
-
doc,
|
|
82
|
-
channel,
|
|
83
|
-
awareness,
|
|
84
|
-
}: {
|
|
85
|
-
space: Space;
|
|
86
|
-
doc: Doc;
|
|
87
|
-
channel: string;
|
|
88
|
-
awareness?: YP.Awareness;
|
|
89
|
-
}) {
|
|
90
|
-
super();
|
|
91
|
-
this._space = space;
|
|
92
|
-
this._awareness = awareness ?? new YP.Awareness(doc);
|
|
93
|
-
this._channel = channel;
|
|
94
|
-
this._clientId = doc.clientID;
|
|
95
|
-
|
|
96
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
97
|
-
this._space.listen(this._channel, this._handleSpaceMessage.bind(this));
|
|
98
|
-
|
|
99
|
-
if (typeof window !== 'undefined') {
|
|
100
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
101
|
-
} else if (typeof process !== 'undefined') {
|
|
102
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// Post queryAwareness.
|
|
106
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
107
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
108
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessQuery));
|
|
109
|
-
|
|
110
|
-
// Post local awareness state.
|
|
111
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
112
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
113
|
-
encoding.writeVarUint8Array(encoderAwarenessState, YP.encodeAwarenessUpdate(this.awareness, [this._clientId]));
|
|
114
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessState));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
get awareness(): YP.Awareness {
|
|
118
|
-
return this._awareness;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
122
|
-
log('awareness update', { added, updated, removed, origin });
|
|
123
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
124
|
-
const encoderAwareness = encoding.createEncoder();
|
|
125
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
126
|
-
encoding.writeVarUint8Array(encoderAwareness, YP.encodeAwarenessUpdate(this._awareness, changedClients));
|
|
127
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwareness));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
private _handleSpaceMessage({ payload }: GossipMessage) {
|
|
131
|
-
log('space message', payload);
|
|
132
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
133
|
-
const encoder = this._readMessage(data);
|
|
134
|
-
if (encoder) {
|
|
135
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoder));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
private _readMessage(message: Uint8Array) {
|
|
140
|
-
const decoder = decoding.createDecoder(message);
|
|
141
|
-
const encoder = encoding.createEncoder();
|
|
142
|
-
const messageType = decoding.readVarUint(decoder);
|
|
143
|
-
|
|
144
|
-
let sendReply = false;
|
|
145
|
-
switch (messageType) {
|
|
146
|
-
case messageAwareness: {
|
|
147
|
-
YP.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
case messageQueryAwareness: {
|
|
152
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
153
|
-
encoding.writeVarUint8Array(
|
|
154
|
-
encoder,
|
|
155
|
-
YP.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
156
|
-
);
|
|
157
|
-
sendReply = true;
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
default: {
|
|
162
|
-
console.error('Invalid message:', messageType);
|
|
163
|
-
return encoder;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (!sendReply) {
|
|
168
|
-
// Nothing has been written, no answer created.
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
return encoder;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private _handleBeforeUnload() {
|
|
176
|
-
YP.removeAwarenessStates(this._awareness, [this._clientId], 'window unload');
|
|
177
|
-
}
|
|
178
|
-
}
|
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,184 +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 YP 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 { type EditorModel } from '../hooks';
|
|
19
|
-
import { cursorColor } from '../styles';
|
|
20
|
-
|
|
21
|
-
type Awareness = YP.Awareness;
|
|
22
|
-
|
|
23
|
-
const messageSync = 0;
|
|
24
|
-
const messageAwareness = 1;
|
|
25
|
-
const messageQueryAwareness = 3;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Yjs awareness provider in-memory.
|
|
29
|
-
*/
|
|
30
|
-
export class AwarenessProvider extends Observable<any> {
|
|
31
|
-
private readonly _remoteUpdate: Event<Uint8Array>;
|
|
32
|
-
private readonly _awareness: Awareness;
|
|
33
|
-
private readonly _doc: Doc;
|
|
34
|
-
|
|
35
|
-
constructor({ update, doc, awareness }: { update: Event<Uint8Array>; doc: Doc; awareness?: Awareness }) {
|
|
36
|
-
super();
|
|
37
|
-
this._remoteUpdate = update;
|
|
38
|
-
this._doc = doc;
|
|
39
|
-
this._awareness = awareness ?? new YP.Awareness(doc);
|
|
40
|
-
|
|
41
|
-
this._doc.on('update', this._handleDocUpdate.bind(this));
|
|
42
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
43
|
-
this._remoteUpdate.on(this._handleRemoteUpdate.bind(this));
|
|
44
|
-
|
|
45
|
-
if (typeof window !== 'undefined') {
|
|
46
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
47
|
-
} else if (typeof process !== 'undefined') {
|
|
48
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Post queryAwareness.
|
|
52
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
53
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
54
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessQuery));
|
|
55
|
-
|
|
56
|
-
// Post local awareness state.
|
|
57
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
58
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
59
|
-
encoding.writeVarUint8Array(encoderAwarenessState, YP.encodeAwarenessUpdate(this.awareness, [this._doc.clientID]));
|
|
60
|
-
void this._remoteUpdate.emit(encoding.toUint8Array(encoderAwarenessState));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get awareness(): Awareness {
|
|
64
|
-
return this._awareness;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private _handleDocUpdate(update: Uint8Array, origin: any) {
|
|
68
|
-
log('doc update', { update, origin });
|
|
69
|
-
const encoder = encoding.createEncoder();
|
|
70
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
71
|
-
syncProtocol.writeUpdate(encoder, update);
|
|
72
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
76
|
-
log('awareness update', { added, updated, removed, origin });
|
|
77
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
78
|
-
const encoderAwareness = encoding.createEncoder();
|
|
79
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
80
|
-
encoding.writeVarUint8Array(encoderAwareness, YP.encodeAwarenessUpdate(this._awareness, changedClients));
|
|
81
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoderAwareness));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private _handleRemoteUpdate(payload: Uint8Array) {
|
|
85
|
-
log('remote update', { payload });
|
|
86
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
87
|
-
const encoder = this._readMessage(data);
|
|
88
|
-
if (encoder) {
|
|
89
|
-
this._remoteUpdate.emit(encoding.toUint8Array(encoder));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
private _readMessage(message: Uint8Array) {
|
|
94
|
-
const decoder = decoding.createDecoder(message);
|
|
95
|
-
const encoder = encoding.createEncoder();
|
|
96
|
-
const messageType = decoding.readVarUint(decoder);
|
|
97
|
-
|
|
98
|
-
let sendReply = false;
|
|
99
|
-
switch (messageType) {
|
|
100
|
-
case messageSync: {
|
|
101
|
-
encoding.writeVarUint(encoder, messageSync);
|
|
102
|
-
syncProtocol.readSyncMessage(decoder, encoder, this._doc, this);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
case messageAwareness: {
|
|
107
|
-
YP.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
case messageQueryAwareness: {
|
|
112
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
113
|
-
encoding.writeVarUint8Array(
|
|
114
|
-
encoder,
|
|
115
|
-
YP.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
116
|
-
);
|
|
117
|
-
sendReply = true;
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
default: {
|
|
122
|
-
console.error('Invalid message:', messageType);
|
|
123
|
-
return encoder;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!sendReply) {
|
|
128
|
-
// Nothing has been written, no answer created.
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return encoder;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
private _handleBeforeUnload() {
|
|
136
|
-
YP.removeAwarenessStates(this._awareness, [this._doc.clientID], 'window unload');
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* This mocks the replication across the swarm.
|
|
142
|
-
*/
|
|
143
|
-
export class Replicator {
|
|
144
|
-
private readonly _update = new Event<Uint8Array>();
|
|
145
|
-
private _peers: EditorModel[] = [];
|
|
146
|
-
|
|
147
|
-
constructor(private readonly _kind: TextKind) {}
|
|
148
|
-
|
|
149
|
-
setPeers(peers: EditorModel[]) {
|
|
150
|
-
this._peers = peers;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
createPeer = (id: string, doc: Doc = new Doc()): EditorModel => {
|
|
154
|
-
const provider = new AwarenessProvider({ update: this._update, doc });
|
|
155
|
-
provider.awareness.setLocalStateField('user', {
|
|
156
|
-
name: 'Anonymous ' + Math.floor(Math.random() * 100),
|
|
157
|
-
color: cursorColor.color,
|
|
158
|
-
colorLight: cursorColor.light,
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const field = 'content';
|
|
162
|
-
const content = this._kind === TextKind.PLAIN ? doc.getText(field) : doc.getXmlFragment(field);
|
|
163
|
-
const model: EditorModel = {
|
|
164
|
-
id: doc.guid,
|
|
165
|
-
text: () => content.toString(),
|
|
166
|
-
content,
|
|
167
|
-
awareness: provider.awareness,
|
|
168
|
-
peer: { id },
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
this._peers.push(model);
|
|
172
|
-
return model;
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export type UseYjsModelOptions = {
|
|
177
|
-
replicator: Replicator;
|
|
178
|
-
id: string;
|
|
179
|
-
doc?: Doc;
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
export const useYjsModel = ({ replicator, id, doc }: UseYjsModelOptions): EditorModel => {
|
|
183
|
-
return useMemo(() => replicator.createPeer(id, doc), [doc]);
|
|
184
|
-
};
|
package/src/util/cursor.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { Facet } from '@codemirror/state';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Converts indexes into the text document into stable peer-independent cursors.
|
|
9
|
-
*
|
|
10
|
-
* See:
|
|
11
|
-
* - https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
12
|
-
* - https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
13
|
-
*
|
|
14
|
-
* @param {assoc} number Negative values will associate the cursor with the previous character
|
|
15
|
-
* while positive - with the next one.
|
|
16
|
-
*/
|
|
17
|
-
export interface CursorConverter {
|
|
18
|
-
toCursor(position: number, assoc?: -1 | 1 | undefined): string;
|
|
19
|
-
fromCursor(cursor: string): number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const DEFAULT_CURSOR_CONVERTER: CursorConverter = {
|
|
23
|
-
toCursor: (position) => position.toString(),
|
|
24
|
-
fromCursor: (cursor) => parseInt(cursor),
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const CursorConverter = Facet.define<CursorConverter, CursorConverter>({
|
|
28
|
-
combine: (providers) => providers[0] ?? DEFAULT_CURSOR_CONVERTER,
|
|
29
|
-
});
|