@dxos/react-ui-editor 0.8.1-staging.391c573 → 0.8.1-staging.5be625a
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 +111 -119
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +113 -120
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +111 -119
- 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/factories.d.ts +1 -0
- package/dist/types/src/extensions/factories.d.ts.map +1 -1
- package/dist/types/src/extensions/folding.d.ts +2 -8
- package/dist/types/src/extensions/folding.d.ts.map +1 -1
- package/dist/types/src/extensions/selection.d.ts +6 -1
- package/dist/types/src/extensions/selection.d.ts.map +1 -1
- package/dist/types/src/styles/theme.d.ts.map +1 -1
- package/package.json +27 -27
- package/src/TextEditor.stories.tsx +10 -2
- package/src/extensions/factories.ts +1 -0
- package/src/extensions/folding.tsx +30 -73
- package/src/extensions/selection.ts +41 -21
- package/src/styles/stack-item-content-class-names.ts +1 -1
- package/src/styles/theme.ts +1 -0
- package/src/util/debug.ts +1 -1
@@ -2,88 +2,45 @@
|
|
2
2
|
// Copyright 2024 DXOS.org
|
3
3
|
//
|
4
4
|
|
5
|
-
import { codeFolding, foldGutter
|
5
|
+
import { codeFolding, foldGutter } 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';
|
11
10
|
import { Icon } from '@dxos/react-ui';
|
12
11
|
|
13
|
-
import { documentId } from './selection';
|
14
12
|
import { createElement, renderRoot } from '../util';
|
15
13
|
|
16
|
-
export type
|
17
|
-
from: number;
|
18
|
-
to: number;
|
19
|
-
};
|
20
|
-
|
21
|
-
export type FoldState = {
|
22
|
-
foldedRanges: FoldRange[];
|
23
|
-
};
|
14
|
+
export type FoldingOptions = {};
|
24
15
|
|
25
16
|
/**
|
26
17
|
* https://codemirror.net/examples/gutter
|
27
18
|
*/
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
};
|
19
|
+
// TODO(burdon): Remember folding state (to state).
|
20
|
+
export const folding = (_props: FoldingOptions = {}): Extension => [
|
21
|
+
codeFolding({
|
22
|
+
placeholderDOM: () => {
|
23
|
+
return document.createElement('span'); // Collapse content.
|
24
|
+
},
|
25
|
+
}),
|
26
|
+
foldGutter({
|
27
|
+
markerDOM: (open) => {
|
28
|
+
// TODO(burdon): Use sprite directly.
|
29
|
+
const el = createElement('div', { className: 'flex h-full items-center' });
|
30
|
+
return renderRoot(
|
31
|
+
el,
|
32
|
+
<Icon icon='ph--caret-right--regular' size={3} classNames={['mx-3 cursor-pointer', open && 'rotate-90']} />,
|
33
|
+
);
|
34
|
+
},
|
35
|
+
}),
|
36
|
+
EditorView.theme({
|
37
|
+
'.cm-foldGutter': {
|
38
|
+
opacity: 0.3,
|
39
|
+
transition: 'opacity 0.3s',
|
40
|
+
width: '32px',
|
41
|
+
},
|
42
|
+
'.cm-foldGutter:hover': {
|
43
|
+
opacity: 1,
|
44
|
+
},
|
45
|
+
}),
|
46
|
+
];
|
@@ -6,6 +6,8 @@ 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';
|
9
11
|
|
10
12
|
import { singleValueFacet } from '../util';
|
11
13
|
|
@@ -24,6 +26,11 @@ export type EditorSelectionState = {
|
|
24
26
|
selection?: EditorSelection;
|
25
27
|
};
|
26
28
|
|
29
|
+
export type EditorStateStore = {
|
30
|
+
setState: (id: string, state: EditorSelectionState) => void;
|
31
|
+
getState: (id: string) => EditorSelectionState | undefined;
|
32
|
+
};
|
33
|
+
|
27
34
|
const stateRestoreAnnotation = 'dxos.org/cm/state-restore';
|
28
35
|
|
29
36
|
export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSelectionState): TransactionSpec => {
|
@@ -35,13 +42,23 @@ export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSele
|
|
35
42
|
};
|
36
43
|
};
|
37
44
|
|
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
|
+
|
38
58
|
/**
|
39
59
|
* Track scrolling and selection state to be restored when switching to document.
|
40
60
|
*/
|
41
|
-
export const selectionState = (
|
42
|
-
const setState = (id: string, selectionState: EditorSelectionState) => {
|
43
|
-
state[id] = selectionState;
|
44
|
-
};
|
61
|
+
export const selectionState = ({ getState, setState }: Partial<EditorStateStore> = {}): Extension => {
|
45
62
|
const setStateDebounced = debounce(setState!, 1_000);
|
46
63
|
|
47
64
|
return [
|
@@ -57,24 +74,27 @@ export const selectionState = (state: Record<string, EditorSelectionState> = {})
|
|
57
74
|
return;
|
58
75
|
}
|
59
76
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
77
|
+
if (setState) {
|
78
|
+
const { scrollTop } = view.scrollDOM;
|
79
|
+
const pos = view.posAtCoords({ x: 0, y: scrollTop });
|
80
|
+
if (pos !== null) {
|
81
|
+
const { anchor, head } = view.state.selection.main;
|
82
|
+
setStateDebounced(id, { scrollTo: pos, selection: { anchor, head } });
|
83
|
+
}
|
65
84
|
}
|
66
85
|
}),
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
86
|
+
getState &&
|
87
|
+
keymap.of([
|
88
|
+
{
|
89
|
+
key: 'ctrl-r', // TODO(burdon): Setting to jump back to selection.
|
90
|
+
run: (view) => {
|
91
|
+
const state = getState(view.state.facet(documentId));
|
92
|
+
if (state) {
|
93
|
+
view.dispatch(createEditorStateTransaction(state));
|
94
|
+
}
|
95
|
+
return true;
|
96
|
+
},
|
76
97
|
},
|
77
|
-
|
78
|
-
|
79
|
-
];
|
98
|
+
]),
|
99
|
+
].filter(isNotFalsy);
|
80
100
|
};
|
@@ -7,7 +7,7 @@ import { mx } from '@dxos/react-ui-theme';
|
|
7
7
|
export const stackItemContentEditorClassNames = (role?: string) =>
|
8
8
|
mx(
|
9
9
|
'dx-focus-ring-inset data-[toolbar=disabled]:pbs-2 attention-surface',
|
10
|
-
role === '
|
10
|
+
role === 'section' ? '[&_.cm-scroller]:overflow-hidden [&_.cm-scroller]:min-bs-24' : 'min-bs-0',
|
11
11
|
);
|
12
12
|
|
13
13
|
export const stackItemContentToolbarClassNames = (role?: string) =>
|
package/src/styles/theme.ts
CHANGED