@dxos/react-ui-editor 0.3.11-main.0e40963 → 0.3.11-main.108b22e
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 +1837 -1502
- 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/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 +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 +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/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +33 -18
- 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 +4 -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/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 +3 -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/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/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 -34
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +77 -80
- package/src/components/TextEditor/TextEditor.stories.tsx +13 -34
- package/src/components/TextEditor/TextEditor.tsx +122 -93
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/comments.stories.tsx +357 -0
- 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 +310 -0
- package/src/extensions/basic.ts +22 -17
- package/src/extensions/code.ts +28 -23
- package/src/extensions/comments.ts +256 -219
- 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 +4 -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/hooks/awareness-provider.ts +124 -0
- package/src/hooks/defs.ts +42 -0
- package/src/hooks/index.ts +4 -1
- package/src/hooks/useTextEditor.ts +29 -0
- package/src/hooks/useTextModel.ts +30 -116
- 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/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/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 +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/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 -134
- 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/{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,124 @@
|
|
|
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
|
+
/**
|
|
33
|
+
* Receives and broadcasts profile and cursor position.
|
|
34
|
+
*/
|
|
35
|
+
export class SpaceAwarenessProvider implements AwarenessProvider {
|
|
36
|
+
private readonly _remoteStates = new Map<string, AwarenessState>();
|
|
37
|
+
|
|
38
|
+
private readonly _space: Space;
|
|
39
|
+
private readonly _channel: string;
|
|
40
|
+
private readonly _peerId: string;
|
|
41
|
+
private readonly _info: AwarenessInfo;
|
|
42
|
+
|
|
43
|
+
private _ctx?: Context;
|
|
44
|
+
private _postTask?: DeferredTask;
|
|
45
|
+
private _localState?: AwarenessState;
|
|
46
|
+
|
|
47
|
+
public readonly remoteStateChange = new Event<void>();
|
|
48
|
+
|
|
49
|
+
constructor(params: AwarenessProviderParams) {
|
|
50
|
+
this._space = params.space;
|
|
51
|
+
this._channel = params.channel;
|
|
52
|
+
this._peerId = params.peerId;
|
|
53
|
+
this._info = params.info;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
open() {
|
|
57
|
+
this._ctx = new Context();
|
|
58
|
+
this._postTask = new DeferredTask(this._ctx, async () => {
|
|
59
|
+
if (this._localState) {
|
|
60
|
+
await this._space.postMessage(this._channel, {
|
|
61
|
+
kind: 'post',
|
|
62
|
+
state: this._localState,
|
|
63
|
+
} satisfies ProtocolMessage);
|
|
64
|
+
|
|
65
|
+
await sleep(DEBOUNCE_INTERVAL);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
this._ctx.onDispose(
|
|
70
|
+
this._space.listen(this._channel, (message: GossipMessage) => {
|
|
71
|
+
switch (message.payload.kind) {
|
|
72
|
+
case 'query': {
|
|
73
|
+
this._handleQueryMessage();
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
case 'post': {
|
|
77
|
+
this._handlePostMessage(message.payload);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
void this._space
|
|
85
|
+
.postMessage(this._channel, {
|
|
86
|
+
kind: 'query',
|
|
87
|
+
} satisfies ProtocolMessage)
|
|
88
|
+
.catch((err) => {
|
|
89
|
+
log.debug('failed to query awareness', { err });
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
close() {
|
|
94
|
+
void this._ctx?.dispose();
|
|
95
|
+
this._ctx = undefined;
|
|
96
|
+
this._postTask = undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
getRemoteStates(): AwarenessState[] {
|
|
100
|
+
return Array.from(this._remoteStates.values());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
update(position: AwarenessPosition | undefined): void {
|
|
104
|
+
invariant(this._postTask);
|
|
105
|
+
this._localState = {
|
|
106
|
+
peerId: this._peerId,
|
|
107
|
+
position,
|
|
108
|
+
info: this._info,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
this._postTask.schedule();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private _handleQueryMessage() {
|
|
115
|
+
invariant(this._postTask);
|
|
116
|
+
this._postTask.schedule();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private _handlePostMessage(message: ProtocolMessage) {
|
|
120
|
+
invariant(message.kind === 'post');
|
|
121
|
+
this._remoteStates.set(message.state.peerId, message.state);
|
|
122
|
+
this.remoteStateChange.emit();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension, StateField } from '@codemirror/state';
|
|
6
|
+
|
|
7
|
+
import { type DocAccessor } from '@dxos/echo-schema';
|
|
8
|
+
|
|
9
|
+
// Runtime data structure.
|
|
10
|
+
export type Range = {
|
|
11
|
+
from: number;
|
|
12
|
+
to: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// Persistent data structure.
|
|
16
|
+
export type Comment = {
|
|
17
|
+
id: string;
|
|
18
|
+
cursor?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type EditorModel = {
|
|
22
|
+
id: string;
|
|
23
|
+
text: () => string;
|
|
24
|
+
extension?: Extension;
|
|
25
|
+
|
|
26
|
+
// TODO(burdon): Remove.
|
|
27
|
+
content: string | DocAccessor;
|
|
28
|
+
|
|
29
|
+
// TODO(burdon): Remove.
|
|
30
|
+
peer?: {
|
|
31
|
+
id: string;
|
|
32
|
+
name?: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* State field makes the model available to other extensions.
|
|
38
|
+
*/
|
|
39
|
+
export const modelState = StateField.define<EditorModel | undefined>({
|
|
40
|
+
create: () => undefined,
|
|
41
|
+
update: (model) => model,
|
|
42
|
+
});
|
package/src/hooks/index.ts
CHANGED
|
@@ -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
|
+
};
|
|
@@ -2,67 +2,20 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Extension, StateField } from '@codemirror/state';
|
|
6
5
|
import get from 'lodash.get';
|
|
7
6
|
import { useEffect, useState } from 'react';
|
|
8
|
-
import { yCollab } from 'y-codemirror.next';
|
|
9
|
-
import type * as awarenessProtocol from 'y-protocols/awareness';
|
|
10
|
-
import * as Y from 'yjs';
|
|
11
7
|
|
|
8
|
+
import { generateName } from '@dxos/display-name';
|
|
9
|
+
import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
|
|
12
10
|
import { invariant } from '@dxos/invariant';
|
|
13
|
-
import {
|
|
14
|
-
type DocAccessor,
|
|
15
|
-
type Space,
|
|
16
|
-
type TextObject,
|
|
17
|
-
type AutomergeTextCompat,
|
|
18
|
-
getRawDoc,
|
|
19
|
-
isActualAutomergeObject,
|
|
20
|
-
} from '@dxos/react-client/echo';
|
|
11
|
+
import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
|
|
21
12
|
import { type Identity } from '@dxos/react-client/halo';
|
|
22
|
-
import
|
|
23
|
-
import { arrayToString, stringToArray } from '@dxos/util';
|
|
13
|
+
import { isNotNullOrUndefined } from '@dxos/util';
|
|
24
14
|
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
type Awareness = awarenessProtocol.Awareness;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* State field makes the model available to other extensions.
|
|
33
|
-
*/
|
|
34
|
-
// TODO(burdon): Use facet?
|
|
35
|
-
export const modelState = StateField.define<EditorModel | undefined>({
|
|
36
|
-
create: () => undefined,
|
|
37
|
-
update: (model) => model,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
export type Range = {
|
|
41
|
-
from: number;
|
|
42
|
-
to: number;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type CommentRange = {
|
|
46
|
-
id: string;
|
|
47
|
-
// TODO(burdon): Split into begin/end?
|
|
48
|
-
cursor: string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
52
|
-
export type EditorModel = {
|
|
53
|
-
id: string;
|
|
54
|
-
// TODO(burdon): Remove.
|
|
55
|
-
content: string | YText | YXmlFragment | DocAccessor;
|
|
56
|
-
text: () => string;
|
|
57
|
-
getCursorFromRange?: (value: Range) => string;
|
|
58
|
-
getRangeFromCursor?: (cursor: string) => Range | undefined;
|
|
59
|
-
extension?: Extension;
|
|
60
|
-
awareness?: Awareness;
|
|
61
|
-
peer?: {
|
|
62
|
-
id: string;
|
|
63
|
-
name?: string;
|
|
64
|
-
};
|
|
65
|
-
};
|
|
15
|
+
import { SpaceAwarenessProvider } from './awareness-provider';
|
|
16
|
+
import { type EditorModel, modelState } from './defs';
|
|
17
|
+
import { automerge, awareness, AwarenessProvider } from '../extensions';
|
|
18
|
+
import { cursorColor } from '../styles';
|
|
66
19
|
|
|
67
20
|
// TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
|
|
68
21
|
export type UseTextModelProps = {
|
|
@@ -71,80 +24,41 @@ export type UseTextModelProps = {
|
|
|
71
24
|
text?: TextObject;
|
|
72
25
|
};
|
|
73
26
|
|
|
74
|
-
// TODO(burdon): Remove YJS/Automerge deps (from UI component -- create abstraction; incl. all ECHO/Space deps).
|
|
75
|
-
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
76
27
|
export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
|
|
77
28
|
const { identity, space, text } = props;
|
|
78
|
-
const [model, setModel] = useState<EditorModel | undefined>(
|
|
29
|
+
const [model, setModel] = useState<EditorModel | undefined>();
|
|
79
30
|
useEffect(() => setModel(createModel(props)), [identity, space, text]);
|
|
80
31
|
return model;
|
|
81
32
|
};
|
|
82
33
|
|
|
83
|
-
const createModel = (
|
|
84
|
-
|
|
85
|
-
if (isActualAutomergeObject(text)) {
|
|
86
|
-
return createAutomergeModel(props);
|
|
87
|
-
} else {
|
|
88
|
-
if (!text?.doc) {
|
|
89
|
-
return undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return createYjsModel(props);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const createYjsModel = ({ identity, space, text }: UseTextModelProps): EditorModel => {
|
|
97
|
-
invariant(text?.doc && text?.content);
|
|
98
|
-
const provider = space
|
|
99
|
-
? new SpaceAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` })
|
|
100
|
-
: undefined;
|
|
101
|
-
|
|
102
|
-
const model: EditorModel = {
|
|
103
|
-
id: text.doc.guid,
|
|
104
|
-
content: text.content,
|
|
105
|
-
text: () => text.content!.toString(),
|
|
106
|
-
// https://github.com/yjs/yjs?tab=readme-ov-file#relative-positions
|
|
107
|
-
getCursorFromRange: (range: Range) => {
|
|
108
|
-
const from = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.from));
|
|
109
|
-
const to = Y.encodeRelativePosition(Y.createRelativePositionFromTypeIndex(text.content as YText, range.to, -1));
|
|
110
|
-
return [arrayToString(from), arrayToString(to)].join(':');
|
|
111
|
-
},
|
|
112
|
-
getRangeFromCursor: (cursor: string) => {
|
|
113
|
-
invariant(text.doc);
|
|
114
|
-
const parts = cursor.split(':');
|
|
115
|
-
const from = Y.createAbsolutePositionFromRelativePosition(
|
|
116
|
-
Y.decodeRelativePosition(stringToArray(parts[0])),
|
|
117
|
-
text.doc,
|
|
118
|
-
);
|
|
119
|
-
const to = Y.createAbsolutePositionFromRelativePosition(
|
|
120
|
-
Y.decodeRelativePosition(stringToArray(parts[1])),
|
|
121
|
-
text.doc,
|
|
122
|
-
);
|
|
123
|
-
return from && to ? { from: from.index, to: to.index } : undefined;
|
|
124
|
-
},
|
|
125
|
-
extension: [yCollab(text.content as YText, provider?.awareness), modelState.init(() => model)],
|
|
126
|
-
awareness: provider?.awareness,
|
|
127
|
-
peer: identity
|
|
128
|
-
? {
|
|
129
|
-
id: identity.identityKey.toHex(),
|
|
130
|
-
name: identity.profile?.displayName,
|
|
131
|
-
}
|
|
132
|
-
: undefined,
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
return model;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const createAutomergeModel = ({ identity, text }: UseTextModelProps): EditorModel => {
|
|
34
|
+
const createModel = ({ space, identity, text }: UseTextModelProps) => {
|
|
35
|
+
invariant(isAutomergeObject(text));
|
|
139
36
|
const obj = text as any as AutomergeTextCompat;
|
|
140
37
|
const doc = getRawDoc(obj, [obj.field]);
|
|
141
38
|
|
|
39
|
+
const awarenessProvider =
|
|
40
|
+
space &&
|
|
41
|
+
new SpaceAwarenessProvider({
|
|
42
|
+
space,
|
|
43
|
+
channel: `automerge.awareness.${obj.id}`,
|
|
44
|
+
info: {
|
|
45
|
+
displayName: identity ? identity.profile?.displayName ?? generateName(identity.identityKey.toHex()) : undefined,
|
|
46
|
+
color: cursorColor.color,
|
|
47
|
+
lightColor: cursorColor.light,
|
|
48
|
+
},
|
|
49
|
+
peerId: identity?.identityKey.toHex() ?? 'Anonymous',
|
|
50
|
+
});
|
|
51
|
+
|
|
142
52
|
const model: EditorModel = {
|
|
143
53
|
id: obj.id,
|
|
144
54
|
content: doc,
|
|
145
55
|
text: () => get(doc.handle.docSync(), doc.path),
|
|
146
|
-
|
|
147
|
-
|
|
56
|
+
extension: [
|
|
57
|
+
modelState.init(() => model),
|
|
58
|
+
automerge({ handle: doc.handle, path: doc.path }),
|
|
59
|
+
awarenessProvider && AwarenessProvider.of(awarenessProvider),
|
|
60
|
+
awareness(),
|
|
61
|
+
].filter(isNotNullOrUndefined),
|
|
148
62
|
peer: identity
|
|
149
63
|
? {
|
|
150
64
|
id: identity.identityKey.toHex(),
|
package/src/index.ts
CHANGED
|
@@ -5,8 +5,11 @@
|
|
|
5
5
|
export { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
|
|
6
6
|
export { Doc, YText, YXmlFragment } from '@dxos/text-model';
|
|
7
7
|
export { type Extension } from '@codemirror/state';
|
|
8
|
+
export { type EditorView } from '@codemirror/view';
|
|
8
9
|
|
|
9
10
|
export * from './components';
|
|
10
11
|
export * from './extensions';
|
|
11
12
|
export * from './hooks';
|
|
13
|
+
export { getToken } from './styles';
|
|
12
14
|
export * from './themes';
|
|
15
|
+
export * from './util';
|
package/src/styles/index.ts
CHANGED
package/src/styles/markdown.ts
CHANGED
|
@@ -39,10 +39,8 @@ export const codeBlock = 'mlb-2 font-mono bg-neutral-500/10 p-3 rounded';
|
|
|
39
39
|
|
|
40
40
|
export const inlineUrl = mx(code, 'px-1');
|
|
41
41
|
|
|
42
|
-
// TODO(burdon): Replace with widget.
|
|
43
42
|
export const blockquote = mx('pl-1 mr-1 border-is-4 border-primary-500/70 dark:border-primary-500/30', light);
|
|
44
43
|
|
|
45
|
-
// TODO(burdon): Replace with widget.
|
|
46
44
|
export const horizontalRule =
|
|
47
45
|
'flex mlb-4 border-b text-neutral-100 dark:text-neutral-900 border-neutral-200 dark:border-neutral-800';
|
|
48
46
|
|
package/src/styles/tokens.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import get from 'lodash.get';
|
|
5
6
|
import type { StyleSpec } from 'style-mod';
|
|
6
7
|
|
|
7
8
|
import { tailwindConfig, type TailwindConfig } from '@dxos/react-ui-theme';
|
|
@@ -12,3 +13,5 @@ export type ThemeStyles = {
|
|
|
12
13
|
|
|
13
14
|
// TODO(thure): Why export the whole theme? Can this be done differently?
|
|
14
15
|
export const tokens: TailwindConfig['theme'] = tailwindConfig({}).theme;
|
|
16
|
+
|
|
17
|
+
export const getToken = (path: string, defaultValue: any = undefined) => get(tokens, path, defaultValue);
|