@contember/react-client 1.1.0-rc.5 → 1.1.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/dist/development/ContemberClient.js +23 -42
- package/dist/development/ContemberClient.js.map +1 -1
- package/dist/development/apiRequest/apiRequestReducer.js.map +1 -1
- package/dist/development/components/RichTextRenderer/ElementRenderer.js +4 -13
- package/dist/development/components/RichTextRenderer/ElementRenderer.js.map +1 -1
- package/dist/development/components/RichTextRenderer/LeafRenderer.js +12 -11
- package/dist/development/components/RichTextRenderer/LeafRenderer.js.map +1 -1
- package/dist/development/components/RichTextRenderer/RenderElementFallback.js +65 -113
- package/dist/development/components/RichTextRenderer/RenderElementFallback.js.map +1 -1
- package/dist/development/components/RichTextRenderer/RenderLeafFallback.js +26 -38
- package/dist/development/components/RichTextRenderer/RenderLeafFallback.js.map +1 -1
- package/dist/development/components/RichTextRenderer/RichTextRenderer.js +54 -34
- package/dist/development/components/RichTextRenderer/RichTextRenderer.js.map +1 -1
- package/dist/development/components/RichTextRenderer/renderChildren.js +22 -28
- package/dist/development/components/RichTextRenderer/renderChildren.js.map +1 -1
- package/dist/development/upload/fileUploadReducer.js +31 -45
- package/dist/development/upload/fileUploadReducer.js.map +1 -1
- package/dist/development/upload/toFileId.js.map +1 -1
- package/dist/development/upload/useFileUpload.js +24 -64
- package/dist/development/upload/useFileUpload.js.map +1 -1
- package/dist/production/ContemberClient.js +2 -20
- package/dist/production/ContemberClient.js.map +1 -1
- package/dist/production/apiRequest/apiRequestReducer.js.map +1 -1
- package/dist/production/components/RichTextRenderer/ElementRenderer.js +3 -11
- package/dist/production/components/RichTextRenderer/ElementRenderer.js.map +1 -1
- package/dist/production/components/RichTextRenderer/LeafRenderer.js +11 -9
- package/dist/production/components/RichTextRenderer/LeafRenderer.js.map +1 -1
- package/dist/production/components/RichTextRenderer/RenderElementFallback.js +34 -89
- package/dist/production/components/RichTextRenderer/RenderElementFallback.js.map +1 -1
- package/dist/production/components/RichTextRenderer/RenderLeafFallback.js +13 -28
- package/dist/production/components/RichTextRenderer/RenderLeafFallback.js.map +1 -1
- package/dist/production/components/RichTextRenderer/RichTextRenderer.js +41 -31
- package/dist/production/components/RichTextRenderer/RichTextRenderer.js.map +1 -1
- package/dist/production/components/RichTextRenderer/renderChildren.js +10 -19
- package/dist/production/components/RichTextRenderer/renderChildren.js.map +1 -1
- package/dist/production/upload/fileUploadReducer.js +31 -45
- package/dist/production/upload/fileUploadReducer.js.map +1 -1
- package/dist/production/upload/toFileId.js.map +1 -1
- package/dist/production/upload/useFileUpload.js +24 -64
- package/dist/production/upload/useFileUpload.js.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/upload/FileUploadAction.d.ts +0 -2
- package/dist/types/upload/FileUploadAction.d.ts.map +1 -1
- package/dist/types/upload/FileUploadMultiTemporalState.d.ts +1 -4
- package/dist/types/upload/FileUploadMultiTemporalState.d.ts.map +1 -1
- package/dist/types/upload/fileUploadReducer.d.ts.map +1 -1
- package/dist/types/upload/toFileId.d.ts.map +1 -1
- package/dist/types/upload/useFileUpload.d.ts +1 -2
- package/dist/types/upload/useFileUpload.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/upload/FileUploadAction.ts +0 -3
- package/src/upload/FileUploadMultiTemporalState.ts +1 -4
- package/src/upload/fileUploadReducer.ts +33 -53
- package/src/upload/toFileId.ts +1 -1
- package/src/upload/useFileUpload.ts +23 -67
- package/LICENSE +0 -186
|
@@ -1,51 +1,36 @@
|
|
|
1
|
-
import { Fragment as Fragment$1 } from "react";
|
|
2
1
|
import { jsx, Fragment, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Fragment as Fragment$1 } from "react";
|
|
3
3
|
function jsxN2br(text) {
|
|
4
4
|
const lines = text.split("\n");
|
|
5
5
|
if (lines.length <= 1) {
|
|
6
6
|
return text;
|
|
7
7
|
}
|
|
8
|
-
return lines.map((line, i) => /* @__PURE__ */ jsxs(Fragment$1, {
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
return lines.map((line, i) => /* @__PURE__ */ jsxs(Fragment$1, { children: [
|
|
9
|
+
!!i && /* @__PURE__ */ jsx("br", {}),
|
|
10
|
+
line
|
|
11
|
+
] }, i));
|
|
11
12
|
}
|
|
12
|
-
function RenderLeafFallback({
|
|
13
|
-
leaf
|
|
14
|
-
}) {
|
|
13
|
+
function RenderLeafFallback({ leaf }) {
|
|
15
14
|
let element = jsxN2br(leaf.text);
|
|
16
15
|
if ("isCode" in leaf && leaf.isCode === true) {
|
|
17
|
-
element = /* @__PURE__ */ jsx("code", {
|
|
18
|
-
children: element
|
|
19
|
-
});
|
|
16
|
+
element = /* @__PURE__ */ jsx("code", { children: element });
|
|
20
17
|
}
|
|
21
18
|
if ("isStruckThrough" in leaf && leaf.isStruckThrough === true) {
|
|
22
|
-
element = /* @__PURE__ */ jsx("s", {
|
|
23
|
-
children: element
|
|
24
|
-
});
|
|
19
|
+
element = /* @__PURE__ */ jsx("s", { children: element });
|
|
25
20
|
}
|
|
26
21
|
if ("isHighlighted" in leaf && leaf.isHighlighted === true) {
|
|
27
|
-
element = /* @__PURE__ */ jsx("em", {
|
|
28
|
-
children: element
|
|
29
|
-
});
|
|
22
|
+
element = /* @__PURE__ */ jsx("em", { children: element });
|
|
30
23
|
}
|
|
31
24
|
if ("isUnderlined" in leaf && leaf.isUnderlined === true) {
|
|
32
|
-
element = /* @__PURE__ */ jsx("u", {
|
|
33
|
-
children: element
|
|
34
|
-
});
|
|
25
|
+
element = /* @__PURE__ */ jsx("u", { children: element });
|
|
35
26
|
}
|
|
36
27
|
if ("isItalic" in leaf && leaf.isItalic === true) {
|
|
37
|
-
element = /* @__PURE__ */ jsx("i", {
|
|
38
|
-
children: element
|
|
39
|
-
});
|
|
28
|
+
element = /* @__PURE__ */ jsx("i", { children: element });
|
|
40
29
|
}
|
|
41
30
|
if ("isBold" in leaf && leaf.isBold === true) {
|
|
42
|
-
element = /* @__PURE__ */ jsx("b", {
|
|
43
|
-
children: element
|
|
44
|
-
});
|
|
31
|
+
element = /* @__PURE__ */ jsx("b", { children: element });
|
|
45
32
|
}
|
|
46
|
-
return /* @__PURE__ */ jsx(Fragment, {
|
|
47
|
-
children: element
|
|
48
|
-
});
|
|
33
|
+
return /* @__PURE__ */ jsx(Fragment, { children: element });
|
|
49
34
|
}
|
|
50
35
|
export {
|
|
51
36
|
RenderLeafFallback
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RenderLeafFallback.js","sources":["../../../../src/components/RichTextRenderer/RenderLeafFallback.tsx"],"sourcesContent":["import { Fragment, ReactNode } from 'react'\nimport type { BuiltinLeaves } from './BuiltinLeaves'\n\nexport interface RenderLeafFallbackProps {\n\tleaf: BuiltinLeaves\n}\n\nfunction jsxN2br(text: string): ReactNode {\n\tconst lines = text.split('\\n')\n\tif (lines.length <= 1) {\n\t\treturn text\n\t}\n\treturn lines.map((line, i) => (\n\t\t<Fragment key={i}>\n\t\t\t{!!i && <br />}\n\t\t\t{line}\n\t\t</Fragment>\n\t))\n}\n\nexport function RenderLeafFallback({ leaf }: RenderLeafFallbackProps) {\n\tlet element = jsxN2br(leaf.text)\n\n\tif ('isCode' in leaf && leaf.isCode === true) {\n\t\telement = <code>{element}</code>\n\t}\n\tif ('isStruckThrough' in leaf && leaf.isStruckThrough === true) {\n\t\telement = <s>{element}</s>\n\t}\n\tif ('isHighlighted' in leaf && leaf.isHighlighted === true) {\n\t\telement = <em>{element}</em>\n\t}\n\tif ('isUnderlined' in leaf && leaf.isUnderlined === true) {\n\t\telement = <u>{element}</u>\n\t}\n\tif ('isItalic' in leaf && leaf.isItalic === true) {\n\t\telement = <i>{element}</i>\n\t}\n\tif ('isBold' in leaf && leaf.isBold === true) {\n\t\telement = <b>{element}</b>\n\t}\n\treturn <>{element}</>\n}\n"],"names":["
|
|
1
|
+
{"version":3,"file":"RenderLeafFallback.js","sources":["../../../../src/components/RichTextRenderer/RenderLeafFallback.tsx"],"sourcesContent":["import { Fragment, ReactNode } from 'react'\nimport type { BuiltinLeaves } from './BuiltinLeaves'\n\nexport interface RenderLeafFallbackProps {\n\tleaf: BuiltinLeaves\n}\n\nfunction jsxN2br(text: string): ReactNode {\n\tconst lines = text.split('\\n')\n\tif (lines.length <= 1) {\n\t\treturn text\n\t}\n\treturn lines.map((line, i) => (\n\t\t<Fragment key={i}>\n\t\t\t{!!i && <br />}\n\t\t\t{line}\n\t\t</Fragment>\n\t))\n}\n\nexport function RenderLeafFallback({ leaf }: RenderLeafFallbackProps) {\n\tlet element = jsxN2br(leaf.text)\n\n\tif ('isCode' in leaf && leaf.isCode === true) {\n\t\telement = <code>{element}</code>\n\t}\n\tif ('isStruckThrough' in leaf && leaf.isStruckThrough === true) {\n\t\telement = <s>{element}</s>\n\t}\n\tif ('isHighlighted' in leaf && leaf.isHighlighted === true) {\n\t\telement = <em>{element}</em>\n\t}\n\tif ('isUnderlined' in leaf && leaf.isUnderlined === true) {\n\t\telement = <u>{element}</u>\n\t}\n\tif ('isItalic' in leaf && leaf.isItalic === true) {\n\t\telement = <i>{element}</i>\n\t}\n\tif ('isBold' in leaf && leaf.isBold === true) {\n\t\telement = <b>{element}</b>\n\t}\n\treturn <>{element}</>\n}\n"],"names":["Fragment"],"mappings":";;AAOA,SAAS,QAAQ,MAAyB;AACnC,QAAA,QAAQ,KAAK,MAAM,IAAI;AACzB,MAAA,MAAM,UAAU,GAAG;AACf,WAAA;AAAA,EACR;AACA,SAAO,MAAM,IAAI,CAAC,MAAM,MACvB,qBAACA,YAAA,EACC,UAAA;AAAA,IAAC,CAAA,CAAC,KAAK,oBAAC,MAAG,CAAA,CAAA;AAAA,IACX;AAAA,EAAA,EAAA,GAFa,CAGf,CACA;AACF;AAEgB,SAAA,mBAAmB,EAAE,QAAiC;AACjE,MAAA,UAAU,QAAQ,KAAK,IAAI;AAE/B,MAAI,YAAY,QAAQ,KAAK,WAAW,MAAM;AACnC,cAAA,oBAAC,UAAM,UAAQ,QAAA,CAAA;AAAA,EAC1B;AACA,MAAI,qBAAqB,QAAQ,KAAK,oBAAoB,MAAM;AACrD,cAAA,oBAAC,OAAG,UAAQ,QAAA,CAAA;AAAA,EACvB;AACA,MAAI,mBAAmB,QAAQ,KAAK,kBAAkB,MAAM;AACjD,cAAA,oBAAC,QAAI,UAAQ,QAAA,CAAA;AAAA,EACxB;AACA,MAAI,kBAAkB,QAAQ,KAAK,iBAAiB,MAAM;AAC/C,cAAA,oBAAC,OAAG,UAAQ,QAAA,CAAA;AAAA,EACvB;AACA,MAAI,cAAc,QAAQ,KAAK,aAAa,MAAM;AACvC,cAAA,oBAAC,OAAG,UAAQ,QAAA,CAAA;AAAA,EACvB;AACA,MAAI,YAAY,QAAQ,KAAK,WAAW,MAAM;AACnC,cAAA,oBAAC,OAAG,UAAQ,QAAA,CAAA;AAAA,EACvB;AACA,yCAAU,UAAQ,QAAA,CAAA;AACnB;"}
|
|
@@ -1,34 +1,32 @@
|
|
|
1
|
+
import { jsx, Fragment } from "react/jsx-runtime";
|
|
1
2
|
import { memo, useMemo, createElement } from "react";
|
|
2
3
|
import { defaultDeserialize } from "./defaultDeserialize.js";
|
|
3
4
|
import { renderChildren } from "./renderChildren.js";
|
|
4
5
|
import { RichTextRendererError } from "./RichTextRendererError.js";
|
|
5
6
|
import { RichTextRenderMetadataContext } from "./RichTextRenderMetadataContext.js";
|
|
6
|
-
import { jsx, Fragment } from "react/jsx-runtime";
|
|
7
7
|
const RichTextRenderer = memo(function RichTextRenderer2({
|
|
8
8
|
deserialize = defaultDeserialize,
|
|
9
9
|
renderElement,
|
|
10
10
|
renderLeaf,
|
|
11
|
-
renderBlock = ({
|
|
12
|
-
children
|
|
13
|
-
}) => /* @__PURE__ */ jsx(Fragment, {
|
|
14
|
-
children
|
|
15
|
-
}),
|
|
11
|
+
renderBlock = ({ children }) => /* @__PURE__ */ jsx(Fragment, { children }),
|
|
16
12
|
attributeNamePrefix,
|
|
17
13
|
...sourceProps
|
|
18
14
|
}) {
|
|
19
15
|
const normalizedSource = useMemo(() => {
|
|
20
16
|
if ("source" in sourceProps && !("blocks" in sourceProps)) {
|
|
21
|
-
return [
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
content: sourceProps.source ? deserialize(sourceProps.source) : {
|
|
20
|
+
formatVersion: 0,
|
|
21
|
+
children: []
|
|
22
|
+
},
|
|
23
|
+
id: void 0,
|
|
24
|
+
referencesField: void 0,
|
|
25
|
+
referenceDiscriminationField: void 0,
|
|
26
|
+
referenceRenderers: {},
|
|
27
|
+
references: void 0
|
|
28
|
+
}
|
|
29
|
+
];
|
|
32
30
|
}
|
|
33
31
|
if (!("source" in sourceProps)) {
|
|
34
32
|
const sourceField = sourceProps.sourceField ?? "source";
|
|
@@ -36,14 +34,18 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
36
34
|
const referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? "type";
|
|
37
35
|
return sourceProps.blocks.map((block) => {
|
|
38
36
|
if (!(sourceField in block)) {
|
|
39
|
-
throw new RichTextRendererError(
|
|
37
|
+
throw new RichTextRendererError(
|
|
38
|
+
`Found a block without a '${sourceField}' field. ` + (sourceProps.sourceField === void 0 ? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.` : `That is what the 'sourceField' prop has been set to, and so either this is a typo, or the data supplied is invalid.`)
|
|
39
|
+
);
|
|
40
40
|
}
|
|
41
41
|
const source = block[sourceField];
|
|
42
42
|
if (typeof source !== "string") {
|
|
43
43
|
throw new RichTextRendererError(`Found a block whose source is not a string.`);
|
|
44
44
|
}
|
|
45
45
|
if (sourceProps.referencesField !== void 0 && !(referencesField in block)) {
|
|
46
|
-
throw new RichTextRendererError(
|
|
46
|
+
throw new RichTextRendererError(
|
|
47
|
+
`The 'referencesField' prop is set to '${referencesField}' but a block without such field has been encountered. Unless this is just a typo, if you do not wish to use references, avoid supplying the 'referencesField' prop.`
|
|
48
|
+
);
|
|
47
49
|
}
|
|
48
50
|
const references = block[referencesField];
|
|
49
51
|
let normalizedReferences = void 0;
|
|
@@ -57,7 +59,9 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
57
59
|
throw new RichTextRendererError(`Found a reference without an id field.`);
|
|
58
60
|
}
|
|
59
61
|
if (!(referenceDiscriminationField in reference)) {
|
|
60
|
-
throw new RichTextRendererError(
|
|
62
|
+
throw new RichTextRendererError(
|
|
63
|
+
`Found a reference without a '${referenceDiscriminationField}' field. ` + (sourceProps.referenceDiscriminationField === void 0 ? `The 'referenceDiscriminationField' prop has not been supplied, and so '${referenceDiscriminationField}' was used as a default.` : `That is what the 'referenceDiscriminationField' prop has been set to, and so either this is a typo, or the data supplied is invalid.`)
|
|
64
|
+
);
|
|
61
65
|
}
|
|
62
66
|
normalizedReferences.set(reference.id, reference);
|
|
63
67
|
}
|
|
@@ -74,8 +78,9 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
74
78
|
}
|
|
75
79
|
throw new RichTextRendererError(`RichTextRenderer: invalid data supplied.`);
|
|
76
80
|
}, [deserialize, sourceProps]);
|
|
77
|
-
return /* @__PURE__ */ jsx(Fragment, {
|
|
78
|
-
|
|
81
|
+
return /* @__PURE__ */ jsx(Fragment, { children: normalizedSource.map((block, i) => /* @__PURE__ */ jsx(
|
|
82
|
+
RichTextRenderMetadataContext.Provider,
|
|
83
|
+
{
|
|
79
84
|
value: {
|
|
80
85
|
formatVersion: block.content.formatVersion,
|
|
81
86
|
referenceRenderers: block.referenceRenderers,
|
|
@@ -83,15 +88,20 @@ const RichTextRenderer = memo(function RichTextRenderer2({
|
|
|
83
88
|
referenceDiscriminationField: block.referenceDiscriminationField,
|
|
84
89
|
references: block.references
|
|
85
90
|
},
|
|
86
|
-
children: createElement(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
children: createElement(
|
|
92
|
+
renderBlock,
|
|
93
|
+
{
|
|
94
|
+
block
|
|
95
|
+
},
|
|
96
|
+
renderChildren(block.content.children, {
|
|
97
|
+
renderLeaf,
|
|
98
|
+
renderElement,
|
|
99
|
+
attributeNamePrefix
|
|
100
|
+
})
|
|
101
|
+
)
|
|
102
|
+
},
|
|
103
|
+
block.id ?? i
|
|
104
|
+
)) });
|
|
95
105
|
});
|
|
96
106
|
export {
|
|
97
107
|
RichTextRenderer
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown, children?: ReactNode }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif (!('source' in sourceProps)) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":["RichTextRenderer"
|
|
1
|
+
{"version":3,"file":"RichTextRenderer.js","sources":["../../../../src/components/RichTextRenderer/RichTextRenderer.tsx"],"sourcesContent":["import { ComponentType, createElement, memo, ReactElement, ReactNode, useMemo } from 'react'\nimport { defaultDeserialize } from './defaultDeserialize'\nimport type { RenderElement } from './ElementRenderer'\nimport type { RenderLeaf } from './LeafRenderer'\nimport type { NormalizedRichTextBlock } from './NormalizedRichTextBlock'\nimport type { ReferenceRenderer } from './ReferenceRenderer'\nimport { renderChildren } from './renderChildren'\nimport type { RichTextBlock } from './RichTextBlock'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\nimport type { RichTextReference } from './RichTextReference'\nimport { RichTextRendererError } from './RichTextRendererError'\nimport { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'\nimport type { RootEditorNode } from './RootEditorNode'\n\nexport interface RichTextRendererFieldProps {\n\tsource: string | null\n}\n\nexport interface RichTextRendererBlockProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\tblocks: RichTextBlock[]\n\treferenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>\n\tsourceField?: string\n\treferencesField?: string\n\treferenceDiscriminationField?: string\n}\n\nexport type RichTextRendererProps<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> = {\n\tdeserialize?: (source: string) => RootEditorNode<CustomElements, CustomLeaves>\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n\trenderBlock?: ComponentType<{ block: unknown, children?: ReactNode }>\n} & (RichTextRendererFieldProps | RichTextRendererBlockProps<CustomElements, CustomLeaves>)\n\nexport const RichTextRenderer = memo(function RichTextRenderer<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>({\n\tdeserialize = defaultDeserialize,\n\trenderElement,\n\trenderLeaf,\n\trenderBlock = ({ children }) => <>{children}</>,\n\tattributeNamePrefix,\n\t...sourceProps\n}: RichTextRendererProps<CustomElements, CustomLeaves>) {\n\tconst normalizedSource = useMemo<NormalizedRichTextBlock<CustomElements, CustomLeaves>[]>(() => {\n\t\tif ('source' in sourceProps && !('blocks' in sourceProps)) {\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\tcontent: sourceProps.source\n\t\t\t\t\t\t? deserialize(sourceProps.source)\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tformatVersion: 0,\n\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t },\n\t\t\t\t\tid: undefined,\n\t\t\t\t\treferencesField: undefined,\n\t\t\t\t\treferenceDiscriminationField: undefined,\n\t\t\t\t\treferenceRenderers: {},\n\t\t\t\t\treferences: undefined,\n\t\t\t\t},\n\t\t\t]\n\t\t}\n\t\tif (!('source' in sourceProps)) {\n\t\t\tconst sourceField = sourceProps.sourceField ?? 'source'\n\t\t\tconst referencesField = sourceProps.referencesField ?? 'references'\n\t\t\tconst referenceDiscriminationField = sourceProps.referenceDiscriminationField ?? 'type'\n\n\t\t\treturn sourceProps.blocks.map(block => {\n\t\t\t\tif (!(sourceField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`Found a block without a '${sourceField}' field. ` +\n\t\t\t\t\t\t\t(sourceProps.sourceField === undefined\n\t\t\t\t\t\t\t\t? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`\n\t\t\t\t\t\t\t\t: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +\n\t\t\t\t\t\t\t\t `or the data supplied is invalid.`),\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst source = block[sourceField]\n\t\t\t\tif (typeof source !== 'string') {\n\t\t\t\t\tthrow new RichTextRendererError(`Found a block whose source is not a string.`)\n\t\t\t\t}\n\n\t\t\t\tif (sourceProps.referencesField !== undefined && !(referencesField in block)) {\n\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +\n\t\t\t\t\t\t\t`has been encountered. Unless this is just a typo, ` +\n\t\t\t\t\t\t\t`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tconst references = block[referencesField]\n\n\t\t\t\tlet normalizedReferences: Map<string, RichTextReference> | undefined = undefined\n\t\t\t\tif (references) {\n\t\t\t\t\tif (typeof Object(references)[Symbol.iterator] !== 'function') {\n\t\t\t\t\t\tthrow new RichTextRendererError(`The set of references must be an iterable!`)\n\t\t\t\t\t}\n\n\t\t\t\t\tnormalizedReferences = new Map()\n\t\t\t\t\tfor (const reference of references as Iterable<RichTextReference>) {\n\t\t\t\t\t\tif (!('id' in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(`Found a reference without an id field.`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (!(referenceDiscriminationField in reference)) {\n\t\t\t\t\t\t\tthrow new RichTextRendererError(\n\t\t\t\t\t\t\t\t`Found a reference without a '${referenceDiscriminationField}' field. ` +\n\t\t\t\t\t\t\t\t\t(sourceProps.referenceDiscriminationField === undefined\n\t\t\t\t\t\t\t\t\t\t? `The 'referenceDiscriminationField' prop has not been supplied, ` +\n\t\t\t\t\t\t\t\t\t\t `and so '${referenceDiscriminationField}' was used as a default.`\n\t\t\t\t\t\t\t\t\t\t: `That is what the 'referenceDiscriminationField' prop has been set to, ` +\n\t\t\t\t\t\t\t\t\t\t `and so either this is a typo, or the data supplied is invalid.`),\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tnormalizedReferences.set(reference.id, reference)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tid: typeof block.id === 'string' ? block.id : undefined,\n\t\t\t\t\tcontent: deserialize(source),\n\t\t\t\t\treferenceDiscriminationField,\n\t\t\t\t\treferenceRenderers: sourceProps.referenceRenderers ?? {},\n\t\t\t\t\treferencesField,\n\t\t\t\t\treferences: normalizedReferences,\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t\tthrow new RichTextRendererError(`RichTextRenderer: invalid data supplied.`)\n\t}, [deserialize, sourceProps])\n\n\treturn (\n\t\t<>\n\t\t\t{normalizedSource.map((block, i) => (\n\t\t\t\t<RichTextRenderMetadataContext.Provider\n\t\t\t\t\tkey={block.id ?? i}\n\t\t\t\t\tvalue={{\n\t\t\t\t\t\tformatVersion: block.content.formatVersion,\n\t\t\t\t\t\treferenceRenderers: block.referenceRenderers,\n\t\t\t\t\t\treferencesField: block.referencesField,\n\t\t\t\t\t\treferenceDiscriminationField: block.referenceDiscriminationField,\n\t\t\t\t\t\treferences: block.references,\n\t\t\t\t\t}}\n\t\t\t\t>\n\t\t\t\t\t{createElement(\n\t\t\t\t\t\trenderBlock, {\n\t\t\t\t\t\t\tblock,\n\t\t\t\t\t\t}, renderChildren(block.content.children, {\n\t\t\t\t\t\t\trenderLeaf,\n\t\t\t\t\t\t\trenderElement,\n\t\t\t\t\t\t\tattributeNamePrefix,\n\t\t\t\t\t\t}),\n\t\t\t\t\t)}\n\t\t\t\t</RichTextRenderMetadataContext.Provider>\n\t\t\t))}\n\t\t</>\n\t)\n}) as <CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never>(\n\tprops: RichTextRendererProps<CustomElements, CustomLeaves>,\n) => ReactElement\n"],"names":["RichTextRenderer"],"mappings":";;;;;;AAyCa,MAAA,mBAAmB,KAAK,SAASA,kBAG5C;AAAA,EACD,cAAc;AAAA,EACd;AAAA,EACA;AAAA,EACA,cAAc,CAAC,EAAE,SAAS,sCAAS,UAAS;AAAA,EAC5C;AAAA,KACG;AACJ,GAAwD;AACjD,QAAA,mBAAmB,QAAiE,M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SAC3C,0EACW,yDACX;AAAA,cAAA;AAAA,YAGN;AACqB,iCAAA,IAAI,UAAU,IAAI,SAAS;AAAA,UACjD;AAAA,QACD;AACO,eAAA;AAAA,UACN,IAAI,OAAO,MAAM,OAAO,WAAW,MAAM,KAAK;AAAA,UAC9C,SAAS,YAAY,MAAM;AAAA,UAC3B;AAAA,UACA,oBAAoB,YAAY,sBAAsB,CAAC;AAAA,UACvD;AAAA,UACA,YAAY;AAAA,QAAA;AAAA,MACb,CACA;AAAA,IACF;AACM,UAAA,IAAI,sBAAsB,0CAA0C;AAAA,EAAA,GACxE,CAAC,aAAa,WAAW,CAAC;AAE7B,SAEG,oBAAA,UAAA,EAAA,UAAA,iBAAiB,IAAI,CAAC,OAAO,MAC7B;AAAA,IAAC,8BAA8B;AAAA,IAA9B;AAAA,MAEA,OAAO;AAAA,QACN,eAAe,MAAM,QAAQ;AAAA,QAC7B,oBAAoB,MAAM;AAAA,QAC1B,iBAAiB,MAAM;AAAA,QACvB,8BAA8B,MAAM;AAAA,QACpC,YAAY,MAAM;AAAA,MACnB;AAAA,MAEC,UAAA;AAAA,QACA;AAAA,QAAa;AAAA,UACZ;AAAA,QACD;AAAA,QAAG,eAAe,MAAM,QAAQ,UAAU;AAAA,UACzC;AAAA,UACA;AAAA,UACA;AAAA,QAAA,CACA;AAAA,MACF;AAAA,IAAA;AAAA,IAjBK,MAAM,MAAM;AAAA,EAmBlB,CAAA,EACF,CAAA;AAEF,CAAC;"}
|
|
@@ -1,28 +1,19 @@
|
|
|
1
|
+
import { jsx, Fragment } from "react/jsx-runtime";
|
|
1
2
|
import { ElementRenderer } from "./ElementRenderer.js";
|
|
2
3
|
import { LeafRenderer } from "./LeafRenderer.js";
|
|
3
|
-
import { jsx, Fragment } from "react/jsx-runtime";
|
|
4
4
|
const renderChildren = (children, options) => {
|
|
5
5
|
if (!Array.isArray(children)) {
|
|
6
6
|
children = [children];
|
|
7
7
|
}
|
|
8
|
-
return /* @__PURE__ */ jsx(Fragment, {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return /* @__PURE__ */ jsx(ElementRenderer, {
|
|
18
|
-
element: child,
|
|
19
|
-
options,
|
|
20
|
-
children: renderChildren(child.children, options)
|
|
21
|
-
}, i);
|
|
22
|
-
}
|
|
23
|
-
return null;
|
|
24
|
-
})
|
|
25
|
-
});
|
|
8
|
+
return /* @__PURE__ */ jsx(Fragment, { children: children.map((child, i) => {
|
|
9
|
+
if ("text" in child) {
|
|
10
|
+
return /* @__PURE__ */ jsx(LeafRenderer, { leaf: child, renderLeaf: options.renderLeaf }, i);
|
|
11
|
+
}
|
|
12
|
+
if ("children" in child) {
|
|
13
|
+
return /* @__PURE__ */ jsx(ElementRenderer, { element: child, options, children: renderChildren(child.children, options) }, i);
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}) });
|
|
26
17
|
};
|
|
27
18
|
export {
|
|
28
19
|
renderChildren
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"renderChildren.js","sources":["../../../../src/components/RichTextRenderer/renderChildren.tsx"],"sourcesContent":["import type { ReactElement } from 'react'\nimport type { BuiltinElements } from './BuiltinElements'\nimport type { BuiltinLeaves } from './BuiltinLeaves'\nimport { ElementRenderer, RenderElement } from './ElementRenderer'\nimport { LeafRenderer, RenderLeaf } from './LeafRenderer'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\n\nexport interface RenderChildrenOptions<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n}\n\nexport const renderChildren = <\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>(\n\tchildren:\n\t\t| CustomElements\n\t\t| BuiltinElements<CustomElements, CustomLeaves>\n\t\t| CustomLeaves\n\t\t| BuiltinLeaves\n\t\t| Array<CustomElements | BuiltinElements<CustomElements, CustomLeaves> | CustomLeaves | BuiltinLeaves>,\n\toptions: RenderChildrenOptions<CustomElements, CustomLeaves>,\n): ReactElement => {\n\tif (!Array.isArray(children)) {\n\t\tchildren = [children]\n\t}\n\treturn (\n\t\t<>\n\t\t\t{children.map((child, i) => {\n\t\t\t\tif ('text' in child) {\n\t\t\t\t\treturn <LeafRenderer<CustomLeaves> key={i} leaf={child} renderLeaf={options.renderLeaf} />\n\t\t\t\t}\n\t\t\t\tif ('children' in child) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<ElementRenderer<CustomElements, CustomLeaves> key={i} element={child} options={options}>\n\t\t\t\t\t\t\t{renderChildren<CustomElements, CustomLeaves>(child.children as typeof children, options)}\n\t\t\t\t\t\t</ElementRenderer>\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn null // Throw?\n\t\t\t})}\n\t\t</>\n\t)\n}\n"],"names":[
|
|
1
|
+
{"version":3,"file":"renderChildren.js","sources":["../../../../src/components/RichTextRenderer/renderChildren.tsx"],"sourcesContent":["import type { ReactElement } from 'react'\nimport type { BuiltinElements } from './BuiltinElements'\nimport type { BuiltinLeaves } from './BuiltinLeaves'\nimport { ElementRenderer, RenderElement } from './ElementRenderer'\nimport { LeafRenderer, RenderLeaf } from './LeafRenderer'\nimport type { RichTextElement } from './RichTextElement'\nimport type { RichTextLeaf } from './RichTextLeaf'\n\nexport interface RenderChildrenOptions<\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n> {\n\trenderElement?: RenderElement<CustomElements, CustomLeaves>\n\trenderLeaf?: RenderLeaf<CustomLeaves>\n\tattributeNamePrefix?: string\n}\n\nexport const renderChildren = <\n\tCustomElements extends RichTextElement = never,\n\tCustomLeaves extends RichTextLeaf = never,\n>(\n\tchildren:\n\t\t| CustomElements\n\t\t| BuiltinElements<CustomElements, CustomLeaves>\n\t\t| CustomLeaves\n\t\t| BuiltinLeaves\n\t\t| Array<CustomElements | BuiltinElements<CustomElements, CustomLeaves> | CustomLeaves | BuiltinLeaves>,\n\toptions: RenderChildrenOptions<CustomElements, CustomLeaves>,\n): ReactElement => {\n\tif (!Array.isArray(children)) {\n\t\tchildren = [children]\n\t}\n\treturn (\n\t\t<>\n\t\t\t{children.map((child, i) => {\n\t\t\t\tif ('text' in child) {\n\t\t\t\t\treturn <LeafRenderer<CustomLeaves> key={i} leaf={child} renderLeaf={options.renderLeaf} />\n\t\t\t\t}\n\t\t\t\tif ('children' in child) {\n\t\t\t\t\treturn (\n\t\t\t\t\t\t<ElementRenderer<CustomElements, CustomLeaves> key={i} element={child} options={options}>\n\t\t\t\t\t\t\t{renderChildren<CustomElements, CustomLeaves>(child.children as typeof children, options)}\n\t\t\t\t\t\t</ElementRenderer>\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\treturn null // Throw?\n\t\t\t})}\n\t\t</>\n\t)\n}\n"],"names":[],"mappings":";;;AAiBa,MAAA,iBAAiB,CAI7B,UAMA,YACkB;AAClB,MAAI,CAAC,MAAM,QAAQ,QAAQ,GAAG;AAC7B,eAAW,CAAC,QAAQ;AAAA,EACrB;AACA,SAEG,oBAAA,UAAA,EAAA,UAAA,SAAS,IAAI,CAAC,OAAO,MAAM;AAC3B,QAAI,UAAU,OAAO;AACpB,iCAAQ,cAAmC,EAAA,MAAM,OAAO,YAAY,QAAQ,cAApC,CAAgD;AAAA,IACzF;AACA,QAAI,cAAc,OAAO;AAEvB,aAAA,oBAAC,iBAAsD,EAAA,SAAS,OAAO,SACrE,yBAA6C,MAAM,UAA6B,OAAO,EAAA,GADrC,CAEpD;AAAA,IAEF;AACO,WAAA;AAAA,EACP,CAAA,EACF,CAAA;AAEF;"}
|
|
@@ -2,34 +2,15 @@ import { FileUploadError } from "@contember/client";
|
|
|
2
2
|
import { toFileId } from "./toFileId.js";
|
|
3
3
|
import { assertNever } from "../utils/assertNever.js";
|
|
4
4
|
const initializeFileUploadState = () => ({
|
|
5
|
-
lastUpdateTime: 0,
|
|
6
5
|
fileIdByFile: /* @__PURE__ */ new WeakMap(),
|
|
7
|
-
|
|
8
|
-
publicState: /* @__PURE__ */ new Map(),
|
|
9
|
-
isLiveStateDirty: false
|
|
6
|
+
state: /* @__PURE__ */ new Map()
|
|
10
7
|
});
|
|
11
8
|
const fileUploadReducer = (previousState, action) => {
|
|
12
|
-
|
|
13
|
-
publicState: previousState.liveState,
|
|
14
|
-
fileIdByFile: previousState.fileIdByFile,
|
|
15
|
-
lastUpdateTime: previousState.lastUpdateTime,
|
|
16
|
-
isLiveStateDirty: false,
|
|
17
|
-
liveState: new Map(previousState.liveState)
|
|
18
|
-
});
|
|
19
|
-
const getNewDirtyState = () => ({
|
|
20
|
-
fileIdByFile: previousState.fileIdByFile,
|
|
21
|
-
liveState: previousState.liveState,
|
|
22
|
-
publicState: previousState.publicState,
|
|
23
|
-
isLiveStateDirty: true,
|
|
24
|
-
lastUpdateTime: Date.now()
|
|
25
|
-
});
|
|
9
|
+
let newState = void 0;
|
|
26
10
|
switch (action.type) {
|
|
27
|
-
case "publishNewestState": {
|
|
28
|
-
return publishNewState();
|
|
29
|
-
}
|
|
30
11
|
case "initialize": {
|
|
31
12
|
for (const [fileId, metadata] of action.files) {
|
|
32
|
-
previousState.
|
|
13
|
+
(newState ?? (newState = new Map(previousState.state))).set(fileId, {
|
|
33
14
|
readyState: "initializing",
|
|
34
15
|
abortController: metadata.abortController,
|
|
35
16
|
previewUrl: metadata.previewUrl,
|
|
@@ -37,16 +18,16 @@ const fileUploadReducer = (previousState, action) => {
|
|
|
37
18
|
});
|
|
38
19
|
previousState.fileIdByFile.set(metadata.file, fileId);
|
|
39
20
|
}
|
|
40
|
-
|
|
21
|
+
break;
|
|
41
22
|
}
|
|
42
23
|
case "startUploading": {
|
|
43
24
|
for (const [fileOrId, metadata] of action.files) {
|
|
44
25
|
const fileId = toFileId(previousState, fileOrId);
|
|
45
|
-
const fileState = previousState.
|
|
26
|
+
const fileState = (newState ?? previousState.state).get(fileId);
|
|
46
27
|
if (fileState === void 0 || fileState.readyState !== "initializing") {
|
|
47
28
|
continue;
|
|
48
29
|
}
|
|
49
|
-
previousState.
|
|
30
|
+
(newState ?? (newState = new Map(previousState.state))).set(fileId, {
|
|
50
31
|
readyState: "uploading",
|
|
51
32
|
abortController: fileState.abortController,
|
|
52
33
|
file: fileState.file,
|
|
@@ -56,16 +37,16 @@ const fileUploadReducer = (previousState, action) => {
|
|
|
56
37
|
uploader: metadata.uploader
|
|
57
38
|
});
|
|
58
39
|
}
|
|
59
|
-
|
|
40
|
+
break;
|
|
60
41
|
}
|
|
61
42
|
case "finishSuccessfully": {
|
|
62
43
|
for (const [fileOrId, result] of action.result) {
|
|
63
44
|
const fileId = toFileId(previousState, fileOrId);
|
|
64
|
-
const previousFileState = previousState.
|
|
45
|
+
const previousFileState = (newState ?? previousState.state).get(fileId);
|
|
65
46
|
if (previousFileState === void 0 || previousFileState.readyState !== "uploading") {
|
|
66
47
|
continue;
|
|
67
48
|
}
|
|
68
|
-
previousState.
|
|
49
|
+
(newState ?? (newState = new Map(previousState.state))).set(fileId, {
|
|
69
50
|
readyState: "success",
|
|
70
51
|
file: previousFileState.file,
|
|
71
52
|
metadata: previousFileState.metadata,
|
|
@@ -74,7 +55,7 @@ const fileUploadReducer = (previousState, action) => {
|
|
|
74
55
|
uploader: previousFileState.uploader
|
|
75
56
|
});
|
|
76
57
|
}
|
|
77
|
-
|
|
58
|
+
break;
|
|
78
59
|
}
|
|
79
60
|
case "finishWithError": {
|
|
80
61
|
for (const errorSpec of action.error) {
|
|
@@ -97,13 +78,13 @@ const fileUploadReducer = (previousState, action) => {
|
|
|
97
78
|
}
|
|
98
79
|
}
|
|
99
80
|
const fileId = toFileId(previousState, fileOrId);
|
|
100
|
-
const previousFileState = previousState.
|
|
81
|
+
const previousFileState = (newState ?? previousState.state).get(fileId);
|
|
101
82
|
if (previousFileState === void 0 || previousFileState.readyState !== "initializing" && previousFileState.readyState !== "uploading") {
|
|
102
83
|
continue;
|
|
103
84
|
}
|
|
104
85
|
const uploader = previousFileState.readyState === "uploading" ? previousFileState.uploader : void 0;
|
|
105
86
|
const metadata = previousFileState.readyState === "uploading" ? previousFileState.metadata : void 0;
|
|
106
|
-
previousState.
|
|
87
|
+
(newState ?? (newState = new Map(previousState.state))).set(fileId, {
|
|
107
88
|
readyState: "error",
|
|
108
89
|
errors: errors.length ? errors : void 0,
|
|
109
90
|
rawError,
|
|
@@ -113,48 +94,53 @@ const fileUploadReducer = (previousState, action) => {
|
|
|
113
94
|
uploader
|
|
114
95
|
});
|
|
115
96
|
}
|
|
116
|
-
|
|
97
|
+
break;
|
|
117
98
|
}
|
|
118
99
|
case "purge": {
|
|
119
|
-
let atLeastOnePurged = false;
|
|
120
100
|
for (const fileOrId of action.files) {
|
|
121
101
|
const fileId = toFileId(previousState, fileOrId);
|
|
122
|
-
const fileState = previousState.
|
|
102
|
+
const fileState = (newState ?? previousState.state).get(fileId);
|
|
123
103
|
if (fileState === void 0) {
|
|
124
104
|
continue;
|
|
125
105
|
}
|
|
126
|
-
|
|
106
|
+
URL.revokeObjectURL(fileState.previewUrl);
|
|
127
107
|
if (fileState.readyState === "initializing" || fileState.readyState === "uploading") {
|
|
128
108
|
fileState.abortController.abort();
|
|
129
109
|
}
|
|
130
|
-
previousState.
|
|
131
|
-
}
|
|
132
|
-
if (atLeastOnePurged) {
|
|
133
|
-
return getNewDirtyState();
|
|
110
|
+
(newState ?? (newState = new Map(previousState.state))).delete(fileId);
|
|
134
111
|
}
|
|
135
|
-
|
|
112
|
+
break;
|
|
136
113
|
}
|
|
137
114
|
case "updateUploadProgress": {
|
|
138
115
|
for (const [fileOrId, { progress }] of action.progress) {
|
|
139
116
|
const fileId = toFileId(previousState, fileOrId);
|
|
140
|
-
const previousFileState = previousState.
|
|
117
|
+
const previousFileState = (newState ?? previousState.state).get(fileId);
|
|
141
118
|
if (progress === void 0 || previousFileState === void 0 || previousFileState.readyState !== "uploading") {
|
|
142
119
|
continue;
|
|
143
120
|
}
|
|
144
|
-
|
|
121
|
+
const roundedProgress = Math.floor(progress);
|
|
122
|
+
if (roundedProgress === previousFileState.progress) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
(newState ?? (newState = new Map(previousState.state))).set(fileId, {
|
|
145
126
|
readyState: "uploading",
|
|
146
127
|
file: previousFileState.file,
|
|
147
128
|
uploader: previousFileState.uploader,
|
|
148
129
|
abortController: previousFileState.abortController,
|
|
149
130
|
previewUrl: previousFileState.previewUrl,
|
|
150
131
|
metadata: previousFileState.metadata,
|
|
151
|
-
progress
|
|
132
|
+
progress: roundedProgress
|
|
152
133
|
});
|
|
153
134
|
}
|
|
154
|
-
|
|
135
|
+
break;
|
|
155
136
|
}
|
|
137
|
+
default:
|
|
138
|
+
assertNever();
|
|
139
|
+
}
|
|
140
|
+
if (newState === void 0) {
|
|
141
|
+
return previousState;
|
|
156
142
|
}
|
|
157
|
-
|
|
143
|
+
return { ...previousState, state: newState };
|
|
158
144
|
};
|
|
159
145
|
export {
|
|
160
146
|
fileUploadReducer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<\n\tResult,\n\tMetadata\n> => ({\n\tlastUpdateTime: 0,\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tliveState: new Map(),\n\tpublicState: new Map(),\n\tisLiveStateDirty: false,\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tconst publishNewState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tpublicState: previousState.liveState,\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tlastUpdateTime: previousState.lastUpdateTime,\n\t\tisLiveStateDirty: false,\n\n\t\t// Immediately make the live state referentially unequal so that this doesn't have to happen during each action\n\t\t// several times between two publishes.\n\t\tliveState: new Map(previousState.liveState),\n\t})\n\tconst getNewDirtyState = (): FileUploadMultiTemporalState<Result, Metadata> => ({\n\t\tfileIdByFile: previousState.fileIdByFile,\n\t\tliveState: previousState.liveState,\n\t\tpublicState: previousState.publicState,\n\t\tisLiveStateDirty: true,\n\t\tlastUpdateTime: Date.now(),\n\t})\n\tswitch (action.type) {\n\t\tcase 'publishNewestState': {\n\t\t\treturn publishNewState()\n\t\t}\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about new files immediate*/\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn publishNewState() /* Making the feedback about started upload immediate*/\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\t// return publishNewState() // Making the feedback about successful upload immediate.\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState() // Bad news can wait.\n\t\t}\n\t\tcase 'purge': {\n\t\t\tlet atLeastOnePurged = false\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = previousState.liveState.get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tatLeastOnePurged = true\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.delete(fileId)\n\t\t\t}\n\t\t\tif (atLeastOnePurged) {\n\t\t\t\treturn getNewDirtyState()\n\t\t\t}\n\t\t\treturn previousState\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = previousState.liveState.get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tpreviousState.liveState.set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tprogress,\n\t\t\t\t})\n\t\t\t}\n\t\t\treturn getNewDirtyState()\n\t\t}\n\t}\n\tassertNever(action)\n}\n"],"names":[],"mappings":";;;AAOO,MAAM,4BAA4B,OAGnC;AAAA,EACL,gBAAgB;AAAA,EAChB,kCAAkB,QAAsB;AAAA,EACxC,+BAAe,IAAI;AAAA,EACnB,iCAAiB,IAAI;AAAA,EACrB,kBAAkB;AACnB;AAEa,MAAA,oBAAoB,CAChC,eACA,WACoD;AACpD,QAAM,kBAAkB,OAAuD;AAAA,IAC9E,aAAa,cAAc;AAAA,IAC3B,cAAc,cAAc;AAAA,IAC5B,gBAAgB,cAAc;AAAA,IAC9B,kBAAkB;AAAA,IAIlB,WAAW,IAAI,IAAI,cAAc,SAAS;AAAA,EAAA;AAE3C,QAAM,mBAAmB,OAAuD;AAAA,IAC/E,cAAc,cAAc;AAAA,IAC5B,WAAW,cAAc;AAAA,IACzB,aAAa,cAAc;AAAA,IAC3B,kBAAkB;AAAA,IAClB,gBAAgB,KAAK,IAAI;AAAA,EAAA;AAE1B,UAAQ,OAAO;AAAA,SACT,sBAAsB;AAC1B,aAAO,gBAAgB;AAAA,IACxB;AAAA,SACK,cAAc;AAClB,iBAAW,CAAC,QAAQ,QAAQ,KAAK,OAAO,OAAO;AAGhC,sBAAA,UAAU,IAAI,QAAQ;AAAA,UACnC,YAAY;AAAA,UACZ,iBAAiB,SAAS;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E,UAAU,KAAK,OAAO,UAAU;AACjD,cAAA,SAAS,SAAS,eAAe,QAAQ;AAC/C,cAAM,oBAAoB,cAAc,UAAU,IAAI,MAAM;AAC5D,YAAI,aAAa,UAAa,sBAAsB,UAAa,kBAAkB,eAAe,aAAa;AAC9G;AAAA,QACD;AACc,sBAAA,UAAU,IAAI,QAAQ;AAAA,UACnC,YAAY;AAAA,UACZ,MAAM,kBAAkB;AAAA,UACxB,UAAU,kBAAkB;AAAA,UAC5B,iBAAiB,kBAAkB;AAAA,UACnC,YAAY,kBAAkB;AAAA,UAC9B,UAAU,kBAAkB;AAAA,UAC5B;AAAA,QAAA,CACA;AAAA,MACF;AACA,aAAO,iBAAiB;AAAA,IACzB;AAAA;AAED,cAAkB;AACnB;"}
|
|
1
|
+
{"version":3,"file":"fileUploadReducer.js","sources":["../../../src/upload/fileUploadReducer.ts"],"sourcesContent":["import { FileUploadError } from '@contember/client'\nimport { assertNever } from '../utils'\nimport type { FileId } from './FileId'\nimport type { FileUploadAction } from './FileUploadAction'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\nimport { toFileId } from './toFileId'\n\nexport const initializeFileUploadState = <Result = unknown, Metadata = undefined>(): FileUploadMultiTemporalState<Result, Metadata> => ({\n\tfileIdByFile: new WeakMap<File, FileId>(),\n\tstate: new Map(),\n})\n\nexport const fileUploadReducer = <Result = unknown, Metadata = undefined>(\n\tpreviousState: FileUploadMultiTemporalState<Result, Metadata>,\n\taction: FileUploadAction<Result, Metadata>,\n): FileUploadMultiTemporalState<Result, Metadata> => {\n\tlet newState: undefined | FileUploadMultiTemporalState<Result, Metadata>['state'] = undefined\n\n\tswitch (action.type) {\n\t\tcase 'initialize': {\n\t\t\tfor (const [fileId, metadata] of action.files) {\n\t\t\t\t// Deliberately allowing starting a new upload with the same id\n\t\t\t\t(newState ??= new Map(previousState.state)).set(fileId, {\n\t\t\t\t\treadyState: 'initializing',\n\t\t\t\t\tabortController: metadata.abortController,\n\t\t\t\t\tpreviewUrl: metadata.previewUrl,\n\t\t\t\t\tfile: metadata.file,\n\t\t\t\t})\n\t\t\t\t// Deliberately mutating\n\t\t\t\tpreviousState.fileIdByFile.set(metadata.file, fileId)\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tcase 'startUploading': {\n\t\t\tfor (const [fileOrId, metadata] of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = (newState ?? previousState.state).get(fileId)\n\n\t\t\t\tif (fileState === undefined || fileState.readyState !== 'initializing') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\n\t\t\t\t(newState ??= new Map(previousState.state)).set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tabortController: fileState.abortController,\n\t\t\t\t\tfile: fileState.file,\n\t\t\t\t\tmetadata: metadata.metadata!, // If the user didn't supply the metadata, then that's on them.\n\t\t\t\t\tpreviewUrl: fileState.previewUrl,\n\t\t\t\t\tprogress: undefined,\n\t\t\t\t\tuploader: metadata.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tcase 'finishSuccessfully': {\n\t\t\tfor (const [fileOrId, result] of action.result) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = (newState ?? previousState.state).get(fileId)\n\t\t\t\tif (previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\t(newState ??= new Map(previousState.state)).set(fileId, {\n\t\t\t\t\treadyState: 'success',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tresult,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tcase 'finishWithError': {\n\t\t\tfor (const errorSpec of action.error) {\n\t\t\t\tlet fileOrId: File | FileId\n\t\t\t\tlet rawError: unknown\n\n\t\t\t\tif (Array.isArray(errorSpec)) {\n\t\t\t\t\t[fileOrId, rawError] = errorSpec\n\t\t\t\t} else {\n\t\t\t\t\tfileOrId = errorSpec\n\t\t\t\t\trawError = undefined\n\t\t\t\t}\n\n\t\t\t\tconst errors: FileUploadError[] = []\n\n\t\t\t\tif (rawError instanceof FileUploadError) {\n\t\t\t\t\terrors.push(rawError)\n\t\t\t\t} else if (Array.isArray(rawError)) {\n\t\t\t\t\tfor (const error of rawError) {\n\t\t\t\t\t\tif (error instanceof FileUploadError) {\n\t\t\t\t\t\t\terrors.push(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = (newState ?? previousState.state).get(fileId)\n\t\t\t\tif (\n\t\t\t\t\tpreviousFileState === undefined ||\n\t\t\t\t\t(previousFileState.readyState !== 'initializing' && previousFileState.readyState !== 'uploading')\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst uploader = previousFileState.readyState === 'uploading' ? previousFileState.uploader : undefined\n\t\t\t\tconst metadata = previousFileState.readyState === 'uploading' ? previousFileState.metadata : undefined\n\t\t\t\t;(newState ??= new Map(previousState.state)).set(fileId, {\n\t\t\t\t\treadyState: 'error',\n\t\t\t\t\terrors: errors.length ? errors : undefined,\n\t\t\t\t\trawError,\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tmetadata,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tuploader,\n\t\t\t\t})\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tcase 'purge': {\n\t\t\tfor (const fileOrId of action.files) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst fileState = (newState ?? previousState.state).get(fileId)\n\n\t\t\t\tif (fileState === undefined) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tURL.revokeObjectURL(fileState.previewUrl)\n\t\t\t\tif (fileState.readyState === 'initializing' || fileState.readyState === 'uploading') {\n\t\t\t\t\tfileState.abortController.abort() // This is a bit naughty… We shouldn't do this from here.\n\t\t\t\t}\n\t\t\t\t(newState ??= new Map(previousState.state)).delete(fileId)\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tcase 'updateUploadProgress': {\n\t\t\tfor (const [fileOrId, { progress }] of action.progress) {\n\t\t\t\tconst fileId = toFileId(previousState, fileOrId)\n\t\t\t\tconst previousFileState = (newState ?? previousState.state).get(fileId)\n\t\t\t\tif (progress === undefined || previousFileState === undefined || previousFileState.readyState !== 'uploading') {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst roundedProgress = Math.floor(progress) // throttling\n\t\t\t\tif (roundedProgress === previousFileState.progress) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\t(newState ??= new Map(previousState.state)).set(fileId, {\n\t\t\t\t\treadyState: 'uploading',\n\t\t\t\t\tfile: previousFileState.file,\n\t\t\t\t\tuploader: previousFileState.uploader,\n\t\t\t\t\tabortController: previousFileState.abortController,\n\t\t\t\t\tpreviewUrl: previousFileState.previewUrl,\n\t\t\t\t\tmetadata: previousFileState.metadata,\n\t\t\t\t\tprogress: roundedProgress,\n\t\t\t\t})\n\t\t\t}\n\t\t\tbreak\n\t\t}\n\t\tdefault:\n\t\t\tassertNever(action)\n\t}\n\tif (newState === undefined) {\n\t\treturn previousState\n\t}\n\treturn { ...previousState, state: newState }\n}\n"],"names":[],"mappings":";;;AAOO,MAAM,4BAA4B,OAA+F;AAAA,EACvI,kCAAkB,QAAsB;AAAA,EACxC,2BAAW,IAAI;AAChB;AAEa,MAAA,oBAAoB,CAChC,eACA,WACoD;AACpD,MAAI,WAAgF;AAEpF,UAAQ,OAAO,MAAM;AAAA,IACpB,KAAK,cAAc;AAClB,iBAAW,CAAC,QAAQ,QAAQ,KAAK,OAAO,OA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,gBAAgB;QAC3B;AACA,SAAC,wBAAa,IAAI,IAAI,cAAc,KAAK,IAAG,OAAO,MAAM;AAAA,MAC1D;AACA;AAAA,IACD;AAAA,IACA,KAAK,wBAAwB;AAC5B,iBAAW,CAAC,UAAU,EAAE,UAAU,KAAK,OAAO,UAAU;AACjD,cAAA,SAAS,SAAS,eAAe,QAAQ;AAC/C,cAAM,qBAAqB,YAAY,cAAc,OAAO,IAAI,MAAM;AACtE,YAAI,aAAa,UAAa,sBAAsB,UAAa,kBAAkB,eAAe,aAAa;AAC9G;AAAA,QACD;AACM,cAAA,kBAAkB,KAAK,MAAM,QAAQ;AACvC,YAAA,oBAAoB,kBAAkB,UAAU;AACnD;AAAA,QACD;AACA,SAAC,wBAAa,IAAI,IAAI,cAAc,KAAK,IAAG,IAAI,QAAQ;AAAA,UACvD,YAAY;AAAA,UACZ,MAAM,kBAAkB;AAAA,UACxB,UAAU,kBAAkB;AAAA,UAC5B,iBAAiB,kBAAkB;AAAA,UACnC,YAAY,kBAAkB;AAAA,UAC9B,UAAU,kBAAkB;AAAA,UAC5B,UAAU;AAAA,QAAA,CACV;AAAA,MACF;AACA;AAAA,IACD;AAAA,IACA;AACC,kBAAkB;AAAA,EACpB;AACA,MAAI,aAAa,QAAW;AACpB,WAAA;AAAA,EACR;AACA,SAAO,EAAE,GAAG,eAAe,OAAO,SAAS;AAC5C;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toFileId.js","sources":["../../../src/upload/toFileId.ts"],"sourcesContent":["import type { FileId } from './FileId'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\n\nexport const toFileId = <Result = unknown, Metadata = undefined>(\n\tstate: FileUploadMultiTemporalState<Result, Metadata>,\n\tfileOrId: File | FileId,\n) => {\n\tif (fileOrId instanceof File) {\n\t\treturn state.fileIdByFile.get(fileOrId)! // Should we throw instead of the yolo \"!\"?\n\t}\n\treturn fileOrId\n}\n"],"names":[],"mappings":"AAGa,MAAA,WAAW,CACvB,OACA,
|
|
1
|
+
{"version":3,"file":"toFileId.js","sources":["../../../src/upload/toFileId.ts"],"sourcesContent":["import type { FileId } from './FileId'\nimport type { FileUploadMultiTemporalState } from './FileUploadMultiTemporalState'\n\nexport const toFileId = <Result = unknown, Metadata = undefined>(\n\tstate: FileUploadMultiTemporalState<Result, Metadata>,\n\tfileOrId: File | FileId,\n): FileId => {\n\tif (fileOrId instanceof File) {\n\t\treturn state.fileIdByFile.get(fileOrId)! // Should we throw instead of the yolo \"!\"?\n\t}\n\treturn fileOrId\n}\n"],"names":[],"mappings":"AAGa,MAAA,WAAW,CACvB,OACA,aACY;AACZ,MAAI,oBAAoB,MAAM;AACtB,WAAA,MAAM,aAAa,IAAI,QAAQ;AAAA,EACvC;AACO,SAAA;AACR;"}
|