@dxos/react-ui-editor 0.3.10-next.ef70620 → 0.3.10

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 (154) hide show
  1. package/dist/lib/browser/index.mjs +1827 -799
  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 +59 -0
  5. package/dist/types/src/components/TextEditor/MarkdownEditor.stories.d.ts.map +1 -0
  6. package/dist/types/src/components/TextEditor/TextEditor.d.ts +7 -3
  7. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  8. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +12 -4
  9. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  10. package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts +10 -0
  11. package/dist/types/src/components/TextEditor/extensions/autocomplete.d.ts.map +1 -0
  12. package/dist/types/src/components/TextEditor/extensions/{basic/bundle.d.ts → basic.d.ts} +3 -2
  13. package/dist/types/src/components/TextEditor/extensions/basic.d.ts.map +1 -0
  14. package/dist/types/src/components/TextEditor/extensions/blast.d.ts +25 -0
  15. package/dist/types/src/components/TextEditor/extensions/blast.d.ts.map +1 -0
  16. package/dist/types/src/components/TextEditor/extensions/code.d.ts +2 -0
  17. package/dist/types/src/components/TextEditor/extensions/code.d.ts.map +1 -0
  18. package/dist/types/src/components/TextEditor/extensions/comments.d.ts +18 -0
  19. package/dist/types/src/components/TextEditor/extensions/comments.d.ts.map +1 -0
  20. package/dist/types/src/components/TextEditor/extensions/demo.d.ts +11 -0
  21. package/dist/types/src/components/TextEditor/extensions/demo.d.ts.map +1 -0
  22. package/dist/types/src/components/TextEditor/extensions/{hyperlink/hyperlinkWidget.d.ts → experimental.d.ts} +2 -2
  23. package/dist/types/src/components/TextEditor/extensions/experimental.d.ts.map +1 -0
  24. package/dist/types/src/components/TextEditor/extensions/highlight.d.ts +24 -0
  25. package/dist/types/src/components/TextEditor/extensions/highlight.d.ts.map +1 -0
  26. package/dist/types/src/components/TextEditor/extensions/image.d.ts +4 -0
  27. package/dist/types/src/components/TextEditor/extensions/image.d.ts.map +1 -0
  28. package/dist/types/src/components/TextEditor/extensions/index.d.ts +13 -2
  29. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
  30. package/dist/types/src/components/TextEditor/extensions/link.d.ts +11 -0
  31. package/dist/types/src/components/TextEditor/extensions/link.d.ts.map +1 -0
  32. package/dist/types/src/components/TextEditor/extensions/listener.d.ts +9 -0
  33. package/dist/types/src/components/TextEditor/extensions/listener.d.ts.map +1 -0
  34. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +10 -1
  35. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -1
  36. package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts +37 -0
  37. package/dist/types/src/components/TextEditor/extensions/markdown/highlight.d.ts.map +1 -0
  38. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts +1 -1
  39. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +1 -1
  40. package/dist/types/src/components/TextEditor/extensions/mention.d.ts +6 -0
  41. package/dist/types/src/components/TextEditor/extensions/mention.d.ts.map +1 -0
  42. package/dist/types/src/components/TextEditor/extensions/table.d.ts +8 -0
  43. package/dist/types/src/components/TextEditor/extensions/table.d.ts.map +1 -0
  44. package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts +3 -0
  45. package/dist/types/src/components/TextEditor/extensions/tasklist.d.ts.map +1 -0
  46. package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts +12 -0
  47. package/dist/types/src/components/TextEditor/extensions/tooltip.d.ts.map +1 -0
  48. package/dist/types/src/components/TextEditor/index.d.ts +2 -0
  49. package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
  50. package/dist/types/src/components/TextEditor/themes/default.d.ts +25 -0
  51. package/dist/types/src/components/TextEditor/themes/default.d.ts.map +1 -0
  52. package/dist/types/src/components/TextEditor/themes/index.d.ts +2 -0
  53. package/dist/types/src/components/TextEditor/themes/index.d.ts.map +1 -0
  54. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.d.ts +3 -0
  55. package/dist/types/src/hooks/automerge/PatchSemaphore.d.ts.map +1 -0
  56. package/dist/types/src/hooks/automerge/automerge.stories.d.ts +16 -0
  57. package/dist/types/src/hooks/automerge/automerge.stories.d.ts.map +1 -0
  58. package/dist/types/src/hooks/automerge/handle.d.ts.map +1 -0
  59. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/index.d.ts +1 -1
  60. package/dist/types/src/hooks/automerge/index.d.ts.map +1 -0
  61. package/dist/types/src/hooks/automerge/plugin.d.ts.map +1 -0
  62. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts +5 -0
  63. package/dist/types/src/hooks/automerge/translation/automerge-to-codemirror.d.ts.map +1 -0
  64. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts +6 -0
  65. package/dist/types/src/hooks/automerge/translation/codemirror-to-automerge.d.ts.map +1 -0
  66. package/dist/types/src/hooks/automerge/translation/index.d.ts +3 -0
  67. package/dist/types/src/hooks/automerge/translation/index.d.ts.map +1 -0
  68. package/dist/types/src/hooks/index.d.ts +0 -1
  69. package/dist/types/src/hooks/index.d.ts.map +1 -1
  70. package/dist/types/src/hooks/useTextModel.d.ts +9 -3
  71. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -1
  72. package/dist/types/src/{components/TextEditor → hooks/yjs}/yjs.stories.d.ts +2 -1
  73. package/dist/types/src/hooks/yjs/yjs.stories.d.ts.map +1 -0
  74. package/dist/types/src/styles/markdown.d.ts +14 -11
  75. package/dist/types/src/styles/markdown.d.ts.map +1 -1
  76. package/dist/types/src/testing/replicator.d.ts.map +1 -1
  77. package/package.json +24 -33
  78. package/src/components/TextEditor/MarkdownEditor.stories.tsx +367 -0
  79. package/src/components/TextEditor/TextEditor.stories.tsx +57 -43
  80. package/src/components/TextEditor/TextEditor.tsx +87 -45
  81. package/src/components/TextEditor/extensions/autocomplete.ts +59 -0
  82. package/src/components/TextEditor/extensions/{basic/bundle.ts → basic.ts} +8 -4
  83. package/src/components/TextEditor/extensions/blast.ts +373 -0
  84. package/src/components/TextEditor/extensions/code.ts +99 -0
  85. package/src/components/TextEditor/extensions/comments.ts +173 -0
  86. package/src/components/TextEditor/extensions/demo.ts +69 -0
  87. package/src/components/TextEditor/extensions/{hyperlink/hyperlinkWidget.tsx → experimental.tsx} +16 -21
  88. package/src/components/TextEditor/extensions/highlight.ts +128 -0
  89. package/src/components/TextEditor/extensions/image.ts +67 -0
  90. package/src/components/TextEditor/extensions/index.ts +13 -2
  91. package/src/components/TextEditor/extensions/{hyperlink/hyperlinkDecoration.ts → link.ts} +75 -38
  92. package/src/components/TextEditor/extensions/{change.ts → listener.ts} +5 -4
  93. package/src/components/TextEditor/extensions/markdown/bundle.ts +56 -49
  94. package/src/components/TextEditor/extensions/markdown/highlight.ts +207 -0
  95. package/src/components/TextEditor/extensions/markdown/index.ts +1 -1
  96. package/src/components/TextEditor/extensions/mention.ts +31 -0
  97. package/src/components/TextEditor/extensions/table.ts +119 -0
  98. package/src/components/TextEditor/extensions/tasklist.ts +121 -0
  99. package/src/components/TextEditor/extensions/{hyperlink/hyperlinkTooltip.tsx → tooltip.tsx} +9 -9
  100. package/src/components/TextEditor/index.ts +3 -0
  101. package/src/components/TextEditor/themes/default.ts +279 -0
  102. package/src/components/TextEditor/themes/index.ts +5 -0
  103. package/src/{automerge/automerge-plugin → hooks/automerge}/PatchSemaphore.ts +12 -9
  104. package/src/{automerge → hooks/automerge}/automerge.stories.tsx +6 -3
  105. package/src/{automerge/automerge-plugin → hooks/automerge}/plugin.ts +1 -0
  106. package/src/{automerge/automerge-plugin/amToCodemirror.ts → hooks/automerge/translation/automerge-to-codemirror.ts} +10 -2
  107. package/src/{automerge/automerge-plugin/codeMirrorToAm.ts → hooks/automerge/translation/codemirror-to-automerge.ts} +7 -4
  108. package/src/hooks/automerge/translation/index.ts +6 -0
  109. package/src/hooks/index.ts +0 -1
  110. package/src/hooks/useTextModel.ts +61 -39
  111. package/src/{components/TextEditor → hooks/yjs}/yjs.stories.tsx +2 -1
  112. package/src/styles/markdown.ts +33 -24
  113. package/src/testing/replicator.ts +7 -6
  114. package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +0 -1
  115. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts +0 -6
  116. package/dist/types/src/automerge/automerge-plugin/amToCodemirror.d.ts.map +0 -1
  117. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts +0 -7
  118. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +0 -1
  119. package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +0 -1
  120. package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +0 -1
  121. package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +0 -1
  122. package/dist/types/src/automerge/automerge.stories.d.ts +0 -9
  123. package/dist/types/src/automerge/automerge.stories.d.ts.map +0 -1
  124. package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts.map +0 -1
  125. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts +0 -3
  126. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts.map +0 -1
  127. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts +0 -10
  128. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts.map +0 -1
  129. package/dist/types/src/components/TextEditor/extensions/change.d.ts +0 -7
  130. package/dist/types/src/components/TextEditor/extensions/change.d.ts.map +0 -1
  131. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts +0 -11
  132. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts.map +0 -1
  133. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts +0 -8
  134. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts.map +0 -1
  135. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts.map +0 -1
  136. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts +0 -4
  137. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts.map +0 -1
  138. package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts +0 -17
  139. package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts.map +0 -1
  140. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts +0 -21
  141. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts.map +0 -1
  142. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +0 -1
  143. package/dist/types/src/hooks/useCollaboration.d.ts +0 -9
  144. package/dist/types/src/hooks/useCollaboration.d.ts.map +0 -1
  145. package/src/components/TextEditor/extensions/basic/index.ts +0 -6
  146. package/src/components/TextEditor/extensions/basic/theme.ts +0 -49
  147. package/src/components/TextEditor/extensions/hyperlink/index.ts +0 -7
  148. package/src/components/TextEditor/extensions/markdown/tags.ts +0 -38
  149. package/src/components/TextEditor/extensions/markdown/theme.ts +0 -265
  150. package/src/hooks/useCollaboration.ts +0 -45
  151. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/handle.d.ts +0 -0
  152. package/dist/types/src/{automerge/automerge-plugin → hooks/automerge}/plugin.d.ts +0 -0
  153. package/src/{automerge/automerge-plugin → hooks/automerge}/handle.ts +0 -0
  154. package/src/{automerge/automerge-plugin → hooks/automerge}/index.ts +1 -1
@@ -13,11 +13,20 @@ import {
13
13
  type ViewUpdate,
14
14
  } from '@codemirror/view';
15
15
 
16
- // Notes: Can't edit widget content (or cursor nav through them).
17
- // - https://discuss.codemirror.net/t/focusing-inputs-within-widgets/5178/7
18
- // Widgets intentionally always get set to contenteditable=false,
19
- // or they would become part of CodeMirror’s editable content element.
20
- // You should be able to introduce new contenteditable=true child elements inside of them.
16
+ // TODO(burdon): Reconcile with theme.
17
+ export const styles = EditorView.baseTheme({
18
+ '.cm-hyperlink-label': {
19
+ cursor: 'pointer',
20
+ textDecoration: 'underline',
21
+ },
22
+ '.cm-hyperlink-bracket': {
23
+ opacity: 0.2,
24
+ },
25
+ '.cm-hyperlink-url': {
26
+ opacity: 0.5,
27
+ fontFamily: 'monospace',
28
+ },
29
+ });
21
30
 
22
31
  const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)(!?)/gi;
23
32
 
@@ -83,7 +92,7 @@ export function hyperLinkExtension() {
83
92
  }
84
93
  },
85
94
  {
86
- decorations: (view) => view.decorations,
95
+ decorations: (value) => value.decorations,
87
96
  eventHandlers: {
88
97
  // TODO(burdon): CLick to expand link.
89
98
  // https://discuss.codemirror.net/t/decorator-iteration-on-click-event/4226/2
@@ -102,18 +111,4 @@ export function hyperLinkExtension() {
102
111
  );
103
112
  }
104
113
 
105
- export const hyperlinkStyle = EditorView.baseTheme({
106
- '.cm-hyperlink-label': {
107
- cursor: 'pointer',
108
- textDecoration: 'underline',
109
- },
110
- '.cm-hyperlink-bracket': {
111
- opacity: 0.2,
112
- },
113
- '.cm-hyperlink-url': {
114
- opacity: 0.5,
115
- fontFamily: 'monospace',
116
- },
117
- });
118
-
119
- export const hyperlinkWidget: Extension = [hyperLinkExtension(), hyperlinkStyle];
114
+ export const hyperlinkWidget: Extension = [hyperLinkExtension(), styles];
@@ -0,0 +1,128 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Extension, StateEffect, StateField } from '@codemirror/state';
6
+ import { Decoration, EditorView } from '@codemirror/view';
7
+ import sortBy from 'lodash.sortby';
8
+ import { useEffect } from 'react';
9
+
10
+ import { type DocumentRange } from '../../../hooks';
11
+
12
+ // TODO(burdon): Detect click, or cursor proximity.
13
+
14
+ // TODO(burdon): Reconcile with theme.
15
+ const styles = EditorView.baseTheme({
16
+ '& .cm-highlight': {
17
+ backgroundColor: 'yellow', // TODO(burdon): Reconcile with tailwind theme.
18
+ },
19
+ '& .cm-highlight::before': {
20
+ content: ']',
21
+ },
22
+ '& .cm-highlight-active': {
23
+ backgroundColor: 'lime',
24
+ },
25
+ });
26
+
27
+ const marks = {
28
+ highlight: Decoration.mark({ class: 'cm-highlight' }),
29
+ HighlightActive: Decoration.mark({ class: 'cm-highlight-active' }),
30
+ };
31
+
32
+ type HighlightRange = DocumentRange & { active?: boolean };
33
+
34
+ // TODO(burdon): Make configurable to support multiple layers.
35
+ const setHighlights = StateEffect.define<HighlightRange[]>();
36
+
37
+ /**
38
+ * Update field set.
39
+ */
40
+ // TODO(burdon): Reconcile range from data model, vs. in-markdown characters.
41
+ export const useHighlights = (view: EditorView | undefined, ranges: HighlightRange[] = []) => {
42
+ useEffect(() => {
43
+ view?.dispatch({
44
+ effects: setHighlights.of(ranges),
45
+ });
46
+ }, [view, ranges]);
47
+ };
48
+
49
+ /**
50
+ * State field that tracks highlight ranges.
51
+ */
52
+ export const highlightStateField = StateField.define<HighlightRange[]>({
53
+ create: () => [],
54
+ update: (threads, tr) => {
55
+ for (const effect of tr.effects) {
56
+ if (effect.is(setHighlights)) {
57
+ return effect.value;
58
+ }
59
+ }
60
+
61
+ return threads;
62
+ },
63
+ });
64
+
65
+ /**
66
+ * Decorate ranges.
67
+ */
68
+ // TODO(burdon): Pass in field to make reusable (e.g., comments, search).
69
+ export const highlightDecorations = EditorView.decorations.compute([highlightStateField], (state) => {
70
+ const selectionRanges = state.field(highlightStateField);
71
+ const decorations =
72
+ sortBy(selectionRanges ?? [], (selection) => selection.from)?.flatMap((selection) => {
73
+ const range = { from: selection.from, to: selection.to + 1 };
74
+ if (selection.to > selection.from) {
75
+ if (selection.active) {
76
+ return marks.HighlightActive.range(range.from, range.to);
77
+ } else {
78
+ return marks.highlight.range(range.from, range.to);
79
+ }
80
+ } else {
81
+ return [];
82
+ }
83
+ }) ?? [];
84
+
85
+ return Decoration.set(decorations);
86
+ });
87
+
88
+ // TODO(burdon): Use current range to create comment.
89
+ // TODO(burdon): Allow/prevent overlapping?
90
+
91
+ /**
92
+ * Track cursor entering and existing ranges.
93
+ */
94
+ const listener = (options: HighlightOptions) => {
95
+ return EditorView.updateListener.of((update) => {
96
+ const { view, state } = update;
97
+ const { /* from, to, */ head } = state.selection.main;
98
+ const ranges = state.field(highlightStateField);
99
+
100
+ // TODO(burdon): Modify range if editing.
101
+ // console.log('::::', update);
102
+
103
+ // Set active.
104
+ let mod = false;
105
+ const newRanges = ranges.map((range) => {
106
+ const active = head >= range.from && head <= range.to;
107
+ if (active !== range.active) {
108
+ mod = true;
109
+ return { ...range, active };
110
+ } else {
111
+ return range;
112
+ }
113
+ });
114
+
115
+ if (mod) {
116
+ view.dispatch({ effects: setHighlights.of(newRanges) });
117
+ options?.onChange?.(newRanges.find((range) => range.active)?.id);
118
+ }
119
+ });
120
+ };
121
+
122
+ export type HighlightOptions = {
123
+ onChange?: (active?: string) => {};
124
+ };
125
+
126
+ export const highlight = (options: HighlightOptions = {}): Extension => {
127
+ return [highlightStateField, highlightDecorations, listener(options), styles];
128
+ };
@@ -0,0 +1,67 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { syntaxTree } from '@codemirror/language';
6
+ import {
7
+ type EditorState,
8
+ type Extension,
9
+ type RangeSet,
10
+ RangeSetBuilder,
11
+ StateField,
12
+ type Transaction,
13
+ } from '@codemirror/state';
14
+ import { Decoration, EditorView, WidgetType } from '@codemirror/view';
15
+
16
+ export type ImageOptions = {};
17
+
18
+ export const image = (options: ImageOptions = {}): Extension => {
19
+ return StateField.define<RangeSet<any>>({
20
+ create: (state) => update(state, options),
21
+ update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
22
+ provide: (field) => EditorView.decorations.from(field),
23
+ });
24
+ };
25
+
26
+ const update = (state: EditorState, options: ImageOptions) => {
27
+ const builder = new RangeSetBuilder();
28
+ const cursor = state.selection.main.head;
29
+
30
+ syntaxTree(state).iterate({
31
+ enter: (node) => {
32
+ if (node.name === 'Image') {
33
+ const urlNode = node.node.getChild('URL');
34
+ if (urlNode) {
35
+ const hide = state.readOnly || cursor < node.from || cursor > node.to;
36
+ const url = state.sliceDoc(urlNode.from, urlNode.to);
37
+ builder.add(
38
+ hide ? node.from : node.to,
39
+ node.to,
40
+ Decoration.replace({
41
+ widget: new ImageWidget(url),
42
+ }),
43
+ );
44
+ }
45
+ }
46
+ },
47
+ });
48
+
49
+ return builder.finish();
50
+ };
51
+
52
+ class ImageWidget extends WidgetType {
53
+ constructor(readonly _url: string) {
54
+ super();
55
+ }
56
+
57
+ override eq(other: WidgetType) {
58
+ return this._url === (other as any as ImageWidget)?._url;
59
+ }
60
+
61
+ toDOM(view: EditorView) {
62
+ const img = document.createElement('img');
63
+ img.setAttribute('src', this._url);
64
+ img.setAttribute('class', 'cm-image');
65
+ return img;
66
+ }
67
+ }
@@ -2,7 +2,18 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ export * from './autocomplete';
5
6
  export * from './basic';
6
- export * from './change';
7
- export * from './hyperlink';
7
+ export * from './blast';
8
+ export * from './code';
9
+ export * from './demo';
10
+ export * from './comments';
11
+ export * from './highlight';
12
+ export * from './image';
13
+ export * from './link';
14
+ export * from './listener';
8
15
  export * from './markdown';
16
+ export * from './mention';
17
+ export * from './table';
18
+ export * from './tasklist';
19
+ export * from './tooltip';
@@ -4,28 +4,64 @@
4
4
  //
5
5
 
6
6
  import { syntaxTree } from '@codemirror/language';
7
- import { type EditorState, type RangeSet, RangeSetBuilder, StateField, type Transaction } from '@codemirror/state';
7
+ import {
8
+ type EditorState,
9
+ type Extension,
10
+ type RangeSet,
11
+ RangeSetBuilder,
12
+ StateField,
13
+ type Transaction,
14
+ } from '@codemirror/state';
8
15
  import { Decoration, EditorView, WidgetType } from '@codemirror/view';
9
16
 
10
17
  // Adapted from:
11
18
  // https://codemirror.net/try/?c=aW1wb3J0IHsgYmFzaWNTZXR1cCwgRWRpdG9yVmlldyB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7IG1hcmtkb3duIH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1tYXJrZG93biIKaW1wb3J0IHsgc3ludGF4VHJlZSB9IGZyb20gIkBjb2RlbWlycm9yL2xhbmd1YWdlIjsKaW1wb3J0IHsgRWRpdG9yU3RhdGUsIFJhbmdlU2V0QnVpbGRlciwgU3RhdGVGaWVsZCB9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIjsKaW1wb3J0IHsgRGVjb3JhdGlvbiwgV2lkZ2V0VHlwZSB9IGZyb20gIkBjb2RlbWlycm9yL3ZpZXciOwoKY2xhc3MgTGluayBleHRlbmRzIFdpZGdldFR5cGUgewogIGNvbnN0cnVjdG9yKHRleHQsIHVybCkgewogICAgc3VwZXIoKQogICAgdGhpcy50ZXh0ID0gdGV4dAogICAgdGhpcy51cmwgPSB1cmwKICB9CiAgZXEob3RoZXIpIHsgcmV0dXJuIHRoaXMudGV4dCA9PSBvdGhlci50ZXh0ICYmIHRoaXMudXJsID09IG90aGVyLnVybCB9CiAgdG9ET00oKSB7CiAgICBsZXQgbGluayA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoImEiKQogICAgbGluay50ZXh0Q29udGVudCA9IHRoaXMudGV4dAogICAgbGluay5ocmVmID0gdGhpcy51cmwKICAgIHJldHVybiBsaW5rOwogIH0KfQoKbGV0IGRlY29yYXRpb25zRmllbGQgPSBTdGF0ZUZpZWxkLmRlZmluZSh7CiAgY3JlYXRlKCkgewogICAgcmV0dXJuIERlY29yYXRpb24ubm9uZTsKICB9LAogIHVwZGF0ZShfLCB0cikgewogICAgY29uc3QgYnVpbGRlciA9IG5ldyBSYW5nZVNldEJ1aWxkZXIoKTsKICAgIGxldCBjdXJzb3IgPSB0ci5zdGF0ZS5zZWxlY3Rpb24ubWFpbi5oZWFkOwogICAgc3ludGF4VHJlZSh0ci5zdGF0ZSkuaXRlcmF0ZSh7CiAgICAgIGVudGVyOiAobm9kZSkgPT4gewogICAgICAgIGlmICgoY3Vyc29yIDwgbm9kZS5mcm9tIHx8IGN1cnNvciA+IG5vZGUudG8pICYmIG5vZGUubmFtZSA9PSAiTGluayIpIHsKICAgICAgICAgIGxldCBtYXJrcyA9IG5vZGUubm9kZS5nZXRDaGlsZHJlbigiTGlua01hcmsiKTsKICAgICAgICAgIGxldCB0ZXh0ID0gbWFya3MubGVuZ3RoID49IDIgPyB0ci5zdGF0ZS5zbGljZURvYyhtYXJrc1swXS50bywgbWFya3NbMV0uZnJvbSkgOiAiIgoKICAgICAgICAgIGxldCB1cmxOb2RlID0gbm9kZS5ub2RlLmdldENoaWxkKCJVUkwiKTsKICAgICAgICAgIGxldCB1cmwgPSB1cmxOb2RlID8gdHIuc3RhdGUuc2xpY2VEb2ModXJsTm9kZS5mcm9tLCB1cmxOb2RlLnRvKSA6ICIiCiAgICAgICAgICBidWlsZGVyLmFkZChub2RlLmZyb20sIG5vZGUudG8sIERlY29yYXRpb24ucmVwbGFjZSh7CiAgICAgICAgICAgIHdpZGdldDogbmV3IExpbmsodGV4dCwgdXJsKSwKICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSwKICAgIH0pOwogICAgcmV0dXJuIGJ1aWxkZXIuZmluaXNoKCk7CiAgfSwKICBwcm92aWRlOiAoZikgPT4gRWRpdG9yVmlldy5kZWNvcmF0aW9ucy5mcm9tKGYpLAp9KTsKCmxldCB2aWV3ID0gbmV3IEVkaXRvclZpZXcoewogIGRvYzogIiMgSGVsbG8gV29ybGRcblxuW1Rlc3RdKGh0dHBzOi8vZXhhbXBsZS5jb20pXG5cblxuIiwKICBleHRlbnNpb25zOiBbZGVjb3JhdGlvbnNGaWVsZCwgbWFya2Rvd24oKV0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5LAp9KTsKCnZpZXcuZm9jdXMoKTs=
12
19
  // https://discuss.codemirror.net/t/link-widget-is-clicked-only-if-editor-is-out-of-focus/7433
13
20
 
14
- // TODO(burdon): Adapt to make all markdown editable?
21
+ export type LinkOptions = {
22
+ link?: boolean;
23
+ onRender?: (el: Element, url: string) => void;
24
+ };
25
+
26
+ /**
27
+ * Creates a state field to replace AST elements with a hyperlink widget.
28
+ * https://codemirror.net/docs/ref/#state.StateField
29
+ */
30
+ export const link = (options: LinkOptions = {}): Extension => {
31
+ return StateField.define<RangeSet<any>>({
32
+ create: (state) => update(state, options),
33
+ update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, options),
34
+ provide: (field) => EditorView.decorations.from(field),
35
+ });
36
+ };
15
37
 
16
- // TODO(burdon): Research overlays.
17
- // https://stackoverflow.com/questions/26933344/codemirror-detect-and-create-links-inside-editor
18
- // https://codemirror.net/5/doc/manual.html#addOverlay
19
- // https://codemirror.net/5/demo/simplemode.html
20
- // https://codemirror.net/5/demo/search.html (Search dialog).
38
+ class LinkButton extends WidgetType {
39
+ constructor(
40
+ private readonly _pos: number,
41
+ private readonly _url: string,
42
+ private readonly _onAttach: LinkOptions['onRender'],
43
+ ) {
44
+ super();
45
+ }
46
+
47
+ override eq(other: LinkButton) {
48
+ return this._pos === other._pos && this._url === other._url;
49
+ }
21
50
 
22
- class Link extends WidgetType {
23
- constructor(private readonly _anchor: number, private readonly _text: string, private readonly _url?: string) {
51
+ toDOM(view: EditorView) {
52
+ const el = document.createElement('span');
53
+ this._onAttach!(el, this._url);
54
+ return el;
55
+ }
56
+ }
57
+
58
+ class LinkText extends WidgetType {
59
+ constructor(private readonly _pos: number, private readonly _text: string, private readonly _url?: string) {
24
60
  super();
25
61
  }
26
62
 
27
- override eq(other: any) {
28
- return this._text === other.text && this._url === other.url;
63
+ override eq(other: LinkText) {
64
+ return this._pos === other._pos && this._url === other._url;
29
65
  }
30
66
 
31
67
  toDOM(view: EditorView) {
@@ -33,13 +69,14 @@ class Link extends WidgetType {
33
69
  link.setAttribute('class', 'cm-link');
34
70
  link.textContent = this._text;
35
71
  if (this._url) {
72
+ link.setAttribute('rel', 'noreferrer');
36
73
  link.setAttribute('target', '_blank');
37
74
  link.href = this._url;
38
75
  } else {
39
76
  link.onclick = () => {
40
77
  view.dispatch({
41
78
  selection: {
42
- anchor: this._anchor,
79
+ anchor: this._pos,
43
80
  },
44
81
  });
45
82
  };
@@ -49,49 +86,49 @@ class Link extends WidgetType {
49
86
  }
50
87
  }
51
88
 
52
- type HyperlinkDecorationConfig = {
53
- link?: boolean;
54
- };
55
-
56
89
  /**
57
- * Creates a state field to replace AST elements with a hyperlink widget.
58
- * https://codemirror.net/docs/ref/#state.StateField
90
+ * Range sets provide a data structure that can hold a collection of tagged,
91
+ * possibly overlapping ranges in such a way that they can efficiently be mapped though document changes.
92
+ * https://codemirror.net/docs/ref/#state
59
93
  */
60
- export const hyperlinkDecoration = ({ link }: HyperlinkDecorationConfig = {}) => {
61
- return StateField.define<RangeSet<any>>({
62
- create: (state) => update(state, link),
63
- update: (_: RangeSet<any>, tr: Transaction) => update(tr.state, link),
64
- provide: (field) => EditorView.decorations.from(field),
65
- });
66
- };
67
-
68
- const update = (state: EditorState, link?: boolean) => {
94
+ const update = (state: EditorState, options: LinkOptions) => {
69
95
  const builder = new RangeSetBuilder();
70
96
  const cursor = state.selection.main.head;
97
+
71
98
  syntaxTree(state).iterate({
72
99
  enter: (node) => {
73
100
  // Check if cursor is inside text.
74
- if (cursor < node.from || cursor > node.to) {
75
- if (node.name === 'Link') {
76
- const marks = node.node.getChildren('LinkMark');
77
- const text = marks.length >= 2 ? state.sliceDoc(marks[0].to, marks[1].from) : '';
78
-
79
- const urlNode = node.node.getChild('URL');
80
- const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : '';
101
+ if (node.name === 'Link') {
102
+ const marks = node.node.getChildren('LinkMark');
103
+ const text = marks.length >= 2 ? state.sliceDoc(marks[0].to, marks[1].from) : '';
104
+ const urlNode = node.node.getChild('URL');
105
+ const url = urlNode ? state.sliceDoc(urlNode.from, urlNode.to) : '';
106
+ if (!url) {
107
+ return false;
108
+ }
81
109
 
110
+ if (state.readOnly || cursor < node.from || cursor > node.to) {
82
111
  builder.add(
83
112
  node.from,
84
113
  node.to,
85
114
  Decoration.replace({
86
- widget: new Link(node.from, text, link ? url : undefined),
115
+ widget: new LinkText(node.from, text, options.link ? url : undefined),
87
116
  }),
88
117
  );
118
+ }
89
119
 
90
- return false;
120
+ if (options.onRender) {
121
+ builder.add(
122
+ node.to,
123
+ node.to,
124
+ Decoration.replace({
125
+ widget: new LinkButton(node.from, url, options.onRender),
126
+ }),
127
+ );
91
128
  }
92
- }
93
129
 
94
- return true;
130
+ return false;
131
+ }
95
132
  },
96
133
  });
97
134
 
@@ -4,13 +4,13 @@
4
4
 
5
5
  import { StateField } from '@codemirror/state';
6
6
 
7
+ export type ListenerOptions = { onChange: (text: string) => void };
8
+
7
9
  /**
8
10
  * Based on https://github.com/codemirror/dev/issues/44#issuecomment-789093799
9
- * @param onChange
10
11
  */
11
- // TODO(burdon): Name?
12
- export const onChangeExtension = (onChange: (text: string) => void) =>
13
- StateField.define({
12
+ export const listener = ({ onChange }: ListenerOptions) => {
13
+ return StateField.define({
14
14
  create: () => null,
15
15
  update: (_value, transaction) => {
16
16
  if (transaction.docChanged && onChange) {
@@ -20,3 +20,4 @@ export const onChangeExtension = (onChange: (text: string) => void) =>
20
20
  return null;
21
21
  },
22
22
  });
23
+ };
@@ -3,18 +3,11 @@
3
3
  //
4
4
 
5
5
  import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
6
- import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
6
+ import { history, historyKeymap, indentWithTab, standardKeymap } from '@codemirror/commands';
7
7
  import { markdownLanguage, markdown } from '@codemirror/lang-markdown';
8
- import {
9
- bracketMatching,
10
- defaultHighlightStyle,
11
- foldKeymap,
12
- indentOnInput,
13
- syntaxHighlighting,
14
- } from '@codemirror/language';
8
+ import { bracketMatching, defaultHighlightStyle, indentOnInput, syntaxHighlighting } from '@codemirror/language';
15
9
  import { languages } from '@codemirror/language-data';
16
- import { lintKeymap } from '@codemirror/lint';
17
- import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
10
+ import { searchKeymap } from '@codemirror/search';
18
11
  import { EditorState, type Extension } from '@codemirror/state';
19
12
  import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
20
13
  import {
@@ -23,77 +16,91 @@ import {
23
16
  dropCursor,
24
17
  EditorView,
25
18
  highlightActiveLine,
26
- highlightActiveLineGutter,
27
- highlightSpecialChars,
28
19
  keymap,
29
20
  placeholder,
30
- rectangularSelection,
31
21
  } from '@codemirror/view';
32
- // import { GFM } from '@lezer/markdown';
33
22
 
34
23
  import type { ThemeMode } from '@dxos/react-ui';
35
24
 
36
- import { markdownTagsExtension } from './tags';
37
- import { markdownHighlightStyle } from './theme';
25
+ import { markdownHighlightStyle, markdownTagsExtensions } from './highlight';
38
26
 
39
27
  export type MarkdownBundleOptions = {
28
+ readonly?: boolean;
40
29
  themeMode?: ThemeMode;
41
30
  placeholder?: string;
42
31
  };
43
32
 
44
- export const markdownBundle = ({ themeMode, placeholder: _placeholder }: MarkdownBundleOptions): Extension[] => {
45
- // All of https://github.com/codemirror/basic-setup minus line numbers and fold gutter.
46
- // https://codemirror.net/docs/ref/#codemirror.basicSetup
33
+ /**
34
+ * Markdown bundle.
35
+ * Refs:
36
+ * https://github.com/codemirror/basic-setup
37
+ * https://github.com/codemirror/basic-setup/blob/main/src/codemirror.ts#L50
38
+ * https://codemirror.net/docs/community
39
+ * https://codemirror.net/docs/ref/#codemirror.basicSetup
40
+ */
41
+ // TODO(burdon): Add Composer here: https://codemirror.net/docs/community
42
+ export const markdownBundle = ({
43
+ readonly,
44
+ themeMode,
45
+ placeholder: _placeholder,
46
+ }: MarkdownBundleOptions): Extension[] => {
47
47
  return [
48
- bracketMatching(),
49
- closeBrackets(),
50
- _placeholder && placeholder(_placeholder),
51
-
52
48
  EditorState.allowMultipleSelections.of(true),
49
+ EditorState.tabSize.of(2),
53
50
  EditorView.lineWrapping,
54
51
 
55
- // autocompletion(),
52
+ customKeymap(),
53
+
54
+ bracketMatching(),
55
+ closeBrackets(),
56
56
  crosshairCursor(),
57
57
  dropCursor(),
58
58
  drawSelection(),
59
59
  highlightActiveLine(),
60
- highlightActiveLineGutter(),
61
- highlightSelectionMatches(),
62
- highlightSpecialChars(),
63
60
  history(),
64
61
  indentOnInput(),
65
- rectangularSelection(),
66
-
67
- keymap.of([
68
- ...closeBracketsKeymap,
69
- // ...completionKeymap,
70
- ...defaultKeymap,
71
- ...foldKeymap,
72
- ...historyKeymap,
73
- ...lintKeymap,
74
- ...searchKeymap,
75
- indentWithTab,
76
- ]),
62
+ _placeholder && placeholder(_placeholder),
77
63
 
78
64
  // Main extension.
79
65
  // https://github.com/codemirror/lang-markdown
66
+ // https://codemirror.net/5/mode/markdown/index.html (demo).
80
67
  markdown({
68
+ // GRM by default (vs strict CommonMark):
69
+ // Table, TaskList, Strikethrough, and Autolink.
70
+ // NOTE: This extends the parser; it doesn't affect rendering.
71
+ // https://github.github.com/gfm
72
+ // https://github.com/lezer-parser/markdown?tab=readme-ov-file#github-flavored-markdown
81
73
  base: markdownLanguage,
74
+
75
+ // Languages for syntax highlighting fenced code blocks.
82
76
  codeLanguages: languages,
83
- extensions: [
84
- // TODO(burdon): This seems to upgrade the parser.
85
- // GitHub flavored markdown bundle: Table, TaskList, Strikethrough, and Autolink.
86
- // https://github.com/lezer-parser/markdown?tab=readme-ov-file#github-flavored-markdown
87
- // https://github.github.com/gfm
88
- // GFM,
89
77
 
90
- // Custom styling.
91
- markdownTagsExtension,
78
+ // Parser extensions.
79
+ extensions: [
80
+ // GFM provided by default.
81
+ markdownTagsExtensions,
92
82
  ],
93
83
  }),
94
84
 
95
- // TODO(thure): All but one rule here apply to both themes; rename or refactor.
96
- syntaxHighlighting(markdownHighlightStyle),
85
+ // https://github.com/codemirror/theme-one-dark
97
86
  themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
87
+
88
+ // Custom styles.
89
+ syntaxHighlighting(markdownHighlightStyle(readonly)),
98
90
  ].filter(Boolean) as Extension[];
99
91
  };
92
+
93
+ // https://codemirror.net/docs/ref/#view.keymap
94
+ const customKeymap = () =>
95
+ keymap.of([
96
+ // https://codemirror.net/docs/ref/#commands.indentWithTab
97
+ indentWithTab,
98
+ // https://codemirror.net/docs/ref/#commands.standardKeymap
99
+ ...standardKeymap,
100
+ // https://codemirror.net/docs/ref/#commands.historyKeymap
101
+ ...historyKeymap,
102
+ // https://codemirror.net/docs/ref/#autocomplete.closeBracketsKeymap
103
+ ...closeBracketsKeymap,
104
+ // https://codemirror.net/docs/ref/#search.searchKeymap
105
+ ...searchKeymap,
106
+ ]);