@squiz/formatted-text-editor 1.21.1-alpha.19 → 1.21.1-alpha.20

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 (86) hide show
  1. package/demo/App.tsx +31 -10
  2. package/demo/index.scss +2 -7
  3. package/lib/Editor/Editor.js +26 -2
  4. package/lib/Editor/EditorContext.d.ts +10 -0
  5. package/lib/Editor/EditorContext.js +15 -0
  6. package/lib/EditorToolbar/FloatingToolbar.js +15 -16
  7. package/lib/EditorToolbar/Tools/Link/Form/LinkForm.d.ts +14 -5
  8. package/lib/EditorToolbar/Tools/Link/Form/LinkForm.js +66 -14
  9. package/lib/EditorToolbar/Tools/Link/LinkButton.js +11 -15
  10. package/lib/EditorToolbar/Tools/Link/LinkModal.js +12 -5
  11. package/lib/EditorToolbar/Tools/Link/RemoveLinkButton.js +1 -8
  12. package/lib/Extensions/CommandsExtension/CommandsExtension.d.ts +20 -0
  13. package/lib/Extensions/CommandsExtension/CommandsExtension.js +52 -0
  14. package/lib/Extensions/Extensions.d.ts +6 -1
  15. package/lib/Extensions/Extensions.js +31 -20
  16. package/lib/Extensions/LinkExtension/AssetLinkExtension.d.ts +26 -0
  17. package/lib/Extensions/LinkExtension/AssetLinkExtension.js +102 -0
  18. package/lib/Extensions/LinkExtension/LinkExtension.d.ts +21 -12
  19. package/lib/Extensions/LinkExtension/LinkExtension.js +63 -65
  20. package/lib/Extensions/LinkExtension/common.d.ts +7 -0
  21. package/lib/Extensions/LinkExtension/common.js +14 -0
  22. package/lib/hooks/index.d.ts +1 -0
  23. package/lib/hooks/index.js +1 -0
  24. package/lib/hooks/useExpandedSelection.d.ts +23 -0
  25. package/lib/hooks/useExpandedSelection.js +37 -0
  26. package/lib/index.css +15 -3
  27. package/lib/index.d.ts +3 -2
  28. package/lib/index.js +5 -3
  29. package/lib/types.d.ts +3 -0
  30. package/lib/types.js +2 -0
  31. package/lib/ui/Button/Button.js +2 -3
  32. package/lib/ui/Fields/Input/Input.d.ts +1 -0
  33. package/lib/ui/Fields/Input/Input.js +8 -3
  34. package/lib/ui/Modal/Modal.js +2 -1
  35. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.d.ts +1 -2
  36. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.js +110 -105
  37. package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.js +93 -69
  38. package/lib/utils/undefinedIfEmpty.d.ts +1 -0
  39. package/lib/utils/undefinedIfEmpty.js +7 -0
  40. package/package.json +3 -2
  41. package/src/Editor/Editor.spec.tsx +0 -26
  42. package/src/Editor/Editor.tsx +4 -3
  43. package/src/Editor/EditorContext.spec.tsx +26 -0
  44. package/src/Editor/EditorContext.ts +19 -0
  45. package/src/EditorToolbar/FloatingToolbar.tsx +19 -18
  46. package/src/EditorToolbar/Tools/Link/Form/LinkForm.spec.tsx +37 -9
  47. package/src/EditorToolbar/Tools/Link/Form/LinkForm.tsx +96 -26
  48. package/src/EditorToolbar/Tools/Link/LinkButton.spec.tsx +103 -25
  49. package/src/EditorToolbar/Tools/Link/LinkButton.tsx +16 -19
  50. package/src/EditorToolbar/Tools/Link/LinkModal.tsx +13 -6
  51. package/src/EditorToolbar/Tools/Link/RemoveLinkButton.spec.tsx +26 -26
  52. package/src/EditorToolbar/Tools/Link/RemoveLinkButton.tsx +2 -10
  53. package/src/EditorToolbar/Tools/Undo/UndoButton.spec.tsx +22 -1
  54. package/src/Extensions/CommandsExtension/CommandsExtension.ts +54 -0
  55. package/src/Extensions/Extensions.ts +31 -19
  56. package/src/Extensions/LinkExtension/AssetLinkExtension.spec.ts +104 -0
  57. package/src/Extensions/LinkExtension/AssetLinkExtension.ts +128 -0
  58. package/src/Extensions/LinkExtension/LinkExtension.spec.ts +68 -0
  59. package/src/Extensions/LinkExtension/LinkExtension.ts +88 -82
  60. package/src/Extensions/LinkExtension/common.ts +10 -0
  61. package/src/hooks/index.ts +1 -0
  62. package/src/hooks/useExpandedSelection.ts +44 -0
  63. package/src/index.ts +3 -2
  64. package/src/types.ts +5 -0
  65. package/src/ui/Button/Button.tsx +2 -4
  66. package/src/ui/Fields/Input/Input.tsx +18 -4
  67. package/src/ui/Modal/Modal.tsx +2 -1
  68. package/src/ui/_forms.scss +14 -0
  69. package/src/utils/converters/mocks/squizNodeJson.mock.ts +177 -0
  70. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.spec.ts +41 -6
  71. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.ts +124 -113
  72. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.spec.ts +56 -34
  73. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.ts +107 -79
  74. package/src/utils/undefinedIfEmpty.spec.ts +12 -0
  75. package/src/utils/undefinedIfEmpty.ts +3 -0
  76. package/tailwind.config.cjs +3 -0
  77. package/tests/renderWithEditor.tsx +21 -12
  78. package/lib/FormattedTextEditor.d.ts +0 -2
  79. package/lib/FormattedTextEditor.js +0 -7
  80. package/lib/utils/converters/validNodeTypes.d.ts +0 -2
  81. package/lib/utils/converters/validNodeTypes.js +0 -21
  82. package/src/Editor/Editor.mock.tsx +0 -43
  83. package/src/FormattedTextEditor.spec.tsx +0 -10
  84. package/src/FormattedTextEditor.tsx +0 -3
  85. package/src/utils/converters/validNodeTypes.spec.ts +0 -33
  86. package/src/utils/converters/validNodeTypes.ts +0 -21
@@ -0,0 +1,54 @@
1
+ import { PlainExtension } from '@remirror/core';
2
+ import { command, CommandFunction, FromToProps, getTextSelection, MarkType } from 'remirror';
3
+ import { Attrs } from 'prosemirror-model';
4
+
5
+ export class CommandsExtension extends PlainExtension {
6
+ get name(): string {
7
+ return 'squizCommands' as const;
8
+ }
9
+
10
+ /**
11
+ * Updates the attributes of a specific mark for the current selection.
12
+ * Optionally, if text is provided it will replace the current selection.
13
+ * The cursor will be place at the end of the selection after changes.
14
+ *
15
+ * @param {object} options
16
+ */
17
+ @command()
18
+ updateMark(options: {
19
+ mark: MarkType;
20
+ attrs?: Attrs;
21
+ text?: string;
22
+ removeMark?: boolean;
23
+ range: FromToProps;
24
+ }): CommandFunction {
25
+ return (props) => {
26
+ const { tr, dispatch, view } = props;
27
+ const { mark, attrs, text, removeMark, range } = options;
28
+
29
+ const selectedText = tr.doc.textBetween(range.from, range.to);
30
+
31
+ if (text !== undefined && text !== selectedText) {
32
+ // update the text in the editor if it was updated, update the range to cover the length of the new text.
33
+ tr.insertText(text, range.from, range.to);
34
+ range.to = range.from + text.length;
35
+ }
36
+
37
+ // apply the link, or remove it if no URL was provided.
38
+ if (removeMark) {
39
+ tr.removeMark(range.from, range.to, mark);
40
+ } else {
41
+ tr.addMark(range.from, range.to, mark.create(attrs));
42
+ }
43
+
44
+ // move the cursor to the end of the link and re-focus the editor.
45
+ tr.setSelection(getTextSelection({ from: range.to, to: range.to }, tr.doc));
46
+
47
+ // apply the transaction.
48
+ dispatch?.(tr);
49
+ view?.focus();
50
+
51
+ return true;
52
+ };
53
+ }
54
+ }
@@ -7,26 +7,38 @@ import {
7
7
  UnderlineExtension,
8
8
  HistoryExtension,
9
9
  } from 'remirror/extensions';
10
+ import { Extension } from '@remirror/core';
10
11
  import { PreformattedExtension } from './PreformattedExtension/PreformattedExtension';
12
+ import { AssetLinkExtension } from './LinkExtension/AssetLinkExtension';
11
13
  import { LinkExtension } from './LinkExtension/LinkExtension';
12
14
  import { ImageExtension } from './ImageExtension/ImageExtension';
13
- import { Extension } from '@remirror/core';
15
+ import { CommandsExtension } from './CommandsExtension/CommandsExtension';
16
+ import { EditorContextOptions } from '../Editor/EditorContext';
17
+
18
+ export enum MarkName {
19
+ Link = 'link',
20
+ AssetLink = 'assetLink',
21
+ }
14
22
 
15
- export const Extensions = (): Extension[] => [
16
- new BoldExtension(),
17
- new HeadingExtension(),
18
- new ItalicExtension(),
19
- new NodeFormattingExtension({ indents: [] }),
20
- new ParagraphExtension(),
21
- new PreformattedExtension(),
22
- new UnderlineExtension(),
23
- new HistoryExtension(),
24
- new ImageExtension({ preferPastedTextContent: false }),
25
- new LinkExtension({
26
- supportedTargets: [
27
- // '_self' is the browser default and will be used when encountering a link with a
28
- // different target is encountered.
29
- '_blank',
30
- ],
31
- }),
32
- ];
23
+ export const createExtensions = (context: EditorContextOptions) => {
24
+ return (): Extension[] => {
25
+ return [
26
+ new CommandsExtension(),
27
+ new BoldExtension(),
28
+ new HeadingExtension(),
29
+ new ItalicExtension(),
30
+ new NodeFormattingExtension({ indents: [] }),
31
+ new ParagraphExtension(),
32
+ new PreformattedExtension(),
33
+ new UnderlineExtension(),
34
+ new HistoryExtension(),
35
+ new ImageExtension(),
36
+ new ImageExtension({ preferPastedTextContent: false }),
37
+ new LinkExtension(),
38
+ new AssetLinkExtension({
39
+ matrixIdentifier: context.matrix.matrixIdentifier,
40
+ matrixDomain: context.matrix.matrixDomain,
41
+ }),
42
+ ];
43
+ };
44
+ };
@@ -0,0 +1,104 @@
1
+ import { renderWithEditor } from '../../../tests';
2
+
3
+ describe('AssetLinkExtension', () => {
4
+ it('Parses HTML content representing an asset link', async () => {
5
+ const { getJsonContent } = await renderWithEditor(null, {
6
+ content: `<a href="https://my-matrix.squiz.net/?a=this-is-actually-ignored"
7
+ target="_self"
8
+ data-matrix-asset-id="123"
9
+ data-matrix-identifier="matrix-api-identifier"
10
+ data-matrix-domain="https://matrix-domain.squiz.net">
11
+ Hello!
12
+ </a>`,
13
+ });
14
+
15
+ expect(getJsonContent()).toEqual({
16
+ type: 'paragraph',
17
+ attrs: expect.any(Object),
18
+ content: [
19
+ {
20
+ type: 'text',
21
+ text: 'Hello!',
22
+ marks: [
23
+ {
24
+ type: 'assetLink',
25
+ attrs: {
26
+ matrixAssetId: '123',
27
+ matrixDomain: 'https://matrix-domain.squiz.net',
28
+ matrixIdentifier: 'matrix-api-identifier',
29
+ target: '_self',
30
+ },
31
+ },
32
+ ],
33
+ },
34
+ ],
35
+ });
36
+ });
37
+
38
+ it('Resolves to a regular link if HTML content is missing some of the expected attributes', async () => {
39
+ const { getJsonContent } = await renderWithEditor(null, {
40
+ content: `<a href="https://my-matrix.squiz.net/?a=123"
41
+ target="_self"
42
+ data-matrix-asset-id="123">
43
+ Hello!
44
+ </a>`,
45
+ });
46
+
47
+ expect(getJsonContent()).toEqual({
48
+ type: 'paragraph',
49
+ attrs: expect.any(Object),
50
+ content: [
51
+ {
52
+ type: 'text',
53
+ text: 'Hello!',
54
+ marks: [
55
+ {
56
+ type: 'link',
57
+ attrs: {
58
+ href: 'https://my-matrix.squiz.net/?a=123',
59
+ target: '_self',
60
+ title: null,
61
+ },
62
+ },
63
+ ],
64
+ },
65
+ ],
66
+ });
67
+ });
68
+
69
+ it('Outputs expected HTML', async () => {
70
+ const { getHtmlContent } = await renderWithEditor(null, {
71
+ content: {
72
+ type: 'paragraph',
73
+ content: [
74
+ {
75
+ type: 'text',
76
+ text: 'Hello!',
77
+ marks: [
78
+ {
79
+ type: 'assetLink',
80
+ attrs: {
81
+ matrixAssetId: '123',
82
+ matrixDomain: 'https://matrix-domain.squiz.net',
83
+ matrixIdentifier: 'matrix-api-identifier',
84
+ target: '_blank',
85
+ },
86
+ },
87
+ ],
88
+ },
89
+ ],
90
+ },
91
+ });
92
+
93
+ expect(getHtmlContent()).toEqual(
94
+ '<a rel="noopener noreferrer nofollow" ' +
95
+ 'href="/?a=123" ' +
96
+ 'target="_blank" ' +
97
+ 'data-matrix-asset-id="123" ' +
98
+ 'data-matrix-identifier="matrix-api-identifier" ' +
99
+ 'data-matrix-domain="https://matrix-domain.squiz.net">' +
100
+ 'Hello!' +
101
+ '</a>',
102
+ );
103
+ });
104
+ });
@@ -0,0 +1,128 @@
1
+ import {
2
+ ApplySchemaAttributes,
3
+ ExtensionPriority,
4
+ FromToProps,
5
+ isElementDomNode,
6
+ MarkExtensionSpec,
7
+ MarkSpecOverride,
8
+ } from 'remirror';
9
+ import { command, CommandFunction, extension, MarkExtension, omitExtraAttributes, removeMark } from '@remirror/core';
10
+ import { LinkTarget, validateTarget } from './common';
11
+ import { CommandsExtension } from '../CommandsExtension/CommandsExtension';
12
+ import { MarkName } from '../Extensions';
13
+
14
+ export type AssetLinkAttributes = {
15
+ matrixAssetId: string;
16
+ matrixIdentifier: string;
17
+ matrixDomain: string;
18
+ target: LinkTarget;
19
+ };
20
+
21
+ export type AssetLinkOptions = {
22
+ matrixIdentifier?: string;
23
+ matrixDomain?: string;
24
+ defaultTarget?: LinkTarget;
25
+ supportedTargets?: LinkTarget[];
26
+ };
27
+
28
+ export type UpdateAssetLinkProps = {
29
+ text: string;
30
+ attrs: Partial<AssetLinkAttributes>;
31
+ range: FromToProps;
32
+ };
33
+
34
+ @extension<AssetLinkOptions>({
35
+ defaultOptions: {
36
+ matrixIdentifier: '',
37
+ matrixDomain: '',
38
+ defaultTarget: LinkTarget.Self,
39
+ supportedTargets: [LinkTarget.Self, LinkTarget.Blank],
40
+ },
41
+ defaultPriority: ExtensionPriority.High,
42
+ })
43
+ export class AssetLinkExtension extends MarkExtension<AssetLinkOptions> {
44
+ get name(): string {
45
+ return MarkName.AssetLink;
46
+ }
47
+
48
+ createMarkSpec(extra: ApplySchemaAttributes, override: MarkSpecOverride): MarkExtensionSpec {
49
+ return {
50
+ inclusive: false,
51
+ excludes: 'link',
52
+ ...override,
53
+ attrs: {
54
+ ...extra.defaults(),
55
+ matrixAssetId: {},
56
+ matrixIdentifier: { default: this.options.matrixIdentifier },
57
+ matrixDomain: { default: this.options.matrixDomain },
58
+ target: { default: this.options.defaultTarget },
59
+ },
60
+ parseDOM: [
61
+ {
62
+ tag: 'a[data-matrix-asset-id]',
63
+ getAttrs: (node) => {
64
+ if (!isElementDomNode(node)) {
65
+ return false;
66
+ }
67
+
68
+ const matrixAssetId = node.getAttribute('data-matrix-asset-id');
69
+ const matrixIdentifier = node.getAttribute('data-matrix-identifier');
70
+ const matrixDomain = node.getAttribute('data-matrix-domain');
71
+
72
+ if (!matrixAssetId || !matrixIdentifier || !matrixDomain) {
73
+ return false;
74
+ }
75
+
76
+ return {
77
+ ...extra.parse(node),
78
+ matrixAssetId,
79
+ matrixIdentifier,
80
+ matrixDomain,
81
+ target: validateTarget(
82
+ node.getAttribute('target'),
83
+ this.options.supportedTargets,
84
+ this.options.defaultTarget,
85
+ ),
86
+ };
87
+ },
88
+ },
89
+ ],
90
+ toDOM: (node) => {
91
+ const { matrixAssetId, matrixIdentifier, matrixDomain, target, ...rest } = omitExtraAttributes(
92
+ node.attrs,
93
+ extra,
94
+ );
95
+ const rel = 'noopener noreferrer nofollow';
96
+ const attrs = {
97
+ ...extra.dom(node),
98
+ ...rest,
99
+ rel,
100
+ // TODO: this won't be acceptable if/when we get to rendering outside of Matrix.
101
+ href: `/?a=${matrixAssetId}`,
102
+ target: validateTarget(target, this.options.supportedTargets, this.options.defaultTarget),
103
+ 'data-matrix-asset-id': matrixAssetId,
104
+ 'data-matrix-identifier': matrixIdentifier,
105
+ 'data-matrix-domain': matrixDomain,
106
+ };
107
+
108
+ return ['a', attrs, 0];
109
+ },
110
+ };
111
+ }
112
+
113
+ @command()
114
+ updateAssetLink({ text, attrs, range }: UpdateAssetLinkProps): CommandFunction {
115
+ return this.store.getExtension(CommandsExtension).updateMark({
116
+ attrs,
117
+ text,
118
+ range,
119
+ mark: this.type,
120
+ removeMark: !attrs.matrixAssetId,
121
+ });
122
+ }
123
+
124
+ @command()
125
+ removeAssetLink(): CommandFunction {
126
+ return removeMark({ type: this.type });
127
+ }
128
+ }
@@ -0,0 +1,68 @@
1
+ import { renderWithEditor } from '../../../tests';
2
+
3
+ describe('AssetLinkExtension', () => {
4
+ it('Parses HTML content representing an asset link', async () => {
5
+ const { getJsonContent } = await renderWithEditor(null, {
6
+ content: `<a href="https://example.org/some-page"
7
+ title="Link title"
8
+ target="_blank"
9
+ rel="this is ignored">
10
+ Hello!
11
+ </a>`,
12
+ });
13
+
14
+ expect(getJsonContent()).toEqual({
15
+ type: 'paragraph',
16
+ attrs: expect.any(Object),
17
+ content: [
18
+ {
19
+ type: 'text',
20
+ text: 'Hello!',
21
+ marks: [
22
+ {
23
+ type: 'link',
24
+ attrs: {
25
+ href: 'https://example.org/some-page',
26
+ title: 'Link title',
27
+ target: '_blank',
28
+ },
29
+ },
30
+ ],
31
+ },
32
+ ],
33
+ });
34
+ });
35
+
36
+ it('Outputs expected HTML', async () => {
37
+ const { getHtmlContent } = await renderWithEditor(null, {
38
+ content: {
39
+ type: 'paragraph',
40
+ content: [
41
+ {
42
+ type: 'text',
43
+ text: 'Hello!',
44
+ marks: [
45
+ {
46
+ type: 'link',
47
+ attrs: {
48
+ href: 'https://example.org/some-page',
49
+ title: 'Link title',
50
+ target: '_blank',
51
+ },
52
+ },
53
+ ],
54
+ },
55
+ ],
56
+ },
57
+ });
58
+
59
+ expect(getHtmlContent()).toEqual(
60
+ '<a href="https://example.org/some-page" ' +
61
+ 'title="Link title" ' +
62
+ 'rel="noopener noreferrer nofollow" ' +
63
+ 'target="_blank">' +
64
+ 'Hello!' +
65
+ '</a>',
66
+ );
67
+ });
68
+ });
@@ -1,26 +1,68 @@
1
- import { ApplySchemaAttributes, getMarkRanges, isElementDomNode, MarkExtensionSpec, MarkSpecOverride } from 'remirror';
2
- import { getTextSelection, getMarkRange, KeyBindingProps } from '@remirror/core';
3
- import { CommandFunction } from '@remirror/pm';
4
- import { LinkAttributes as RemirrorLinkAttributes, LinkExtension as RemirrorLinkExtension } from 'remirror/extensions';
1
+ import {
2
+ ApplySchemaAttributes,
3
+ ExtensionPriority,
4
+ FromToProps,
5
+ isElementDomNode,
6
+ MarkExtensionSpec,
7
+ MarkSpecOverride,
8
+ } from 'remirror';
9
+ import {
10
+ command,
11
+ CommandFunction,
12
+ extension,
13
+ Handler,
14
+ keyBinding,
15
+ KeyBindingProps,
16
+ MarkExtension,
17
+ NamedShortcut,
18
+ omitExtraAttributes,
19
+ removeMark,
20
+ } from '@remirror/core';
21
+ import { LinkTarget, validateTarget } from './common';
22
+ import { CommandsExtension } from '../CommandsExtension/CommandsExtension';
23
+ import { MarkName } from '../Extensions';
24
+
25
+ export type LinkAttributes = {
26
+ href: string;
27
+ title?: string;
28
+ target: LinkTarget;
29
+ };
5
30
 
6
- export type UpdateLinkOptions = LinkAttributes & {
7
- text: string;
31
+ export type LinkOptions = {
32
+ defaultTarget?: LinkTarget;
33
+ supportedTargets?: LinkTarget[];
34
+ onShortcut?: Handler<() => void>;
8
35
  };
9
36
 
10
- export type LinkAttributes = RemirrorLinkAttributes & {
11
- title?: string;
37
+ export type UpdateLinkProps = {
38
+ text: string;
39
+ attrs: Partial<LinkAttributes>;
40
+ range: FromToProps;
12
41
  };
13
42
 
14
- export class LinkExtension extends RemirrorLinkExtension {
15
- createMarkSpec(extra: ApplySchemaAttributes, override: MarkSpecOverride): MarkExtensionSpec {
16
- const spec = super.createMarkSpec(extra, override);
43
+ @extension<LinkOptions>({
44
+ defaultOptions: {
45
+ defaultTarget: LinkTarget.Self,
46
+ supportedTargets: [LinkTarget.Self, LinkTarget.Blank],
47
+ },
48
+ handlerKeys: ['onShortcut'],
49
+ defaultPriority: ExtensionPriority.Medium,
50
+ })
51
+ export class LinkExtension extends MarkExtension<LinkOptions> {
52
+ get name(): string {
53
+ return MarkName.Link;
54
+ }
17
55
 
56
+ createMarkSpec(extra: ApplySchemaAttributes, override: MarkSpecOverride): MarkExtensionSpec {
18
57
  return {
19
- ...spec,
20
- excludes: undefined,
58
+ inclusive: false,
59
+ excludes: 'assetLink',
60
+ ...override,
21
61
  attrs: {
22
- ...spec.attrs,
62
+ ...extra.defaults(),
63
+ href: {},
23
64
  title: { default: undefined },
65
+ target: { default: this.options.defaultTarget },
24
66
  },
25
67
  parseDOM: [
26
68
  {
@@ -32,88 +74,52 @@ export class LinkExtension extends RemirrorLinkExtension {
32
74
 
33
75
  return {
34
76
  ...extra.parse(node),
35
- auto: false,
36
77
  href: node.getAttribute('href'),
37
- target: node.getAttribute('target'),
38
78
  title: node.getAttribute('title'),
79
+ target: validateTarget(
80
+ node.getAttribute('target'),
81
+ this.options.supportedTargets,
82
+ this.options.defaultTarget,
83
+ ),
39
84
  };
40
85
  },
41
86
  },
42
87
  ],
88
+ toDOM: (node) => {
89
+ const { target, ...rest } = omitExtraAttributes(node.attrs, extra);
90
+ const rel = 'noopener noreferrer nofollow';
91
+ const attrs = {
92
+ ...extra.dom(node),
93
+ ...rest,
94
+ rel,
95
+ target: validateTarget(target, this.options.supportedTargets, this.options.defaultTarget),
96
+ };
97
+
98
+ return ['a', attrs, 0];
99
+ },
43
100
  };
44
101
  }
45
102
 
46
- shortcut({ tr }: KeyBindingProps): boolean {
47
- // override parent implementation to allow a link to be inserted without requiring a text selection first.
48
- const { from, to, $from } = tr.selection;
49
- const mark = getMarkRange($from, this.type);
50
- const selectedText = tr.doc.textBetween(from, to);
51
-
52
- this.options.onShortcut({
53
- activeLink: mark ? { attrs: mark.mark.attrs as LinkAttributes, from: mark.from, to: mark.to } : undefined,
54
- selectedText,
55
- from,
56
- to,
57
- });
103
+ @keyBinding({ shortcut: NamedShortcut.InsertLink })
104
+ shortcut(_: KeyBindingProps): boolean {
105
+ this.options.onShortcut();
58
106
 
59
107
  return true;
60
108
  }
61
109
 
62
- selectLink(): CommandFunction {
63
- // parent implementation selects only the link text, this mimics closer to Google Docs where
64
- // the select text is widened to include the link but retains non-link selected text as well.
65
- return (props) => {
66
- const { tr } = props;
67
- const ranges = getMarkRanges(tr.selection, this.type);
68
-
69
- if (ranges.length === 0) {
70
- return false;
71
- }
72
-
73
- // work out the start position of the first link and end position of the last link in the selection.
74
- const from = Math.min(tr.selection.from, ...ranges.map((range) => range.from));
75
- const to = Math.max(tr.selection.to, ...ranges.map((range) => range.to));
76
-
77
- // don't need to widen the selection, return early.
78
- if (tr.selection.from === from && tr.selection.to === to) {
79
- return false;
80
- }
81
-
82
- // widen the selection to make sure the full link is included.
83
- this.store.commands.selectText.original({ from, to })(props);
84
- return true;
85
- };
110
+ @command()
111
+ updateLink({ text, attrs, range }: UpdateLinkProps): CommandFunction {
112
+ return this.store.getExtension(CommandsExtension).updateMark({
113
+ attrs,
114
+ text,
115
+ range,
116
+ mark: this.type,
117
+ removeMark: !attrs.href,
118
+ });
86
119
  }
87
120
 
88
- updateLink(options: UpdateLinkOptions): CommandFunction {
89
- const { text, ...attrs } = options;
90
-
91
- return (props) => {
92
- const { tr, dispatch, view } = props;
93
- const range = { from: tr.selection.from, to: tr.selection.to };
94
- const selectedText = tr.doc.textBetween(range.from, range.to);
95
-
96
- if (text !== selectedText) {
97
- // update the text in the editor if it was updated, update the range to cover the length of the new text.
98
- tr.insertText(text);
99
- range.to = range.from + text.length;
100
- }
101
-
102
- // apply the link, or remove it if no URL was provided.
103
- if (attrs.href.length > 0) {
104
- tr.addMark(range.from, range.to, this.type.create(attrs));
105
- } else {
106
- tr.removeMark(range.from, range.to, this.type);
107
- }
108
-
109
- // move the cursor to the end of the link and re-focus the editor.
110
- tr.setSelection(getTextSelection({ from: range.to, to: range.to }, tr.doc));
111
-
112
- // apply the transaction.
113
- dispatch?.(tr);
114
- view?.focus();
115
-
116
- return true;
117
- };
121
+ @command()
122
+ removeLink(): CommandFunction {
123
+ return removeMark({ type: this.type });
118
124
  }
119
125
  }
@@ -0,0 +1,10 @@
1
+ export enum LinkTarget {
2
+ Self = '_self',
3
+ Blank = '_blank',
4
+ Parent = '_parent',
5
+ Top = '_top',
6
+ }
7
+
8
+ export const validateTarget = (target: unknown, supportedTargets: LinkTarget[], defaultTarget: LinkTarget) => {
9
+ return supportedTargets.includes(target as LinkTarget) ? (target as LinkTarget) : defaultTarget;
10
+ };
@@ -1 +1,2 @@
1
1
  export * from './useExtensionNames';
2
+ export * from './useExpandedSelection';
@@ -0,0 +1,44 @@
1
+ import { Selection } from '@remirror/core';
2
+ import { GetMarkRange, getMarkRanges, getTextSelection, Mark, MarkType } from 'remirror';
3
+ import { useEditorState } from '@remirror/react';
4
+
5
+ export type ExpandedSelection = {
6
+ selection: Selection;
7
+ marks: Mark[];
8
+ };
9
+
10
+ /**
11
+ * Returns a single range that is either:
12
+ * 1. The current selection if the current selection does not contain any of the indicated marks.
13
+ * 2. An expanded version of the current selection if the range contains any of the indicated marks.
14
+ *
15
+ * For example, given the content:
16
+ *
17
+ * <strong>Bold</strong> regular <u>underline</u> <u>more underline</u>
18
+ *
19
+ * If the marks passed in are 'bold' and 'underline' and the text "old regular under" is selected
20
+ * the returned range will be "Bold regular underline".
21
+ *
22
+ * @param {(MarkType|string)[]} markTypes
23
+ *
24
+ * @return {ExpandedSelection}
25
+ */
26
+ export const useExpandedSelection = (markTypes: (MarkType | string)[]): ExpandedSelection => {
27
+ const { doc, selection } = useEditorState();
28
+ const ranges: GetMarkRange[] = [];
29
+
30
+ markTypes.forEach((markType) => {
31
+ ranges.push(...getMarkRanges(selection, markType));
32
+ });
33
+
34
+ if (ranges.length === 0) {
35
+ return { selection, marks: [] };
36
+ }
37
+
38
+ // work out the start position of the first link and end position of the last link in the selection.
39
+ const from = Math.min(selection.from, ...ranges.map((range) => range.from));
40
+ const to = Math.max(selection.to, ...ranges.map((range) => range.to));
41
+ const marks = ranges.map((range) => range.mark);
42
+
43
+ return { selection: getTextSelection({ from, to }, doc), marks };
44
+ };
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
- import FormattedTextEditor from './FormattedTextEditor';
1
+ import Editor from './Editor/Editor';
2
+ import { EditorContext } from './Editor/EditorContext';
2
3
  import { remirrorNodeToSquizNode } from './utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode';
3
4
  import { squizNodeToRemirrorNode } from './utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode';
4
5
 
5
- export { FormattedTextEditor, remirrorNodeToSquizNode, squizNodeToRemirrorNode };
6
+ export { Editor, EditorContext, remirrorNodeToSquizNode, squizNodeToRemirrorNode };