@squiz/formatted-text-editor 1.21.1-alpha.34 → 1.21.1-alpha.39
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.
- package/demo/App.tsx +9 -2
- package/lib/Editor/EditorContext.d.ts +6 -1
- package/lib/Editor/EditorContext.js +1 -1
- package/lib/EditorToolbar/FloatingToolbar.js +1 -1
- package/lib/EditorToolbar/Tools/Image/Form/ImageForm.d.ts +9 -8
- package/lib/EditorToolbar/Tools/Image/Form/ImageForm.js +71 -43
- package/lib/EditorToolbar/Tools/Image/ImageButton.js +13 -15
- package/lib/EditorToolbar/Tools/Image/ImageModal.js +7 -1
- package/lib/EditorToolbar/Tools/Link/Form/LinkForm.js +1 -1
- package/lib/Extensions/Extensions.d.ts +5 -0
- package/lib/Extensions/Extensions.js +12 -1
- package/lib/Extensions/ImageExtension/AssetImageExtension.d.ts +17 -0
- package/lib/Extensions/ImageExtension/AssetImageExtension.js +92 -0
- package/lib/Extensions/ImageExtension/ImageExtension.d.ts +1 -4
- package/lib/Extensions/ImageExtension/ImageExtension.js +4 -78
- package/lib/Extensions/LinkExtension/AssetLinkExtension.js +3 -3
- package/lib/Extensions/LinkExtension/LinkExtension.d.ts +1 -3
- package/lib/Extensions/LinkExtension/LinkExtension.js +1 -9
- package/lib/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.js +12 -3
- package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.js +14 -5
- package/lib/utils/resolveMatrixAssetUrl.d.ts +1 -0
- package/lib/utils/resolveMatrixAssetUrl.js +10 -0
- package/package.json +3 -3
- package/src/Editor/EditorContext.spec.tsx +3 -3
- package/src/Editor/EditorContext.ts +9 -2
- package/src/EditorToolbar/FloatingToolbar.spec.tsx +24 -4
- package/src/EditorToolbar/FloatingToolbar.tsx +1 -1
- package/src/EditorToolbar/Tools/Image/Form/ImageForm.spec.tsx +26 -5
- package/src/EditorToolbar/Tools/Image/Form/ImageForm.tsx +145 -96
- package/src/EditorToolbar/Tools/Image/ImageButton.spec.tsx +128 -7
- package/src/EditorToolbar/Tools/Image/ImageButton.tsx +15 -17
- package/src/EditorToolbar/Tools/Image/ImageModal.tsx +7 -1
- package/src/EditorToolbar/Tools/Link/Form/LinkForm.tsx +1 -1
- package/src/EditorToolbar/Tools/Link/LinkButton.spec.tsx +17 -5
- package/src/EditorToolbar/Tools/Link/RemoveLinkButton.spec.tsx +1 -0
- package/src/Extensions/Extensions.ts +11 -0
- package/src/Extensions/ImageExtension/AssetImageExtension.spec.ts +76 -0
- package/src/Extensions/ImageExtension/AssetImageExtension.ts +111 -0
- package/src/Extensions/ImageExtension/ImageExtension.ts +6 -99
- package/src/Extensions/LinkExtension/AssetLinkExtension.spec.ts +1 -1
- package/src/Extensions/LinkExtension/AssetLinkExtension.ts +3 -3
- package/src/Extensions/LinkExtension/LinkExtension.ts +2 -22
- package/src/utils/converters/mocks/squizNodeJson.mock.ts +19 -0
- package/src/utils/converters/remirrorNodeToSquizNode/remirrorNodeToSquizNode.ts +13 -3
- package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.spec.ts +13 -1
- package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.ts +13 -5
- package/src/utils/resolveMatrixAssetUrl.spec.ts +26 -0
- package/src/utils/resolveMatrixAssetUrl.ts +7 -0
@@ -19,7 +19,7 @@ let LinkExtension = class LinkExtension extends core_1.MarkExtension {
|
|
19
19
|
createMarkSpec(extra, override) {
|
20
20
|
return {
|
21
21
|
inclusive: false,
|
22
|
-
excludes:
|
22
|
+
excludes: Extensions_1.MarkName.AssetLink,
|
23
23
|
...override,
|
24
24
|
attrs: {
|
25
25
|
...extra.defaults(),
|
@@ -56,10 +56,6 @@ let LinkExtension = class LinkExtension extends core_1.MarkExtension {
|
|
56
56
|
},
|
57
57
|
};
|
58
58
|
}
|
59
|
-
shortcut(_) {
|
60
|
-
this.options.onShortcut();
|
61
|
-
return true;
|
62
|
-
}
|
63
59
|
updateLink({ text, attrs, range }) {
|
64
60
|
return this.store.getExtension(CommandsExtension_1.CommandsExtension).updateMark({
|
65
61
|
attrs,
|
@@ -73,9 +69,6 @@ let LinkExtension = class LinkExtension extends core_1.MarkExtension {
|
|
73
69
|
return (0, core_1.removeMark)({ type: this.type });
|
74
70
|
}
|
75
71
|
};
|
76
|
-
__decorate([
|
77
|
-
(0, core_1.keyBinding)({ shortcut: core_1.NamedShortcut.InsertLink })
|
78
|
-
], LinkExtension.prototype, "shortcut", null);
|
79
72
|
__decorate([
|
80
73
|
(0, core_1.command)()
|
81
74
|
], LinkExtension.prototype, "updateLink", null);
|
@@ -88,7 +81,6 @@ LinkExtension = __decorate([
|
|
88
81
|
defaultTarget: common_1.LinkTarget.Self,
|
89
82
|
supportedTargets: [common_1.LinkTarget.Self, common_1.LinkTarget.Blank],
|
90
83
|
},
|
91
|
-
handlerKeys: ['onShortcut'],
|
92
84
|
defaultPriority: remirror_1.ExtensionPriority.Medium,
|
93
85
|
})
|
94
86
|
], LinkExtension);
|
@@ -2,6 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.remirrorNodeToSquizNode = exports.resolveNodeTag = void 0;
|
4
4
|
const undefinedIfEmpty_1 = require("../../undefinedIfEmpty");
|
5
|
+
const Extensions_1 = require("../../../Extensions/Extensions");
|
5
6
|
const resolveNodeTag = (node) => {
|
6
7
|
if (node.type.name === 'text') {
|
7
8
|
return 'span';
|
@@ -62,7 +63,7 @@ const transformFragment = (fragment) => {
|
|
62
63
|
return transformed;
|
63
64
|
};
|
64
65
|
const transformNode = (node) => {
|
65
|
-
const attributes = node.type.name ===
|
66
|
+
const attributes = node.type.name === Extensions_1.NodeName.Image ? transformAttributes(node.attrs) : undefined;
|
66
67
|
const formattingOptions = (0, undefinedIfEmpty_1.undefinedIfEmpty)(resolveFormattingOptions(node));
|
67
68
|
const font = (0, undefinedIfEmpty_1.undefinedIfEmpty)(resolveFontOptions(node));
|
68
69
|
let transformedNode = { type: 'text', value: node.text || '' };
|
@@ -71,16 +72,24 @@ const transformNode = (node) => {
|
|
71
72
|
// If the node isn't a text type assume it is a tag type and wrap in a tag.
|
72
73
|
// If we pick the wrong tag here it will be corrected later as part of looping through the
|
73
74
|
// non-font marks.
|
74
|
-
if (node.type.name !==
|
75
|
+
if (node.type.name !== Extensions_1.NodeName.Text || attributes || formattingOptions || font) {
|
75
76
|
transformedNode = {
|
76
77
|
type: 'tag',
|
77
78
|
tag: (0, exports.resolveNodeTag)(node),
|
78
|
-
children: node.type.name ===
|
79
|
+
children: node.type.name === Extensions_1.NodeName.Text ? [transformedNode] : transformFragment(node.content),
|
79
80
|
attributes,
|
80
81
|
formattingOptions,
|
81
82
|
font,
|
82
83
|
};
|
83
84
|
}
|
85
|
+
if (node.type.name === Extensions_1.NodeName.AssetImage) {
|
86
|
+
transformedNode = {
|
87
|
+
type: 'matrix-image',
|
88
|
+
matrixAssetId: node.attrs.matrixAssetId,
|
89
|
+
matrixIdentifier: node.attrs.matrixIdentifier,
|
90
|
+
matrixDomain: node.attrs.matrixDomain,
|
91
|
+
};
|
92
|
+
}
|
84
93
|
node.marks.forEach((mark) => {
|
85
94
|
switch (mark.type.name) {
|
86
95
|
case 'bold':
|
@@ -2,9 +2,11 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.squizNodeToRemirrorNode = void 0;
|
4
4
|
const undefinedIfEmpty_1 = require("../../undefinedIfEmpty");
|
5
|
+
const Extensions_1 = require("../../../Extensions/Extensions");
|
5
6
|
const getNodeType = (node) => {
|
6
7
|
const typeMap = {
|
7
|
-
'link-to-matrix-asset':
|
8
|
+
'link-to-matrix-asset': Extensions_1.NodeName.Text,
|
9
|
+
'matrix-image': Extensions_1.NodeName.AssetImage,
|
8
10
|
text: 'text',
|
9
11
|
};
|
10
12
|
const tagMap = {
|
@@ -17,8 +19,8 @@ const getNodeType = (node) => {
|
|
17
19
|
img: 'image',
|
18
20
|
pre: 'preformatted',
|
19
21
|
p: 'paragraph',
|
20
|
-
a:
|
21
|
-
span:
|
22
|
+
a: Extensions_1.NodeName.Text,
|
23
|
+
span: Extensions_1.NodeName.Text,
|
22
24
|
};
|
23
25
|
if (typeMap[node.type]) {
|
24
26
|
return typeMap[node.type];
|
@@ -41,6 +43,13 @@ const getNodeAttributes = (node) => {
|
|
41
43
|
title: node.attributes?.title,
|
42
44
|
};
|
43
45
|
}
|
46
|
+
else if (node.type === 'matrix-image') {
|
47
|
+
return {
|
48
|
+
matrixAssetId: node.matrixAssetId,
|
49
|
+
matrixDomain: node.matrixDomain,
|
50
|
+
matrixIdentifier: node.matrixIdentifier,
|
51
|
+
};
|
52
|
+
}
|
44
53
|
else if (node.type === 'tag') {
|
45
54
|
return {
|
46
55
|
nodeIndent: null,
|
@@ -56,7 +65,7 @@ const getNodeMarks = (node) => {
|
|
56
65
|
const marks = [];
|
57
66
|
if (node.type === 'tag' && node.tag === 'a') {
|
58
67
|
marks.push({
|
59
|
-
type:
|
68
|
+
type: Extensions_1.MarkName.Link,
|
60
69
|
attrs: {
|
61
70
|
href: node.attributes?.href,
|
62
71
|
target: node.attributes?.target ?? null,
|
@@ -67,7 +76,7 @@ const getNodeMarks = (node) => {
|
|
67
76
|
}
|
68
77
|
else if (node.type === 'link-to-matrix-asset') {
|
69
78
|
marks.push({
|
70
|
-
type:
|
79
|
+
type: Extensions_1.MarkName.AssetLink,
|
71
80
|
attrs: {
|
72
81
|
matrixAssetId: node.matrixAssetId,
|
73
82
|
matrixDomain: node.matrixDomain,
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const resolveMatrixAssetUrl: (id: string, matrixDomain: string) => string;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.resolveMatrixAssetUrl = void 0;
|
4
|
+
const resolveMatrixAssetUrl = (id, matrixDomain) => {
|
5
|
+
if (matrixDomain.indexOf('://') < 0) {
|
6
|
+
matrixDomain = `${window.location.protocol}//${matrixDomain}`;
|
7
|
+
}
|
8
|
+
return new URL(`/_nocache?a=${encodeURIComponent(id)}`, matrixDomain).toString();
|
9
|
+
};
|
10
|
+
exports.resolveMatrixAssetUrl = resolveMatrixAssetUrl;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@squiz/formatted-text-editor",
|
3
|
-
"version": "1.21.1-alpha.
|
3
|
+
"version": "1.21.1-alpha.39",
|
4
4
|
"main": "lib/index.js",
|
5
5
|
"types": "lib/index.d.ts",
|
6
6
|
"scripts": {
|
@@ -37,7 +37,7 @@
|
|
37
37
|
"@vitejs/plugin-react": "3.0.0",
|
38
38
|
"autoprefixer": "10.4.13",
|
39
39
|
"cypress": "12.5.1",
|
40
|
-
"deepmerge": "
|
40
|
+
"deepmerge": "4.3.0",
|
41
41
|
"eslint-plugin-cypress": "2.12.1",
|
42
42
|
"eslint-plugin-jsx-a11y": "6.7.1",
|
43
43
|
"eslint-plugin-react": "7.32.2",
|
@@ -74,5 +74,5 @@
|
|
74
74
|
"volta": {
|
75
75
|
"node": "18.15.0"
|
76
76
|
},
|
77
|
-
"gitHead": "
|
77
|
+
"gitHead": "52f09f966b5eaa35149786dd1f2c55bf9ad8bde5"
|
78
78
|
}
|
@@ -9,18 +9,18 @@ describe('EditorContext', () => {
|
|
9
9
|
return null;
|
10
10
|
};
|
11
11
|
|
12
|
-
it('Has expected defaults', () => {
|
12
|
+
it('Has expected defaults', async () => {
|
13
13
|
render(<Component />);
|
14
14
|
|
15
15
|
const defaultContext = defaultContextFn.mock.calls[0][0];
|
16
16
|
|
17
17
|
expect(defaultContext).toEqual({
|
18
18
|
matrix: {
|
19
|
-
|
19
|
+
resolveMatrixAsset: expect.any(Function),
|
20
20
|
matrixDomain: '',
|
21
21
|
matrixIdentifier: '',
|
22
22
|
},
|
23
23
|
});
|
24
|
-
expect(defaultContext.matrix.
|
24
|
+
expect(await defaultContext.matrix.resolveMatrixAsset('fake-asset-id')).toBeNull();
|
25
25
|
});
|
26
26
|
});
|
@@ -1,10 +1,17 @@
|
|
1
1
|
import React from 'react';
|
2
2
|
|
3
|
+
export type MatrixAsset = {
|
4
|
+
id: string;
|
5
|
+
type: string | 'image';
|
6
|
+
};
|
7
|
+
|
8
|
+
export type MatrixAssetResolver = (assetId: string) => Promise<MatrixAsset | null>;
|
9
|
+
|
3
10
|
export type EditorContextOptions = {
|
4
11
|
matrix: {
|
5
12
|
matrixIdentifier: string;
|
6
13
|
matrixDomain: string;
|
7
|
-
|
14
|
+
resolveMatrixAsset: MatrixAssetResolver;
|
8
15
|
};
|
9
16
|
};
|
10
17
|
|
@@ -12,7 +19,7 @@ export const defaultEditorContext: EditorContextOptions = {
|
|
12
19
|
matrix: {
|
13
20
|
matrixIdentifier: '',
|
14
21
|
matrixDomain: '',
|
15
|
-
|
22
|
+
resolveMatrixAsset: () => Promise.resolve(null),
|
16
23
|
},
|
17
24
|
};
|
18
25
|
|
@@ -1,7 +1,12 @@
|
|
1
1
|
import { act, screen } from '@testing-library/react';
|
2
|
+
import { NodeSelection } from 'prosemirror-state';
|
2
3
|
import { renderWithEditor } from '../../tests';
|
3
4
|
|
4
5
|
describe('FloatingToolbar', () => {
|
6
|
+
const getFloatingButtonNames = () => {
|
7
|
+
return screen.queryAllByRole('button').map((button) => button.getAttribute('title'));
|
8
|
+
};
|
9
|
+
|
5
10
|
it.each([
|
6
11
|
['Nothing selected', 3, 3, []],
|
7
12
|
['Regular text selected', 3, 4, ['Bold (cmd+B)', 'Italic (cmd+I)', 'Underline (cmd+U)', 'Link (cmd+K)']],
|
@@ -9,7 +14,6 @@ describe('FloatingToolbar', () => {
|
|
9
14
|
['Nothing selected, positioned directly on the left of a link', 12, 12, []],
|
10
15
|
['Nothing selected, positioned directly on the right of a link', 19, 19, []],
|
11
16
|
['Nothing selected, positioned within a link', 13, 13, ['Link (cmd+K)', 'Remove link']],
|
12
|
-
['Link partially selected', 15, 17, ['Link (cmd+K)', 'Remove link']],
|
13
17
|
])(
|
14
18
|
'Renders formatting buttons when text is selected - %s',
|
15
19
|
async (description: string, from: number, to: number, expectedButtons: string[]) => {
|
@@ -20,10 +24,26 @@ describe('FloatingToolbar', () => {
|
|
20
24
|
|
21
25
|
await act(() => editor.selectText({ from, to }));
|
22
26
|
|
23
|
-
|
24
|
-
|
27
|
+
expect(getFloatingButtonNames()).toEqual(expectedButtons);
|
28
|
+
},
|
29
|
+
);
|
30
|
+
|
31
|
+
it.each([
|
32
|
+
['Image selected', 1, ['Image (cmd+L)']],
|
33
|
+
['Asset image selected', 2, ['Image (cmd+L)']],
|
34
|
+
])(
|
35
|
+
'Renders formatting buttons when node is selected - %s',
|
36
|
+
async (description: string, pos: number, expectedButtons: string[]) => {
|
37
|
+
const { editor } = await renderWithEditor(null, {
|
38
|
+
content:
|
39
|
+
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />' +
|
40
|
+
'<img data-matrix-asset-id="100" data-matrix-identifier="key" data-matrix-domain="my-matrix.squiz.net" />',
|
41
|
+
editable: true,
|
42
|
+
});
|
43
|
+
|
44
|
+
await act(() => editor.selectText(new NodeSelection(editor.state.doc.resolve(pos))));
|
25
45
|
|
26
|
-
expect(
|
46
|
+
expect(getFloatingButtonNames()).toEqual(expectedButtons);
|
27
47
|
},
|
28
48
|
);
|
29
49
|
});
|
@@ -27,7 +27,7 @@ export const FloatingToolbar = () => {
|
|
27
27
|
extensionNames.underline && <UnderlineButton key="underline" />,
|
28
28
|
];
|
29
29
|
|
30
|
-
if (active.image()) {
|
30
|
+
if (active.image() || active.assetImage()) {
|
31
31
|
buttons = [<ImageButton key="add-image" inPopover={true} />];
|
32
32
|
} else if (marks?.[MarkName.Link].isExclusivelyActive || marks?.[MarkName.AssetLink].isExclusivelyActive) {
|
33
33
|
// if all of the selected text is a link show the options to update/remove the link instead of the regular
|
@@ -2,25 +2,46 @@ import '@testing-library/jest-dom';
|
|
2
2
|
import { render, screen, act, fireEvent } from '@testing-library/react';
|
3
3
|
import React from 'react';
|
4
4
|
import ImageForm from './ImageForm';
|
5
|
+
import { NodeName } from '../../../../Extensions/Extensions';
|
5
6
|
|
6
7
|
describe('Image Form', () => {
|
7
8
|
const handleSubmit = jest.fn();
|
8
9
|
const data = {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
imageType: NodeName.Image,
|
11
|
+
image: {
|
12
|
+
src: 'https://httpcats.com/302.jpg',
|
13
|
+
alt: 'Cat with mouse in mouth',
|
14
|
+
width: 1600,
|
15
|
+
height: 1400,
|
16
|
+
},
|
13
17
|
};
|
14
18
|
|
15
|
-
it('Renders the form with the relevant fields', () => {
|
19
|
+
it('Renders the form with the relevant fields for arbitrary images', () => {
|
16
20
|
render(<ImageForm data={data} onSubmit={handleSubmit} />);
|
17
21
|
|
22
|
+
expect(screen.getByLabelText('Type')).toHaveTextContent('External image');
|
18
23
|
expect(screen.getByLabelText('Source')).toHaveValue('https://httpcats.com/302.jpg');
|
19
24
|
expect(screen.getByLabelText('Alternative description')).toHaveValue('Cat with mouse in mouth');
|
20
25
|
expect(screen.getByLabelText('Width')).toHaveValue(1600);
|
21
26
|
expect(screen.getByLabelText('Height')).toHaveValue(1400);
|
22
27
|
});
|
23
28
|
|
29
|
+
it('Renders the form with the relevant fields for asset images', () => {
|
30
|
+
render(
|
31
|
+
<ImageForm
|
32
|
+
data={{
|
33
|
+
...data,
|
34
|
+
imageType: NodeName.AssetImage,
|
35
|
+
assetImage: { matrixAssetId: '100' },
|
36
|
+
}}
|
37
|
+
onSubmit={handleSubmit}
|
38
|
+
/>,
|
39
|
+
);
|
40
|
+
|
41
|
+
expect(screen.getByLabelText('Type')).toHaveTextContent('Asset image');
|
42
|
+
expect(screen.getByLabelText('Asset ID')).toHaveValue('100');
|
43
|
+
});
|
44
|
+
|
24
45
|
it('calculates the height when width changes and aspect ratio is locked', () => {
|
25
46
|
render(<ImageForm data={data} onSubmit={handleSubmit} />);
|
26
47
|
const widthInput = screen.getByLabelText('Width');
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import React, { ReactElement, useState } from 'react';
|
1
|
+
import React, { ReactElement, useContext, useState } from 'react';
|
2
2
|
import { Input } from '../../../../ui/Fields/Input/Input';
|
3
3
|
import { SubmitHandler, useForm } from 'react-hook-form';
|
4
4
|
import { getImageSize } from 'react-image-size';
|
@@ -7,20 +7,28 @@ import Button from '../../../../ui/Button/Button';
|
|
7
7
|
import LinkOffIcon from '@mui/icons-material/LinkOff';
|
8
8
|
import InsertLinkRoundedIcon from '@mui/icons-material/InsertLinkRounded';
|
9
9
|
import clsx from 'clsx';
|
10
|
+
import { NodeName } from '../../../../Extensions/Extensions';
|
11
|
+
import { AssetImageAttributes } from '../../../../Extensions/ImageExtension/AssetImageExtension';
|
12
|
+
import { DeepPartial } from '../../../../types';
|
13
|
+
import { Select, SelectOptions } from '../../../../ui/Fields/Select/Select';
|
14
|
+
import { EditorContext } from '../../../../Editor/EditorContext';
|
10
15
|
|
11
|
-
export type
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
export type ImageFormData = {
|
17
|
+
imageType: NodeName;
|
18
|
+
image: Pick<ImageAttributes, 'src' | 'alt' | 'width' | 'height'>;
|
19
|
+
assetImage: AssetImageAttributes;
|
20
|
+
};
|
21
|
+
|
22
|
+
const imageTypeOptions: SelectOptions = {
|
23
|
+
[NodeName.Image]: { label: 'External image' },
|
24
|
+
[NodeName.AssetImage]: { label: 'Asset image' },
|
16
25
|
};
|
17
|
-
export type ImageFormData = Pick<UpdateImageOptions, 'src' | 'alt' | 'width' | 'height'>;
|
18
26
|
|
19
27
|
export type FormProps = {
|
20
|
-
data:
|
28
|
+
data: DeepPartial<ImageFormData>;
|
21
29
|
onSubmit: SubmitHandler<ImageFormData>;
|
22
30
|
};
|
23
|
-
export type Dimensions = 'width' | 'height';
|
31
|
+
export type Dimensions = 'image.width' | 'image.height';
|
24
32
|
|
25
33
|
const regexDataURI =
|
26
34
|
/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*)$/i;
|
@@ -30,24 +38,29 @@ const ImageForm = ({ data, onSubmit }: FormProps): ReactElement => {
|
|
30
38
|
register,
|
31
39
|
handleSubmit,
|
32
40
|
setValue,
|
41
|
+
watch,
|
33
42
|
formState: { errors },
|
34
43
|
} = useForm<ImageFormData>({
|
35
44
|
defaultValues: data,
|
36
45
|
});
|
37
|
-
|
46
|
+
const imageType = watch('imageType') || NodeName.Image;
|
47
|
+
const context = useContext(EditorContext);
|
38
48
|
const [aspectRatioFromWidth, setAspectRatioFromWidth] = useState(9 / 16);
|
39
49
|
const [aspectRatioFromHeight, setAspectRatioFromHeight] = useState(16 / 9);
|
40
50
|
const [aspectRatioLocked, setAspectRatioLocked] = useState(true);
|
41
51
|
|
42
|
-
const setDimensionsFromURL = (e: React.ChangeEvent<HTMLInputElement>) => {
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
52
|
+
const setDimensionsFromURL = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
53
|
+
try {
|
54
|
+
const { width, height } = await getImageSize(e.target.value);
|
55
|
+
|
56
|
+
setValue('image.width', width);
|
57
|
+
setValue('image.height', height);
|
58
|
+
setAspectRatioFromWidth(height / width);
|
59
|
+
setAspectRatioFromHeight(width / height);
|
60
|
+
} catch (error) {
|
61
|
+
// swallow the error for fetching the image size, will occur if the URL does not point to an image.
|
62
|
+
// will be handled by validation when attempting to add the image.
|
63
|
+
}
|
51
64
|
};
|
52
65
|
|
53
66
|
const validateIsNotImage = async (src: string) => {
|
@@ -59,13 +72,13 @@ const ImageForm = ({ data, onSubmit }: FormProps): ReactElement => {
|
|
59
72
|
}
|
60
73
|
};
|
61
74
|
|
62
|
-
const calculateDimensions = () => {
|
75
|
+
const calculateDimensions = (event: Event) => {
|
63
76
|
if (aspectRatioLocked) {
|
64
77
|
const currentTarget = event?.target as HTMLInputElement;
|
65
78
|
const type = currentTarget.name as Dimensions;
|
66
79
|
const currentValue = currentTarget.value as string;
|
67
|
-
const otherValue = type === 'width' ? 'height' : 'width';
|
68
|
-
const aspectRatio = type === 'width' ? aspectRatioFromWidth : aspectRatioFromHeight;
|
80
|
+
const otherValue = type === 'image.width' ? 'image.height' : 'image.width';
|
81
|
+
const aspectRatio = type === 'image.width' ? aspectRatioFromWidth : aspectRatioFromHeight;
|
69
82
|
const newValue = Math.round(aspectRatio * Number(currentValue) * 100) / 100;
|
70
83
|
setValue(otherValue, newValue);
|
71
84
|
}
|
@@ -78,84 +91,120 @@ const ImageForm = ({ data, onSubmit }: FormProps): ReactElement => {
|
|
78
91
|
return (
|
79
92
|
<form className="squiz-fte-form" onSubmit={handleSubmit(onSubmit)}>
|
80
93
|
<div className="squiz-fte-form-group mb-2">
|
81
|
-
<
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
{
|
86
|
-
|
87
|
-
required: 'Source is required',
|
88
|
-
validate: {
|
89
|
-
isValidImage: async (value: string | undefined) => {
|
90
|
-
if (value && regexDataURI.test(value)) {
|
91
|
-
return 'Must not be a data URI';
|
92
|
-
}
|
93
|
-
if (value && (await validateIsNotImage(value))) {
|
94
|
-
return 'Must be a valid image URL';
|
95
|
-
}
|
96
|
-
},
|
97
|
-
},
|
98
|
-
})}
|
99
|
-
/>
|
100
|
-
</div>
|
101
|
-
<div className="squiz-fte-form-group mb-2">
|
102
|
-
<Input
|
103
|
-
label="Alternative description"
|
104
|
-
required
|
105
|
-
error={errors?.alt?.message}
|
106
|
-
{...register('alt', { required: 'Alternative description is required' })}
|
94
|
+
<Select
|
95
|
+
name="imageType"
|
96
|
+
label="Type"
|
97
|
+
value={imageType}
|
98
|
+
options={imageTypeOptions}
|
99
|
+
onChange={(value) => setValue('imageType', value as NodeName)}
|
107
100
|
/>
|
108
101
|
</div>
|
109
|
-
|
110
|
-
|
111
|
-
<
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
102
|
+
{imageType === NodeName.Image && (
|
103
|
+
<>
|
104
|
+
<div className="squiz-fte-form-group mb-2">
|
105
|
+
<Input
|
106
|
+
label="Source"
|
107
|
+
required
|
108
|
+
error={errors?.image?.src?.message}
|
109
|
+
{...register('image.src', {
|
110
|
+
onChange: setDimensionsFromURL,
|
111
|
+
required: 'Source is required',
|
112
|
+
validate: {
|
113
|
+
isValidImage: async (value: string | undefined) => {
|
114
|
+
if (value && regexDataURI.test(value)) {
|
115
|
+
return 'Must not be a data URI';
|
116
|
+
}
|
117
|
+
if (value && (await validateIsNotImage(value))) {
|
118
|
+
return 'Must be a valid image URL';
|
119
|
+
}
|
120
|
+
},
|
124
121
|
},
|
125
|
-
}
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
}
|
122
|
+
})}
|
123
|
+
/>
|
124
|
+
</div>
|
125
|
+
<div className="squiz-fte-form-group mb-2">
|
126
|
+
<Input
|
127
|
+
label="Alternative description"
|
128
|
+
required
|
129
|
+
error={errors?.image?.alt?.message}
|
130
|
+
{...register('image.alt', { required: 'Alternative description is required' })}
|
131
|
+
/>
|
132
|
+
</div>
|
133
|
+
<div className="flex flex-row">
|
134
|
+
<div className="squiz-fte-form-group mb-2">
|
135
|
+
<Input
|
136
|
+
label="Width"
|
137
|
+
type="number"
|
138
|
+
required
|
139
|
+
error={errors?.image?.width?.message}
|
140
|
+
{...register('image.width', {
|
141
|
+
onChange: calculateDimensions,
|
142
|
+
required: 'Width is required',
|
143
|
+
validate: {
|
144
|
+
isValidWidth: (value) => {
|
145
|
+
if (value && !(value > 0)) {
|
146
|
+
return 'Must be higher than 0';
|
147
|
+
}
|
148
|
+
},
|
149
|
+
},
|
150
|
+
})}
|
151
|
+
/>
|
152
|
+
</div>
|
153
|
+
<div className="flex mx-1 mb-2">
|
154
|
+
<Button
|
155
|
+
handleOnClick={toggleAspectRatio}
|
156
|
+
isActive={false}
|
157
|
+
icon={aspectRatioLocked ? <InsertLinkRoundedIcon /> : <LinkOffIcon />}
|
158
|
+
label="Constrain properties"
|
159
|
+
isDisabled={false}
|
160
|
+
className={clsx('my-auto', !errors?.image?.height && !errors?.image?.width && 'mb-0')}
|
161
|
+
/>
|
162
|
+
</div>
|
163
|
+
<div className="squiz-fte-form-group mb-2">
|
164
|
+
<Input
|
165
|
+
label="Height"
|
166
|
+
type="number"
|
167
|
+
required
|
168
|
+
error={errors?.image?.height?.message}
|
169
|
+
{...register('image.height', {
|
170
|
+
onChange: calculateDimensions,
|
171
|
+
required: 'Height is required',
|
172
|
+
validate: {
|
173
|
+
isValidHeight: (value) => {
|
174
|
+
if (value && !(value > 0)) {
|
175
|
+
return 'Must be higher than 0';
|
176
|
+
}
|
177
|
+
},
|
178
|
+
},
|
179
|
+
})}
|
180
|
+
/>
|
181
|
+
</div>
|
182
|
+
</div>
|
183
|
+
</>
|
184
|
+
)}
|
185
|
+
{imageType === NodeName.AssetImage && (
|
186
|
+
<>
|
187
|
+
<div className="squiz-fte-form-group mb-2">
|
188
|
+
<Input
|
189
|
+
label="Asset ID"
|
190
|
+
required
|
191
|
+
error={errors?.assetImage?.matrixAssetId?.message}
|
192
|
+
{...register('assetImage.matrixAssetId', {
|
193
|
+
required: 'Asset ID is required',
|
194
|
+
validate: {
|
195
|
+
isImage: async (assetId) => {
|
196
|
+
const asset = await context.matrix.resolveMatrixAsset(assetId);
|
197
|
+
|
198
|
+
if (asset?.type !== 'image') {
|
199
|
+
return 'Asset ID is invalid or not an image';
|
200
|
+
}
|
201
|
+
},
|
153
202
|
},
|
154
|
-
}
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
203
|
+
})}
|
204
|
+
/>
|
205
|
+
</div>
|
206
|
+
</>
|
207
|
+
)}
|
159
208
|
</form>
|
160
209
|
);
|
161
210
|
};
|