@dxos/react-ui-editor 0.3.11-main.8a2cfe6 → 0.3.11-main.8be6c8a
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 +61 -58
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +6 -2
- 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/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +22 -22
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +7 -4
- 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/table.ts +4 -1
- package/src/hooks/useTextModel.ts +1 -1
- package/src/index.ts +2 -0
|
@@ -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/table.ts
CHANGED
|
@@ -81,7 +81,6 @@ const update = (state: EditorState, options: TableOptions) => {
|
|
|
81
81
|
table.from,
|
|
82
82
|
table.to,
|
|
83
83
|
Decoration.replace({
|
|
84
|
-
block: true,
|
|
85
84
|
widget: new TableWidget(table),
|
|
86
85
|
}),
|
|
87
86
|
);
|
|
@@ -132,4 +131,8 @@ class TableWidget extends WidgetType {
|
|
|
132
131
|
|
|
133
132
|
return table;
|
|
134
133
|
}
|
|
134
|
+
|
|
135
|
+
override ignoreEvent(e: Event) {
|
|
136
|
+
return !/^mouse/.test(e.type);
|
|
137
|
+
}
|
|
135
138
|
}
|
|
@@ -66,7 +66,7 @@ export type UseTextModelProps = {
|
|
|
66
66
|
// TODO(wittjosiah): Factor out to common package? @dxos/react-client?
|
|
67
67
|
export const useTextModel = (props: UseTextModelProps): EditorModel | undefined => {
|
|
68
68
|
const { identity, space, text } = props;
|
|
69
|
-
const [model, setModel] = useState<EditorModel | undefined>(
|
|
69
|
+
const [model, setModel] = useState<EditorModel | undefined>();
|
|
70
70
|
useEffect(() => setModel(createModel(props)), [identity, space, text]);
|
|
71
71
|
return model;
|
|
72
72
|
};
|
package/src/index.ts
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
export { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
|
|
6
6
|
export { Doc, YText, YXmlFragment } from '@dxos/text-model';
|
|
7
7
|
export { type Extension } from '@codemirror/state';
|
|
8
|
+
export { type EditorView } from '@codemirror/view';
|
|
8
9
|
|
|
9
10
|
export * from './components';
|
|
10
11
|
export * from './extensions';
|
|
11
12
|
export * from './hooks';
|
|
12
13
|
export { getToken } from './styles';
|
|
13
14
|
export * from './themes';
|
|
15
|
+
export * from './util';
|