@dxos/react-ui-editor 0.8.1-main.ba2dec9 → 0.8.1-staging.391c573
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 +122 -114
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +123 -116
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +122 -114
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/folding.d.ts +8 -2
- package/dist/types/src/extensions/folding.d.ts.map +1 -1
- package/dist/types/src/extensions/selection.d.ts +1 -6
- package/dist/types/src/extensions/selection.d.ts.map +1 -1
- package/dist/types/src/hooks/useTextEditor.d.ts +3 -3
- package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
- package/package.json +28 -28
- package/src/TextEditor.stories.tsx +2 -10
- package/src/extensions/folding.tsx +73 -30
- package/src/extensions/selection.ts +21 -41
- package/src/hooks/useTextEditor.ts +17 -8
@@ -2,45 +2,88 @@
|
|
2
2
|
// Copyright 2024 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
-
import { codeFolding, foldGutter } from '@codemirror/language';
|
5
|
+
import { codeFolding, foldGutter, foldedRanges, foldEffect } from '@codemirror/language';
|
6
6
|
import { type Extension } from '@codemirror/state';
|
7
7
|
import { EditorView } from '@codemirror/view';
|
8
8
|
import React from 'react';
|
9
9
|
|
10
|
+
import { debounce } from '@dxos/async';
|
10
11
|
import { Icon } from '@dxos/react-ui';
|
11
12
|
|
13
|
+
import { documentId } from './selection';
|
12
14
|
import { createElement, renderRoot } from '../util';
|
13
15
|
|
14
|
-
export type
|
16
|
+
export type FoldRange = {
|
17
|
+
from: number;
|
18
|
+
to: number;
|
19
|
+
};
|
20
|
+
|
21
|
+
export type FoldState = {
|
22
|
+
foldedRanges: FoldRange[];
|
23
|
+
};
|
15
24
|
|
16
25
|
/**
|
17
26
|
* https://codemirror.net/examples/gutter
|
18
27
|
*/
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
},
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
28
|
+
export const folding = (state: Record<string, FoldState> = {}): Extension => {
|
29
|
+
const setState = (id: string, foldState: FoldState) => {
|
30
|
+
state[id] = foldState;
|
31
|
+
};
|
32
|
+
const setStateDebounced = debounce(setState, 1_000);
|
33
|
+
let initialized = false;
|
34
|
+
|
35
|
+
return [
|
36
|
+
codeFolding({
|
37
|
+
placeholderDOM: () => {
|
38
|
+
return document.createElement('span'); // Collapse content.
|
39
|
+
},
|
40
|
+
}),
|
41
|
+
foldGutter({
|
42
|
+
markerDOM: (open) => {
|
43
|
+
// TODO(burdon): Use sprite directly.
|
44
|
+
const el = createElement('div', { className: 'flex h-full items-center' });
|
45
|
+
return renderRoot(
|
46
|
+
el,
|
47
|
+
<Icon icon='ph--caret-right--regular' size={3} classNames={['mx-3 cursor-pointer', open && 'rotate-90']} />,
|
48
|
+
);
|
49
|
+
},
|
50
|
+
}),
|
51
|
+
EditorView.theme({
|
52
|
+
'.cm-foldGutter': {
|
53
|
+
opacity: 0.3,
|
54
|
+
transition: 'opacity 0.3s',
|
55
|
+
width: '32px',
|
56
|
+
},
|
57
|
+
'.cm-foldGutter:hover': {
|
58
|
+
opacity: 1,
|
59
|
+
},
|
60
|
+
}),
|
61
|
+
EditorView.updateListener.of(({ view }) => {
|
62
|
+
const id = view.state.facet(documentId);
|
63
|
+
if (!id) {
|
64
|
+
return;
|
65
|
+
}
|
66
|
+
|
67
|
+
// Handle initial state restoration only once
|
68
|
+
if (!initialized) {
|
69
|
+
initialized = true;
|
70
|
+
const foldState = state[id];
|
71
|
+
if (foldState?.foldedRanges?.length) {
|
72
|
+
view.dispatch({
|
73
|
+
effects: foldState.foldedRanges.map((range) => foldEffect.of({ from: range.from, to: range.to })),
|
74
|
+
});
|
75
|
+
}
|
76
|
+
return;
|
77
|
+
}
|
78
|
+
|
79
|
+
// Track fold changes for saving state
|
80
|
+
const decorations = foldedRanges(view.state);
|
81
|
+
const ranges: FoldRange[] = [];
|
82
|
+
decorations.between(0, view.state.doc.length, (from: number, to: number) => {
|
83
|
+
ranges.push({ from, to });
|
84
|
+
});
|
85
|
+
const foldState: FoldState = { foldedRanges: ranges };
|
86
|
+
setStateDebounced?.(id, foldState);
|
87
|
+
}),
|
88
|
+
];
|
89
|
+
};
|
@@ -6,8 +6,6 @@ import { type Extension, Transaction, type TransactionSpec } from '@codemirror/s
|
|
6
6
|
import { EditorView, keymap } from '@codemirror/view';
|
7
7
|
|
8
8
|
import { debounce } from '@dxos/async';
|
9
|
-
import { invariant } from '@dxos/invariant';
|
10
|
-
import { isNotFalsy } from '@dxos/util';
|
11
9
|
|
12
10
|
import { singleValueFacet } from '../util';
|
13
11
|
|
@@ -26,11 +24,6 @@ export type EditorSelectionState = {
|
|
26
24
|
selection?: EditorSelection;
|
27
25
|
};
|
28
26
|
|
29
|
-
export type EditorStateStore = {
|
30
|
-
setState: (id: string, state: EditorSelectionState) => void;
|
31
|
-
getState: (id: string) => EditorSelectionState | undefined;
|
32
|
-
};
|
33
|
-
|
34
27
|
const stateRestoreAnnotation = 'dxos.org/cm/state-restore';
|
35
28
|
|
36
29
|
export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSelectionState): TransactionSpec => {
|
@@ -42,23 +35,13 @@ export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSele
|
|
42
35
|
};
|
43
36
|
};
|
44
37
|
|
45
|
-
export const createEditorStateStore = (keyPrefix: string): EditorStateStore => ({
|
46
|
-
getState: (id) => {
|
47
|
-
invariant(id);
|
48
|
-
const state = localStorage.getItem(`${keyPrefix}/${id}`);
|
49
|
-
return state ? JSON.parse(state) : undefined;
|
50
|
-
},
|
51
|
-
|
52
|
-
setState: (id, state) => {
|
53
|
-
invariant(id);
|
54
|
-
localStorage.setItem(`${keyPrefix}/${id}`, JSON.stringify(state));
|
55
|
-
},
|
56
|
-
});
|
57
|
-
|
58
38
|
/**
|
59
39
|
* Track scrolling and selection state to be restored when switching to document.
|
60
40
|
*/
|
61
|
-
export const selectionState = (
|
41
|
+
export const selectionState = (state: Record<string, EditorSelectionState> = {}): Extension => {
|
42
|
+
const setState = (id: string, selectionState: EditorSelectionState) => {
|
43
|
+
state[id] = selectionState;
|
44
|
+
};
|
62
45
|
const setStateDebounced = debounce(setState!, 1_000);
|
63
46
|
|
64
47
|
return [
|
@@ -74,27 +57,24 @@ export const selectionState = ({ getState, setState }: Partial<EditorStateStore>
|
|
74
57
|
return;
|
75
58
|
}
|
76
59
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
setStateDebounced(id, { scrollTo: pos, selection: { anchor, head } });
|
83
|
-
}
|
60
|
+
const { scrollTop } = view.scrollDOM;
|
61
|
+
const pos = view.posAtCoords({ x: 0, y: scrollTop });
|
62
|
+
if (pos !== null) {
|
63
|
+
const { anchor, head } = view.state.selection.main;
|
64
|
+
setStateDebounced(id, { scrollTo: pos, selection: { anchor, head } });
|
84
65
|
}
|
85
66
|
}),
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
return true;
|
96
|
-
},
|
67
|
+
keymap.of([
|
68
|
+
{
|
69
|
+
key: 'ctrl-r', // TODO(burdon): Setting to jump back to selection.
|
70
|
+
run: (view) => {
|
71
|
+
const selection = state[view.state.facet(documentId)];
|
72
|
+
if (selection) {
|
73
|
+
view.dispatch(createEditorStateTransaction(selection));
|
74
|
+
}
|
75
|
+
return true;
|
97
76
|
},
|
98
|
-
|
99
|
-
|
77
|
+
},
|
78
|
+
]),
|
79
|
+
];
|
100
80
|
};
|
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
import { EditorState, type EditorStateConfig } from '@codemirror/state';
|
6
6
|
import { EditorView } from '@codemirror/view';
|
7
|
-
import { useFocusableGroup } from '@fluentui/react-tabster';
|
7
|
+
import { useFocusableGroup, type TabsterTypes } from '@fluentui/react-tabster';
|
8
8
|
import {
|
9
9
|
type DependencyList,
|
10
10
|
type KeyboardEventHandler,
|
@@ -19,14 +19,14 @@ import {
|
|
19
19
|
import { log } from '@dxos/log';
|
20
20
|
import { getProviderValue, isNotFalsy, type MaybeProvider } from '@dxos/util';
|
21
21
|
|
22
|
-
import {
|
22
|
+
import { type EditorSelection, documentId, createEditorStateTransaction, editorInputMode } from '../extensions';
|
23
23
|
import { debugDispatcher } from '../util';
|
24
24
|
|
25
25
|
export type UseTextEditor = {
|
26
26
|
// TODO(burdon): Rename.
|
27
27
|
parentRef: RefObject<HTMLDivElement>;
|
28
28
|
view?: EditorView;
|
29
|
-
focusAttributes
|
29
|
+
focusAttributes?: TabsterTypes.TabsterDOMAttribute & {
|
30
30
|
tabIndex: 0;
|
31
31
|
onKeyUp: KeyboardEventHandler<HTMLDivElement>;
|
32
32
|
};
|
@@ -55,7 +55,7 @@ export type UseTextEditorProps = Pick<EditorStateConfig, 'extensions'> & {
|
|
55
55
|
let instanceCount = 0;
|
56
56
|
|
57
57
|
/**
|
58
|
-
*
|
58
|
+
* Creates codemirror text editor.
|
59
59
|
*/
|
60
60
|
export const useTextEditor = (
|
61
61
|
props: MaybeProvider<UseTextEditorProps> = {},
|
@@ -155,9 +155,11 @@ export const useTextEditor = (
|
|
155
155
|
}
|
156
156
|
}, [autoFocus, view]);
|
157
157
|
|
158
|
-
const
|
158
|
+
const focusableGroupAttrs = useFocusableGroup({
|
159
159
|
tabBehavior: 'limited',
|
160
|
-
ignoreDefaultKeydown: {
|
160
|
+
ignoreDefaultKeydown: {
|
161
|
+
Escape: view?.state.facet(editorInputMode).noTabster,
|
162
|
+
},
|
161
163
|
});
|
162
164
|
|
163
165
|
// Focus editor on Enter (e.g., when tabbing to this component).
|
@@ -176,6 +178,13 @@ export const useTextEditor = (
|
|
176
178
|
[view],
|
177
179
|
);
|
178
180
|
|
179
|
-
|
180
|
-
|
181
|
+
return {
|
182
|
+
parentRef,
|
183
|
+
view,
|
184
|
+
focusAttributes: {
|
185
|
+
tabIndex: 0 as const,
|
186
|
+
...focusableGroupAttrs,
|
187
|
+
onKeyUp: handleKeyUp,
|
188
|
+
},
|
189
|
+
};
|
181
190
|
};
|