@squiz/formatted-text-editor 1.40.1-alpha.7 → 1.41.0
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/AppContext.tsx +14 -3
- package/demo/resources.json +135 -3
- package/lib/Editor/Editor.js +3 -2
- package/lib/EditorToolbar/Toolbar.js +2 -0
- package/lib/EditorToolbar/Tools/Image/ImageButton.js +1 -1
- package/lib/EditorToolbar/Tools/Lists/ListButtons.d.ts +2 -0
- package/lib/EditorToolbar/Tools/Lists/ListButtons.js +14 -0
- package/lib/EditorToolbar/Tools/Lists/OrderedList/OrderedListButton.d.ts +2 -0
- package/lib/EditorToolbar/Tools/Lists/OrderedList/OrderedListButton.js +22 -0
- package/lib/EditorToolbar/Tools/Lists/UnorderedList/UnorderedListButton.d.ts +2 -0
- package/lib/EditorToolbar/Tools/Lists/UnorderedList/UnorderedListButton.js +22 -0
- package/lib/Extensions/Extensions.d.ts +2 -1
- package/lib/Extensions/Extensions.js +9 -1
- package/lib/Extensions/FetchUrlExtension/FetchUrlExtension.d.ts +14 -0
- package/lib/Extensions/FetchUrlExtension/FetchUrlExtension.js +63 -0
- package/lib/Extensions/ImageExtension/AssetImageExtension.d.ts +1 -0
- package/lib/Extensions/ImageExtension/AssetImageExtension.js +4 -4
- package/lib/Extensions/LinkExtension/AssetLinkExtension.d.ts +2 -0
- package/lib/Extensions/LinkExtension/AssetLinkExtension.js +4 -4
- package/lib/hooks/useFocus.js +24 -5
- package/lib/index.css +13 -0
- package/lib/ui/Fields/MatrixAsset/MatrixAsset.d.ts +1 -0
- package/lib/ui/Fields/MatrixAsset/MatrixAsset.js +1 -0
- package/lib/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.js +3 -0
- package/package.json +4 -4
- package/src/Editor/Editor.spec.tsx +91 -16
- package/src/Editor/Editor.tsx +3 -2
- package/src/EditorToolbar/Toolbar.tsx +2 -0
- package/src/EditorToolbar/Tools/Image/ImageButton.spec.tsx +12 -2
- package/src/EditorToolbar/Tools/Image/ImageButton.tsx +1 -1
- package/src/EditorToolbar/Tools/Link/LinkButton.spec.tsx +14 -2
- package/src/EditorToolbar/Tools/Lists/ListButtons.tsx +14 -0
- package/src/EditorToolbar/Tools/Lists/OrderedList/OrderListButton.spec.tsx +39 -0
- package/src/EditorToolbar/Tools/Lists/OrderedList/OrderedListButton.tsx +30 -0
- package/src/EditorToolbar/Tools/Lists/UnorderedList/UnorderedList.spec.tsx +19 -0
- package/src/EditorToolbar/Tools/Lists/UnorderedList/UnorderedListButton.tsx +30 -0
- package/src/Extensions/Extensions.ts +14 -2
- package/src/Extensions/FetchUrlExtension/FetchUrlExtension.ts +73 -0
- package/src/Extensions/ImageExtension/AssetImageExtension.spec.ts +2 -1
- package/src/Extensions/ImageExtension/AssetImageExtension.ts +5 -5
- package/src/Extensions/LinkExtension/AssetLinkExtension.spec.ts +3 -1
- package/src/Extensions/LinkExtension/AssetLinkExtension.ts +6 -5
- package/src/hooks/useFocus.ts +30 -7
- package/src/ui/Fields/MatrixAsset/MatrixAsset.spec.tsx +1 -0
- package/src/ui/Fields/MatrixAsset/MatrixAsset.tsx +2 -0
- package/src/ui/_typography.scss +16 -0
- package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.spec.ts +191 -0
- package/src/utils/converters/squizNodeToRemirrorNode/squizNodeToRemirrorNode.ts +3 -0
- package/src/utils/getNodeNamesByGroup.spec.ts +11 -1
- package/tests/mockResourceBrowserContext.tsx +2 -2
- package/tests/renderWithContext.tsx +30 -1
- package/tests/renderWithEditor.tsx +18 -13
- package/lib/utils/resolveMatrixAssetUrl.d.ts +0 -1
- package/lib/utils/resolveMatrixAssetUrl.js +0 -10
- package/src/utils/resolveMatrixAssetUrl.spec.ts +0 -26
- package/src/utils/resolveMatrixAssetUrl.ts +0 -7
package/src/hooks/useFocus.ts
CHANGED
@@ -13,14 +13,37 @@ const useFocus = (
|
|
13
13
|
|
14
14
|
const handleFocus = useCallback(() => {
|
15
15
|
setIsVisible(true);
|
16
|
-
}, []);
|
16
|
+
}, [wrapperRef]);
|
17
17
|
|
18
|
-
const handleBlur
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
const handleBlur = useCallback(
|
19
|
+
(event: FocusEvent<HTMLDivElement>) => {
|
20
|
+
// React event bubbling is interesting, it bubbles up the React tree rather than the DOM tree.
|
21
|
+
// The tree deviates when rendering portals (eg. for modals).
|
22
|
+
//
|
23
|
+
// Only hide the toolbar if:
|
24
|
+
// 1. We are blurring a node in the editor **DOM** tree.
|
25
|
+
// 2. We are focusing on something that is not in the editor DOM tree
|
26
|
+
// (elements in the portal won't be in the tree but don't influence the focus state per #1).
|
27
|
+
//
|
28
|
+
// This avoids the scenario where an element in a portal is blurred and another one in the portal focused.
|
29
|
+
// Without this logic the blur and focus handlers are called (in that order). The impact of these handlers being
|
30
|
+
// called is that the "isFocused" state changes inconsistently. This state changing then causes subtle issues.
|
31
|
+
// eg. unable to drill down in resource browser, toolbar appearing/disappearing.
|
32
|
+
//
|
33
|
+
// Ideally we would instead solely seeing if the "relatedTarget" is in the React tree. This isn't easily
|
34
|
+
// identifiable however without reaching into React internals.
|
35
|
+
//
|
36
|
+
// An assumption here is that anything in a portal will only blur to another element that is also in the portal
|
37
|
+
// (and therefore still in our React tree resulting in the element still effectively being focused).
|
38
|
+
const isBlurringEditor = wrapperRef.current?.contains(event.target);
|
39
|
+
const isFocusedInEditor = wrapperRef.current?.contains(event.relatedTarget);
|
40
|
+
|
41
|
+
if (isBlurringEditor && !isFocusedInEditor) {
|
42
|
+
setIsVisible(false);
|
43
|
+
}
|
44
|
+
},
|
45
|
+
[wrapperRef],
|
46
|
+
);
|
24
47
|
|
25
48
|
return { handleFocus, handleBlur, isVisible, wrapperRef };
|
26
49
|
};
|
@@ -5,6 +5,7 @@ import { InputContainer, InputContainerProps } from '../InputContainer/InputCont
|
|
5
5
|
type MatrixAssetValue = {
|
6
6
|
matrixIdentifier?: string;
|
7
7
|
matrixAssetId?: string;
|
8
|
+
url?: string;
|
8
9
|
};
|
9
10
|
|
10
11
|
export type MatrixAssetProps<T extends MatrixAssetValue> = Omit<InputContainerProps, 'children'> & {
|
@@ -45,6 +46,7 @@ export const MatrixAsset = <T extends MatrixAssetValue>({
|
|
45
46
|
...value,
|
46
47
|
matrixIdentifier: reference?.source?.id,
|
47
48
|
matrixAssetId: reference?.resource?.id,
|
49
|
+
url: reference?.resource?.url,
|
48
50
|
} as T,
|
49
51
|
},
|
50
52
|
});
|
package/src/ui/_typography.scss
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
+
// Add any formatting we want the editor to have for the tools we have added - all default styling has been removed by the reset
|
1
2
|
.remirror-editor {
|
2
3
|
a {
|
3
4
|
@apply text-blue-300;
|
4
5
|
text-decoration: underline;
|
5
6
|
}
|
6
7
|
|
8
|
+
p {
|
9
|
+
margin-block-end: 0.8rem;
|
10
|
+
}
|
11
|
+
|
7
12
|
h1 {
|
8
13
|
font-size: 1.625rem;
|
9
14
|
font-weight: 600;
|
@@ -71,4 +76,15 @@
|
|
71
76
|
padding-bottom: 0.75rem;
|
72
77
|
}
|
73
78
|
}
|
79
|
+
|
80
|
+
ul,
|
81
|
+
ol {
|
82
|
+
list-style-type: disc;
|
83
|
+
padding: 0 0 0 2.5rem;
|
84
|
+
margin: 1rem 0;
|
85
|
+
}
|
86
|
+
|
87
|
+
ol {
|
88
|
+
list-style-type: decimal;
|
89
|
+
}
|
74
90
|
}
|
@@ -425,4 +425,195 @@ describe('squizNodeToRemirrorNode', () => {
|
|
425
425
|
});
|
426
426
|
},
|
427
427
|
);
|
428
|
+
|
429
|
+
it('should handle ordered lists', () => {
|
430
|
+
const squizComponentJSON: FormattedText = [
|
431
|
+
{
|
432
|
+
type: 'tag',
|
433
|
+
tag: 'ol',
|
434
|
+
children: [
|
435
|
+
{
|
436
|
+
type: 'tag',
|
437
|
+
tag: 'li',
|
438
|
+
children: [
|
439
|
+
{
|
440
|
+
type: 'tag',
|
441
|
+
tag: 'p',
|
442
|
+
children: [
|
443
|
+
{
|
444
|
+
type: 'text',
|
445
|
+
value: 'ddd',
|
446
|
+
},
|
447
|
+
],
|
448
|
+
},
|
449
|
+
],
|
450
|
+
},
|
451
|
+
],
|
452
|
+
},
|
453
|
+
];
|
454
|
+
|
455
|
+
const expected: RemirrorJSON = {
|
456
|
+
content: [
|
457
|
+
{
|
458
|
+
attrs: { level: undefined, nodeIndent: null, nodeLineHeight: null, nodeTextAlignment: null, style: '' },
|
459
|
+
type: 'orderedList',
|
460
|
+
marks: undefined,
|
461
|
+
text: undefined,
|
462
|
+
content: [
|
463
|
+
{
|
464
|
+
attrs: { level: undefined, nodeIndent: null, nodeLineHeight: null, nodeTextAlignment: null, style: '' },
|
465
|
+
type: 'listItem',
|
466
|
+
marks: undefined,
|
467
|
+
text: undefined,
|
468
|
+
content: [
|
469
|
+
{
|
470
|
+
attrs: {
|
471
|
+
level: undefined,
|
472
|
+
nodeIndent: null,
|
473
|
+
nodeLineHeight: null,
|
474
|
+
nodeTextAlignment: null,
|
475
|
+
style: '',
|
476
|
+
},
|
477
|
+
marks: undefined,
|
478
|
+
text: undefined,
|
479
|
+
type: 'paragraph',
|
480
|
+
content: [
|
481
|
+
{
|
482
|
+
attrs: undefined,
|
483
|
+
content: undefined,
|
484
|
+
marks: undefined,
|
485
|
+
text: 'ddd',
|
486
|
+
type: 'text',
|
487
|
+
},
|
488
|
+
],
|
489
|
+
},
|
490
|
+
],
|
491
|
+
},
|
492
|
+
],
|
493
|
+
},
|
494
|
+
],
|
495
|
+
type: 'doc',
|
496
|
+
};
|
497
|
+
|
498
|
+
const result = squizNodeToRemirrorNode(squizComponentJSON);
|
499
|
+
expect(result).toEqual(expected);
|
500
|
+
});
|
501
|
+
|
502
|
+
it('should handle unordered lists', () => {
|
503
|
+
const squizComponentJSON: FormattedText = [
|
504
|
+
{
|
505
|
+
type: 'tag',
|
506
|
+
tag: 'ul',
|
507
|
+
children: [
|
508
|
+
{
|
509
|
+
type: 'tag',
|
510
|
+
tag: 'li',
|
511
|
+
children: [
|
512
|
+
{
|
513
|
+
type: 'tag',
|
514
|
+
tag: 'p',
|
515
|
+
children: [
|
516
|
+
{
|
517
|
+
type: 'text',
|
518
|
+
value: 'chicken',
|
519
|
+
},
|
520
|
+
],
|
521
|
+
},
|
522
|
+
],
|
523
|
+
},
|
524
|
+
{
|
525
|
+
type: 'tag',
|
526
|
+
tag: 'li',
|
527
|
+
children: [
|
528
|
+
{
|
529
|
+
type: 'tag',
|
530
|
+
tag: 'p',
|
531
|
+
children: [
|
532
|
+
{
|
533
|
+
type: 'text',
|
534
|
+
value: 'egg',
|
535
|
+
},
|
536
|
+
],
|
537
|
+
},
|
538
|
+
],
|
539
|
+
},
|
540
|
+
],
|
541
|
+
},
|
542
|
+
];
|
543
|
+
|
544
|
+
const expected: RemirrorJSON = {
|
545
|
+
content: [
|
546
|
+
{
|
547
|
+
attrs: { level: undefined, nodeIndent: null, nodeLineHeight: null, nodeTextAlignment: null, style: '' },
|
548
|
+
type: 'bulletList',
|
549
|
+
marks: undefined,
|
550
|
+
text: undefined,
|
551
|
+
content: [
|
552
|
+
{
|
553
|
+
attrs: { level: undefined, nodeIndent: null, nodeLineHeight: null, nodeTextAlignment: null, style: '' },
|
554
|
+
type: 'listItem',
|
555
|
+
marks: undefined,
|
556
|
+
text: undefined,
|
557
|
+
content: [
|
558
|
+
{
|
559
|
+
attrs: {
|
560
|
+
level: undefined,
|
561
|
+
nodeIndent: null,
|
562
|
+
nodeLineHeight: null,
|
563
|
+
nodeTextAlignment: null,
|
564
|
+
style: '',
|
565
|
+
},
|
566
|
+
marks: undefined,
|
567
|
+
text: undefined,
|
568
|
+
type: 'paragraph',
|
569
|
+
content: [
|
570
|
+
{
|
571
|
+
attrs: undefined,
|
572
|
+
content: undefined,
|
573
|
+
marks: undefined,
|
574
|
+
text: 'chicken',
|
575
|
+
type: 'text',
|
576
|
+
},
|
577
|
+
],
|
578
|
+
},
|
579
|
+
],
|
580
|
+
},
|
581
|
+
{
|
582
|
+
attrs: { level: undefined, nodeIndent: null, nodeLineHeight: null, nodeTextAlignment: null, style: '' },
|
583
|
+
type: 'listItem',
|
584
|
+
marks: undefined,
|
585
|
+
text: undefined,
|
586
|
+
content: [
|
587
|
+
{
|
588
|
+
attrs: {
|
589
|
+
level: undefined,
|
590
|
+
nodeIndent: null,
|
591
|
+
nodeLineHeight: null,
|
592
|
+
nodeTextAlignment: null,
|
593
|
+
style: '',
|
594
|
+
},
|
595
|
+
marks: undefined,
|
596
|
+
text: undefined,
|
597
|
+
type: 'paragraph',
|
598
|
+
content: [
|
599
|
+
{
|
600
|
+
attrs: undefined,
|
601
|
+
content: undefined,
|
602
|
+
marks: undefined,
|
603
|
+
text: 'egg',
|
604
|
+
type: 'text',
|
605
|
+
},
|
606
|
+
],
|
607
|
+
},
|
608
|
+
],
|
609
|
+
},
|
610
|
+
],
|
611
|
+
},
|
612
|
+
],
|
613
|
+
type: 'doc',
|
614
|
+
};
|
615
|
+
|
616
|
+
const result = squizNodeToRemirrorNode(squizComponentJSON);
|
617
|
+
expect(result).toEqual(expected);
|
618
|
+
});
|
428
619
|
});
|
@@ -15,6 +15,16 @@ describe('getNodeNamesByGroup', () => {
|
|
15
15
|
// Nodes in the first array will be transformed to a paragraph when formatting is cleared.
|
16
16
|
// Nodes in the second array will be left as-is.
|
17
17
|
expect(formattingNodeNames).toEqual(['paragraph', 'heading', 'preformatted']);
|
18
|
-
expect(otherNodeNames).toEqual([
|
18
|
+
expect(otherNodeNames).toEqual([
|
19
|
+
'assetImage',
|
20
|
+
'doc',
|
21
|
+
'text',
|
22
|
+
'codeBlock',
|
23
|
+
'image',
|
24
|
+
'unsupportedNode',
|
25
|
+
'bulletList',
|
26
|
+
'listItem',
|
27
|
+
'orderedList',
|
28
|
+
]);
|
19
29
|
});
|
20
30
|
});
|
@@ -8,7 +8,7 @@ export type MockResourceBrowserContextOptions = DeepPartial<{
|
|
8
8
|
resources: Resource[];
|
9
9
|
}>;
|
10
10
|
|
11
|
-
const mockResource = (resource: DeepPartial<Resource> = {}): Resource =>
|
11
|
+
export const mockResource = (resource: DeepPartial<Resource> = {}): Resource =>
|
12
12
|
({
|
13
13
|
id: 'default-resource',
|
14
14
|
name: 'Default resource',
|
@@ -26,7 +26,7 @@ const mockResource = (resource: DeepPartial<Resource> = {}): Resource =>
|
|
26
26
|
...resource,
|
27
27
|
} as Resource);
|
28
28
|
|
29
|
-
const mockSource = (source: DeepPartial<Source>): Source => ({
|
29
|
+
export const mockSource = (source: DeepPartial<Source> = {}): Source => ({
|
30
30
|
id: 'default-source',
|
31
31
|
name: 'Default source',
|
32
32
|
...source,
|
@@ -4,6 +4,8 @@ import merge from 'deepmerge';
|
|
4
4
|
import { EditorContext } from '../src';
|
5
5
|
import { defaultEditorContext, EditorContextOptions } from '../src/Editor/EditorContext';
|
6
6
|
import { DeepPartial } from '../src/types';
|
7
|
+
import { ResourceBrowserContext } from '@squiz/resource-browser';
|
8
|
+
import { mockSource } from './mockResourceBrowserContext';
|
7
9
|
|
8
10
|
export type ContextRenderOptions = RenderOptions & {
|
9
11
|
context?: DeepPartial<{
|
@@ -13,6 +15,33 @@ export type ContextRenderOptions = RenderOptions & {
|
|
13
15
|
|
14
16
|
export const renderWithContext = (ui: ReactElement | null, options?: ContextRenderOptions): RenderResult => {
|
15
17
|
const editorContext = merge(defaultEditorContext, options?.context?.editor || {}) as EditorContextOptions;
|
18
|
+
const sources = mockSource();
|
19
|
+
const resources = [
|
20
|
+
{
|
21
|
+
id: 'default-resource',
|
22
|
+
name: 'Default resource',
|
23
|
+
url: 'https://default-resource/',
|
24
|
+
urls: [],
|
25
|
+
childCount: 0,
|
26
|
+
type: {
|
27
|
+
code: 'unspecified',
|
28
|
+
name: 'Unspecified',
|
29
|
+
},
|
30
|
+
status: {
|
31
|
+
code: 'live',
|
32
|
+
name: 'Live',
|
33
|
+
},
|
34
|
+
},
|
35
|
+
];
|
36
|
+
const onRequestSources = jest.fn().mockResolvedValue(sources);
|
37
|
+
const onRequestChildren = jest.fn().mockResolvedValue(resources);
|
38
|
+
const onRequestResource = jest.fn(() => Promise.resolve(resources[0]));
|
16
39
|
|
17
|
-
return render(
|
40
|
+
return render(
|
41
|
+
<EditorContext.Provider value={editorContext}>
|
42
|
+
<ResourceBrowserContext.Provider value={{ onRequestSources, onRequestChildren, onRequestResource }}>
|
43
|
+
{ui}
|
44
|
+
</ResourceBrowserContext.Provider>
|
45
|
+
</EditorContext.Provider>,
|
46
|
+
);
|
18
47
|
};
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import React, { ReactElement, useContext, useEffect } from 'react';
|
2
2
|
import { Extension, RemirrorContentType, RemirrorManager } from '@remirror/core';
|
3
3
|
import { CorePreset } from '@remirror/preset-core';
|
4
|
+
import { ResourceBrowserContext } from '@squiz/resource-browser';
|
4
5
|
import { BuiltinPreset } from 'remirror';
|
5
6
|
import { EditorComponent, Remirror, useRemirror } from '@remirror/react';
|
6
7
|
import { RemirrorTestChain } from 'jest-remirror';
|
@@ -8,6 +9,7 @@ import { createExtensions } from '../src/Extensions/Extensions';
|
|
8
9
|
import { EditorContext } from '../src';
|
9
10
|
import { FloatingToolbar } from '../src/EditorToolbar';
|
10
11
|
import { renderWithContext, ContextRenderOptions } from './renderWithContext';
|
12
|
+
import { act } from '@testing-library/react';
|
11
13
|
|
12
14
|
export type EditorRenderOptions = ContextRenderOptions & {
|
13
15
|
content?: RemirrorContentType;
|
@@ -20,7 +22,7 @@ type TestEditorProps = EditorRenderOptions & {
|
|
20
22
|
onReady: (manager: RemirrorManager<Extension>) => void;
|
21
23
|
};
|
22
24
|
|
23
|
-
type EditorRenderResult = {
|
25
|
+
export type EditorRenderResult = {
|
24
26
|
editor: RemirrorTestChain<Extension | CorePreset | BuiltinPreset>;
|
25
27
|
getHtmlContent: () => string | undefined;
|
26
28
|
getJsonContent: () => any;
|
@@ -32,8 +34,9 @@ type EditorRenderResult = {
|
|
32
34
|
|
33
35
|
const TestEditor = ({ children, extensions, content, onReady, editable }: TestEditorProps) => {
|
34
36
|
const context = useContext(EditorContext);
|
37
|
+
const browserContext = useContext(ResourceBrowserContext);
|
35
38
|
const { manager, state, setState } = useRemirror({
|
36
|
-
extensions: () => extensions || createExtensions(context)(),
|
39
|
+
extensions: () => extensions || createExtensions(context, browserContext)(),
|
37
40
|
content: content,
|
38
41
|
selection: 'start',
|
39
42
|
stringHandler: 'html',
|
@@ -95,17 +98,19 @@ export const renderWithEditor = async (
|
|
95
98
|
};
|
96
99
|
let isReady = false;
|
97
100
|
|
98
|
-
const { container } =
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
101
|
+
const { container } = await act(() =>
|
102
|
+
renderWithContext(
|
103
|
+
<TestEditor
|
104
|
+
onReady={(manager) => {
|
105
|
+
result.editor = RemirrorTestChain.create(manager);
|
106
|
+
isReady = true;
|
107
|
+
}}
|
108
|
+
{...options}
|
109
|
+
>
|
110
|
+
{ui}
|
111
|
+
</TestEditor>,
|
112
|
+
options,
|
113
|
+
),
|
109
114
|
);
|
110
115
|
|
111
116
|
if (!isReady) {
|
@@ -1 +0,0 @@
|
|
1
|
-
export declare const resolveMatrixAssetUrl: (id: string, matrixDomain: string) => string;
|
@@ -1,10 +0,0 @@
|
|
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;
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import { resolveMatrixAssetUrl } from './resolveMatrixAssetUrl';
|
2
|
-
|
3
|
-
describe('resolveMatrixAssetUrl', () => {
|
4
|
-
it.each([
|
5
|
-
[
|
6
|
-
'domain with no scheme',
|
7
|
-
'123:hello?.txt',
|
8
|
-
'matrix.labs.squiz.test',
|
9
|
-
'http://matrix.labs.squiz.test/_nocache?a=123%3Ahello%3F.txt',
|
10
|
-
],
|
11
|
-
[
|
12
|
-
'domain with scheme',
|
13
|
-
'123:hello?.txt',
|
14
|
-
'https://matrix.labs.squiz.test',
|
15
|
-
'https://matrix.labs.squiz.test/_nocache?a=123%3Ahello%3F.txt',
|
16
|
-
],
|
17
|
-
[
|
18
|
-
'domain with path',
|
19
|
-
'123:hello?.txt',
|
20
|
-
'https://matrix.labs.squiz.test/site-1',
|
21
|
-
'https://matrix.labs.squiz.test/_nocache?a=123%3Ahello%3F.txt',
|
22
|
-
],
|
23
|
-
])('Resolves to expected URL for %s', (description: string, id: string, matrixDomain: string, expected: string) => {
|
24
|
-
expect(resolveMatrixAssetUrl(id, matrixDomain)).toBe(expected);
|
25
|
-
});
|
26
|
-
});
|
@@ -1,7 +0,0 @@
|
|
1
|
-
export const resolveMatrixAssetUrl = (id: string, matrixDomain: string): string => {
|
2
|
-
if (matrixDomain.indexOf('://') < 0) {
|
3
|
-
matrixDomain = `${window.location.protocol}//${matrixDomain}`;
|
4
|
-
}
|
5
|
-
|
6
|
-
return new URL(`/_nocache?a=${encodeURIComponent(id)}`, matrixDomain).toString();
|
7
|
-
};
|