@squiz/formatted-text-editor 1.21.1-alpha.4 → 1.21.1-alpha.41

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 (118) hide show
  1. package/demo/App.tsx +52 -10
  2. package/demo/index.scss +11 -10
  3. package/jest.config.ts +0 -2
  4. package/lib/Editor/Editor.js +45 -7
  5. package/lib/Editor/EditorContext.d.ts +15 -0
  6. package/lib/Editor/EditorContext.js +15 -0
  7. package/lib/EditorToolbar/FloatingToolbar.js +11 -5
  8. package/lib/EditorToolbar/Tools/Image/Form/ImageForm.d.ts +9 -8
  9. package/lib/EditorToolbar/Tools/Image/Form/ImageForm.js +91 -23
  10. package/lib/EditorToolbar/Tools/Image/ImageButton.d.ts +4 -1
  11. package/lib/EditorToolbar/Tools/Image/ImageButton.js +22 -14
  12. package/lib/EditorToolbar/Tools/Image/ImageModal.js +8 -5
  13. package/lib/EditorToolbar/Tools/Link/Form/LinkForm.d.ts +14 -5
  14. package/lib/EditorToolbar/Tools/Link/Form/LinkForm.js +66 -14
  15. package/lib/EditorToolbar/Tools/Link/LinkButton.js +21 -13
  16. package/lib/EditorToolbar/Tools/Link/LinkModal.js +12 -5
  17. package/lib/EditorToolbar/Tools/Link/RemoveLinkButton.js +1 -8
  18. package/lib/Extensions/CommandsExtension/CommandsExtension.d.ts +20 -0
  19. package/lib/Extensions/CommandsExtension/CommandsExtension.js +52 -0
  20. package/lib/Extensions/Extensions.d.ts +12 -5
  21. package/lib/Extensions/Extensions.js +42 -20
  22. package/lib/Extensions/ImageExtension/AssetImageExtension.d.ts +17 -0
  23. package/lib/Extensions/ImageExtension/AssetImageExtension.js +92 -0
  24. package/lib/Extensions/ImageExtension/ImageExtension.d.ts +4 -0
  25. package/lib/Extensions/ImageExtension/ImageExtension.js +11 -0
  26. package/lib/Extensions/LinkExtension/AssetLinkExtension.d.ts +26 -0
  27. package/lib/Extensions/LinkExtension/AssetLinkExtension.js +102 -0
  28. package/lib/Extensions/LinkExtension/LinkExtension.d.ts +19 -12
  29. package/lib/Extensions/LinkExtension/LinkExtension.js +56 -66
  30. package/lib/Extensions/LinkExtension/common.d.ts +7 -0
  31. package/lib/Extensions/LinkExtension/common.js +14 -0
  32. package/lib/Extensions/PreformattedExtension/PreformattedExtension.d.ts +1 -1
  33. package/lib/hooks/index.d.ts +1 -0
  34. package/lib/hooks/index.js +1 -0
  35. package/lib/hooks/useExpandedSelection.d.ts +23 -0
  36. package/lib/hooks/useExpandedSelection.js +37 -0
  37. package/lib/index.css +58 -23
  38. package/lib/index.d.ts +5 -2
  39. package/lib/index.js +9 -3
  40. package/lib/types.d.ts +3 -0
  41. package/lib/types.js +2 -0
  42. package/lib/ui/Button/Button.d.ts +2 -1
  43. package/lib/ui/Button/Button.js +4 -5
  44. package/lib/ui/Fields/Input/Input.d.ts +1 -0
  45. package/lib/ui/Fields/Input/Input.js +9 -3
  46. package/lib/ui/Modal/Modal.js +5 -3
  47. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.d.ts +9 -0
  48. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.js +174 -0
  49. package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.d.ts +9 -0
  50. package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.js +138 -0
  51. package/lib/utils/resolveMatrixAssetUrl.d.ts +1 -0
  52. package/lib/utils/resolveMatrixAssetUrl.js +10 -0
  53. package/lib/utils/undefinedIfEmpty.d.ts +1 -0
  54. package/lib/utils/undefinedIfEmpty.js +7 -0
  55. package/package.json +10 -4
  56. package/src/Editor/Editor.spec.tsx +78 -18
  57. package/src/Editor/Editor.tsx +28 -9
  58. package/src/Editor/EditorContext.spec.tsx +26 -0
  59. package/src/Editor/EditorContext.ts +26 -0
  60. package/src/Editor/_editor.scss +20 -4
  61. package/src/EditorToolbar/FloatingToolbar.spec.tsx +26 -7
  62. package/src/EditorToolbar/FloatingToolbar.tsx +15 -6
  63. package/src/EditorToolbar/Tools/Image/Form/ImageForm.spec.tsx +81 -6
  64. package/src/EditorToolbar/Tools/Image/Form/ImageForm.tsx +167 -47
  65. package/src/EditorToolbar/Tools/Image/ImageButton.spec.tsx +216 -1
  66. package/src/EditorToolbar/Tools/Image/ImageButton.tsx +29 -16
  67. package/src/EditorToolbar/Tools/Image/ImageModal.spec.tsx +47 -8
  68. package/src/EditorToolbar/Tools/Image/ImageModal.tsx +11 -10
  69. package/src/EditorToolbar/Tools/Link/Form/LinkForm.spec.tsx +37 -9
  70. package/src/EditorToolbar/Tools/Link/Form/LinkForm.tsx +96 -26
  71. package/src/EditorToolbar/Tools/Link/LinkButton.spec.tsx +116 -26
  72. package/src/EditorToolbar/Tools/Link/LinkButton.tsx +28 -19
  73. package/src/EditorToolbar/Tools/Link/LinkModal.tsx +13 -6
  74. package/src/EditorToolbar/Tools/Link/RemoveLinkButton.spec.tsx +27 -26
  75. package/src/EditorToolbar/Tools/Link/RemoveLinkButton.tsx +2 -10
  76. package/src/EditorToolbar/Tools/Undo/UndoButton.spec.tsx +22 -1
  77. package/src/EditorToolbar/_floating-toolbar.scss +4 -5
  78. package/src/EditorToolbar/_toolbar.scss +1 -1
  79. package/src/Extensions/CommandsExtension/CommandsExtension.ts +54 -0
  80. package/src/Extensions/Extensions.ts +42 -18
  81. package/src/Extensions/ImageExtension/AssetImageExtension.spec.ts +76 -0
  82. package/src/Extensions/ImageExtension/AssetImageExtension.ts +111 -0
  83. package/src/Extensions/ImageExtension/ImageExtension.ts +17 -1
  84. package/src/Extensions/LinkExtension/AssetLinkExtension.spec.ts +104 -0
  85. package/src/Extensions/LinkExtension/AssetLinkExtension.ts +128 -0
  86. package/src/Extensions/LinkExtension/LinkExtension.spec.ts +68 -0
  87. package/src/Extensions/LinkExtension/LinkExtension.ts +71 -85
  88. package/src/Extensions/LinkExtension/common.ts +10 -0
  89. package/src/hooks/index.ts +1 -0
  90. package/src/hooks/useExpandedSelection.ts +44 -0
  91. package/src/index.ts +5 -2
  92. package/src/types.ts +5 -0
  93. package/src/ui/Button/Button.tsx +10 -6
  94. package/src/ui/Button/_button.scss +1 -1
  95. package/src/ui/Fields/Input/Input.spec.tsx +7 -1
  96. package/src/ui/Fields/Input/Input.tsx +23 -4
  97. package/src/ui/Modal/Modal.spec.tsx +15 -0
  98. package/src/ui/Modal/Modal.tsx +8 -4
  99. package/src/ui/ToolbarDropdown/_toolbar-dropdown.scss +1 -1
  100. package/src/ui/_forms.scss +14 -0
  101. package/src/utils/converters/mocks/squizNodeJson.mock.ts +271 -0
  102. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.spec.ts +480 -0
  103. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.ts +212 -0
  104. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.spec.ts +341 -0
  105. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.ts +159 -0
  106. package/src/utils/resolveMatrixAssetUrl.spec.ts +26 -0
  107. package/src/utils/resolveMatrixAssetUrl.ts +7 -0
  108. package/src/utils/undefinedIfEmpty.spec.ts +12 -0
  109. package/src/utils/undefinedIfEmpty.ts +3 -0
  110. package/tailwind.config.cjs +3 -0
  111. package/tests/renderWithEditor.tsx +28 -15
  112. package/tsconfig.json +1 -1
  113. package/lib/FormattedTextEditor.d.ts +0 -2
  114. package/lib/FormattedTextEditor.js +0 -7
  115. package/src/Editor/Editor.mock.tsx +0 -43
  116. package/src/FormattedTextEditor.spec.tsx +0 -10
  117. package/src/FormattedTextEditor.tsx +0 -3
  118. /package/tests/{select.tsx → select.ts} +0 -0
@@ -37,7 +37,7 @@ describe('LinkButton', () => {
37
37
  marks: [
38
38
  {
39
39
  type: 'link',
40
- attrs: { auto: false, href: 'https://www.squiz.net/link-button', target: '_blank', title: 'Link title' },
40
+ attrs: { href: 'https://www.squiz.net/link-button', target: '_blank', title: 'Link title' },
41
41
  },
42
42
  ],
43
43
  },
@@ -75,7 +75,7 @@ describe('LinkButton', () => {
75
75
  marks: [
76
76
  {
77
77
  type: 'link',
78
- attrs: { auto: false, href: 'https://www.example.org/updated-link', target: null, title: null },
78
+ attrs: { href: 'https://www.example.org/updated-link', target: '_self', title: null },
79
79
  },
80
80
  ],
81
81
  },
@@ -134,25 +134,7 @@ describe('LinkButton', () => {
134
134
  });
135
135
  });
136
136
 
137
- it.each([
138
- ['Link fully selected', 1, 12, 'Sample link'],
139
- ['Link partially selected', 2, 4, 'Sample link'],
140
- ['Link partially selected along with other content', 8, 15, 'Sample link wi'],
141
- ])(
142
- 'Expands selection when a link is partially selected - %s',
143
- async (description: string, from: number, to: number, expectedSelection: string) => {
144
- const { editor, getSelectedText } = await renderWithEditor(<LinkButton />, {
145
- content: '<a href="https://www.example.org/my-link">Sample link</a> <strong>with</strong> some other content.',
146
- });
147
-
148
- await act(() => editor.selectText({ from, to }));
149
- await openModal();
150
-
151
- expect(getSelectedText()).toBe(expectedSelection);
152
- },
153
- );
154
-
155
- it('Updates full selection when it is expanded from what was initially selected', async () => {
137
+ it('Updates unselected part of link when link is partially selected', async () => {
156
138
  const { editor, getJsonContent } = await renderWithEditor(<LinkButton />, {
157
139
  content: '<a href="https://www.example.org/my-link">Sample link</a> <strong>with</strong> some other content.',
158
140
  });
@@ -179,7 +161,7 @@ describe('LinkButton', () => {
179
161
  marks: [
180
162
  {
181
163
  type: 'link',
182
- attrs: { auto: false, href: 'https://www.example.org/my-link', target: null, title: null },
164
+ attrs: { href: 'https://www.example.org/my-link', target: '_self', title: null },
183
165
  },
184
166
  ],
185
167
  },
@@ -189,7 +171,7 @@ describe('LinkButton', () => {
189
171
  marks: [
190
172
  {
191
173
  type: 'link',
192
- attrs: { auto: false, href: 'https://www.example.org/my-link', target: null, title: null },
174
+ attrs: { href: 'https://www.example.org/my-link', target: '_self', title: null },
193
175
  },
194
176
  { type: 'bold' },
195
177
  ],
@@ -200,7 +182,7 @@ describe('LinkButton', () => {
200
182
  marks: [
201
183
  {
202
184
  type: 'link',
203
- attrs: { auto: false, href: 'https://www.example.org/my-link', target: null, title: null },
185
+ attrs: { href: 'https://www.example.org/my-link', target: '_self', title: null },
204
186
  },
205
187
  ],
206
188
  },
@@ -237,7 +219,7 @@ describe('LinkButton', () => {
237
219
  marks: [
238
220
  {
239
221
  type: 'link',
240
- attrs: { auto: false, href: 'https://www.example.org/my-link', target: null, title: null },
222
+ attrs: { href: 'https://www.example.org/my-link', target: '_self', title: null },
241
223
  },
242
224
  ],
243
225
  },
@@ -252,7 +234,7 @@ describe('LinkButton', () => {
252
234
  });
253
235
 
254
236
  // jump to the middle of the link.
255
- await act(() => editor.selectText(5));
237
+ await act(() => editor.selectText({ from: 1, to: 12 }));
256
238
 
257
239
  // press the keyboard shortcut.
258
240
  fireEvent.keyDown(elements.editor, { key: 'k', ctrlKey: true });
@@ -274,4 +256,112 @@ describe('LinkButton', () => {
274
256
  fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
275
257
  expect(modalHeading).not.toBeInTheDocument();
276
258
  });
259
+
260
+ it('Add a new asset link', async () => {
261
+ const matrixIdentifier = 'matrix-api-identifier';
262
+ const matrixDomain = 'https://my-matrix.squiz.net';
263
+ const { getJsonContent } = await renderWithEditor(<LinkButton />, {
264
+ context: {
265
+ matrix: {
266
+ matrixIdentifier,
267
+ matrixDomain,
268
+ resolveMatrixAsset: () => Promise.resolve({ id: '100', type: 'physical_file' }),
269
+ },
270
+ },
271
+ });
272
+
273
+ await openModal();
274
+ select(screen.getByLabelText('Type'), 'Link to asset');
275
+ fireEvent.change(screen.getByLabelText('Asset ID'), { target: { value: '123' } });
276
+ fireEvent.change(screen.getByLabelText('Text'), { target: { value: 'Link text' } });
277
+ fireEvent.click(screen.getByRole('button', { name: 'Apply' }));
278
+
279
+ await waitForElementToBeRemoved(() => screen.getByRole('button', { name: 'Apply' }));
280
+
281
+ expect(getJsonContent()).toEqual({
282
+ type: 'paragraph',
283
+ attrs: expect.any(Object),
284
+ content: [
285
+ {
286
+ type: 'text',
287
+ text: 'Link text',
288
+ marks: [
289
+ {
290
+ type: 'assetLink',
291
+ attrs: { matrixAssetId: '123', target: '_self', matrixDomain, matrixIdentifier },
292
+ },
293
+ ],
294
+ },
295
+ ],
296
+ });
297
+ });
298
+
299
+ it('Updates an existing link to be an asset link', async () => {
300
+ const matrixIdentifier = 'matrix-api-identifier';
301
+ const matrixDomain = 'https://my-matrix.squiz.net';
302
+ const { editor, getJsonContent } = await renderWithEditor(<LinkButton />, {
303
+ content:
304
+ '<a href="https://www.example.org/my-link">Sample link</a> with ' +
305
+ '<a href="https://www.example.org/another-link">another link</a>',
306
+ context: {
307
+ matrix: {
308
+ matrixIdentifier,
309
+ matrixDomain,
310
+ resolveMatrixAsset: () => Promise.resolve({ id: '100', type: 'physical_file' }),
311
+ },
312
+ },
313
+ });
314
+
315
+ await act(() => editor.selectText(5));
316
+
317
+ await openModal();
318
+ select(screen.getByLabelText('Type'), 'Link to asset');
319
+ fireEvent.change(screen.getByLabelText('Asset ID'), { target: { value: '123' } });
320
+ select(screen.getByLabelText('Target'), 'New window');
321
+ fireEvent.click(screen.getByRole('button', { name: 'Apply' }));
322
+
323
+ await waitForElementToBeRemoved(() => screen.getByRole('button', { name: 'Apply' }));
324
+
325
+ expect(getJsonContent()).toEqual({
326
+ type: 'paragraph',
327
+ attrs: expect.any(Object),
328
+ content: [
329
+ {
330
+ type: 'text',
331
+ text: 'Sample link',
332
+ marks: [
333
+ {
334
+ type: 'assetLink',
335
+ attrs: { matrixAssetId: '123', target: '_blank', matrixDomain, matrixIdentifier },
336
+ },
337
+ ],
338
+ },
339
+ { type: 'text', text: ' with ' },
340
+ {
341
+ type: 'text',
342
+ text: 'another link',
343
+ marks: [
344
+ {
345
+ type: 'link',
346
+ attrs: { href: 'https://www.example.org/another-link', target: '_self', title: null },
347
+ },
348
+ ],
349
+ },
350
+ ],
351
+ });
352
+ });
353
+
354
+ it('Shows an error if an invalid asset ID is provided', async () => {
355
+ const resolveMatrixAsset = jest.fn(() => Promise.resolve(null));
356
+
357
+ await renderWithEditor(<LinkButton />, { context: { matrix: { resolveMatrixAsset } } });
358
+
359
+ await openModal();
360
+ select(screen.getByLabelText('Type'), 'Link to asset');
361
+ fireEvent.change(screen.getByLabelText('Asset ID'), { target: { value: 'invalid-asset-id' } });
362
+ await act(() => fireEvent.click(screen.getByRole('button', { name: 'Apply' })));
363
+
364
+ expect(screen.getByText('Invalid asset ID')).toBeInTheDocument();
365
+ expect(resolveMatrixAsset).toHaveBeenCalledWith('invalid-asset-id');
366
+ });
277
367
  });
@@ -3,8 +3,12 @@ import InsertLinkRoundedIcon from '@mui/icons-material/InsertLinkRounded';
3
3
  import LinkModal from './LinkModal';
4
4
  import { LinkFormData } from './Form/LinkForm';
5
5
  import Button from '../../../ui/Button/Button';
6
- import { useActive, useCommands, useExtensionEvent } from '@remirror/react';
6
+ import { useActive, useCommands, useKeymap } from '@remirror/react';
7
7
  import { LinkExtension } from '../../../Extensions/LinkExtension/LinkExtension';
8
+ import { CommandsExtension } from '../../../Extensions/CommandsExtension/CommandsExtension';
9
+ import { AssetLinkExtension } from '../../../Extensions/LinkExtension/AssetLinkExtension';
10
+ import { MarkName } from '../../../Extensions/Extensions';
11
+ import { ImageExtension } from '../../../Extensions/ImageExtension/ImageExtension';
8
12
 
9
13
  type LinkButtonProps = {
10
14
  inPopover?: boolean;
@@ -12,41 +16,46 @@ type LinkButtonProps = {
12
16
 
13
17
  const LinkButton = ({ inPopover = false }: LinkButtonProps) => {
14
18
  const [showModal, setShowModal] = useState(false);
15
- const { selectLink, updateLink } = useCommands<LinkExtension>();
16
- const active = useActive<LinkExtension>();
19
+ const { updateLink, updateAssetLink } = useCommands<AssetLinkExtension | LinkExtension | CommandsExtension>();
20
+ const active = useActive<LinkExtension | AssetLinkExtension | ImageExtension>();
21
+ // If the image tool is active, disable the link tool as they shouldn't work at the same time
22
+ const disabled = active.image();
17
23
  const handleClick = () => {
18
24
  if (!showModal) {
19
- selectLink();
20
-
21
- // form element are uncontrolled, let the event loop run to
22
- // update the selected text in state before showing the modal.
23
- requestAnimationFrame(() => {
24
- setShowModal(true);
25
- });
25
+ setShowModal(true);
26
26
  }
27
27
  };
28
+ const handleShortcut = useCallback(() => {
29
+ handleClick();
30
+ // Prevent other key handlers being run
31
+ return true;
32
+ }, []);
33
+
28
34
  const handleSubmit = (data: LinkFormData) => {
29
- updateLink(data);
35
+ if (data.linkType === MarkName.AssetLink) {
36
+ updateAssetLink({ text: data.text, attrs: data.assetLink, range: data.range });
37
+ } else {
38
+ updateLink({ text: data.text, attrs: data.link, range: data.range });
39
+ }
40
+
30
41
  setShowModal(false);
31
42
  };
32
43
 
44
+ // when Ctrl+K is pressed show the modal, only registered in the toolbar button instance to avoid the key press
45
+ // being double handled.
33
46
  if (!inPopover) {
34
- // when Ctrl+K is pressed show the modal, only registered in the toolbar button instance to avoid the key press
35
- // being double handled.
36
- useExtensionEvent(
37
- LinkExtension,
38
- 'onShortcut',
39
- useCallback(() => handleClick(), []),
40
- );
47
+ // disable the shortcut if the button is disabled
48
+ useKeymap('Mod-k', disabled ? () => true : handleShortcut);
41
49
  }
42
50
 
43
51
  return (
44
52
  <>
45
53
  <Button
46
54
  handleOnClick={handleClick}
47
- isActive={active.link()}
55
+ isActive={active.link() || active.assetLink()}
48
56
  icon={<InsertLinkRoundedIcon />}
49
57
  label="Link (cmd+K)"
58
+ isDisabled={disabled}
50
59
  />
51
60
  {showModal && <LinkModal onCancel={() => setShowModal(false)} onSubmit={handleSubmit} />}
52
61
  </>
@@ -1,9 +1,10 @@
1
- import { getMarkRanges } from 'remirror';
2
- import LinkForm, { LinkFormData } from './Form/LinkForm';
1
+ import { LinkForm, LinkFormData } from './Form/LinkForm';
3
2
  import React from 'react';
4
- import { useRemirrorContext, useCurrentSelection } from '@remirror/react';
3
+ import { useRemirrorContext } from '@remirror/react';
5
4
  import FormModal from '../../../ui/Modal/FormModal';
6
5
  import { SubmitHandler } from 'react-hook-form';
6
+ import { useExpandedSelection } from '../../../hooks';
7
+ import { MarkName } from '../../../Extensions/Extensions';
7
8
 
8
9
  type LinkModalProps = {
9
10
  onCancel: () => void;
@@ -15,13 +16,19 @@ const LinkModal = ({ onCancel, onSubmit }: LinkModalProps) => {
15
16
  helpers,
16
17
  view: { state },
17
18
  } = useRemirrorContext();
18
- const selection = useCurrentSelection();
19
- const currentLink = getMarkRanges(selection, 'link')[0];
19
+ const { selection, marks } = useExpandedSelection([MarkName.Link, MarkName.AssetLink]);
20
20
  const selectedText = helpers.getTextBetween(selection.from, selection.to, state.doc);
21
+ const data = {
22
+ linkType: marks[0]?.type?.name === MarkName.AssetLink ? MarkName.AssetLink : MarkName.Link,
23
+ text: selectedText,
24
+ link: marks.find((mark) => mark.type.name === 'link')?.attrs || {},
25
+ assetLink: marks.find((mark) => mark.type.name === MarkName.AssetLink)?.attrs || {},
26
+ range: { from: selection.from, to: selection.to },
27
+ };
21
28
 
22
29
  return (
23
30
  <FormModal title="Link" onCancel={onCancel}>
24
- <LinkForm data={{ ...currentLink?.mark.attrs, text: selectedText }} onSubmit={onSubmit} />
31
+ <LinkForm data={data} onSubmit={onSubmit} />
25
32
  </FormModal>
26
33
  );
27
34
  };
@@ -7,40 +7,41 @@ import RemoveLinkButton from './RemoveLinkButton';
7
7
  describe('RemoveLinkButton', () => {
8
8
  it('Removes a link', async () => {
9
9
  const { editor, getJsonContent } = await renderWithEditor(<RemoveLinkButton />, {
10
- content:
11
- '<a href="https://www.example.org/my-link">Sample link</a> with some other content and ' +
12
- '<a href="https://www.example.org/another-link">another link</a>.',
10
+ context: { matrix: { matrixDomain: 'my-matrix.squiz.net' } },
11
+ content: {
12
+ type: 'doc',
13
+ content: [
14
+ {
15
+ type: 'paragraph',
16
+ content: [
17
+ {
18
+ type: 'text',
19
+ text: 'Sample link',
20
+ marks: [{ type: 'assetLink', attrs: { matrixAssetId: '123', target: '_blank' } }],
21
+ },
22
+ { type: 'text', text: ' with ' },
23
+ {
24
+ type: 'text',
25
+ text: 'another link',
26
+ marks: [{ type: 'link', attrs: { href: 'https://www.example.org/another-link', target: '_self' } }],
27
+ },
28
+ ],
29
+ },
30
+ ],
31
+ },
13
32
  });
14
33
 
15
- // move the cursor to inside of the link.
16
- await act(() => editor.selectText(5));
34
+ // select all of the text.
35
+ await act(() => editor.selectText('all'));
17
36
 
18
- // remove the link.
37
+ // remove the links.
19
38
  fireEvent.click(screen.getByRole('button', { name: 'Remove link' }));
20
39
 
21
- // make sure the link has been removed.
40
+ // make sure both types of link have been removed.
22
41
  expect(getJsonContent()).toEqual({
23
42
  type: 'paragraph',
24
43
  attrs: expect.any(Object),
25
- content: [
26
- { type: 'text', text: 'Sample link with some other content and ' },
27
- {
28
- type: 'text',
29
- text: 'another link',
30
- marks: [
31
- {
32
- type: 'link',
33
- attrs: {
34
- auto: false,
35
- href: 'https://www.example.org/another-link',
36
- target: null,
37
- title: null,
38
- },
39
- },
40
- ],
41
- },
42
- { type: 'text', text: '.' },
43
- ],
44
+ content: [{ type: 'text', text: 'Sample link with another link' }],
44
45
  });
45
46
  });
46
47
  });
@@ -1,23 +1,15 @@
1
1
  import React from 'react';
2
- import { useRemirrorContext, useChainedCommands } from '@remirror/react';
2
+ import { useChainedCommands } from '@remirror/react';
3
3
  import Button from '../../../ui/Button/Button';
4
4
  import LinkOffIcon from '@mui/icons-material/LinkOff';
5
5
 
6
6
  const RemoveLinkButton = () => {
7
- const { commands } = useRemirrorContext({ autoUpdate: true });
8
7
  const chain = useChainedCommands();
9
- const enabled = commands.removeLink.enabled();
10
- const handleClick = () => {
11
- if (enabled) {
12
- chain.removeLink().focus().run();
13
- }
14
- };
15
8
 
16
9
  return (
17
10
  <Button
18
- handleOnClick={handleClick}
11
+ handleOnClick={() => chain.removeLink().removeAssetLink().focus().run()}
19
12
  isActive={false}
20
- isDisabled={!enabled}
21
13
  icon={<LinkOffIcon />}
22
14
  label="Remove link"
23
15
  />
@@ -1,7 +1,9 @@
1
1
  import '@testing-library/jest-dom';
2
- import { render, screen, fireEvent } from '@testing-library/react';
2
+ import { render, screen, fireEvent, act } from '@testing-library/react';
3
3
  import Editor from '../../../Editor/Editor';
4
4
  import React from 'react';
5
+ import { renderWithEditor } from '../../../../tests';
6
+ import UndoButton from './UndoButton';
5
7
 
6
8
  describe('Undo button', () => {
7
9
  it('Renders the undo button', () => {
@@ -46,4 +48,23 @@ describe('Undo button', () => {
46
48
  fireEvent.click(undo);
47
49
  expect(baseElement.querySelector('p[data-node-text-align="left"]')).toBeFalsy();
48
50
  });
51
+
52
+ it('Reverts text content changes', async () => {
53
+ const { editor, getJsonContent } = await renderWithEditor(<UndoButton />, { content: 'Initial content...' });
54
+
55
+ await act(() => editor.jumpTo('end'));
56
+ await act(() => editor.paste(' with some updated content.'));
57
+ expect(getJsonContent()).toEqual({
58
+ type: 'paragraph',
59
+ attrs: expect.any(Object),
60
+ content: [{ type: 'text', text: 'Initial content... with some updated content.' }],
61
+ });
62
+
63
+ fireEvent.click(screen.getByRole('button', { name: 'Undo (cmd+Z)' }));
64
+ expect(getJsonContent()).toEqual({
65
+ type: 'paragraph',
66
+ attrs: expect.any(Object),
67
+ content: [{ type: 'text', text: 'Initial content...' }],
68
+ });
69
+ });
49
70
  });
@@ -1,10 +1,9 @@
1
1
  /// This class is excluded from the scope of squiz-fte-scope as it is outside of the scoped element
2
2
  .squiz-fte-scope__floating-popover {
3
+ @extend .editor-toolbar;
3
4
  @apply bg-white border-gray-200 p-1 shadow rounded-md border flex;
4
- .squiz-fte-btn {
5
- @apply p-1;
6
- ~ .squiz-fte-btn {
7
- margin-left: 2px;
8
- }
5
+
6
+ .editor-divider {
7
+ @apply my-0;
9
8
  }
10
9
  }
@@ -14,7 +14,7 @@
14
14
  height: auto;
15
15
  }
16
16
  .squiz-fte-btn {
17
- @apply p-1;
17
+ @apply p-1 font-bold;
18
18
  ~ .squiz-fte-btn {
19
19
  margin-left: 2px;
20
20
  }
@@ -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,25 +7,49 @@ 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';
15
+ import { CommandsExtension } from './CommandsExtension/CommandsExtension';
16
+ import { EditorContextOptions } from '../Editor/EditorContext';
17
+ import { AssetImageExtension } from './ImageExtension/AssetImageExtension';
13
18
 
14
- export const Extensions = () => [
15
- new BoldExtension(),
16
- new HeadingExtension(),
17
- new ItalicExtension(),
18
- new NodeFormattingExtension(),
19
- new ParagraphExtension(),
20
- new PreformattedExtension(),
21
- new UnderlineExtension(),
22
- new HistoryExtension(),
23
- new ImageExtension(),
24
- new LinkExtension({
25
- supportedTargets: [
26
- // '_self' is the browser default and will be used when encountering a link with a
27
- // different target is encountered.
28
- '_blank',
29
- ],
30
- }),
31
- ];
19
+ export enum NodeName {
20
+ Image = 'image',
21
+ AssetImage = 'assetImage',
22
+ Text = 'text',
23
+ }
24
+
25
+ export enum MarkName {
26
+ Link = 'link',
27
+ AssetLink = 'assetLink',
28
+ }
29
+
30
+ export const createExtensions = (context: EditorContextOptions) => {
31
+ return (): Extension[] => {
32
+ return [
33
+ new CommandsExtension(),
34
+ new BoldExtension(),
35
+ new HeadingExtension(),
36
+ new ItalicExtension(),
37
+ new NodeFormattingExtension({ indents: [] }),
38
+ new ParagraphExtension(),
39
+ new PreformattedExtension(),
40
+ new UnderlineExtension(),
41
+ new HistoryExtension(),
42
+ new ImageExtension(),
43
+ new ImageExtension({ preferPastedTextContent: false }),
44
+ new AssetImageExtension({
45
+ matrixIdentifier: context.matrix.matrixIdentifier,
46
+ matrixDomain: context.matrix.matrixDomain,
47
+ }),
48
+ new LinkExtension(),
49
+ new AssetLinkExtension({
50
+ matrixIdentifier: context.matrix.matrixIdentifier,
51
+ matrixDomain: context.matrix.matrixDomain,
52
+ }),
53
+ ];
54
+ };
55
+ };
@@ -0,0 +1,76 @@
1
+ import { renderWithEditor } from '../../../tests';
2
+
3
+ describe('AssetImageExtension', () => {
4
+ it('Parses HTML content representing an asset image', async () => {
5
+ const { getJsonContent } = await renderWithEditor(null, {
6
+ content: `<img
7
+ src="https://my-matrix.squiz.net/?a=this-is-actually-ignored"
8
+ data-matrix-asset-id="123"
9
+ data-matrix-identifier="matrix-api-identifier"
10
+ data-matrix-domain="https://matrix-domain.squiz.net" />`,
11
+ });
12
+
13
+ expect(getJsonContent()).toEqual({
14
+ type: 'paragraph',
15
+ attrs: expect.any(Object),
16
+ content: [
17
+ {
18
+ type: 'assetImage',
19
+ attrs: {
20
+ matrixAssetId: '123',
21
+ matrixDomain: 'https://matrix-domain.squiz.net',
22
+ matrixIdentifier: 'matrix-api-identifier',
23
+ },
24
+ },
25
+ ],
26
+ });
27
+ });
28
+
29
+ it('Resolves to a regular image if HTML content is missing some of the expected attributes', async () => {
30
+ const { getJsonContent } = await renderWithEditor(null, {
31
+ content: '<img src="https://my-matrix.squiz.net/?a=123" data-matrix-asset-id="123" />',
32
+ });
33
+
34
+ expect(getJsonContent()).toEqual({
35
+ type: 'paragraph',
36
+ attrs: expect.any(Object),
37
+ content: [
38
+ {
39
+ type: 'image',
40
+ attrs: expect.objectContaining({
41
+ src: 'https://my-matrix.squiz.net/?a=123',
42
+ }),
43
+ },
44
+ ],
45
+ });
46
+ });
47
+
48
+ it('Outputs expected HTML', async () => {
49
+ const { getHtmlContent } = await renderWithEditor(null, {
50
+ content: {
51
+ type: 'paragraph',
52
+ content: [
53
+ {
54
+ type: 'assetImage',
55
+ attrs: {
56
+ matrixAssetId: '123',
57
+ matrixDomain: 'https://matrix-domain.squiz.net',
58
+ matrixIdentifier: 'matrix-api-identifier',
59
+ },
60
+ },
61
+ ],
62
+ },
63
+ });
64
+
65
+ expect(getHtmlContent()).toEqual(
66
+ '<img ' +
67
+ 'src="https://matrix-domain.squiz.net/_nocache?a=123" ' +
68
+ 'data-matrix-asset-id="123" ' +
69
+ 'data-matrix-identifier="matrix-api-identifier" ' +
70
+ 'data-matrix-domain="https://matrix-domain.squiz.net" ' +
71
+ 'draggable="true">' +
72
+ '<img class="ProseMirror-separator" alt="">' +
73
+ '<br class="ProseMirror-trailingBreak">',
74
+ );
75
+ });
76
+ });