@dxos/react-ui-editor 0.3.11-main.5e1976a → 0.3.11-main.604a65a

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 (114) hide show
  1. package/dist/lib/browser/index.mjs +1245 -654
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts +1 -2
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -1
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  7. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +1 -1
  8. package/dist/types/src/components/TextEditor/automerge.stories.d.ts.map +1 -1
  9. package/dist/types/src/components/TextEditor/hooks.stories.d.ts +23 -0
  10. package/dist/types/src/components/TextEditor/hooks.stories.d.ts.map +1 -0
  11. package/dist/types/src/components/Toolbar/Toolbar.d.ts +25 -0
  12. package/dist/types/src/components/Toolbar/Toolbar.d.ts.map +1 -0
  13. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts +23 -0
  14. package/dist/types/src/components/Toolbar/Toolbar.stories.d.ts.map +1 -0
  15. package/dist/types/src/components/Toolbar/index.d.ts +2 -0
  16. package/dist/types/src/components/Toolbar/index.d.ts.map +1 -0
  17. package/dist/types/src/components/index.d.ts +1 -0
  18. package/dist/types/src/components/index.d.ts.map +1 -1
  19. package/dist/types/src/extensions/automerge/automerge.d.ts.map +1 -1
  20. package/dist/types/src/extensions/automerge/automerge.spec.d.ts +2 -0
  21. package/dist/types/src/extensions/automerge/automerge.spec.d.ts.map +1 -0
  22. package/dist/types/src/extensions/automerge/automerge.test.d.ts +2 -0
  23. package/dist/types/src/extensions/automerge/automerge.test.d.ts.map +1 -0
  24. package/dist/types/src/extensions/automerge/defs.d.ts +5 -5
  25. package/dist/types/src/extensions/automerge/defs.d.ts.map +1 -1
  26. package/dist/types/src/extensions/automerge/semaphore.d.ts +2 -1
  27. package/dist/types/src/extensions/automerge/semaphore.d.ts.map +1 -1
  28. package/dist/types/src/extensions/automerge/update-automerge.d.ts.map +1 -1
  29. package/dist/types/src/extensions/awareness.d.ts +1 -1
  30. package/dist/types/src/extensions/basic.d.ts +1 -1
  31. package/dist/types/src/extensions/basic.d.ts.map +1 -1
  32. package/dist/types/src/extensions/comments.d.ts +6 -2
  33. package/dist/types/src/extensions/comments.d.ts.map +1 -1
  34. package/dist/types/src/extensions/index.d.ts +0 -6
  35. package/dist/types/src/extensions/index.d.ts.map +1 -1
  36. package/dist/types/src/extensions/markdown/bundle.d.ts +2 -2
  37. package/dist/types/src/extensions/markdown/bundle.d.ts.map +1 -1
  38. package/dist/types/src/extensions/markdown/code.d.ts.map +1 -0
  39. package/dist/types/src/extensions/markdown/formatting.d.ts +13 -0
  40. package/dist/types/src/extensions/markdown/formatting.d.ts.map +1 -0
  41. package/dist/types/src/extensions/markdown/heading.d.ts +6 -0
  42. package/dist/types/src/extensions/markdown/heading.d.ts.map +1 -0
  43. package/dist/types/src/extensions/markdown/highlight.d.ts.map +1 -1
  44. package/dist/types/src/extensions/markdown/hr.d.ts.map +1 -0
  45. package/dist/types/src/extensions/markdown/image.d.ts.map +1 -0
  46. package/dist/types/src/extensions/markdown/index.d.ts +8 -0
  47. package/dist/types/src/extensions/markdown/index.d.ts.map +1 -1
  48. package/dist/types/src/extensions/markdown/link.d.ts.map +1 -0
  49. package/dist/types/src/extensions/markdown/table.d.ts.map +1 -0
  50. package/dist/types/src/extensions/markdown/tasklist.d.ts.map +1 -0
  51. package/dist/types/src/hooks/index.d.ts +2 -0
  52. package/dist/types/src/hooks/index.d.ts.map +1 -1
  53. package/dist/types/src/hooks/useActionHandler.d.ts +4 -0
  54. package/dist/types/src/hooks/useActionHandler.d.ts.map +1 -0
  55. package/dist/types/src/hooks/useEditorView.d.ts +17 -0
  56. package/dist/types/src/hooks/useEditorView.d.ts.map +1 -0
  57. package/dist/types/src/hooks/useTextEditor.d.ts +36 -14
  58. package/dist/types/src/hooks/useTextEditor.d.ts.map +1 -1
  59. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  60. package/dist/types/src/styles/markdown.d.ts +2 -2
  61. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  62. package/package.json +39 -26
  63. package/src/components/TextEditor/MarkdownEditor.stories.tsx +14 -18
  64. package/src/components/TextEditor/TextEditor.tsx +24 -12
  65. package/src/components/TextEditor/automerge.stories.tsx +1 -2
  66. package/src/components/TextEditor/comments.stories.tsx +3 -3
  67. package/src/components/TextEditor/hooks.stories.tsx +106 -0
  68. package/src/components/Toolbar/Toolbar.stories.tsx +90 -0
  69. package/src/components/Toolbar/Toolbar.tsx +166 -0
  70. package/src/components/Toolbar/index.ts +5 -0
  71. package/src/components/index.ts +1 -0
  72. package/src/extensions/automerge/automerge.spec.tsx +76 -0
  73. package/src/extensions/automerge/automerge.test.ts +13 -0
  74. package/src/extensions/automerge/automerge.ts +26 -11
  75. package/src/extensions/automerge/cursor.ts +3 -3
  76. package/src/extensions/automerge/defs.ts +9 -9
  77. package/src/extensions/automerge/semaphore.ts +17 -19
  78. package/src/extensions/automerge/update-automerge.ts +12 -6
  79. package/src/extensions/automerge/update-codemirror.ts +5 -5
  80. package/src/extensions/awareness.ts +3 -3
  81. package/src/extensions/basic.ts +15 -13
  82. package/src/extensions/comments.ts +113 -65
  83. package/src/extensions/index.ts +0 -6
  84. package/src/extensions/markdown/bundle.ts +9 -8
  85. package/src/extensions/markdown/code.ts +150 -0
  86. package/src/extensions/markdown/formatting.ts +188 -0
  87. package/src/extensions/markdown/heading.ts +59 -0
  88. package/src/extensions/markdown/highlight.ts +9 -15
  89. package/src/extensions/{hr.ts → markdown/hr.ts} +3 -4
  90. package/src/extensions/markdown/index.ts +8 -0
  91. package/src/extensions/{link.ts → markdown/link.ts} +5 -2
  92. package/src/extensions/{tasklist.ts → markdown/tasklist.ts} +2 -3
  93. package/src/hooks/index.ts +2 -0
  94. package/src/hooks/useActionHandler.ts +62 -0
  95. package/src/hooks/useEditorView.ts +29 -0
  96. package/src/hooks/useTextEditor.ts +143 -19
  97. package/src/hooks/useTextModel.ts +11 -8
  98. package/src/styles/markdown.ts +9 -10
  99. package/src/themes/default.ts +4 -4
  100. package/dist/types/src/extensions/code.d.ts.map +0 -1
  101. package/dist/types/src/extensions/hr.d.ts.map +0 -1
  102. package/dist/types/src/extensions/image.d.ts.map +0 -1
  103. package/dist/types/src/extensions/link.d.ts.map +0 -1
  104. package/dist/types/src/extensions/table.d.ts.map +0 -1
  105. package/dist/types/src/extensions/tasklist.d.ts.map +0 -1
  106. package/src/extensions/code.ts +0 -104
  107. /package/dist/types/src/extensions/{code.d.ts → markdown/code.d.ts} +0 -0
  108. /package/dist/types/src/extensions/{hr.d.ts → markdown/hr.d.ts} +0 -0
  109. /package/dist/types/src/extensions/{image.d.ts → markdown/image.d.ts} +0 -0
  110. /package/dist/types/src/extensions/{link.d.ts → markdown/link.d.ts} +0 -0
  111. /package/dist/types/src/extensions/{table.d.ts → markdown/table.d.ts} +0 -0
  112. /package/dist/types/src/extensions/{tasklist.d.ts → markdown/tasklist.d.ts} +0 -0
  113. /package/src/extensions/{image.ts → markdown/image.ts} +0 -0
  114. /package/src/extensions/{table.ts → markdown/table.ts} +0 -0
@@ -2,28 +2,152 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { type EditorView } from '@codemirror/view';
6
- import { type RefCallback, useState } from 'react';
5
+ import {
6
+ EditorSelection,
7
+ EditorState,
8
+ type EditorStateConfig,
9
+ type Extension,
10
+ type StateEffect,
11
+ } from '@codemirror/state';
12
+ import { EditorView } from '@codemirror/view';
13
+ import { type RefObject, useEffect, useRef, useState } from 'react';
14
+
15
+ import { log } from '@dxos/log';
16
+ import { type ThemeMode } from '@dxos/react-ui';
17
+ import { isNotFalsy } from '@dxos/util';
18
+
19
+ import { type ThemeStyles } from '..//styles';
20
+ import { defaultTheme } from '../themes';
21
+ import { logChanges } from '../util';
22
+
23
+ export type UseTextEditorOptions = {
24
+ autoFocus?: boolean;
25
+ scrollTo?: StateEffect<any>;
26
+ debug?: boolean;
27
+ } & EditorStateConfig;
28
+
29
+ export type UseTextEditor = {
30
+ parentRef: RefObject<HTMLDivElement>;
31
+ view?: EditorView;
32
+ };
7
33
 
8
34
  /**
9
- * Hook for accessing the editor view.
10
- *
11
- * ```tsx
12
- * const Test = () => {
13
- * const [editorRef, editorView] = useTextEditor();
14
- * useEffect(() => {
15
- * editorView?.focus();
16
- * }, [editorView]);
17
- * return <TextEditor ref={editorRef} />;
18
- * };
19
- * ```
35
+ * Hook for creating editor.
20
36
  */
21
- export const useTextEditor = (): [RefCallback<EditorView | null>, EditorView | null] => {
22
- const [view, setView] = useState<EditorView | null>(null);
37
+ export const useTextEditor = ({
38
+ autoFocus,
39
+ scrollTo,
40
+ debug,
41
+ doc,
42
+ selection,
43
+ extensions,
44
+ }: UseTextEditorOptions = {}): UseTextEditor => {
45
+ const onUpdate = useRef<() => void>();
46
+ const [view, setView] = useState<EditorView>();
47
+ const parentRef = useRef<HTMLDivElement>(null);
48
+ useEffect(() => {
49
+ if (parentRef.current) {
50
+ // https://codemirror.net/docs/ref/#state.EditorStateConfig
51
+ const state = EditorState.create({
52
+ doc,
53
+ selection,
54
+ extensions: [
55
+ // TODO(burdon): Doesn't catch errors in keymap functions.
56
+ EditorView.exceptionSink.of((err) => {
57
+ log.catch(err);
58
+ }),
59
+ extensions,
60
+ EditorView.updateListener.of(() => {
61
+ onUpdate.current?.();
62
+ }),
63
+ ].filter(isNotFalsy),
64
+ });
65
+
66
+ // https://codemirror.net/docs/ref/#view.EditorViewConfig
67
+ const view = new EditorView({
68
+ parent: parentRef.current,
69
+ scrollTo,
70
+ state,
71
+ // NOTE: Uncomment to debug/monitor all transactions.
72
+ // https://codemirror.net/docs/ref/#view.EditorView.dispatch
73
+ dispatchTransactions: (trs, view) => {
74
+ if (debug) {
75
+ logChanges(trs);
76
+ }
77
+ view.update(trs);
78
+ },
79
+ });
80
+
81
+ setView(view);
82
+ }
83
+
84
+ return () => {
85
+ view?.destroy();
86
+ };
87
+ }, [parentRef]);
88
+
89
+ useEffect(() => {
90
+ if (view) {
91
+ // Select end of line if not specified.
92
+ if (!selection && !view.state.selection.main.anchor) {
93
+ selection = EditorSelection.single(view.state.doc.line(1).to);
94
+ }
95
+
96
+ // Set selection after first update (since content may rerender on focus).
97
+ // TODO(burdon): BUG on first render may appear in middle of formatted heading.
98
+ // TODO(burdon): Make invisible until first render?
99
+ if (selection || scrollTo) {
100
+ onUpdate.current = () => {
101
+ onUpdate.current = undefined;
102
+ view.dispatch({ selection, effects: scrollTo && [scrollTo], scrollIntoView: !scrollTo });
103
+ };
104
+ }
105
+
106
+ if (autoFocus) {
107
+ view.focus();
108
+ }
109
+ }
110
+ }, [view, autoFocus, selection, scrollTo]);
111
+
112
+ return { parentRef, view };
113
+ };
114
+
115
+ // TODO(burdon): Factor out extension factories.
116
+
117
+ export type DataExtensionsOptions = {
118
+ readonly?: boolean;
119
+ };
120
+
121
+ // TODO(burdon): Pass in TextObject (remove model).
122
+ export const createDataExtensions = ({ readonly = false } = {}): Extension => {
23
123
  return [
24
- (ref: EditorView | null) => {
25
- setView(ref);
26
- },
27
- view,
124
+ //
125
+ EditorState.readOnly.of(readonly),
126
+ EditorView.editable.of(!readonly),
28
127
  ];
29
128
  };
129
+
130
+ export type ThemeExtensionsOptions = {
131
+ theme?: ThemeStyles;
132
+ themeMode?: ThemeMode;
133
+ lineWrap?: boolean;
134
+ slots?: {
135
+ editor?: {
136
+ className?: string;
137
+ };
138
+ content?: {
139
+ className?: string;
140
+ };
141
+ };
142
+ };
143
+
144
+ export const createThemeExtensions = ({ theme, themeMode, slots }: ThemeExtensionsOptions = {}): Extension => {
145
+ return [
146
+ //
147
+ EditorView.baseTheme(defaultTheme),
148
+ theme && EditorView.theme(theme),
149
+ EditorView.darkTheme.of(themeMode === 'dark'),
150
+ EditorView.editorAttributes.of({ class: slots?.editor?.className ?? '' }),
151
+ EditorView.contentAttributes.of({ class: slots?.content?.className ?? '' }),
152
+ ].filter(isNotFalsy);
153
+ };
@@ -10,11 +10,10 @@ import { type AutomergeTextCompat, getRawDoc } from '@dxos/echo-schema';
10
10
  import { invariant } from '@dxos/invariant';
11
11
  import { isAutomergeObject, type Space, type TextObject } from '@dxos/react-client/echo';
12
12
  import { type Identity } from '@dxos/react-client/halo';
13
- import { isNotNullOrUndefined } from '@dxos/util';
14
13
 
15
14
  import { SpaceAwarenessProvider } from './awareness-provider';
16
15
  import { type EditorModel, modelState } from './defs';
17
- import { automerge, awareness, AwarenessProvider } from '../extensions';
16
+ import { automerge, awareness } from '../extensions';
18
17
  import { cursorColor } from '../styles';
19
18
 
20
19
  // TODO(burdon): Remove space/identity dependency. Define interface for the framework re content and presence.
@@ -49,16 +48,20 @@ const createModel = ({ space, identity, text }: UseTextModelProps) => {
49
48
  peerId: identity?.identityKey.toHex() ?? 'Anonymous',
50
49
  });
51
50
 
51
+ const extensions = [
52
+ //
53
+ modelState.init(() => model),
54
+ automerge({ handle: doc.handle, path: doc.path }),
55
+ ];
56
+ if (awarenessProvider) {
57
+ extensions.push(awareness(awarenessProvider));
58
+ }
59
+
52
60
  const model: EditorModel = {
53
61
  id: obj.id,
54
62
  content: doc,
55
63
  text: () => get(doc.handle.docSync(), doc.path),
56
- extension: [
57
- modelState.init(() => model),
58
- automerge({ handle: doc.handle, path: doc.path }),
59
- awarenessProvider && AwarenessProvider.of(awarenessProvider),
60
- awareness(),
61
- ].filter(isNotNullOrUndefined),
64
+ extension: extensions,
62
65
  peer: identity
63
66
  ? {
64
67
  id: identity.identityKey.toHex(),
@@ -8,18 +8,17 @@ export type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6;
8
8
 
9
9
  // TODO(burdon): Better way to align vertically than negative margin? Font-specific?
10
10
  // https://tailwindcss.com/docs/font-weight
11
- export const headings: Record<HeadingLevel, string[]> = {
12
- 1: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl', '-ml-[10px]'],
13
- 2: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl', '-ml-[8px]'],
14
- 3: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl', '-ml-[7px]'],
15
- 4: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-xl', '-ml-[6px]'],
16
- 5: ['mbs-4 mbe-2 font-medium text-inherit no-underline text-lg', '-ml-[5px]'],
17
- 6: ['mbs-4 mbe-2 font-medium text-inherit no-underline', '-ml-[4px]'],
11
+ export const headings: Record<HeadingLevel, string> = {
12
+ 1: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-4xl',
13
+ 2: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-3xl',
14
+ 3: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-2xl',
15
+ 4: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-xl',
16
+ 5: 'mbs-4 mbe-2 font-medium text-inherit no-underline text-lg',
17
+ 6: 'mbs-4 mbe-2 font-medium text-inherit no-underline',
18
18
  };
19
19
 
20
- export const heading = (level: HeadingLevel, readonly?: boolean) => {
21
- const [style, offset] = headings[level];
22
- return mx(style, readonly && offset);
20
+ export const heading = (level: HeadingLevel) => {
21
+ return headings[level];
23
22
  };
24
23
 
25
24
  export const text = 'text-neutral-800 dark:text-neutral-200';
@@ -49,9 +49,11 @@ export const defaultTheme: ThemeStyles = {
49
49
  '&.cm-focused': {
50
50
  outline: 'none',
51
51
  },
52
+
52
53
  '.cm-scroller': {
53
- overflow: 'visible',
54
+ // overflow: 'visible',
54
55
  fontFamily: get(tokens, 'fontFamily.mono', []).join(','),
56
+ lineHeight: 1.4,
55
57
  },
56
58
 
57
59
  '.cm-content': {
@@ -86,11 +88,9 @@ export const defaultTheme: ThemeStyles = {
86
88
  //
87
89
  '.cm-line': {
88
90
  paddingInline: 0,
89
- // TODO(burdon): Other values leave gaps between lines when highlighted.
90
- // lineHeight: 'normal',
91
91
  },
92
92
  '.cm-activeLine': {
93
- background: 'inherit',
93
+ background: 'transparent',
94
94
  },
95
95
 
96
96
  //
@@ -1 +0,0 @@
1
- {"version":3,"file":"code.d.ts","sourceRoot":"","sources":["../../../../src/extensions/code.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,IAAI,+CAiDhB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"hr.d.ts","sourceRoot":"","sources":["../../../../src/extensions/hr.ts"],"names":[],"mappings":"AAmDA,eAAO,MAAM,EAAE,+CAoBd,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../../src/extensions/image.ts"],"names":[],"mappings":"AAKA,OAAO,EAAoB,KAAK,SAAS,EAA4C,MAAM,mBAAmB,CAAC;AAG/G,MAAM,MAAM,YAAY,GAAG,EAAE,CAAC;AAE9B,eAAO,MAAM,KAAK,aAAa,YAAY,KAAQ,SA8BlD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../../../../src/extensions/link.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,SAAS,EAAmB,MAAM,mBAAmB,CAAC;AAkBpE,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7C,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;CAC/C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,IAAI,aAAa,WAAW,KAAQ,SAsDhD,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../../../src/extensions/table.ts"],"names":[],"mappings":"AAKA,OAAO,EAAoB,KAAK,QAAQ,EAAmB,UAAU,EAAoB,MAAM,mBAAmB,CAAC;AASnH,MAAM,MAAM,YAAY,GAAG,EAAE,CAAC;AAE9B;;;GAGG;AACH,eAAO,MAAM,KAAK,aAAa,YAAY,8BAM1C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"tasklist.d.ts","sourceRoot":"","sources":["../../../../src/extensions/tasklist.ts"],"names":[],"mappings":"AAsFA,MAAM,MAAM,eAAe,GAAG,EAAE,CAAC;AAEjC,eAAO,MAAM,QAAQ,aAAa,eAAe,4CAsBhD,CAAC"}
@@ -1,104 +0,0 @@
1
- //
2
- // Copyright 2023 DXOS.org
3
- //
4
-
5
- import { type Range } from '@codemirror/state';
6
- import {
7
- ViewPlugin,
8
- type DecorationSet,
9
- EditorView,
10
- type ViewUpdate,
11
- Decoration,
12
- type BlockInfo,
13
- } from '@codemirror/view';
14
-
15
- import { mx } from '@dxos/react-ui-theme';
16
-
17
- import { getToken } from '../styles';
18
-
19
- const CODE_REGEX = /```[\s\S]*?```/gs;
20
-
21
- // TODO(burdon): Reconcile with theme.
22
- const styles = EditorView.baseTheme({
23
- '& .cm-codeblock': {
24
- fontFamily: getToken('fontFamily.mono', []).join(','),
25
- },
26
- '&light [aria-readonly="true"] .cm-codeblock': {
27
- background: getToken('extend.colors.neutral.50'),
28
- },
29
- '&dark [aria-readonly="true"] .cm-codeblock': {
30
- background: getToken('extend.colors.neutral.850'),
31
- },
32
- '& [aria-readonly="true"] .cm-codeblock': {
33
- display: 'block',
34
- paddingInline: '4px !important',
35
- },
36
- '& [aria-readonly="true"] .cm-codeblock-first': {
37
- paddingTop: '4px !important',
38
- borderTopLeftRadius: '4px',
39
- borderTopRightRadius: '4px',
40
- },
41
- '& [aria-readonly="true"] .cm-codeblock-last': {
42
- paddingBottom: '4px !important',
43
- borderBottomLeftRadius: '4px',
44
- borderBottomRightRadius: '4px',
45
- },
46
- });
47
-
48
- // TODO(burdon): Start from EditorView.lineBlockAt(pos).
49
- const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
50
- const start = lines.findIndex((line) => line.from >= from);
51
- const end = lines.findIndex((line) => line.to >= to);
52
- return [start, end];
53
- };
54
-
55
- export const code = () => {
56
- const buildDecorations = (view: EditorView) => {
57
- const decorations: Range<Decoration>[] = [];
58
- const text = view.state.doc.sliceString(0);
59
- const matches = text.matchAll(CODE_REGEX);
60
- const blocks = view.viewportLineBlocks;
61
- for (const match of matches) {
62
- const range = getLineRange(blocks, match.index!, match.index! + match[0].length);
63
- for (let i = range[0]; i <= range[1]; i++) {
64
- const block = blocks[i];
65
- decorations.push(
66
- Decoration.line({
67
- class: mx('cm-codeblock', i === range[0] && 'cm-codeblock-first', i === range[1] && 'cm-codeblock-last'),
68
- }).range(block.from),
69
- );
70
- }
71
- }
72
-
73
- return Decoration.set(decorations);
74
- };
75
-
76
- return [
77
- ViewPlugin.fromClass(
78
- class {
79
- hasFocus = false;
80
- decorations: DecorationSet;
81
- constructor(view: EditorView) {
82
- this.decorations = buildDecorations(view);
83
- }
84
-
85
- update(update: ViewUpdate) {
86
- if (
87
- // TODO(burdon): Generalize for other extensions.
88
- this.hasFocus !== update.view.hasFocus ||
89
- update.startState.readOnly !== update.view.state.readOnly ||
90
- update.docChanged ||
91
- update.viewportChanged
92
- ) {
93
- this.hasFocus = update.view.hasFocus;
94
- this.decorations = buildDecorations(update.view);
95
- }
96
- }
97
- },
98
- {
99
- decorations: (value) => value.decorations,
100
- },
101
- ),
102
- styles,
103
- ];
104
- };
File without changes
File without changes