@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.
@@ -2,88 +2,45 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { codeFolding, foldGutter, foldedRanges, foldEffect } from '@codemirror/language';
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 FoldRange = {
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
- 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
- };
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 = (state: Record<string, EditorSelectionState> = {}): Extension => {
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
- 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 } });
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
- 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;
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 === 'article' ? 'min-bs-0' : '[&_.cm-scroller]:overflow-hidden [&_.cm-scroller]:min-bs-24',
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) =>
@@ -137,6 +137,7 @@ export const defaultTheme: ThemeStyles = {
137
137
  '.cm-link': {
138
138
  textDecorationLine: 'underline',
139
139
  textDecorationThickness: '1px',
140
+ textDecorationColor: 'var(--dx-separator)',
140
141
  textUnderlineOffset: '2px',
141
142
  borderRadius: '.125rem',
142
143
  },
package/src/util/debug.ts CHANGED
@@ -59,6 +59,6 @@ export const logChanges = (trs: readonly Transaction[]) => {
59
59
  .filter(Boolean);
60
60
 
61
61
  if (changes.length) {
62
- log.info('changes', { changes });
62
+ log('changes', { changes });
63
63
  }
64
64
  };