@dxos/react-ui-editor 0.3.11-main.668f0ff → 0.3.11-main.690ca41
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 +1643 -1148
- 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 +3 -6
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +3 -0
- 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/index.d.ts +0 -2
- package/dist/types/src/components/TextEditor/index.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 -0
- 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 +3 -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/basic.d.ts.map +1 -0
- package/dist/types/src/extensions/blast.d.ts.map +1 -0
- package/dist/types/src/extensions/code.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/comments.d.ts +4 -2
- package/dist/types/src/extensions/comments.d.ts.map +1 -0
- package/dist/types/src/extensions/hr.d.ts.map +1 -0
- package/dist/types/src/extensions/image.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/index.d.ts +3 -2
- package/dist/types/src/extensions/index.d.ts.map +1 -0
- package/dist/types/src/{components/TextEditor/extensions → extensions}/link.d.ts +1 -0
- package/dist/types/src/extensions/link.d.ts.map +1 -0
- package/dist/types/src/extensions/listener.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -0
- package/dist/types/src/extensions/markdown/index.d.ts.map +1 -0
- package/dist/types/src/extensions/mention.d.ts.map +1 -0
- package/dist/types/src/extensions/table.d.ts.map +1 -0
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -0
- package/dist/types/src/extensions/typewriter.d.ts.map +1 -0
- 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 +9 -8
- 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/{components/TextEditor/themes → themes}/default.d.ts +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -0
- package/dist/types/src/themes/index.d.ts.map +1 -0
- 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 +17 -20
- package/src/components/TextEditor/TextEditor.stories.tsx +14 -1
- package/src/components/TextEditor/TextEditor.tsx +3 -3
- package/src/{hooks/automerge → components/TextEditor}/automerge.stories.tsx +66 -6
- package/src/components/TextEditor/codemirror.stories.ts +3 -2
- package/src/components/TextEditor/index.ts +0 -3
- package/src/{hooks/yjs → components/TextEditor}/yjs.stories.tsx +1 -1
- 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 +6 -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/{components/TextEditor/extensions → extensions}/code.ts +4 -5
- package/src/{components/TextEditor/extensions → extensions}/comments.ts +50 -19
- package/src/{components/TextEditor/extensions → extensions}/hr.ts +2 -3
- package/src/extensions/image.ts +87 -0
- package/src/{components/TextEditor/extensions → extensions}/index.ts +3 -2
- package/src/{components/TextEditor/extensions → extensions}/link.ts +112 -58
- package/src/extensions/listener.ts +35 -0
- package/src/{components/TextEditor/extensions → extensions}/markdown/highlight.ts +4 -5
- package/src/{components/TextEditor/extensions → extensions}/table.ts +5 -2
- package/src/{components/TextEditor/extensions → extensions}/tasklist.ts +1 -3
- package/src/{components/TextEditor/extensions → extensions}/typewriter.ts +1 -1
- 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 -93
- 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/{components/TextEditor/themes → themes}/default.ts +40 -34
- package/src/util/cursor.ts +29 -0
- package/src/util/index.ts +6 -0
- package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/hr.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/components/TextEditor/extensions/mermaid.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts +0 -12
- package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/typewriter.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/extensions/util.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/default.d.ts.map +0 -1
- package/dist/types/src/components/TextEditor/themes/index.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/components/TextEditor/extensions/image.ts +0 -69
- package/src/components/TextEditor/extensions/listener.ts +0 -39
- package/src/components/TextEditor/extensions/mermaid.ts +0 -11
- package/src/components/TextEditor/extensions/tooltip.ts +0 -61
- 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/{components/TextEditor/extensions → extensions}/autocomplete.d.ts +0 -0
- /package/dist/types/src/{hooks → extensions}/automerge/update-codemirror.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/basic.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/blast.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/code.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/hr.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/image.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/listener.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/bundle.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/highlight.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/markdown/index.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/mention.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/table.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/tasklist.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → extensions}/typewriter.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/{components/TextEditor/themes → themes}/index.d.ts +0 -0
- /package/dist/types/src/{components/TextEditor/extensions → util}/util.d.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/autocomplete.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/basic.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/blast.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/bundle.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/markdown/index.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/mention.ts +0 -0
- /package/src/{hooks/yjs → styles}/cursors.ts +0 -0
- /package/src/{components/TextEditor/themes → themes}/index.ts +0 -0
- /package/src/{components/TextEditor/extensions → util}/util.ts +0 -0
|
@@ -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
|
+
});
|
|
@@ -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,11 +108,13 @@ const commentsStateField = StateField.define<CommentsState>({
|
|
|
93
108
|
|
|
94
109
|
// Update range from store.
|
|
95
110
|
if (effect.is(setCommentRange)) {
|
|
96
|
-
const {
|
|
97
|
-
const ranges: ExtendedCommentRange[] = comments
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
111
|
+
const { comments } = effect.value;
|
|
112
|
+
const ranges: ExtendedCommentRange[] = comments
|
|
113
|
+
.map((comment) => {
|
|
114
|
+
const range = getRangeFromCursor(cursorConverter, comment.cursor);
|
|
115
|
+
return range && { ...comment, ...range };
|
|
116
|
+
})
|
|
117
|
+
.filter(nonNullable);
|
|
101
118
|
|
|
102
119
|
return { ...value, ranges };
|
|
103
120
|
}
|
|
@@ -202,14 +219,15 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
202
219
|
* Create comment thread action.
|
|
203
220
|
*/
|
|
204
221
|
const createCommentThread: Command = (view) => {
|
|
222
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
223
|
+
|
|
205
224
|
invariant(options.onCreate);
|
|
206
225
|
const { head, from, to } = view.state.selection.main;
|
|
207
226
|
if (from === to) {
|
|
208
227
|
return false;
|
|
209
228
|
}
|
|
210
229
|
|
|
211
|
-
const
|
|
212
|
-
const relPos = model?.getCursorFromRange?.({ from, to });
|
|
230
|
+
const relPos = getCursorFromRange(cursorConverter, { from, to });
|
|
213
231
|
if (relPos) {
|
|
214
232
|
// Create thread via callback.
|
|
215
233
|
const id = callbackWrapper(options.onCreate)(relPos);
|
|
@@ -322,27 +340,27 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
322
340
|
// Monitor cursor movement and text updates.
|
|
323
341
|
// TODO(burdon): Is there a better (finer grained) way to do this?
|
|
324
342
|
//
|
|
325
|
-
EditorView.updateListener.of(({ view, state,
|
|
343
|
+
EditorView.updateListener.of(({ view, state, changes }) => {
|
|
344
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
345
|
+
|
|
326
346
|
//
|
|
327
347
|
// Test if need to recompute indexed range if document changes before the end of the range.
|
|
328
348
|
//
|
|
329
349
|
{
|
|
330
350
|
let mod = false;
|
|
331
|
-
const model = state.field(modelState);
|
|
332
351
|
const { active, ranges } = state.field(commentsStateField);
|
|
333
352
|
changes.iterChanges((from, to, from2, to2) => {
|
|
334
|
-
invariant(model);
|
|
335
353
|
ranges.forEach((range) => {
|
|
336
354
|
if (from2 === to2) {
|
|
337
|
-
const newRange =
|
|
338
|
-
if (newRange.to - newRange.from === 0) {
|
|
355
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
356
|
+
if (!newRange || newRange.to - newRange.from === 0) {
|
|
339
357
|
// TODO(burdon): Delete range if empty.
|
|
340
358
|
log.info('deleted comment', { thread: range.id });
|
|
341
359
|
}
|
|
342
360
|
}
|
|
343
361
|
|
|
344
362
|
if (from <= range.to) {
|
|
345
|
-
const newRange =
|
|
363
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
346
364
|
Object.assign(range, newRange);
|
|
347
365
|
mod = true;
|
|
348
366
|
}
|
|
@@ -387,3 +405,16 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
387
405
|
}),
|
|
388
406
|
];
|
|
389
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
|
+
};
|
|
@@ -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
|
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { syntaxTree } from '@codemirror/language';
|
|
6
|
+
import { type EditorState, type Extension, StateField, type Transaction, type Range } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, WidgetType } from '@codemirror/view';
|
|
8
|
+
|
|
9
|
+
export type ImageOptions = {};
|
|
10
|
+
|
|
11
|
+
export const image = (options: ImageOptions = {}): Extension => {
|
|
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
|
+
},
|
|
39
|
+
provide: (field) => EditorView.decorations.from(field),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const buildDecorations = (from: number, to: number, state: EditorState) => {
|
|
44
|
+
const decorations: Range<Decoration>[] = [];
|
|
45
|
+
const cursor = state.selection.main.head;
|
|
46
|
+
|
|
47
|
+
syntaxTree(state).iterate({
|
|
48
|
+
enter: (node) => {
|
|
49
|
+
if (node.name === 'Image') {
|
|
50
|
+
const urlNode = node.node.getChild('URL');
|
|
51
|
+
if (urlNode) {
|
|
52
|
+
const hide = state.readOnly || cursor < node.from || cursor > node.to;
|
|
53
|
+
const url = state.sliceDoc(urlNode.from, urlNode.to);
|
|
54
|
+
decorations.push(
|
|
55
|
+
Decoration.replace({
|
|
56
|
+
block: true, // Prevent cursor from entering.
|
|
57
|
+
widget: new ImageWidget(url),
|
|
58
|
+
}).range(hide ? node.from : node.to, node.to),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
from,
|
|
64
|
+
to,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return decorations;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
class ImageWidget extends WidgetType {
|
|
71
|
+
constructor(readonly _url: string) {
|
|
72
|
+
super();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
override eq(other: WidgetType) {
|
|
76
|
+
return this._url === (other as any as ImageWidget)._url;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
toDOM(view: EditorView) {
|
|
80
|
+
const img = document.createElement('img');
|
|
81
|
+
img.setAttribute('src', this._url);
|
|
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');
|
|
85
|
+
return img;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
export * from './autocomplete';
|
|
6
|
+
export * from './automerge';
|
|
7
|
+
export * from './awareness';
|
|
6
8
|
export * from './basic';
|
|
7
9
|
export * from './blast';
|
|
8
10
|
export * from './code';
|
|
@@ -13,8 +15,7 @@ export * from './link';
|
|
|
13
15
|
export * from './listener';
|
|
14
16
|
export * from './markdown';
|
|
15
17
|
export * from './mention';
|
|
16
|
-
export * from './mermaid';
|
|
17
18
|
export * from './table';
|
|
18
19
|
export * from './tasklist';
|
|
19
|
-
export * from './tooltip';
|
|
20
20
|
export * from './typewriter';
|
|
21
|
+
export * from './yjs';
|