@dxos/react-ui-editor 0.3.11-main.cb8120f → 0.3.11-main.ccc0ca3
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 +362 -45
- 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 +1 -2
- package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts +23 -0
- package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/components/Toolbar/Toolbar.d.ts +23 -7
- package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +10 -1
- package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -1
- package/dist/types/src/components/Toolbar/index.d.ts +2 -0
- package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +1 -0
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
- package/dist/types/src/extensions/basic.d.ts +1 -1
- package/dist/types/src/extensions/basic.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/markdown/bundle.d.ts +2 -2
- package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
- package/dist/types/src/extensions/markdown/formatting.d.ts +2 -0
- package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +1 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useActionHandler.d.ts +2 -2
- package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -1
- package/dist/types/src/hooks/useEditorView.d.ts +17 -0
- package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
- package/dist/types/src/hooks/useTextEditor.d.ts +36 -14
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
- package/dist/types/src/themes/default.d.ts.map +1 -1
- package/package.json +26 -25
- package/src/components/TextEditor/MarkdownEditor.stories.tsx +2 -3
- package/src/components/TextEditor/TextEditor.tsx +23 -13
- package/src/components/TextEditor/comments.stories.tsx +3 -3
- package/src/components/TextEditor/hooks.stories.tsx +106 -0
- package/src/components/Toolbar/Toolbar.stories.tsx +7 -3
- package/src/components/Toolbar/Toolbar.tsx +120 -69
- package/src/components/Toolbar/index.ts +5 -0
- package/src/components/index.ts +1 -0
- package/src/extensions/automerge/update-automerge.ts +7 -7
- package/src/extensions/basic.ts +15 -13
- package/src/extensions/comments.ts +2 -2
- package/src/extensions/markdown/bundle.ts +10 -5
- package/src/extensions/markdown/formatting.ts +31 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useActionHandler.ts +22 -5
- package/src/hooks/useEditorView.ts +29 -0
- package/src/hooks/useTextEditor.ts +143 -19
- package/src/themes/default.ts +3 -1
|
@@ -2,28 +2,152 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
EditorSelection,
|
|
7
|
+
EditorState,
|
|
8
|
+
type EditorStateConfig,
|
|
9
|
+
type Extension,
|
|
10
|
+
type StateEffect,
|
|
11
|
+
} from '@codemirror/state';
|
|
12
|
+
import { EditorView } from '@codemirror/view';
|
|
13
|
+
import { type RefObject, useEffect, useRef, useState } from 'react';
|
|
14
|
+
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { type ThemeMode } from '@dxos/react-ui';
|
|
17
|
+
import { isNotFalsy } from '@dxos/util';
|
|
18
|
+
|
|
19
|
+
import { type ThemeStyles } from '..//styles';
|
|
20
|
+
import { defaultTheme } from '../themes';
|
|
21
|
+
import { logChanges } from '../util';
|
|
22
|
+
|
|
23
|
+
export type UseTextEditorOptions = {
|
|
24
|
+
autoFocus?: boolean;
|
|
25
|
+
scrollTo?: StateEffect<any>;
|
|
26
|
+
debug?: boolean;
|
|
27
|
+
} & EditorStateConfig;
|
|
28
|
+
|
|
29
|
+
export type UseTextEditor = {
|
|
30
|
+
parentRef: RefObject<HTMLDivElement>;
|
|
31
|
+
view?: EditorView;
|
|
32
|
+
};
|
|
7
33
|
|
|
8
34
|
/**
|
|
9
|
-
* Hook for
|
|
10
|
-
*
|
|
11
|
-
* ```tsx
|
|
12
|
-
* const Test = () => {
|
|
13
|
-
* const [editorRef, editorView] = useTextEditor();
|
|
14
|
-
* useEffect(() => {
|
|
15
|
-
* editorView?.focus();
|
|
16
|
-
* }, [editorView]);
|
|
17
|
-
* return <TextEditor ref={editorRef} />;
|
|
18
|
-
* };
|
|
19
|
-
* ```
|
|
35
|
+
* Hook for creating editor.
|
|
20
36
|
*/
|
|
21
|
-
export const useTextEditor = (
|
|
22
|
-
|
|
37
|
+
export const useTextEditor = ({
|
|
38
|
+
autoFocus,
|
|
39
|
+
scrollTo,
|
|
40
|
+
debug,
|
|
41
|
+
doc,
|
|
42
|
+
selection,
|
|
43
|
+
extensions,
|
|
44
|
+
}: UseTextEditorOptions = {}): UseTextEditor => {
|
|
45
|
+
const onUpdate = useRef<() => void>();
|
|
46
|
+
const [view, setView] = useState<EditorView>();
|
|
47
|
+
const parentRef = useRef<HTMLDivElement>(null);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (parentRef.current) {
|
|
50
|
+
// https://codemirror.net/docs/ref/#state.EditorStateConfig
|
|
51
|
+
const state = EditorState.create({
|
|
52
|
+
doc,
|
|
53
|
+
selection,
|
|
54
|
+
extensions: [
|
|
55
|
+
// TODO(burdon): Doesn't catch errors in keymap functions.
|
|
56
|
+
EditorView.exceptionSink.of((err) => {
|
|
57
|
+
log.catch(err);
|
|
58
|
+
}),
|
|
59
|
+
extensions,
|
|
60
|
+
EditorView.updateListener.of(() => {
|
|
61
|
+
onUpdate.current?.();
|
|
62
|
+
}),
|
|
63
|
+
].filter(isNotFalsy),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// https://codemirror.net/docs/ref/#view.EditorViewConfig
|
|
67
|
+
const view = new EditorView({
|
|
68
|
+
parent: parentRef.current,
|
|
69
|
+
scrollTo,
|
|
70
|
+
state,
|
|
71
|
+
// NOTE: Uncomment to debug/monitor all transactions.
|
|
72
|
+
// https://codemirror.net/docs/ref/#view.EditorView.dispatch
|
|
73
|
+
dispatchTransactions: (trs, view) => {
|
|
74
|
+
if (debug) {
|
|
75
|
+
logChanges(trs);
|
|
76
|
+
}
|
|
77
|
+
view.update(trs);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
setView(view);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return () => {
|
|
85
|
+
view?.destroy();
|
|
86
|
+
};
|
|
87
|
+
}, [parentRef]);
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (view) {
|
|
91
|
+
// Select end of line if not specified.
|
|
92
|
+
if (!selection && !view.state.selection.main.anchor) {
|
|
93
|
+
selection = EditorSelection.single(view.state.doc.line(1).to);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Set selection after first update (since content may rerender on focus).
|
|
97
|
+
// TODO(burdon): BUG on first render may appear in middle of formatted heading.
|
|
98
|
+
// TODO(burdon): Make invisible until first render?
|
|
99
|
+
if (selection || scrollTo) {
|
|
100
|
+
onUpdate.current = () => {
|
|
101
|
+
onUpdate.current = undefined;
|
|
102
|
+
view.dispatch({ selection, effects: scrollTo && [scrollTo], scrollIntoView: !scrollTo });
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (autoFocus) {
|
|
107
|
+
view.focus();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}, [view, autoFocus, selection, scrollTo]);
|
|
111
|
+
|
|
112
|
+
return { parentRef, view };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// TODO(burdon): Factor out extension factories.
|
|
116
|
+
|
|
117
|
+
export type DataExtensionsOptions = {
|
|
118
|
+
readonly?: boolean;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// TODO(burdon): Pass in TextObject (remove model).
|
|
122
|
+
export const createDataExtensions = ({ readonly = false } = {}): Extension => {
|
|
23
123
|
return [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
view,
|
|
124
|
+
//
|
|
125
|
+
EditorState.readOnly.of(readonly),
|
|
126
|
+
EditorView.editable.of(!readonly),
|
|
28
127
|
];
|
|
29
128
|
};
|
|
129
|
+
|
|
130
|
+
export type ThemeExtensionsOptions = {
|
|
131
|
+
theme?: ThemeStyles;
|
|
132
|
+
themeMode?: ThemeMode;
|
|
133
|
+
lineWrap?: boolean;
|
|
134
|
+
slots?: {
|
|
135
|
+
editor?: {
|
|
136
|
+
className?: string;
|
|
137
|
+
};
|
|
138
|
+
content?: {
|
|
139
|
+
className?: string;
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const createThemeExtensions = ({ theme, themeMode, slots }: ThemeExtensionsOptions = {}): Extension => {
|
|
145
|
+
return [
|
|
146
|
+
//
|
|
147
|
+
EditorView.baseTheme(defaultTheme),
|
|
148
|
+
theme && EditorView.theme(theme),
|
|
149
|
+
EditorView.darkTheme.of(themeMode === 'dark'),
|
|
150
|
+
EditorView.editorAttributes.of({ class: slots?.editor?.className ?? '' }),
|
|
151
|
+
EditorView.contentAttributes.of({ class: slots?.content?.className ?? '' }),
|
|
152
|
+
].filter(isNotFalsy);
|
|
153
|
+
};
|
package/src/themes/default.ts
CHANGED
|
@@ -49,9 +49,11 @@ export const defaultTheme: ThemeStyles = {
|
|
|
49
49
|
'&.cm-focused': {
|
|
50
50
|
outline: 'none',
|
|
51
51
|
},
|
|
52
|
+
|
|
52
53
|
'.cm-scroller': {
|
|
53
|
-
overflow: 'visible',
|
|
54
|
+
// overflow: 'visible',
|
|
54
55
|
fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
|
|
56
|
+
lineHeight: 1.4,
|
|
55
57
|
},
|
|
56
58
|
|
|
57
59
|
'.cm-content': {
|