@monolith-forensics/monolith-ui 1.9.1-dev.2 → 1.9.1-dev.6
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.
|
@@ -60,124 +60,20 @@ const getImageFilename = (image) => {
|
|
|
60
60
|
return "image.png";
|
|
61
61
|
};
|
|
62
62
|
const getImageBlob = (src) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
-
const response = yield fetch(src
|
|
64
|
-
mode: "cors",
|
|
65
|
-
credentials: "omit",
|
|
66
|
-
cache: "no-store",
|
|
67
|
-
});
|
|
63
|
+
const response = yield fetch(src);
|
|
68
64
|
if (!response.ok) {
|
|
69
65
|
throw new Error("Unable to load image.");
|
|
70
66
|
}
|
|
71
|
-
return
|
|
72
|
-
blob: yield response.blob(),
|
|
73
|
-
contentType: response.headers.get("content-type") || "",
|
|
74
|
-
};
|
|
75
|
-
});
|
|
76
|
-
const clipboardPngType = "image/png";
|
|
77
|
-
const imageMimeTypesByExtension = {
|
|
78
|
-
gif: "image/gif",
|
|
79
|
-
jpg: "image/jpeg",
|
|
80
|
-
jpeg: "image/jpeg",
|
|
81
|
-
png: clipboardPngType,
|
|
82
|
-
svg: "image/svg+xml",
|
|
83
|
-
webp: "image/webp",
|
|
84
|
-
};
|
|
85
|
-
const normalizeMimeType = (type) => type.toLowerCase().split(";")[0].trim();
|
|
86
|
-
const getImageMimeTypeFromSource = (src) => {
|
|
87
|
-
var _a;
|
|
88
|
-
try {
|
|
89
|
-
const url = new URL(src);
|
|
90
|
-
const filename = url.pathname.split("/").filter(Boolean).pop();
|
|
91
|
-
const extension = (_a = filename === null || filename === void 0 ? void 0 : filename.split(".").pop()) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
92
|
-
return extension ? imageMimeTypesByExtension[extension] || "" : "";
|
|
93
|
-
}
|
|
94
|
-
catch (_b) {
|
|
95
|
-
const dataUrlMatch = src.match(/^data:([^;,]+)/);
|
|
96
|
-
return (dataUrlMatch === null || dataUrlMatch === void 0 ? void 0 : dataUrlMatch[1]) || "";
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
const canWriteClipboardType = (ClipboardItemCtor, type) => {
|
|
100
|
-
if (!type)
|
|
101
|
-
return false;
|
|
102
|
-
if (typeof ClipboardItemCtor.supports === "function") {
|
|
103
|
-
return ClipboardItemCtor.supports(type);
|
|
104
|
-
}
|
|
105
|
-
return type === clipboardPngType;
|
|
106
|
-
};
|
|
107
|
-
const loadImageSource = (src) => new Promise((resolve, reject) => {
|
|
108
|
-
const image = new Image();
|
|
109
|
-
image.crossOrigin = "anonymous";
|
|
110
|
-
image.onload = () => resolve(image);
|
|
111
|
-
image.onerror = () => {
|
|
112
|
-
reject(new Error("Unable to prepare image for clipboard."));
|
|
113
|
-
};
|
|
114
|
-
image.src = src;
|
|
115
|
-
});
|
|
116
|
-
const renderImageSourceToPngBlob = (src) => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
-
const image = yield loadImageSource(src);
|
|
118
|
-
const width = image.naturalWidth || image.width;
|
|
119
|
-
const height = image.naturalHeight || image.height;
|
|
120
|
-
if (!width || !height) {
|
|
121
|
-
throw new Error("Unable to prepare image for clipboard.");
|
|
122
|
-
}
|
|
123
|
-
const canvas = document.createElement("canvas");
|
|
124
|
-
canvas.width = width;
|
|
125
|
-
canvas.height = height;
|
|
126
|
-
const context = canvas.getContext("2d");
|
|
127
|
-
if (!context) {
|
|
128
|
-
throw new Error("Unable to prepare image for clipboard.");
|
|
129
|
-
}
|
|
130
|
-
context.drawImage(image, 0, 0, width, height);
|
|
131
|
-
return new Promise((resolve, reject) => {
|
|
132
|
-
canvas.toBlob((blob) => {
|
|
133
|
-
if (blob) {
|
|
134
|
-
resolve(blob);
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
reject(new Error("Unable to prepare image for clipboard."));
|
|
138
|
-
}
|
|
139
|
-
}, clipboardPngType);
|
|
140
|
-
});
|
|
141
|
-
});
|
|
142
|
-
const convertBlobToClipboardPng = (blob, fallbackSrc) => __awaiter(void 0, void 0, void 0, function* () {
|
|
143
|
-
const objectUrl = URL.createObjectURL(blob);
|
|
144
|
-
try {
|
|
145
|
-
return yield renderImageSourceToPngBlob(objectUrl);
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
if (!fallbackSrc || fallbackSrc === objectUrl)
|
|
149
|
-
throw error;
|
|
150
|
-
return renderImageSourceToPngBlob(fallbackSrc);
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
153
|
-
URL.revokeObjectURL(objectUrl);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
const getClipboardImageBlob = (image, ClipboardItemCtor) => __awaiter(void 0, void 0, void 0, function* () {
|
|
157
|
-
const src = image.currentSrc || image.src;
|
|
158
|
-
const { blob, contentType } = yield getImageBlob(src);
|
|
159
|
-
const type = normalizeMimeType(blob.type) ||
|
|
160
|
-
normalizeMimeType(contentType) ||
|
|
161
|
-
getImageMimeTypeFromSource(src);
|
|
162
|
-
if (canWriteClipboardType(ClipboardItemCtor, type)) {
|
|
163
|
-
return {
|
|
164
|
-
blob: blob.type === type ? blob : new Blob([blob], { type }),
|
|
165
|
-
type,
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
blob: yield convertBlobToClipboardPng(blob, src),
|
|
170
|
-
type: clipboardPngType,
|
|
171
|
-
};
|
|
67
|
+
return response.blob();
|
|
172
68
|
});
|
|
173
69
|
const copyImage = (image) => __awaiter(void 0, void 0, void 0, function* () {
|
|
174
70
|
var _a;
|
|
175
|
-
const ClipboardItemCtor = window
|
|
176
|
-
.ClipboardItem;
|
|
71
|
+
const ClipboardItemCtor = window.ClipboardItem;
|
|
177
72
|
if (!((_a = navigator.clipboard) === null || _a === void 0 ? void 0 : _a.write) || !ClipboardItemCtor) {
|
|
178
73
|
throw new Error("Image copying is not supported by this browser.");
|
|
179
74
|
}
|
|
180
|
-
const
|
|
75
|
+
const blob = yield getImageBlob(image.src);
|
|
76
|
+
const type = blob.type || "image/png";
|
|
181
77
|
yield navigator.clipboard.write([
|
|
182
78
|
new ClipboardItemCtor({
|
|
183
79
|
[type]: blob,
|
|
@@ -189,7 +85,7 @@ const downloadImage = (image) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
189
85
|
let href = image.src;
|
|
190
86
|
let objectUrl = null;
|
|
191
87
|
try {
|
|
192
|
-
const
|
|
88
|
+
const blob = yield getImageBlob(image.src);
|
|
193
89
|
objectUrl = URL.createObjectURL(blob);
|
|
194
90
|
href = objectUrl;
|
|
195
91
|
}
|