@dxos/react-ui-editor 0.4.8-main.6919ef3 → 0.4.8-main.8cb190e
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 +228 -321
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/types/src/components/TextEditor/TextEditor.d.ts +2 -2
- 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/TextEditor.stories.d.ts.map +1 -1
- package/dist/types/src/extensions/index.d.ts +0 -1
- package/dist/types/src/extensions/index.d.ts.map +1 -1
- package/dist/types/src/extensions/modes.d.ts +0 -1
- package/dist/types/src/extensions/modes.d.ts.map +1 -1
- package/package.json +24 -24
- package/src/components/TextEditor/TextEditor.stories.tsx +4 -16
- package/src/components/TextEditor/TextEditor.tsx +10 -11
- package/src/extensions/index.ts +0 -1
- package/src/extensions/modes.ts +1 -3
- package/dist/types/src/extensions/state.d.ts +0 -20
- package/dist/types/src/extensions/state.d.ts.map +0 -1
- package/src/extensions/state.ts +0 -90
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { type Extension } from '@codemirror/state';
|
|
2
|
-
export type SelectionState = {
|
|
3
|
-
scrollTo: {
|
|
4
|
-
from: number;
|
|
5
|
-
};
|
|
6
|
-
selection: {
|
|
7
|
-
anchor: number;
|
|
8
|
-
head?: number;
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
export type StateOptions = {
|
|
12
|
-
setState: (id: string, state: SelectionState) => void;
|
|
13
|
-
getState: (id: string) => SelectionState | undefined;
|
|
14
|
-
};
|
|
15
|
-
export declare const localStorageStateStoreAdapter: StateOptions;
|
|
16
|
-
/**
|
|
17
|
-
* Track scrolling and selection state to be restored when switching to document.
|
|
18
|
-
*/
|
|
19
|
-
export declare const state: ({ getState, setState }?: Partial<StateOptions>) => Extension;
|
|
20
|
-
//# sourceMappingURL=state.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../../src/extensions/state.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,SAAS,EAAe,MAAM,mBAAmB,CAAC;AAYhE,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,EAAE;QACR,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,SAAS,EAAE;QACT,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;IACtD,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,cAAc,GAAG,SAAS,CAAC;CACtD,CAAC;AAGF,eAAO,MAAM,6BAA6B,EAAE,YAU3C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,KAAK,4BAA4B,QAAQ,YAAY,CAAC,KAAQ,SA0C1E,CAAC"}
|
package/src/extensions/state.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { type Extension, Transaction } from '@codemirror/state';
|
|
6
|
-
import { EditorView, keymap } from '@codemirror/view';
|
|
7
|
-
|
|
8
|
-
import { debounce } from '@dxos/async';
|
|
9
|
-
import { invariant } from '@dxos/invariant';
|
|
10
|
-
import { isNotFalsy } from '@dxos/util';
|
|
11
|
-
|
|
12
|
-
import { documentId } from './doc';
|
|
13
|
-
|
|
14
|
-
const scrollAnnotation = 'dxos.org/cm/scrolling';
|
|
15
|
-
|
|
16
|
-
// NOTE: Serializable.
|
|
17
|
-
export type SelectionState = {
|
|
18
|
-
scrollTo: {
|
|
19
|
-
from: number;
|
|
20
|
-
};
|
|
21
|
-
selection: {
|
|
22
|
-
anchor: number;
|
|
23
|
-
head?: number;
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export type StateOptions = {
|
|
28
|
-
setState: (id: string, state: SelectionState) => void;
|
|
29
|
-
getState: (id: string) => SelectionState | undefined;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const keyPrefix = 'dxos.org/react-ui-editor/state';
|
|
33
|
-
export const localStorageStateStoreAdapter: StateOptions = {
|
|
34
|
-
setState: (id, state) => {
|
|
35
|
-
invariant(id);
|
|
36
|
-
localStorage.setItem(`${keyPrefix}/${id}`, JSON.stringify(state));
|
|
37
|
-
},
|
|
38
|
-
getState: (id) => {
|
|
39
|
-
invariant(id);
|
|
40
|
-
const state = localStorage.getItem(`${keyPrefix}/${id}`);
|
|
41
|
-
return state ? JSON.parse(state) : undefined;
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Track scrolling and selection state to be restored when switching to document.
|
|
47
|
-
*/
|
|
48
|
-
export const state = ({ getState, setState }: Partial<StateOptions> = {}): Extension => {
|
|
49
|
-
const setStateDebounced = debounce(setState!, 1_000);
|
|
50
|
-
|
|
51
|
-
return [
|
|
52
|
-
// TODO(burdon): Track scrolling (currently only updates when cursor moves).
|
|
53
|
-
EditorView.updateListener.of(({ view, changes, transactions }) => {
|
|
54
|
-
// TODO(burdon): Don't react to initial scroll.
|
|
55
|
-
const id = view.state.facet(documentId);
|
|
56
|
-
if (!id || transactions.some((tr) => tr.isUserEvent(scrollAnnotation))) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (setState) {
|
|
61
|
-
const { top } = view.dom.getBoundingClientRect();
|
|
62
|
-
const pos = view.posAtCoords({ x: 0, y: top });
|
|
63
|
-
if (pos !== null) {
|
|
64
|
-
const { anchor, head } = view.state.selection.main;
|
|
65
|
-
setStateDebounced(id, {
|
|
66
|
-
scrollTo: { from: pos, yMargin: 0 },
|
|
67
|
-
selection: { anchor, head },
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}),
|
|
72
|
-
getState &&
|
|
73
|
-
keymap.of([
|
|
74
|
-
{
|
|
75
|
-
key: 'ctrl-r', // TODO(burdon): Setting to jump back to bookmark.
|
|
76
|
-
run: (view) => {
|
|
77
|
-
const state = getState(view.state.facet(documentId));
|
|
78
|
-
if (state) {
|
|
79
|
-
view.dispatch({
|
|
80
|
-
effects: EditorView.scrollIntoView(state.scrollTo.from, { yMargin: 0 }),
|
|
81
|
-
selection: state.selection,
|
|
82
|
-
annotations: Transaction.userEvent.of(scrollAnnotation),
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
return true;
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
]),
|
|
89
|
-
].filter(isNotFalsy);
|
|
90
|
-
};
|