@dxos/react-ui-editor 0.3.11-main.cd64fe0 → 0.3.11-main.cf49a01
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 +1016 -1024
- 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 +12 -16
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +13 -5
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/comments.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/defs.d.ts +6 -8
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/semaphore.d.ts +6 -6
- 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/comments.d.ts +18 -14
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- package/dist/types/src/extensions/cursor.d.ts +3 -3
- package/dist/types/src/extensions/cursor.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -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/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 +7 -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 +3 -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/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 -27
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/styles/markdown.d.ts +2 -2
- package/dist/types/src/styles/markdown.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +2 -0
- package/dist/types/src/util.d.ts.map +1 -1
- package/package.json +32 -35
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +60 -65
- package/src/components/TextEditor/TextEditor.tsx +91 -66
- package/src/components/TextEditor/comments.stories.tsx +45 -43
- package/src/extensions/autocomplete.ts +6 -3
- package/src/extensions/automerge/automerge.ts +35 -39
- package/src/extensions/automerge/defs.ts +8 -33
- package/src/extensions/automerge/semaphore.ts +17 -12
- package/src/extensions/automerge/update-automerge.ts +2 -2
- package/src/extensions/awareness.ts +14 -14
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/comments.ts +161 -141
- package/src/extensions/cursor.ts +7 -3
- package/src/extensions/index.ts +0 -7
- package/src/extensions/markdown/bundle.ts +20 -30
- package/src/extensions/markdown/code.ts +127 -0
- package/src/extensions/markdown/heading.ts +59 -0
- package/src/extensions/markdown/highlight.ts +9 -15
- package/src/extensions/markdown/hr.ts +71 -0
- package/src/extensions/markdown/index.ts +7 -0
- package/src/extensions/{link.ts → markdown/link.ts} +1 -2
- package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +48 -34
- package/src/extensions/mention.ts +9 -2
- package/src/hooks/awareness-provider.ts +24 -20
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +2 -2
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +47 -56
- package/src/styles/markdown.ts +9 -10
- package/src/themes/default.ts +0 -2
- package/src/util.ts +26 -3
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +0 -12
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/extensions/code.d.ts.map +0 -1
- package/dist/types/src/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/extensions/image.d.ts.map +0 -1
- package/dist/types/src/extensions/link.d.ts.map +0 -1
- package/dist/types/src/extensions/table.d.ts.map +0 -1
- package/dist/types/src/extensions/tasklist.d.ts +0 -7
- package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/cursor.d.ts +0 -4
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/index.d.ts +0 -2
- package/dist/types/src/extensions/yjs/index.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.d.ts +0 -4
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +0 -1
- package/dist/types/src/extensions/yjs/yjs.test.d.ts +0 -2
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +0 -1
- package/dist/types/src/hooks/automerge.d.ts +0 -3
- package/dist/types/src/hooks/automerge.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs.d.ts +0 -27
- package/dist/types/src/hooks/yjs.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/proto/gen/schema.d.ts +0 -15
- package/dist/types/src/testing/proto/gen/schema.d.ts.map +0 -1
- package/dist/types/src/testing/proto/index.d.ts +0 -2
- package/dist/types/src/testing/proto/index.d.ts.map +0 -1
- package/dist/types/src/testing/replicator.d.ts +0 -45
- package/dist/types/src/testing/replicator.d.ts.map +0 -1
- package/src/components/TextEditor/codemirror.stories.ts +0 -115
- package/src/components/TextEditor/yjs.stories.tsx +0 -56
- package/src/extensions/code.ts +0 -104
- package/src/extensions/hr.ts +0 -72
- package/src/extensions/yjs/cursor.ts +0 -22
- package/src/extensions/yjs/index.ts +0 -5
- package/src/extensions/yjs/yjs.test.ts +0 -35
- package/src/extensions/yjs/yjs.ts +0 -16
- package/src/hooks/automerge.ts +0 -52
- package/src/hooks/yjs.ts +0 -156
- package/src/testing/index.ts +0 -6
- package/src/testing/proto/gen/schema.ts +0 -49
- package/src/testing/proto/index.ts +0 -5
- package/src/testing/proto/schema.proto +0 -15
- package/src/testing/replicator.ts +0 -184
- /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
- /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
- /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
- /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
- /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
- /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
- /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
package/src/extensions/code.ts
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Range } from '@codemirror/state';
|
|
6
|
-
import {
|
|
7
|
-
ViewPlugin,
|
|
8
|
-
type DecorationSet,
|
|
9
|
-
EditorView,
|
|
10
|
-
type ViewUpdate,
|
|
11
|
-
Decoration,
|
|
12
|
-
type BlockInfo,
|
|
13
|
-
} from '@codemirror/view';
|
|
14
|
-
|
|
15
|
-
import { mx } from '@dxos/react-ui-theme';
|
|
16
|
-
|
|
17
|
-
import { getToken } from '../styles';
|
|
18
|
-
|
|
19
|
-
const CODE_REGEX = /```[\s\S]*?```/gs;
|
|
20
|
-
|
|
21
|
-
// TODO(burdon): Reconcile with theme.
|
|
22
|
-
const styles = EditorView.baseTheme({
|
|
23
|
-
'& .cm-codeblock': {
|
|
24
|
-
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
25
|
-
},
|
|
26
|
-
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
27
|
-
background: getToken('extend.colors.neutral.50'),
|
|
28
|
-
},
|
|
29
|
-
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
30
|
-
background: getToken('extend.colors.neutral.850'),
|
|
31
|
-
},
|
|
32
|
-
'& [aria-readonly="true"] .cm-codeblock': {
|
|
33
|
-
display: 'block',
|
|
34
|
-
paddingInline: '4px !important',
|
|
35
|
-
},
|
|
36
|
-
'& [aria-readonly="true"] .cm-codeblock-first': {
|
|
37
|
-
paddingTop: '4px !important',
|
|
38
|
-
borderTopLeftRadius: '4px',
|
|
39
|
-
borderTopRightRadius: '4px',
|
|
40
|
-
},
|
|
41
|
-
'& [aria-readonly="true"] .cm-codeblock-last': {
|
|
42
|
-
paddingBottom: '4px !important',
|
|
43
|
-
borderBottomLeftRadius: '4px',
|
|
44
|
-
borderBottomRightRadius: '4px',
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// TODO(burdon): Start from EditorView.lineBlockAt(pos).
|
|
49
|
-
const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
50
|
-
const start = lines.findIndex((line) => line.from >= from);
|
|
51
|
-
const end = lines.findIndex((line) => line.to >= to);
|
|
52
|
-
return [start, end];
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export const code = () => {
|
|
56
|
-
const buildDecorations = (view: EditorView) => {
|
|
57
|
-
const decorations: Range<Decoration>[] = [];
|
|
58
|
-
const text = view.state.doc.sliceString(0);
|
|
59
|
-
const matches = text.matchAll(CODE_REGEX);
|
|
60
|
-
const blocks = view.viewportLineBlocks;
|
|
61
|
-
for (const match of matches) {
|
|
62
|
-
const range = getLineRange(blocks, match.index!, match.index! + match[0].length);
|
|
63
|
-
for (let i = range[0]; i <= range[1]; i++) {
|
|
64
|
-
const block = blocks[i];
|
|
65
|
-
decorations.push(
|
|
66
|
-
Decoration.line({
|
|
67
|
-
class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
|
|
68
|
-
}).range(block.from),
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return Decoration.set(decorations);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
return [
|
|
77
|
-
ViewPlugin.fromClass(
|
|
78
|
-
class {
|
|
79
|
-
hasFocus = false;
|
|
80
|
-
decorations: DecorationSet;
|
|
81
|
-
constructor(view: EditorView) {
|
|
82
|
-
this.decorations = buildDecorations(view);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
update(update: ViewUpdate) {
|
|
86
|
-
if (
|
|
87
|
-
// TODO(burdon): Generalize for other extensions.
|
|
88
|
-
this.hasFocus !== update.view.hasFocus ||
|
|
89
|
-
update.startState.readOnly !== update.view.state.readOnly ||
|
|
90
|
-
update.docChanged ||
|
|
91
|
-
update.viewportChanged
|
|
92
|
-
) {
|
|
93
|
-
this.hasFocus = update.view.hasFocus;
|
|
94
|
-
this.decorations = buildDecorations(update.view);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
decorations: (value) => value.decorations,
|
|
100
|
-
},
|
|
101
|
-
),
|
|
102
|
-
styles,
|
|
103
|
-
];
|
|
104
|
-
};
|
package/src/extensions/hr.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2023 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
Decoration,
|
|
7
|
-
type DecorationSet,
|
|
8
|
-
EditorView,
|
|
9
|
-
MatchDecorator,
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type ViewUpdate,
|
|
12
|
-
WidgetType,
|
|
13
|
-
} from '@codemirror/view';
|
|
14
|
-
|
|
15
|
-
import { getToken } from '../styles';
|
|
16
|
-
|
|
17
|
-
// TODO(burdon): Reconcile with theme.
|
|
18
|
-
const styles = EditorView.baseTheme({
|
|
19
|
-
'& .cm-hr': {
|
|
20
|
-
// TODO(burdon): ???
|
|
21
|
-
// Note that block-level decorations should not have vertical margins,
|
|
22
|
-
borderBottom: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
class HorizontalRuleWidget extends WidgetType {
|
|
27
|
-
constructor(readonly _pos: number) {
|
|
28
|
-
super();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
override eq(other: this) {
|
|
32
|
-
return this._pos === (other as any as HorizontalRuleWidget)._pos;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
override toDOM(view: EditorView) {
|
|
36
|
-
const el = document.createElement('div');
|
|
37
|
-
el.className = 'cm-hr';
|
|
38
|
-
return el;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// TODO(burdon): Like Tasklist, allow cursor to move into range.
|
|
43
|
-
|
|
44
|
-
// NOTE: Without a blank line before this markup will treat the previous line as a heading.
|
|
45
|
-
// https://www.markdownguide.org/basic-syntax/#horizontal-rules
|
|
46
|
-
const placeholderMatcher = new MatchDecorator({
|
|
47
|
-
// regexp: /(?<=\n\n)---/gs,
|
|
48
|
-
regexp: /^---$/gs,
|
|
49
|
-
decoration: (match, view, pos) =>
|
|
50
|
-
Decoration.replace({
|
|
51
|
-
widget: new HorizontalRuleWidget(pos),
|
|
52
|
-
}),
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
export const hr = () => [
|
|
56
|
-
styles,
|
|
57
|
-
ViewPlugin.fromClass(
|
|
58
|
-
class {
|
|
59
|
-
rules: DecorationSet;
|
|
60
|
-
constructor(view: EditorView) {
|
|
61
|
-
this.rules = placeholderMatcher.createDeco(view);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
update(update: ViewUpdate) {
|
|
65
|
-
this.rules = placeholderMatcher.updateDeco(update, this.rules);
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
decorations: (instance) => instance.rules,
|
|
70
|
-
},
|
|
71
|
-
),
|
|
72
|
-
];
|
|
@@ -1,22 +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 '../cursor';
|
|
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
|
-
|
|
16
|
-
fromCursor: (cursor) => {
|
|
17
|
-
return (
|
|
18
|
-
Y.createAbsolutePositionFromRelativePosition(Y.decodeRelativePosition(stringToArray(cursor)), text.doc!)?.index ??
|
|
19
|
-
0
|
|
20
|
-
);
|
|
21
|
-
},
|
|
22
|
-
});
|
|
@@ -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.skip('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 { Cursor } from '../cursor';
|
|
12
|
-
|
|
13
|
-
export const yjs = (content: YText, awareness?: YP.Awareness) => [
|
|
14
|
-
Cursor.converter.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,156 +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 { type Doc } from 'yjs';
|
|
10
|
-
|
|
11
|
-
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { type Space } from '@dxos/react-client/echo';
|
|
14
|
-
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
15
|
-
import type { YText } from '@dxos/text-model';
|
|
16
|
-
|
|
17
|
-
import { type EditorModel, modelState, type UseTextModelProps } from './useTextModel';
|
|
18
|
-
import { yjs } from '../extensions';
|
|
19
|
-
|
|
20
|
-
export const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
|
|
21
|
-
invariant(text?.doc && text?.content);
|
|
22
|
-
const provider = space
|
|
23
|
-
? new YAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` })
|
|
24
|
-
: undefined;
|
|
25
|
-
|
|
26
|
-
const model: EditorModel = {
|
|
27
|
-
id: text.doc.guid,
|
|
28
|
-
content: text.content,
|
|
29
|
-
text: () => text.content!.toString(),
|
|
30
|
-
extension: [modelState.init(() => model), yjs(text.content as YText, provider?.awareness)],
|
|
31
|
-
awareness: provider?.awareness,
|
|
32
|
-
peer: identity
|
|
33
|
-
? {
|
|
34
|
-
id: identity.identityKey.toHex(),
|
|
35
|
-
name: identity.profile?.displayName,
|
|
36
|
-
}
|
|
37
|
-
: undefined,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return model;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Based on https://github.com/yjs/y-webrtc/blob/88baab2/src/y-webrtc.js.
|
|
44
|
-
|
|
45
|
-
const messageAwareness = 1;
|
|
46
|
-
const messageQueryAwareness = 3;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Yjs awareness provider on top of a DXOS space.
|
|
50
|
-
*/
|
|
51
|
-
export class YAwarenessProvider extends Observable<any> {
|
|
52
|
-
private readonly _space: Space;
|
|
53
|
-
private readonly _awareness: YP.Awareness;
|
|
54
|
-
private readonly _clientId: number;
|
|
55
|
-
private readonly _channel: string;
|
|
56
|
-
|
|
57
|
-
constructor({
|
|
58
|
-
space,
|
|
59
|
-
doc,
|
|
60
|
-
channel,
|
|
61
|
-
awareness,
|
|
62
|
-
}: {
|
|
63
|
-
space: Space;
|
|
64
|
-
doc: Doc;
|
|
65
|
-
channel: string;
|
|
66
|
-
awareness?: YP.Awareness;
|
|
67
|
-
}) {
|
|
68
|
-
super();
|
|
69
|
-
this._space = space;
|
|
70
|
-
this._awareness = awareness ?? new YP.Awareness(doc);
|
|
71
|
-
this._channel = channel;
|
|
72
|
-
this._clientId = doc.clientID;
|
|
73
|
-
|
|
74
|
-
this._awareness.on('update', this._handleAwarenessUpdate.bind(this));
|
|
75
|
-
this._space.listen(this._channel, this._handleSpaceMessage.bind(this));
|
|
76
|
-
|
|
77
|
-
if (typeof window !== 'undefined') {
|
|
78
|
-
window.addEventListener('beforeunload', this._handleBeforeUnload.bind(this));
|
|
79
|
-
} else if (typeof process !== 'undefined') {
|
|
80
|
-
process.on('exit', this._handleBeforeUnload.bind(this));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Post queryAwareness.
|
|
84
|
-
const encoderAwarenessQuery = encoding.createEncoder();
|
|
85
|
-
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness);
|
|
86
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessQuery));
|
|
87
|
-
|
|
88
|
-
// Post local awareness state.
|
|
89
|
-
const encoderAwarenessState = encoding.createEncoder();
|
|
90
|
-
encoding.writeVarUint(encoderAwarenessState, messageAwareness);
|
|
91
|
-
encoding.writeVarUint8Array(encoderAwarenessState, YP.encodeAwarenessUpdate(this.awareness, [this._clientId]));
|
|
92
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwarenessState));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
get awareness(): YP.Awareness {
|
|
96
|
-
return this._awareness;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private _handleAwarenessUpdate({ added, updated, removed }: any, origin: any) {
|
|
100
|
-
log('awareness update', { added, updated, removed, origin });
|
|
101
|
-
const changedClients = added.concat(updated).concat(removed);
|
|
102
|
-
const encoderAwareness = encoding.createEncoder();
|
|
103
|
-
encoding.writeVarUint(encoderAwareness, messageAwareness);
|
|
104
|
-
encoding.writeVarUint8Array(encoderAwareness, YP.encodeAwarenessUpdate(this._awareness, changedClients));
|
|
105
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoderAwareness));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
private _handleSpaceMessage({ payload }: GossipMessage) {
|
|
109
|
-
log('space message', payload);
|
|
110
|
-
const data = new Uint8Array(Array.from(Object.values(payload)));
|
|
111
|
-
const encoder = this._readMessage(data);
|
|
112
|
-
if (encoder) {
|
|
113
|
-
void this._space.postMessage(this._channel, encoding.toUint8Array(encoder));
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private _readMessage(message: Uint8Array) {
|
|
118
|
-
const decoder = decoding.createDecoder(message);
|
|
119
|
-
const encoder = encoding.createEncoder();
|
|
120
|
-
const messageType = decoding.readVarUint(decoder);
|
|
121
|
-
|
|
122
|
-
let sendReply = false;
|
|
123
|
-
switch (messageType) {
|
|
124
|
-
case messageAwareness: {
|
|
125
|
-
YP.applyAwarenessUpdate(this._awareness, decoding.readVarUint8Array(decoder), this);
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
case messageQueryAwareness: {
|
|
130
|
-
encoding.writeVarUint(encoder, messageAwareness);
|
|
131
|
-
encoding.writeVarUint8Array(
|
|
132
|
-
encoder,
|
|
133
|
-
YP.encodeAwarenessUpdate(this._awareness, Array.from(this._awareness.getStates().keys())),
|
|
134
|
-
);
|
|
135
|
-
sendReply = true;
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
default: {
|
|
140
|
-
console.error('Invalid message:', messageType);
|
|
141
|
-
return encoder;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!sendReply) {
|
|
146
|
-
// Nothing has been written, no answer created.
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return encoder;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
private _handleBeforeUnload() {
|
|
154
|
-
YP.removeAwarenessStates(this._awareness, [this._clientId], 'window unload');
|
|
155
|
-
}
|
|
156
|
-
}
|
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();
|