@dxos/react-ui-editor 0.6.12-main.15a606f → 0.6.12-main.2d19bf1

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.
Files changed (112) hide show
  1. package/dist/lib/browser/chunk-CIQSMP7K.mjs +148 -0
  2. package/dist/lib/browser/chunk-CIQSMP7K.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +226 -344
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/state/index.mjs +17 -0
  7. package/dist/lib/browser/state/index.mjs.map +7 -0
  8. package/dist/lib/node/chunk-GZWIENFM.cjs +169 -0
  9. package/dist/lib/node/chunk-GZWIENFM.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +5480 -0
  11. package/dist/lib/node/index.cjs.map +7 -0
  12. package/dist/lib/node/meta.json +1 -0
  13. package/dist/lib/node/state/index.cjs +39 -0
  14. package/dist/lib/node/state/index.cjs.map +7 -0
  15. package/dist/lib/node-esm/chunk-GP5RCZ3X.mjs +150 -0
  16. package/dist/lib/node-esm/chunk-GP5RCZ3X.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +5471 -0
  18. package/dist/lib/node-esm/index.mjs.map +7 -0
  19. package/dist/lib/node-esm/meta.json +1 -0
  20. package/dist/lib/node-esm/state/index.mjs +18 -0
  21. package/dist/lib/node-esm/state/index.mjs.map +7 -0
  22. package/dist/types/src/TextEditor.stories.d.ts +3 -0
  23. package/dist/types/src/TextEditor.stories.d.ts.map +1 -1
  24. package/dist/types/src/extensions/autocomplete.d.ts +2 -1
  25. package/dist/types/src/extensions/autocomplete.d.ts.map +1 -1
  26. package/dist/types/src/extensions/automerge/cursor.d.ts +1 -1
  27. package/dist/types/src/extensions/automerge/cursor.d.ts.map +1 -1
  28. package/dist/types/src/extensions/awareness/awareness.d.ts +2 -2
  29. package/dist/types/src/extensions/awareness/awareness.d.ts.map +1 -1
  30. package/dist/types/src/extensions/command/state.d.ts +2 -2
  31. package/dist/types/src/extensions/command/state.d.ts.map +1 -1
  32. package/dist/types/src/extensions/comments.d.ts +1 -1
  33. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  34. package/dist/types/src/extensions/debug.d.ts +2 -2
  35. package/dist/types/src/extensions/debug.d.ts.map +1 -1
  36. package/dist/types/src/extensions/folding.d.ts.map +1 -1
  37. package/dist/types/src/extensions/index.d.ts +0 -4
  38. package/dist/types/src/extensions/index.d.ts.map +1 -1
  39. package/dist/types/src/extensions/listener.d.ts +1 -0
  40. package/dist/types/src/extensions/listener.d.ts.map +1 -1
  41. package/dist/types/src/extensions/markdown/decorate.d.ts.map +1 -1
  42. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  43. package/dist/types/src/extensions/markdown/link.d.ts +1 -1
  44. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -1
  45. package/dist/types/src/extensions/markdown/styles.d.ts.map +1 -1
  46. package/dist/types/src/extensions/modes.d.ts +3 -3
  47. package/dist/types/src/extensions/modes.d.ts.map +1 -1
  48. package/dist/types/src/extensions/util/overlap.d.ts +1 -1
  49. package/dist/types/src/extensions/util/overlap.d.ts.map +1 -1
  50. package/dist/types/src/extensions/util/react.d.ts +1 -1
  51. package/dist/types/src/extensions/util/react.d.ts.map +1 -1
  52. package/dist/types/src/hooks/useTextEditor.d.ts +1 -1
  53. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  54. package/dist/types/src/index.d.ts +1 -0
  55. package/dist/types/src/index.d.ts.map +1 -1
  56. package/dist/types/src/{extensions → state}/cursor.d.ts +2 -2
  57. package/dist/types/src/state/cursor.d.ts.map +1 -0
  58. package/dist/types/src/state/doc.d.ts +5 -0
  59. package/dist/types/src/state/doc.d.ts.map +1 -0
  60. package/dist/types/src/state/index.d.ts +6 -0
  61. package/dist/types/src/state/index.d.ts.map +1 -0
  62. package/dist/types/src/{extensions → state}/state.d.ts +2 -2
  63. package/dist/types/src/state/state.d.ts.map +1 -0
  64. package/dist/types/src/state/types.d.ts.map +1 -0
  65. package/dist/types/src/state/util.d.ts +3 -0
  66. package/dist/types/src/state/util.d.ts.map +1 -0
  67. package/dist/types/src/styles/markdown.d.ts +1 -2
  68. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  69. package/dist/types/src/styles/theme.d.ts.map +1 -1
  70. package/dist/types/src/util.d.ts +6 -0
  71. package/dist/types/src/util.d.ts.map +1 -1
  72. package/package.json +45 -33
  73. package/src/TextEditor.stories.tsx +9 -5
  74. package/src/extensions/annotations.ts +1 -1
  75. package/src/extensions/autocomplete.ts +9 -8
  76. package/src/extensions/automerge/automerge.stories.tsx +1 -1
  77. package/src/extensions/automerge/automerge.ts +1 -1
  78. package/src/extensions/automerge/cursor.ts +1 -1
  79. package/src/extensions/awareness/awareness.ts +3 -5
  80. package/src/extensions/command/hint.ts +1 -1
  81. package/src/extensions/command/state.ts +3 -4
  82. package/src/extensions/comments.ts +44 -45
  83. package/src/extensions/debug.ts +2 -2
  84. package/src/extensions/folding.tsx +4 -2
  85. package/src/extensions/index.ts +0 -4
  86. package/src/extensions/listener.ts +1 -0
  87. package/src/extensions/markdown/decorate.ts +49 -6
  88. package/src/extensions/markdown/highlight.ts +0 -5
  89. package/src/extensions/markdown/link.ts +3 -2
  90. package/src/extensions/markdown/styles.ts +10 -0
  91. package/src/extensions/markdown/table.ts +3 -3
  92. package/src/extensions/modes.ts +6 -5
  93. package/src/extensions/util/overlap.ts +1 -1
  94. package/src/extensions/util/react.tsx +5 -1
  95. package/src/hooks/useTextEditor.ts +30 -31
  96. package/src/index.ts +1 -0
  97. package/src/{extensions → state}/cursor.ts +3 -6
  98. package/src/state/doc.ts +10 -0
  99. package/src/state/index.ts +11 -0
  100. package/src/{extensions → state}/state.ts +7 -3
  101. package/src/state/util.ts +13 -0
  102. package/src/styles/markdown.ts +1 -3
  103. package/src/styles/theme.ts +2 -0
  104. package/src/util.ts +10 -0
  105. package/dist/types/src/extensions/cursor.d.ts.map +0 -1
  106. package/dist/types/src/extensions/doc.d.ts +0 -6
  107. package/dist/types/src/extensions/doc.d.ts.map +0 -1
  108. package/dist/types/src/extensions/state.d.ts.map +0 -1
  109. package/dist/types/src/extensions/types.d.ts.map +0 -1
  110. package/src/extensions/doc.ts +0 -17
  111. /package/dist/types/src/{extensions → state}/types.d.ts +0 -0
  112. /package/src/{extensions → state}/types.ts +0 -0
@@ -45,7 +45,7 @@ class HorizontalRuleWidget extends WidgetType {
45
45
  class LinkButton extends WidgetType {
46
46
  constructor(
47
47
  private readonly url: string,
48
- private readonly render: (el: Element, url: string) => void,
48
+ private readonly render: (el: HTMLElement, url: string) => void,
49
49
  ) {
50
50
  super();
51
51
  }
@@ -54,6 +54,7 @@ class LinkButton extends WidgetType {
54
54
  return this.url === other.url;
55
55
  }
56
56
 
57
+ // TODO(burdon): Create icon and link directly without react?
57
58
  override toDOM(view: EditorView) {
58
59
  const el = document.createElement('span');
59
60
  this.render(el, this.url);
@@ -127,6 +128,7 @@ class TextWidget extends WidgetType {
127
128
  }
128
129
 
129
130
  const hide = Decoration.replace({});
131
+ const blockQuote = Decoration.line({ class: mx('cm-blockquote') });
130
132
  const fencedCodeLine = Decoration.line({ class: mx('cm-code cm-codeblock-line') });
131
133
  const fencedCodeLineFirst = Decoration.line({ class: mx('cm-code cm-codeblock-line', 'cm-codeblock-first') });
132
134
  const fencedCodeLineLast = Decoration.line({ class: mx('cm-code cm-codeblock-line', 'cm-codeblock-last') });
@@ -199,7 +201,7 @@ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boo
199
201
  return listLevels[listLevels.length - 1];
200
202
  };
201
203
 
202
- // const count = 0;
204
+ // let count = 0;
203
205
  const enterNode = (node: SyntaxNodeRef) => {
204
206
  // console.log(`[${count++}]`, { node: node.name, from: node.from, to: node.to });
205
207
  switch (node.name) {
@@ -329,7 +331,36 @@ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boo
329
331
  break;
330
332
  }
331
333
 
334
+ //
335
+ // Blockquote > QuoteMark > Paragraph
336
+ //
337
+
338
+ case 'Blockquote': {
339
+ const editing = editingRange(state, node, focus);
340
+ const quoteMark = node.node.getChild('QuoteMark');
341
+ const paragraph = node.node.getChild('Paragraph');
342
+ if (!editing && quoteMark && paragraph) {
343
+ atomicDeco.add(quoteMark.from, paragraph.from, hide);
344
+ }
345
+
346
+ for (const block of view.viewportLineBlocks) {
347
+ if (block.to < node.from) {
348
+ continue;
349
+ }
350
+ if (block.from > node.to) {
351
+ break;
352
+ }
353
+
354
+ deco.add(block.from, block.from, blockQuote);
355
+ }
356
+
357
+ break;
358
+ }
359
+
360
+ //
332
361
  // CommentBlock
362
+ //
363
+
333
364
  case 'CommentBlock': {
334
365
  const editing = editingRange(state, node, focus);
335
366
  for (const block of view.viewportLineBlocks) {
@@ -339,21 +370,27 @@ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boo
339
370
  if (block.from > node.to) {
340
371
  break;
341
372
  }
342
- const first = block.from <= node.from;
343
- const last = block.to >= node.to && /^(\s>)*-->$/.test(state.doc.sliceString(block.from, block.to));
373
+
374
+ const isFirst = block.from <= node.from;
375
+ const isLast = block.to >= node.to && /^(\s>)*-->$/.test(state.doc.sliceString(block.from, block.to));
376
+
344
377
  deco.add(
345
378
  block.from,
346
379
  block.from,
347
- first ? commentBlockLineFirst : last ? commentBlockLineLast : commentBlockLine,
380
+ isFirst ? commentBlockLineFirst : isLast ? commentBlockLineLast : commentBlockLine,
348
381
  );
349
- if (!editing && (first || last)) {
382
+
383
+ if (!editing && (isFirst || isLast)) {
350
384
  atomicDeco.add(block.from, block.to, hide);
351
385
  }
352
386
  }
353
387
  break;
354
388
  }
355
389
 
390
+ //
356
391
  // FencedCode > CodeMark > [CodeInfo] > CodeText > CodeMark
392
+ //
393
+
357
394
  case 'FencedCode': {
358
395
  for (const block of view.viewportLineBlocks) {
359
396
  if (block.to < node.from) {
@@ -375,7 +412,10 @@ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boo
375
412
  return false;
376
413
  }
377
414
 
415
+ //
378
416
  // Link > [LinkMark, URL]
417
+ //
418
+
379
419
  case 'Link': {
380
420
  const marks = node.node.getChildren('LinkMark');
381
421
  const urlNode = node.node.getChild('URL');
@@ -412,7 +452,10 @@ const buildDecorations = (view: EditorView, options: DecorateOptions, focus: boo
412
452
  break;
413
453
  }
414
454
 
455
+ //
415
456
  // HR
457
+ //
458
+
416
459
  case 'HorizontalRule': {
417
460
  if (!editingRange(state, node, focus)) {
418
461
  deco.add(node.from, node.to, horizontalRule);
@@ -168,11 +168,6 @@ export const markdownHighlightStyle = (_options: HighlightOptions = {}) => {
168
168
  class: theme.code,
169
169
  },
170
170
 
171
- {
172
- tag: [markdownTags.QuoteMark],
173
- class: theme.blockquote,
174
- },
175
-
176
171
  {
177
172
  tag: [markdownTags.TableCell],
178
173
  class: 'font-mono',
@@ -9,8 +9,8 @@ import { type SyntaxNode } from '@lezer/common';
9
9
 
10
10
  import { tooltipContent } from '@dxos/react-ui-theme';
11
11
 
12
- export const linkTooltip = (render: (el: Element, url: string) => void) =>
13
- hoverTooltip((view, pos, side) => {
12
+ export const linkTooltip = (render: (el: HTMLElement, url: string) => void) => {
13
+ return hoverTooltip((view, pos, side) => {
14
14
  const syntax = syntaxTree(view.state).resolveInner(pos, side);
15
15
  let link = null;
16
16
  for (let i = 0, node: SyntaxNode | null = syntax; !link && node && i < 5; node = node.parent, i++) {
@@ -35,3 +35,4 @@ export const linkTooltip = (render: (el: Element, url: string) => void) =>
35
35
  },
36
36
  };
37
37
  });
38
+ };
@@ -39,6 +39,16 @@ export const formattingStyles = EditorView.theme({
39
39
  width: `${orderedListIndentationWidth}px`,
40
40
  },
41
41
 
42
+ /**
43
+ * Blockquote.
44
+ */
45
+ '& .cm-blockquote': {
46
+ background: 'var(--dx-cmCodeblock)',
47
+ borderLeft: '2px solid var(--dx-cmSeparator)',
48
+ paddingLeft: '1rem',
49
+ margin: '0',
50
+ },
51
+
42
52
  /**
43
53
  * Code and codeblocks.
44
54
  */
@@ -25,9 +25,9 @@ export type TableOptions = {};
25
25
  * https://github.github.com/gfm/#tables-extension
26
26
  */
27
27
  export const table = (options: TableOptions = {}): Extension => {
28
- return StateField.define<RangeSet<any>>({
28
+ return StateField.define<RangeSet<Decoration>>({
29
29
  create: (state) => update(state, options),
30
- update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
30
+ update: (_: RangeSet<Decoration>, tr: Transaction) => update(tr.state, options),
31
31
  provide: (field) => EditorView.decorations.from(field),
32
32
  });
33
33
  };
@@ -40,7 +40,7 @@ type Table = {
40
40
  };
41
41
 
42
42
  const update = (state: EditorState, _options: TableOptions) => {
43
- const builder = new RangeSetBuilder();
43
+ const builder = new RangeSetBuilder<Decoration>();
44
44
  const cursor = state.selection.main.head;
45
45
 
46
46
  const tables: Table[] = [];
@@ -2,26 +2,27 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Extension, Facet } from '@codemirror/state';
5
+ import { type Extension } from '@codemirror/state';
6
6
  import { keymap } from '@codemirror/view';
7
7
  import { vim } from '@replit/codemirror-vim';
8
8
  import { vscodeKeymap } from '@replit/codemirror-vscode-keymap';
9
9
 
10
+ import { singleValueFacet } from '../state';
11
+
10
12
  export const focusEvent = 'focus.container';
11
13
 
12
14
  export const EditorViewModes = ['preview', 'readonly', 'source'] as const;
13
15
  export type EditorViewMode = (typeof EditorViewModes)[number];
16
+
14
17
  export const EditorInputModes = ['default', 'vim', 'vscode'] as const;
15
18
  export type EditorInputMode = (typeof EditorInputModes)[number];
16
19
 
17
20
  export type EditorInputConfig = {
18
- type: string;
21
+ type?: string;
19
22
  noTabster?: boolean;
20
23
  };
21
24
 
22
- export const editorInputMode = Facet.define<EditorInputConfig, EditorInputConfig>({
23
- combine: (modes) => modes[0] ?? {},
24
- });
25
+ export const editorInputMode = singleValueFacet<EditorInputConfig>({});
25
26
 
26
27
  export const InputModeExtensions: { [mode: string]: Extension } = {
27
28
  default: [],
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Range } from '../types';
5
+ import { type Range } from '../../state';
6
6
 
7
7
  /**
8
8
  * Determines if two ranges overlap.
@@ -8,6 +8,8 @@ import { createRoot } from 'react-dom/client';
8
8
  import { ThemeProvider } from '@dxos/react-ui';
9
9
  import { defaultTx } from '@dxos/react-ui-theme';
10
10
 
11
+ // TODO(burdon): Factor out.
12
+
11
13
  export type ElementOptions = {
12
14
  className?: string;
13
15
  };
@@ -23,7 +25,9 @@ export const createElement = (tag: string, options?: ElementOptions, children?:
23
25
  return el;
24
26
  };
25
27
 
26
- export const renderRoot = (root: HTMLElement, node: ReactNode): HTMLElement => {
28
+ // TODO(burdon): Remove react rendering; use DOM directly.
29
+ // NOTE: CM seems to remove/detach/overwrite portals that are attached to the DOM it control.s
30
+ export const renderRoot = <T extends Element>(root: T, node: ReactNode): T => {
27
31
  createRoot(root).render(<ThemeProvider tx={defaultTx}>{node}</ThemeProvider>);
28
32
  return root;
29
33
  };
@@ -19,10 +19,12 @@ import {
19
19
  import { log } from '@dxos/log';
20
20
  import { getProviderValue, isNotFalsy, type MaybeProvider } from '@dxos/util';
21
21
 
22
- import { createEditorStateTransaction, documentId, editorInputMode, type EditorSelection } from '../extensions';
23
- import { logChanges } from '../util';
22
+ import { editorInputMode } from '../extensions';
23
+ import { type EditorSelection, documentId, createEditorStateTransaction } from '../state';
24
+ import { debugDispatcher } from '../util';
24
25
 
25
26
  export type UseTextEditor = {
27
+ // TODO(burdon): Rename.
26
28
  parentRef: RefObject<HTMLDivElement>;
27
29
  view?: EditorView;
28
30
  focusAttributes: ReturnType<typeof useFocusableGroup> & {
@@ -65,8 +67,6 @@ export const useTextEditor = (
65
67
 
66
68
  // NOTE: Increments by 2 in strict mode.
67
69
  const [instanceId] = useState(() => `text-editor-${++instanceCount}`);
68
- // Callback once view is created.
69
- const onUpdate = useRef<() => void>();
70
70
  const [view, setView] = useState<EditorView>();
71
71
  const parentRef = useRef<HTMLDivElement>(null);
72
72
 
@@ -87,44 +87,45 @@ export const useTextEditor = (
87
87
  }
88
88
 
89
89
  // https://codemirror.net/docs/ref/#state.EditorStateConfig
90
- // NOTE: Don't set selection here in case it is invalid (and crashes the state); dispatch below.
91
90
  const state = EditorState.create({
92
91
  doc: initialValue,
93
92
  selection: initialSelection,
94
93
  extensions: [
95
94
  id && documentId.of(id),
96
- // NOTE: Doesn't catch errors in keymap functions.
95
+ extensions,
96
+ // NOTE: This doesn't catch errors in keymap functions.
97
97
  EditorView.exceptionSink.of((err) => {
98
98
  log.catch(err);
99
99
  }),
100
- extensions,
101
- EditorView.updateListener.of(() => {
102
- setTimeout(() => {
103
- onUpdate.current?.();
104
- });
105
- }),
100
+ // TODO(burdon): Factor out debug inspector.
101
+ // ViewPlugin.fromClass(
102
+ // class {
103
+ // constructor(_view: EditorView) {
104
+ // log('construct', { id });
105
+ // }
106
+ //
107
+ // destroy() {
108
+ // log('destroy', { id });
109
+ // }
110
+ // },
111
+ // ),
106
112
  ].filter(isNotFalsy),
107
113
  });
108
114
 
109
115
  // https://codemirror.net/docs/ref/#view.EditorViewConfig
110
116
  view = new EditorView({
111
117
  parent: parentRef.current,
112
- selection: initialSelection,
113
118
  state,
114
- // NOTE: Uncomment to debug/monitor all transactions.
115
- // https://codemirror.net/docs/ref/#view.EditorView.dispatch
116
- dispatchTransactions: (trs, view) => {
117
- if (debug) {
118
- logChanges(trs);
119
- }
120
- view.update(trs);
121
- },
119
+ scrollTo: scrollTo ? EditorView.scrollIntoView(scrollTo, { yMargin: 96 }) : undefined,
120
+ dispatchTransactions: debug ? debugDispatcher : undefined,
122
121
  });
123
122
 
124
- // Move to end of line after document loaded.
125
- if (!initialValue && moveToEndOfLine) {
123
+ // Move to end of line after document loaded (unless selection is specified).
124
+ if (moveToEndOfLine && !initialSelection) {
126
125
  const { to } = view.state.doc.lineAt(0);
127
- view.dispatch({ selection: { anchor: to } });
126
+ if (to) {
127
+ view.dispatch({ selection: { anchor: to } });
128
+ }
128
129
  }
129
130
 
130
131
  setView(view);
@@ -138,18 +139,16 @@ export const useTextEditor = (
138
139
 
139
140
  useEffect(() => {
140
141
  if (view) {
141
- // NOTE: Set selection after first update (since content may rerender on focus).
142
- onUpdate.current = () => {
143
- onUpdate.current = undefined;
144
- view.dispatch(createEditorStateTransaction({ scrollTo, selection }));
145
- };
146
-
147
142
  // Remove tabster attribute (rely on custom keymap).
148
143
  if (view.state.facet(editorInputMode).noTabster) {
149
144
  parentRef.current?.removeAttribute('data-tabster');
150
145
  }
146
+
147
+ if (scrollTo || selection) {
148
+ view.dispatch(createEditorStateTransaction(view.state, { scrollTo, selection }));
149
+ }
151
150
  }
152
- }, [view, selection, scrollTo]);
151
+ }, [view, scrollTo, selection]);
153
152
 
154
153
  useEffect(() => {
155
154
  if (view && autoFocus) {
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export * from './components';
14
14
  export * from './defaults';
15
15
  export * from './extensions';
16
16
  export * from './hooks';
17
+ export * from './state';
17
18
  export * from './util';
18
19
 
19
20
  export { translations };
@@ -2,9 +2,10 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type EditorState, Facet } from '@codemirror/state';
5
+ import { type EditorState } from '@codemirror/state';
6
6
 
7
7
  import { type Range } from './types';
8
+ import { singleValueFacet } from './util';
8
9
 
9
10
  /**
10
11
  * Converts indexes into the text document into stable peer-independent cursors.
@@ -27,11 +28,7 @@ const defaultCursorConverter: CursorConverter = {
27
28
  };
28
29
 
29
30
  export class Cursor {
30
- static readonly converter = Facet.define<CursorConverter, CursorConverter>({
31
- combine: (providers) => {
32
- return providers[0] ?? defaultCursorConverter;
33
- },
34
- });
31
+ static readonly converter = singleValueFacet(defaultCursorConverter);
35
32
 
36
33
  static readonly getCursorFromRange = (state: EditorState, range: Range) => {
37
34
  const cursorConverter = state.facet(Cursor.converter);
@@ -0,0 +1,10 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { singleValueFacet } from './util';
6
+
7
+ /**
8
+ * Currently edited document id as FQ string.
9
+ */
10
+ export const documentId = singleValueFacet<string>();
@@ -0,0 +1,11 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ // Non-UX exports.
6
+
7
+ export * from './cursor';
8
+ export * from './doc';
9
+ export * from './state';
10
+ export * from './types';
11
+ export * from './util';
@@ -2,7 +2,7 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type Extension, Transaction, type TransactionSpec } from '@codemirror/state';
5
+ import { type EditorState, type Extension, Transaction, type TransactionSpec } from '@codemirror/state';
6
6
  import { EditorView, keymap } from '@codemirror/view';
7
7
 
8
8
  import { debounce } from '@dxos/async';
@@ -42,7 +42,10 @@ export const localStorageStateStoreAdapter: EditorStateOptions = {
42
42
  },
43
43
  };
44
44
 
45
- export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSelectionState): TransactionSpec => {
45
+ export const createEditorStateTransaction = (
46
+ state: EditorState,
47
+ { scrollTo, selection }: EditorSelectionState,
48
+ ): TransactionSpec => {
46
49
  return {
47
50
  selection,
48
51
  scrollIntoView: !scrollTo,
@@ -54,6 +57,7 @@ export const createEditorStateTransaction = ({ scrollTo, selection }: EditorSele
54
57
  /**
55
58
  * Track scrolling and selection state to be restored when switching to document.
56
59
  */
60
+ // TODO(burdon): Rename.
57
61
  export const state = ({ getState, setState }: Partial<EditorStateOptions> = {}): Extension => {
58
62
  const setStateDebounced = debounce(setState!, 1_000);
59
63
 
@@ -86,7 +90,7 @@ export const state = ({ getState, setState }: Partial<EditorStateOptions> = {}):
86
90
  run: (view) => {
87
91
  const state = getState(view.state.facet(documentId));
88
92
  if (state) {
89
- view.dispatch(createEditorStateTransaction(state));
93
+ view.dispatch(createEditorStateTransaction(view.state, state));
90
94
  }
91
95
  return true;
92
96
  },
@@ -0,0 +1,13 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Facet } from '@codemirror/state';
6
+
7
+ export const singleValueFacet = <I, O = I>(defaultValue?: O) =>
8
+ Facet.define<I, O>({
9
+ // Called immediately.
10
+ combine: (providers) => {
11
+ return (providers[0] ?? defaultValue) as O;
12
+ },
13
+ });
@@ -17,11 +17,9 @@ const headings: Record<HeadingLevel, string> = {
17
17
  };
18
18
 
19
19
  export const theme = {
20
- mark: 'opacity-50',
21
20
  code: 'font-mono !no-underline text-neutral-700 dark:text-neutral-300',
22
21
  codeMark: 'font-mono text-primary-500',
23
- // TODO(burdon): Replace with widget.
24
- blockquote: 'pl-1 mr-1 border-is-4 border-orange-500 dark:border-orange-500 dark:text-neutral-500',
22
+ mark: 'opacity-50',
25
23
  heading: (level: HeadingLevel) => {
26
24
  return mx(headings[level], 'dark:text-primary-400');
27
25
  },
@@ -71,9 +71,11 @@ export const defaultTheme: ThemeStyles = {
71
71
  /**
72
72
  * Gutters
73
73
  * NOTE: Gutters should have the same top margin as the content.
74
+ * NOTE: They can't be transparent since the content needs to scroll below.
74
75
  */
75
76
  '.cm-gutters': {
76
77
  background: 'var(--surface-bg)',
78
+ borderRight: 'none',
77
79
  },
78
80
  '.cm-gutter': {},
79
81
  '.cm-gutter.cm-lineNumbers .cm-gutterElement': {
package/src/util.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  //
4
4
 
5
5
  import type { Transaction } from '@codemirror/state';
6
+ import { type EditorView } from '@codemirror/view';
6
7
 
7
8
  import { log } from '@dxos/log';
8
9
 
@@ -19,6 +20,15 @@ export const callbackWrapper = <T extends Function>(fn: T): T =>
19
20
  }
20
21
  }) as unknown as T;
21
22
 
23
+ /**
24
+ * Log all changes before dispatching them to the view.
25
+ * https://codemirror.net/docs/ref/#view.EditorView.dispatch
26
+ */
27
+ export const debugDispatcher = (trs: readonly Transaction[], view: EditorView) => {
28
+ logChanges(trs);
29
+ view.update(trs);
30
+ };
31
+
22
32
  export const logChanges = (trs: readonly Transaction[]) => {
23
33
  const changes = trs
24
34
  .flatMap((tr) => {
@@ -1 +0,0 @@
1
- {"version":3,"file":"cursor.d.ts","sourceRoot":"","sources":["../../../../src/extensions/cursor.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,WAAW,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,SAAS,GAAG,MAAM,CAAC;IAC/D,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;CACpC;AAOD,qBAAa,MAAM;IACjB,MAAM,CAAC,QAAQ,CAAC,SAAS,0CAItB;IAEH,MAAM,CAAC,QAAQ,CAAC,kBAAkB,UAAW,WAAW,SAAS,KAAK,YAMpE;IAEF,MAAM,CAAC,QAAQ,CAAC,kBAAkB,UAAW,WAAW,UAAU,MAAM;;;kBAOtE;CACH"}
@@ -1,6 +0,0 @@
1
- import { Facet } from '@codemirror/state';
2
- /**
3
- * Currently edited document id as FQ string.
4
- */
5
- export declare const documentId: Facet<string, string>;
6
- //# sourceMappingURL=doc.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"doc.d.ts","sourceRoot":"","sources":["../../../../src/extensions/doc.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAI1C;;GAEG;AACH,eAAO,MAAM,UAAU,uBAKrB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../../../src/extensions/state.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,SAAS,EAAe,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAWtF,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,eAAe,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,oBAAoB,KAAK,IAAI,CAAC;IAC5D,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,oBAAoB,GAAG,SAAS,CAAC;CAC5D,CAAC;AAGF,eAAO,MAAM,6BAA6B,EAAE,kBAW3C,CAAC;AAEF,eAAO,MAAM,4BAA4B,4BAA6B,oBAAoB,KAAG,eAO5F,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,KAAK,4BAA4B,OAAO,CAAC,kBAAkB,CAAC,KAAQ,SAuChF,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/extensions/types.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;CACZ,CAAC;AAIF,MAAM,MAAM,OAAO,GAAG;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC"}
@@ -1,17 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- import { Facet } from '@codemirror/state';
6
-
7
- import { invariant } from '@dxos/invariant';
8
-
9
- /**
10
- * Currently edited document id as FQ string.
11
- */
12
- export const documentId = Facet.define<string, string>({
13
- combine: (providers) => {
14
- invariant(providers.length <= 1);
15
- return providers[0];
16
- },
17
- });
File without changes
File without changes