@dxos/react-ui-editor 0.3.11-main.f08de7a → 0.3.11-main.f14932f
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 +58 -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 +6 -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 +51 -45
- package/src/extensions/autocomplete.ts +18 -17
- package/src/extensions/automerge/index.ts +1 -0
- 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,7 @@ export type TextEditorSlots = {
|
|
|
56
52
|
|
|
57
53
|
export type TextEditorProps = {
|
|
58
54
|
model: EditorModel;
|
|
55
|
+
focus?: boolean;
|
|
59
56
|
readonly?: boolean; // TODO(burdon): Move into model.
|
|
60
57
|
comments?: CommentRange[]; // TODO(burdon): Move into extension.
|
|
61
58
|
extensions?: Extension[];
|
|
@@ -65,17 +62,20 @@ export type TextEditorProps = {
|
|
|
65
62
|
|
|
66
63
|
/**
|
|
67
64
|
* Base text editor.
|
|
68
|
-
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
69
65
|
*/
|
|
70
|
-
export const BaseTextEditor = forwardRef<
|
|
71
|
-
({ model, readonly, comments, extensions = [], editorMode, slots = defaultSlots }, forwardedRef) => {
|
|
72
|
-
const { themeMode } = useThemeContext();
|
|
66
|
+
export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
67
|
+
({ model, focus, readonly, comments, extensions = [], editorMode, slots = defaultSlots }, forwardedRef) => {
|
|
73
68
|
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
useImperativeHandle(forwardedRef, () =>
|
|
69
|
+
const { themeMode } = useThemeContext();
|
|
70
|
+
const rootRef = useRef<HTMLDivElement>(null);
|
|
71
|
+
const [view, setView] = useState<EditorView | null>(null);
|
|
72
|
+
// NOTE: This doesn't update useRef.
|
|
73
|
+
useImperativeHandle<EditorView | null, EditorView | null>(forwardedRef, () => view, [view]);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
if (focus && view) {
|
|
76
|
+
view.focus();
|
|
77
|
+
}
|
|
78
|
+
}, [focus, view]);
|
|
79
79
|
|
|
80
80
|
// TODO(burdon): Factor out as extension.
|
|
81
81
|
const { awareness, peer } = model;
|
|
@@ -99,7 +99,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
99
99
|
}, [view, comments]);
|
|
100
100
|
|
|
101
101
|
useEffect(() => {
|
|
102
|
-
if (!
|
|
102
|
+
if (!model || !rootRef.current) {
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -131,36 +131,35 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
131
131
|
// If the new state is derived from the old state, it will likely not be visible other than the cursor resetting.
|
|
132
132
|
// Ideally this should not happen except when changing between text objects.
|
|
133
133
|
view?.destroy();
|
|
134
|
-
|
|
134
|
+
const newView = new EditorView({
|
|
135
|
+
parent: rootRef.current,
|
|
135
136
|
state,
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// dispatch: (transaction, view) => {
|
|
142
|
-
// view.update([transaction]);
|
|
143
|
-
// },
|
|
144
|
-
}),
|
|
137
|
+
// NOTE: Uncomment to spy on all transactions.
|
|
138
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
139
|
+
// dispatch: (transaction, view) => {
|
|
140
|
+
// view.update([transaction]);
|
|
141
|
+
// },
|
|
145
142
|
});
|
|
146
143
|
|
|
144
|
+
setView(newView);
|
|
147
145
|
return () => {
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
newView?.destroy();
|
|
147
|
+
setView(null);
|
|
150
148
|
};
|
|
151
|
-
}, [
|
|
149
|
+
}, [rootRef, model, readonly, editorMode, themeMode]);
|
|
152
150
|
|
|
153
151
|
const handleKeyUp = useCallback(
|
|
154
152
|
(event: KeyboardEvent) => {
|
|
155
153
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
156
154
|
switch (key) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
// TODO(burdon): ???
|
|
156
|
+
// case 'Enter': {
|
|
157
|
+
// view?.contentDOM.focus();
|
|
158
|
+
// break;
|
|
159
|
+
// }
|
|
161
160
|
|
|
162
161
|
case 'Escape': {
|
|
163
|
-
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) &&
|
|
162
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && rootRef.current?.focus();
|
|
164
163
|
break;
|
|
165
164
|
}
|
|
166
165
|
}
|
|
@@ -171,7 +170,7 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
171
170
|
return (
|
|
172
171
|
<div
|
|
173
172
|
key={model.id}
|
|
174
|
-
ref={
|
|
173
|
+
ref={rootRef}
|
|
175
174
|
tabIndex={0}
|
|
176
175
|
{...slots?.root}
|
|
177
176
|
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
@@ -181,32 +180,39 @@ export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
|
181
180
|
},
|
|
182
181
|
);
|
|
183
182
|
|
|
184
|
-
export const TextEditor = forwardRef<
|
|
185
|
-
({ readonly, extensions = [], slots
|
|
183
|
+
export const TextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
184
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
186
185
|
const { themeMode } = useThemeContext();
|
|
187
|
-
const
|
|
186
|
+
const updatedSlots = defaultsDeep({}, slots, defaultTextSlots);
|
|
188
187
|
return (
|
|
189
188
|
<BaseTextEditor
|
|
190
189
|
ref={forwardedRef}
|
|
191
190
|
readonly={readonly}
|
|
192
|
-
extensions={[
|
|
193
|
-
|
|
191
|
+
extensions={[
|
|
192
|
+
basicBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
193
|
+
...extensions,
|
|
194
|
+
]}
|
|
195
|
+
slots={updatedSlots}
|
|
194
196
|
{...props}
|
|
195
197
|
/>
|
|
196
198
|
);
|
|
197
199
|
},
|
|
198
200
|
);
|
|
199
201
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
// TODO(burdon): Remove (Just provide bundle, slots).
|
|
203
|
+
export const MarkdownEditor = forwardRef<EditorView, TextEditorProps>(
|
|
204
|
+
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
202
205
|
const { themeMode } = useThemeContext();
|
|
203
|
-
const
|
|
206
|
+
const updatedSlots = defaultsDeep({}, slots, defaultMarkdownSlots);
|
|
204
207
|
return (
|
|
205
208
|
<BaseTextEditor
|
|
206
209
|
ref={forwardedRef}
|
|
207
210
|
readonly={readonly}
|
|
208
|
-
extensions={[
|
|
209
|
-
|
|
211
|
+
extensions={[
|
|
212
|
+
markdownBundle({ readonly, themeMode, placeholder: updatedSlots?.editor?.placeholder }),
|
|
213
|
+
...extensions,
|
|
214
|
+
]}
|
|
215
|
+
slots={updatedSlots}
|
|
210
216
|
{...props}
|
|
211
217
|
/>
|
|
212
218
|
);
|
|
@@ -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
|
};
|
|
@@ -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';
|