@dxos/react-ui-editor 0.3.11-main.1d8382a → 0.3.11-main.2c83f41
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 +38 -34
- 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 +2 -6
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +10 -6
- 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/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/package.json +22 -22
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +4 -7
- package/src/components/TextEditor/TextEditor.stories.tsx +4 -3
- package/src/components/TextEditor/TextEditor.tsx +45 -51
- package/src/hooks/useTextModel.ts +1 -1
- package/src/index.ts +0 -1
|
@@ -15,7 +15,6 @@ import React, {
|
|
|
15
15
|
useEffect,
|
|
16
16
|
useImperativeHandle,
|
|
17
17
|
useState,
|
|
18
|
-
useRef,
|
|
19
18
|
} from 'react';
|
|
20
19
|
|
|
21
20
|
import { generateName } from '@dxos/display-name';
|
|
@@ -35,10 +34,15 @@ export type CursorInfo = {
|
|
|
35
34
|
to: number;
|
|
36
35
|
line: number;
|
|
37
36
|
lines: number;
|
|
38
|
-
length: number;
|
|
39
37
|
after?: string;
|
|
40
38
|
};
|
|
41
39
|
|
|
40
|
+
export type TextEditorRef = {
|
|
41
|
+
root: HTMLDivElement | null;
|
|
42
|
+
state?: EditorState;
|
|
43
|
+
view?: EditorView;
|
|
44
|
+
};
|
|
45
|
+
|
|
42
46
|
export type TextEditorSlots = {
|
|
43
47
|
root?: Omit<ComponentProps<'div'>, 'ref'>;
|
|
44
48
|
editor?: {
|
|
@@ -52,7 +56,6 @@ export type TextEditorSlots = {
|
|
|
52
56
|
|
|
53
57
|
export type TextEditorProps = {
|
|
54
58
|
model: EditorModel;
|
|
55
|
-
focus?: boolean;
|
|
56
59
|
readonly?: boolean; // TODO(burdon): Move into model.
|
|
57
60
|
comments?: CommentRange[]; // TODO(burdon): Move into extension.
|
|
58
61
|
extensions?: Extension[];
|
|
@@ -62,20 +65,17 @@ export type TextEditorProps = {
|
|
|
62
65
|
|
|
63
66
|
/**
|
|
64
67
|
* Base text editor.
|
|
68
|
+
* NOTE: Rather than adding properties, try to create extensions that can be reused.
|
|
65
69
|
*/
|
|
66
|
-
export const BaseTextEditor = forwardRef<
|
|
67
|
-
({ model,
|
|
68
|
-
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
70
|
+
export const BaseTextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
71
|
+
({ model, readonly, comments, extensions = [], editorMode, slots = defaultSlots }, forwardedRef) => {
|
|
69
72
|
const { themeMode } = useThemeContext();
|
|
70
|
-
const
|
|
71
|
-
const [
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
view.focus();
|
|
77
|
-
}
|
|
78
|
-
}, [focus, view]);
|
|
73
|
+
const tabsterDOMAttribute = useFocusableGroup({ tabBehavior: 'limited' });
|
|
74
|
+
const [root, setRoot] = useState<HTMLDivElement | null>(null);
|
|
75
|
+
const [{ state = undefined, view = undefined } = {}, setEditor] = useState<
|
|
76
|
+
{ state?: EditorState; view?: EditorView } | undefined
|
|
77
|
+
>();
|
|
78
|
+
useImperativeHandle(forwardedRef, () => ({ root, state, view }), [view, state, root]);
|
|
79
79
|
|
|
80
80
|
// TODO(burdon): Factor out as extension.
|
|
81
81
|
const { awareness, peer } = model;
|
|
@@ -99,7 +99,7 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
99
99
|
}, [view, comments]);
|
|
100
100
|
|
|
101
101
|
useEffect(() => {
|
|
102
|
-
if (!
|
|
102
|
+
if (!root) {
|
|
103
103
|
return;
|
|
104
104
|
}
|
|
105
105
|
|
|
@@ -131,35 +131,36 @@ export const BaseTextEditor = forwardRef<EditorView, 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
|
-
|
|
135
|
-
parent: rootRef.current,
|
|
134
|
+
setEditor({
|
|
136
135
|
state,
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
136
|
+
view: new EditorView({
|
|
137
|
+
state,
|
|
138
|
+
parent: root,
|
|
139
|
+
// NOTE: Uncomment to spy on all transactions.
|
|
140
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
141
|
+
// dispatch: (transaction, view) => {
|
|
142
|
+
// view.update([transaction]);
|
|
143
|
+
// },
|
|
144
|
+
}),
|
|
142
145
|
});
|
|
143
146
|
|
|
144
|
-
setView(newView);
|
|
145
147
|
return () => {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
+
view?.destroy();
|
|
149
|
+
setEditor(undefined);
|
|
148
150
|
};
|
|
149
|
-
}, [
|
|
151
|
+
}, [root, model, readonly, editorMode, themeMode]);
|
|
150
152
|
|
|
151
153
|
const handleKeyUp = useCallback(
|
|
152
154
|
(event: KeyboardEvent) => {
|
|
153
155
|
const { key, altKey, shiftKey, metaKey, ctrlKey } = event;
|
|
154
156
|
switch (key) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
// }
|
|
157
|
+
case 'Enter': {
|
|
158
|
+
view?.contentDOM.focus();
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
160
161
|
|
|
161
162
|
case 'Escape': {
|
|
162
|
-
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) &&
|
|
163
|
+
editorMode === 'vim' && (altKey || shiftKey || metaKey || ctrlKey) && root?.focus();
|
|
163
164
|
break;
|
|
164
165
|
}
|
|
165
166
|
}
|
|
@@ -170,7 +171,7 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
170
171
|
return (
|
|
171
172
|
<div
|
|
172
173
|
key={model.id}
|
|
173
|
-
ref={
|
|
174
|
+
ref={setRoot}
|
|
174
175
|
tabIndex={0}
|
|
175
176
|
{...slots?.root}
|
|
176
177
|
{...(editorMode !== 'vim' && tabsterDOMAttribute)}
|
|
@@ -180,39 +181,32 @@ export const BaseTextEditor = forwardRef<EditorView, TextEditorProps>(
|
|
|
180
181
|
},
|
|
181
182
|
);
|
|
182
183
|
|
|
183
|
-
export const TextEditor = forwardRef<
|
|
184
|
-
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
184
|
+
export const TextEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
185
|
+
({ readonly, extensions = [], slots: _slots, ...props }, forwardedRef) => {
|
|
185
186
|
const { themeMode } = useThemeContext();
|
|
186
|
-
const
|
|
187
|
+
const slots = defaultsDeep({}, _slots, defaultTextSlots);
|
|
187
188
|
return (
|
|
188
189
|
<BaseTextEditor
|
|
189
190
|
ref={forwardedRef}
|
|
190
191
|
readonly={readonly}
|
|
191
|
-
extensions={[
|
|
192
|
-
|
|
193
|
-
...extensions,
|
|
194
|
-
]}
|
|
195
|
-
slots={updatedSlots}
|
|
192
|
+
extensions={[basicBundle({ readonly, themeMode, placeholder: slots?.editor?.placeholder }), ...extensions]}
|
|
193
|
+
slots={slots}
|
|
196
194
|
{...props}
|
|
197
195
|
/>
|
|
198
196
|
);
|
|
199
197
|
},
|
|
200
198
|
);
|
|
201
199
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
({ readonly, extensions = [], slots, ...props }, forwardedRef) => {
|
|
200
|
+
export const MarkdownEditor = forwardRef<TextEditorRef, TextEditorProps>(
|
|
201
|
+
({ readonly, extensions = [], slots: _slots, ...props }, forwardedRef) => {
|
|
205
202
|
const { themeMode } = useThemeContext();
|
|
206
|
-
const
|
|
203
|
+
const slots = defaultsDeep({}, _slots, defaultMarkdownSlots);
|
|
207
204
|
return (
|
|
208
205
|
<BaseTextEditor
|
|
209
206
|
ref={forwardedRef}
|
|
210
207
|
readonly={readonly}
|
|
211
|
-
extensions={[
|
|
212
|
-
|
|
213
|
-
...extensions,
|
|
214
|
-
]}
|
|
215
|
-
slots={updatedSlots}
|
|
208
|
+
extensions={[markdownBundle({ readonly, themeMode, placeholder: slots?.editor?.placeholder }), ...extensions]}
|
|
209
|
+
slots={slots}
|
|
216
210
|
{...props}
|
|
217
211
|
/>
|
|
218
212
|
);
|
|
@@ -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>(() => createModel(props));
|
|
70
70
|
useEffect(() => setModel(createModel(props)), [identity, space, text]);
|
|
71
71
|
return model;
|
|
72
72
|
};
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
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';
|
|
9
8
|
|
|
10
9
|
export * from './components';
|
|
11
10
|
export * from './extensions';
|