@dxos/react-ui-editor 0.3.11-main.9c729f3 → 0.3.11-main.9d68491
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 +292 -267
- 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.map +1 -1
- package/dist/types/src/extensions/hr.d.ts.map +1 -1
- package/dist/types/src/extensions/image.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/link.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
- package/dist/types/src/extensions/tasklist.d.ts.map +1 -1
- package/dist/types/src/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/dist/types/src/themes/default.d.ts.map +1 -1
- package/package.json +22 -22
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +13 -11
- 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/code.ts +4 -5
- package/src/extensions/comments.ts +4 -5
- package/src/extensions/hr.ts +2 -3
- package/src/extensions/image.ts +38 -20
- package/src/extensions/index.ts +0 -1
- package/src/extensions/link.ts +82 -84
- package/src/extensions/markdown/highlight.ts +3 -4
- package/src/extensions/table.ts +9 -3
- package/src/extensions/tasklist.ts +1 -3
- package/src/hooks/useTextModel.ts +1 -1
- package/src/index.ts +3 -0
- package/src/styles/tokens.ts +3 -0
- package/src/themes/default.ts +39 -33
- 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,7 +8,7 @@ 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';
|
|
@@ -16,7 +16,7 @@ import { PublicKey } from '@dxos/keys';
|
|
|
16
16
|
import { fixedInsetFlexLayout, getSize, groupSurface, mx } from '@dxos/react-ui-theme';
|
|
17
17
|
import { withTheme } from '@dxos/storybook-utils';
|
|
18
18
|
|
|
19
|
-
import { MarkdownEditor, type TextEditorProps
|
|
19
|
+
import { MarkdownEditor, type TextEditorProps } from './TextEditor';
|
|
20
20
|
import {
|
|
21
21
|
autocomplete,
|
|
22
22
|
blast,
|
|
@@ -33,7 +33,6 @@ import {
|
|
|
33
33
|
typewriter,
|
|
34
34
|
type CommentsOptions,
|
|
35
35
|
type LinkOptions,
|
|
36
|
-
mermaid,
|
|
37
36
|
} from '../../extensions';
|
|
38
37
|
import { type CommentRange, useTextModel } from '../../hooks';
|
|
39
38
|
|
|
@@ -123,8 +122,6 @@ const text = {
|
|
|
123
122
|
|
|
124
123
|
paragraphs: str(...faker.helpers.multiple(() => [faker.lorem.paragraph(), ''], { count: 3 }).flat()),
|
|
125
124
|
|
|
126
|
-
mermaid: str('```mermaid', 'graph TD;', 'A-->B;', 'A-->C;', 'B-->D;', 'C-->D;', '```'),
|
|
127
|
-
|
|
128
125
|
footer: str('', '', '', '', '')
|
|
129
126
|
};
|
|
130
127
|
|
|
@@ -216,7 +213,6 @@ type StoryProps = {
|
|
|
216
213
|
} & Pick<TextEditorProps, 'comments' | 'readonly' | 'extensions' | 'slots'>;
|
|
217
214
|
|
|
218
215
|
const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
219
|
-
const ref = useRef<TextEditorRef>(null);
|
|
220
216
|
const [item] = useState({ text: new TextObject(text, undefined, undefined, { automerge }) });
|
|
221
217
|
const model = useTextModel({ text: item.text });
|
|
222
218
|
if (!model) {
|
|
@@ -227,7 +223,7 @@ const Story = ({ text, automerge, ...props }: StoryProps) => {
|
|
|
227
223
|
<div className={mx(fixedInsetFlexLayout, groupSurface)}>
|
|
228
224
|
<div className='flex justify-center overflow-y-scroll'>
|
|
229
225
|
<div className='flex flex-col w-[800px] py-16'>
|
|
230
|
-
<MarkdownEditor
|
|
226
|
+
<MarkdownEditor model={model} {...props} />
|
|
231
227
|
<div className='flex shrink-0 h-[300px]'></div>
|
|
232
228
|
</div>
|
|
233
229
|
</div>
|
|
@@ -262,6 +258,16 @@ export const Readonly = {
|
|
|
262
258
|
render: () => <Story text={document} extensions={extensions} readonly />,
|
|
263
259
|
};
|
|
264
260
|
|
|
261
|
+
const large = faker.helpers.multiple(() => faker.lorem.paragraph({ min: 8, max: 16 }), { count: 20 }).join('\n\n');
|
|
262
|
+
|
|
263
|
+
export const Large = {
|
|
264
|
+
render: () => <Story text={str('# Large Document', '', large)} extensions={[]} />,
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export const Empty = {
|
|
268
|
+
render: () => <Story />,
|
|
269
|
+
};
|
|
270
|
+
|
|
265
271
|
export const NoExtensions = {
|
|
266
272
|
render: () => <Story text={document} />,
|
|
267
273
|
};
|
|
@@ -342,10 +348,6 @@ export const Mention = {
|
|
|
342
348
|
),
|
|
343
349
|
};
|
|
344
350
|
|
|
345
|
-
export const Mermaid = {
|
|
346
|
-
render: () => <Story text={str('# Mermaid', '', text.mermaid, text.footer)} extensions={[code(), mermaid()]} />,
|
|
347
|
-
};
|
|
348
|
-
|
|
349
351
|
export const Comments = {
|
|
350
352
|
render: () => {
|
|
351
353
|
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
|
};
|
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',
|
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
WidgetType,
|
|
17
17
|
type Rect,
|
|
18
18
|
} from '@codemirror/view';
|
|
19
|
-
import get from 'lodash.get';
|
|
20
19
|
import sortBy from 'lodash.sortby';
|
|
21
20
|
|
|
22
21
|
import { debounce } from '@dxos/async';
|
|
@@ -25,7 +24,7 @@ import { log } from '@dxos/log';
|
|
|
25
24
|
import { nonNullable } from '@dxos/util';
|
|
26
25
|
|
|
27
26
|
import { type CommentRange, type EditorModel, type Range } from '../hooks';
|
|
28
|
-
import {
|
|
27
|
+
import { getToken } from '../styles';
|
|
29
28
|
import { callbackWrapper, CursorConverter } from '../util';
|
|
30
29
|
|
|
31
30
|
// TODO(burdon): Handle delete, cut, copy, and paste (separately) text that includes comment range.
|
|
@@ -43,10 +42,10 @@ const styles = EditorView.baseTheme({
|
|
|
43
42
|
backgroundColor: 'orange',
|
|
44
43
|
},
|
|
45
44
|
'& .cm-comment': {
|
|
46
|
-
backgroundColor:
|
|
45
|
+
backgroundColor: getToken('extend.colors.yellow.50'),
|
|
47
46
|
},
|
|
48
47
|
'& .cm-comment-active': {
|
|
49
|
-
backgroundColor:
|
|
48
|
+
backgroundColor: getToken('extend.colors.yellow.100'),
|
|
50
49
|
},
|
|
51
50
|
});
|
|
52
51
|
|
|
@@ -417,5 +416,5 @@ const getRangeFromCursor = (cursorConverter: CursorConverter, cursor: string) =>
|
|
|
417
416
|
const parts = cursor.split(':');
|
|
418
417
|
const from = cursorConverter.fromCursor(parts[0]);
|
|
419
418
|
const to = cursorConverter.fromCursor(parts[1]);
|
|
420
|
-
return from && to ? { from, to } : undefined;
|
|
419
|
+
return from !== undefined && to !== undefined ? { from, to } : undefined;
|
|
421
420
|
};
|
package/src/extensions/hr.ts
CHANGED
|
@@ -11,16 +11,15 @@ import {
|
|
|
11
11
|
type ViewUpdate,
|
|
12
12
|
WidgetType,
|
|
13
13
|
} from '@codemirror/view';
|
|
14
|
-
import get from 'lodash.get';
|
|
15
14
|
|
|
16
|
-
import {
|
|
15
|
+
import { getToken } from '../styles';
|
|
17
16
|
|
|
18
17
|
// TODO(burdon): Reconcile with theme.
|
|
19
18
|
const styles = EditorView.baseTheme({
|
|
20
19
|
'& .cm-hr': {
|
|
21
20
|
// TODO(burdon): ???
|
|
22
21
|
// Note that block-level decorations should not have vertical margins,
|
|
23
|
-
borderBottom: `1px solid ${
|
|
22
|
+
borderBottom: `1px solid ${getToken('extend.colors.neutral.200')}`,
|
|
24
23
|
},
|
|
25
24
|
});
|
|
26
25
|
|
package/src/extensions/image.ts
CHANGED
|
@@ -3,28 +3,45 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { syntaxTree } from '@codemirror/language';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
type Extension,
|
|
9
|
-
type RangeSet,
|
|
10
|
-
RangeSetBuilder,
|
|
11
|
-
StateField,
|
|
12
|
-
type Transaction,
|
|
13
|
-
} from '@codemirror/state';
|
|
14
|
-
import { Decoration, EditorView, WidgetType } from '@codemirror/view';
|
|
6
|
+
import { type EditorState, type Extension, StateField, type Transaction, type Range } from '@codemirror/state';
|
|
7
|
+
import { Decoration, type DecorationSet, EditorView, WidgetType } from '@codemirror/view';
|
|
15
8
|
|
|
16
9
|
export type ImageOptions = {};
|
|
17
10
|
|
|
18
11
|
export const image = (options: ImageOptions = {}): Extension => {
|
|
19
|
-
return StateField.define<
|
|
20
|
-
create: (state) =>
|
|
21
|
-
|
|
12
|
+
return StateField.define<DecorationSet>({
|
|
13
|
+
create: (state) => {
|
|
14
|
+
return Decoration.set(buildDecorations(0, state.doc.length, state));
|
|
15
|
+
},
|
|
16
|
+
update: (value: DecorationSet, tr: Transaction) => {
|
|
17
|
+
if (!tr.docChanged && !tr.selection) {
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
// Find range of changes and cursor changes.
|
|
21
|
+
const cursor = tr.state.selection.main.head;
|
|
22
|
+
const oldCursor = tr.changes.mapPos(tr.startState.selection.main.head);
|
|
23
|
+
let from = Math.min(cursor, oldCursor);
|
|
24
|
+
let to = Math.max(cursor, oldCursor);
|
|
25
|
+
tr.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
|
|
26
|
+
from = Math.min(from, fromB);
|
|
27
|
+
to = Math.max(to, toB);
|
|
28
|
+
});
|
|
29
|
+
// Expand to cover lines
|
|
30
|
+
from = tr.state.doc.lineAt(from).from;
|
|
31
|
+
to = tr.state.doc.lineAt(to).to;
|
|
32
|
+
return value.map(tr.changes).update({
|
|
33
|
+
filterFrom: from,
|
|
34
|
+
filterTo: to,
|
|
35
|
+
filter: () => false,
|
|
36
|
+
add: buildDecorations(from, to, tr.state),
|
|
37
|
+
});
|
|
38
|
+
},
|
|
22
39
|
provide: (field) => EditorView.decorations.from(field),
|
|
23
40
|
});
|
|
24
41
|
};
|
|
25
42
|
|
|
26
|
-
const
|
|
27
|
-
const
|
|
43
|
+
const buildDecorations = (from: number, to: number, state: EditorState) => {
|
|
44
|
+
const decorations: Range<Decoration>[] = [];
|
|
28
45
|
const cursor = state.selection.main.head;
|
|
29
46
|
|
|
30
47
|
syntaxTree(state).iterate({
|
|
@@ -34,20 +51,20 @@ const update = (state: EditorState, options: ImageOptions) => {
|
|
|
34
51
|
if (urlNode) {
|
|
35
52
|
const hide = state.readOnly || cursor < node.from || cursor > node.to;
|
|
36
53
|
const url = state.sliceDoc(urlNode.from, urlNode.to);
|
|
37
|
-
|
|
38
|
-
hide ? node.from : node.to,
|
|
39
|
-
node.to,
|
|
54
|
+
decorations.push(
|
|
40
55
|
Decoration.replace({
|
|
41
56
|
block: true, // Prevent cursor from entering.
|
|
42
57
|
widget: new ImageWidget(url),
|
|
43
|
-
}),
|
|
58
|
+
}).range(hide ? node.from : node.to, node.to),
|
|
44
59
|
);
|
|
45
60
|
}
|
|
46
61
|
}
|
|
47
62
|
},
|
|
63
|
+
from,
|
|
64
|
+
to,
|
|
48
65
|
});
|
|
49
66
|
|
|
50
|
-
return
|
|
67
|
+
return decorations;
|
|
51
68
|
};
|
|
52
69
|
|
|
53
70
|
class ImageWidget extends WidgetType {
|
|
@@ -55,7 +72,6 @@ class ImageWidget extends WidgetType {
|
|
|
55
72
|
super();
|
|
56
73
|
}
|
|
57
74
|
|
|
58
|
-
// TODO(burdon): Does this need to be unique for each position?
|
|
59
75
|
override eq(other: WidgetType) {
|
|
60
76
|
return this._url === (other as any as ImageWidget)._url;
|
|
61
77
|
}
|
|
@@ -64,6 +80,8 @@ class ImageWidget extends WidgetType {
|
|
|
64
80
|
const img = document.createElement('img');
|
|
65
81
|
img.setAttribute('src', this._url);
|
|
66
82
|
img.setAttribute('class', 'cm-image');
|
|
83
|
+
// Images are hidden until successfully loaded to avoid flickering effects.
|
|
84
|
+
img.onload = () => img.classList.add('cm-loaded-image');
|
|
67
85
|
return img;
|
|
68
86
|
}
|
|
69
87
|
}
|
package/src/extensions/index.ts
CHANGED