@codingame/monaco-vscode-image-resize-service-override 25.1.2 → 26.0.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingame/monaco-vscode-image-resize-service-override",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "26.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "VSCode public API plugged on the monaco editor - image-resize service-override",
|
|
6
6
|
"keywords": [],
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@codingame/monaco-vscode-api": "
|
|
18
|
+
"@codingame/monaco-vscode-api": "26.0.0"
|
|
19
19
|
},
|
|
20
20
|
"main": "index.js",
|
|
21
21
|
"module": "index.js",
|
|
@@ -7,18 +7,23 @@ import '@codingame/monaco-vscode-api/vscode/vs/platform/instantiation/common/ins
|
|
|
7
7
|
|
|
8
8
|
class ImageResizeService {
|
|
9
9
|
async resizeImage(data, mimeType) {
|
|
10
|
-
const isGif = mimeType ===
|
|
11
|
-
if (typeof data ===
|
|
10
|
+
const isGif = mimeType === "image/gif";
|
|
11
|
+
if (typeof data === "string") {
|
|
12
12
|
data = this.convertStringToUInt8Array(data);
|
|
13
13
|
}
|
|
14
14
|
return ( new Promise((resolve, reject) => {
|
|
15
|
-
const blob = ( new Blob([data], {
|
|
15
|
+
const blob = ( new Blob([data], {
|
|
16
|
+
type: mimeType
|
|
17
|
+
}));
|
|
16
18
|
const img = ( new Image());
|
|
17
19
|
const url = URL.createObjectURL(blob);
|
|
18
20
|
img.src = url;
|
|
19
21
|
img.onload = () => {
|
|
20
22
|
URL.revokeObjectURL(url);
|
|
21
|
-
let {
|
|
23
|
+
let {
|
|
24
|
+
width,
|
|
25
|
+
height
|
|
26
|
+
} = img;
|
|
22
27
|
if ((width <= 768 || height <= 768) && !isGif) {
|
|
23
28
|
resolve(data);
|
|
24
29
|
return;
|
|
@@ -31,40 +36,38 @@ class ImageResizeService {
|
|
|
31
36
|
const scaleFactor = 768 / Math.min(width, height);
|
|
32
37
|
width = Math.round(width * scaleFactor);
|
|
33
38
|
height = Math.round(height * scaleFactor);
|
|
34
|
-
const canvas = createElement(
|
|
39
|
+
const canvas = createElement("canvas");
|
|
35
40
|
canvas.width = width;
|
|
36
41
|
canvas.height = height;
|
|
37
|
-
const ctx = canvas.getContext(
|
|
42
|
+
const ctx = canvas.getContext("2d");
|
|
38
43
|
if (ctx) {
|
|
39
44
|
ctx.drawImage(img, 0, 0, width, height);
|
|
40
|
-
const jpegTypes = [
|
|
41
|
-
const outputMimeType = mimeType && jpegTypes.includes(mimeType) ?
|
|
45
|
+
const jpegTypes = ["image/jpeg", "image/jpg"];
|
|
46
|
+
const outputMimeType = mimeType && jpegTypes.includes(mimeType) ? "image/jpeg" : "image/png";
|
|
42
47
|
canvas.toBlob(blob => {
|
|
43
48
|
if (blob) {
|
|
44
49
|
const reader = ( new FileReader());
|
|
45
50
|
reader.onload = () => {
|
|
46
51
|
resolve(( new Uint8Array(reader.result)));
|
|
47
52
|
};
|
|
48
|
-
reader.onerror =
|
|
53
|
+
reader.onerror = error => reject(error);
|
|
49
54
|
reader.readAsArrayBuffer(blob);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
reject(( new Error('Failed to create blob from canvas')));
|
|
55
|
+
} else {
|
|
56
|
+
reject(( new Error("Failed to create blob from canvas")));
|
|
53
57
|
}
|
|
54
58
|
}, outputMimeType);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
reject(( new Error('Failed to get canvas context')));
|
|
59
|
+
} else {
|
|
60
|
+
reject(( new Error("Failed to get canvas context")));
|
|
58
61
|
}
|
|
59
62
|
};
|
|
60
|
-
img.onerror =
|
|
63
|
+
img.onerror = error => {
|
|
61
64
|
URL.revokeObjectURL(url);
|
|
62
65
|
reject(error);
|
|
63
66
|
};
|
|
64
67
|
}));
|
|
65
68
|
}
|
|
66
69
|
convertStringToUInt8Array(data) {
|
|
67
|
-
const base64Data = data.includes(
|
|
70
|
+
const base64Data = data.includes(",") ? data.split(",")[1] : data;
|
|
68
71
|
if (this.isValidBase64(base64Data)) {
|
|
69
72
|
return decodeBase64(base64Data).buffer;
|
|
70
73
|
}
|
|
@@ -75,17 +78,15 @@ class ImageResizeService {
|
|
|
75
78
|
const decoder = ( new TextDecoder());
|
|
76
79
|
const decodedString = decoder.decode(data);
|
|
77
80
|
return decodedString;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return '';
|
|
81
|
+
} catch {
|
|
82
|
+
return "";
|
|
81
83
|
}
|
|
82
84
|
}
|
|
83
85
|
isValidBase64(str) {
|
|
84
86
|
try {
|
|
85
87
|
decodeBase64(str);
|
|
86
88
|
return true;
|
|
87
|
-
}
|
|
88
|
-
catch {
|
|
89
|
+
} catch {
|
|
89
90
|
return false;
|
|
90
91
|
}
|
|
91
92
|
}
|
|
@@ -94,7 +95,7 @@ class ImageResizeService {
|
|
|
94
95
|
if (!exists) {
|
|
95
96
|
await fileService.createFolder(imagesFolder);
|
|
96
97
|
}
|
|
97
|
-
const ext = mimeType.split(
|
|
98
|
+
const ext = mimeType.split("/")[1] || "png";
|
|
98
99
|
const filename = `image-${Date.now()}.${ext}`;
|
|
99
100
|
const fileUri = joinPath(imagesFolder, filename);
|
|
100
101
|
const buffer = VSBuffer.wrap(dataTransfer);
|
|
@@ -111,15 +112,14 @@ class ImageResizeService {
|
|
|
111
112
|
if (!files.children) {
|
|
112
113
|
return;
|
|
113
114
|
}
|
|
114
|
-
await Promise.all(( files.children.map(async
|
|
115
|
+
await Promise.all(( files.children.map(async file => {
|
|
115
116
|
try {
|
|
116
117
|
const timestamp = this.getTimestampFromFilename(file.name);
|
|
117
118
|
if (timestamp && (Date.now() - timestamp > duration)) {
|
|
118
119
|
await fileService.del(file.resource);
|
|
119
120
|
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
logService.error('Failed to clean up old images', err);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
logService.error("Failed to clean up old images", err);
|
|
123
123
|
}
|
|
124
124
|
})));
|
|
125
125
|
}
|