@dxos/react-ui-editor 0.3.9-next.4584ca9 → 0.3.9-next.7522f7a

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 (36) hide show
  1. package/dist/lib/browser/index.mjs +183 -102
  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/Editor/Editor.d.ts +0 -1
  5. package/dist/types/src/components/Editor/Editor.d.ts.map +1 -1
  6. package/dist/types/src/components/Markdown/Markdown.d.ts +2 -1
  7. package/dist/types/src/components/Markdown/Markdown.d.ts.map +1 -1
  8. package/dist/types/src/components/Markdown/Markdown.stories.d.ts.map +1 -1
  9. package/dist/types/src/components/Markdown/markdownTheme.d.ts +8 -1
  10. package/dist/types/src/components/Markdown/markdownTheme.d.ts.map +1 -1
  11. package/dist/types/src/components/TextEditor/TextEditor.d.ts.map +1 -1
  12. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts +0 -14
  13. package/dist/types/src/components/TextEditor/TextEditor.stories.d.ts.map +1 -1
  14. package/dist/types/src/components/TextEditor/extensions/hyperlinkDecoration.d.ts +11 -0
  15. package/dist/types/src/components/TextEditor/extensions/hyperlinkDecoration.d.ts.map +1 -0
  16. package/dist/types/src/components/TextEditor/extensions/hyperlinkTooltip.d.ts +0 -1
  17. package/dist/types/src/components/TextEditor/extensions/hyperlinkTooltip.d.ts.map +1 -1
  18. package/dist/types/src/components/TextEditor/extensions/{hyperlink.d.ts → hyperlinkWidget.d.ts} +3 -3
  19. package/dist/types/src/components/TextEditor/extensions/hyperlinkWidget.d.ts.map +1 -0
  20. package/dist/types/src/components/TextEditor/extensions/index.d.ts +2 -1
  21. package/dist/types/src/components/TextEditor/extensions/index.d.ts.map +1 -1
  22. package/dist/types/src/components/TextEditor/theme.d.ts +1 -0
  23. package/dist/types/src/components/TextEditor/theme.d.ts.map +1 -1
  24. package/package.json +18 -18
  25. package/src/components/Editor/Editor.tsx +4 -1
  26. package/src/components/Markdown/Markdown.stories.tsx +12 -1
  27. package/src/components/Markdown/Markdown.tsx +47 -22
  28. package/src/components/Markdown/markdownTheme.ts +12 -1
  29. package/src/components/TextEditor/TextEditor.stories.tsx +43 -54
  30. package/src/components/TextEditor/TextEditor.tsx +1 -0
  31. package/src/components/TextEditor/extensions/hyperlinkDecoration.ts +99 -0
  32. package/src/components/TextEditor/extensions/hyperlinkTooltip.tsx +2 -17
  33. package/src/components/TextEditor/extensions/{hyperlink.tsx → hyperlinkWidget.tsx} +21 -10
  34. package/src/components/TextEditor/extensions/index.ts +2 -1
  35. package/src/components/TextEditor/theme.ts +6 -4
  36. package/dist/types/src/components/TextEditor/extensions/hyperlink.d.ts.map +0 -1
@@ -0,0 +1,99 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ // Copyright CodeMirror
4
+ //
5
+
6
+ import { syntaxTree } from '@codemirror/language';
7
+ import { type EditorState, type RangeSet, RangeSetBuilder, StateField, type Transaction } from '@codemirror/state';
8
+ import { Decoration, EditorView, WidgetType } from '@codemirror/view';
9
+
10
+ // Adapted from:
11
+ // https://codemirror.net/try/?c=aW1wb3J0IHsgYmFzaWNTZXR1cCwgRWRpdG9yVmlldyB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7IG1hcmtkb3duIH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1tYXJrZG93biIKaW1wb3J0IHsgc3ludGF4VHJlZSB9IGZyb20gIkBjb2RlbWlycm9yL2xhbmd1YWdlIjsKaW1wb3J0IHsgRWRpdG9yU3RhdGUsIFJhbmdlU2V0QnVpbGRlciwgU3RhdGVGaWVsZCB9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIjsKaW1wb3J0IHsgRGVjb3JhdGlvbiwgV2lkZ2V0VHlwZSB9IGZyb20gIkBjb2RlbWlycm9yL3ZpZXciOwoKY2xhc3MgTGluayBleHRlbmRzIFdpZGdldFR5cGUgewogIGNvbnN0cnVjdG9yKHRleHQsIHVybCkgewogICAgc3VwZXIoKQogICAgdGhpcy50ZXh0ID0gdGV4dAogICAgdGhpcy51cmwgPSB1cmwKICB9CiAgZXEob3RoZXIpIHsgcmV0dXJuIHRoaXMudGV4dCA9PSBvdGhlci50ZXh0ICYmIHRoaXMudXJsID09IG90aGVyLnVybCB9CiAgdG9ET00oKSB7CiAgICBsZXQgbGluayA9IGRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoImEiKQogICAgbGluay50ZXh0Q29udGVudCA9IHRoaXMudGV4dAogICAgbGluay5ocmVmID0gdGhpcy51cmwKICAgIHJldHVybiBsaW5rOwogIH0KfQoKbGV0IGRlY29yYXRpb25zRmllbGQgPSBTdGF0ZUZpZWxkLmRlZmluZSh7CiAgY3JlYXRlKCkgewogICAgcmV0dXJuIERlY29yYXRpb24ubm9uZTsKICB9LAogIHVwZGF0ZShfLCB0cikgewogICAgY29uc3QgYnVpbGRlciA9IG5ldyBSYW5nZVNldEJ1aWxkZXIoKTsKICAgIGxldCBjdXJzb3IgPSB0ci5zdGF0ZS5zZWxlY3Rpb24ubWFpbi5oZWFkOwogICAgc3ludGF4VHJlZSh0ci5zdGF0ZSkuaXRlcmF0ZSh7CiAgICAgIGVudGVyOiAobm9kZSkgPT4gewogICAgICAgIGlmICgoY3Vyc29yIDwgbm9kZS5mcm9tIHx8IGN1cnNvciA+IG5vZGUudG8pICYmIG5vZGUubmFtZSA9PSAiTGluayIpIHsKICAgICAgICAgIGxldCBtYXJrcyA9IG5vZGUubm9kZS5nZXRDaGlsZHJlbigiTGlua01hcmsiKTsKICAgICAgICAgIGxldCB0ZXh0ID0gbWFya3MubGVuZ3RoID49IDIgPyB0ci5zdGF0ZS5zbGljZURvYyhtYXJrc1swXS50bywgbWFya3NbMV0uZnJvbSkgOiAiIgoKICAgICAgICAgIGxldCB1cmxOb2RlID0gbm9kZS5ub2RlLmdldENoaWxkKCJVUkwiKTsKICAgICAgICAgIGxldCB1cmwgPSB1cmxOb2RlID8gdHIuc3RhdGUuc2xpY2VEb2ModXJsTm9kZS5mcm9tLCB1cmxOb2RlLnRvKSA6ICIiCiAgICAgICAgICBidWlsZGVyLmFkZChub2RlLmZyb20sIG5vZGUudG8sIERlY29yYXRpb24ucmVwbGFjZSh7CiAgICAgICAgICAgIHdpZGdldDogbmV3IExpbmsodGV4dCwgdXJsKSwKICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICApOwogICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgfSwKICAgIH0pOwogICAgcmV0dXJuIGJ1aWxkZXIuZmluaXNoKCk7CiAgfSwKICBwcm92aWRlOiAoZikgPT4gRWRpdG9yVmlldy5kZWNvcmF0aW9ucy5mcm9tKGYpLAp9KTsKCmxldCB2aWV3ID0gbmV3IEVkaXRvclZpZXcoewogIGRvYzogIiMgSGVsbG8gV29ybGRcblxuW1Rlc3RdKGh0dHBzOi8vZXhhbXBsZS5jb20pXG5cblxuIiwKICBleHRlbnNpb25zOiBbZGVjb3JhdGlvbnNGaWVsZCwgbWFya2Rvd24oKV0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5LAp9KTsKCnZpZXcuZm9jdXMoKTs=
12
+ // https://discuss.codemirror.net/t/link-widget-is-clicked-only-if-editor-is-out-of-focus/7433
13
+
14
+ // TODO(burdon): Adapt to make all markdown editable?
15
+
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).
21
+
22
+ class Link extends WidgetType {
23
+ constructor(private readonly _anchor: number, private readonly _text: string, private readonly _url?: string) {
24
+ super();
25
+ }
26
+
27
+ override eq(other: any) {
28
+ return this._text === other.text && this._url === other.url;
29
+ }
30
+
31
+ toDOM(view: EditorView) {
32
+ const link = document.createElement('a');
33
+ link.setAttribute('class', 'cm-link');
34
+ link.textContent = this._text;
35
+ if (this._url) {
36
+ link.setAttribute('target', '_blank');
37
+ link.href = this._url;
38
+ } else {
39
+ link.onclick = () => {
40
+ view.dispatch({
41
+ selection: {
42
+ anchor: this._anchor,
43
+ },
44
+ });
45
+ };
46
+ }
47
+
48
+ return link;
49
+ }
50
+ }
51
+
52
+ type HyperlinkDecorationConfig = {
53
+ link?: boolean;
54
+ };
55
+
56
+ /**
57
+ * Creates a state field to replace AST elements with a hyperlink widget.
58
+ * https://codemirror.net/docs/ref/#state.StateField
59
+ */
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) => {
69
+ const builder = new RangeSetBuilder();
70
+ const cursor = state.selection.main.head;
71
+ syntaxTree(state).iterate({
72
+ enter: (node) => {
73
+ // 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) : '';
81
+
82
+ builder.add(
83
+ node.from,
84
+ node.to,
85
+ Decoration.replace({
86
+ widget: new Link(node.from, text, link ? url : undefined),
87
+ }),
88
+ );
89
+
90
+ return false;
91
+ }
92
+ }
93
+
94
+ return true;
95
+ },
96
+ });
97
+
98
+ return builder.finish();
99
+ };
@@ -3,11 +3,8 @@
3
3
  //
4
4
 
5
5
  import { hoverTooltip } from '@codemirror/view';
6
- import { ArrowCircleUp } from '@phosphor-icons/react';
7
- import React, { StrictMode } from 'react';
8
- import { createRoot } from 'react-dom/client';
9
6
 
10
- import { getSize, mx, tooltipContent } from '@dxos/react-ui-theme';
7
+ import { tooltipContent } from '@dxos/react-ui-theme';
11
8
 
12
9
  const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)/;
13
10
 
@@ -18,6 +15,7 @@ export type OnHyperlinkHover = (el: Element, url: string) => void;
18
15
  * https://codemirror.net/docs/ref/#view.hoverTooltip
19
16
  * https://github.com/codemirror/view/blob/main/src/tooltip.ts
20
17
  */
18
+ // TODO(burdon): Create config object.
21
19
  export const createHyperlinkTooltip = (onHover: OnHyperlinkHover, regexp = markdownLinkRegexp) =>
22
20
  hoverTooltip((view, pos) => {
23
21
  const { from, text } = view.state.doc.lineAt(pos);
@@ -61,16 +59,3 @@ export const createHyperlinkTooltip = (onHover: OnHyperlinkHover, regexp = markd
61
59
  },
62
60
  };
63
61
  });
64
-
65
- // TODO(burdon): Factor out (remove theme/icon deps)?
66
- export const defaultHyperLinkTooltip = createHyperlinkTooltip((el, url) => {
67
- const web = new URL(url);
68
- createRoot(el).render(
69
- <StrictMode>
70
- <div className='flex gap-1 items-center'>
71
- <ArrowCircleUp className={mx(getSize(6), 'text-blue-500')} />
72
- <p className='pr-1'>{web.origin}</p>
73
- </div>
74
- </StrictMode>,
75
- );
76
- });
@@ -15,14 +15,20 @@ import {
15
15
 
16
16
  // Notes: Can't edit widget content (or cursor nav through them).
17
17
  // - https://discuss.codemirror.net/t/focusing-inputs-within-widgets/5178/7
18
- // Widgets intentionally always get set to contenteditable=false, or they would become part of CodeMirror’s editable content element.
18
+ // Widgets intentionally always get set to contenteditable=false,
19
+ // or they would become part of CodeMirror’s editable content element.
19
20
  // You should be able to introduce new contenteditable=true child elements inside of them.
20
21
 
21
22
  const markdownLinkRegexp = /\[([^\]]+)]\(([^)]+)\)(!?)/gi;
22
23
 
24
+ /**
25
+ * Custom link decorator.
26
+ * NOTE: The default markdown extension formats links by default.
27
+ * @deprecated
28
+ */
23
29
  const linkDecorator = () =>
24
30
  new MatchDecorator({
25
- regexp: markdownLinkRegexp, // regexp || defaultRegexp,
31
+ regexp: markdownLinkRegexp,
26
32
  decorate: (add, from, to, match, view) => {
27
33
  const [_, label, url] = match;
28
34
  const p0 = {
@@ -45,16 +51,21 @@ const linkDecorator = () =>
45
51
  end: p1.end + 1 + url.length + 2,
46
52
  };
47
53
 
48
- // TODO(burdon): Wrap with tagName?
49
- add(p0.start, p0.end, Decoration.mark({ class: 'cm-hyperlink-bracket' }));
54
+ add(
55
+ p0.start,
56
+ p0.end,
57
+ Decoration.mark({ class: 'cm-hyperlink-bracket', _attributes: { style: 'display: none' } }),
58
+ );
50
59
  add(p1.start, p1.end, Decoration.mark({ class: 'cm-hyperlink-label' }));
51
- add(p2.start, p2.end, Decoration.mark({ class: 'cm-hyperlink-bracket' }));
52
- add(p3.start, p3.end, Decoration.mark({ class: 'cm-hyperlink-url', __attributes: { style: 'display: none' } }));
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' } }));
53
66
  },
54
67
  });
55
68
 
56
- // const anchor = (dom: HTMLAnchorElement) => dom;
57
-
58
69
  export function hyperLinkExtension() {
59
70
  return ViewPlugin.fromClass(
60
71
  class HyperLinkView {
@@ -91,7 +102,7 @@ export function hyperLinkExtension() {
91
102
  );
92
103
  }
93
104
 
94
- export const hyperLinkStyle = EditorView.baseTheme({
105
+ export const hyperlinkStyle = EditorView.baseTheme({
95
106
  '.cm-hyperlink-label': {
96
107
  cursor: 'pointer',
97
108
  textDecoration: 'underline',
@@ -105,4 +116,4 @@ export const hyperLinkStyle = EditorView.baseTheme({
105
116
  },
106
117
  });
107
118
 
108
- export const hyperlink: Extension = [hyperLinkExtension(), hyperLinkStyle];
119
+ export const hyperlinkWidget: Extension = [hyperLinkExtension(), hyperlinkStyle];
@@ -2,5 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './hyperlink';
5
+ export * from './hyperlinkDecoration';
6
6
  export * from './hyperlinkTooltip';
7
+ export * from './hyperlinkWidget';
@@ -10,6 +10,7 @@ import { tailwindConfig, type TailwindConfig } from '@dxos/react-ui-theme';
10
10
  const tokens: TailwindConfig['theme'] = tailwindConfig({}).theme;
11
11
 
12
12
  /**
13
+ * Minimal styles.
13
14
  * https://codemirror.net/examples/styling
14
15
  */
15
16
  // TODO(burdon): If given a "theme" suffix, `__docgen` properties are added to the object.
@@ -19,10 +20,7 @@ export const defaultStyles: {
19
20
  '&.cm-focused': {
20
21
  outline: 'none',
21
22
  },
22
- '.cm-placeholder': {
23
- fontFamily: get(tokens, 'fontFamily.body', []).join(','),
24
- },
25
- '& .cm-scroller': {
23
+ '.cm-scroller': {
26
24
  fontFamily: get(tokens, 'fontFamily.body', []).join(','),
27
25
  overflow: 'visible',
28
26
  },
@@ -30,4 +28,8 @@ export const defaultStyles: {
30
28
  backgroundColor: 'transparent',
31
29
  border: 'none',
32
30
  },
31
+ '.cm-link': {
32
+ color: get(tokens, 'extend.colors.primary.500'),
33
+ textDecoration: 'underline',
34
+ },
33
35
  };
@@ -1 +0,0 @@
1
- {"version":3,"file":"hyperlink.d.ts","sourceRoot":"","sources":["../../../../../../src/components/TextEditor/extensions/hyperlink.tsx"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAEL,KAAK,aAAa,EAElB,cAAc,EACd,UAAU,EACV,KAAK,UAAU,EAChB,MAAM,kBAAkB,CAAC;AA4C1B,wBAAgB,kBAAkB;wBAGR,cAAc;;mBAOnB,UAAU;GAwB9B;AAED,eAAO,MAAM,cAAc,WAYzB,CAAC;AAEH,eAAO,MAAM,SAAS,EAAE,SAAkD,CAAC"}