@dxos/react-ui-editor 0.3.11-main.b4b98ab → 0.3.11-main.b929e1d
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 +1666 -1179
- 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 +9 -5
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +6 -10
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +5 -2
- 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/{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 +26 -0
- package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts +4 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -0
- package/dist/types/src/{hooks → extensions}/automerge/semaphore.d.ts +4 -4
- 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/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +3 -1
- package/dist/types/src/extensions/comments.d.ts.map +1 -1
- 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 +3 -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/highlight.d.ts.map +1 -1
- 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 +3 -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/index.d.ts +2 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextModel.d.ts +8 -7
- package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
- package/dist/types/src/hooks/{yjs/space-provider.d.ts → yjs.d.ts} +8 -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/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/cursor.d.ts +17 -0
- package/dist/types/src/util/cursor.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +3 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/util.d.ts.map +1 -0
- package/package.json +22 -21
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +13 -11
- package/src/components/TextEditor/TextEditor.stories.tsx +16 -4
- package/src/components/TextEditor/TextEditor.tsx +52 -46
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- package/src/extensions/autocomplete.ts +18 -17
- package/src/extensions/automerge/automerge.ts +94 -0
- package/src/extensions/automerge/cursor.ts +51 -0
- package/src/extensions/automerge/defs.ts +62 -0
- package/src/extensions/automerge/index.ts +7 -0
- package/src/{hooks → extensions}/automerge/semaphore.ts +14 -12
- 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 +316 -0
- package/src/extensions/code.ts +4 -5
- package/src/extensions/comments.ts +44 -15
- package/src/extensions/hr.ts +2 -3
- package/src/extensions/image.ts +38 -20
- package/src/extensions/index.ts +3 -1
- package/src/extensions/link.ts +82 -84
- package/src/extensions/markdown/highlight.ts +3 -4
- package/src/extensions/table.ts +9 -3
- package/src/extensions/tasklist.ts +1 -3
- package/src/extensions/yjs/cursor.ts +21 -0
- package/src/extensions/yjs/index.ts +5 -0
- package/src/{hooks → extensions}/yjs/yjs.test.ts +1 -25
- package/src/extensions/yjs/yjs.ts +16 -0
- package/src/hooks/automerge.ts +52 -0
- package/src/hooks/awareness-provider.ts +120 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useTextModel.ts +20 -94
- package/src/hooks/{yjs/space-provider.ts → yjs.ts} +71 -19
- package/src/index.ts +3 -0
- package/src/styles/index.ts +1 -0
- package/src/styles/tokens.ts +3 -0
- package/src/testing/replicator.ts +10 -15
- package/src/themes/default.ts +39 -33
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -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.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.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/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/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/dist/types/src/{extensions → util}/util.d.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{extensions → util}/util.ts +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
//
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
|
+
// Copyright 2024 Automerge
|
|
4
|
+
// Ref: https://github.com/automerge/automerge-codemirror
|
|
3
5
|
//
|
|
4
6
|
|
|
5
|
-
// TODO(burdon): Additional copyright?
|
|
6
|
-
|
|
7
7
|
import { ChangeSet, type ChangeSpec, type EditorSelection, type EditorState } from '@codemirror/state';
|
|
8
8
|
import { type EditorView } from '@codemirror/view';
|
|
9
9
|
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
type SpliceTextPatch,
|
|
17
17
|
} from '@dxos/automerge/automerge';
|
|
18
18
|
|
|
19
|
-
import { reconcileAnnotationType } from './
|
|
19
|
+
import { reconcileAnnotationType } from './defs';
|
|
20
20
|
|
|
21
21
|
export const updateCodeMirror = (view: EditorView, selection: EditorSelection, target: Prop[], patches: Patch[]) => {
|
|
22
22
|
for (const patch of patches) {
|
|
@@ -56,6 +56,7 @@ const handleInsert = (target: Prop[], patch: InsertPatch): Array<ChangeSpec> =>
|
|
|
56
56
|
if (index == null) {
|
|
57
57
|
return [];
|
|
58
58
|
}
|
|
59
|
+
|
|
59
60
|
const text = patch.values.map((v) => (v ? v.toString() : '')).join('');
|
|
60
61
|
return [{ from: index, to: index, insert: text }];
|
|
61
62
|
};
|
|
@@ -65,6 +66,7 @@ const handleSplice = (target: Prop[], patch: SpliceTextPatch): Array<ChangeSpec>
|
|
|
65
66
|
if (index == null) {
|
|
66
67
|
return [];
|
|
67
68
|
}
|
|
69
|
+
|
|
68
70
|
return [{ from: index, insert: patch.value }];
|
|
69
71
|
};
|
|
70
72
|
|
|
@@ -73,6 +75,7 @@ const handleDel = (target: Prop[], patch: DelPatch): Array<ChangeSpec> => {
|
|
|
73
75
|
if (index == null) {
|
|
74
76
|
return [];
|
|
75
77
|
}
|
|
78
|
+
|
|
76
79
|
const length = patch.length || 1;
|
|
77
80
|
return [{ from: index, to: index + length }];
|
|
78
81
|
};
|
|
@@ -82,27 +85,32 @@ const handlePut = (target: Prop[], patch: PutPatch, state: EditorState): Array<C
|
|
|
82
85
|
if (index == null) {
|
|
83
86
|
return [];
|
|
84
87
|
}
|
|
88
|
+
|
|
85
89
|
const length = state.doc.length;
|
|
86
90
|
if (typeof patch.value !== 'string') {
|
|
87
91
|
return []; // TODO(dmaretskyi): How to handle non string values?
|
|
88
92
|
}
|
|
93
|
+
|
|
89
94
|
return [{ from: 0, to: length, insert: patch.value as any }];
|
|
90
95
|
};
|
|
91
96
|
|
|
92
|
-
// If the path of the patch is of the form [path, <index>] then we know this is
|
|
93
|
-
//
|
|
97
|
+
// If the path of the patch is of the form [path, <index>] then we know this is a path to a character
|
|
98
|
+
// within the sequence given by path.
|
|
94
99
|
const charPath = (textPath: Prop[], candidatePath: Prop[]): number | null => {
|
|
95
100
|
if (candidatePath.length !== textPath.length + 1) {
|
|
96
101
|
return null;
|
|
97
102
|
}
|
|
103
|
+
|
|
98
104
|
for (let i = 0; i < textPath.length; i++) {
|
|
99
105
|
if (textPath[i] !== candidatePath[i]) {
|
|
100
106
|
return null;
|
|
101
107
|
}
|
|
102
108
|
}
|
|
109
|
+
|
|
103
110
|
const index = candidatePath[candidatePath.length - 1];
|
|
104
111
|
if (typeof index === 'number') {
|
|
105
112
|
return index;
|
|
106
113
|
}
|
|
114
|
+
|
|
107
115
|
return null;
|
|
108
116
|
};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Annotation, Facet, type Extension, RangeSet, type Range } from '@codemirror/state';
|
|
6
|
+
import {
|
|
7
|
+
Decoration,
|
|
8
|
+
type DecorationSet,
|
|
9
|
+
EditorView,
|
|
10
|
+
type PluginValue,
|
|
11
|
+
ViewPlugin,
|
|
12
|
+
type ViewUpdate,
|
|
13
|
+
WidgetType,
|
|
14
|
+
} from '@codemirror/view';
|
|
15
|
+
|
|
16
|
+
import { Event } from '@dxos/async';
|
|
17
|
+
import { Context } from '@dxos/context';
|
|
18
|
+
|
|
19
|
+
import { CursorConverter } from '../util';
|
|
20
|
+
|
|
21
|
+
export interface AwarenessProvider {
|
|
22
|
+
remoteStateChange: Event<void>;
|
|
23
|
+
|
|
24
|
+
open(): void;
|
|
25
|
+
close(): void;
|
|
26
|
+
|
|
27
|
+
update(position: AwarenessPosition | undefined): void;
|
|
28
|
+
getRemoteStates(): AwarenessState[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const dummyProvider: AwarenessProvider = {
|
|
32
|
+
remoteStateChange: new Event(),
|
|
33
|
+
|
|
34
|
+
open: () => {},
|
|
35
|
+
close: () => {},
|
|
36
|
+
|
|
37
|
+
update: () => {},
|
|
38
|
+
getRemoteStates: () => [],
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const AwarenessProvider = Facet.define<AwarenessProvider, AwarenessProvider>({
|
|
42
|
+
combine: (providers) => providers[0] ?? dummyProvider,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// TODO(dmaretskyi): Specify the users that actually changed. Currently, we recalculate positions for every user.
|
|
46
|
+
const RemoteSelectionChangedAnnotation = Annotation.define();
|
|
47
|
+
|
|
48
|
+
export type AwarenessPosition = {
|
|
49
|
+
anchor?: string;
|
|
50
|
+
head?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type AwarenessInfo = {
|
|
54
|
+
displayName?: string;
|
|
55
|
+
// TODO(burdon): Rename light/dark.
|
|
56
|
+
color?: string;
|
|
57
|
+
lightColor?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type AwarenessState = {
|
|
61
|
+
position?: AwarenessPosition;
|
|
62
|
+
peerId: string;
|
|
63
|
+
info: AwarenessInfo;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extension provides presence information about other peers.
|
|
68
|
+
*/
|
|
69
|
+
// TODO(burdon): Why provide default?
|
|
70
|
+
export const awareness = (provider = dummyProvider): Extension => {
|
|
71
|
+
return [
|
|
72
|
+
AwarenessProvider.of(provider),
|
|
73
|
+
ViewPlugin.fromClass(RemoteSelectionsDecorator, {
|
|
74
|
+
decorations: (value) => value.decorations,
|
|
75
|
+
}),
|
|
76
|
+
styles,
|
|
77
|
+
];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Generates selection decorations from remote peers.
|
|
82
|
+
*/
|
|
83
|
+
export class RemoteSelectionsDecorator implements PluginValue {
|
|
84
|
+
public decorations: DecorationSet = RangeSet.of([]);
|
|
85
|
+
|
|
86
|
+
private readonly _ctx = new Context();
|
|
87
|
+
|
|
88
|
+
private _cursorConverter: CursorConverter;
|
|
89
|
+
private _provider: AwarenessProvider;
|
|
90
|
+
private _lastAnchor?: number = undefined;
|
|
91
|
+
private _lastHead?: number = undefined;
|
|
92
|
+
|
|
93
|
+
constructor(view: EditorView) {
|
|
94
|
+
this._cursorConverter = view.state.facet(CursorConverter);
|
|
95
|
+
this._provider = view.state.facet(AwarenessProvider);
|
|
96
|
+
this._provider.open();
|
|
97
|
+
this._provider.remoteStateChange.on(this._ctx, () => {
|
|
98
|
+
view.dispatch({ annotations: [RemoteSelectionChangedAnnotation.of([])] });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
destroy() {
|
|
103
|
+
void this._ctx.dispose();
|
|
104
|
+
this._provider.close();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
update(update: ViewUpdate) {
|
|
108
|
+
this._updateLocalSelection(update);
|
|
109
|
+
this._updateRemoteSelections(update);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private _updateLocalSelection(update: ViewUpdate) {
|
|
113
|
+
const hasFocus = update.view.hasFocus && update.view.dom.ownerDocument.hasFocus();
|
|
114
|
+
const { anchor = undefined, head = undefined } = hasFocus ? update.state.selection.main : {};
|
|
115
|
+
if (this._lastAnchor === anchor && this._lastHead === head) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this._lastAnchor = anchor;
|
|
120
|
+
this._lastHead = head;
|
|
121
|
+
|
|
122
|
+
this._provider.update(
|
|
123
|
+
anchor !== undefined && head !== undefined
|
|
124
|
+
? {
|
|
125
|
+
anchor: this._cursorConverter.toCursor(anchor),
|
|
126
|
+
head: this._cursorConverter.toCursor(head),
|
|
127
|
+
}
|
|
128
|
+
: undefined,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private _updateRemoteSelections(update: ViewUpdate) {
|
|
133
|
+
const decorations: Range<Decoration>[] = [];
|
|
134
|
+
const awarenessStates = this._provider.getRemoteStates();
|
|
135
|
+
for (const state of awarenessStates) {
|
|
136
|
+
const anchor = state.position?.anchor ? this._cursorConverter.fromCursor(state.position.anchor) : null;
|
|
137
|
+
const head = state.position?.head ? this._cursorConverter.fromCursor(state.position.head) : null;
|
|
138
|
+
if (anchor == null || head == null) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const start = Math.min(Math.min(anchor, head), update.view.state.doc.length);
|
|
143
|
+
const end = Math.min(Math.max(anchor, head), update.view.state.doc.length);
|
|
144
|
+
|
|
145
|
+
const startLine = update.view.state.doc.lineAt(start);
|
|
146
|
+
const endLine = update.view.state.doc.lineAt(end);
|
|
147
|
+
|
|
148
|
+
// TODO(burdon): Factor out styles.
|
|
149
|
+
const color = state.info.color ?? '#30bced';
|
|
150
|
+
const lightColor = state.info.lightColor ?? color + '33';
|
|
151
|
+
|
|
152
|
+
if (startLine.number === endLine.number) {
|
|
153
|
+
// Selected content in a single line.
|
|
154
|
+
decorations.push({
|
|
155
|
+
from: start,
|
|
156
|
+
to: end,
|
|
157
|
+
value: Decoration.mark({
|
|
158
|
+
attributes: { style: `background-color: ${lightColor}` },
|
|
159
|
+
class: 'cm-ySelection',
|
|
160
|
+
}),
|
|
161
|
+
});
|
|
162
|
+
} else {
|
|
163
|
+
// Selected content in multiple lines; first, render text-selection in the first line.
|
|
164
|
+
decorations.push({
|
|
165
|
+
from: start,
|
|
166
|
+
to: startLine.from + startLine.length,
|
|
167
|
+
value: Decoration.mark({
|
|
168
|
+
attributes: { style: `background-color: ${color}` },
|
|
169
|
+
class: 'cm-ySelection',
|
|
170
|
+
}),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// Render text-selection in the last line.
|
|
174
|
+
decorations.push({
|
|
175
|
+
from: endLine.from,
|
|
176
|
+
to: end,
|
|
177
|
+
value: Decoration.mark({
|
|
178
|
+
attributes: { style: `background-color: ${color}` },
|
|
179
|
+
class: 'cm-ySelection',
|
|
180
|
+
}),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
for (let i = startLine.number + 1; i < endLine.number; i++) {
|
|
184
|
+
const linePos = update.view.state.doc.line(i).from;
|
|
185
|
+
decorations.push({
|
|
186
|
+
from: linePos,
|
|
187
|
+
to: linePos,
|
|
188
|
+
value: Decoration.line({
|
|
189
|
+
attributes: { style: `background-color: ${color}`, class: 'cm-yLineSelection' },
|
|
190
|
+
}),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
decorations.push({
|
|
196
|
+
from: head,
|
|
197
|
+
to: head,
|
|
198
|
+
value: Decoration.widget({
|
|
199
|
+
side: head - anchor > 0 ? -1 : 1, // The local cursor should be rendered outside the remote selection.
|
|
200
|
+
block: false,
|
|
201
|
+
widget: new RemoteCaretWidget(state.info.displayName ?? 'Anonymous', color),
|
|
202
|
+
}),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.decorations = Decoration.set(decorations, true);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class RemoteCaretWidget extends WidgetType {
|
|
211
|
+
constructor(public name: string, public color: string) {
|
|
212
|
+
super();
|
|
213
|
+
this.name = name;
|
|
214
|
+
this.color = color;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
toDOM(): HTMLElement {
|
|
218
|
+
const span = document.createElement('span');
|
|
219
|
+
span.className = 'cm-ySelectionCaret';
|
|
220
|
+
span.style.backgroundColor = this.color;
|
|
221
|
+
span.style.borderColor = this.color;
|
|
222
|
+
|
|
223
|
+
const dot = document.createElement('div');
|
|
224
|
+
dot.className = 'cm-ySelectionCaretDot';
|
|
225
|
+
|
|
226
|
+
const info = document.createElement('div');
|
|
227
|
+
info.className = 'cm-ySelectionInfo';
|
|
228
|
+
info.innerText = this.name;
|
|
229
|
+
|
|
230
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
231
|
+
span.appendChild(dot);
|
|
232
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
233
|
+
span.appendChild(info);
|
|
234
|
+
span.appendChild(document.createTextNode('\u2060'));
|
|
235
|
+
|
|
236
|
+
return span;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
override eq(widget: this) {
|
|
240
|
+
return widget.color === this.color;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
compare(widget: this) {
|
|
244
|
+
return widget.color === this.color;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
override updateDOM() {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
override get estimatedHeight() {
|
|
252
|
+
return -1;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
override ignoreEvent() {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// TODO(burdon): Rename prefix (y for yjs?)
|
|
261
|
+
const styles = EditorView.baseTheme({
|
|
262
|
+
'.cm-ySelection': {},
|
|
263
|
+
'.cm-yLineSelection': {
|
|
264
|
+
padding: 0,
|
|
265
|
+
margin: '0px 2px 0px 4px',
|
|
266
|
+
},
|
|
267
|
+
'.cm-ySelectionCaret': {
|
|
268
|
+
position: 'relative',
|
|
269
|
+
borderLeft: '1px solid black',
|
|
270
|
+
borderRight: '1px solid black',
|
|
271
|
+
marginLeft: '-1px',
|
|
272
|
+
marginRight: '-1px',
|
|
273
|
+
boxSizing: 'border-box',
|
|
274
|
+
display: 'inline',
|
|
275
|
+
},
|
|
276
|
+
'.cm-ySelectionCaretDot': {
|
|
277
|
+
borderRadius: '50%',
|
|
278
|
+
position: 'absolute',
|
|
279
|
+
width: '.4em',
|
|
280
|
+
height: '.4em',
|
|
281
|
+
top: '-.2em',
|
|
282
|
+
left: '-.2em',
|
|
283
|
+
backgroundColor: 'inherit',
|
|
284
|
+
transition: 'transform .3s ease-in-out',
|
|
285
|
+
boxSizing: 'border-box',
|
|
286
|
+
},
|
|
287
|
+
'.cm-ySelectionCaret:hover > .cm-ySelectionCaretDot': {
|
|
288
|
+
transformOrigin: 'bottom center',
|
|
289
|
+
transform: 'scale(0)',
|
|
290
|
+
},
|
|
291
|
+
'.cm-ySelectionInfo': {
|
|
292
|
+
position: 'absolute',
|
|
293
|
+
top: '-1.05em',
|
|
294
|
+
left: '-1px',
|
|
295
|
+
fontSize: '.75em',
|
|
296
|
+
fontFamily: 'serif',
|
|
297
|
+
fontStyle: 'normal',
|
|
298
|
+
fontWeight: 'normal',
|
|
299
|
+
lineHeight: 'normal',
|
|
300
|
+
userSelect: 'none',
|
|
301
|
+
color: 'white',
|
|
302
|
+
paddingLeft: '2px',
|
|
303
|
+
paddingRight: '2px',
|
|
304
|
+
zIndex: 101,
|
|
305
|
+
transition: 'opacity .3s ease-in-out',
|
|
306
|
+
backgroundColor: 'inherit',
|
|
307
|
+
// these should be separate
|
|
308
|
+
opacity: 0,
|
|
309
|
+
transitionDelay: '0s',
|
|
310
|
+
whiteSpace: 'nowrap',
|
|
311
|
+
},
|
|
312
|
+
'.cm-ySelectionCaret:hover > .cm-ySelectionInfo': {
|
|
313
|
+
opacity: 1,
|
|
314
|
+
transitionDelay: '0s',
|
|
315
|
+
},
|
|
316
|
+
});
|
package/src/extensions/code.ts
CHANGED
|
@@ -11,24 +11,23 @@ import {
|
|
|
11
11
|
Decoration,
|
|
12
12
|
type BlockInfo,
|
|
13
13
|
} from '@codemirror/view';
|
|
14
|
-
import get from 'lodash.get';
|
|
15
14
|
|
|
16
15
|
import { mx } from '@dxos/react-ui-theme';
|
|
17
16
|
|
|
18
|
-
import {
|
|
17
|
+
import { getToken } from '../styles';
|
|
19
18
|
|
|
20
19
|
const CODE_REGEX = /```[\s\S]*?```/gs;
|
|
21
20
|
|
|
22
21
|
// TODO(burdon): Reconcile with theme.
|
|
23
22
|
const styles = EditorView.baseTheme({
|
|
24
23
|
'& .cm-codeblock': {
|
|
25
|
-
fontFamily:
|
|
24
|
+
fontFamily: getToken('fontFamily.mono', []).join(','),
|
|
26
25
|
},
|
|
27
26
|
'&light [aria-readonly="true"] .cm-codeblock': {
|
|
28
|
-
background:
|
|
27
|
+
background: getToken('extend.colors.neutral.50'),
|
|
29
28
|
},
|
|
30
29
|
'&dark [aria-readonly="true"] .cm-codeblock': {
|
|
31
|
-
background:
|
|
30
|
+
background: getToken('extend.colors.neutral.850'),
|
|
32
31
|
},
|
|
33
32
|
'& [aria-readonly="true"] .cm-codeblock': {
|
|
34
33
|
display: 'block',
|
|
@@ -23,10 +23,10 @@ import { invariant } from '@dxos/invariant';
|
|
|
23
23
|
import { log } from '@dxos/log';
|
|
24
24
|
import { nonNullable } from '@dxos/util';
|
|
25
25
|
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
26
|
+
import { type CommentRange, type EditorModel, type Range } from '../hooks';
|
|
27
|
+
import { getToken } from '../styles';
|
|
28
|
+
import { callbackWrapper, CursorConverter } from '../util';
|
|
28
29
|
|
|
29
|
-
// TODO(burdon): Handle scroll to.
|
|
30
30
|
// TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
|
|
31
31
|
// TODO(burdon): Consider breaking into separate plugin (since not standalone)? Like mermaid?
|
|
32
32
|
|
|
@@ -42,10 +42,10 @@ const styles = EditorView.baseTheme({
|
|
|
42
42
|
backgroundColor: 'orange',
|
|
43
43
|
},
|
|
44
44
|
'& .cm-comment': {
|
|
45
|
-
backgroundColor: 'yellow',
|
|
45
|
+
backgroundColor: getToken('extend.colors.yellow.50'),
|
|
46
46
|
},
|
|
47
47
|
'& .cm-comment-active': {
|
|
48
|
-
backgroundColor: '
|
|
48
|
+
backgroundColor: getToken('extend.colors.yellow.100'),
|
|
49
49
|
},
|
|
50
50
|
});
|
|
51
51
|
|
|
@@ -71,9 +71,22 @@ export type CommentsState = CommentSelected & {
|
|
|
71
71
|
ranges: ExtendedCommentRange[];
|
|
72
72
|
};
|
|
73
73
|
|
|
74
|
+
export const setFocus = (view: EditorView, thread: string) => {
|
|
75
|
+
const range = view.state.field(commentsStateField).ranges.find((range) => range.id === thread);
|
|
76
|
+
if (!range) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
view.dispatch({
|
|
81
|
+
effects: setSelection.of({ active: thread }),
|
|
82
|
+
selection: { anchor: range.from },
|
|
83
|
+
scrollIntoView: true,
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
74
87
|
export const setCommentRange = StateEffect.define<{ model: EditorModel; comments: CommentRange[] }>();
|
|
75
88
|
|
|
76
|
-
const setSelection = StateEffect.define<CommentSelected>();
|
|
89
|
+
export const setSelection = StateEffect.define<CommentSelected>();
|
|
77
90
|
|
|
78
91
|
const setCommentState = StateEffect.define<CommentsState>();
|
|
79
92
|
|
|
@@ -85,6 +98,8 @@ const setCommentState = StateEffect.define<CommentsState>();
|
|
|
85
98
|
const commentsStateField = StateField.define<CommentsState>({
|
|
86
99
|
create: () => ({ ranges: [] }),
|
|
87
100
|
update: (value, tr) => {
|
|
101
|
+
const cursorConverter = tr.state.facet(CursorConverter);
|
|
102
|
+
|
|
88
103
|
for (const effect of tr.effects) {
|
|
89
104
|
// Update selection.
|
|
90
105
|
if (effect.is(setSelection)) {
|
|
@@ -93,10 +108,10 @@ const commentsStateField = StateField.define<CommentsState>({
|
|
|
93
108
|
|
|
94
109
|
// Update range from store.
|
|
95
110
|
if (effect.is(setCommentRange)) {
|
|
96
|
-
const {
|
|
111
|
+
const { comments } = effect.value;
|
|
97
112
|
const ranges: ExtendedCommentRange[] = comments
|
|
98
113
|
.map((comment) => {
|
|
99
|
-
const range =
|
|
114
|
+
const range = getRangeFromCursor(cursorConverter, comment.cursor);
|
|
100
115
|
return range && { ...comment, ...range };
|
|
101
116
|
})
|
|
102
117
|
.filter(nonNullable);
|
|
@@ -204,14 +219,15 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
204
219
|
* Create comment thread action.
|
|
205
220
|
*/
|
|
206
221
|
const createCommentThread: Command = (view) => {
|
|
222
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
223
|
+
|
|
207
224
|
invariant(options.onCreate);
|
|
208
225
|
const { head, from, to } = view.state.selection.main;
|
|
209
226
|
if (from === to) {
|
|
210
227
|
return false;
|
|
211
228
|
}
|
|
212
229
|
|
|
213
|
-
const
|
|
214
|
-
const relPos = model?.getCursorFromRange?.({ from, to });
|
|
230
|
+
const relPos = getCursorFromRange(cursorConverter, { from, to });
|
|
215
231
|
if (relPos) {
|
|
216
232
|
// Create thread via callback.
|
|
217
233
|
const id = callbackWrapper(options.onCreate)(relPos);
|
|
@@ -324,19 +340,19 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
324
340
|
// Monitor cursor movement and text updates.
|
|
325
341
|
// TODO(burdon): Is there a better (finer grained) way to do this?
|
|
326
342
|
//
|
|
327
|
-
EditorView.updateListener.of(({ view, state,
|
|
343
|
+
EditorView.updateListener.of(({ view, state, changes }) => {
|
|
344
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
345
|
+
|
|
328
346
|
//
|
|
329
347
|
// Test if need to recompute indexed range if document changes before the end of the range.
|
|
330
348
|
//
|
|
331
349
|
{
|
|
332
350
|
let mod = false;
|
|
333
|
-
const model = state.field(modelState);
|
|
334
351
|
const { active, ranges } = state.field(commentsStateField);
|
|
335
352
|
changes.iterChanges((from, to, from2, to2) => {
|
|
336
|
-
invariant(model);
|
|
337
353
|
ranges.forEach((range) => {
|
|
338
354
|
if (from2 === to2) {
|
|
339
|
-
const newRange =
|
|
355
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
340
356
|
if (!newRange || newRange.to - newRange.from === 0) {
|
|
341
357
|
// TODO(burdon): Delete range if empty.
|
|
342
358
|
log.info('deleted comment', { thread: range.id });
|
|
@@ -344,7 +360,7 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
344
360
|
}
|
|
345
361
|
|
|
346
362
|
if (from <= range.to) {
|
|
347
|
-
const newRange =
|
|
363
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
348
364
|
Object.assign(range, newRange);
|
|
349
365
|
mod = true;
|
|
350
366
|
}
|
|
@@ -389,3 +405,16 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
389
405
|
}),
|
|
390
406
|
];
|
|
391
407
|
};
|
|
408
|
+
|
|
409
|
+
const getCursorFromRange = (cursorConverter: CursorConverter, range: Range) => {
|
|
410
|
+
const from = cursorConverter.toCursor(range.from);
|
|
411
|
+
const to = cursorConverter.toCursor(range.to, -1);
|
|
412
|
+
return [from, to].join(':');
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
const getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) => {
|
|
416
|
+
const parts = cursor.split(':');
|
|
417
|
+
const from = cursorConverter.fromCursor(parts[0]);
|
|
418
|
+
const to = cursorConverter.fromCursor(parts[1]);
|
|
419
|
+
return from !== undefined && to !== undefined ? { from, to } : undefined;
|
|
420
|
+
};
|
package/src/extensions/hr.ts
CHANGED
|
@@ -11,16 +11,15 @@ import {
|
|
|
11
11
|
type ViewUpdate,
|
|
12
12
|
WidgetType,
|
|
13
13
|
} from '@codemirror/view';
|
|
14
|
-
import get from 'lodash.get';
|
|
15
14
|
|
|
16
|
-
import {
|
|
15
|
+
import { getToken } from '../styles';
|
|
17
16
|
|
|
18
17
|
// TODO(burdon): Reconcile with theme.
|
|
19
18
|
const styles = EditorView.baseTheme({
|
|
20
19
|
'& .cm-hr': {
|
|
21
20
|
// TODO(burdon): ???
|
|
22
21
|
// Note that block-level decorations should not have vertical margins,
|
|
23
|
-
borderBottom: `1px solid ${
|
|
22
|
+
borderBottom: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
24
23
|
},
|
|
25
24
|
});
|
|
26
25
|
|
package/src/extensions/image.ts
CHANGED
|
@@ -3,28 +3,45 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { syntaxTree } from '@codemirror/language';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
type Extension,
|
|
9
|
-
type RangeSet,
|
|
10
|
-
RangeSetBuilder,
|
|
11
|
-
StateField,
|
|
12
|
-
type Transaction,
|
|
13
|
-
} from '@codemirror/state';
|
|
14
|
-
import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
6
|
+
import { type EditorState, type Extension, StateField, type Transaction, type Range } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, WidgetType } from '@codemirror/view';
|
|
15
8
|
|
|
16
9
|
export type ImageOptions = {};
|
|
17
10
|
|
|
18
11
|
export const image = (options: ImageOptions = {}): Extension => {
|
|
19
|
-
return StateField.define<
|
|
20
|
-
create: (state) =>
|
|
21
|
-
|
|
12
|
+
return StateField.define<DecorationSet>({
|
|
13
|
+
create: (state) => {
|
|
14
|
+
return Decoration.set(buildDecorations(0, state.doc.length, state));
|
|
15
|
+
},
|
|
16
|
+
update: (value: DecorationSet, tr: Transaction) => {
|
|
17
|
+
if (!tr.docChanged && !tr.selection) {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
// Find range of changes and cursor changes.
|
|
21
|
+
const cursor = tr.state.selection.main.head;
|
|
22
|
+
const oldCursor = tr.changes.mapPos(tr.startState.selection.main.head);
|
|
23
|
+
let from = Math.min(cursor, oldCursor);
|
|
24
|
+
let to = Math.max(cursor, oldCursor);
|
|
25
|
+
tr.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
|
|
26
|
+
from = Math.min(from, fromB);
|
|
27
|
+
to = Math.max(to, toB);
|
|
28
|
+
});
|
|
29
|
+
// Expand to cover lines
|
|
30
|
+
from = tr.state.doc.lineAt(from).from;
|
|
31
|
+
to = tr.state.doc.lineAt(to).to;
|
|
32
|
+
return value.map(tr.changes).update({
|
|
33
|
+
filterFrom: from,
|
|
34
|
+
filterTo: to,
|
|
35
|
+
filter: () => false,
|
|
36
|
+
add: buildDecorations(from, to, tr.state),
|
|
37
|
+
});
|
|
38
|
+
},
|
|
22
39
|
provide: (field) => EditorView.decorations.from(field),
|
|
23
40
|
});
|
|
24
41
|
};
|
|
25
42
|
|
|
26
|
-
const
|
|
27
|
-
const
|
|
43
|
+
const buildDecorations = (from: number, to: number, state: EditorState) => {
|
|
44
|
+
const decorations: Range<Decoration>[] = [];
|
|
28
45
|
const cursor = state.selection.main.head;
|
|
29
46
|
|
|
30
47
|
syntaxTree(state).iterate({
|
|
@@ -34,20 +51,20 @@ const update = (state: EditorState, options: ImageOptions) => {
|
|
|
34
51
|
if (urlNode) {
|
|
35
52
|
const hide = state.readOnly || cursor < node.from || cursor > node.to;
|
|
36
53
|
const url = state.sliceDoc(urlNode.from, urlNode.to);
|
|
37
|
-
|
|
38
|
-
hide ? node.from : node.to,
|
|
39
|
-
node.to,
|
|
54
|
+
decorations.push(
|
|
40
55
|
Decoration.replace({
|
|
41
56
|
block: true, // Prevent cursor from entering.
|
|
42
57
|
widget: new ImageWidget(url),
|
|
43
|
-
}),
|
|
58
|
+
}).range(hide ? node.from : node.to, node.to),
|
|
44
59
|
);
|
|
45
60
|
}
|
|
46
61
|
}
|
|
47
62
|
},
|
|
63
|
+
from,
|
|
64
|
+
to,
|
|
48
65
|
});
|
|
49
66
|
|
|
50
|
-
return
|
|
67
|
+
return decorations;
|
|
51
68
|
};
|
|
52
69
|
|
|
53
70
|
class ImageWidget extends WidgetType {
|
|
@@ -55,7 +72,6 @@ class ImageWidget extends WidgetType {
|
|
|
55
72
|
super();
|
|
56
73
|
}
|
|
57
74
|
|
|
58
|
-
// TODO(burdon): Does this need to be unique for each position?
|
|
59
75
|
override eq(other: WidgetType) {
|
|
60
76
|
return this._url === (other as any as ImageWidget)._url;
|
|
61
77
|
}
|
|
@@ -64,6 +80,8 @@ class ImageWidget extends WidgetType {
|
|
|
64
80
|
const img = document.createElement('img');
|
|
65
81
|
img.setAttribute('src', this._url);
|
|
66
82
|
img.setAttribute('class', 'cm-image');
|
|
83
|
+
// Images are hidden until successfully loaded to avoid flickering effects.
|
|
84
|
+
img.onload = () => img.classList.add('cm-loaded-image');
|
|
67
85
|
return img;
|
|
68
86
|
}
|
|
69
87
|
}
|