@dxos/react-ui-editor 0.3.11-main.13264f1 → 0.3.11-main.1690b50

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,7 +2,8 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Range } from '@codemirror/state';
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import { RangeSetBuilder } from '@codemirror/state';
6
7
  import {
7
8
  ViewPlugin,
8
9
  type DecorationSet,
@@ -10,38 +11,37 @@ import {
10
11
  type ViewUpdate,
11
12
  Decoration,
12
13
  type BlockInfo,
14
+ WidgetType,
13
15
  } from '@codemirror/view';
14
16
 
15
17
  import { mx } from '@dxos/react-ui-theme';
16
18
 
17
19
  import { getToken } from '../../styles';
18
20
 
19
- const CODE_REGEX = /```[\s\S]*?```/gs;
20
-
21
21
  // TODO(burdon): Reconcile with theme.
22
+ // [aria-readonly="true"]
22
23
  const styles = EditorView.baseTheme({
23
- '& .cm-codeblock': {
24
+ '& .cm-code': {
25
+ paddingInline: '1rem !important',
24
26
  fontFamily: getToken('fontFamily.mono', []).join(','),
25
27
  },
26
- '&light [aria-readonly="true"] .cm-codeblock': {
27
- background: getToken('extend.colors.neutral.50'),
28
+ '& .cm-codeblock': {
29
+ display: 'inline-block',
30
+ width: '100%',
28
31
  },
29
- '&dark [aria-readonly="true"] .cm-codeblock': {
30
- background: getToken('extend.colors.neutral.850'),
32
+ '&light .cm-codeblock, &light .cm-codeblock.cm-activeLine': {
33
+ background: getToken('extend.colors.neutral.25'),
31
34
  },
32
- '& [aria-readonly="true"] .cm-codeblock': {
33
- display: 'block',
34
- paddingInline: '4px !important',
35
+ '&dark .cm-codeblock, &dark .cm-codeblock.cm-activeLine': {
36
+ background: getToken('extend.colors.neutral.850'),
35
37
  },
36
- '& [aria-readonly="true"] .cm-codeblock-first': {
37
- paddingTop: '4px !important',
38
- borderTopLeftRadius: '4px',
39
- borderTopRightRadius: '4px',
38
+ '& .cm-codeblock-first': {
39
+ borderTopLeftRadius: '.5rem',
40
+ borderTopRightRadius: '.5rem',
40
41
  },
41
- '& [aria-readonly="true"] .cm-codeblock-last': {
42
- paddingBottom: '4px !important',
43
- borderBottomLeftRadius: '4px',
44
- borderBottomRightRadius: '4px',
42
+ '& .cm-codeblock-last': {
43
+ borderBottomLeftRadius: '.5rem',
44
+ borderBottomRightRadius: '.5rem',
45
45
  },
46
46
  });
47
47
 
@@ -52,47 +52,70 @@ const getLineRange = (lines: BlockInfo[], from: number, to: number) => {
52
52
  return [start, end];
53
53
  };
54
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
- }
55
+ class LineWidget extends WidgetType {
56
+ constructor(private readonly _className: string) {
57
+ super();
58
+ }
59
+
60
+ override toDOM() {
61
+ const el = document.createElement('span');
62
+ el.innerHTML = '&nbsp;';
63
+ el.className = mx('cm-code cm-codeblock', this._className);
64
+ return el;
65
+ }
66
+ }
67
+
68
+ const top = new LineWidget('cm-codeblock-first');
69
+ const bottom = new LineWidget('cm-codeblock-last');
72
70
 
73
- return Decoration.set(decorations);
74
- };
71
+ const buildDecorations = (view: EditorView): DecorationSet => {
72
+ const builder = new RangeSetBuilder<Decoration>();
73
+ const { state } = view;
74
+ const blocks = view.viewportLineBlocks;
75
+ const cursor = state.selection.main.head;
75
76
 
77
+ // TODO(burdon): Add copy to clipboard widget.
78
+ for (const { from, to } of view.visibleRanges) {
79
+ syntaxTree(state).iterate({
80
+ enter: (node) => {
81
+ if (node.name === 'FencedCode') {
82
+ const edit = !view.state.readOnly && cursor >= node.from && cursor <= node.to;
83
+ const range = getLineRange(blocks, node.from, node.to);
84
+ for (let i = range[0]; i <= range[1]; i++) {
85
+ const block = blocks[i];
86
+ if (!edit && (i === range[0] || i === range[1])) {
87
+ builder.add(block.from, block.to, Decoration.replace({ widget: i === range[0] ? top : bottom }));
88
+ } else {
89
+ builder.add(
90
+ block.from,
91
+ block.from,
92
+ Decoration.line({
93
+ class: mx('cm-code cm-codeblock'),
94
+ }),
95
+ );
96
+ }
97
+ }
98
+ }
99
+ },
100
+ from,
101
+ to,
102
+ });
103
+ }
104
+
105
+ return builder.finish();
106
+ };
107
+
108
+ export const code = () => {
76
109
  return [
77
110
  ViewPlugin.fromClass(
78
111
  class {
79
- hasFocus = false;
80
112
  decorations: DecorationSet;
81
113
  constructor(view: EditorView) {
82
114
  this.decorations = buildDecorations(view);
83
115
  }
84
116
 
85
117
  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
- }
118
+ this.decorations = buildDecorations(update.view);
96
119
  }
97
120
  },
98
121
  {
@@ -22,10 +22,10 @@ const buildDecorations = (view: EditorView): DecorationSet => {
22
22
  case 'ATXHeading5':
23
23
  case 'ATXHeading6': {
24
24
  const mark = node.node.getChild('HeaderMark');
25
-
26
- // Check if cursor is inside text.
27
- if (mark && (cursor < node.from || cursor > node.to)) {
28
- builder.add(mark.from, mark.to + 1, Decoration.replace({}));
25
+ if (mark) {
26
+ if (view.state.readOnly || cursor < node.from || cursor > node.to) {
27
+ builder.add(mark.from, mark.to + 1, Decoration.replace({}));
28
+ }
29
29
  }
30
30
  }
31
31
  }
@@ -43,7 +43,6 @@ export const heading = () => {
43
43
  ViewPlugin.fromClass(
44
44
  class {
45
45
  decorations: DecorationSet;
46
-
47
46
  constructor(view: EditorView) {
48
47
  this.decorations = buildDecorations(view);
49
48
  }
@@ -53,7 +52,7 @@ export const heading = () => {
53
52
  }
54
53
  },
55
54
  {
56
- decorations: (v) => v.decorations,
55
+ decorations: (value) => value.decorations,
57
56
  },
58
57
  ),
59
58
  ];
@@ -128,13 +128,13 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
128
128
  // Markdown marks.
129
129
  {
130
130
  tag: [markdownTags.CodeMark, markdownTags.HeaderMark, markdownTags.QuoteMark, markdownTags.EmphasisMark],
131
- class: readonly ? 'hidden' : mark,
131
+ class: mark,
132
132
  },
133
133
 
134
134
  // E.g., code block language (after ```).
135
135
  {
136
136
  tag: [tags.function(tags.variableName), tags.labelName],
137
- class: readonly ? 'hidden' : codeMark,
137
+ class: codeMark,
138
138
  },
139
139
 
140
140
  {
@@ -143,12 +143,12 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
143
143
  },
144
144
 
145
145
  // Headings.
146
- { tag: tags.heading1, class: heading(1, readonly) },
147
- { tag: tags.heading2, class: heading(2, readonly) },
148
- { tag: tags.heading3, class: heading(3, readonly) },
149
- { tag: tags.heading4, class: heading(4, readonly) },
150
- { tag: tags.heading5, class: heading(5, readonly) },
151
- { tag: tags.heading6, class: heading(6, readonly) },
146
+ { tag: tags.heading1, class: heading(1) },
147
+ { tag: tags.heading2, class: heading(2) },
148
+ { tag: tags.heading3, class: heading(3) },
149
+ { tag: tags.heading4, class: heading(4) },
150
+ { tag: tags.heading5, class: heading(5) },
151
+ { tag: tags.heading6, class: heading(6) },
152
152
 
153
153
  // Emphasis.
154
154
  { tag: tags.emphasis, class: italic },
@@ -165,7 +165,7 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
165
165
  // However, since `codeLanguages` is also defined, the `lezer` will not parse fenced code blocks,
166
166
  // when a language is specified. In this case, the syntax highlighting extensions will colorize
167
167
  // the code, but all other CSS properties will be inherited.
168
- // IMPORTANT: Therefore, the fenced code block will use the base editor font.
168
+ // IMPORTANT: Therefore, the fenced code block will use the base editor font unless changed by an extension.
169
169
  {
170
170
  tag: [markdownTags.CodeText, markdownTags.InlineCode],
171
171
  class: code,
@@ -180,12 +180,6 @@ export const markdownHighlightStyle = (readonly?: boolean) => {
180
180
  tag: [markdownTags.TableCell],
181
181
  class: 'font-mono',
182
182
  },
183
-
184
- // Main content, paragraphs, etc.
185
- // {
186
- // tag: [tags.content],
187
- // fontFamily: getToken('fontFamily.body', []).join(','),
188
- // },
189
183
  ],
190
184
  {
191
185
  scope: markdownLanguage,
@@ -51,11 +51,9 @@ const buildDecorations = (view: EditorView): DecorationSet => {
51
51
 
52
52
  export const hr = () => {
53
53
  return [
54
- styles,
55
54
  ViewPlugin.fromClass(
56
55
  class {
57
56
  decorations: DecorationSet;
58
-
59
57
  constructor(view: EditorView) {
60
58
  this.decorations = buildDecorations(view);
61
59
  }
@@ -65,8 +63,9 @@ export const hr = () => {
65
63
  }
66
64
  },
67
65
  {
68
- decorations: (v) => v.decorations,
66
+ decorations: (value) => value.decorations,
69
67
  },
70
68
  ),
69
+ styles,
71
70
  ];
72
71
  };
@@ -37,7 +37,6 @@ export const link = (options: LinkOptions = {}): Extension => {
37
37
  ViewPlugin.fromClass(
38
38
  class {
39
39
  decorations: DecorationSet;
40
-
41
40
  constructor(view: EditorView) {
42
41
  this.decorations = buildDecorations(view, options);
43
42
  }
@@ -49,7 +48,7 @@ export const link = (options: LinkOptions = {}): Extension => {
49
48
  }
50
49
  },
51
50
  {
52
- decorations: (v) => v.decorations,
51
+ decorations: (value) => value.decorations,
53
52
  },
54
53
  ),
55
54
  ];
@@ -91,7 +91,6 @@ export const tasklist = (options: TasklistOptions = {}) => {
91
91
  ViewPlugin.fromClass(
92
92
  class {
93
93
  decorations: DecorationSet;
94
-
95
94
  constructor(view: EditorView) {
96
95
  this.decorations = buildDecorations(view);
97
96
  }
@@ -103,7 +102,7 @@ export const tasklist = (options: TasklistOptions = {}) => {
103
102
  }
104
103
  },
105
104
  {
106
- decorations: (v) => v.decorations,
105
+ decorations: (value) => value.decorations,
107
106
  },
108
107
  ),
109
108
  styles,
@@ -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';
@@ -86,8 +86,6 @@ export const defaultTheme: ThemeStyles = {
86
86
  //
87
87
  '.cm-line': {
88
88
  paddingInline: 0,
89
- // TODO(burdon): Other values leave gaps between lines when highlighted.
90
- // lineHeight: 'normal',
91
89
  },
92
90
  '.cm-activeLine': {
93
91
  background: 'inherit',