@weapp-vite/web 1.3.15 → 1.3.16
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/_virtual/_rolldown/runtime.mjs +18 -0
- package/dist/compiler/wxml/attributes.mjs +93 -0
- package/dist/compiler/wxml/attributes.mjs.map +1 -0
- package/dist/compiler/wxml/compile.mjs +158 -0
- package/dist/compiler/wxml/compile.mjs.map +1 -0
- package/dist/compiler/wxml/dependency.mjs +31 -0
- package/dist/compiler/wxml/dependency.mjs.map +1 -0
- package/dist/compiler/wxml/interpolation.mjs +142 -0
- package/dist/compiler/wxml/interpolation.mjs.map +1 -0
- package/dist/compiler/wxml/navigation.mjs +85 -0
- package/dist/compiler/wxml/navigation.mjs.map +1 -0
- package/dist/compiler/wxml/parser.mjs +41 -0
- package/dist/compiler/wxml/parser.mjs.map +1 -0
- package/dist/compiler/wxml/renderer.mjs +114 -0
- package/dist/compiler/wxml/renderer.mjs.map +1 -0
- package/dist/compiler/wxml/specialNodes.mjs +84 -0
- package/dist/compiler/wxml/specialNodes.mjs.map +1 -0
- package/dist/compiler/wxs.mjs +83 -0
- package/dist/compiler/wxs.mjs.map +1 -0
- package/dist/css/wxss.d.mts +25 -0
- package/dist/css/wxss.mjs +17 -0
- package/dist/css/wxss.mjs.map +1 -0
- package/dist/index.d.mts +18 -2
- package/dist/index.mjs +18 -3
- package/dist/plugin/constants.mjs +43 -0
- package/dist/plugin/constants.mjs.map +1 -0
- package/dist/plugin/entry.mjs +29 -0
- package/dist/plugin/entry.mjs.map +1 -0
- package/dist/plugin/files.mjs +58 -0
- package/dist/plugin/files.mjs.map +1 -0
- package/dist/plugin/index.d.mts +33 -0
- package/dist/plugin/index.mjs +188 -0
- package/dist/plugin/index.mjs.map +1 -0
- package/dist/plugin/navigation.mjs +20 -0
- package/dist/plugin/navigation.mjs.map +1 -0
- package/dist/plugin/path.mjs +85 -0
- package/dist/plugin/path.mjs.map +1 -0
- package/dist/plugin/register.mjs +83 -0
- package/dist/plugin/register.mjs.map +1 -0
- package/dist/plugin/scan.mjs +195 -0
- package/dist/plugin/scan.mjs.map +1 -0
- package/dist/plugin/scanConfig.mjs +45 -0
- package/dist/plugin/scanConfig.mjs.map +1 -0
- package/dist/plugin/state.mjs +22 -0
- package/dist/plugin/state.mjs.map +1 -0
- package/dist/plugin/types.d.mts +46 -0
- package/dist/plugin.d.mts +3 -2
- package/dist/plugin.mjs +3 -2
- package/dist/runtime/button/helpers.mjs +92 -0
- package/dist/runtime/button/helpers.mjs.map +1 -0
- package/dist/runtime/button/index.d.mts +9 -0
- package/dist/runtime/button/index.mjs +198 -0
- package/dist/runtime/button/index.mjs.map +1 -0
- package/dist/runtime/button/style.mjs +168 -0
- package/dist/runtime/button/style.mjs.map +1 -0
- package/dist/runtime/component/behavior.mjs +98 -0
- package/dist/runtime/component/behavior.mjs.map +1 -0
- package/dist/runtime/component/constants.mjs +8 -0
- package/dist/runtime/component/constants.mjs.map +1 -0
- package/dist/runtime/component/dom.mjs +26 -0
- package/dist/runtime/component/dom.mjs.map +1 -0
- package/dist/runtime/component/element.mjs +208 -0
- package/dist/runtime/component/element.mjs.map +1 -0
- package/dist/runtime/component/events.mjs +48 -0
- package/dist/runtime/component/events.mjs.map +1 -0
- package/dist/runtime/component/index.d.mts +7 -0
- package/dist/runtime/component/index.mjs +44 -0
- package/dist/runtime/component/index.mjs.map +1 -0
- package/dist/runtime/component/state.mjs +63 -0
- package/dist/runtime/component/state.mjs.map +1 -0
- package/dist/runtime/component/types.d.mts +49 -0
- package/dist/runtime/component/utils.mjs +39 -0
- package/dist/runtime/component/utils.mjs.map +1 -0
- package/dist/runtime/execution.d.mts +7 -0
- package/dist/runtime/execution.mjs +37 -0
- package/dist/runtime/execution.mjs.map +1 -0
- package/dist/runtime/index.d.mts +16 -2
- package/dist/runtime/index.mjs +16 -2
- package/dist/runtime/legacyTemplate/dom.mjs +131 -0
- package/dist/runtime/legacyTemplate/dom.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/expression.mjs +113 -0
- package/dist/runtime/legacyTemplate/expression.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/index.d.mts +9 -0
- package/dist/runtime/legacyTemplate/index.mjs +131 -0
- package/dist/runtime/legacyTemplate/index.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/types.d.mts +6 -0
- package/dist/runtime/navigationBar/index.d.mts +10 -0
- package/dist/runtime/navigationBar/index.mjs +162 -0
- package/dist/runtime/navigationBar/index.mjs.map +1 -0
- package/dist/runtime/navigationBar/style.mjs +108 -0
- package/dist/runtime/navigationBar/style.mjs.map +1 -0
- package/dist/runtime/polyfill/ad.mjs +150 -0
- package/dist/runtime/polyfill/ad.mjs.map +1 -0
- package/dist/runtime/polyfill/appState.mjs +31 -0
- package/dist/runtime/polyfill/appState.mjs.map +1 -0
- package/dist/runtime/polyfill/async.mjs +31 -0
- package/dist/runtime/polyfill/async.mjs.map +1 -0
- package/dist/runtime/polyfill/auth.mjs +120 -0
- package/dist/runtime/polyfill/auth.mjs.map +1 -0
- package/dist/runtime/polyfill/authApi.mjs +101 -0
- package/dist/runtime/polyfill/authApi.mjs.map +1 -0
- package/dist/runtime/polyfill/background.mjs +31 -0
- package/dist/runtime/polyfill/background.mjs.map +1 -0
- package/dist/runtime/polyfill/canvasContext.mjs +115 -0
- package/dist/runtime/polyfill/canvasContext.mjs.map +1 -0
- package/dist/runtime/polyfill/capability.mjs +17 -0
- package/dist/runtime/polyfill/capability.mjs.map +1 -0
- package/dist/runtime/polyfill/cloud.mjs +39 -0
- package/dist/runtime/polyfill/cloud.mjs.map +1 -0
- package/dist/runtime/polyfill/device.mjs +44 -0
- package/dist/runtime/polyfill/device.mjs.map +1 -0
- package/dist/runtime/polyfill/deviceApi.mjs +31 -0
- package/dist/runtime/polyfill/deviceApi.mjs.map +1 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.d.mts +102 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.mjs +144 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.mjs.map +1 -0
- package/dist/runtime/polyfill/filePicker.mjs +158 -0
- package/dist/runtime/polyfill/filePicker.mjs.map +1 -0
- package/dist/runtime/polyfill/fileSystemManager.mjs +45 -0
- package/dist/runtime/polyfill/fileSystemManager.mjs.map +1 -0
- package/dist/runtime/polyfill/files.mjs +118 -0
- package/dist/runtime/polyfill/files.mjs.map +1 -0
- package/dist/runtime/polyfill/index.d.mts +40 -0
- package/dist/runtime/polyfill/index.mjs +99 -0
- package/dist/runtime/polyfill/index.mjs.map +1 -0
- package/dist/runtime/polyfill/interaction.mjs +38 -0
- package/dist/runtime/polyfill/interaction.mjs.map +1 -0
- package/dist/runtime/polyfill/interactionApi.mjs +54 -0
- package/dist/runtime/polyfill/interactionApi.mjs.map +1 -0
- package/dist/runtime/polyfill/location.mjs +106 -0
- package/dist/runtime/polyfill/location.mjs.map +1 -0
- package/dist/runtime/polyfill/locationApi.mjs +120 -0
- package/dist/runtime/polyfill/locationApi.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaActions.mjs +52 -0
- package/dist/runtime/polyfill/mediaActions.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/file.mjs +63 -0
- package/dist/runtime/polyfill/mediaApi/file.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/info.mjs +59 -0
- package/dist/runtime/polyfill/mediaApi/info.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/picker.mjs +82 -0
- package/dist/runtime/polyfill/mediaApi/picker.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/preview.mjs +39 -0
- package/dist/runtime/polyfill/mediaApi/preview.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/process.mjs +46 -0
- package/dist/runtime/polyfill/mediaApi/process.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaInfo.mjs +136 -0
- package/dist/runtime/polyfill/mediaInfo.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaPicker.mjs +213 -0
- package/dist/runtime/polyfill/mediaPicker.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaProcess.mjs +65 -0
- package/dist/runtime/polyfill/mediaProcess.mjs.map +1 -0
- package/dist/runtime/polyfill/menuApi.mjs +70 -0
- package/dist/runtime/polyfill/menuApi.mjs.map +1 -0
- package/dist/runtime/polyfill/navigationBarRuntime.mjs +65 -0
- package/dist/runtime/polyfill/navigationBarRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/network/request.mjs +162 -0
- package/dist/runtime/polyfill/network/request.mjs.map +1 -0
- package/dist/runtime/polyfill/network/requestBridge.mjs +49 -0
- package/dist/runtime/polyfill/network/requestBridge.mjs.map +1 -0
- package/dist/runtime/polyfill/network/status.mjs +55 -0
- package/dist/runtime/polyfill/network/status.mjs.map +1 -0
- package/dist/runtime/polyfill/platformApi.mjs +33 -0
- package/dist/runtime/polyfill/platformApi.mjs.map +1 -0
- package/dist/runtime/polyfill/platformRuntime.mjs +120 -0
- package/dist/runtime/polyfill/platformRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/dom.mjs +36 -0
- package/dist/runtime/polyfill/routeRuntime/dom.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/lifecycle.mjs +83 -0
- package/dist/runtime/polyfill/routeRuntime/lifecycle.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/options.d.mts +33 -0
- package/dist/runtime/polyfill/routeRuntime/options.mjs +76 -0
- package/dist/runtime/polyfill/routeRuntime/options.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/url.mjs +35 -0
- package/dist/runtime/polyfill/routeRuntime/url.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime.d.mts +43 -0
- package/dist/runtime/polyfill/routeRuntime.mjs +202 -0
- package/dist/runtime/polyfill/routeRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeCapabilityApi.mjs +26 -0
- package/dist/runtime/polyfill/runtimeCapabilityApi.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeDataApi.d.mts +79 -0
- package/dist/runtime/polyfill/runtimeDataApi.mjs +134 -0
- package/dist/runtime/polyfill/runtimeDataApi.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeInfra.mjs +40 -0
- package/dist/runtime/polyfill/runtimeInfra.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeOps.mjs +62 -0
- package/dist/runtime/polyfill/runtimeOps.mjs.map +1 -0
- package/dist/runtime/polyfill/selectorQuery.mjs +204 -0
- package/dist/runtime/polyfill/selectorQuery.mjs.map +1 -0
- package/dist/runtime/polyfill/storage.mjs +103 -0
- package/dist/runtime/polyfill/storage.mjs.map +1 -0
- package/dist/runtime/polyfill/storageAsync.mjs +62 -0
- package/dist/runtime/polyfill/storageAsync.mjs.map +1 -0
- package/dist/runtime/polyfill/subscribe.mjs +30 -0
- package/dist/runtime/polyfill/subscribe.mjs.map +1 -0
- package/dist/runtime/polyfill/system.mjs +111 -0
- package/dist/runtime/polyfill/system.mjs.map +1 -0
- package/dist/runtime/polyfill/systemApi.mjs +65 -0
- package/dist/runtime/polyfill/systemApi.mjs.map +1 -0
- package/dist/runtime/polyfill/types/base.d.mts +12 -0
- package/dist/runtime/polyfill/types/common.d.mts +126 -0
- package/dist/runtime/polyfill/types/locationRuntime.d.mts +151 -0
- package/dist/runtime/polyfill/types/mediaAuth.d.mts +152 -0
- package/dist/runtime/polyfill/types/platformRuntime.d.mts +141 -0
- package/dist/runtime/polyfill/types/systemAuth.d.mts +122 -0
- package/dist/runtime/polyfill/ui.mjs +135 -0
- package/dist/runtime/polyfill/ui.mjs.map +1 -0
- package/dist/runtime/polyfill/uiFeedback.mjs +35 -0
- package/dist/runtime/polyfill/uiFeedback.mjs.map +1 -0
- package/dist/runtime/polyfill/uiMediaApi.d.mts +172 -0
- package/dist/runtime/polyfill/uiMediaApi.mjs +162 -0
- package/dist/runtime/polyfill/uiMediaApi.mjs.map +1 -0
- package/dist/runtime/polyfill/videoContext.mjs +56 -0
- package/dist/runtime/polyfill/videoContext.mjs.map +1 -0
- package/dist/runtime/polyfill/vkSession.mjs +58 -0
- package/dist/runtime/polyfill/vkSession.mjs.map +1 -0
- package/dist/runtime/polyfill/windowResize.mjs +39 -0
- package/dist/runtime/polyfill/windowResize.mjs.map +1 -0
- package/dist/runtime/polyfill/worker.mjs +77 -0
- package/dist/runtime/polyfill/worker.mjs.map +1 -0
- package/dist/runtime/polyfill.d.mts +5 -0
- package/dist/runtime/renderContext.d.mts +21 -0
- package/dist/runtime/renderContext.mjs +147 -0
- package/dist/runtime/renderContext.mjs.map +1 -0
- package/dist/runtime/rpx.d.mts +9 -0
- package/dist/runtime/rpx.mjs +27 -0
- package/dist/runtime/rpx.mjs.map +1 -0
- package/dist/runtime/style.d.mts +6 -0
- package/dist/runtime/style.mjs +37 -0
- package/dist/runtime/style.mjs.map +1 -0
- package/dist/runtime/template.d.mts +10 -0
- package/dist/runtime/warning.d.mts +10 -0
- package/dist/runtime/warning.mjs +43 -0
- package/dist/runtime/warning.mjs.map +1 -0
- package/dist/{slugify-B4l45KNs.mjs → shared/slugify.mjs} +3 -1
- package/dist/shared/slugify.mjs.map +1 -0
- package/dist/shared/wxml.mjs +64 -0
- package/dist/shared/wxml.mjs.map +1 -0
- package/package.json +1 -1
- package/dist/index-w9xxIH-l.d.mts +0 -1229
- package/dist/plugin-BARxSPO8.mjs +0 -1567
- package/dist/plugin-DP2iPVmw.d.mts +0 -96
- package/dist/runtime-D6j2_x_N.mjs +0 -6251
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
//#region src/runtime/polyfill/mediaPicker.ts
|
|
2
|
+
function normalizeChooseImageCount(count) {
|
|
3
|
+
if (typeof count !== "number" || Number.isNaN(count)) return 9;
|
|
4
|
+
return Math.max(1, Math.floor(count));
|
|
5
|
+
}
|
|
6
|
+
function createTempFilePath(file) {
|
|
7
|
+
const runtimeUrl = globalThis.URL;
|
|
8
|
+
if (runtimeUrl && typeof runtimeUrl.createObjectURL === "function") {
|
|
9
|
+
const result = runtimeUrl.createObjectURL(file);
|
|
10
|
+
if (result) return result;
|
|
11
|
+
}
|
|
12
|
+
return file.name ?? "";
|
|
13
|
+
}
|
|
14
|
+
function normalizeChooseImageFile(file) {
|
|
15
|
+
return {
|
|
16
|
+
path: createTempFilePath(file),
|
|
17
|
+
size: typeof file.size === "number" ? file.size : 0,
|
|
18
|
+
type: typeof file.type === "string" ? file.type : "",
|
|
19
|
+
name: typeof file.name === "string" ? file.name : ""
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
async function pickImageFilesByOpenPicker(count) {
|
|
23
|
+
const picker = globalThis.showOpenFilePicker;
|
|
24
|
+
if (typeof picker !== "function") return null;
|
|
25
|
+
const handles = await picker({
|
|
26
|
+
multiple: count > 1,
|
|
27
|
+
types: [{
|
|
28
|
+
description: "Images",
|
|
29
|
+
accept: { "image/*": [
|
|
30
|
+
".png",
|
|
31
|
+
".jpg",
|
|
32
|
+
".jpeg",
|
|
33
|
+
".gif",
|
|
34
|
+
".webp",
|
|
35
|
+
".bmp",
|
|
36
|
+
".svg",
|
|
37
|
+
".avif"
|
|
38
|
+
] }
|
|
39
|
+
}]
|
|
40
|
+
});
|
|
41
|
+
const files = [];
|
|
42
|
+
for (const handle of handles ?? []) {
|
|
43
|
+
const file = await handle?.getFile?.();
|
|
44
|
+
if (file) files.push(file);
|
|
45
|
+
if (files.length >= count) break;
|
|
46
|
+
}
|
|
47
|
+
return files;
|
|
48
|
+
}
|
|
49
|
+
async function pickImageFilesByInput(count) {
|
|
50
|
+
if (typeof document === "undefined" || typeof document.createElement !== "function") return null;
|
|
51
|
+
const input = document.createElement("input");
|
|
52
|
+
if (!input || typeof input !== "object") return null;
|
|
53
|
+
input.setAttribute("type", "file");
|
|
54
|
+
input.setAttribute("accept", "image/*");
|
|
55
|
+
if (count > 1) input.setAttribute("multiple", "true");
|
|
56
|
+
input.setAttribute("style", "position: fixed; left: -9999px; top: -9999px; opacity: 0;");
|
|
57
|
+
if (document.body) document.body.append(input);
|
|
58
|
+
try {
|
|
59
|
+
return await new Promise((resolve, reject) => {
|
|
60
|
+
const onChange = () => {
|
|
61
|
+
const selected = input.files ? Array.from(input.files) : [];
|
|
62
|
+
if (selected.length) {
|
|
63
|
+
resolve(selected.slice(0, count));
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
reject(/* @__PURE__ */ new Error("no file selected"));
|
|
67
|
+
};
|
|
68
|
+
input.addEventListener("change", onChange, { once: true });
|
|
69
|
+
if (typeof input.click === "function") {
|
|
70
|
+
input.click();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
reject(/* @__PURE__ */ new Error("file input click is unavailable"));
|
|
74
|
+
});
|
|
75
|
+
} finally {
|
|
76
|
+
if (input.parentNode) input.parentNode.removeChild(input);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function pickChooseImageFiles(count) {
|
|
80
|
+
const viaPicker = await pickImageFilesByOpenPicker(count);
|
|
81
|
+
if (Array.isArray(viaPicker)) return viaPicker;
|
|
82
|
+
const viaInput = await pickImageFilesByInput(count);
|
|
83
|
+
if (Array.isArray(viaInput)) return viaInput;
|
|
84
|
+
throw new TypeError("Image picker is unavailable in current environment.");
|
|
85
|
+
}
|
|
86
|
+
function normalizeChooseMediaCount(count) {
|
|
87
|
+
if (typeof count !== "number" || Number.isNaN(count)) return 1;
|
|
88
|
+
return Math.max(1, Math.floor(count));
|
|
89
|
+
}
|
|
90
|
+
function normalizeChooseMediaTypes(mediaType) {
|
|
91
|
+
const normalized = /* @__PURE__ */ new Set();
|
|
92
|
+
for (const item of mediaType ?? []) {
|
|
93
|
+
if (item === "image") {
|
|
94
|
+
normalized.add("image");
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (item === "video") {
|
|
98
|
+
normalized.add("video");
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (item === "mix") {
|
|
102
|
+
normalized.add("image");
|
|
103
|
+
normalized.add("video");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (normalized.size === 0) {
|
|
107
|
+
normalized.add("image");
|
|
108
|
+
normalized.add("video");
|
|
109
|
+
}
|
|
110
|
+
return normalized;
|
|
111
|
+
}
|
|
112
|
+
function buildChooseMediaOpenPickerAccept(types) {
|
|
113
|
+
const accept = {};
|
|
114
|
+
if (types.has("image")) accept["image/*"] = [
|
|
115
|
+
".png",
|
|
116
|
+
".jpg",
|
|
117
|
+
".jpeg",
|
|
118
|
+
".gif",
|
|
119
|
+
".webp",
|
|
120
|
+
".bmp",
|
|
121
|
+
".svg",
|
|
122
|
+
".avif"
|
|
123
|
+
];
|
|
124
|
+
if (types.has("video")) accept["video/*"] = [
|
|
125
|
+
".mp4",
|
|
126
|
+
".mov",
|
|
127
|
+
".m4v",
|
|
128
|
+
".webm"
|
|
129
|
+
];
|
|
130
|
+
return accept;
|
|
131
|
+
}
|
|
132
|
+
function buildChooseMediaInputAccept(types) {
|
|
133
|
+
if (types.size === 2) return "image/*,video/*";
|
|
134
|
+
return types.has("video") ? "video/*" : "image/*";
|
|
135
|
+
}
|
|
136
|
+
function inferChooseMediaFileType(file) {
|
|
137
|
+
const mimeType = typeof file.type === "string" ? file.type.toLowerCase() : "";
|
|
138
|
+
if (mimeType.startsWith("video/")) return "video";
|
|
139
|
+
if (mimeType.startsWith("image/")) return "image";
|
|
140
|
+
const fileName = typeof file.name === "string" ? file.name.toLowerCase() : "";
|
|
141
|
+
if (/\.(?:mp4|mov|m4v|webm)$/i.test(fileName)) return "video";
|
|
142
|
+
return "image";
|
|
143
|
+
}
|
|
144
|
+
function normalizeChooseMediaFile(file) {
|
|
145
|
+
const fileType = inferChooseMediaFileType(file);
|
|
146
|
+
return {
|
|
147
|
+
tempFilePath: createTempFilePath(file),
|
|
148
|
+
size: typeof file.size === "number" ? file.size : 0,
|
|
149
|
+
fileType,
|
|
150
|
+
width: 0,
|
|
151
|
+
height: 0,
|
|
152
|
+
duration: 0
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function pickMediaFilesByOpenPicker(count, types) {
|
|
156
|
+
const picker = globalThis.showOpenFilePicker;
|
|
157
|
+
if (typeof picker !== "function") return null;
|
|
158
|
+
const handles = await picker({
|
|
159
|
+
multiple: count > 1,
|
|
160
|
+
types: [{
|
|
161
|
+
description: "Media",
|
|
162
|
+
accept: buildChooseMediaOpenPickerAccept(types)
|
|
163
|
+
}]
|
|
164
|
+
});
|
|
165
|
+
const files = [];
|
|
166
|
+
for (const handle of handles ?? []) {
|
|
167
|
+
const file = await handle?.getFile?.();
|
|
168
|
+
if (file) files.push(file);
|
|
169
|
+
if (files.length >= count) break;
|
|
170
|
+
}
|
|
171
|
+
return files;
|
|
172
|
+
}
|
|
173
|
+
async function pickMediaFilesByInput(count, types) {
|
|
174
|
+
if (typeof document === "undefined" || typeof document.createElement !== "function") return null;
|
|
175
|
+
const input = document.createElement("input");
|
|
176
|
+
if (!input || typeof input !== "object") return null;
|
|
177
|
+
input.setAttribute("type", "file");
|
|
178
|
+
input.setAttribute("accept", buildChooseMediaInputAccept(types));
|
|
179
|
+
if (count > 1) input.setAttribute("multiple", "true");
|
|
180
|
+
input.setAttribute("style", "position: fixed; left: -9999px; top: -9999px; opacity: 0;");
|
|
181
|
+
if (document.body) document.body.append(input);
|
|
182
|
+
try {
|
|
183
|
+
return await new Promise((resolve, reject) => {
|
|
184
|
+
const onChange = () => {
|
|
185
|
+
const selected = input.files ? Array.from(input.files) : [];
|
|
186
|
+
if (selected.length) {
|
|
187
|
+
resolve(selected.slice(0, count));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
reject(/* @__PURE__ */ new Error("no file selected"));
|
|
191
|
+
};
|
|
192
|
+
input.addEventListener("change", onChange, { once: true });
|
|
193
|
+
if (typeof input.click === "function") {
|
|
194
|
+
input.click();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
reject(/* @__PURE__ */ new Error("file input click is unavailable"));
|
|
198
|
+
});
|
|
199
|
+
} finally {
|
|
200
|
+
if (input.parentNode) input.parentNode.removeChild(input);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
async function pickChooseMediaFiles(count, types) {
|
|
204
|
+
const viaPicker = await pickMediaFilesByOpenPicker(count, types);
|
|
205
|
+
if (Array.isArray(viaPicker)) return viaPicker;
|
|
206
|
+
const viaInput = await pickMediaFilesByInput(count, types);
|
|
207
|
+
if (Array.isArray(viaInput)) return viaInput;
|
|
208
|
+
throw new TypeError("Media picker is unavailable in current environment.");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
export { createTempFilePath, inferChooseMediaFileType, normalizeChooseImageCount, normalizeChooseImageFile, normalizeChooseMediaCount, normalizeChooseMediaFile, normalizeChooseMediaTypes, pickChooseImageFiles, pickChooseMediaFiles };
|
|
213
|
+
//# sourceMappingURL=mediaPicker.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediaPicker.mjs","names":[],"sources":["../../../src/runtime/polyfill/mediaPicker.ts"],"sourcesContent":["type ChooseMediaType = 'image' | 'video'\n\ntype RawChooseMediaType = ChooseMediaType | 'mix'\n\nexport function normalizeChooseImageCount(count: number | undefined) {\n if (typeof count !== 'number' || Number.isNaN(count)) {\n return 9\n }\n return Math.max(1, Math.floor(count))\n}\n\nexport function createTempFilePath(file: { name?: string }) {\n const runtimeUrl = (globalThis as {\n URL?: {\n createObjectURL?: (value: unknown) => string\n }\n }).URL\n if (runtimeUrl && typeof runtimeUrl.createObjectURL === 'function') {\n const result = runtimeUrl.createObjectURL(file)\n if (result) {\n return result\n }\n }\n return file.name ?? ''\n}\n\nexport function normalizeChooseImageFile(file: {\n size?: number\n type?: string\n name?: string\n}) {\n return {\n path: createTempFilePath(file),\n size: typeof file.size === 'number' ? file.size : 0,\n type: typeof file.type === 'string' ? file.type : '',\n name: typeof file.name === 'string' ? file.name : '',\n }\n}\n\nasync function pickImageFilesByOpenPicker(count: number) {\n const picker = (globalThis as {\n showOpenFilePicker?: (options: {\n multiple?: boolean\n types?: Array<{\n description?: string\n accept?: Record<string, string[]>\n }>\n }) => Promise<Array<{ getFile?: () => Promise<any> }>>\n }).showOpenFilePicker\n if (typeof picker !== 'function') {\n return null\n }\n const handles = await picker({\n multiple: count > 1,\n types: [{\n description: 'Images',\n accept: {\n 'image/*': ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg', '.avif'],\n },\n }],\n })\n const files: any[] = []\n for (const handle of handles ?? []) {\n const file = await handle?.getFile?.()\n if (file) {\n files.push(file)\n }\n if (files.length >= count) {\n break\n }\n }\n return files\n}\n\nasync function pickImageFilesByInput(count: number) {\n if (typeof document === 'undefined' || typeof document.createElement !== 'function') {\n return null\n }\n const input = document.createElement('input') as HTMLInputElement\n if (!input || typeof input !== 'object') {\n return null\n }\n input.setAttribute('type', 'file')\n input.setAttribute('accept', 'image/*')\n if (count > 1) {\n input.setAttribute('multiple', 'true')\n }\n input.setAttribute('style', 'position: fixed; left: -9999px; top: -9999px; opacity: 0;')\n if (document.body) {\n document.body.append(input)\n }\n try {\n const files = await new Promise<any[]>((resolve, reject) => {\n const onChange = () => {\n const selected = input.files ? Array.from(input.files) : []\n if (selected.length) {\n resolve(selected.slice(0, count))\n return\n }\n reject(new Error('no file selected'))\n }\n input.addEventListener('change', onChange, { once: true })\n if (typeof input.click === 'function') {\n input.click()\n return\n }\n reject(new Error('file input click is unavailable'))\n })\n return files\n }\n finally {\n if (input.parentNode) {\n input.parentNode.removeChild(input)\n }\n }\n}\n\nexport async function pickChooseImageFiles(count: number) {\n const viaPicker = await pickImageFilesByOpenPicker(count)\n if (Array.isArray(viaPicker)) {\n return viaPicker\n }\n const viaInput = await pickImageFilesByInput(count)\n if (Array.isArray(viaInput)) {\n return viaInput\n }\n throw new TypeError('Image picker is unavailable in current environment.')\n}\n\nexport function normalizeChooseMediaCount(count: number | undefined) {\n if (typeof count !== 'number' || Number.isNaN(count)) {\n return 1\n }\n return Math.max(1, Math.floor(count))\n}\n\nexport function normalizeChooseMediaTypes(mediaType: RawChooseMediaType[] | undefined) {\n const normalized = new Set<ChooseMediaType>()\n for (const item of mediaType ?? []) {\n if (item === 'image') {\n normalized.add('image')\n continue\n }\n if (item === 'video') {\n normalized.add('video')\n continue\n }\n if (item === 'mix') {\n normalized.add('image')\n normalized.add('video')\n }\n }\n if (normalized.size === 0) {\n normalized.add('image')\n normalized.add('video')\n }\n return normalized\n}\n\nfunction buildChooseMediaOpenPickerAccept(types: Set<ChooseMediaType>) {\n const accept: Record<string, string[]> = {}\n if (types.has('image')) {\n accept['image/*'] = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg', '.avif']\n }\n if (types.has('video')) {\n accept['video/*'] = ['.mp4', '.mov', '.m4v', '.webm']\n }\n return accept\n}\n\nfunction buildChooseMediaInputAccept(types: Set<ChooseMediaType>) {\n if (types.size === 2) {\n return 'image/*,video/*'\n }\n return types.has('video') ? 'video/*' : 'image/*'\n}\n\nexport function inferChooseMediaFileType(file: { type?: string, name?: string }): ChooseMediaType {\n const mimeType = typeof file.type === 'string' ? file.type.toLowerCase() : ''\n if (mimeType.startsWith('video/')) {\n return 'video'\n }\n if (mimeType.startsWith('image/')) {\n return 'image'\n }\n const fileName = typeof file.name === 'string' ? file.name.toLowerCase() : ''\n if (/\\.(?:mp4|mov|m4v|webm)$/i.test(fileName)) {\n return 'video'\n }\n return 'image'\n}\n\nexport function normalizeChooseMediaFile(file: {\n size?: number\n type?: string\n name?: string\n}) {\n const fileType = inferChooseMediaFileType(file)\n return {\n tempFilePath: createTempFilePath(file),\n size: typeof file.size === 'number' ? file.size : 0,\n fileType,\n width: 0,\n height: 0,\n duration: 0,\n }\n}\n\nasync function pickMediaFilesByOpenPicker(count: number, types: Set<ChooseMediaType>) {\n const picker = (globalThis as {\n showOpenFilePicker?: (options: {\n multiple?: boolean\n types?: Array<{\n description?: string\n accept?: Record<string, string[]>\n }>\n }) => Promise<Array<{ getFile?: () => Promise<any> }>>\n }).showOpenFilePicker\n if (typeof picker !== 'function') {\n return null\n }\n const handles = await picker({\n multiple: count > 1,\n types: [{\n description: 'Media',\n accept: buildChooseMediaOpenPickerAccept(types),\n }],\n })\n const files: any[] = []\n for (const handle of handles ?? []) {\n const file = await handle?.getFile?.()\n if (file) {\n files.push(file)\n }\n if (files.length >= count) {\n break\n }\n }\n return files\n}\n\nasync function pickMediaFilesByInput(count: number, types: Set<ChooseMediaType>) {\n if (typeof document === 'undefined' || typeof document.createElement !== 'function') {\n return null\n }\n const input = document.createElement('input') as HTMLInputElement\n if (!input || typeof input !== 'object') {\n return null\n }\n input.setAttribute('type', 'file')\n input.setAttribute('accept', buildChooseMediaInputAccept(types))\n if (count > 1) {\n input.setAttribute('multiple', 'true')\n }\n input.setAttribute('style', 'position: fixed; left: -9999px; top: -9999px; opacity: 0;')\n if (document.body) {\n document.body.append(input)\n }\n try {\n const files = await new Promise<any[]>((resolve, reject) => {\n const onChange = () => {\n const selected = input.files ? Array.from(input.files) : []\n if (selected.length) {\n resolve(selected.slice(0, count))\n return\n }\n reject(new Error('no file selected'))\n }\n input.addEventListener('change', onChange, { once: true })\n if (typeof input.click === 'function') {\n input.click()\n return\n }\n reject(new Error('file input click is unavailable'))\n })\n return files\n }\n finally {\n if (input.parentNode) {\n input.parentNode.removeChild(input)\n }\n }\n}\n\nexport async function pickChooseMediaFiles(count: number, types: Set<ChooseMediaType>) {\n const viaPicker = await pickMediaFilesByOpenPicker(count, types)\n if (Array.isArray(viaPicker)) {\n return viaPicker\n }\n const viaInput = await pickMediaFilesByInput(count, types)\n if (Array.isArray(viaInput)) {\n return viaInput\n }\n throw new TypeError('Media picker is unavailable in current environment.')\n}\n"],"mappings":";AAIA,SAAgB,0BAA0B,OAA2B;AACnE,KAAI,OAAO,UAAU,YAAY,OAAO,MAAM,MAAM,CAClD,QAAO;AAET,QAAO,KAAK,IAAI,GAAG,KAAK,MAAM,MAAM,CAAC;;AAGvC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ileType, pickChooseMediaFiles } from "./mediaPicker.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/polyfill/mediaProcess.ts
|
|
4
|
+
function normalizeCompressImageQuality(quality) {
|
|
5
|
+
if (typeof quality !== "number" || Number.isNaN(quality)) return 80;
|
|
6
|
+
return Math.max(0, Math.min(100, Math.round(quality)));
|
|
7
|
+
}
|
|
8
|
+
function resolveCompressImageMimeType(src) {
|
|
9
|
+
const lower = src.toLowerCase();
|
|
10
|
+
if (lower.includes(".jpg") || lower.includes(".jpeg")) return "image/jpeg";
|
|
11
|
+
if (lower.includes(".webp")) return "image/webp";
|
|
12
|
+
return "image/png";
|
|
13
|
+
}
|
|
14
|
+
async function compressImageByCanvas(src, quality) {
|
|
15
|
+
if (typeof document === "undefined" || typeof document.createElement !== "function") return src;
|
|
16
|
+
const ImageCtor = globalThis.Image;
|
|
17
|
+
if (typeof ImageCtor !== "function") return src;
|
|
18
|
+
const canvas = document.createElement("canvas");
|
|
19
|
+
if (!canvas || typeof canvas.getContext !== "function") return src;
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const image = new ImageCtor();
|
|
22
|
+
image.onload = () => {
|
|
23
|
+
try {
|
|
24
|
+
const width = Number(image.naturalWidth ?? image.width ?? 0);
|
|
25
|
+
const height = Number(image.naturalHeight ?? image.height ?? 0);
|
|
26
|
+
const context = canvas.getContext("2d");
|
|
27
|
+
if (!context || typeof context.drawImage !== "function" || width <= 0 || height <= 0) {
|
|
28
|
+
resolve(src);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
canvas.width = width;
|
|
32
|
+
canvas.height = height;
|
|
33
|
+
context.drawImage(image, 0, 0, width, height);
|
|
34
|
+
if (typeof canvas.toDataURL !== "function") {
|
|
35
|
+
resolve(src);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
resolve(canvas.toDataURL(resolveCompressImageMimeType(src), quality / 100) || src);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
reject(error);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
image.onerror = () => {
|
|
44
|
+
reject(/* @__PURE__ */ new Error("image load error"));
|
|
45
|
+
};
|
|
46
|
+
image.src = src;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function normalizeChooseVideoFile(file) {
|
|
50
|
+
if (inferChooseMediaFileType(file) !== "video") return null;
|
|
51
|
+
return {
|
|
52
|
+
tempFilePath: createTempFilePath(file),
|
|
53
|
+
duration: 0,
|
|
54
|
+
size: typeof file.size === "number" ? file.size : 0,
|
|
55
|
+
height: 0,
|
|
56
|
+
width: 0
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function pickChooseVideoFile() {
|
|
60
|
+
return (await pickChooseMediaFiles(1, new Set(["video"])))[0] ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { compressImageByCanvas, normalizeChooseVideoFile, normalizeCompressImageQuality, pickChooseVideoFile };
|
|
65
|
+
//# sourceMappingURL=mediaProcess.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediaProcess.mjs","names":[],"sources":["../../../src/runtime/polyfill/mediaProcess.ts"],"sourcesContent":["import {\n createTempFilePath,\n inferChooseMediaFileType,\n pickChooseMediaFiles,\n} from './mediaPicker'\n\nexport function normalizeCompressImageQuality(quality: number | undefined) {\n if (typeof quality !== 'number' || Number.isNaN(quality)) {\n return 80\n }\n return Math.max(0, Math.min(100, Math.round(quality)))\n}\n\nfunction resolveCompressImageMimeType(src: string) {\n const lower = src.toLowerCase()\n if (lower.includes('.jpg') || lower.includes('.jpeg')) {\n return 'image/jpeg'\n }\n if (lower.includes('.webp')) {\n return 'image/webp'\n }\n return 'image/png'\n}\n\nexport async function compressImageByCanvas(src: string, quality: number) {\n if (typeof document === 'undefined' || typeof document.createElement !== 'function') {\n return src\n }\n const ImageCtor = (globalThis as { Image?: typeof Image }).Image\n if (typeof ImageCtor !== 'function') {\n return src\n }\n const canvas = document.createElement('canvas') as HTMLCanvasElement\n if (!canvas || typeof canvas.getContext !== 'function') {\n return src\n }\n return new Promise<string>((resolve, reject) => {\n const image = new ImageCtor()\n image.onload = () => {\n try {\n const width = Number((image as { naturalWidth?: number }).naturalWidth ?? image.width ?? 0)\n const height = Number((image as { naturalHeight?: number }).naturalHeight ?? image.height ?? 0)\n const context = canvas.getContext('2d')\n if (!context || typeof context.drawImage !== 'function' || width <= 0 || height <= 0) {\n resolve(src)\n return\n }\n canvas.width = width\n canvas.height = height\n context.drawImage(image, 0, 0, width, height)\n if (typeof canvas.toDataURL !== 'function') {\n resolve(src)\n return\n }\n const dataUrl = canvas.toDataURL(resolveCompressImageMimeType(src), quality / 100)\n resolve(dataUrl || src)\n }\n catch (error) {\n reject(error)\n }\n }\n image.onerror = () => {\n reject(new Error('image load error'))\n }\n image.src = src\n })\n}\n\nexport function normalizeChooseVideoFile(file: {\n size?: number\n type?: string\n name?: string\n}) {\n if (inferChooseMediaFileType(file) !== 'video') {\n return null\n }\n return {\n tempFilePath: createTempFilePath(file),\n duration: 0,\n size: typeof file.size === 'number' ? file.size : 0,\n height: 0,\n width: 0,\n }\n}\n\nexport async function pickChooseVideoFile() {\n const files = await pickChooseMediaFiles(1, new Set(['video']))\n return files[0] ?? null\n}\n"],"mappings":";;;AAMA,SAAgB,8BAA8B,SAA6B;AACzE,KAAI,OAAO,YAAY,YAAY,OAAO,MAAM,QAAQ,CACtD,QAAO;AAET,QAAO,KAAK,IAAI,GAAG,KAAK,IAAI,KAAK,KAAK,MAAM,QAAQ,CAAC,CAAC;;AAGxD,SAAS,6BAA6B,KAAa;CACjD,MAAM,QAAQ,IAAI,aAAa;AAC/B,KAAI,MAAM,SAAS,OAAO,IAAI,MAAM,SAAS,QAAQ,CACnD,QAAO;AAET,KAAI,MAAM,SAAS,QAAQ,CACzB,QAAO;AAET,QAAO;;AAGT,eAAsB,sBAAsB,KAAa,SAAiB;AACxE,KAAI,OAAO,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|
+
import { callWxAsyncFailure, callWxAsyncSuccess } from "./async.mjs";
|
|
2
|
+
import { normalizeActionSheetItems, resolveActionSheetSelection, resolveModalSelection } from "./ui.mjs";
|
|
3
|
+
import { readNetworkStatusSnapshot } from "./network/status.mjs";
|
|
4
|
+
import { normalizeSubscribeTemplateIds, resolveSubscribeDecisionMap } from "./subscribe.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime/polyfill/menuApi.ts
|
|
7
|
+
function getNetworkTypeBridge(options) {
|
|
8
|
+
const status = readNetworkStatusSnapshot();
|
|
9
|
+
return Promise.resolve(callWxAsyncSuccess(options, {
|
|
10
|
+
errMsg: "getNetworkType:ok",
|
|
11
|
+
...status
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
function showTabBarBridge(options) {
|
|
15
|
+
return Promise.resolve(callWxAsyncSuccess(options, { errMsg: "showTabBar:ok" }));
|
|
16
|
+
}
|
|
17
|
+
function hideTabBarBridge(options) {
|
|
18
|
+
return Promise.resolve(callWxAsyncSuccess(options, { errMsg: "hideTabBar:ok" }));
|
|
19
|
+
}
|
|
20
|
+
function requestPaymentBridge(options) {
|
|
21
|
+
return Promise.resolve(callWxAsyncSuccess(options, { errMsg: "requestPayment:ok" }));
|
|
22
|
+
}
|
|
23
|
+
function requestSubscribeMessageBridge(options) {
|
|
24
|
+
const tmplIds = normalizeSubscribeTemplateIds(options?.tmplIds);
|
|
25
|
+
if (tmplIds.length === 0) {
|
|
26
|
+
const failure = callWxAsyncFailure(options, "requestSubscribeMessage:fail invalid tmplIds");
|
|
27
|
+
return Promise.reject(failure);
|
|
28
|
+
}
|
|
29
|
+
const decisionMap = resolveSubscribeDecisionMap(tmplIds);
|
|
30
|
+
const result = tmplIds.reduce((payload, tmplId) => {
|
|
31
|
+
payload[tmplId] = decisionMap[tmplId];
|
|
32
|
+
return payload;
|
|
33
|
+
}, { errMsg: "requestSubscribeMessage:ok" });
|
|
34
|
+
return Promise.resolve(callWxAsyncSuccess(options, result));
|
|
35
|
+
}
|
|
36
|
+
function showModalBridge(options) {
|
|
37
|
+
const modalResult = resolveModalSelection(options);
|
|
38
|
+
const result = {
|
|
39
|
+
errMsg: "showModal:ok",
|
|
40
|
+
confirm: modalResult.confirm,
|
|
41
|
+
cancel: modalResult.cancel
|
|
42
|
+
};
|
|
43
|
+
return Promise.resolve(callWxAsyncSuccess(options, result));
|
|
44
|
+
}
|
|
45
|
+
function showActionSheetBridge(options) {
|
|
46
|
+
const itemList = normalizeActionSheetItems(options?.itemList);
|
|
47
|
+
if (!itemList.length) {
|
|
48
|
+
const failure = callWxAsyncFailure(options, "showActionSheet:fail invalid itemList");
|
|
49
|
+
return Promise.reject(failure);
|
|
50
|
+
}
|
|
51
|
+
const tapIndex = resolveActionSheetSelection(itemList);
|
|
52
|
+
if (tapIndex === null) {
|
|
53
|
+
const failure = callWxAsyncFailure(options, "showActionSheet:fail cancel");
|
|
54
|
+
return Promise.reject(failure);
|
|
55
|
+
}
|
|
56
|
+
return Promise.resolve(callWxAsyncSuccess(options, {
|
|
57
|
+
errMsg: "showActionSheet:ok",
|
|
58
|
+
tapIndex
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
function showShareMenuBridge(options) {
|
|
62
|
+
return Promise.resolve(callWxAsyncSuccess(options, { errMsg: "showShareMenu:ok" }));
|
|
63
|
+
}
|
|
64
|
+
function updateShareMenuBridge(options) {
|
|
65
|
+
return Promise.resolve(callWxAsyncSuccess(options, { errMsg: "updateShareMenu:ok" }));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { getNetworkTypeBridge, hideTabBarBridge, requestPaymentBridge, requestSubscribeMessageBridge, showActionSheetBridge, showModalBridge, showShareMenuBridge, showTabBarBridge, updateShareMenuBridge };
|
|
70
|
+
//# sourceMappingURL=menuApi.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"menuApi.mjs","names":[],"sources":["../../../src/runtime/polyfill/menuApi.ts"],"sourcesContent":["import {\n callWxAsyncFailure,\n callWxAsyncSuccess,\n} from './async'\nimport { readNetworkStatusSnapshot } from './network'\nimport {\n normalizeSubscribeTemplateIds,\n resolveSubscribeDecisionMap,\n} from './subscribe'\nimport {\n normalizeActionSheetItems,\n resolveActionSheetSelection,\n resolveModalSelection,\n} from './ui'\n\nexport function getNetworkTypeBridge(options?: any): Promise<any> {\n const status = readNetworkStatusSnapshot()\n return Promise.resolve(callWxAsyncSuccess(options, {\n errMsg: 'getNetworkType:ok',\n ...status,\n }))\n}\n\nexport function showTabBarBridge(options?: any): Promise<any> {\n return Promise.resolve(callWxAsyncSuccess(options, { errMsg: 'showTabBar:ok' }))\n}\n\nexport function hideTabBarBridge(options?: any): Promise<any> {\n return Promise.resolve(callWxAsyncSuccess(options, { errMsg: 'hideTabBar:ok' }))\n}\n\nexport function requestPaymentBridge(options?: any): Promise<any> {\n return Promise.resolve(callWxAsyncSuccess(options, { errMsg: 'requestPayment:ok' }))\n}\n\nexport function requestSubscribeMessageBridge(options?: any): Promise<any> {\n const tmplIds = normalizeSubscribeTemplateIds(options?.tmplIds)\n if (tmplIds.length === 0) {\n const failure = callWxAsyncFailure(options, 'requestSubscribeMessage:fail invalid tmplIds')\n return Promise.reject(failure)\n }\n const decisionMap = resolveSubscribeDecisionMap(tmplIds)\n const result: Record<string, any> & { errMsg: string } = tmplIds.reduce<Record<string, any> & { errMsg: string }>((payload, tmplId) => {\n payload[tmplId] = decisionMap[tmplId]\n return payload\n }, { errMsg: 'requestSubscribeMessage:ok' })\n return Promise.resolve(callWxAsyncSuccess(options, result))\n}\n\nexport function showModalBridge(options?: any): Promise<any> {\n const modalResult = resolveModalSelection(options)\n const result = {\n errMsg: 'showModal:ok',\n confirm: modalResult.confirm,\n cancel: modalResult.cancel,\n }\n return Promise.resolve(callWxAsyncSuccess(options, result))\n}\n\nexport function showActionSheetBridge(options?: any): Promise<any> {\n const itemList = normalizeActionSheetItems(options?.itemList)\n if (!itemList.length) {\n const failure = callWxAsyncFailure(options, 'showActionSheet:fail invalid itemList')\n return Promise.reject(failure)\n }\n const tapIndex = resolveActionSheetSelection(itemList)\n if (tapIndex === null) {\n const failure = callWxAsyncFailure(options, 'showActionSheet:fail cancel')\n return Promise.reject(failure)\n }\n return Promise.resolve(callWxAsyncSuccess(options, {\n errMsg: 'showActionSheet:ok',\n tapIndex,\n }))\n}\n\nexport function showShareMenuBridge(options?: any): Promise<any> {\n return Promise.resolve(callWxAsyncSuccess(options, { errMsg: 'showShareMenu:ok' }))\n}\n\nexport function updateShareMenuBridge(options?: any): Promise<any> {\n return Promise.resolve(callWxAsyncSuccess(options, { errMsg: 'updateShareMenu:ok' }))\n}\n"],"mappings":";;;;;;AAeA,SAAgB,qBAAqB,SAA6B;CAChE,MAAM,SAAS,2BAA2B;AAC1C,QAAO,QAAQ,QAAQ,mBAAmB,SAAS;EACjD,QAAQ;EACR,GAAG;EACJ,CAAC,CAAC;;AAGL,SAAgB,iBAAiB,SAA6B;AAC5D,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,EAAE,QAAQ,iBAAiB,CAAC,CAAC;;AAGlF,SAAgB,iBAAiB,SAA6B;AAC5D,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,EAAE,QAAQ,iBAAiB,CAAC,CAAC;;AAGlF,SAAgB,qBAAqB,SAA6B;AAChE,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,EAAE,QAAQ,qBAAqB,CAAC,CAAC;;AAGtF,SAAgB,8BAA8B,SAA6B;CACzE,MAAM,UAAU,8BAA8B,SAAS,QAAQ;AAC/D,KAAI,QAAQ,WAAW,GAAG;EACxB,MAAM,UAAU,mBAAmB,SAAS,+CAA+C;AAC3F,SAAO,QAAQ,OAAO,QAAQ;;CAEhC,MAAM,cAAc,4BAA4B,QAAQ;CACxD,MAAM,SAAmD,QAAQ,QAAkD,SAAS,WAAW;AACrI,UAAQ,UAAU,YAAY;AAC9B,SAAO;IACN,EAAE,QAAQ,8BAA8B,CAAC;AAC5C,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,OAAO,CAAC;;AAG7D,SAAgB,gBAAgB,SAA6B;CAC3D,MAAM,cAAc,sBAAsB,QAAQ;CAClD,MAAM,SAAS;EACb,QAAQ;EACR,SAAS,YAAY;EACrB,QAAQ,YAAY;EACrB;AACD,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,OAAO,CAAC;;AAG7D,SAAgB,sBAAsB,SAA6B;CACjE,MAAM,WAAW,0BAA0B,SAAS,SAAS;AAC7D,KAAI,CAAC,SAAS,QAAQ;EACpB,MAAM,UAAU,mBAAmB,SAAS,wCAAwC;AACpF,SAAO,QAAQ,OAAO,QAAQ;;CAEhC,MAAM,WAAW,4BAA4B,SAAS;AACtD,KAAI,aAAa,MAAM;EACrB,MAAM,UAAU,mBAAmB,SAAS,8BAA8B;AAC1E,SAAO,QAAQ,OAAO,QAAQ;;AAEhC,QAAO,QAAQ,QAAQ,mBAAmB,SAAS;EACjD,QAAQ;EACR;EACD,CAAC,CAAC;;AAGL,SAAgB,oBAAoB,SAA6B;AAC/D,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,EAAE,QAAQ,oBAAoB,CAAC,CAAC;;AAGrF,SAAgB,sBAAsB,SAA6B;AACjE,QAAO,QAAQ,QAAQ,mBAAmB,SAAS,EAAE,QAAQ,sBAAsB,CAAC,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//#region src/runtime/polyfill/navigationBarRuntime.ts
|
|
2
|
+
function getActiveNavigationBar(pages) {
|
|
3
|
+
const current = pages[pages.length - 1];
|
|
4
|
+
if (!current) return;
|
|
5
|
+
const renderRoot = current.renderRoot ?? current.shadowRoot ?? current;
|
|
6
|
+
if (!renderRoot || typeof renderRoot.querySelector !== "function") return;
|
|
7
|
+
return renderRoot.querySelector("weapp-navigation-bar");
|
|
8
|
+
}
|
|
9
|
+
function warnNavigationBarMissing(emitWarning, action) {
|
|
10
|
+
emitWarning(`[@weapp-vite/web] ${action} 需要默认导航栏支持,但当前页面未渲染 weapp-navigation-bar。`, {
|
|
11
|
+
key: "navigation-bar-missing",
|
|
12
|
+
context: "runtime:navigation"
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function createNavigationBarRuntimeBridge(getCurrentPages, emitWarning) {
|
|
16
|
+
return {
|
|
17
|
+
setNavigationBarTitle(options) {
|
|
18
|
+
const bar = getActiveNavigationBar(getCurrentPages());
|
|
19
|
+
if (!bar) {
|
|
20
|
+
warnNavigationBarMissing(emitWarning, "wx.setNavigationBarTitle");
|
|
21
|
+
return Promise.resolve();
|
|
22
|
+
}
|
|
23
|
+
if (options?.title !== void 0) bar.setAttribute("title", options.title);
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
},
|
|
26
|
+
setNavigationBarColor(options) {
|
|
27
|
+
const bar = getActiveNavigationBar(getCurrentPages());
|
|
28
|
+
if (!bar) {
|
|
29
|
+
warnNavigationBarMissing(emitWarning, "wx.setNavigationBarColor");
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}
|
|
32
|
+
if (options?.frontColor) bar.setAttribute("front-color", options.frontColor);
|
|
33
|
+
if (options?.backgroundColor) bar.setAttribute("background-color", options.backgroundColor);
|
|
34
|
+
if (options?.animation) {
|
|
35
|
+
const duration = typeof options.animation.duration === "number" ? `${options.animation.duration}ms` : void 0;
|
|
36
|
+
const easing = options.animation.timingFunction;
|
|
37
|
+
if (duration) bar.style.setProperty("--weapp-nav-transition-duration", duration);
|
|
38
|
+
if (easing) bar.style.setProperty("--weapp-nav-transition-easing", easing);
|
|
39
|
+
}
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
},
|
|
42
|
+
showNavigationBarLoading() {
|
|
43
|
+
const bar = getActiveNavigationBar(getCurrentPages());
|
|
44
|
+
if (!bar) {
|
|
45
|
+
warnNavigationBarMissing(emitWarning, "wx.showNavigationBarLoading");
|
|
46
|
+
return Promise.resolve();
|
|
47
|
+
}
|
|
48
|
+
bar.setAttribute("loading", "true");
|
|
49
|
+
return Promise.resolve();
|
|
50
|
+
},
|
|
51
|
+
hideNavigationBarLoading() {
|
|
52
|
+
const bar = getActiveNavigationBar(getCurrentPages());
|
|
53
|
+
if (!bar) {
|
|
54
|
+
warnNavigationBarMissing(emitWarning, "wx.hideNavigationBarLoading");
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
}
|
|
57
|
+
bar.removeAttribute("loading");
|
|
58
|
+
return Promise.resolve();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createNavigationBarRuntimeBridge };
|
|
65
|
+
//# sourceMappingURL=navigationBarRuntime.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigationBarRuntime.mjs","names":[],"sources":["../../../src/runtime/polyfill/navigationBarRuntime.ts"],"sourcesContent":["interface RuntimeWarningOptions {\n key: string\n context: string\n}\n\ntype RuntimeWarningEmitter = (message: string, options: RuntimeWarningOptions) => void\n\ninterface PageLike extends HTMLElement {\n renderRoot?: ShadowRoot | HTMLElement\n}\n\nfunction getActiveNavigationBar(pages: PageLike[]) {\n const current = pages[pages.length - 1]\n if (!current) {\n return undefined\n }\n const renderRoot = current.renderRoot\n ?? current.shadowRoot\n ?? current\n if (!renderRoot || typeof (renderRoot as ParentNode).querySelector !== 'function') {\n return undefined\n }\n return (renderRoot as ParentNode).querySelector('weapp-navigation-bar') as HTMLElement | null\n}\n\nfunction warnNavigationBarMissing(emitWarning: RuntimeWarningEmitter, action: string) {\n emitWarning(`[@weapp-vite/web] ${action} 需要默认导航栏支持,但当前页面未渲染 weapp-navigation-bar。`, {\n key: 'navigation-bar-missing',\n context: 'runtime:navigation',\n })\n}\n\nexport function createNavigationBarRuntimeBridge(\n getCurrentPages: () => PageLike[],\n emitWarning: RuntimeWarningEmitter,\n) {\n return {\n setNavigationBarTitle(options: { title: string }) {\n const bar = getActiveNavigationBar(getCurrentPages())\n if (!bar) {\n warnNavigationBarMissing(emitWarning, 'wx.setNavigationBarTitle')\n return Promise.resolve()\n }\n if (options?.title !== undefined) {\n bar.setAttribute('title', options.title)\n }\n return Promise.resolve()\n },\n setNavigationBarColor(options: {\n frontColor?: string\n backgroundColor?: string\n animation?: { duration?: number, timingFunction?: string }\n }) {\n const bar = getActiveNavigationBar(getCurrentPages())\n if (!bar) {\n warnNavigationBarMissing(emitWarning, 'wx.setNavigationBarColor')\n return Promise.resolve()\n }\n if (options?.frontColor) {\n bar.setAttribute('front-color', options.frontColor)\n }\n if (options?.backgroundColor) {\n bar.setAttribute('background-color', options.backgroundColor)\n }\n if (options?.animation) {\n const duration = typeof options.animation.duration === 'number'\n ? `${options.animation.duration}ms`\n : undefined\n const easing = options.animation.timingFunction\n if (duration) {\n bar.style.setProperty('--weapp-nav-transition-duration', duration)\n }\n if (easing) {\n bar.style.setProperty('--weapp-nav-transition-easing', easing)\n }\n }\n return Promise.resolve()\n },\n showNavigationBarLoading() {\n const bar = getActiveNavigationBar(getCurrentPages())\n if (!bar) {\n warnNavigationBarMissing(emitWarning, 'wx.showNavigationBarLoading')\n return Promise.resolve()\n }\n bar.setAttribute('loading', 'true')\n return Promise.resolve()\n },\n hideNavigationBarLoading() {\n const bar = getActiveNavigationBar(getCurrentPages())\n if (!bar) {\n warnNavigationBarMissing(emitWarning, 'wx.hideNavigationBarLoading')\n return Promise.resolve()\n }\n bar.removeAttribute('loading')\n return Promise.resolve()\n },\n }\n}\n"],"mappings":";AAWA,SAAS,uBAAuB,OAAmB;CACjD,MAAM,UAAU,MAAM,MAAM,SAAS;AACrC,KAAI,CAAC,QACH;CAEF,MAAM,aAAa,QAAQ,cACtB,QAAQ,cACR;AACL,KAAI,CAAC,cAAc,OAAQ,WAA0B,kBAAkB,WACrE;AAEF,QAAQ,WAA0B,cAAc,uBAAuB;;AAGzE,SAAS,yBAAyB,aAAoC,QAAgB;AACpF,aAAY,qBAAqB,OAAO,4CAA4C;EAClF,KAAK;E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|
|
|
1
|
+
import { normalizeFilePath, resolveUploadFileBlob, resolveUploadFileName } from "../files.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/polyfill/network/request.ts
|
|
4
|
+
function resolveTimeoutSignal(timeout) {
|
|
5
|
+
const controller = typeof AbortController === "function" ? new AbortController() : void 0;
|
|
6
|
+
let timeoutTimer;
|
|
7
|
+
if (timeout > 0 && controller) timeoutTimer = setTimeout(() => controller.abort(), timeout);
|
|
8
|
+
return {
|
|
9
|
+
signal: controller?.signal,
|
|
10
|
+
clear: () => {
|
|
11
|
+
if (timeoutTimer) clearTimeout(timeoutTimer);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function getRuntimeFetch() {
|
|
16
|
+
const maybeFetch = globalThis.fetch;
|
|
17
|
+
if (typeof maybeFetch === "function") return maybeFetch;
|
|
18
|
+
}
|
|
19
|
+
function normalizeRequestMethod(method) {
|
|
20
|
+
return (method || "GET").toUpperCase();
|
|
21
|
+
}
|
|
22
|
+
function normalizeRequestHeaders(header) {
|
|
23
|
+
if (!header) return {};
|
|
24
|
+
return { ...header };
|
|
25
|
+
}
|
|
26
|
+
function buildRequestUrl(url, method, data) {
|
|
27
|
+
if (method !== "GET" || data == null) return url;
|
|
28
|
+
if (typeof data === "string") {
|
|
29
|
+
if (!data) return url;
|
|
30
|
+
return `${url}${url.includes("?") ? "&" : "?"}${data}`;
|
|
31
|
+
}
|
|
32
|
+
if (typeof URLSearchParams !== "undefined" && data instanceof URLSearchParams) {
|
|
33
|
+
const query = data.toString();
|
|
34
|
+
if (!query) return url;
|
|
35
|
+
return `${url}${url.includes("?") ? "&" : "?"}${query}`;
|
|
36
|
+
}
|
|
37
|
+
if (typeof data === "object") {
|
|
38
|
+
const query = new URLSearchParams();
|
|
39
|
+
for (const [key, value] of Object.entries(data)) query.append(key, value == null ? "" : String(value));
|
|
40
|
+
const queryText = query.toString();
|
|
41
|
+
if (!queryText) return url;
|
|
42
|
+
return `${url}${url.includes("?") ? "&" : "?"}${queryText}`;
|
|
43
|
+
}
|
|
44
|
+
return url;
|
|
45
|
+
}
|
|
46
|
+
function buildRequestBody(method, data, headers) {
|
|
47
|
+
if (method === "GET" || data == null) return;
|
|
48
|
+
if (typeof data === "string") return data;
|
|
49
|
+
if (typeof URLSearchParams !== "undefined" && data instanceof URLSearchParams) return data;
|
|
50
|
+
if (typeof FormData !== "undefined" && data instanceof FormData) return data;
|
|
51
|
+
const contentTypeKey = Object.keys(headers).find((key) => key.toLowerCase() === "content-type");
|
|
52
|
+
const contentType = contentTypeKey ? headers[contentTypeKey] : "";
|
|
53
|
+
if (contentType && !contentType.includes("application/json")) return String(data);
|
|
54
|
+
if (!contentTypeKey) headers["content-type"] = "application/json";
|
|
55
|
+
return JSON.stringify(data);
|
|
56
|
+
}
|
|
57
|
+
async function parseRequestResponseData(response, options) {
|
|
58
|
+
if (options?.responseType === "arraybuffer") return response.arrayBuffer();
|
|
59
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
60
|
+
if (options?.dataType === "text") return response.text();
|
|
61
|
+
if (options?.dataType === "json" || contentType.includes("application/json")) return response.json();
|
|
62
|
+
return response.text();
|
|
63
|
+
}
|
|
64
|
+
function createBlobObjectUrl(blob) {
|
|
65
|
+
const runtimeUrl = globalThis.URL;
|
|
66
|
+
if (runtimeUrl && typeof runtimeUrl.createObjectURL === "function") return runtimeUrl.createObjectURL(blob);
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
function collectResponseHeaders(response) {
|
|
70
|
+
const headers = {};
|
|
71
|
+
response.headers.forEach((value, key) => {
|
|
72
|
+
headers[key] = value;
|
|
73
|
+
});
|
|
74
|
+
return headers;
|
|
75
|
+
}
|
|
76
|
+
function stripUploadContentType(headers) {
|
|
77
|
+
const normalized = { ...headers };
|
|
78
|
+
for (const key of Object.keys(normalized)) if (key.toLowerCase() === "content-type") delete normalized[key];
|
|
79
|
+
return normalized;
|
|
80
|
+
}
|
|
81
|
+
async function performRequestByFetch(options) {
|
|
82
|
+
const url = options?.url?.trim() ?? "";
|
|
83
|
+
if (!url) throw new TypeError("invalid url");
|
|
84
|
+
const runtimeFetch = getRuntimeFetch();
|
|
85
|
+
if (!runtimeFetch) throw new TypeError("fetch is unavailable");
|
|
86
|
+
const method = normalizeRequestMethod(options?.method);
|
|
87
|
+
const headers = normalizeRequestHeaders(options?.header);
|
|
88
|
+
const requestUrl = buildRequestUrl(url, method, options?.data);
|
|
89
|
+
const body = buildRequestBody(method, options?.data, headers);
|
|
90
|
+
const timeoutControl = resolveTimeoutSignal(typeof options?.timeout === "number" && options.timeout > 0 ? options.timeout : 0);
|
|
91
|
+
try {
|
|
92
|
+
const response = await runtimeFetch(requestUrl, {
|
|
93
|
+
method,
|
|
94
|
+
headers,
|
|
95
|
+
body,
|
|
96
|
+
signal: timeoutControl.signal
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
data: await parseRequestResponseData(response, options),
|
|
100
|
+
statusCode: response.status,
|
|
101
|
+
header: collectResponseHeaders(response)
|
|
102
|
+
};
|
|
103
|
+
} finally {
|
|
104
|
+
timeoutControl.clear();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async function performDownloadByFetch(options) {
|
|
108
|
+
const url = options?.url?.trim() ?? "";
|
|
109
|
+
if (!url) throw new TypeError("invalid url");
|
|
110
|
+
const runtimeFetch = getRuntimeFetch();
|
|
111
|
+
if (!runtimeFetch) throw new TypeError("fetch is unavailable");
|
|
112
|
+
const headers = normalizeRequestHeaders(options?.header);
|
|
113
|
+
const timeoutControl = resolveTimeoutSignal(typeof options?.timeout === "number" && options.timeout > 0 ? options.timeout : 0);
|
|
114
|
+
try {
|
|
115
|
+
const response = await runtimeFetch(url, {
|
|
116
|
+
method: "GET",
|
|
117
|
+
headers,
|
|
118
|
+
signal: timeoutControl.signal
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
tempFilePath: createBlobObjectUrl(await response.blob()) || url,
|
|
122
|
+
statusCode: response.status
|
|
123
|
+
};
|
|
124
|
+
} finally {
|
|
125
|
+
timeoutControl.clear();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function performUploadByFetch(options) {
|
|
129
|
+
const url = options?.url?.trim() ?? "";
|
|
130
|
+
if (!url) throw new TypeError("invalid url");
|
|
131
|
+
const filePath = normalizeFilePath(options?.filePath);
|
|
132
|
+
if (!filePath) throw new TypeError("invalid filePath");
|
|
133
|
+
const runtimeFetch = getRuntimeFetch();
|
|
134
|
+
if (!runtimeFetch) throw new TypeError("fetch is unavailable");
|
|
135
|
+
const FormDataCtor = globalThis.FormData;
|
|
136
|
+
if (typeof FormDataCtor !== "function") throw new TypeError("FormData is unavailable");
|
|
137
|
+
const headers = stripUploadContentType(normalizeRequestHeaders(options?.header));
|
|
138
|
+
const formData = new FormDataCtor();
|
|
139
|
+
for (const [key, value] of Object.entries(options?.formData ?? {})) formData.append(key, value == null ? "" : String(value));
|
|
140
|
+
const blob = await resolveUploadFileBlob(filePath, runtimeFetch);
|
|
141
|
+
formData.append(options?.name?.trim() || "file", blob, resolveUploadFileName(filePath));
|
|
142
|
+
const timeoutControl = resolveTimeoutSignal(typeof options?.timeout === "number" && options.timeout > 0 ? options.timeout : 0);
|
|
143
|
+
try {
|
|
144
|
+
const response = await runtimeFetch(url, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers,
|
|
147
|
+
body: formData,
|
|
148
|
+
signal: timeoutControl.signal
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
data: await response.text(),
|
|
152
|
+
statusCode: response.status,
|
|
153
|
+
header: collectResponseHeaders(response)
|
|
154
|
+
};
|
|
155
|
+
} finally {
|
|
156
|
+
timeoutControl.clear();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
export { performDownloadByFetch, performRequestByFetch, performUploadByFetch };
|
|
162
|
+
//# sourceMappingURL=request.mjs.map
|