@midscene/shared 0.7.1 → 0.7.2-beta-20241024094141.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/browser/constants.js +41 -0
- package/dist/{types → browser}/fs.d.ts +2 -2
- package/dist/browser/fs.js +73 -0
- package/dist/{types → browser}/img.d.ts +3 -1
- package/dist/browser/img.js +366 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/utils.d.ts +4 -0
- package/dist/browser/utils.js +34 -0
- package/dist/es/constants.d.ts +13 -0
- package/dist/es/fs.d.ts +14 -0
- package/dist/es/fs.js +7 -3
- package/dist/es/img.d.ts +131 -0
- package/dist/es/img.js +144 -89
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -3
- package/dist/es/utils.d.ts +4 -0
- package/dist/es/utils.js +13 -0
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/fs.d.ts +14 -0
- package/dist/lib/fs.js +8 -4
- package/dist/lib/img.d.ts +131 -0
- package/dist/lib/img.js +142 -87
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +1 -3
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +39 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +41 -17
- package/src/fs/index.ts +6 -2
- package/src/img/box-select.ts +8 -5
- package/src/img/get-jimp.ts +7 -0
- package/src/img/index.ts +1 -0
- package/src/img/info.ts +11 -4
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +6 -1
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/constants/index.ts
|
|
21
|
+
var constants_exports = {};
|
|
22
|
+
__export(constants_exports, {
|
|
23
|
+
CONTAINER_MINI_HEIGHT: () => CONTAINER_MINI_HEIGHT,
|
|
24
|
+
CONTAINER_MINI_WIDTH: () => CONTAINER_MINI_WIDTH,
|
|
25
|
+
NodeType: () => NodeType,
|
|
26
|
+
TEXT_MAX_SIZE: () => TEXT_MAX_SIZE,
|
|
27
|
+
TEXT_SIZE_THRESHOLD: () => TEXT_SIZE_THRESHOLD
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(constants_exports);
|
|
30
|
+
var TEXT_SIZE_THRESHOLD = 9;
|
|
31
|
+
var TEXT_MAX_SIZE = 40;
|
|
32
|
+
var CONTAINER_MINI_HEIGHT = 3;
|
|
33
|
+
var CONTAINER_MINI_WIDTH = 3;
|
|
34
|
+
var NodeType = /* @__PURE__ */ ((NodeType2) => {
|
|
35
|
+
NodeType2["CONTAINER"] = "CONTAINER Node";
|
|
36
|
+
NodeType2["FORM_ITEM"] = "FORM_ITEM Node";
|
|
37
|
+
NodeType2["BUTTON"] = "BUTTON Node";
|
|
38
|
+
NodeType2["IMG"] = "IMG Node";
|
|
39
|
+
NodeType2["TEXT"] = "TEXT Node";
|
|
40
|
+
return NodeType2;
|
|
41
|
+
})(NodeType || {});
|
|
@@ -3,7 +3,7 @@ interface PkgInfo {
|
|
|
3
3
|
version: string;
|
|
4
4
|
dir: string;
|
|
5
5
|
}
|
|
6
|
-
declare function
|
|
6
|
+
declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
7
|
/**
|
|
8
8
|
* Find the nearest package.json file recursively
|
|
9
9
|
* @param {string} dir - Home directory
|
|
@@ -11,4 +11,4 @@ declare function getMidscenePkgInfo(dir: string): PkgInfo;
|
|
|
11
11
|
*/
|
|
12
12
|
declare function findNearestPackageJson(dir: string): string | null;
|
|
13
13
|
|
|
14
|
-
export { findNearestPackageJson,
|
|
14
|
+
export { findNearestPackageJson, getRunningPkgInfo };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/fs/index.ts
|
|
31
|
+
var fs_exports = {};
|
|
32
|
+
__export(fs_exports, {
|
|
33
|
+
findNearestPackageJson: () => findNearestPackageJson,
|
|
34
|
+
getRunningPkgInfo: () => getRunningPkgInfo
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(fs_exports);
|
|
37
|
+
var import_node_assert = __toESM(require("assert"));
|
|
38
|
+
var import_node_fs = require("fs");
|
|
39
|
+
var import_node_path = require("path");
|
|
40
|
+
var pkg;
|
|
41
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
42
|
+
function getRunningPkgInfo(dir) {
|
|
43
|
+
if (ifInBrowser) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
if (pkg) {
|
|
47
|
+
return pkg;
|
|
48
|
+
}
|
|
49
|
+
const pkgDir = findNearestPackageJson(dir || process.cwd());
|
|
50
|
+
(0, import_node_assert.default)(pkgDir, "package.json not found");
|
|
51
|
+
const pkgJsonFile = (0, import_node_path.join)(pkgDir, "package.json");
|
|
52
|
+
if (pkgJsonFile) {
|
|
53
|
+
const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8"));
|
|
54
|
+
pkg = { name, version, dir: pkgDir };
|
|
55
|
+
return pkg;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
name: "midscene-unknown-package-name",
|
|
59
|
+
version: "0.0.0",
|
|
60
|
+
dir: pkgDir
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function findNearestPackageJson(dir) {
|
|
64
|
+
const packageJsonPath = (0, import_node_path.join)(dir, "package.json");
|
|
65
|
+
if ((0, import_node_fs.existsSync)(packageJsonPath)) {
|
|
66
|
+
return dir;
|
|
67
|
+
}
|
|
68
|
+
const parentDir = (0, import_node_path.dirname)(dir);
|
|
69
|
+
if (parentDir === dir) {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return findNearestPackageJson(parentDir);
|
|
73
|
+
}
|
|
@@ -21,6 +21,7 @@ declare function imageInfo(image: string | Buffer): Promise<Size$1>;
|
|
|
21
21
|
* @throws Error if the image data is invalid
|
|
22
22
|
*/
|
|
23
23
|
declare function imageInfoOfBase64(imageBase64: string): Promise<Size$1>;
|
|
24
|
+
declare function bufferFromBase64(imageBase64: string): Promise<Buffer>;
|
|
24
25
|
/**
|
|
25
26
|
* Encodes an image file to a base64 encoded string
|
|
26
27
|
*
|
|
@@ -34,6 +35,7 @@ declare function imageInfoOfBase64(imageBase64: string): Promise<Size$1>;
|
|
|
34
35
|
declare function base64Encoded(image: string, withHeader?: boolean): string;
|
|
35
36
|
declare function base64ToPngFormat(base64: string): string;
|
|
36
37
|
|
|
38
|
+
/**
|
|
37
39
|
/**
|
|
38
40
|
* Saves a Base64-encoded image to a file
|
|
39
41
|
*
|
|
@@ -126,4 +128,4 @@ declare const processImageElementInfo: (options: {
|
|
|
126
128
|
compositeElementInfoImgWithoutTextBase64: string;
|
|
127
129
|
}>;
|
|
128
130
|
|
|
129
|
-
export { base64Encoded, base64ToPngFormat, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, saveBase64Image, transformImgPathToBase64, trimImage };
|
|
131
|
+
export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, saveBase64Image, transformImgPathToBase64, trimImage };
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/img/index.ts
|
|
51
|
+
var img_exports = {};
|
|
52
|
+
__export(img_exports, {
|
|
53
|
+
base64Encoded: () => base64Encoded,
|
|
54
|
+
base64ToPngFormat: () => base64ToPngFormat,
|
|
55
|
+
bufferFromBase64: () => bufferFromBase64,
|
|
56
|
+
calculateNewDimensions: () => calculateNewDimensions,
|
|
57
|
+
compositeElementInfoImg: () => compositeElementInfoImg,
|
|
58
|
+
imageInfo: () => imageInfo,
|
|
59
|
+
imageInfoOfBase64: () => imageInfoOfBase64,
|
|
60
|
+
processImageElementInfo: () => processImageElementInfo,
|
|
61
|
+
resizeImg: () => resizeImg,
|
|
62
|
+
saveBase64Image: () => saveBase64Image,
|
|
63
|
+
transformImgPathToBase64: () => transformImgPathToBase64,
|
|
64
|
+
trimImage: () => trimImage
|
|
65
|
+
});
|
|
66
|
+
module.exports = __toCommonJS(img_exports);
|
|
67
|
+
|
|
68
|
+
// src/img/info.ts
|
|
69
|
+
var import_node_assert = __toESM(require("assert"));
|
|
70
|
+
var import_node_buffer = require("buffer");
|
|
71
|
+
var import_node_fs = require("fs");
|
|
72
|
+
|
|
73
|
+
// src/img/get-jimp.ts
|
|
74
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
75
|
+
function getJimp() {
|
|
76
|
+
return __async(this, null, function* () {
|
|
77
|
+
if (ifInBrowser) {
|
|
78
|
+
return (yield import("jimp/browser/lib/jimp.js")).default;
|
|
79
|
+
}
|
|
80
|
+
return (yield import("jimp")).default;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/img/info.ts
|
|
85
|
+
function imageInfo(image) {
|
|
86
|
+
return __async(this, null, function* () {
|
|
87
|
+
const Jimp = yield getJimp();
|
|
88
|
+
let jimpImage;
|
|
89
|
+
if (typeof image === "string") {
|
|
90
|
+
jimpImage = yield Jimp.read(image);
|
|
91
|
+
} else if (import_node_buffer.Buffer.isBuffer(image)) {
|
|
92
|
+
jimpImage = yield Jimp.read(image);
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
95
|
+
}
|
|
96
|
+
const { width, height } = jimpImage.bitmap;
|
|
97
|
+
(0, import_node_assert.default)(
|
|
98
|
+
width && height,
|
|
99
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
100
|
+
);
|
|
101
|
+
return { width, height };
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function imageInfoOfBase64(imageBase64) {
|
|
105
|
+
return __async(this, null, function* () {
|
|
106
|
+
const buffer = yield bufferFromBase64(imageBase64);
|
|
107
|
+
return imageInfo(buffer);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function bufferFromBase64(imageBase64) {
|
|
111
|
+
return __async(this, null, function* () {
|
|
112
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
113
|
+
return import_node_buffer.Buffer.from(base64Data, "base64");
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function base64Encoded(image, withHeader = true) {
|
|
117
|
+
const imageBuffer = (0, import_node_fs.readFileSync)(image);
|
|
118
|
+
if (!withHeader) {
|
|
119
|
+
return imageBuffer.toString("base64");
|
|
120
|
+
}
|
|
121
|
+
if (image.endsWith("png")) {
|
|
122
|
+
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
|
123
|
+
}
|
|
124
|
+
if (image.endsWith("jpg") || image.endsWith("jpeg")) {
|
|
125
|
+
return `data:image/jpeg;base64,${imageBuffer.toString("base64")}`;
|
|
126
|
+
}
|
|
127
|
+
throw new Error("unsupported image type");
|
|
128
|
+
}
|
|
129
|
+
function base64ToPngFormat(base64) {
|
|
130
|
+
return `data:image/png;base64,${base64}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/img/transform.ts
|
|
134
|
+
var import_node_buffer2 = require("buffer");
|
|
135
|
+
function saveBase64Image(options) {
|
|
136
|
+
return __async(this, null, function* () {
|
|
137
|
+
const { base64Data, outputPath } = options;
|
|
138
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
139
|
+
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
|
|
140
|
+
const Jimp = yield getJimp();
|
|
141
|
+
const image = yield Jimp.read(imageBuffer);
|
|
142
|
+
yield image.writeAsync(outputPath);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function transformImgPathToBase64(inputPath) {
|
|
146
|
+
return __async(this, null, function* () {
|
|
147
|
+
const Jimp = yield getJimp();
|
|
148
|
+
const image = yield Jimp.read(inputPath);
|
|
149
|
+
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
150
|
+
return buffer.toString("base64");
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
function resizeImg(inputData, newSize) {
|
|
154
|
+
return __async(this, null, function* () {
|
|
155
|
+
const isBase64 = typeof inputData === "string";
|
|
156
|
+
const imageBuffer = isBase64 ? import_node_buffer2.Buffer.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData;
|
|
157
|
+
const Jimp = yield getJimp();
|
|
158
|
+
const image = yield Jimp.read(imageBuffer);
|
|
159
|
+
const { width, height } = image.bitmap;
|
|
160
|
+
if (!width || !height) {
|
|
161
|
+
throw Error("Undefined width or height from the input image.");
|
|
162
|
+
}
|
|
163
|
+
const finalNewSize = newSize || calculateNewDimensions(width, height);
|
|
164
|
+
image.resize(finalNewSize.width, finalNewSize.height);
|
|
165
|
+
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
166
|
+
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
170
|
+
const maxWidth = 2048;
|
|
171
|
+
const maxHeight = 768;
|
|
172
|
+
let newWidth = originalWidth;
|
|
173
|
+
let newHeight = originalHeight;
|
|
174
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
175
|
+
if (originalWidth > maxWidth) {
|
|
176
|
+
newWidth = maxWidth;
|
|
177
|
+
newHeight = newWidth / aspectRatio;
|
|
178
|
+
}
|
|
179
|
+
if (newHeight > maxHeight) {
|
|
180
|
+
newHeight = maxHeight;
|
|
181
|
+
newWidth = newHeight * aspectRatio;
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
width: Math.round(newWidth),
|
|
185
|
+
height: Math.round(newHeight)
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function trimImage(image) {
|
|
189
|
+
return __async(this, null, function* () {
|
|
190
|
+
const Jimp = yield getJimp();
|
|
191
|
+
const jimpImage = yield Jimp.read(
|
|
192
|
+
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image)
|
|
193
|
+
);
|
|
194
|
+
const { width, height } = jimpImage.bitmap;
|
|
195
|
+
if (width <= 3 || height <= 3) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
const trimmedImage = jimpImage.autocrop();
|
|
199
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
200
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
201
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
202
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
207
|
+
trimOffsetTop: -trimOffsetTop,
|
|
208
|
+
width: trimmedWidth,
|
|
209
|
+
height: trimmedHeight
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/img/box-select.ts
|
|
215
|
+
var import_node_assert2 = __toESM(require("assert"));
|
|
216
|
+
var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
217
|
+
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () {
|
|
218
|
+
const Jimp = yield getJimp();
|
|
219
|
+
const image = new Jimp(imageWidth2, imageHeight2, 0);
|
|
220
|
+
const colors = [
|
|
221
|
+
{ rect: 4278190335, text: 4294967295 }
|
|
222
|
+
// red, white
|
|
223
|
+
// { rect: 0x0000ffff, text: 0xffffffff }, // blue, white
|
|
224
|
+
// { rect: 0x8b4513ff, text: 0xffffffff }, // brown, white
|
|
225
|
+
];
|
|
226
|
+
const boxPadding = 5;
|
|
227
|
+
for (let index = 0; index < elements2.length; index++) {
|
|
228
|
+
const element = elements2[index];
|
|
229
|
+
const color = colors[index % colors.length];
|
|
230
|
+
const paddedRect = {
|
|
231
|
+
left: Math.max(0, element.rect.left - boxPadding),
|
|
232
|
+
top: Math.max(0, element.rect.top - boxPadding),
|
|
233
|
+
width: Math.min(
|
|
234
|
+
imageWidth2 - element.rect.left,
|
|
235
|
+
element.rect.width + boxPadding * 2
|
|
236
|
+
),
|
|
237
|
+
height: Math.min(
|
|
238
|
+
imageHeight2 - element.rect.top,
|
|
239
|
+
element.rect.height + boxPadding * 2
|
|
240
|
+
)
|
|
241
|
+
};
|
|
242
|
+
image.scan(
|
|
243
|
+
paddedRect.left,
|
|
244
|
+
paddedRect.top,
|
|
245
|
+
paddedRect.width,
|
|
246
|
+
paddedRect.height,
|
|
247
|
+
function(x, y, idx) {
|
|
248
|
+
if (x === paddedRect.left || x === paddedRect.left + paddedRect.width - 1 || y === paddedRect.top || y === paddedRect.top + paddedRect.height - 1) {
|
|
249
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
250
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
251
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
252
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
const textWidth = element.indexId.toString().length * 8;
|
|
257
|
+
const textHeight = 12;
|
|
258
|
+
const rectWidth = textWidth + 5;
|
|
259
|
+
const rectHeight = textHeight + 4;
|
|
260
|
+
let rectX = paddedRect.left - rectWidth;
|
|
261
|
+
let rectY = paddedRect.top + paddedRect.height / 2 - textHeight / 2 - 2;
|
|
262
|
+
const checkOverlap = (x, y) => {
|
|
263
|
+
return elements2.slice(0, index).some((otherElement) => {
|
|
264
|
+
return x < otherElement.rect.left + otherElement.rect.width && x + rectWidth > otherElement.rect.left && y < otherElement.rect.top + otherElement.rect.height && y + rectHeight > otherElement.rect.top;
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
const isWithinBounds = (x, y) => {
|
|
268
|
+
return x >= 0 && x + rectWidth <= imageWidth2 && y >= 0 && y + rectHeight <= imageHeight2;
|
|
269
|
+
};
|
|
270
|
+
if (checkOverlap(rectX, rectY) || !isWithinBounds(rectX, rectY)) {
|
|
271
|
+
if (!checkOverlap(paddedRect.left, paddedRect.top - rectHeight - 2) && isWithinBounds(paddedRect.left, paddedRect.top - rectHeight - 2)) {
|
|
272
|
+
rectX = paddedRect.left;
|
|
273
|
+
rectY = paddedRect.top - rectHeight - 2;
|
|
274
|
+
} else if (!checkOverlap(
|
|
275
|
+
paddedRect.left,
|
|
276
|
+
paddedRect.top + paddedRect.height + 2
|
|
277
|
+
) && isWithinBounds(
|
|
278
|
+
paddedRect.left,
|
|
279
|
+
paddedRect.top + paddedRect.height + 2
|
|
280
|
+
)) {
|
|
281
|
+
rectX = paddedRect.left;
|
|
282
|
+
rectY = paddedRect.top + paddedRect.height + 2;
|
|
283
|
+
} else if (!checkOverlap(
|
|
284
|
+
paddedRect.left + paddedRect.width + 2,
|
|
285
|
+
paddedRect.top
|
|
286
|
+
) && isWithinBounds(paddedRect.left + paddedRect.width + 2, paddedRect.top)) {
|
|
287
|
+
rectX = paddedRect.left + paddedRect.width + 2;
|
|
288
|
+
rectY = paddedRect.top;
|
|
289
|
+
} else {
|
|
290
|
+
rectX = paddedRect.left;
|
|
291
|
+
rectY = paddedRect.top + 2;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
image.scan(rectX, rectY, rectWidth, rectHeight, function(x, y, idx) {
|
|
295
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
296
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
297
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
298
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
299
|
+
});
|
|
300
|
+
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
301
|
+
image.print(
|
|
302
|
+
font,
|
|
303
|
+
rectX,
|
|
304
|
+
rectY,
|
|
305
|
+
{
|
|
306
|
+
text: element.indexId.toString(),
|
|
307
|
+
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
308
|
+
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
309
|
+
},
|
|
310
|
+
rectWidth,
|
|
311
|
+
rectHeight
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
return image.getBufferAsync(Jimp.MIME_PNG);
|
|
315
|
+
});
|
|
316
|
+
return createPngOverlay(elements, imageWidth, imageHeight);
|
|
317
|
+
};
|
|
318
|
+
var compositeElementInfoImg = (options) => __async(void 0, null, function* () {
|
|
319
|
+
const Jimp = yield getJimp();
|
|
320
|
+
const { inputImgBase64, elementsPositionInfo } = options;
|
|
321
|
+
const imageBuffer = yield bufferFromBase64(inputImgBase64);
|
|
322
|
+
const { width, height } = yield imageInfo(imageBuffer);
|
|
323
|
+
if (!width || !height) {
|
|
324
|
+
throw Error("Image processing failed because width or height is undefined");
|
|
325
|
+
}
|
|
326
|
+
const svgOverlay = yield createSvgOverlay(
|
|
327
|
+
elementsPositionInfo,
|
|
328
|
+
width,
|
|
329
|
+
height
|
|
330
|
+
);
|
|
331
|
+
return yield Jimp.read(imageBuffer).then((image) => __async(void 0, null, function* () {
|
|
332
|
+
const svgImage = yield Jimp.read(svgOverlay);
|
|
333
|
+
return image.composite(svgImage, 0, 0, {
|
|
334
|
+
mode: Jimp.BLEND_SOURCE_OVER,
|
|
335
|
+
opacitySource: 1,
|
|
336
|
+
opacityDest: 1
|
|
337
|
+
});
|
|
338
|
+
})).then((compositeImage) => {
|
|
339
|
+
return compositeImage.getBufferAsync(Jimp.MIME_PNG);
|
|
340
|
+
}).then((buffer) => {
|
|
341
|
+
return buffer.toString("base64");
|
|
342
|
+
}).catch((error) => {
|
|
343
|
+
throw error;
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
var processImageElementInfo = (options) => __async(void 0, null, function* () {
|
|
347
|
+
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
348
|
+
(0, import_node_assert2.default)(base64Image, "base64Image is undefined");
|
|
349
|
+
const [
|
|
350
|
+
compositeElementInfoImgBase64,
|
|
351
|
+
compositeElementInfoImgWithoutTextBase64
|
|
352
|
+
] = yield Promise.all([
|
|
353
|
+
compositeElementInfoImg({
|
|
354
|
+
inputImgBase64: options.inputImgBase64,
|
|
355
|
+
elementsPositionInfo: options.elementsPositionInfo
|
|
356
|
+
}),
|
|
357
|
+
compositeElementInfoImg({
|
|
358
|
+
inputImgBase64: options.inputImgBase64,
|
|
359
|
+
elementsPositionInfo: options.elementsPositionInfoWithoutText
|
|
360
|
+
})
|
|
361
|
+
]);
|
|
362
|
+
return {
|
|
363
|
+
compositeElementInfoImgBase64,
|
|
364
|
+
compositeElementInfoImgWithoutTextBase64
|
|
365
|
+
};
|
|
366
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
var src_default = {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/utils.ts
|
|
21
|
+
var utils_exports = {};
|
|
22
|
+
__export(utils_exports, {
|
|
23
|
+
ifInBrowser: () => ifInBrowser,
|
|
24
|
+
uuid: () => uuid
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(utils_exports);
|
|
27
|
+
var import_node_crypto = require("crypto");
|
|
28
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
29
|
+
function uuid() {
|
|
30
|
+
if (ifInBrowser) {
|
|
31
|
+
return Math.random().toString(36).substring(2, 15);
|
|
32
|
+
}
|
|
33
|
+
return (0, import_node_crypto.randomUUID)();
|
|
34
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const TEXT_SIZE_THRESHOLD = 9;
|
|
2
|
+
declare const TEXT_MAX_SIZE = 40;
|
|
3
|
+
declare const CONTAINER_MINI_HEIGHT = 3;
|
|
4
|
+
declare const CONTAINER_MINI_WIDTH = 3;
|
|
5
|
+
declare enum NodeType {
|
|
6
|
+
CONTAINER = "CONTAINER Node",
|
|
7
|
+
FORM_ITEM = "FORM_ITEM Node",
|
|
8
|
+
BUTTON = "BUTTON Node",
|
|
9
|
+
IMG = "IMG Node",
|
|
10
|
+
TEXT = "TEXT Node"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { CONTAINER_MINI_HEIGHT, CONTAINER_MINI_WIDTH, NodeType, TEXT_MAX_SIZE, TEXT_SIZE_THRESHOLD };
|
package/dist/es/fs.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface PkgInfo {
|
|
2
|
+
name: string;
|
|
3
|
+
version: string;
|
|
4
|
+
dir: string;
|
|
5
|
+
}
|
|
6
|
+
declare function getRunningPkgInfo(dir?: string): PkgInfo | null;
|
|
7
|
+
/**
|
|
8
|
+
* Find the nearest package.json file recursively
|
|
9
|
+
* @param {string} dir - Home directory
|
|
10
|
+
* @returns {string|null} - The most recent package.json file path or null
|
|
11
|
+
*/
|
|
12
|
+
declare function findNearestPackageJson(dir: string): string | null;
|
|
13
|
+
|
|
14
|
+
export { findNearestPackageJson, getRunningPkgInfo };
|
package/dist/es/fs.js
CHANGED
|
@@ -3,7 +3,11 @@ import assert from "assert";
|
|
|
3
3
|
import { existsSync, readFileSync } from "fs";
|
|
4
4
|
import { dirname, join } from "path";
|
|
5
5
|
var pkg;
|
|
6
|
-
|
|
6
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
7
|
+
function getRunningPkgInfo(dir) {
|
|
8
|
+
if (ifInBrowser) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
7
11
|
if (pkg) {
|
|
8
12
|
return pkg;
|
|
9
13
|
}
|
|
@@ -16,7 +20,7 @@ function getMidscenePkgInfo(dir) {
|
|
|
16
20
|
return pkg;
|
|
17
21
|
}
|
|
18
22
|
return {
|
|
19
|
-
name: "midscene-unknown-
|
|
23
|
+
name: "midscene-unknown-package-name",
|
|
20
24
|
version: "0.0.0",
|
|
21
25
|
dir: pkgDir
|
|
22
26
|
};
|
|
@@ -34,5 +38,5 @@ function findNearestPackageJson(dir) {
|
|
|
34
38
|
}
|
|
35
39
|
export {
|
|
36
40
|
findNearestPackageJson,
|
|
37
|
-
|
|
41
|
+
getRunningPkgInfo
|
|
38
42
|
};
|