@dxos/react-ui-editor 0.3.11-main.bd26370 → 0.3.11-main.fbbdc2a
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 +1541 -1062
- 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 +0 -3
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +2 -2
- 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 -1
- 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 +10 -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/mermaid.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 +2 -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/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 +11 -13
- package/src/components/TextEditor/TextEditor.stories.tsx +14 -1
- package/src/components/TextEditor/TextEditor.tsx +7 -7
- 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 +1 -1
- package/src/{components/TextEditor/extensions → extensions}/comments.ts +52 -19
- package/src/{components/TextEditor/extensions → extensions}/hr.ts +1 -1
- package/src/{components/TextEditor/extensions → extensions}/index.ts +3 -1
- package/src/{components/TextEditor/extensions → extensions}/link.ts +66 -10
- package/src/extensions/listener.ts +35 -0
- package/src/{components/TextEditor/extensions → extensions}/markdown/highlight.ts +1 -1
- 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 +2 -0
- package/src/styles/index.ts +1 -0
- package/src/testing/replicator.ts +10 -15
- package/src/{components/TextEditor/themes → themes}/default.ts +1 -1
- 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 +0 -9
- 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.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/listener.ts +0 -23
- 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}/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}/mermaid.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}/image.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/{components/TextEditor/extensions → extensions}/mermaid.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/table.ts +0 -0
- /package/src/{components/TextEditor/extensions → extensions}/tasklist.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
|
+
});
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
WidgetType,
|
|
17
17
|
type Rect,
|
|
18
18
|
} from '@codemirror/view';
|
|
19
|
+
import get from 'lodash.get';
|
|
19
20
|
import sortBy from 'lodash.sortby';
|
|
20
21
|
|
|
21
22
|
import { debounce } from '@dxos/async';
|
|
@@ -23,8 +24,9 @@ import { invariant } from '@dxos/invariant';
|
|
|
23
24
|
import { log } from '@dxos/log';
|
|
24
25
|
import { nonNullable } from '@dxos/util';
|
|
25
26
|
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
27
|
+
import { type CommentRange, type EditorModel, type Range } from '../hooks';
|
|
28
|
+
import { tokens } from '../styles';
|
|
29
|
+
import { callbackWrapper, CursorConverter } from '../util';
|
|
28
30
|
|
|
29
31
|
// TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
|
|
30
32
|
// TODO(burdon): Consider breaking into separate plugin (since not standalone)? Like mermaid?
|
|
@@ -41,10 +43,10 @@ const styles = EditorView.baseTheme({
|
|
|
41
43
|
backgroundColor: 'orange',
|
|
42
44
|
},
|
|
43
45
|
'& .cm-comment': {
|
|
44
|
-
backgroundColor: 'yellow',
|
|
46
|
+
backgroundColor: get(tokens, 'extend.colors.yellow.50'),
|
|
45
47
|
},
|
|
46
48
|
'& .cm-comment-active': {
|
|
47
|
-
backgroundColor: '
|
|
49
|
+
backgroundColor: get(tokens, 'extend.colors.yellow.100'),
|
|
48
50
|
},
|
|
49
51
|
});
|
|
50
52
|
|
|
@@ -70,9 +72,22 @@ export type CommentsState = CommentSelected & {
|
|
|
70
72
|
ranges: ExtendedCommentRange[];
|
|
71
73
|
};
|
|
72
74
|
|
|
75
|
+
export const setFocus = (view: EditorView, thread: string) => {
|
|
76
|
+
const range = view.state.field(commentsStateField).ranges.find((range) => range.id === thread);
|
|
77
|
+
if (!range) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
view.dispatch({
|
|
82
|
+
effects: setSelection.of({ active: thread }),
|
|
83
|
+
selection: { anchor: range.from },
|
|
84
|
+
scrollIntoView: true,
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
73
88
|
export const setCommentRange = StateEffect.define<{ model: EditorModel; comments: CommentRange[] }>();
|
|
74
89
|
|
|
75
|
-
const setSelection = StateEffect.define<CommentSelected>();
|
|
90
|
+
export const setSelection = StateEffect.define<CommentSelected>();
|
|
76
91
|
|
|
77
92
|
const setCommentState = StateEffect.define<CommentsState>();
|
|
78
93
|
|
|
@@ -84,6 +99,8 @@ const setCommentState = StateEffect.define<CommentsState>();
|
|
|
84
99
|
const commentsStateField = StateField.define<CommentsState>({
|
|
85
100
|
create: () => ({ ranges: [] }),
|
|
86
101
|
update: (value, tr) => {
|
|
102
|
+
const cursorConverter = tr.state.facet(CursorConverter);
|
|
103
|
+
|
|
87
104
|
for (const effect of tr.effects) {
|
|
88
105
|
// Update selection.
|
|
89
106
|
if (effect.is(setSelection)) {
|
|
@@ -92,11 +109,13 @@ const commentsStateField = StateField.define<CommentsState>({
|
|
|
92
109
|
|
|
93
110
|
// Update range from store.
|
|
94
111
|
if (effect.is(setCommentRange)) {
|
|
95
|
-
const {
|
|
96
|
-
const ranges: ExtendedCommentRange[] = comments
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
112
|
+
const { comments } = effect.value;
|
|
113
|
+
const ranges: ExtendedCommentRange[] = comments
|
|
114
|
+
.map((comment) => {
|
|
115
|
+
const range = getRangeFromCursor(cursorConverter, comment.cursor);
|
|
116
|
+
return range && { ...comment, ...range };
|
|
117
|
+
})
|
|
118
|
+
.filter(nonNullable);
|
|
100
119
|
|
|
101
120
|
return { ...value, ranges };
|
|
102
121
|
}
|
|
@@ -201,14 +220,15 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
201
220
|
* Create comment thread action.
|
|
202
221
|
*/
|
|
203
222
|
const createCommentThread: Command = (view) => {
|
|
223
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
224
|
+
|
|
204
225
|
invariant(options.onCreate);
|
|
205
226
|
const { head, from, to } = view.state.selection.main;
|
|
206
227
|
if (from === to) {
|
|
207
228
|
return false;
|
|
208
229
|
}
|
|
209
230
|
|
|
210
|
-
const
|
|
211
|
-
const relPos = model?.getCursorFromRange?.({ from, to });
|
|
231
|
+
const relPos = getCursorFromRange(cursorConverter, { from, to });
|
|
212
232
|
if (relPos) {
|
|
213
233
|
// Create thread via callback.
|
|
214
234
|
const id = callbackWrapper(options.onCreate)(relPos);
|
|
@@ -284,7 +304,7 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
284
304
|
? keymap.of([
|
|
285
305
|
{
|
|
286
306
|
key: options?.key ?? "meta-'",
|
|
287
|
-
run: createCommentThread,
|
|
307
|
+
run: callbackWrapper(createCommentThread),
|
|
288
308
|
},
|
|
289
309
|
])
|
|
290
310
|
: [],
|
|
@@ -321,27 +341,27 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
321
341
|
// Monitor cursor movement and text updates.
|
|
322
342
|
// TODO(burdon): Is there a better (finer grained) way to do this?
|
|
323
343
|
//
|
|
324
|
-
EditorView.updateListener.of(({ view, state,
|
|
344
|
+
EditorView.updateListener.of(({ view, state, changes }) => {
|
|
345
|
+
const cursorConverter = view.state.facet(CursorConverter);
|
|
346
|
+
|
|
325
347
|
//
|
|
326
348
|
// Test if need to recompute indexed range if document changes before the end of the range.
|
|
327
349
|
//
|
|
328
350
|
{
|
|
329
351
|
let mod = false;
|
|
330
|
-
const model = state.field(modelState);
|
|
331
352
|
const { active, ranges } = state.field(commentsStateField);
|
|
332
353
|
changes.iterChanges((from, to, from2, to2) => {
|
|
333
|
-
invariant(model);
|
|
334
354
|
ranges.forEach((range) => {
|
|
335
355
|
if (from2 === to2) {
|
|
336
|
-
const newRange =
|
|
337
|
-
if (newRange.to - newRange.from === 0) {
|
|
356
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
357
|
+
if (!newRange || newRange.to - newRange.from === 0) {
|
|
338
358
|
// TODO(burdon): Delete range if empty.
|
|
339
359
|
log.info('deleted comment', { thread: range.id });
|
|
340
360
|
}
|
|
341
361
|
}
|
|
342
362
|
|
|
343
363
|
if (from <= range.to) {
|
|
344
|
-
const newRange =
|
|
364
|
+
const newRange = getRangeFromCursor(cursorConverter, range.cursor);
|
|
345
365
|
Object.assign(range, newRange);
|
|
346
366
|
mod = true;
|
|
347
367
|
}
|
|
@@ -386,3 +406,16 @@ export const comments = (options: CommentsOptions = {}): Extension => {
|
|
|
386
406
|
}),
|
|
387
407
|
];
|
|
388
408
|
};
|
|
409
|
+
|
|
410
|
+
const getCursorFromRange = (cursorConverter: CursorConverter, range: Range) => {
|
|
411
|
+
const from = cursorConverter.toCursor(range.from);
|
|
412
|
+
const to = cursorConverter.toCursor(range.to, -1);
|
|
413
|
+
return [from, to].join(':');
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
const getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) => {
|
|
417
|
+
const parts = cursor.split(':');
|
|
418
|
+
const from = cursorConverter.fromCursor(parts[0]);
|
|
419
|
+
const to = cursorConverter.fromCursor(parts[1]);
|
|
420
|
+
return from && to ? { from, to } : undefined;
|
|
421
|
+
};
|
|
@@ -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';
|
|
@@ -16,5 +18,5 @@ export * from './mention';
|
|
|
16
18
|
export * from './mermaid';
|
|
17
19
|
export * from './table';
|
|
18
20
|
export * from './tasklist';
|
|
19
|
-
export * from './tooltip';
|
|
20
21
|
export * from './typewriter';
|
|
22
|
+
export * from './yjs';
|
|
@@ -12,7 +12,11 @@ import {
|
|
|
12
12
|
StateField,
|
|
13
13
|
type Transaction,
|
|
14
14
|
} from '@codemirror/state';
|
|
15
|
-
import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
15
|
+
import { Decoration, EditorView, WidgetType, hoverTooltip } from '@codemirror/view';
|
|
16
|
+
|
|
17
|
+
import { tooltipContent } from '@dxos/react-ui-theme';
|
|
18
|
+
|
|
19
|
+
const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)/;
|
|
16
20
|
|
|
17
21
|
// Adapted from:
|
|
18
22
|
// https://codemirror.net/try/?c=aW1wb3J0IHsgYmFzaWNTZXR1cCwgRWRpdG9yVmlldyB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7IG1hcmtkb3duIH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1tYXJrZG93biIKaW1wb3J0IHsgc3ludGF4VHJlZSB9IGZyb20gIkBjb2RlbWlycm9yL2xhbmd1YWdlIjsKaW1wb3J0IHsgRWRpdG9yU3RhdGUsIFJhbmdlU2V0QnVpbGRlciwgU3RhdGVGaWVsZCB9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIjsKaW1wb3J0IHsgRGVjb3JhdGlvbiwgV2lkZ2V0VHlwZSB9IGZyb20gIkBjb2RlbWlycm9yL3ZpZXciOwoKY2xhc3MgTGluayBleHRlbmRzIFdpZGdldFR5cGUgewogIGNvbnN0cnVjdG9yKHRleHQsIHVybCkgewogICAgc3VwZXIoKQogICAgdGhpcy50ZXh0ID0gdGV4dAogICAgdGhpcy51cmwgPSB1cmwKICB9CiAgZXEob3RoZXIpIHsgcmV0dXJuIHRoaXMudGV4dCA9PSBvdGhlci50ZXh0ICYmIHRoaXMudXJsID09IG90aGVyLnVybCB9CiAgdG9ET00oKSB7CiAgICBsZXQgbGluayA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoImEiKQogICAgbGluay50ZXh0Q29udGVudCA9IHRoaXMudGV4dAogICAgbGluay5ocmVmID0gdGhpcy51cmwKICAgIHJldHVybiBsaW5rOwogIH0KfQoKbGV0IGRlY29yYXRpb25zRmllbGQgPSBTdGF0ZUZpZWxkLmRlZmluZSh7CiAgY3JlYXRlKCkgewogICAgcmV0dXJuIERlY29yYXRpb24ubm9uZTsKICB9LAogIHVwZGF0ZShfLCB0cikgewogICAgY29uc3QgYnVpbGRlciA9IG5ldyBSYW5nZVNldEJ1aWxkZXIoKTsKICAgIGxldCBjdXJzb3IgPSB0ci5zdGF0ZS5zZWxlY3Rpb24ubWFpbi5oZWFkOwogICAgc3ludGF4VHJlZSh0ci5zdGF0ZSkuaXRlcmF0ZSh7CiAgICAgIGVudGVyOiAobm9kZSkgPT4gewogICAgICAgIGlmICgoY3Vyc29yIDwgbm9kZS5mcm9tIHx8IGN1cnNvciA+IG5vZGUudG8pICYmIG5vZGUubmFtZSA9PSAiTGluayIpIHsKICAgICAgICAgIGxldCBtYXJrcyA9IG5vZGUubm9kZS5nZXRDaGlsZHJlbigiTGlua01hcmsiKTsKICAgICAgICAgIGxldCB0ZXh0ID0gbWFya3MubGVuZ3RoID49IDIgPyB0ci5zdGF0ZS5zbGljZURvYyhtYXJrc1swXS50bywgbWFya3NbMV0uZnJvbSkgOiAiIgoKICAgICAgICAgIGxldCB1cmxOb2RlID0gbm9kZS5ub2RlLmdldENoaWxkKCJVUkwiKTsKICAgICAgICAgIGxldCB1cmwgPSB1cmxOb2RlID8gdHIuc3RhdGUuc2xpY2VEb2ModXJsTm9kZS5mcm9tLCB1cmxOb2RlLnRvKSA6ICIiCiAgICAgICAgICBidWlsZGVyLmFkZChub2RlLmZyb20sIG5vZGUudG8sIERlY29yYXRpb24ucmVwbGFjZSh7CiAgICAgICAgICAgIHdpZGdldDogbmV3IExpbmsodGV4dCwgdXJsKSwKICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSwKICAgIH0pOwogICAgcmV0dXJuIGJ1aWxkZXIuZmluaXNoKCk7CiAgfSwKICBwcm92aWRlOiAoZikgPT4gRWRpdG9yVmlldy5kZWNvcmF0aW9ucy5mcm9tKGYpLAp9KTsKCmxldCB2aWV3ID0gbmV3IEVkaXRvclZpZXcoewogIGRvYzogIiMgSGVsbG8gV29ybGRcblxuW1Rlc3RdKGh0dHBzOi8vZXhhbXBsZS5jb20pXG5cblxuIiwKICBleHRlbnNpb25zOiBbZGVjb3JhdGlvbnNGaWVsZCwgbWFya2Rvd24oKV0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5LAp9KTsKCnZpZXcuZm9jdXMoKTs=
|
|
@@ -20,6 +24,7 @@ import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
|
20
24
|
|
|
21
25
|
export type LinkOptions = {
|
|
22
26
|
link?: boolean;
|
|
27
|
+
onHover?: (el: Element, url: string) => void;
|
|
23
28
|
onRender?: (el: Element, url: string) => void;
|
|
24
29
|
};
|
|
25
30
|
|
|
@@ -28,11 +33,62 @@ export type LinkOptions = {
|
|
|
28
33
|
* https://codemirror.net/docs/ref/#state.StateField
|
|
29
34
|
*/
|
|
30
35
|
export const link = (options: LinkOptions = {}): Extension => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
const extensions: Extension[] = [
|
|
37
|
+
StateField.define<RangeSet<any>>({
|
|
38
|
+
create: (state) => update(state, options),
|
|
39
|
+
update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
|
|
40
|
+
provide: (field) => EditorView.decorations.from(field),
|
|
41
|
+
}),
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
if (options.onHover) {
|
|
45
|
+
extensions.push(
|
|
46
|
+
// https://codemirror.net/examples/tooltip
|
|
47
|
+
// https://codemirror.net/docs/ref/#view.hoverTooltip
|
|
48
|
+
// https://github.com/codemirror/view/blob/main/src/tooltip.ts
|
|
49
|
+
hoverTooltip((view, pos) => {
|
|
50
|
+
const { from, text } = view.state.doc.lineAt(pos);
|
|
51
|
+
const p = pos - from;
|
|
52
|
+
|
|
53
|
+
let idx = 0;
|
|
54
|
+
let match;
|
|
55
|
+
do {
|
|
56
|
+
match = text.substring(idx).match(markdownLinkRegexp);
|
|
57
|
+
if (!match) {
|
|
58
|
+
match = undefined;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
idx = text.indexOf(match[0]);
|
|
63
|
+
if (p >= idx && p < idx + match[0].length) {
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
idx += match[0].length;
|
|
68
|
+
} while (true);
|
|
69
|
+
|
|
70
|
+
if (!match) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const [, , url] = match ?? [];
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
pos: idx + from,
|
|
78
|
+
end: idx + from + match[0].length,
|
|
79
|
+
above: true,
|
|
80
|
+
create: () => {
|
|
81
|
+
const el = document.createElement('div');
|
|
82
|
+
el.className = tooltipContent({}, 'pli-2 plb-1');
|
|
83
|
+
options.onHover!(el, url);
|
|
84
|
+
return { dom: el, offset: { x: 0, y: 4 } };
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return extensions;
|
|
36
92
|
};
|
|
37
93
|
|
|
38
94
|
class LinkButton extends WidgetType {
|
|
@@ -56,12 +112,12 @@ class LinkButton extends WidgetType {
|
|
|
56
112
|
}
|
|
57
113
|
|
|
58
114
|
class LinkText extends WidgetType {
|
|
59
|
-
constructor(private readonly
|
|
115
|
+
constructor(private readonly _text: string, private readonly _url?: string) {
|
|
60
116
|
super();
|
|
61
117
|
}
|
|
62
118
|
|
|
63
119
|
override eq(other: LinkText) {
|
|
64
|
-
return this.
|
|
120
|
+
return this._url === other._url;
|
|
65
121
|
}
|
|
66
122
|
|
|
67
123
|
toDOM(view: EditorView) {
|
|
@@ -76,7 +132,7 @@ class LinkText extends WidgetType {
|
|
|
76
132
|
link.onclick = () => {
|
|
77
133
|
view.dispatch({
|
|
78
134
|
selection: {
|
|
79
|
-
anchor:
|
|
135
|
+
anchor: view.posAtDOM(link),
|
|
80
136
|
},
|
|
81
137
|
});
|
|
82
138
|
};
|
|
@@ -112,7 +168,7 @@ const update = (state: EditorState, options: LinkOptions) => {
|
|
|
112
168
|
node.from,
|
|
113
169
|
node.to,
|
|
114
170
|
Decoration.replace({
|
|
115
|
-
widget: new LinkText(
|
|
171
|
+
widget: new LinkText(text, options.link ? url : undefined),
|
|
116
172
|
}),
|
|
117
173
|
);
|
|
118
174
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Extension } from '@codemirror/state';
|
|
6
|
+
import { EditorView } from '@codemirror/view';
|
|
7
|
+
|
|
8
|
+
export type ListenerOptions = {
|
|
9
|
+
onFocus?: (focused: boolean) => void;
|
|
10
|
+
onChange?: (text: string) => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Event listener.
|
|
15
|
+
*/
|
|
16
|
+
export const listener = ({ onFocus, onChange }: ListenerOptions): Extension => {
|
|
17
|
+
const extensions: Extension[] = [];
|
|
18
|
+
|
|
19
|
+
onFocus &&
|
|
20
|
+
extensions.push(
|
|
21
|
+
EditorView.focusChangeEffect.of((_, focused) => {
|
|
22
|
+
onFocus(focused);
|
|
23
|
+
return null;
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
onChange &&
|
|
28
|
+
extensions.push(
|
|
29
|
+
EditorView.updateListener.of((update) => {
|
|
30
|
+
onChange(update.state.doc.toString());
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return extensions;
|
|
35
|
+
};
|
|
@@ -19,7 +19,7 @@ const defaultItems = ['hello world!', 'this is a test.', 'this is [DXOS](https:/
|
|
|
19
19
|
*/
|
|
20
20
|
export const typewriter = ({ delay = 75, items = defaultItems }: DemoOptions = {}): Extension => {
|
|
21
21
|
let t: any;
|
|
22
|
-
let idx = 0;
|
|
22
|
+
let idx = 0; // TODO(burdon): Make global.
|
|
23
23
|
|
|
24
24
|
return [
|
|
25
25
|
keymap.of([
|