@squiz/formatted-text-editor 1.16.0 → 1.21.1-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/.eslintrc.json +7 -0
  2. package/README.md +10 -0
  3. package/demo/App.tsx +18 -4
  4. package/demo/index.scss +16 -10
  5. package/jest.config.ts +8 -9
  6. package/lib/Editor/Editor.js +18 -13
  7. package/lib/EditorToolbar/FloatingToolbar.js +50 -20
  8. package/lib/EditorToolbar/Toolbar.js +33 -24
  9. package/lib/EditorToolbar/Tools/Bold/BoldButton.js +14 -9
  10. package/lib/EditorToolbar/Tools/Image/Form/ImageForm.d.ts +17 -0
  11. package/lib/EditorToolbar/Tools/Image/Form/ImageForm.js +84 -0
  12. package/lib/EditorToolbar/Tools/Image/ImageButton.d.ts +2 -0
  13. package/lib/EditorToolbar/Tools/Image/ImageButton.js +67 -0
  14. package/lib/EditorToolbar/Tools/Image/ImageModal.d.ts +8 -0
  15. package/lib/EditorToolbar/Tools/Image/ImageModal.js +19 -0
  16. package/lib/EditorToolbar/Tools/Italic/ItalicButton.js +14 -9
  17. package/lib/EditorToolbar/Tools/Link/Form/LinkForm.js +20 -15
  18. package/lib/EditorToolbar/Tools/Link/LinkButton.js +42 -14
  19. package/lib/EditorToolbar/Tools/Link/LinkModal.js +16 -11
  20. package/lib/EditorToolbar/Tools/Link/RemoveLinkButton.js +13 -8
  21. package/lib/EditorToolbar/Tools/Redo/RedoButton.js +13 -8
  22. package/lib/EditorToolbar/Tools/TextAlign/CenterAlign/CenterAlignButton.js +13 -8
  23. package/lib/EditorToolbar/Tools/TextAlign/JustifyAlign/JustifyAlignButton.js +13 -8
  24. package/lib/EditorToolbar/Tools/TextAlign/LeftAlign/LeftAlignButton.js +13 -8
  25. package/lib/EditorToolbar/Tools/TextAlign/RightAlign/RightAlignButton.js +13 -8
  26. package/lib/EditorToolbar/Tools/TextAlign/TextAlignButtons.js +19 -14
  27. package/lib/EditorToolbar/Tools/TextType/Heading/HeadingButton.js +19 -14
  28. package/lib/EditorToolbar/Tools/TextType/Paragraph/ParagraphButton.js +14 -9
  29. package/lib/EditorToolbar/Tools/TextType/Preformatted/PreformattedButton.js +13 -8
  30. package/lib/EditorToolbar/Tools/TextType/TextTypeDropdown.js +24 -19
  31. package/lib/EditorToolbar/Tools/Underline/UnderlineButton.js +14 -9
  32. package/lib/EditorToolbar/Tools/Undo/UndoButton.js +13 -8
  33. package/lib/EditorToolbar/index.js +18 -2
  34. package/lib/Extensions/Extensions.d.ts +2 -4
  35. package/lib/Extensions/Extensions.js +19 -13
  36. package/lib/Extensions/ImageExtension/ImageExtension.d.ts +3 -0
  37. package/lib/Extensions/ImageExtension/ImageExtension.js +7 -0
  38. package/lib/Extensions/LinkExtension/LinkExtension.js +17 -11
  39. package/lib/Extensions/PreformattedExtension/PreformattedExtension.d.ts +1 -1
  40. package/lib/Extensions/PreformattedExtension/PreformattedExtension.js +10 -7
  41. package/lib/FormattedTextEditor.js +7 -2
  42. package/lib/hooks/index.js +17 -1
  43. package/lib/hooks/useExtensionNames.js +9 -5
  44. package/lib/index.css +133 -76
  45. package/lib/index.d.ts +3 -1
  46. package/lib/index.js +12 -2
  47. package/lib/ui/Button/Button.d.ts +11 -0
  48. package/lib/ui/Button/Button.js +14 -0
  49. package/lib/ui/Fields/Input/Input.d.ts +4 -0
  50. package/lib/ui/Fields/Input/Input.js +33 -0
  51. package/lib/ui/Fields/Select/Select.js +53 -0
  52. package/lib/ui/Modal/FormModal.js +33 -5
  53. package/lib/ui/Modal/Modal.js +50 -22
  54. package/lib/ui/ToolbarDropdown/ToolbarDropdown.js +38 -10
  55. package/lib/ui/ToolbarDropdownButton/ToolbarDropdownButton.js +11 -6
  56. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.d.ts +10 -0
  57. package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.js +160 -0
  58. package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.d.ts +9 -0
  59. package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.js +105 -0
  60. package/lib/utils/converters/validNodeTypes.d.ts +2 -0
  61. package/lib/utils/converters/validNodeTypes.js +21 -0
  62. package/lib/utils/createToolbarPositioner.js +16 -12
  63. package/lib/utils/getCursorRect.js +5 -1
  64. package/package.json +7 -3
  65. package/src/Editor/_editor.scss +2 -49
  66. package/src/EditorToolbar/FloatingToolbar.tsx +1 -1
  67. package/src/EditorToolbar/Toolbar.tsx +2 -0
  68. package/src/EditorToolbar/Tools/Bold/BoldButton.spec.tsx +1 -1
  69. package/src/EditorToolbar/Tools/Bold/BoldButton.tsx +2 -2
  70. package/src/EditorToolbar/Tools/Image/Form/ImageForm.spec.tsx +23 -0
  71. package/src/EditorToolbar/Tools/Image/Form/ImageForm.tsx +92 -0
  72. package/src/EditorToolbar/Tools/Image/ImageButton.spec.tsx +79 -0
  73. package/src/EditorToolbar/Tools/Image/ImageButton.tsx +57 -0
  74. package/src/EditorToolbar/Tools/Image/ImageModal.spec.tsx +83 -0
  75. package/src/EditorToolbar/Tools/Image/ImageModal.tsx +29 -0
  76. package/src/EditorToolbar/Tools/Italic/ItalicButton.spec.tsx +1 -1
  77. package/src/EditorToolbar/Tools/Italic/ItalicButton.tsx +2 -2
  78. package/src/EditorToolbar/Tools/Link/Form/LinkForm.tsx +5 -5
  79. package/src/EditorToolbar/Tools/Link/LinkButton.tsx +2 -2
  80. package/src/EditorToolbar/Tools/Link/RemoveLinkButton.tsx +2 -2
  81. package/src/EditorToolbar/Tools/Redo/RedoButton.tsx +2 -2
  82. package/src/EditorToolbar/Tools/TextAlign/CenterAlign/CenterAlignButton.tsx +2 -2
  83. package/src/EditorToolbar/Tools/TextAlign/JustifyAlign/JustifyAlignButton.tsx +2 -2
  84. package/src/EditorToolbar/Tools/TextAlign/LeftAlign/LeftAlignButton.tsx +2 -2
  85. package/src/EditorToolbar/Tools/TextAlign/RightAlign/RightAlignButton.tsx +2 -2
  86. package/src/EditorToolbar/Tools/Underline/Underline.spec.tsx +1 -1
  87. package/src/EditorToolbar/Tools/Underline/UnderlineButton.tsx +2 -2
  88. package/src/EditorToolbar/Tools/Undo/UndoButton.tsx +2 -2
  89. package/src/EditorToolbar/_floating-toolbar.scss +6 -0
  90. package/src/EditorToolbar/_toolbar.scss +8 -2
  91. package/src/Extensions/Extensions.ts +5 -2
  92. package/src/Extensions/ImageExtension/ImageExtension.ts +3 -0
  93. package/src/Extensions/LinkExtension/LinkExtension.ts +8 -5
  94. package/src/index.scss +2 -2
  95. package/src/index.ts +3 -1
  96. package/src/ui/Button/Button.spec.tsx +44 -0
  97. package/src/ui/Button/Button.tsx +31 -0
  98. package/src/ui/{_buttons.scss → Button/_button.scss} +19 -1
  99. package/src/ui/{Inputs/Text/TextInput.spec.tsx → Fields/Input/Input.spec.tsx} +8 -8
  100. package/src/ui/{Inputs/Text/TextInput.tsx → Fields/Input/Input.tsx} +4 -4
  101. package/src/ui/Modal/Modal.tsx +1 -0
  102. package/src/ui/ToolbarDropdown/ToolbarDropdown.spec.tsx +4 -2
  103. package/src/ui/ToolbarDropdown/ToolbarDropdown.tsx +1 -1
  104. package/src/ui/_typography.scss +46 -0
  105. package/src/utils/converters/mocks/squizNodeJson.mock.ts +75 -0
  106. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.spec.ts +445 -0
  107. package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.ts +191 -0
  108. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.spec.ts +307 -0
  109. package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.ts +123 -0
  110. package/src/utils/converters/validNodeTypes.spec.ts +33 -0
  111. package/src/utils/converters/validNodeTypes.ts +21 -0
  112. package/tests/renderWithEditor.tsx +2 -2
  113. package/tsconfig.json +1 -1
  114. package/lib/ui/Inputs/Select/Select.js +0 -23
  115. package/lib/ui/Inputs/Text/TextInput.d.ts +0 -4
  116. package/lib/ui/Inputs/Text/TextInput.js +0 -7
  117. package/lib/ui/ToolbarButton/ToolbarButton.d.ts +0 -10
  118. package/lib/ui/ToolbarButton/ToolbarButton.js +0 -5
  119. package/src/ui/ToolbarButton/ToolbarButton.tsx +0 -26
  120. package/src/ui/ToolbarButton/_toolbar-button.scss +0 -17
  121. /package/lib/ui/{Inputs → Fields}/Select/Select.d.ts +0 -0
  122. /package/src/ui/{Inputs → Fields}/Select/Select.spec.tsx +0 -0
  123. /package/src/ui/{Inputs → Fields}/Select/Select.tsx +0 -0
@@ -1,3 +1,7 @@
1
- export const getCursorRect = (coords) => {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCursorRect = void 0;
4
+ const getCursorRect = (coords) => {
2
5
  return new DOMRect(coords.left, coords.top, 1, coords.top - coords.bottom);
3
6
  };
7
+ exports.getCursorRect = getCursorRect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/formatted-text-editor",
3
- "version": "1.16.0",
3
+ "version": "1.21.1-alpha.10",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "scripts": {
@@ -12,6 +12,7 @@
12
12
  "dev": "vite",
13
13
  "preview": "vite preview",
14
14
  "test": "jest",
15
+ "test:coverage": "jest --coverage",
15
16
  "test:e2e": "vite build && vite preview --port 8080 & cypress open",
16
17
  "test:watch": "jest --watch"
17
18
  },
@@ -19,8 +20,10 @@
19
20
  "@headlessui/react": "1.7.11",
20
21
  "@mui/icons-material": "5.11.0",
21
22
  "@remirror/react": "2.0.25",
23
+ "@squiz/dx-json-schema-lib": "1.21.1-alpha.2",
22
24
  "clsx": "1.2.1",
23
- "react-hook-form": "7.43.2"
25
+ "react-hook-form": "7.43.2",
26
+ "react-image-size": "2.0.0"
24
27
  },
25
28
  "devDependencies": {
26
29
  "@testing-library/cypress": "9.0.0",
@@ -42,6 +45,7 @@
42
45
  "postcss-nested": "6.0.0",
43
46
  "postcss-prefix-selector": "1.16.0",
44
47
  "react": "18.2.0",
48
+ "react-diff-viewer-continued": "3.2.6",
45
49
  "react-dom": "18.2.0",
46
50
  "rimraf": "4.1.2",
47
51
  "tailwindcss": "3.2.6",
@@ -66,5 +70,5 @@
66
70
  "volta": {
67
71
  "node": "16.19.0"
68
72
  },
69
- "gitHead": "6fbf11b4507a80f8ef6dd09b489bbebf2bcfbac1"
73
+ "gitHead": "e4aaed9499051ca99e7f3e3bab76b3ffc1fd0f4b"
70
74
  }
@@ -6,12 +6,12 @@
6
6
  }
7
7
 
8
8
  .remirror-editor-wrapper {
9
- @apply text-gray-600 pt-0;
9
+ @apply text-gray-800 pt-0;
10
10
  }
11
11
 
12
12
  .remirror-editor {
13
13
  @apply bg-white shadow-none rounded-b p-3;
14
- min-height: 6rem;
14
+ overflow: auto;
15
15
 
16
16
  &:active,
17
17
  &:focus {
@@ -33,50 +33,3 @@
33
33
  @apply text-gray-500;
34
34
  }
35
35
  }
36
-
37
- a {
38
- @apply text-blue-300;
39
- text-decoration: underline;
40
- }
41
-
42
- .remirror-theme h1 {
43
- font-size: 1.625rem;
44
- font-weight: 600;
45
- letter-spacing: -0.2px;
46
- line-height: 2rem;
47
- }
48
-
49
- .remirror-theme h2 {
50
- font-size: 1.25rem;
51
- font-weight: 600;
52
- letter-spacing: -0.5px;
53
- line-height: 1.5rem;
54
- }
55
-
56
- .remirror-theme h3 {
57
- font-size: 1.125rem;
58
- font-weight: 600;
59
- letter-spacing: -0.2px;
60
- line-height: 1.375rem;
61
- }
62
-
63
- .remirror-theme h4 {
64
- font-size: 1rem;
65
- font-weight: 700;
66
- letter-spacing: -0.2px;
67
- line-height: 1.25rem;
68
- }
69
-
70
- .remirror-theme h5 {
71
- font-size: 1rem;
72
- font-weight: 600;
73
- letter-spacing: -0.2px;
74
- line-height: 1.25rem;
75
- }
76
-
77
- .remirror-theme h6 {
78
- font-size: 0.875rem;
79
- font-weight: 600;
80
- letter-spacing: -0.2px;
81
- line-height: 1.25rem;
82
- }
@@ -27,7 +27,7 @@ export const FloatingToolbar = () => {
27
27
  } else if (!data.marks?.link.isActive) {
28
28
  // if none of the selected text is a link show the option to create a link.
29
29
  buttons.push(
30
- <VerticalDivider key="link-divider" className="link-divider" />,
30
+ <VerticalDivider key="link-divider" className="editor-divider" />,
31
31
  <LinkButton key="add-link" inPopover={true} />,
32
32
  );
33
33
  }
@@ -9,6 +9,7 @@ import RedoButton from './Tools/Redo/RedoButton';
9
9
  import TextTypeDropdown from './Tools/TextType/TextTypeDropdown';
10
10
  import { useExtensionNames } from '../hooks';
11
11
  import LinkButton from './Tools/Link/LinkButton';
12
+ import ImageButton from './Tools/Image/ImageButton';
12
13
 
13
14
  export const Toolbar = () => {
14
15
  const extensionNames = useExtensionNames();
@@ -28,6 +29,7 @@ export const Toolbar = () => {
28
29
  {extensionNames.underline && <UnderlineButton />}
29
30
  {extensionNames.nodeFormatting && <TextAlignButtons />}
30
31
  {extensionNames.link && <LinkButton />}
32
+ {extensionNames.image && <ImageButton />}
31
33
  </RemirrorToolbar>
32
34
  );
33
35
  };
@@ -14,6 +14,6 @@ describe('Bold button', () => {
14
14
  expect(screen.getByRole('button', { name: 'Bold (cmd+B)' }).classList.contains('squiz-fte-btn')).toBeTruthy();
15
15
  const bold = screen.getByRole('button', { name: 'Bold (cmd+B)' });
16
16
  fireEvent.click(bold);
17
- expect(bold.classList.contains('is-active')).toBeTruthy();
17
+ expect(bold.classList.contains('squiz-fte-btn--is-active')).toBeTruthy();
18
18
  });
19
19
  });
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { useCommands, useActive, useChainedCommands } from '@remirror/react';
3
3
  import { BoldExtension } from '@remirror/extension-bold';
4
- import ToolbarButton from '../../../ui/ToolbarButton/ToolbarButton';
4
+ import Button from '../../../ui/Button/Button';
5
5
  import FormatBoldRoundedIcon from '@mui/icons-material/FormatBoldRounded';
6
6
 
7
7
  const BoldButton = () => {
@@ -17,7 +17,7 @@ const BoldButton = () => {
17
17
  };
18
18
 
19
19
  return (
20
- <ToolbarButton
20
+ <Button
21
21
  handleOnClick={handleSelect}
22
22
  isDisabled={!enabled}
23
23
  isActive={active.bold()}
@@ -0,0 +1,23 @@
1
+ import '@testing-library/jest-dom';
2
+ import { render, screen } from '@testing-library/react';
3
+ import React from 'react';
4
+ import ImageForm from './ImageForm';
5
+
6
+ describe('Image Form', () => {
7
+ const handleSubmit = jest.fn();
8
+ const data = {
9
+ src: 'https://httpcats.com/302.jpg',
10
+ alt: 'Cat with mouse in mouth',
11
+ width: 1600,
12
+ height: 1400,
13
+ };
14
+
15
+ it('Renders the form with the relevant fields', () => {
16
+ render(<ImageForm data={data} onSubmit={handleSubmit} />);
17
+
18
+ expect(screen.getByLabelText('Source')).toHaveValue('https://httpcats.com/302.jpg');
19
+ expect(screen.getByLabelText('Alternative description')).toHaveValue('Cat with mouse in mouth');
20
+ expect(screen.getByLabelText('Width')).toHaveValue(1600);
21
+ expect(screen.getByLabelText('Height')).toHaveValue(1400);
22
+ });
23
+ });
@@ -0,0 +1,92 @@
1
+ import React, { ReactElement, useState } from 'react';
2
+ import { Input } from '../../../../ui/Fields/Input/Input';
3
+ import { SubmitHandler, useForm } from 'react-hook-form';
4
+ import { getImageSize } from 'react-image-size';
5
+ import { ImageAttributes } from '@remirror/extension-image/dist-types/image-extension';
6
+ import Button from '../../../../ui/Button/Button';
7
+ import LinkOffIcon from '@mui/icons-material/LinkOff';
8
+ import InsertLinkRoundedIcon from '@mui/icons-material/InsertLinkRounded';
9
+
10
+ export type UpdateImageOptions = ImageAttributes & {
11
+ src: string;
12
+ alt: string;
13
+ width: number;
14
+ height: number;
15
+ };
16
+ export type ImageFormData = Pick<UpdateImageOptions, 'src' | 'alt' | 'width' | 'height'>;
17
+
18
+ export type FormProps = {
19
+ data: Partial<ImageFormData>;
20
+ onSubmit: SubmitHandler<ImageFormData>;
21
+ };
22
+ export type Dimensions = 'width' | 'height';
23
+
24
+ const ImageForm = ({ data, onSubmit }: FormProps): ReactElement => {
25
+ const { register, handleSubmit, setValue } = useForm<ImageFormData>({
26
+ defaultValues: data,
27
+ });
28
+ const [aspectRatioFromWidth, setAspectRatioFromWidth] = useState(9 / 16);
29
+ const [aspectRatioFromHeight, setAspectRatioFromHeight] = useState(16 / 9);
30
+ const [aspectRatioLocked, setAspectRatioLocked] = useState(true);
31
+
32
+ const setDimensionsFromURL = (e: { target: { value: string } }) => {
33
+ // get the new url, calculate the width and height and set those fields
34
+ getImageSize(e.target.value)
35
+ .then(({ width, height }) => {
36
+ setValue('width', width);
37
+ setValue('height', height);
38
+ setAspectRatioFromWidth(height / width);
39
+ setAspectRatioFromHeight(width / height);
40
+ })
41
+ .catch((errorMessage) => {
42
+ // TODO: we will use this when we add validation in a follow-up ticket
43
+ console.log(errorMessage);
44
+ });
45
+ };
46
+
47
+ const calculateDimensions = () => {
48
+ if (aspectRatioLocked) {
49
+ const currentTarget = event?.target as HTMLInputElement;
50
+ const type = currentTarget.name as Dimensions;
51
+ const currentValue = currentTarget.value as string;
52
+ const otherValue = type === 'width' ? 'height' : 'width';
53
+ const aspectRatio = type === 'width' ? aspectRatioFromWidth : aspectRatioFromHeight;
54
+ const newValue = Math.round(aspectRatio * Number(currentValue) * 100) / 100;
55
+ setValue(otherValue, newValue);
56
+ }
57
+ };
58
+
59
+ const toggleAspectRatio = () => {
60
+ setAspectRatioLocked(!aspectRatioLocked);
61
+ };
62
+
63
+ return (
64
+ <form className="squiz-fte-form" onSubmit={handleSubmit(onSubmit)}>
65
+ <div className="squiz-fte-form-group mb-2">
66
+ <Input label="Source" {...register('src')} onBlur={setDimensionsFromURL} />
67
+ </div>
68
+ <div className="squiz-fte-form-group mb-2">
69
+ <Input label="Alternative description" {...register('alt')} />
70
+ </div>
71
+ <div className="flex flex-row items-end">
72
+ <div className="squiz-fte-form-group mb-2">
73
+ <Input label="Width" {...register('width')} type="number" name="width" onChange={calculateDimensions} />
74
+ </div>
75
+ <div className="flex mx-1 mb-2">
76
+ <Button
77
+ handleOnClick={toggleAspectRatio}
78
+ isActive={false}
79
+ icon={aspectRatioLocked ? <InsertLinkRoundedIcon /> : <LinkOffIcon />}
80
+ label="Constrain properties"
81
+ isDisabled={false}
82
+ />
83
+ </div>
84
+ <div className="squiz-fte-form-group mb-2">
85
+ <Input label="Height" {...register('height')} type="number" name="height" onChange={calculateDimensions} />
86
+ </div>
87
+ </div>
88
+ </form>
89
+ );
90
+ };
91
+
92
+ export default ImageForm;
@@ -0,0 +1,79 @@
1
+ import '@testing-library/jest-dom';
2
+ import { screen, fireEvent, waitForElementToBeRemoved, act } from '@testing-library/react';
3
+ import React from 'react';
4
+ import { renderWithEditor } from '../../../../tests';
5
+ import ImageButton from './ImageButton';
6
+
7
+ describe('ImageButton', () => {
8
+ const openModal = async () => {
9
+ fireEvent.click(screen.getByRole('button', { name: 'Image (cmd+L)' }));
10
+ await screen.findByRole('button', { name: 'Apply' });
11
+ };
12
+
13
+ it('Opens the modal when clicking on the image button', async () => {
14
+ await renderWithEditor(<ImageButton />);
15
+
16
+ // open the modal and assert it is visible.
17
+ await openModal();
18
+ const modalHeading = screen.getByRole('heading', { name: 'Image' });
19
+ expect(modalHeading).toBeInTheDocument();
20
+ });
21
+
22
+ it('Opens the modal when clicking the keyboard shortcut', async () => {
23
+ const { editor, elements } = await renderWithEditor(<ImageButton />);
24
+
25
+ // press the keyboard shortcut.
26
+ fireEvent.keyDown(elements.editor, { key: 'l', ctrlKey: true });
27
+
28
+ // verify the modal opens
29
+ await act(() => editor.selectText(2));
30
+ expect(await screen.findByLabelText('Source')).toHaveValue('');
31
+ });
32
+
33
+ it('Adds a new image', async () => {
34
+ const { getJsonContent } = await renderWithEditor(<ImageButton />, { content: 'Some nonsense content here' });
35
+
36
+ // open the modal and add an image.
37
+ await openModal();
38
+ fireEvent.change(screen.getByLabelText('Source'), { target: { value: 'https://httpcats.com/529.jpg' } });
39
+ fireEvent.change(screen.getByLabelText('Alternative description'), { target: { value: 'Many cats' } });
40
+ fireEvent.click(screen.getByRole('button', { name: 'Apply' }));
41
+
42
+ await waitForElementToBeRemoved(() => screen.getByRole('button', { name: 'Apply' }));
43
+
44
+ expect(getJsonContent()).toEqual({
45
+ type: 'paragraph',
46
+ attrs: expect.any(Object),
47
+ content: [
48
+ {
49
+ type: 'image',
50
+ attrs: {
51
+ alt: 'Many cats',
52
+ crop: null,
53
+ height: '',
54
+ width: '',
55
+ rotate: null,
56
+ src: 'https://httpcats.com/529.jpg',
57
+ title: '',
58
+ fileName: null,
59
+ resizable: false,
60
+ },
61
+ },
62
+ { type: 'text', text: 'Some nonsense content here' },
63
+ ],
64
+ });
65
+ });
66
+
67
+ it('Closes the modal when clicking on the cancel button', async () => {
68
+ await renderWithEditor(<ImageButton />);
69
+
70
+ // open the modal and assert it is visible.
71
+ await openModal();
72
+ const modalHeading = screen.getByRole('heading', { name: 'Image' });
73
+ expect(modalHeading).toBeInTheDocument();
74
+
75
+ // close the modal and assert it has disappeared.
76
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
77
+ expect(modalHeading).not.toBeInTheDocument();
78
+ });
79
+ });
@@ -0,0 +1,57 @@
1
+ import React, { useState, useCallback } from 'react';
2
+ import ImageRoundedIcon from '@mui/icons-material/ImageRounded';
3
+ import ImageModal from './ImageModal';
4
+ import { ImageFormData } from './Form/ImageForm';
5
+ import Button from '../../../ui/Button/Button';
6
+ import { useCommands, useKeymap } from '@remirror/react';
7
+
8
+ const ImageButton = () => {
9
+ const [showModal, setShowModal] = useState(false);
10
+ const { insertImage } = useCommands();
11
+ const active = showModal;
12
+
13
+ const handleClick = () => {
14
+ if (!showModal) {
15
+ // form element are uncontrolled, let the event loop run to
16
+ // update the selected text in state before showing the modal.
17
+ requestAnimationFrame(() => {
18
+ setShowModal(true);
19
+ });
20
+ }
21
+ };
22
+
23
+ const insertImageFromData = (data: ImageFormData) => {
24
+ const { src, alt, width, height } = data;
25
+ if (src) {
26
+ insertImage({ src, alt, width, height });
27
+ }
28
+ };
29
+
30
+ const handleSubmit = (data: ImageFormData) => {
31
+ insertImageFromData(data);
32
+ setShowModal(false);
33
+ };
34
+
35
+ const handleShortcut = useCallback(() => {
36
+ handleClick();
37
+ // Prevent other key handlers being run
38
+ return true;
39
+ }, []);
40
+
41
+ useKeymap('Mod-l', handleShortcut);
42
+
43
+ return (
44
+ <>
45
+ <Button
46
+ handleOnClick={handleClick}
47
+ isActive={active}
48
+ icon={<ImageRoundedIcon />}
49
+ label="Image (cmd+L)"
50
+ isDisabled={false}
51
+ />
52
+ {showModal && <ImageModal onCancel={() => setShowModal(false)} onSubmit={handleSubmit} />}
53
+ </>
54
+ );
55
+ };
56
+
57
+ export default ImageButton;
@@ -0,0 +1,83 @@
1
+ import '@testing-library/jest-dom';
2
+ import { screen, fireEvent } from '@testing-library/react';
3
+ import React from 'react';
4
+ import { renderWithEditor } from '../../../../tests';
5
+ import ImageModal from './ImageModal';
6
+
7
+ const mockSubmitFunction = jest.fn();
8
+ const mockCancelFunction = jest.fn();
9
+ const setup = () => {
10
+ const utils = renderWithEditor(<ImageModal onCancel={mockCancelFunction} onSubmit={mockSubmitFunction} />);
11
+ const sourceInput = screen.getByRole('textbox', { name: /source/i }) as HTMLInputElement;
12
+ const altInput = screen.getByRole('textbox', { name: /alt/i }) as HTMLInputElement;
13
+ const widthInput = screen.getByRole('spinbutton', { name: /Width/i }) as HTMLInputElement;
14
+ const heightInput = screen.getByRole('spinbutton', { name: /Height/i }) as HTMLInputElement;
15
+ return {
16
+ sourceInput,
17
+ altInput,
18
+ widthInput,
19
+ heightInput,
20
+ ...utils,
21
+ };
22
+ };
23
+
24
+ describe('ImageModal', () => {
25
+ it('Renders the modal with title', async () => {
26
+ await renderWithEditor(<ImageModal onCancel={mockCancelFunction} onSubmit={mockSubmitFunction} />);
27
+ const modalHeading = screen.getByRole('heading', { name: 'Image' });
28
+ expect(modalHeading).toBeInTheDocument();
29
+ });
30
+
31
+ it('Populates the source field when a source is supplied', async () => {
32
+ const { sourceInput } = setup();
33
+ fireEvent.change(sourceInput, { target: { value: 'https://httpcats.com/302.jpg' } });
34
+ expect(sourceInput.value).toBe('https://httpcats.com/302.jpg');
35
+ });
36
+
37
+ it('Renders empty width and height fields if the image source is empty', async () => {
38
+ const { sourceInput, widthInput, heightInput } = setup();
39
+ fireEvent.change(sourceInput, { target: { value: '' } });
40
+ expect(widthInput.value).toBe('');
41
+ expect(heightInput.value).toBe('');
42
+ });
43
+
44
+ it('Updates the height field with aspect ratio based value from width', async () => {
45
+ const { widthInput, heightInput } = setup();
46
+ fireEvent.change(widthInput, { target: { value: '300' } });
47
+ expect(widthInput.value).toBe('300');
48
+ expect(heightInput.value).toBe('168.75');
49
+ });
50
+ it('Updates the width field with aspect ratio based value from height', async () => {
51
+ const { widthInput, heightInput } = setup();
52
+ fireEvent.change(heightInput, { target: { value: '100' } });
53
+ expect(heightInput.value).toBe('100');
54
+ expect(widthInput.value).toBe('177.78');
55
+ });
56
+ it('Does not change the width when height is changed and aspect ratio link is off', () => {
57
+ const { widthInput, heightInput } = setup();
58
+ fireEvent.change(heightInput, { target: { value: '100' } });
59
+ expect(heightInput.value).toBe('100');
60
+ expect(widthInput.value).toBe('177.78');
61
+ fireEvent.click(screen.getByRole('button', { name: /constrain properties/i }));
62
+ fireEvent.change(heightInput, { target: { value: '200' } });
63
+ expect(heightInput.value).toBe('200');
64
+ expect(widthInput.value).toBe('177.78');
65
+ });
66
+ it('Does not change the height when width is changed and aspect ratio link is off', () => {
67
+ const { widthInput, heightInput } = setup();
68
+ fireEvent.change(widthInput, { target: { value: '450' } });
69
+ expect(widthInput.value).toBe('450');
70
+ expect(heightInput.value).toBe('253.13');
71
+ fireEvent.click(screen.getByRole('button', { name: /constrain properties/i }));
72
+ fireEvent.change(widthInput, { target: { value: '600' } });
73
+ expect(widthInput.value).toBe('600');
74
+ expect(heightInput.value).toBe('253.13');
75
+ });
76
+ it('Changes the icon when aspect ratio button is toggled', () => {
77
+ renderWithEditor(<ImageModal onCancel={mockCancelFunction} onSubmit={mockSubmitFunction} />);
78
+ expect(screen.getByTestId('InsertLinkRoundedIcon')).toBeInTheDocument();
79
+ fireEvent.click(screen.getByRole('button', { name: /constrain properties/i }));
80
+ expect(screen.queryByTestId('InsertLinkRoundedIcon')).not.toBeInTheDocument();
81
+ expect(screen.getByTestId('LinkOffIcon')).toBeInTheDocument();
82
+ });
83
+ });
@@ -0,0 +1,29 @@
1
+ import { getMarkRanges } from 'remirror';
2
+ import ImageForm, { ImageFormData } from './Form/ImageForm';
3
+ import React from 'react';
4
+ import { useRemirrorContext, useCurrentSelection } from '@remirror/react';
5
+ import FormModal from '../../../ui/Modal/FormModal';
6
+ import { SubmitHandler } from 'react-hook-form';
7
+
8
+ type ImageModalProps = {
9
+ onCancel: () => void;
10
+ onSubmit: SubmitHandler<ImageFormData>;
11
+ };
12
+
13
+ const ImageModal = ({ onCancel, onSubmit }: ImageModalProps) => {
14
+ const {
15
+ helpers,
16
+ view: { state },
17
+ } = useRemirrorContext();
18
+ const selection = useCurrentSelection();
19
+ const currentImage = getMarkRanges(selection, 'image')[0];
20
+ const selectedImage = helpers.getTextBetween(selection.from, selection.to, state.doc);
21
+
22
+ return (
23
+ <FormModal title="Image" onCancel={onCancel}>
24
+ <ImageForm data={{ ...currentImage?.mark.attrs, src: selectedImage }} onSubmit={onSubmit} />
25
+ </FormModal>
26
+ );
27
+ };
28
+
29
+ export default ImageModal;
@@ -14,6 +14,6 @@ describe('Italic button', () => {
14
14
  expect(screen.getByRole('button', { name: 'Italic (cmd+I)' }).classList.contains('squiz-fte-btn')).toBeTruthy();
15
15
  const italic = screen.getByRole('button', { name: 'Italic (cmd+I)' });
16
16
  fireEvent.click(italic);
17
- expect(italic.classList.contains('is-active')).toBeTruthy();
17
+ expect(italic.classList.contains('squiz-fte-btn--is-active')).toBeTruthy();
18
18
  });
19
19
  });
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { useCommands, useActive, useChainedCommands } from '@remirror/react';
3
3
  import { ItalicExtension } from '@remirror/extension-italic';
4
- import ToolbarButton from '../../../ui/ToolbarButton/ToolbarButton';
4
+ import Button from '../../../ui/Button/Button';
5
5
  import FormatItalicRoundedIcon from '@mui/icons-material/FormatItalicRounded';
6
6
 
7
7
  const ItalicButton = () => {
@@ -17,7 +17,7 @@ const ItalicButton = () => {
17
17
  };
18
18
 
19
19
  return (
20
- <ToolbarButton
20
+ <Button
21
21
  handleOnClick={handleSelect}
22
22
  isDisabled={!enabled}
23
23
  isActive={active.italic()}
@@ -1,6 +1,6 @@
1
1
  import React, { ReactElement } from 'react';
2
- import { TextInput } from '../../../../ui/Inputs/Text/TextInput';
3
- import { Select, SelectOptions } from '../../../../ui/Inputs/Select/Select';
2
+ import { Input } from '../../../../ui/Fields/Input/Input';
3
+ import { Select, SelectOptions } from '../../../../ui/Fields/Select/Select';
4
4
  import { SubmitHandler, useForm } from 'react-hook-form';
5
5
  import { UpdateLinkOptions } from '../../../../Extensions/LinkExtension/LinkExtension';
6
6
 
@@ -24,13 +24,13 @@ const LinkForm = ({ data, onSubmit }: FormProps): ReactElement => {
24
24
  return (
25
25
  <form className="squiz-fte-form" onSubmit={handleSubmit(onSubmit)}>
26
26
  <div className="squiz-fte-form-group mb-2">
27
- <TextInput label="URL" {...register('href')} />
27
+ <Input label="URL" {...register('href')} />
28
28
  </div>
29
29
  <div className="squiz-fte-form-group mb-2">
30
- <TextInput label="Text" {...register('text')} />
30
+ <Input label="Text" {...register('text')} />
31
31
  </div>
32
32
  <div className="squiz-fte-form-group mb-2">
33
- <TextInput label="Title" {...register('title')} />
33
+ <Input label="Title" {...register('title')} />
34
34
  </div>
35
35
  <div className="squiz-fte-form-group mb-0">
36
36
  <Select
@@ -2,7 +2,7 @@ import React, { useCallback, useState } from 'react';
2
2
  import InsertLinkRoundedIcon from '@mui/icons-material/InsertLinkRounded';
3
3
  import LinkModal from './LinkModal';
4
4
  import { LinkFormData } from './Form/LinkForm';
5
- import ToolbarButton from '../../../ui/ToolbarButton/ToolbarButton';
5
+ import Button from '../../../ui/Button/Button';
6
6
  import { useActive, useCommands, useExtensionEvent } from '@remirror/react';
7
7
  import { LinkExtension } from '../../../Extensions/LinkExtension/LinkExtension';
8
8
 
@@ -42,7 +42,7 @@ const LinkButton = ({ inPopover = false }: LinkButtonProps) => {
42
42
 
43
43
  return (
44
44
  <>
45
- <ToolbarButton
45
+ <Button
46
46
  handleOnClick={handleClick}
47
47
  isActive={active.link()}
48
48
  icon={<InsertLinkRoundedIcon />}
@@ -1,6 +1,6 @@
1
1
  import React from 'react';
2
2
  import { useRemirrorContext, useChainedCommands } from '@remirror/react';
3
- import ToolbarButton from '../../../ui/ToolbarButton/ToolbarButton';
3
+ import Button from '../../../ui/Button/Button';
4
4
  import LinkOffIcon from '@mui/icons-material/LinkOff';
5
5
 
6
6
  const RemoveLinkButton = () => {
@@ -14,7 +14,7 @@ const RemoveLinkButton = () => {
14
14
  };
15
15
 
16
16
  return (
17
- <ToolbarButton
17
+ <Button
18
18
  handleOnClick={handleClick}
19
19
  isActive={false}
20
20
  isDisabled={!enabled}
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { useCommands, useHelpers } from '@remirror/react';
3
3
  import { HistoryExtension } from 'remirror/extensions';
4
- import ToolbarButton from '../../../ui/ToolbarButton/ToolbarButton';
4
+ import Button from '../../../ui/Button/Button';
5
5
  import RedoRoundedIcon from '@mui/icons-material/RedoRounded';
6
6
 
7
7
  const RedoButton = () => {
@@ -17,7 +17,7 @@ const RedoButton = () => {
17
17
  const enabled = redoDepth() > 0;
18
18
 
19
19
  return (
20
- <ToolbarButton
20
+ <Button
21
21
  handleOnClick={handleSelect}
22
22
  isDisabled={!enabled}
23
23
  isActive={false}
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { useCommands, useChainedCommands } from '@remirror/react';
3
3
  import { NodeFormattingExtension } from '@remirror/extension-node-formatting';
4
- import ToolbarButton from '../../../../ui/ToolbarButton/ToolbarButton';
4
+ import Button from '../../../../ui/Button/Button';
5
5
  import FormatAlignCenterIcon from '@mui/icons-material/FormatAlignCenter';
6
6
 
7
7
  const CenterAlignButton = () => {
@@ -18,7 +18,7 @@ const CenterAlignButton = () => {
18
18
  const enabled = centerAlign.enabled();
19
19
 
20
20
  return (
21
- <ToolbarButton
21
+ <Button
22
22
  handleOnClick={handleSelect}
23
23
  isDisabled={!enabled}
24
24
  isActive={active}