@dxos/react-ui-editor 0.3.9-main.632a97f → 0.3.9-main.71124bf

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 (147) hide show
  1. package/dist/lib/browser/index.mjs +962 -608
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts +2 -2
  5. package/dist/types/src/automerge/automerge-plugin/PatchSemaphore.d.ts.map +1 -1
  6. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts +2 -2
  7. package/dist/types/src/automerge/automerge-plugin/codeMirrorToAm.d.ts.map +1 -1
  8. package/dist/types/src/automerge/automerge-plugin/handle.d.ts +9 -0
  9. package/dist/types/src/automerge/automerge-plugin/handle.d.ts.map +1 -0
  10. package/dist/types/src/automerge/automerge-plugin/index.d.ts +2 -2
  11. package/dist/types/src/automerge/automerge-plugin/index.d.ts.map +1 -1
  12. package/dist/types/src/automerge/automerge-plugin/plugin.d.ts +6 -5
  13. package/dist/types/src/automerge/automerge-plugin/plugin.d.ts.map +1 -1
  14. package/dist/types/src/automerge/automerge.stories.d.ts +2 -1
  15. package/dist/types/src/automerge/automerge.stories.d.ts.map +1 -1
  16. package/dist/types/src/components/TextEditor/TextEditor.d.ts +24 -20
  17. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  18. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +8 -10
  19. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  20. package/dist/types/{stories/html/code-mirror.stories.d.ts → src/components/TextEditor/codemirror.stories.d.ts} +2 -2
  21. package/dist/types/src/components/TextEditor/codemirror.stories.d.ts.map +1 -0
  22. package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts +8 -0
  23. package/dist/types/src/components/TextEditor/extensions/basic/bundle.d.ts.map +1 -0
  24. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts +3 -0
  25. package/dist/types/src/components/TextEditor/extensions/basic/index.d.ts.map +1 -0
  26. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts +10 -0
  27. package/dist/types/src/components/TextEditor/extensions/basic/theme.d.ts.map +1 -0
  28. package/dist/types/src/components/TextEditor/extensions/change.d.ts +7 -0
  29. package/dist/types/src/components/TextEditor/extensions/change.d.ts.map +1 -0
  30. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts +11 -0
  31. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.d.ts.map +1 -0
  32. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts +8 -0
  33. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.d.ts.map +1 -0
  34. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts +10 -0
  35. package/dist/types/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.d.ts.map +1 -0
  36. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts +4 -0
  37. package/dist/types/src/components/TextEditor/extensions/hyperlink/index.d.ts.map +1 -0
  38. package/dist/types/src/components/TextEditor/extensions/index.d.ts +5 -0
  39. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -0
  40. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts +8 -0
  41. package/dist/types/src/components/TextEditor/extensions/markdown/bundle.d.ts.map +1 -0
  42. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts +3 -0
  43. package/dist/types/src/components/TextEditor/extensions/markdown/index.d.ts.map +1 -0
  44. package/dist/types/src/components/{Markdown/markdownTags.d.ts → TextEditor/extensions/markdown/tags.d.ts} +1 -1
  45. package/dist/types/src/components/TextEditor/extensions/markdown/tags.d.ts.map +1 -0
  46. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts +21 -0
  47. package/dist/types/src/components/TextEditor/extensions/markdown/theme.d.ts.map +1 -0
  48. package/dist/types/src/components/TextEditor/index.d.ts +1 -2
  49. package/dist/types/src/components/TextEditor/index.d.ts.map +1 -1
  50. package/dist/types/src/components/TextEditor/yjs.stories.d.ts +11 -0
  51. package/dist/types/src/components/TextEditor/yjs.stories.d.ts.map +1 -0
  52. package/dist/types/src/components/index.d.ts +0 -3
  53. package/dist/types/src/components/index.d.ts.map +1 -1
  54. package/dist/types/src/hooks/index.d.ts +4 -0
  55. package/dist/types/src/hooks/index.d.ts.map +1 -0
  56. package/dist/types/src/hooks/useCollaboration.d.ts +9 -0
  57. package/dist/types/src/hooks/useCollaboration.d.ts.map +1 -0
  58. package/dist/types/src/{model.d.ts → hooks/useTextModel.d.ts} +3 -15
  59. package/dist/types/src/hooks/useTextModel.d.ts.map +1 -0
  60. package/dist/types/src/hooks/yjs/cursors.d.ts.map +1 -0
  61. package/dist/types/src/hooks/yjs/index.d.ts.map +1 -0
  62. package/dist/types/src/hooks/yjs/space-provider.d.ts.map +1 -0
  63. package/dist/types/src/index.d.ts +2 -2
  64. package/dist/types/src/index.d.ts.map +1 -1
  65. package/dist/types/src/styles/tokens.d.ts +4 -0
  66. package/dist/types/src/styles/tokens.d.ts.map +1 -1
  67. package/dist/types/src/testing/replicator.d.ts +1 -1
  68. package/dist/types/src/testing/replicator.d.ts.map +1 -1
  69. package/package.json +32 -26
  70. package/src/automerge/automerge-plugin/PatchSemaphore.ts +5 -5
  71. package/src/automerge/automerge-plugin/codeMirrorToAm.ts +10 -7
  72. package/src/automerge/automerge-plugin/handle.ts +14 -0
  73. package/src/automerge/automerge-plugin/index.ts +2 -2
  74. package/src/automerge/automerge-plugin/plugin.ts +39 -10
  75. package/src/automerge/automerge.stories.tsx +32 -47
  76. package/src/components/TextEditor/TextEditor.stories.tsx +56 -8
  77. package/src/components/TextEditor/TextEditor.tsx +113 -55
  78. package/src/components/TextEditor/codemirror.stories.ts +114 -0
  79. package/src/components/TextEditor/extensions/basic/bundle.ts +26 -0
  80. package/src/components/TextEditor/extensions/basic/index.ts +6 -0
  81. package/src/components/TextEditor/extensions/basic/theme.ts +49 -0
  82. package/src/components/TextEditor/extensions/change.ts +22 -0
  83. package/src/components/TextEditor/extensions/hyperlink/hyperlinkDecoration.ts +99 -0
  84. package/src/components/TextEditor/extensions/hyperlink/hyperlinkTooltip.tsx +61 -0
  85. package/src/components/TextEditor/extensions/hyperlink/hyperlinkWidget.tsx +119 -0
  86. package/src/components/TextEditor/extensions/hyperlink/index.ts +7 -0
  87. package/src/components/TextEditor/extensions/index.ts +8 -0
  88. package/src/components/TextEditor/extensions/markdown/bundle.ts +99 -0
  89. package/src/components/TextEditor/extensions/markdown/index.ts +6 -0
  90. package/src/components/{Markdown/markdownTheme.ts → TextEditor/extensions/markdown/theme.ts} +42 -27
  91. package/src/components/TextEditor/index.ts +1 -2
  92. package/src/components/TextEditor/yjs.stories.tsx +55 -0
  93. package/src/components/index.ts +0 -3
  94. package/src/hooks/index.ts +7 -0
  95. package/src/hooks/useCollaboration.ts +45 -0
  96. package/src/{model.ts → hooks/useTextModel.ts} +42 -17
  97. package/src/index.ts +3 -3
  98. package/src/styles/tokens.ts +6 -0
  99. package/src/testing/replicator.ts +1 -2
  100. package/dist/types/src/automerge/demo.d.ts +0 -25
  101. package/dist/types/src/automerge/demo.d.ts.map +0 -1
  102. package/dist/types/src/components/Editor/Editor.d.ts +0 -15
  103. package/dist/types/src/components/Editor/Editor.d.ts.map +0 -1
  104. package/dist/types/src/components/Editor/Editor.stories.d.ts +0 -28
  105. package/dist/types/src/components/Editor/Editor.stories.d.ts.map +0 -1
  106. package/dist/types/src/components/Editor/index.d.ts +0 -2
  107. package/dist/types/src/components/Editor/index.d.ts.map +0 -1
  108. package/dist/types/src/components/Markdown/Markdown.d.ts +0 -19
  109. package/dist/types/src/components/Markdown/Markdown.d.ts.map +0 -1
  110. package/dist/types/src/components/Markdown/Markdown.stories.d.ts +0 -19
  111. package/dist/types/src/components/Markdown/Markdown.stories.d.ts.map +0 -1
  112. package/dist/types/src/components/Markdown/index.d.ts +0 -2
  113. package/dist/types/src/components/Markdown/index.d.ts.map +0 -1
  114. package/dist/types/src/components/Markdown/markdownTags.d.ts.map +0 -1
  115. package/dist/types/src/components/Markdown/markdownTheme.d.ts +0 -107
  116. package/dist/types/src/components/Markdown/markdownTheme.d.ts.map +0 -1
  117. package/dist/types/src/components/RichText/RichText.d.ts +0 -20
  118. package/dist/types/src/components/RichText/RichText.d.ts.map +0 -1
  119. package/dist/types/src/components/RichText/RichText.stories.d.ts +0 -26
  120. package/dist/types/src/components/RichText/RichText.stories.d.ts.map +0 -1
  121. package/dist/types/src/components/RichText/index.d.ts +0 -2
  122. package/dist/types/src/components/RichText/index.d.ts.map +0 -1
  123. package/dist/types/src/components/TextEditor/theme.d.ts +0 -8
  124. package/dist/types/src/components/TextEditor/theme.d.ts.map +0 -1
  125. package/dist/types/src/model.d.ts.map +0 -1
  126. package/dist/types/src/yjs/cursors.d.ts.map +0 -1
  127. package/dist/types/src/yjs/index.d.ts.map +0 -1
  128. package/dist/types/src/yjs/space-provider.d.ts.map +0 -1
  129. package/dist/types/stories/html/code-mirror.stories.d.ts.map +0 -1
  130. package/src/automerge/demo.ts +0 -106
  131. package/src/components/Editor/Editor.stories.tsx +0 -80
  132. package/src/components/Editor/Editor.tsx +0 -33
  133. package/src/components/Editor/index.ts +0 -5
  134. package/src/components/Markdown/Markdown.stories.tsx +0 -57
  135. package/src/components/Markdown/Markdown.tsx +0 -210
  136. package/src/components/Markdown/index.ts +0 -5
  137. package/src/components/RichText/RichText.stories.tsx +0 -68
  138. package/src/components/RichText/RichText.tsx +0 -181
  139. package/src/components/RichText/index.ts +0 -5
  140. package/src/components/TextEditor/theme.ts +0 -29
  141. /package/dist/types/src/{yjs → hooks/yjs}/cursors.d.ts +0 -0
  142. /package/dist/types/src/{yjs → hooks/yjs}/index.d.ts +0 -0
  143. /package/dist/types/src/{yjs → hooks/yjs}/space-provider.d.ts +0 -0
  144. /package/src/components/{Markdown/markdownTags.ts → TextEditor/extensions/markdown/tags.ts} +0 -0
  145. /package/src/{yjs → hooks/yjs}/cursors.ts +0 -0
  146. /package/src/{yjs → hooks/yjs}/index.ts +0 -0
  147. /package/src/{yjs → hooks/yjs}/space-provider.ts +0 -0
@@ -0,0 +1,119 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ // Adapted from: https://github.com/uiwjs/react-codemirror/blob/master/extensions/hyper-link/src/index.ts
4
+ //
5
+
6
+ import { type Extension } from '@codemirror/state';
7
+ import {
8
+ Decoration,
9
+ type DecorationSet,
10
+ EditorView,
11
+ MatchDecorator,
12
+ ViewPlugin,
13
+ type ViewUpdate,
14
+ } from '@codemirror/view';
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.
21
+
22
+ const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)(!?)/gi;
23
+
24
+ /**
25
+ * Custom link decorator.
26
+ * NOTE: The default markdown extension formats links by default.
27
+ * @deprecated
28
+ */
29
+ const linkDecorator = () =>
30
+ new MatchDecorator({
31
+ regexp: markdownLinkRegexp,
32
+ decorate: (add, from, to, match, view) => {
33
+ const [_, label, url] = match;
34
+ const p0 = {
35
+ start: from,
36
+ end: from + 1,
37
+ };
38
+
39
+ const p1 = {
40
+ start: from + 1,
41
+ end: from + 1 + label.length,
42
+ };
43
+
44
+ const p2 = {
45
+ start: p1.end,
46
+ end: p1.end + 1,
47
+ };
48
+
49
+ const p3 = {
50
+ start: p1.end + 1,
51
+ end: p1.end + 1 + url.length + 2,
52
+ };
53
+
54
+ add(
55
+ p0.start,
56
+ p0.end,
57
+ Decoration.mark({ class: 'cm-hyperlink-bracket', _attributes: { style: 'display: none' } }),
58
+ );
59
+ add(p1.start, p1.end, Decoration.mark({ class: 'cm-hyperlink-label' }));
60
+ add(
61
+ p2.start,
62
+ p2.end,
63
+ Decoration.mark({ class: 'cm-hyperlink-bracket', _attributes: { style: 'display: none' } }),
64
+ );
65
+ add(p3.start, p3.end, Decoration.mark({ class: 'cm-hyperlink-url', _attributes: { style: 'display: none' } }));
66
+ },
67
+ });
68
+
69
+ export function hyperLinkExtension() {
70
+ return ViewPlugin.fromClass(
71
+ class HyperLinkView {
72
+ readonly decorator: MatchDecorator;
73
+ decorations: DecorationSet;
74
+ constructor(view: EditorView) {
75
+ this.decorator = linkDecorator();
76
+ this.decorations = this.decorator.createDeco(view);
77
+ }
78
+
79
+ update(update: ViewUpdate) {
80
+ if (update.docChanged || update.viewportChanged) {
81
+ this.decorations = this.decorator.updateDeco(update, this.decorations);
82
+ }
83
+ }
84
+ },
85
+ {
86
+ decorations: (view) => view.decorations,
87
+ eventHandlers: {
88
+ // TODO(burdon): CLick to expand link.
89
+ // https://discuss.codemirror.net/t/decorator-iteration-on-click-event/4226/2
90
+ mousedown: function (event, view) {
91
+ const pos = view.posAtDOM(event.target as Node);
92
+ if (pos !== 0) {
93
+ console.log(this);
94
+ }
95
+ },
96
+ // TODO(burdon): Hide when cursor moves away.
97
+ keydown: (event, view) => {
98
+ view.update([]);
99
+ },
100
+ },
101
+ },
102
+ );
103
+ }
104
+
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];
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './hyperlinkDecoration';
6
+ export * from './hyperlinkTooltip';
7
+ export * from './hyperlinkWidget';
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './basic';
6
+ export * from './change';
7
+ export * from './hyperlink';
8
+ export * from './markdown';
@@ -0,0 +1,99 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
6
+ import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
7
+ import { markdownLanguage, markdown } from '@codemirror/lang-markdown';
8
+ import {
9
+ bracketMatching,
10
+ defaultHighlightStyle,
11
+ foldKeymap,
12
+ indentOnInput,
13
+ syntaxHighlighting,
14
+ } from '@codemirror/language';
15
+ import { languages } from '@codemirror/language-data';
16
+ import { lintKeymap } from '@codemirror/lint';
17
+ import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
18
+ import { EditorState, type Extension } from '@codemirror/state';
19
+ import { oneDarkHighlightStyle } from '@codemirror/theme-one-dark';
20
+ import {
21
+ crosshairCursor,
22
+ drawSelection,
23
+ dropCursor,
24
+ EditorView,
25
+ highlightActiveLine,
26
+ highlightActiveLineGutter,
27
+ highlightSpecialChars,
28
+ keymap,
29
+ placeholder,
30
+ rectangularSelection,
31
+ } from '@codemirror/view';
32
+ // import { GFM } from '@lezer/markdown';
33
+
34
+ import type { ThemeMode } from '@dxos/react-ui';
35
+
36
+ import { markdownTagsExtension } from './tags';
37
+ import { markdownHighlightStyle } from './theme';
38
+
39
+ export type MarkdownBundleOptions = {
40
+ themeMode?: ThemeMode;
41
+ placeholder?: string;
42
+ };
43
+
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
47
+ return [
48
+ bracketMatching(),
49
+ closeBrackets(),
50
+ _placeholder && placeholder(_placeholder),
51
+
52
+ EditorState.allowMultipleSelections.of(true),
53
+ EditorView.lineWrapping,
54
+
55
+ // autocompletion(),
56
+ crosshairCursor(),
57
+ dropCursor(),
58
+ drawSelection(),
59
+ highlightActiveLine(),
60
+ highlightActiveLineGutter(),
61
+ highlightSelectionMatches(),
62
+ highlightSpecialChars(),
63
+ history(),
64
+ indentOnInput(),
65
+ rectangularSelection(),
66
+
67
+ keymap.of([
68
+ ...closeBracketsKeymap,
69
+ // ...completionKeymap,
70
+ ...defaultKeymap,
71
+ ...foldKeymap,
72
+ ...historyKeymap,
73
+ ...lintKeymap,
74
+ ...searchKeymap,
75
+ indentWithTab,
76
+ ]),
77
+
78
+ // Main extension.
79
+ // https://github.com/codemirror/lang-markdown
80
+ markdown({
81
+ base: markdownLanguage,
82
+ 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
+
90
+ // Custom styling.
91
+ markdownTagsExtension,
92
+ ],
93
+ }),
94
+
95
+ // TODO(thure): All but one rule here apply to both themes; rename or refactor.
96
+ syntaxHighlighting(markdownHighlightStyle),
97
+ themeMode === 'dark' ? syntaxHighlighting(oneDarkHighlightStyle) : syntaxHighlighting(defaultHighlightStyle),
98
+ ].filter(Boolean) as Extension[];
99
+ };
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './bundle';
6
+ export * from './theme';
@@ -7,12 +7,10 @@ import { HighlightStyle } from '@codemirror/language';
7
7
  import { tags } from '@lezer/highlight';
8
8
  import get from 'lodash.get';
9
9
 
10
- import { markdownTags } from './markdownTags';
11
- import { bold, heading, italic, mark, strikethrough, tokens } from '../../styles';
12
-
13
- // TODO(burdon): Use theme colors.
14
- // TODO(burdon): Light mode.
10
+ import { markdownTags } from './tags';
11
+ import { bold, heading, italic, mark, strikethrough, tokens, type ThemeStyles } from '../../../../styles';
15
12
 
13
+ // TODO(burdon): Rationalize theme/colors. Hoist onto single property object.
16
14
  export const chalky = '#e5c07b';
17
15
  export const coral = '#e06c75';
18
16
  export const cyan = '#56b6c2';
@@ -28,12 +26,15 @@ export const highlightBackground = '#2c313a';
28
26
  const _background = '#282c34';
29
27
  export const tooltipBackground = '#353a42';
30
28
  const _selection = '#3E4451';
31
- export const cursor = '#ffffff';
32
29
 
33
30
  const monospace = get(tokens, 'fontFamily.mono', ['monospace']).join(',');
34
31
 
35
- export const markdownTheme = {
36
- // TODO(thure): consider whether these commented-out rules from one-dark-theme should be integrated.
32
+ /**
33
+ * NOTE: The '&' prefix denotes the CM editor root.
34
+ * https://codemirror.net/examples/styling
35
+ */
36
+ export const markdownTheme: ThemeStyles = {
37
+ // TODO(thure): Consider whether these commented-out rules from one-dark-theme should be integrated.
37
38
  // '&': {
38
39
  // color: ivory,
39
40
  // backgroundColor: background
@@ -71,29 +72,34 @@ export const markdownTheme = {
71
72
  // color: '#ddd'
72
73
  // },
73
74
 
74
- '.dark & .cm-tooltip': {
75
+ '& .cm-tooltip': {
76
+ backgroundColor: 'transparent',
75
77
  border: 'none',
76
- backgroundColor: tooltipBackground,
77
- },
78
- '.dark & .cm-tooltip .cm-tooltip-arrow:before': {
79
- borderTopColor: 'transparent',
80
- borderBottomColor: 'transparent',
81
- },
82
- '.dark & .cm-tooltip .cm-tooltip-arrow:after': {
83
- borderTopColor: tooltipBackground,
84
- borderBottomColor: tooltipBackground,
85
- },
86
- '.dark & .cm-tooltip-autocomplete': {
87
- '& > ul > li[aria-selected]': {
88
- backgroundColor: highlightBackground,
89
- color: ivory,
90
- },
91
78
  },
79
+ // '.dark & .cm-tooltip': {
80
+ // border: 'none',
81
+ // backgroundColor: tooltipBackground,
82
+ // },
83
+ // '.dark & .cm-tooltip .cm-tooltip-arrow:before': {
84
+ // borderTopColor: 'transparent',
85
+ // borderBottomColor: 'transparent',
86
+ // },
87
+ // '.dark & .cm-tooltip .cm-tooltip-arrow:after': {
88
+ // borderTopColor: tooltipBackground,
89
+ // borderBottomColor: tooltipBackground,
90
+ // },
91
+ // '.dark & .cm-tooltip-autocomplete': {
92
+ // '& > ul > li[aria-selected]': {
93
+ // backgroundColor: highlightBackground,
94
+ // color: ivory,
95
+ // },
96
+ // },
92
97
  '&.cm-focused': {
93
98
  outline: 'none',
94
99
  },
95
100
  '& .cm-line': {
96
101
  paddingInline: 0,
102
+ lineHeight: 1.6,
97
103
  minBlockSize: '1.6em',
98
104
  },
99
105
  '& .cm-line *': {
@@ -112,16 +118,17 @@ export const markdownTheme = {
112
118
  background: get(tokens, 'extend.colors.primary.600', '#00ffff') + '44',
113
119
  },
114
120
  '& .cm-content': {
121
+ padding: 0,
115
122
  caretColor: 'black',
116
123
  },
117
124
  '.dark & .cm-content': {
118
- caretColor: cursor,
125
+ caretColor: 'white',
119
126
  },
120
127
  '& .cm-cursor': {
121
128
  borderLeftColor: 'black',
122
129
  },
123
130
  '.dark & .cm-cursor': {
124
- borderLeftColor: cursor,
131
+ borderLeftColor: 'white',
125
132
  },
126
133
  '.cm-placeholder': {
127
134
  fontFamily: get(tokens, 'fontFamily.body', []).join(','),
@@ -160,6 +167,14 @@ export const markdownTheme = {
160
167
  '& .cm-yLineSelection': {
161
168
  margin: '0',
162
169
  },
170
+ '.cm-link': {
171
+ color: 'rgb(20 89 208)', // TODO(burdon): Rationalize.
172
+ textDecorationLine: 'underline',
173
+ textDecorationThickness: '1px',
174
+ textUnderlineOffset: '2px',
175
+ borderRadius: '.125rem',
176
+ },
177
+ // TODO(burdon): Document.
163
178
  ...Object.keys(get(tokens, 'extend.fontSize', {})).reduce((acc: Record<string, any>, fontSize) => {
164
179
  const height = get(tokens, ['extend', 'fontSize', fontSize, 1, 'lineHeight']);
165
180
  // TODO(thure): This appears to be the best or only way to set selection caret heights, but it's far more verbose than it needs to be.
@@ -170,7 +185,7 @@ export const markdownTheme = {
170
185
  }, {}),
171
186
  };
172
187
 
173
- export const markdownDarkHighlighting = HighlightStyle.define(
188
+ export const markdownHighlightStyle = HighlightStyle.define(
174
189
  [
175
190
  {
176
191
  tag: [
@@ -2,8 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export { type StyleSpec } from 'style-mod';
6
5
  export { tags } from '@lezer/highlight';
7
6
 
7
+ export * from './extensions';
8
8
  export * from './TextEditor';
9
- export * from './theme';
@@ -0,0 +1,55 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import React, { useState } from 'react';
8
+
9
+ import { PublicKey } from '@dxos/react-client';
10
+ import { TextKind } from '@dxos/react-client/echo';
11
+ import { ClientRepeater, textGenerator, useDataGenerator } from '@dxos/react-client/testing';
12
+ import { useId, Input } from '@dxos/react-ui';
13
+ import { withTheme } from '@dxos/storybook-utils';
14
+
15
+ import { MarkdownEditor } from './TextEditor';
16
+ import { Replicator, useYjsModel } from '../../testing';
17
+
18
+ export default {
19
+ component: MarkdownEditor,
20
+ decorators: [withTheme],
21
+ };
22
+
23
+ const replicator = new Replicator(TextKind.PLAIN);
24
+
25
+ const Story = () => {
26
+ const [generate, setGenerate] = useState(false);
27
+ const generateId = useId('generate');
28
+
29
+ const [id] = useState(PublicKey.random().toHex());
30
+ const model = useYjsModel({ id, replicator });
31
+
32
+ useDataGenerator({
33
+ generator: generate ? textGenerator : undefined,
34
+ options: { text: typeof model?.content !== 'string' ? (model?.content as any) : undefined },
35
+ });
36
+
37
+ return (
38
+ <main className='flex flex-col flex-1 p-4 gap-4'>
39
+ <div id={generateId} className='flex gap-2'>
40
+ <Input.Root>
41
+ <Input.Checkbox checked={generate} onCheckedChange={(checked) => setGenerate(!!checked)} />
42
+ <Input.Label>Start</Input.Label>
43
+ </Input.Root>
44
+ </div>
45
+ <div className='border'>
46
+ <MarkdownEditor model={model} />
47
+ </div>
48
+ </main>
49
+ );
50
+ };
51
+
52
+ export const Default = {
53
+ // TODO(wittjosiah): Decorator for doing this without clients being initialized?
54
+ render: () => <ClientRepeater count={2} Component={Story} />,
55
+ };
@@ -2,7 +2,4 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- export * from './Editor';
6
- export * from './Markdown';
7
- export * from './RichText';
8
5
  export * from './TextEditor';
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './useCollaboration';
6
+ export * from './useTextModel';
7
+ export * from './yjs';
@@ -0,0 +1,45 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import type { Extension } from '@codemirror/state';
6
+ import { useEffect, useMemo } from 'react';
7
+ import { yCollab } from 'y-codemirror.next';
8
+
9
+ import { generateName } from '@dxos/display-name';
10
+ import { isDocAccessor } from '@dxos/echo-schema';
11
+ import type { ThemeMode } from '@dxos/react-ui';
12
+ import { getColorForValue } from '@dxos/react-ui-theme';
13
+ import { YText } from '@dxos/text-model';
14
+
15
+ import type { EditorModel } from './useTextModel';
16
+ import { automergePlugin } from '../automerge/automerge-plugin';
17
+
18
+ /**
19
+ * Replication and awareness (incl. remote selection).
20
+ * https://codemirror.net/docs/ref/#collab
21
+ */
22
+ export const useCollaboration = (model: EditorModel, themeMode: ThemeMode): Extension | undefined => {
23
+ const { provider, peer, content } = model;
24
+ const extension = useMemo(() => {
25
+ if (content instanceof YText) {
26
+ return yCollab(content, provider?.awareness);
27
+ } else if (isDocAccessor(content)) {
28
+ return automergePlugin(content.handle, content.path);
29
+ } else {
30
+ return undefined;
31
+ }
32
+ }, []);
33
+
34
+ useEffect(() => {
35
+ if (provider && peer) {
36
+ provider.awareness.setLocalStateField('user', {
37
+ name: peer.name ?? generateName(peer.id),
38
+ color: getColorForValue({ value: peer.id, type: 'color' }),
39
+ colorLight: getColorForValue({ value: peer.id, themeMode, type: 'highlight' }),
40
+ });
41
+ }
42
+ }, [provider, peer, themeMode]);
43
+
44
+ return extension;
45
+ };
@@ -2,34 +2,30 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type EditorView } from '@codemirror/view';
6
- import { type ComponentProps, useMemo } from 'react';
5
+ import { useMemo } from 'react';
7
6
  import type * as awarenessProtocol from 'y-protocols/awareness';
8
7
 
9
- import { type Space, type TextObject } from '@dxos/react-client/echo';
8
+ import {
9
+ type DocAccessor,
10
+ type Space,
11
+ type TextObject,
12
+ type AutomergeTextCompat,
13
+ getRawDoc,
14
+ isActualAutomergeObject,
15
+ } from '@dxos/react-client/echo';
10
16
  import { type Identity } from '@dxos/react-client/halo';
11
17
  import type { YText, YXmlFragment } from '@dxos/text-model';
12
18
 
13
19
  import { SpaceAwarenessProvider } from './yjs';
14
20
 
15
- export type EditorSlots = {
16
- root?: Omit<ComponentProps<'div'>, 'ref'>;
17
- editor?: {
18
- className?: string;
19
- placeholder?: string;
20
- spellCheck?: boolean;
21
- tabIndex?: number;
22
- markdownTheme?: Parameters<typeof EditorView.theme>[0];
23
- };
24
- };
25
-
21
+ // TODO(burdon): Move.
26
22
  type Awareness = awarenessProtocol.Awareness;
27
23
  type Provider = { awareness: Awareness };
28
24
 
29
25
  // TODO(wittjosiah): Factor out to common package? @dxos/react-client?
30
26
  export type EditorModel = {
31
27
  id: string;
32
- content: string | YText | YXmlFragment;
28
+ content: string | YText | YXmlFragment | DocAccessor;
33
29
  provider?: Provider;
34
30
  peer?: {
35
31
  id: string;
@@ -43,16 +39,45 @@ export type UseTextModelOptions = {
43
39
  text?: TextObject;
44
40
  };
45
41
 
46
- // TODO(wittjosiah): Factor out to common package? @dxos/react-client?
42
+ // TODO(burdon): Don't support returning undefined (and remove checks from calling code).
47
43
  // TODO(burdon): Decouple space (make Editor less dependent on entire stack)?
44
+ // TODO(wittjosiah): Factor out to common package? @dxos/react-client?
48
45
  export const useTextModel = ({ identity, space, text }: UseTextModelOptions): EditorModel | undefined => {
49
46
  const provider = useMemo(() => {
47
+ if (isActualAutomergeObject(text)) {
48
+ return undefined;
49
+ }
50
+
50
51
  if (!space || !text?.doc) {
51
52
  return undefined;
52
53
  }
53
54
 
54
55
  return new SpaceAwarenessProvider({ space, doc: text.doc, channel: `yjs.awareness.${text.id}` });
55
- }, [identity, space, text?.doc]);
56
+ }, [identity, space, text, text?.doc]);
57
+
58
+ const content = useMemo(() => {
59
+ if (isActualAutomergeObject(text)) {
60
+ const obj = text as any as AutomergeTextCompat;
61
+ return getRawDoc(obj, [obj.field]);
62
+ } else {
63
+ return text?.content;
64
+ }
65
+ }, [text]);
66
+
67
+ if (isActualAutomergeObject(text)) {
68
+ const obj = text as any as AutomergeTextCompat;
69
+ return {
70
+ id: obj.id,
71
+ content: content!,
72
+ provider: undefined,
73
+ peer: identity
74
+ ? {
75
+ id: identity.identityKey.toHex(),
76
+ name: identity.profile?.displayName,
77
+ }
78
+ : undefined,
79
+ };
80
+ }
56
81
 
57
82
  if (!text?.doc || !text?.content) {
58
83
  return undefined;
package/src/index.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  // Copyright 2022 DXOS.org
3
3
  //
4
4
 
5
- export * from './components';
6
- export * from './model';
7
-
8
5
  export { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
9
6
  export { Doc, YText, YXmlFragment } from '@dxos/text-model';
7
+
8
+ export * from './components';
9
+ export * from './hooks';
@@ -2,7 +2,13 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ import type { StyleSpec } from 'style-mod';
6
+
5
7
  import { tailwindConfig, type TailwindConfig } from '@dxos/react-ui-theme';
6
8
 
9
+ export type ThemeStyles = {
10
+ [selector: string]: StyleSpec;
11
+ };
12
+
7
13
  // TODO(thure): Why export the whole theme? Can this be done differently?
8
14
  export const tokens: TailwindConfig['theme'] = tailwindConfig({}).theme;
@@ -15,8 +15,7 @@ import { Event } from '@dxos/async';
15
15
  import { log } from '@dxos/log';
16
16
  import { TextKind } from '@dxos/protocols/proto/dxos/echo/model/text';
17
17
 
18
- import { type EditorModel } from '../model';
19
- import { cursorColor } from '../yjs';
18
+ import { cursorColor, type EditorModel } from '../hooks';
20
19
 
21
20
  type Awareness = awarenessProtocol.Awareness;
22
21
 
@@ -1,25 +0,0 @@
1
- import { Event } from '@dxos/async';
2
- import { type Doc, next as automerge, type ChangeOptions, type ChangeFn, type Heads } from '@dxos/automerge/automerge';
3
- export declare class Peer {
4
- changeEvent: Event<void>;
5
- storage: Map<string, Uint8Array>;
6
- stats: {
7
- messagesSent: number;
8
- messagesReceived: number;
9
- bytesSent: number;
10
- bytesReceived: number;
11
- snapshotBytes: number;
12
- incrementalBytes: number;
13
- };
14
- doc: Doc<{
15
- text: string;
16
- }>;
17
- change(options: string | ChangeOptions<any> | ChangeFn<any>, callback?: ChangeFn<any>): void;
18
- changeAt(scope: Heads, options: string | ChangeOptions<any> | ChangeFn<any>, callback?: ChangeFn<any>): automerge.Heads | null;
19
- replicate(): {
20
- readable: ReadableStream<Uint8Array>;
21
- writable: WritableStream<Uint8Array>;
22
- };
23
- private _handleChange;
24
- }
25
- //# sourceMappingURL=demo.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"demo.d.ts","sourceRoot":"","sources":["../../../../src/automerge/demo.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EACL,KAAK,GAAG,EACR,IAAI,IAAI,SAAS,EACjB,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,KAAK,EAEX,MAAM,2BAA2B,CAAC;AAEnC,qBAAa,IAAI;IACf,WAAW,cAAe;IAE1B,OAAO,0BAAiC;IACxC,KAAK;;;;;;;MAOH;IAEF,GAAG,EAAG,GAAG,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAE5B,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;IAKrF,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;IAOrG,SAAS,IAAI;QAAE,QAAQ,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;QAAC,QAAQ,EAAE,cAAc,CAAC,UAAU,CAAC,CAAA;KAAE;IAgD3F,OAAO,CAAC,aAAa;CAgBtB"}
@@ -1,15 +0,0 @@
1
- import React from 'react';
2
- import { type EditorSlots, type UseTextModelOptions } from '../../model';
3
- import { type MarkdownEditorRef } from '../Markdown';
4
- export type EditorProps = UseTextModelOptions & {
5
- slots?: EditorSlots;
6
- };
7
- /**
8
- * Memoized editor which depends on DXOS platform.
9
- *
10
- * Determines which editor to render based on the kind of text.
11
- */
12
- export declare const Editor: React.MemoExoticComponent<React.ForwardRefExoticComponent<UseTextModelOptions & {
13
- slots?: EditorSlots | undefined;
14
- } & React.RefAttributes<MarkdownEditorRef | import("@tiptap/react").Editor>>>;
15
- //# sourceMappingURL=Editor.d.ts.map