@dxos/react-ui-editor 0.3.11-main.a996413 → 0.3.11-main.b00882f
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 +2010 -1505
- 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 -3
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/{hooks/automerge → components/TextEditor}/automerge.stories.d.ts +6 -1
- package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -0
- package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -1
- package/dist/types/src/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/{hooks/yjs → components/TextEditor}/yjs.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/automerge.d.ts +9 -0
- package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts +5 -0
- package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/defs.d.ts +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 +14 -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 +48 -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/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/extensions/cursor.d.ts +25 -0
- 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 +5 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/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/extensions/yjs/cursor.d.ts +4 -0
- package/dist/types/src/extensions/yjs/cursor.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/index.d.ts +2 -0
- package/dist/types/src/extensions/yjs/index.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts +4 -0
- package/dist/types/src/extensions/yjs/yjs.d.ts.map +1 -0
- package/dist/types/src/extensions/yjs/yjs.test.d.ts.map +1 -0
- package/dist/types/src/hooks/automerge.d.ts +4 -0
- package/dist/types/src/hooks/automerge.d.ts.map +1 -0
- package/dist/types/src/hooks/awareness-provider.d.ts +28 -0
- package/dist/types/src/hooks/awareness-provider.d.ts.map +1 -0
- package/dist/types/src/hooks/defs.d.ts +28 -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/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 -31
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +9 -8
- package/dist/types/src/hooks/yjs.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/cursors.d.ts.map +1 -0
- package/dist/types/src/styles/index.d.ts +1 -0
- package/dist/types/src/styles/index.d.ts.map +1 -1
- package/dist/types/src/styles/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/testing/replicator.d.ts +2 -2
- package/dist/types/src/testing/replicator.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +7 -0
- package/dist/types/src/util.d.ts.map +1 -0
- package/package.json +32 -30
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +77 -80
- package/src/components/TextEditor/TextEditor.stories.tsx +13 -34
- package/src/components/TextEditor/TextEditor.tsx +125 -93
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/comments.stories.tsx +357 -0
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/extensions/autocomplete.ts +23 -19
- package/src/extensions/automerge/automerge.ts +88 -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/{hooks → extensions}/automerge/semaphore.ts +34 -29
- package/src/{hooks → extensions}/automerge/update-automerge.ts +5 -6
- package/src/{hooks → extensions}/automerge/update-codemirror.ts +13 -5
- package/src/extensions/awareness.ts +327 -0
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/code.ts +28 -23
- package/src/extensions/comments.ts +252 -229
- 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 +5 -1
- 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/extensions/yjs/cursor.ts +22 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +2 -26
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +53 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/defs.ts +47 -0
- package/src/hooks/index.ts +6 -1
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +9 -129
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +50 -19
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/markdown.ts +0 -2
- package/src/styles/tokens.ts +3 -0
- package/src/testing/replicator.ts +10 -15
- package/src/themes/default.ts +51 -78
- package/src/util.ts +41 -0
- package/dist/types/src/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/extensions/util.d.ts +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.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +0 -1
- package/dist/types/src/hooks/yjs/yjs.test.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -11
- package/src/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/yjs/index.ts +0 -6
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/yjs/yjs.test.d.ts +0 -0
- /package/dist/types/src/{hooks/yjs → styles}/cursors.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
package/src/extensions/table.ts
CHANGED
|
@@ -81,7 +81,6 @@ const update = (state: EditorState, options: TableOptions) => {
|
|
|
81
81
|
table.from,
|
|
82
82
|
table.to,
|
|
83
83
|
Decoration.replace({
|
|
84
|
-
block: true,
|
|
85
84
|
widget: new TableWidget(table),
|
|
86
85
|
}),
|
|
87
86
|
);
|
|
@@ -98,11 +97,14 @@ class TableWidget extends WidgetType {
|
|
|
98
97
|
super();
|
|
99
98
|
}
|
|
100
99
|
|
|
101
|
-
override eq(other:
|
|
102
|
-
return
|
|
100
|
+
override eq(other: this) {
|
|
101
|
+
return (
|
|
102
|
+
this._table.header?.join() === other._table.header?.join() &&
|
|
103
|
+
this._table.rows?.join() === other._table.rows?.join()
|
|
104
|
+
);
|
|
103
105
|
}
|
|
104
106
|
|
|
105
|
-
toDOM(view: EditorView) {
|
|
107
|
+
override toDOM(view: EditorView) {
|
|
106
108
|
const table = document.createElement('table');
|
|
107
109
|
|
|
108
110
|
{
|
|
@@ -129,4 +131,8 @@ class TableWidget extends WidgetType {
|
|
|
129
131
|
|
|
130
132
|
return table;
|
|
131
133
|
}
|
|
134
|
+
|
|
135
|
+
override ignoreEvent(e: Event) {
|
|
136
|
+
return !/^mouse/.test(e.type);
|
|
137
|
+
}
|
|
132
138
|
}
|
|
@@ -2,58 +2,53 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
ViewPlugin,
|
|
11
|
-
type DecorationSet,
|
|
12
|
-
type ViewUpdate,
|
|
13
|
-
} from '@codemirror/view';
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { RangeSetBuilder } from '@codemirror/state';
|
|
7
|
+
import { EditorView, Decoration, WidgetType, ViewPlugin, type DecorationSet, type ViewUpdate } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
import { getToken } from '../styles';
|
|
14
10
|
|
|
15
11
|
// TODO(burdon): Reconcile with theme.
|
|
16
12
|
const styles = EditorView.baseTheme({
|
|
17
|
-
'& .cm-task
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
'& .cm-task': {
|
|
14
|
+
color: getToken('extend.colors.blue.500'),
|
|
15
|
+
},
|
|
16
|
+
'& .cm-task-checkbox': {
|
|
17
|
+
marginLeft: '4px',
|
|
18
|
+
marginRight: '4px',
|
|
20
19
|
},
|
|
21
20
|
});
|
|
22
21
|
|
|
23
22
|
class CheckboxWidget extends WidgetType {
|
|
24
|
-
constructor(
|
|
25
|
-
private readonly _pos: number,
|
|
26
|
-
private _checked: boolean,
|
|
27
|
-
private readonly _indent: number,
|
|
28
|
-
private readonly _onCheck?: (check: boolean) => void,
|
|
29
|
-
) {
|
|
23
|
+
constructor(private _checked: boolean) {
|
|
30
24
|
super();
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
override eq(other:
|
|
34
|
-
return this.
|
|
27
|
+
override eq(other: this) {
|
|
28
|
+
return this._checked === other._checked;
|
|
35
29
|
}
|
|
36
30
|
|
|
37
|
-
toDOM(view: EditorView) {
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
wrap.setAttribute('aria-hidden', 'true');
|
|
41
|
-
wrap.style.setProperty('margin-left', this._indent * 24 + 'px');
|
|
42
|
-
|
|
43
|
-
const input = wrap.appendChild(document.createElement('input'));
|
|
31
|
+
override toDOM(view: EditorView) {
|
|
32
|
+
const input = document.createElement('input');
|
|
33
|
+
input.className = 'cm-task-checkbox';
|
|
44
34
|
input.type = 'checkbox';
|
|
45
35
|
input.checked = this._checked;
|
|
46
|
-
if (
|
|
36
|
+
if (view.state.readOnly) {
|
|
47
37
|
input.setAttribute('disabled', 'true');
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
38
|
+
} else {
|
|
39
|
+
input.onmousedown = (event: Event) => {
|
|
40
|
+
const pos = view.posAtDOM(input);
|
|
41
|
+
const text = view.state.sliceDoc(pos, pos + 3);
|
|
42
|
+
if (text === (this._checked ? '[x]' : '[ ]')) {
|
|
43
|
+
view.dispatch({
|
|
44
|
+
changes: { from: pos + 1, to: pos + 2, insert: this._checked ? ' ' : 'x' },
|
|
45
|
+
});
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
}
|
|
53
48
|
};
|
|
54
49
|
}
|
|
55
50
|
|
|
56
|
-
return
|
|
51
|
+
return input;
|
|
57
52
|
}
|
|
58
53
|
|
|
59
54
|
override ignoreEvent() {
|
|
@@ -61,61 +56,56 @@ class CheckboxWidget extends WidgetType {
|
|
|
61
56
|
}
|
|
62
57
|
}
|
|
63
58
|
|
|
59
|
+
const checkedDecoration = Decoration.replace({ widget: new CheckboxWidget(true) });
|
|
60
|
+
const uncheckedDecoration = Decoration.replace({ widget: new CheckboxWidget(false) });
|
|
61
|
+
|
|
62
|
+
const buildDecorations = (view: EditorView): DecorationSet => {
|
|
63
|
+
const builder = new RangeSetBuilder<Decoration>();
|
|
64
|
+
const { state } = view;
|
|
65
|
+
const cursor = state.selection.main.head;
|
|
66
|
+
|
|
67
|
+
for (const { from, to } of view.visibleRanges) {
|
|
68
|
+
syntaxTree(state).iterate({
|
|
69
|
+
enter: (node) => {
|
|
70
|
+
if (node.name === 'TaskMarker') {
|
|
71
|
+
// Check if cursor is inside text.
|
|
72
|
+
if (cursor < node.from || cursor > node.to) {
|
|
73
|
+
const checked = state.doc.sliceString(node.from + 1, node.to - 1) === 'x';
|
|
74
|
+
builder.add(node.from - 2, node.from - 1, Decoration.mark({ class: 'cm-task' }));
|
|
75
|
+
builder.add(node.from, node.to, checked ? checkedDecoration : uncheckedDecoration);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
from,
|
|
80
|
+
to,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return builder.finish();
|
|
85
|
+
};
|
|
86
|
+
|
|
64
87
|
export type TasklistOptions = {};
|
|
65
88
|
|
|
66
89
|
export const tasklist = (options: TasklistOptions = {}) => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
},
|
|
89
|
-
// TODO(burdon): Restore cursor position? More useful to move to end of line (can indent).
|
|
90
|
-
selection: {
|
|
91
|
-
anchor: idx + 3,
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
),
|
|
96
|
-
});
|
|
97
|
-
},
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const tasks = ViewPlugin.fromClass(
|
|
101
|
-
class {
|
|
102
|
-
tasks: DecorationSet;
|
|
103
|
-
constructor(view: EditorView) {
|
|
104
|
-
this.tasks = taskMatcher.createDeco(view);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
update(update: ViewUpdate) {
|
|
108
|
-
this.tasks = taskMatcher.updateDeco(update, this.tasks);
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
decorations: (value) => value.tasks,
|
|
113
|
-
provide: (plugin) =>
|
|
114
|
-
EditorView.atomicRanges.of((view) => {
|
|
115
|
-
return view.plugin(plugin)?.tasks || Decoration.none;
|
|
116
|
-
}),
|
|
117
|
-
},
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
return [tasks, styles];
|
|
90
|
+
return [
|
|
91
|
+
ViewPlugin.fromClass(
|
|
92
|
+
class {
|
|
93
|
+
decorations: DecorationSet;
|
|
94
|
+
|
|
95
|
+
constructor(view: EditorView) {
|
|
96
|
+
this.decorations = buildDecorations(view);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
update(update: ViewUpdate) {
|
|
100
|
+
if (update.docChanged || update.viewportChanged || update.selectionSet) {
|
|
101
|
+
this.decorations = buildDecorations(update.view);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
decorations: (v) => v.decorations,
|
|
107
|
+
},
|
|
108
|
+
),
|
|
109
|
+
styles,
|
|
110
|
+
];
|
|
121
111
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
});
|
|
@@ -2,20 +2,17 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { effect } from '@preact/signals-react';
|
|
6
5
|
import { expect } from 'chai';
|
|
7
6
|
import * as Y from 'yjs';
|
|
8
7
|
|
|
9
|
-
import {
|
|
10
|
-
import { Expando, setGlobalAutomergePreference, TextObject } from '@dxos/echo-schema';
|
|
11
|
-
import { registerSignalFactory } from '@dxos/echo-signals';
|
|
8
|
+
import { TextObject } from '@dxos/echo-schema';
|
|
12
9
|
import { describe, test } from '@dxos/test';
|
|
13
10
|
import { type YText } from '@dxos/text-model';
|
|
14
11
|
|
|
15
12
|
describe('YJS', () => {
|
|
16
13
|
// https://docs.yjs.dev/api/shared-types/y.text
|
|
17
14
|
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
18
|
-
test('absolute position', () => {
|
|
15
|
+
test.skip('absolute position', () => {
|
|
19
16
|
const obj = new TextObject('hello world');
|
|
20
17
|
const index = obj.text.indexOf('world');
|
|
21
18
|
const relPos = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(obj.content as YText, index));
|
|
@@ -35,25 +32,4 @@ describe('YJS', () => {
|
|
|
35
32
|
expect(obj.text.substring(cursor!.index)).to.equal('world');
|
|
36
33
|
}
|
|
37
34
|
});
|
|
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
35
|
});
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
];
|
|
@@ -0,0 +1,53 @@
|
|
|
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 } from './defs';
|
|
12
|
+
import { type UseTextModelProps } from './useTextModel';
|
|
13
|
+
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
14
|
+
import { cursorColor } from '../styles';
|
|
15
|
+
|
|
16
|
+
export const createAutomergeModel = ({ space, identity, text }: UseTextModelProps): EditorModel => {
|
|
17
|
+
const obj = text as any as AutomergeTextCompat;
|
|
18
|
+
const doc = getRawDoc(obj, [obj.field]);
|
|
19
|
+
|
|
20
|
+
const awarenessProvider =
|
|
21
|
+
space &&
|
|
22
|
+
new SpaceAwarenessProvider({
|
|
23
|
+
space,
|
|
24
|
+
channel: `automerge.awareness.${obj.id}`,
|
|
25
|
+
info: {
|
|
26
|
+
displayName: identity?.profile?.displayName ?? '',
|
|
27
|
+
color: cursorColor.color,
|
|
28
|
+
lightColor: cursorColor.light,
|
|
29
|
+
},
|
|
30
|
+
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const model: EditorModel = {
|
|
34
|
+
id: obj.id,
|
|
35
|
+
content: doc,
|
|
36
|
+
text: () => get(doc.handle.docSync(), doc.path),
|
|
37
|
+
// TODO(burdon): https://automerge.org/automerge/api-docs/js/functions/next.getCursor.html
|
|
38
|
+
extension: [
|
|
39
|
+
modelState.init(() => model),
|
|
40
|
+
automerge({ handle: doc.handle, path: doc.path }),
|
|
41
|
+
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
42
|
+
awareness(),
|
|
43
|
+
].filter(isNotNullOrUndefined),
|
|
44
|
+
peer: identity
|
|
45
|
+
? {
|
|
46
|
+
id: identity.identityKey.toHex(),
|
|
47
|
+
name: identity.profile?.displayName,
|
|
48
|
+
}
|
|
49
|
+
: undefined,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return model;
|
|
53
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { DeferredTask, Event, sleep } from '@dxos/async';
|
|
6
|
+
import { Context } from '@dxos/context';
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
import { type Space } from '@dxos/react-client/echo';
|
|
10
|
+
import { type GossipMessage } from '@dxos/react-client/mesh';
|
|
11
|
+
|
|
12
|
+
import { type AwarenessInfo, type AwarenessPosition, type AwarenessProvider, type AwarenessState } from '../extensions';
|
|
13
|
+
|
|
14
|
+
type ProtocolMessage =
|
|
15
|
+
| {
|
|
16
|
+
kind: 'query';
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
kind: 'post';
|
|
20
|
+
state: AwarenessState;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const DEBOUNCE_INTERVAL = 100; // ms
|
|
24
|
+
|
|
25
|
+
export type AwarenessProviderParams = {
|
|
26
|
+
space: Space;
|
|
27
|
+
channel: string;
|
|
28
|
+
peerId: string;
|
|
29
|
+
info: AwarenessInfo;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export class SpaceAwarenessProvider implements AwarenessProvider {
|
|
33
|
+
public readonly remoteStateChange = new Event<void>();
|
|
34
|
+
|
|
35
|
+
private readonly _remoteStates = new Map<string, AwarenessState>();
|
|
36
|
+
|
|
37
|
+
private readonly _space: Space;
|
|
38
|
+
private readonly _channel: string;
|
|
39
|
+
private readonly _peerId: string;
|
|
40
|
+
private readonly _info: AwarenessInfo;
|
|
41
|
+
|
|
42
|
+
private _localState?: AwarenessState;
|
|
43
|
+
private _postTask?: DeferredTask;
|
|
44
|
+
private _ctx?: Context;
|
|
45
|
+
|
|
46
|
+
constructor(params: AwarenessProviderParams) {
|
|
47
|
+
this._space = params.space;
|
|
48
|
+
this._channel = params.channel;
|
|
49
|
+
this._peerId = params.peerId;
|
|
50
|
+
this._info = params.info;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
open() {
|
|
54
|
+
this._ctx = new Context();
|
|
55
|
+
this._postTask = new DeferredTask(this._ctx, async () => {
|
|
56
|
+
if (this._localState) {
|
|
57
|
+
await this._space.postMessage(this._channel, {
|
|
58
|
+
kind: 'post',
|
|
59
|
+
state: this._localState,
|
|
60
|
+
} satisfies ProtocolMessage);
|
|
61
|
+
|
|
62
|
+
await sleep(DEBOUNCE_INTERVAL);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const unsubscribe = this._space.listen(this._channel, (message: GossipMessage) => {
|
|
67
|
+
switch (message.payload.kind) {
|
|
68
|
+
case 'query': {
|
|
69
|
+
this._handleQueryMessage();
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
case 'post': {
|
|
73
|
+
this._handlePostMessage(message.payload);
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
this._ctx.onDispose(unsubscribe);
|
|
79
|
+
|
|
80
|
+
void this._space
|
|
81
|
+
.postMessage(this._channel, {
|
|
82
|
+
kind: 'query',
|
|
83
|
+
} satisfies ProtocolMessage)
|
|
84
|
+
.catch((err) => {
|
|
85
|
+
log.debug('failed to query awareness', { err });
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
close() {
|
|
90
|
+
void this._ctx?.dispose();
|
|
91
|
+
this._ctx = undefined;
|
|
92
|
+
this._postTask = undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
update(position: AwarenessPosition | undefined): void {
|
|
96
|
+
invariant(this._postTask);
|
|
97
|
+
this._localState = {
|
|
98
|
+
peerId: this._peerId,
|
|
99
|
+
position,
|
|
100
|
+
info: this._info,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
this._postTask.schedule();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getRemoteStates(): AwarenessState[] {
|
|
107
|
+
return Array.from(this._remoteStates.values());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private _handleQueryMessage() {
|
|
111
|
+
invariant(this._postTask);
|
|
112
|
+
this._postTask.schedule();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private _handlePostMessage(message: ProtocolMessage) {
|
|
116
|
+
invariant(message.kind === 'post');
|
|
117
|
+
this._remoteStates.set(message.state.peerId, message.state);
|
|
118
|
+
this.remoteStateChange.emit();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension, StateField } from '@codemirror/state';
|
|
6
|
+
import type * as YP from 'y-protocols/awareness';
|
|
7
|
+
|
|
8
|
+
import { type DocAccessor } from '@dxos/echo-schema';
|
|
9
|
+
import type { YText, YXmlFragment } from '@dxos/text-model';
|
|
10
|
+
|
|
11
|
+
// Runtime data structure.
|
|
12
|
+
export type Range = {
|
|
13
|
+
from: number;
|
|
14
|
+
to: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Persistent data structure.
|
|
18
|
+
export type Comment = {
|
|
19
|
+
id: string;
|
|
20
|
+
cursor?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type EditorModel = {
|
|
24
|
+
id: string;
|
|
25
|
+
text: () => string;
|
|
26
|
+
extension?: Extension;
|
|
27
|
+
|
|
28
|
+
// TODO(burdon): Remove.
|
|
29
|
+
content: string | YText | YXmlFragment | DocAccessor;
|
|
30
|
+
|
|
31
|
+
// TODO(burdon): Move into extension.
|
|
32
|
+
awareness?: YP.Awareness;
|
|
33
|
+
|
|
34
|
+
// TODO(burdon): Remove.
|
|
35
|
+
peer?: {
|
|
36
|
+
id: string;
|
|
37
|
+
name?: string;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* State field makes the model available to other extensions.
|
|
43
|
+
*/
|
|
44
|
+
export const modelState = StateField.define<EditorModel | undefined>({
|
|
45
|
+
create: () => undefined,
|
|
46
|
+
update: (model) => model,
|
|
47
|
+
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -2,5 +2,10 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './automerge';
|
|
6
|
+
export * from './awareness-provider';
|
|
7
|
+
export * from './defs';
|
|
6
8
|
export * from './yjs';
|
|
9
|
+
|
|
10
|
+
export * from './useTextEditor';
|
|
11
|
+
export * from './useTextModel';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type EditorView } from '@codemirror/view';
|
|
6
|
+
import { type RefCallback, useState } from 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Hook for accessing the editor view.
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const Test = () => {
|
|
13
|
+
* const [editorRef, editorView] = useTextEditor();
|
|
14
|
+
* useEffect(() => {
|
|
15
|
+
* editorView?.focus();
|
|
16
|
+
* }, [editorView]);
|
|
17
|
+
* return <TextEditor ref={editorRef} />;
|
|
18
|
+
* };
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export const useTextEditor = (): [RefCallback<EditorView | null>, EditorView | null] => {
|
|
22
|
+
const [view, setView] = useState<EditorView | null>(null);
|
|
23
|
+
return [
|
|
24
|
+
(ref: EditorView | null) => {
|
|
25
|
+
setView(ref);
|
|
26
|
+
},
|
|
27
|
+
view,
|
|
28
|
+
];
|
|
29
|
+
};
|