@dxos/react-ui-editor 0.3.11-main.dafb7f2 → 0.3.11-main.e0dc42b
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 +137 -137
- 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 +10 -10
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +2 -2
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/yjs.stories.d.ts +1 -1
- package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/index.d.ts +1 -0
- package/dist/types/src/extensions/automerge/index.d.ts.map +1 -1
- package/dist/types/src/extensions/code.d.ts.map +1 -1
- package/dist/types/src/extensions/comments.d.ts +1 -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/index.d.ts +0 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/styles/tokens.d.ts +1 -0
- package/dist/types/src/styles/tokens.d.ts.map +1 -1
- package/package.json +23 -22
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +18 -15
- package/src/components/TextEditor/TextEditor.stories.tsx +3 -4
- package/src/components/TextEditor/TextEditor.tsx +59 -45
- package/src/extensions/autocomplete.ts +18 -17
- package/src/extensions/automerge/index.ts +1 -0
- package/src/extensions/awareness.ts +14 -15
- package/src/extensions/code.ts +14 -7
- package/src/extensions/comments.ts +45 -47
- package/src/extensions/hr.ts +4 -5
- package/src/extensions/image.ts +2 -2
- package/src/extensions/index.ts +0 -1
- package/src/extensions/link.ts +5 -5
- package/src/extensions/markdown/highlight.ts +3 -4
- package/src/extensions/table.ts +6 -3
- package/src/extensions/tasklist.ts +2 -2
- package/src/hooks/useTextModel.ts +1 -1
- package/src/index.ts +3 -0
- package/src/styles/tokens.ts +3 -0
- package/dist/types/src/extensions/mermaid.d.ts +0 -3
- package/dist/types/src/extensions/mermaid.d.ts.map +0 -1
- package/src/extensions/mermaid.ts +0 -11
|
@@ -8,15 +8,16 @@ import { EditorView } from '@codemirror/view';
|
|
|
8
8
|
import { faker } from '@faker-js/faker';
|
|
9
9
|
import { ArrowSquareOut } from '@phosphor-icons/react';
|
|
10
10
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
11
|
-
import React, { type FC, StrictMode,
|
|
11
|
+
import React, { type FC, StrictMode, useState } from 'react';
|
|
12
12
|
import { createRoot } from 'react-dom/client';
|
|
13
13
|
|
|
14
14
|
import { TextObject } from '@dxos/echo-schema';
|
|
15
|
+
import { keySymbols, parseShortcut } from '@dxos/keyboard';
|
|
15
16
|
import { PublicKey } from '@dxos/keys';
|
|
16
17
|
import { fixedInsetFlexLayout, getSize, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
17
18
|
import { withTheme } from '@dxos/storybook-utils';
|
|
18
19
|
|
|
19
|
-
import { MarkdownEditor, type TextEditorProps
|
|
20
|
+
import { MarkdownEditor, type TextEditorProps } from './TextEditor';
|
|
20
21
|
import {
|
|
21
22
|
autocomplete,
|
|
22
23
|
blast,
|
|
@@ -33,7 +34,6 @@ import {
|
|
|
33
34
|
typewriter,
|
|
34
35
|
type CommentsOptions,
|
|
35
36
|
type LinkOptions,
|
|
36
|
-
mermaid,
|
|
37
37
|
} from '../../extensions';
|
|
38
38
|
import { type CommentRange, useTextModel } from '../../hooks';
|
|
39
39
|
|
|
@@ -123,8 +123,6 @@ const text = {
|
|
|
123
123
|
|
|
124
124
|
paragraphs: str(...faker.helpers.multiple(() => [faker.lorem.paragraph(), ''], { count: 3 }).flat()),
|
|
125
125
|
|
|
126
|
-
mermaid: str('```mermaid', 'graph TD;', 'A-->B;', 'A-->C;', 'B-->D;', 'C-->D;', '```'),
|
|
127
|
-
|
|
128
126
|
footer: str('', '', '', '', '')
|
|
129
127
|
};
|
|
130
128
|
|
|
@@ -185,15 +183,15 @@ const Key: FC<{ char: string }> = ({ char }) => (
|
|
|
185
183
|
</span>
|
|
186
184
|
);
|
|
187
185
|
|
|
188
|
-
const onCommentsHover: CommentsOptions['onHover'] = (el) => {
|
|
186
|
+
const onCommentsHover: CommentsOptions['onHover'] = (el, shortcut) => {
|
|
189
187
|
createRoot(el).render(
|
|
190
188
|
<StrictMode>
|
|
191
189
|
<div className='flex items-center gap-2 px-2 py-2 bg-neutral-700 text-white text-xs rounded'>
|
|
192
190
|
<div>Create comment</div>
|
|
193
|
-
{/* TODO(burdon): Unify shortcuts. */}
|
|
194
191
|
<div className='flex gap-1'>
|
|
195
|
-
|
|
196
|
-
|
|
192
|
+
{keySymbols(parseShortcut(shortcut)).map((char) => (
|
|
193
|
+
<Key key={char} char={char} />
|
|
194
|
+
))}
|
|
197
195
|
</div>
|
|
198
196
|
</div>
|
|
199
197
|
</StrictMode>,
|
|
@@ -216,7 +214,6 @@ type StoryProps = {
|
|
|
216
214
|
} & Pick<TextEditorProps, 'comments' | 'readonly' | 'extensions' | 'slots'>;
|
|
217
215
|
|
|
218
216
|
const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
219
|
-
const ref = useRef<TextEditorRef>(null);
|
|
220
217
|
const [item] = useState({ text: new TextObject(text, undefined, undefined, { automerge }) });
|
|
221
218
|
const model = useTextModel({ text: item.text });
|
|
222
219
|
if (!model) {
|
|
@@ -227,7 +224,7 @@ const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
|
227
224
|
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
228
225
|
<div className='flex justify-center overflow-y-scroll'>
|
|
229
226
|
<div className='flex flex-col w-[800px] py-16'>
|
|
230
|
-
<MarkdownEditor
|
|
227
|
+
<MarkdownEditor model={model} {...props} />
|
|
231
228
|
<div className='flex shrink-0 h-[300px]'></div>
|
|
232
229
|
</div>
|
|
233
230
|
</div>
|
|
@@ -262,6 +259,16 @@ export const Readonly = {
|
|
|
262
259
|
render: () => <Story text={document} extensions={extensions} readonly />,
|
|
263
260
|
};
|
|
264
261
|
|
|
262
|
+
const large = faker.helpers.multiple(() => faker.lorem.paragraph({ min: 8, max: 16 }), { count: 20 }).join('\n\n');
|
|
263
|
+
|
|
264
|
+
export const Large = {
|
|
265
|
+
render: () => <Story text={str('# Large Document', '', large)} extensions={[]} />,
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export const Empty = {
|
|
269
|
+
render: () => <Story />,
|
|
270
|
+
};
|
|
271
|
+
|
|
265
272
|
export const NoExtensions = {
|
|
266
273
|
render: () => <Story text={document} />,
|
|
267
274
|
};
|
|
@@ -342,10 +349,6 @@ export const Mention = {
|
|
|
342
349
|
),
|
|
343
350
|
};
|
|
344
351
|
|
|
345
|
-
export const Mermaid = {
|
|
346
|
-
render: () => <Story text={str('# Mermaid', '', text.mermaid, text.footer)} extensions={[code(), mermaid()]} />,
|
|
347
|
-
};
|
|
348
|
-
|
|
349
352
|
export const Comments = {
|
|
350
353
|
render: () => {
|
|
351
354
|
const [commentsStates, setCommentStates] = useState<CommentRange[]>([]);
|
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
import '@dxosTheme';
|
|
6
6
|
|
|
7
7
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
8
|
-
import React, {
|
|
8
|
+
import React, { useState } from 'react';
|
|
9
9
|
|
|
10
10
|
import { TextObject } from '@dxos/echo-schema';
|
|
11
11
|
import { fixedInsetFlexLayout, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
12
12
|
import { withTheme } from '@dxos/storybook-utils';
|
|
13
13
|
|
|
14
|
-
import { defaultSlots, TextEditor, type TextEditorProps, type
|
|
14
|
+
import { defaultSlots, TextEditor, type TextEditorProps, type TextEditorSlots } from './TextEditor';
|
|
15
15
|
import { listener } from '../../extensions';
|
|
16
16
|
import { useTextModel } from '../../hooks';
|
|
17
17
|
import { textTheme } from '../../themes';
|
|
@@ -31,7 +31,6 @@ const Story = ({
|
|
|
31
31
|
automerge,
|
|
32
32
|
...props
|
|
33
33
|
}: { text?: string; automerge?: boolean } & Pick<TextEditorProps, 'extensions' | 'slots'>) => {
|
|
34
|
-
const ref = useRef<TextEditorRef>(null);
|
|
35
34
|
const [item] = useState({ text: new TextObject(text, undefined, undefined, { automerge }) });
|
|
36
35
|
const model = useTextModel({ text: item.text });
|
|
37
36
|
if (!model) {
|
|
@@ -42,7 +41,7 @@ const Story = ({
|
|
|
42
41
|
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
43
42
|
<div className='flex justify-center overflow-y-scroll'>
|
|
44
43
|
<div className='flex flex-col w-[800px] py-16'>
|
|
45
|
-
<TextEditor
|
|
44
|
+
<TextEditor model={model} {...props} />
|
|
46
45
|
<div className='flex shrink-0 h-[300px]'></div>
|
|
47
46
|
</div>
|
|
48
47
|
</div>
|
|
@@ -15,6 +15,7 @@ import React, {
|
|
|
15
15
|
useEffect,
|
|
16
16
|
useImperativeHandle,
|
|
17
17
|
useState,
|
|
18
|
+
useRef,
|
|
18
19
|
} from 'react';
|
|
19
20
|
|
|
20
21
|
import { generateName } from '@dxos/display-name';
|
|
@@ -34,15 +35,10 @@ export type CursorInfo = {
|
|
|
34
35
|
to: number;
|
|
35
36
|
line: number;
|
|
36
37
|
lines: number;
|
|
38
|
+
length: number;
|
|
37
39
|
after?: string;
|
|
38
40
|
};
|
|
39
41
|
|
|
40
|
-
export type TextEditorRef = {
|
|
41
|
-
root: HTMLDivElement | null;
|
|
42
|
-
state?: EditorState;
|
|
43
|
-
view?: EditorView;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
42
|
export type TextEditorSlots = {
|
|
47
43
|
root?: Omit<ComponentProps<'div'>, 'ref'>;
|
|
48
44
|
editor?: {
|
|
@@ -56,6 +52,8 @@ export type TextEditorSlots = {
|
|
|
56
52
|
|
|
57
53
|
export type TextEditorProps = {
|
|
58
54
|
model: EditorModel;
|
|
55
|
+
focus?: boolean;
|
|
56
|
+
selection?: { anchor: number; head?: number };
|
|
59
57
|
readonly?: boolean; // TODO(burdon): Move into model.
|
|
60
58
|
comments?: CommentRange[]; // TODO(burdon): Move into extension.
|
|
61
59
|
extensions?: Extension[];
|
|
@@ -65,17 +63,26 @@ export type TextEditorProps = {
|
|
|
65
63
|
|
|
66
64
|
/**
|
|
67
65
|
* Base text editor.
|
|
68
|
-
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
69
66
|
*/
|
|
70
|
-
export const BaseTextEditor = forwardRef<
|
|
71
|
-
(
|
|
72
|
-
|
|
67
|
+
export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
68
|
+
(
|
|
69
|
+
{ model, focus, selection, readonly, comments, extensions = [], editorMode, slots = defaultSlots },
|
|
70
|
+
forwardedRef,
|
|
71
|
+
) => {
|
|
73
72
|
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
>();
|
|
78
|
-
|
|
73
|
+
const { themeMode } = useThemeContext();
|
|
74
|
+
|
|
75
|
+
// The editor view ref should only be used as an escape hatch.
|
|
76
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
77
|
+
const [view, setView] = useState<EditorView | null>(null);
|
|
78
|
+
useImperativeHandle<EditorView | null, EditorView | null>(forwardedRef, () => view, [view]);
|
|
79
|
+
|
|
80
|
+
// Focus.
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if (view && focus) {
|
|
83
|
+
view.focus();
|
|
84
|
+
}
|
|
85
|
+
}, [view, focus]);
|
|
79
86
|
|
|
80
87
|
// TODO(burdon): Factor out as extension.
|
|
81
88
|
const { awareness, peer } = model;
|
|
@@ -99,7 +106,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
99
106
|
}, [view, comments]);
|
|
100
107
|
|
|
101
108
|
useEffect(() => {
|
|
102
|
-
if (!
|
|
109
|
+
if (!model || !rootRef.current) {
|
|
103
110
|
return;
|
|
104
111
|
}
|
|
105
112
|
|
|
@@ -107,6 +114,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
107
114
|
// https://codemirror.net/docs/ref/#state.EditorStateConfig
|
|
108
115
|
const state = EditorState.create({
|
|
109
116
|
doc: model.text(),
|
|
117
|
+
selection,
|
|
110
118
|
extensions: [
|
|
111
119
|
readonly && EditorState.readOnly.of(readonly),
|
|
112
120
|
|
|
@@ -131,36 +139,35 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
131
139
|
// If the new state is derived from the old state, it will likely not be visible other than the cursor resetting.
|
|
132
140
|
// Ideally this should not happen except when changing between text objects.
|
|
133
141
|
view?.destroy();
|
|
134
|
-
|
|
142
|
+
const newView = new EditorView({
|
|
143
|
+
parent: rootRef.current,
|
|
135
144
|
state,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// dispatch: (transaction, view) => {
|
|
142
|
-
// view.update([transaction]);
|
|
143
|
-
// },
|
|
144
|
-
}),
|
|
145
|
+
// NOTE: Uncomment to spy on all transactions.
|
|
146
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
147
|
+
// dispatch: (transaction, view) => {
|
|
148
|
+
// view.update([transaction]);
|
|
149
|
+
// },
|
|
145
150
|
});
|
|
146
151
|
|
|
152
|
+
setView(newView);
|
|
147
153
|
return () => {
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
newView?.destroy();
|
|
155
|
+
setView(null);
|
|
150
156
|
};
|
|
151
|
-
}, [
|
|
157
|
+
}, [rootRef, model, readonly, editorMode, themeMode]);
|
|
152
158
|
|
|
153
159
|
const handleKeyUp = useCallback(
|
|
154
160
|
(event: KeyboardEvent) => {
|
|
155
161
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
156
162
|
switch (key) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
163
|
+
// TODO(burdon): ???
|
|
164
|
+
// case 'Enter': {
|
|
165
|
+
// view?.contentDOM.focus();
|
|
166
|
+
// break;
|
|
167
|
+
// }
|
|
161
168
|
|
|
162
169
|
case 'Escape': {
|
|
163
|
-
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) &&
|
|
170
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && rootRef.current?.focus();
|
|
164
171
|
break;
|
|
165
172
|
}
|
|
166
173
|
}
|
|
@@ -171,7 +178,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
171
178
|
return (
|
|
172
179
|
<div
|
|
173
180
|
key={model.id}
|
|
174
|
-
ref={
|
|
181
|
+
ref={rootRef}
|
|
175
182
|
tabIndex={0}
|
|
176
183
|
{...slots?.root}
|
|
177
184
|
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
@@ -181,32 +188,39 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
181
188
|
},
|
|
182
189
|
);
|
|
183
190
|
|
|
184
|
-
export const TextEditor = forwardRef<
|
|
185
|
-
({ readonly, extensions = [], slots
|
|
191
|
+
export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
192
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
186
193
|
const { themeMode } = useThemeContext();
|
|
187
|
-
const
|
|
194
|
+
const updatedSlots = defaultsDeep({}, slots, defaultTextSlots);
|
|
188
195
|
return (
|
|
189
196
|
<BaseTextEditor
|
|
190
197
|
ref={forwardedRef}
|
|
191
198
|
readonly={readonly}
|
|
192
|
-
extensions={[
|
|
193
|
-
|
|
199
|
+
extensions={[
|
|
200
|
+
basicBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
201
|
+
...extensions,
|
|
202
|
+
]}
|
|
203
|
+
slots={updatedSlots}
|
|
194
204
|
{...props}
|
|
195
205
|
/>
|
|
196
206
|
);
|
|
197
207
|
},
|
|
198
208
|
);
|
|
199
209
|
|
|
200
|
-
|
|
201
|
-
|
|
210
|
+
// TODO(burdon): Remove (Just provide bundle, slots).
|
|
211
|
+
export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
212
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
202
213
|
const { themeMode } = useThemeContext();
|
|
203
|
-
const
|
|
214
|
+
const updatedSlots = defaultsDeep({}, slots, defaultMarkdownSlots);
|
|
204
215
|
return (
|
|
205
216
|
<BaseTextEditor
|
|
206
217
|
ref={forwardedRef}
|
|
207
218
|
readonly={readonly}
|
|
208
|
-
extensions={[
|
|
209
|
-
|
|
219
|
+
extensions={[
|
|
220
|
+
markdownBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
221
|
+
...extensions,
|
|
222
|
+
]}
|
|
223
|
+
slots={updatedSlots}
|
|
210
224
|
{...props}
|
|
211
225
|
/>
|
|
212
226
|
);
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type CompletionContext,
|
|
14
14
|
type CompletionResult,
|
|
15
15
|
} from '@codemirror/autocomplete';
|
|
16
|
+
import { markdownLanguage } from '@codemirror/lang-markdown';
|
|
16
17
|
import { keymap } from '@codemirror/view';
|
|
17
18
|
|
|
18
19
|
export type AutocompleteResult = Completion;
|
|
@@ -28,32 +29,32 @@ export const autocomplete = ({ onSearch }: AutocompleteOptions) => {
|
|
|
28
29
|
return [
|
|
29
30
|
// https://codemirror.net/docs/ref/#view.keymap
|
|
30
31
|
// https://discuss.codemirror.net/t/how-can-i-replace-the-default-autocompletion-keymap-v6/3322
|
|
32
|
+
// TODO(burdon): Set custom keymap.
|
|
31
33
|
keymap.of(completionKeymap),
|
|
32
34
|
|
|
33
35
|
// https://codemirror.net/examples/autocompletion
|
|
34
36
|
// https://codemirror.net/docs/ref/#autocomplete.autocompletion
|
|
35
37
|
autocompletion({
|
|
36
|
-
|
|
37
|
-
// defaultKeymap: false,
|
|
38
|
+
defaultKeymap: false,
|
|
38
39
|
|
|
39
40
|
// Don't start unless key press.
|
|
40
41
|
activateOnTyping: false,
|
|
42
|
+
}),
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
],
|
|
44
|
+
// TODO(burdon): Option to create new page?
|
|
45
|
+
// TODO(burdon): Optional decoration via addToOptions
|
|
46
|
+
markdownLanguage.data.of({
|
|
47
|
+
autocomplete: (context: CompletionContext): CompletionResult | null => {
|
|
48
|
+
const match = context.matchBefore(/\w*/);
|
|
49
|
+
if (!match || (match.from === match.to && !context.explicit)) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
from: match.from,
|
|
55
|
+
options: onSearch(match.text.toLowerCase()),
|
|
56
|
+
};
|
|
57
|
+
},
|
|
57
58
|
}),
|
|
58
59
|
];
|
|
59
60
|
};
|
|
@@ -208,24 +208,22 @@ export class RemoteSelectionsDecorator implements PluginValue {
|
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
class RemoteCaretWidget extends WidgetType {
|
|
211
|
-
constructor(
|
|
211
|
+
constructor(private readonly _name: string, private readonly _color: string) {
|
|
212
212
|
super();
|
|
213
|
-
this.name = name;
|
|
214
|
-
this.color = color;
|
|
215
213
|
}
|
|
216
214
|
|
|
217
|
-
toDOM(): HTMLElement {
|
|
215
|
+
override toDOM(): HTMLElement {
|
|
218
216
|
const span = document.createElement('span');
|
|
219
217
|
span.className = 'cm-ySelectionCaret';
|
|
220
|
-
span.style.backgroundColor = this.
|
|
221
|
-
span.style.borderColor = this.
|
|
218
|
+
span.style.backgroundColor = this._color;
|
|
219
|
+
span.style.borderColor = this._color;
|
|
222
220
|
|
|
223
221
|
const dot = document.createElement('div');
|
|
224
222
|
dot.className = 'cm-ySelectionCaretDot';
|
|
225
223
|
|
|
226
224
|
const info = document.createElement('div');
|
|
227
225
|
info.className = 'cm-ySelectionInfo';
|
|
228
|
-
info.innerText = this.
|
|
226
|
+
info.innerText = this._name;
|
|
229
227
|
|
|
230
228
|
span.appendChild(document.createTextNode('\u2060'));
|
|
231
229
|
span.appendChild(dot);
|
|
@@ -236,18 +234,14 @@ class RemoteCaretWidget extends WidgetType {
|
|
|
236
234
|
return span;
|
|
237
235
|
}
|
|
238
236
|
|
|
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
237
|
override updateDOM() {
|
|
248
238
|
return false;
|
|
249
239
|
}
|
|
250
240
|
|
|
241
|
+
override eq(widget: this) {
|
|
242
|
+
return widget._color === this._color;
|
|
243
|
+
}
|
|
244
|
+
|
|
251
245
|
override get estimatedHeight() {
|
|
252
246
|
return -1;
|
|
253
247
|
}
|
|
@@ -255,6 +249,11 @@ class RemoteCaretWidget extends WidgetType {
|
|
|
255
249
|
override ignoreEvent() {
|
|
256
250
|
return true;
|
|
257
251
|
}
|
|
252
|
+
|
|
253
|
+
// TODO(burdon): Not used?
|
|
254
|
+
compare(widget: this) {
|
|
255
|
+
return widget._color === this._color;
|
|
256
|
+
}
|
|
258
257
|
}
|
|
259
258
|
|
|
260
259
|
// TODO(burdon): Rename prefix (y for yjs?)
|
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',
|
|
@@ -56,7 +55,7 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
|
|
|
56
55
|
export const code = () => {
|
|
57
56
|
const getDecorations = (view: EditorView) => {
|
|
58
57
|
const decorations: Range<Decoration>[] = [];
|
|
59
|
-
if (view.state.readOnly) {
|
|
58
|
+
if (!view.hasFocus || view.state.readOnly) {
|
|
60
59
|
const text = view.state.doc.sliceString(0);
|
|
61
60
|
const matches = text.matchAll(CODE_REGEX);
|
|
62
61
|
const blocks = view.viewportLineBlocks;
|
|
@@ -79,13 +78,21 @@ export const code = () => {
|
|
|
79
78
|
return [
|
|
80
79
|
ViewPlugin.fromClass(
|
|
81
80
|
class {
|
|
81
|
+
hasFocus = false;
|
|
82
82
|
decorations: DecorationSet;
|
|
83
83
|
constructor(view: EditorView) {
|
|
84
84
|
this.decorations = getDecorations(view);
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
update(update: ViewUpdate) {
|
|
88
|
-
if (
|
|
88
|
+
if (
|
|
89
|
+
// TODO(burdon): Generalize for other extensions.
|
|
90
|
+
this.hasFocus !== update.view.hasFocus ||
|
|
91
|
+
update.startState.readOnly !== update.view.state.readOnly ||
|
|
92
|
+
update.docChanged ||
|
|
93
|
+
update.viewportChanged
|
|
94
|
+
) {
|
|
95
|
+
this.hasFocus = update.view.hasFocus;
|
|
89
96
|
this.decorations = getDecorations(update.view);
|
|
90
97
|
}
|
|
91
98
|
}
|