@fleox/agentphoto 0.0.0-stage → 0.1.1
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/LICENSE +143 -0
- package/README.md +334 -2
- package/changelog.json +35 -0
- package/dist/agent/agentphoto.mjs +3354 -0
- package/dist/agent/chunks/assets-BZVUUHjg.mjs +151 -0
- package/dist/agent/chunks/catalog-DIZ5dnH5.mjs +953 -0
- package/dist/agent/chunks/context-CNpRboiC.mjs +75 -0
- package/dist/agent/chunks/doc-D_v_yE07.mjs +512 -0
- package/dist/agent/chunks/export-aNJcPNbJ.mjs +140 -0
- package/dist/agent/chunks/geometry-BKIhUO9x.mjs +315 -0
- package/dist/agent/chunks/ids-DAmiNwd9.mjs +28 -0
- package/dist/agent/chunks/imageInfo-QxAfu8R6.mjs +173 -0
- package/dist/agent/chunks/jsonPath-B7vAPXsi.mjs +155 -0
- package/dist/agent/chunks/library-C_EXiphj.mjs +16 -0
- package/dist/agent/chunks/models-CxrjmA0W.mjs +387 -0
- package/dist/agent/chunks/output-3F12IR9O.mjs +130 -0
- package/dist/agent/chunks/package-Cu1GyvKL.mjs +71 -0
- package/dist/agent/chunks/project-o1DH2nOw.mjs +200 -0
- package/dist/agent/chunks/rolldown-runtime-CMQPkI6i.mjs +18 -0
- package/dist/agent/chunks/server-I0trf5tM.mjs +241 -0
- package/dist/agent/chunks/templates-BJ2me4XZ.mjs +61 -0
- package/dist/agent/chunks/tools-DKaAPEV5.mjs +1419 -0
- package/dist/agent/chunks/tree-D8qZKPAb.mjs +127 -0
- package/dist/agent/chunks/types-DOK7tAAu.mjs +30 -0
- package/dist/agent/chunks/validate-CXPFB-i6.mjs +936 -0
- package/dist/engine/agentphoto-engine.js +2833 -0
- package/dist/engine/chunks/transformers.web-D5Vsg6-A.js +18494 -0
- package/dist/engine/ort/ort-wasm-simd-threaded.asyncify.mjs +124 -0
- package/dist/engine/ort/ort-wasm-simd-threaded.asyncify.wasm +0 -0
- package/dist/studio/assets/index-CmOzzoXv.css +1 -0
- package/dist/studio/assets/index-D3Iw5q8v.js +28 -0
- package/dist/studio/assets/transformers.web-osEqoMhg.js +38 -0
- package/dist/studio/index.html +15 -0
- package/dist/studio/library/LICENSES.md +57 -0
- package/dist/studio/library/fonts/OFL.txt +90 -0
- package/dist/studio/library/fonts/anton/anton-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/anton/anton-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/archivo-black/archivo-black-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/archivo-black/archivo-black-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/bebas-neue/bebas-neue-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/bebas-neue/bebas-neue-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/dm-serif/dm-serif-display-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/dm-serif/dm-serif-display-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/lobster/lobster-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/lobster/lobster-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/pacifico/pacifico-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/pacifico/pacifico-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-400-italic.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-400-italic.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/LICENSE.txt +202 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-500-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-ext-500-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/images/city.jpg +0 -0
- package/dist/studio/library/images/food.jpg +0 -0
- package/dist/studio/library/images/landscape.jpg +0 -0
- package/dist/studio/library/images/pet.jpg +0 -0
- package/dist/studio/library/images/portrait.jpg +0 -0
- package/dist/studio/library/images/product.jpg +0 -0
- package/dist/studio/library/index.json +752 -0
- package/dist/studio/templates/avatar-pro/assets/masks/photo-cutout.png +0 -0
- package/dist/studio/templates/avatar-pro/photo.json +13 -0
- package/dist/studio/templates/avatar-pro/preview.jpg +0 -0
- package/dist/studio/templates/avatar-pro/template.json +1 -0
- package/dist/studio/templates/index.json +1 -0
- package/dist/studio/templates/og-article/photo.json +13 -0
- package/dist/studio/templates/og-article/preview.jpg +0 -0
- package/dist/studio/templates/og-article/template.json +1 -0
- package/dist/studio/templates/polaroid/photo.json +13 -0
- package/dist/studio/templates/polaroid/preview.jpg +0 -0
- package/dist/studio/templates/polaroid/template.json +1 -0
- package/dist/studio/templates/product-white/assets/masks/photo-cutout.png +0 -0
- package/dist/studio/templates/product-white/photo.json +72 -0
- package/dist/studio/templates/product-white/preview.jpg +0 -0
- package/dist/studio/templates/product-white/template.json +1 -0
- package/dist/studio/templates/sale-banner/photo.json +125 -0
- package/dist/studio/templates/sale-banner/preview.jpg +0 -0
- package/dist/studio/templates/sale-banner/template.json +1 -0
- package/dist/studio/templates/social-quote/photo.json +13 -0
- package/dist/studio/templates/social-quote/preview.jpg +0 -0
- package/dist/studio/templates/social-quote/template.json +1 -0
- package/dist/studio/templates/story-promo/photo.json +13 -0
- package/dist/studio/templates/story-promo/preview.jpg +0 -0
- package/dist/studio/templates/story-promo/template.json +1 -0
- package/dist/studio/templates/youtube-thumbnail/photo.json +12 -0
- package/dist/studio/templates/youtube-thumbnail/preview.jpg +0 -0
- package/dist/studio/templates/youtube-thumbnail/template.json +1 -0
- package/library/LICENSES.md +57 -0
- package/library/fonts/OFL.txt +90 -0
- package/library/fonts/anton/anton-latin-400-normal.woff2 +0 -0
- package/library/fonts/anton/anton-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/archivo-black/archivo-black-latin-400-normal.woff2 +0 -0
- package/library/fonts/archivo-black/archivo-black-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/bebas-neue/bebas-neue-latin-400-normal.woff2 +0 -0
- package/library/fonts/bebas-neue/bebas-neue-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-700-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/dm-serif/dm-serif-display-latin-400-normal.woff2 +0 -0
- package/library/fonts/dm-serif/dm-serif-display-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-600-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-700-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-800-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-700-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/lobster/lobster-latin-400-normal.woff2 +0 -0
- package/library/fonts/lobster/lobster-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-700-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-800-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-800-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-400-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-600-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/pacifico/pacifico-latin-400-normal.woff2 +0 -0
- package/library/fonts/pacifico/pacifico-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-400-italic.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-700-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-400-italic.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-400-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-600-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-700-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/LICENSE.txt +202 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-400-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-700-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-500-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-700-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-ext-500-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/library/images/city.jpg +0 -0
- package/library/images/food.jpg +0 -0
- package/library/images/landscape.jpg +0 -0
- package/library/images/pet.jpg +0 -0
- package/library/images/portrait.jpg +0 -0
- package/library/images/product.jpg +0 -0
- package/library/index.json +752 -0
- package/package.json +65 -4
- package/plugin/.claude-plugin/plugin.json +19 -0
- package/plugin/skills/agentphoto-cutout/SKILL.md +137 -0
- package/plugin/skills/agentphoto-design/SKILL.md +104 -0
- package/plugin/skills/agentphoto-design/references/layouts.md +60 -0
- package/plugin/skills/agentphoto-publish/SKILL.md +79 -0
- package/plugin/skills/agentphoto-retouch/SKILL.md +144 -0
- package/plugin/skills/agentphoto-retouch/references/filters.md +51 -0
- package/plugin/skills/agentphoto-retouch/references/patch-ops.md +67 -0
- package/plugin/skills/agentphoto-retouch/references/recipes.md +129 -0
- package/templates/avatar-pro/assets/masks/photo-cutout.png +0 -0
- package/templates/avatar-pro/photo.json +13 -0
- package/templates/avatar-pro/preview.jpg +0 -0
- package/templates/avatar-pro/template.json +1 -0
- package/templates/og-article/photo.json +13 -0
- package/templates/og-article/preview.jpg +0 -0
- package/templates/og-article/template.json +1 -0
- package/templates/polaroid/photo.json +13 -0
- package/templates/polaroid/preview.jpg +0 -0
- package/templates/polaroid/template.json +1 -0
- package/templates/product-white/assets/masks/photo-cutout.png +0 -0
- package/templates/product-white/photo.json +72 -0
- package/templates/product-white/preview.jpg +0 -0
- package/templates/product-white/template.json +1 -0
- package/templates/sale-banner/photo.json +125 -0
- package/templates/sale-banner/preview.jpg +0 -0
- package/templates/sale-banner/template.json +1 -0
- package/templates/social-quote/photo.json +13 -0
- package/templates/social-quote/preview.jpg +0 -0
- package/templates/social-quote/template.json +1 -0
- package/templates/story-promo/photo.json +13 -0
- package/templates/story-promo/preview.jpg +0 -0
- package/templates/story-promo/template.json +1 -0
- package/templates/youtube-thumbnail/photo.json +12 -0
- package/templates/youtube-thumbnail/preview.jpg +0 -0
- package/templates/youtube-thumbnail/template.json +1 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createRequire as __apRequire } from "node:module";
|
|
2
|
+
import { fileURLToPath as __apPath } from "node:url";
|
|
3
|
+
import { dirname as __apDirname } from "node:path";
|
|
4
|
+
__apRequire(import.meta.url);
|
|
5
|
+
const __filename = __apPath(import.meta.url);
|
|
6
|
+
__apDirname(__filename);
|
|
7
|
+
import { n as version } from "./package-Cu1GyvKL.mjs";
|
|
8
|
+
// src/core/fs.ts
|
|
9
|
+
var FsError = class extends Error {
|
|
10
|
+
code;
|
|
11
|
+
name = "FsError";
|
|
12
|
+
constructor(code, message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function normalizePath(path) {
|
|
18
|
+
const p = path.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+/g, "/");
|
|
19
|
+
if (p.startsWith("/") || p.split("/").includes("..") || /^[a-zA-Z]:/.test(p)) throw new FsError("invalidPath", `Invalid project path: ${path}`);
|
|
20
|
+
return p;
|
|
21
|
+
}
|
|
22
|
+
// src/ops/context.ts
|
|
23
|
+
var AGENTPHOTO_VERSION = version;
|
|
24
|
+
var OpsError = class extends Error {
|
|
25
|
+
code;
|
|
26
|
+
hint;
|
|
27
|
+
details;
|
|
28
|
+
name = "OpsError";
|
|
29
|
+
constructor(code, message, hint, details) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.hint = hint;
|
|
33
|
+
this.details = details;
|
|
34
|
+
}
|
|
35
|
+
toJSON() {
|
|
36
|
+
return {
|
|
37
|
+
code: this.code,
|
|
38
|
+
message: this.message,
|
|
39
|
+
...this.hint ? { hint: this.hint } : {},
|
|
40
|
+
...this.details !== void 0 ? { details: this.details } : {}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function validationError(message, issues, hint) {
|
|
45
|
+
const errors = issues.filter((i) => i.severity === "error");
|
|
46
|
+
const first = errors[0];
|
|
47
|
+
return new OpsError("validation", `${message} ${errors.length} error${errors.length === 1 ? "" : "s"}${first ? `, first: ${first.path ? `${first.path}: ` : ""}${first.message}` : ""}`, hint ?? first?.hint ?? "Fix the listed issues (details.issues) and retry.", { issues });
|
|
48
|
+
}
|
|
49
|
+
var hasErrors = (issues) => issues.some((i) => i.severity === "error");
|
|
50
|
+
function nowIso() {
|
|
51
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
52
|
+
}
|
|
53
|
+
async function readJson(fs, path, what) {
|
|
54
|
+
let text;
|
|
55
|
+
try {
|
|
56
|
+
text = await fs.readText(path);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
if (e instanceof FsError && e.code === "notFound") throw new OpsError("notFound", `${what} not found (${path}).`, path === "agentphoto.json" || path === "photo.json" ? "This folder is not an AgentPhoto project: create one with `agentphoto new <dir>` (tool project_create) or pass --project <dir>." : void 0);
|
|
59
|
+
throw new OpsError("io", `Cannot read ${path}: ${e.message}`);
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(text);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
throw new OpsError("invalid", `${path} is not valid JSON: ${e.message}`, `Fix the JSON syntax in ${path} (trailing commas and comments are not allowed).`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function writeJson(fs, path, value) {
|
|
68
|
+
await fs.writeText(path, `${JSON.stringify(value, null, 2)}\n`);
|
|
69
|
+
}
|
|
70
|
+
async function readJsonOr(fs, path, fallback) {
|
|
71
|
+
if (!await fs.exists(path)) return fallback;
|
|
72
|
+
return readJson(fs, path, path);
|
|
73
|
+
}
|
|
74
|
+
var KB = (bytes) => Math.round(bytes / 1024 * 10) / 10;
|
|
75
|
+
export { nowIso as a, validationError as c, normalizePath as d, hasErrors as i, writeJson as l, KB as n, readJson as o, OpsError as r, readJsonOr as s, AGENTPHOTO_VERSION as t, FsError as u };
|
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { createRequire as __apRequire } from "node:module";
|
|
2
|
+
import { fileURLToPath as __apPath } from "node:url";
|
|
3
|
+
import { dirname as __apDirname } from "node:path";
|
|
4
|
+
__apRequire(import.meta.url);
|
|
5
|
+
const __filename = __apPath(import.meta.url);
|
|
6
|
+
__apDirname(__filename);
|
|
7
|
+
import { t as __exportAll } from "./rolldown-runtime-CMQPkI6i.mjs";
|
|
8
|
+
import { i as uniqueId, n as isValidId } from "./ids-DAmiNwd9.mjs";
|
|
9
|
+
import { c as setAt, i as getAt, n as clone, o as mergePatch, r as deepEqual, s as removeAt, t as PathError } from "./jsonPath-B7vAPXsi.mjs";
|
|
10
|
+
import { n as isLibraryRef } from "./library-C_EXiphj.mjs";
|
|
11
|
+
import { i as validateDoc, l as didYouMean, r as normalizeDoc } from "./validate-CXPFB-i6.mjs";
|
|
12
|
+
import { a as isShapeMask, n as hasBox } from "./types-DOK7tAAu.mjs";
|
|
13
|
+
import { i as hasErrors, o as readJson, r as OpsError } from "./context-CNpRboiC.mjs";
|
|
14
|
+
import { c as rotateDoc, i as flipDoc, l as scaleDoc, n as cropDoc, r as fitLayerDoc, s as resizeCanvasDoc, t as ANCHORS } from "./geometry-BKIhUO9x.mjs";
|
|
15
|
+
import { a as layerDetail, i as isDescendant, l as walkLayers, n as findLayer, o as maskKind, r as flattenLayers, t as allLayerIds } from "./tree-D8qZKPAb.mjs";
|
|
16
|
+
import { n as DOC_FILE, o as saveDoc } from "./project-o1DH2nOw.mjs";
|
|
17
|
+
// src/ops/doc.ts
|
|
18
|
+
var doc_exports = /* @__PURE__ */ __exportAll({
|
|
19
|
+
DOC_FILE: () => DOC_FILE,
|
|
20
|
+
GEOMETRY_OPS: () => GEOMETRY_OPS,
|
|
21
|
+
PATCH_OPS: () => PATCH_OPS,
|
|
22
|
+
applyDoc: () => applyDoc,
|
|
23
|
+
applyPatchOps: () => applyPatchOps,
|
|
24
|
+
diffDocs: () => diffDocs,
|
|
25
|
+
findLayer: () => findLayer,
|
|
26
|
+
flattenLayers: () => flattenLayers,
|
|
27
|
+
getDoc: () => getDoc,
|
|
28
|
+
layerDetail: () => layerDetail,
|
|
29
|
+
maskKind: () => maskKind,
|
|
30
|
+
patchDoc: () => patchDoc,
|
|
31
|
+
sourceSizes: () => sourceSizes,
|
|
32
|
+
summarizeDoc: () => summarizeDoc
|
|
33
|
+
});
|
|
34
|
+
async function getDoc(fs) {
|
|
35
|
+
return normalizeDoc(await readJson(fs, DOC_FILE, "The document"));
|
|
36
|
+
}
|
|
37
|
+
function summarizeLayer(l, f) {
|
|
38
|
+
const { parent: _p, depth: _d, index: _i, locked: _l, ...rest } = f;
|
|
39
|
+
const out = { ...rest };
|
|
40
|
+
if (l.type === "group") out.children = [...l.children ?? []].reverse().map((c, k, arr) => summarizeLayer(c, flatOf(c, l, arr.length - 1 - k)));
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
var flatOf = (l, parent, index) => {
|
|
44
|
+
return {
|
|
45
|
+
...flattenLayers({ layers: [l] })[0],
|
|
46
|
+
parent: parent?.id ?? null,
|
|
47
|
+
index
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
function summarizeDoc(doc) {
|
|
51
|
+
const byType = {};
|
|
52
|
+
let hidden = 0;
|
|
53
|
+
let masks = 0;
|
|
54
|
+
let filters = 0;
|
|
55
|
+
const sources = /* @__PURE__ */ new Set();
|
|
56
|
+
walkLayers(doc.layers, (l) => {
|
|
57
|
+
byType[l.type] = (byType[l.type] ?? 0) + 1;
|
|
58
|
+
if (l.visible === false) hidden++;
|
|
59
|
+
if (l.mask) masks++;
|
|
60
|
+
filters += l.filters?.length ?? 0;
|
|
61
|
+
if (l.type === "image") sources.add(l.src);
|
|
62
|
+
if (l.mask && "src" in l.mask) sources.add(l.mask.src);
|
|
63
|
+
});
|
|
64
|
+
const bg = doc.canvas?.background;
|
|
65
|
+
return {
|
|
66
|
+
id: doc.id,
|
|
67
|
+
...doc.title ? { title: doc.title } : {},
|
|
68
|
+
canvas: {
|
|
69
|
+
width: doc.canvas?.width,
|
|
70
|
+
height: doc.canvas?.height,
|
|
71
|
+
background: bg === void 0 || bg === null ? "transparent" : typeof bg === "string" ? bg : `${bg.type} gradient`
|
|
72
|
+
},
|
|
73
|
+
layers: [...Array.isArray(doc.layers) ? doc.layers : []].reverse().map((l, k, arr) => summarizeLayer(l, flatOf(l, null, arr.length - 1 - k))),
|
|
74
|
+
counts: {
|
|
75
|
+
layers: Object.values(byType).reduce((a, b) => a + b, 0),
|
|
76
|
+
byType,
|
|
77
|
+
hidden,
|
|
78
|
+
masks,
|
|
79
|
+
filters
|
|
80
|
+
},
|
|
81
|
+
sources: [...sources],
|
|
82
|
+
...doc.export ? { export: doc.export } : {}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function diffDocs(before, after) {
|
|
86
|
+
const map = (d) => {
|
|
87
|
+
const m = /* @__PURE__ */ new Map();
|
|
88
|
+
if (!d) return m;
|
|
89
|
+
const visit = (layers, parent) => {
|
|
90
|
+
if (!Array.isArray(layers)) return;
|
|
91
|
+
layers.forEach((l, i) => {
|
|
92
|
+
if (!l || typeof l !== "object") return;
|
|
93
|
+
m.set(l.id, {
|
|
94
|
+
layer: l,
|
|
95
|
+
where: `${parent}[${i}]`
|
|
96
|
+
});
|
|
97
|
+
if (l.type === "group") visit(l.children, l.id);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
visit(d.layers, "root");
|
|
101
|
+
return m;
|
|
102
|
+
};
|
|
103
|
+
const a = map(before);
|
|
104
|
+
const b = map(after);
|
|
105
|
+
const added = [...b.keys()].filter((id) => !a.has(id));
|
|
106
|
+
const removed = [...a.keys()].filter((id) => !b.has(id));
|
|
107
|
+
const changed = [];
|
|
108
|
+
const movedIds = /* @__PURE__ */ new Set();
|
|
109
|
+
const parentOf = (where) => where.slice(0, where.lastIndexOf("["));
|
|
110
|
+
const indexOf = (where) => Number(where.slice(where.lastIndexOf("[") + 1, -1));
|
|
111
|
+
const sameParent = /* @__PURE__ */ new Map();
|
|
112
|
+
for (const [id, { layer, where }] of b) {
|
|
113
|
+
const old = a.get(id);
|
|
114
|
+
if (!old) continue;
|
|
115
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(old.layer), ...Object.keys(layer)]);
|
|
116
|
+
keys.delete("children");
|
|
117
|
+
const fields = [...keys].filter((k) => !deepEqual(old.layer[k], layer[k]));
|
|
118
|
+
if (fields.length) changed.push({
|
|
119
|
+
id,
|
|
120
|
+
fields
|
|
121
|
+
});
|
|
122
|
+
if (parentOf(old.where) !== parentOf(where)) movedIds.add(id);
|
|
123
|
+
else {
|
|
124
|
+
const list = sameParent.get(parentOf(where)) ?? [];
|
|
125
|
+
list.push({
|
|
126
|
+
id,
|
|
127
|
+
from: indexOf(old.where),
|
|
128
|
+
to: indexOf(where)
|
|
129
|
+
});
|
|
130
|
+
sameParent.set(parentOf(where), list);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
for (const list of sameParent.values()) {
|
|
134
|
+
list.sort((x, y) => x.from - y.from);
|
|
135
|
+
const keep = longestIncreasing(list.map((x) => x.to));
|
|
136
|
+
list.forEach((x, k) => {
|
|
137
|
+
if (!keep.has(k)) movedIds.add(x.id);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const realMoves = [...b.keys()].filter((id) => movedIds.has(id)).map((id) => ({
|
|
141
|
+
id,
|
|
142
|
+
from: a.get(id).where,
|
|
143
|
+
to: b.get(id).where
|
|
144
|
+
}));
|
|
145
|
+
const sections = [
|
|
146
|
+
"canvas",
|
|
147
|
+
"title",
|
|
148
|
+
"description",
|
|
149
|
+
"export",
|
|
150
|
+
"id",
|
|
151
|
+
"version"
|
|
152
|
+
].filter((k) => !deepEqual(before?.[k], after[k]));
|
|
153
|
+
const parts = [];
|
|
154
|
+
if (added.length) parts.push(`added ${added.join(", ")}`);
|
|
155
|
+
if (removed.length) parts.push(`removed ${removed.join(", ")}`);
|
|
156
|
+
if (changed.length) parts.push(`changed ${changed.map((c) => `${c.id} (${c.fields.join(", ")})`).join("; ")}`);
|
|
157
|
+
if (realMoves.length) parts.push(`moved ${realMoves.map((m) => m.id).join(", ")}`);
|
|
158
|
+
if (sections.length) parts.push(`updated ${sections.join(", ")}`);
|
|
159
|
+
const empty = !parts.length;
|
|
160
|
+
return {
|
|
161
|
+
empty,
|
|
162
|
+
layers: {
|
|
163
|
+
added,
|
|
164
|
+
removed,
|
|
165
|
+
changed,
|
|
166
|
+
moved: realMoves
|
|
167
|
+
},
|
|
168
|
+
sections,
|
|
169
|
+
summary: empty ? "No change." : `${parts.join("; ")}.`
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function longestIncreasing(seq) {
|
|
173
|
+
const len = [];
|
|
174
|
+
const prev = [];
|
|
175
|
+
let best = -1;
|
|
176
|
+
for (let i = 0; i < seq.length; i++) {
|
|
177
|
+
len[i] = 1;
|
|
178
|
+
prev[i] = -1;
|
|
179
|
+
for (let j = 0; j < i; j++) if (seq[j] < seq[i] && len[j] + 1 > len[i]) {
|
|
180
|
+
len[i] = len[j] + 1;
|
|
181
|
+
prev[i] = j;
|
|
182
|
+
}
|
|
183
|
+
if (best < 0 || len[i] > len[best]) best = i;
|
|
184
|
+
}
|
|
185
|
+
const out = /* @__PURE__ */ new Set();
|
|
186
|
+
for (let i = best; i >= 0; i = prev[i]) out.add(i);
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
async function applyDoc(fs, next, opts = {}) {
|
|
190
|
+
const before = await fs.exists("photo.json") ? await getDoc(fs).catch(() => null) : null;
|
|
191
|
+
const doc = normalizeDoc(next);
|
|
192
|
+
const issues = validateDoc(doc);
|
|
193
|
+
const plan = diffDocs(before, doc);
|
|
194
|
+
if (hasErrors(issues) || opts.dryRun || plan.empty) return {
|
|
195
|
+
plan,
|
|
196
|
+
issues,
|
|
197
|
+
applied: false
|
|
198
|
+
};
|
|
199
|
+
await saveDoc(fs, doc, { allowInvalid: false });
|
|
200
|
+
return {
|
|
201
|
+
plan,
|
|
202
|
+
issues,
|
|
203
|
+
applied: true
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
var PATCH_OPS = [
|
|
207
|
+
"set",
|
|
208
|
+
"merge",
|
|
209
|
+
"remove",
|
|
210
|
+
"addLayer",
|
|
211
|
+
"updateLayer",
|
|
212
|
+
"removeLayer",
|
|
213
|
+
"moveLayer",
|
|
214
|
+
"duplicateLayer",
|
|
215
|
+
"groupLayers",
|
|
216
|
+
"ungroup",
|
|
217
|
+
"addFilter",
|
|
218
|
+
"updateFilter",
|
|
219
|
+
"removeFilter",
|
|
220
|
+
"setMask",
|
|
221
|
+
"crop",
|
|
222
|
+
"resizeCanvas",
|
|
223
|
+
"resizeImage",
|
|
224
|
+
"rotateCanvas",
|
|
225
|
+
"flipCanvas",
|
|
226
|
+
"fitLayer"
|
|
227
|
+
];
|
|
228
|
+
var GEOMETRY_OPS = /* @__PURE__ */ new Set([
|
|
229
|
+
"resizeImage",
|
|
230
|
+
"rotateCanvas",
|
|
231
|
+
"flipCanvas",
|
|
232
|
+
"fitLayer"
|
|
233
|
+
]);
|
|
234
|
+
var isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
235
|
+
function opError(i, code, message, hint) {
|
|
236
|
+
return new OpsError(code === "notFound" ? "notFound" : code, `ops[${i}]: ${message}`, hint, { opIndex: i });
|
|
237
|
+
}
|
|
238
|
+
function need(doc, id, i) {
|
|
239
|
+
if (typeof id !== "string") throw opError(i, "invalid", "\"id\" (a layer id) is required.");
|
|
240
|
+
const loc = findLayer(doc.layers, id);
|
|
241
|
+
if (!loc) throw opError(i, "notFound", `No layer "${id}".`, `${didYouMean(id, allLayerIds(doc))}doc_summary / layer_list list the layer ids.`);
|
|
242
|
+
return loc;
|
|
243
|
+
}
|
|
244
|
+
function target(doc, pos, i, moving) {
|
|
245
|
+
const ref = pos.above ?? pos.below;
|
|
246
|
+
if (ref !== void 0) {
|
|
247
|
+
const loc = need(doc, ref, i);
|
|
248
|
+
if (moving && (loc.layer === moving || isDescendant(moving, ref))) throw opError(i, "invalid", `Cannot place "${moving.id}" relative to itself or its own child.`);
|
|
249
|
+
return {
|
|
250
|
+
container: loc.container,
|
|
251
|
+
index: pos.above !== void 0 ? loc.index + 1 : loc.index
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
let container = doc.layers;
|
|
255
|
+
if (pos.parent !== void 0 && pos.parent !== null) {
|
|
256
|
+
const loc = need(doc, pos.parent, i);
|
|
257
|
+
if (loc.layer.type !== "group") throw opError(i, "invalid", `"${pos.parent}" is a ${loc.layer.type} layer, not a group.`, "Only group layers have children (groupLayers creates one).");
|
|
258
|
+
if (moving && (loc.layer === moving || isDescendant(moving, loc.layer.id))) throw opError(i, "invalid", `Cannot move "${moving.id}" into itself.`);
|
|
259
|
+
container = loc.layer.children ??= [];
|
|
260
|
+
}
|
|
261
|
+
const index = pos.index === void 0 ? container.length : Math.max(0, Math.min(container.length, Math.round(pos.index)));
|
|
262
|
+
return {
|
|
263
|
+
container,
|
|
264
|
+
index
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
function checkNewIds(doc, layer, i) {
|
|
268
|
+
const taken = new Set(allLayerIds(doc));
|
|
269
|
+
const visit = (l) => {
|
|
270
|
+
if (!isObj(l)) throw opError(i, "invalid", "layer must be an object.");
|
|
271
|
+
if (!isValidId(l.id)) throw opError(i, "invalid", `Invalid layer id ${JSON.stringify(l.id)}.`, "Ids start with a letter: \"photo\", \"title\", \"badge-2\".");
|
|
272
|
+
if (taken.has(l.id)) throw opError(i, "conflict", `A layer "${l.id}" already exists.`, `Use updateLayer to change it, or another id (e.g. "${uniqueId(l.id, taken)}").`);
|
|
273
|
+
taken.add(l.id);
|
|
274
|
+
if (l.type === "group") (l.children ?? []).forEach(visit);
|
|
275
|
+
};
|
|
276
|
+
visit(layer);
|
|
277
|
+
}
|
|
278
|
+
function renameTree(l, taken, rootId) {
|
|
279
|
+
const copy = clone(l);
|
|
280
|
+
const visit = (x, isRoot) => {
|
|
281
|
+
x.id = isRoot && rootId ? rootId : uniqueId(`${x.id}-copy`, taken);
|
|
282
|
+
taken.add(x.id);
|
|
283
|
+
if (x.type === "group") (x.children ?? []).forEach((c) => visit(c, false));
|
|
284
|
+
};
|
|
285
|
+
visit(copy, true);
|
|
286
|
+
return copy;
|
|
287
|
+
}
|
|
288
|
+
function filtersOf(loc, i) {
|
|
289
|
+
const l = loc.layer;
|
|
290
|
+
if (!Array.isArray(l.filters)) l.filters = [];
|
|
291
|
+
return l.filters;
|
|
292
|
+
}
|
|
293
|
+
function filterIndex(list, index, i, id) {
|
|
294
|
+
if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index >= list.length) throw opError(i, "invalid", `Layer "${id}" has no filter at index ${JSON.stringify(index)} (it has ${list.length}).`, "layer_list / doc_summary list each layer's filters in order.");
|
|
295
|
+
return index;
|
|
296
|
+
}
|
|
297
|
+
function applyPatchOps(doc, ops, sizes = {}) {
|
|
298
|
+
if (!Array.isArray(ops)) throw new OpsError("invalid", "ops must be an array of patch operations.");
|
|
299
|
+
let d = clone(doc);
|
|
300
|
+
ops.forEach((op, i) => {
|
|
301
|
+
if (!isObj(op) || typeof op.op !== "string") throw opError(i, "invalid", "Each op is an object with an \"op\" field.", `Ops: ${PATCH_OPS.join(", ")}.`);
|
|
302
|
+
try {
|
|
303
|
+
switch (op.op) {
|
|
304
|
+
case "set":
|
|
305
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
306
|
+
setAt(d, op.path, clone(op.value));
|
|
307
|
+
break;
|
|
308
|
+
case "merge": {
|
|
309
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
310
|
+
let cur;
|
|
311
|
+
try {
|
|
312
|
+
cur = getAt(d, op.path);
|
|
313
|
+
} catch {
|
|
314
|
+
cur = void 0;
|
|
315
|
+
}
|
|
316
|
+
setAt(d, op.path, mergePatch(cur, op.value));
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case "remove":
|
|
320
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
321
|
+
if (!removeAt(d, op.path)) throw opError(i, "notFound", `Nothing at "${op.path}".`);
|
|
322
|
+
break;
|
|
323
|
+
case "addLayer": {
|
|
324
|
+
if (!isObj(op.layer)) throw opError(i, "invalid", "\"layer\" (a layer object) is required.");
|
|
325
|
+
checkNewIds(d, op.layer, i);
|
|
326
|
+
const t = target(d, op, i);
|
|
327
|
+
t.container.splice(t.index, 0, clone(op.layer));
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
case "updateLayer": {
|
|
331
|
+
const loc = need(d, op.id, i);
|
|
332
|
+
if (!isObj(op.set)) throw opError(i, "invalid", "\"set\" (an object of fields to change) is required.");
|
|
333
|
+
for (const k of [
|
|
334
|
+
"id",
|
|
335
|
+
"type",
|
|
336
|
+
"children"
|
|
337
|
+
]) if (k in op.set) throw opError(i, "invalid", `updateLayer cannot change "${k}".`, k === "children" ? "Use addLayer / moveLayer with parent." : "Remove the layer and add a new one.");
|
|
338
|
+
const next = mergePatch(loc.layer, op.set);
|
|
339
|
+
loc.container[loc.index] = next;
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
case "removeLayer": {
|
|
343
|
+
const loc = need(d, op.id, i);
|
|
344
|
+
loc.container.splice(loc.index, 1);
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
case "moveLayer": {
|
|
348
|
+
const loc = need(d, op.id, i);
|
|
349
|
+
const layer = loc.layer;
|
|
350
|
+
target(d, op, i, layer);
|
|
351
|
+
loc.container.splice(loc.index, 1);
|
|
352
|
+
const t = target(d, op, i, layer);
|
|
353
|
+
t.container.splice(t.index, 0, layer);
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
case "duplicateLayer": {
|
|
357
|
+
const loc = need(d, op.id, i);
|
|
358
|
+
const taken = new Set(allLayerIds(d));
|
|
359
|
+
if (op.newId !== void 0 && (!isValidId(op.newId) || taken.has(op.newId))) throw opError(i, "conflict", `newId ${JSON.stringify(op.newId)} is invalid or taken.`);
|
|
360
|
+
const copy = renameTree(loc.layer, taken, op.newId);
|
|
361
|
+
if (op.offset) {
|
|
362
|
+
const [dx, dy] = op.offset;
|
|
363
|
+
if (!Number.isFinite(dx) || !Number.isFinite(dy)) throw opError(i, "invalid", "offset must be [dx, dy] in px.");
|
|
364
|
+
walkLayers([copy], (l) => {
|
|
365
|
+
if (hasBox(l)) {
|
|
366
|
+
l.x = (l.x ?? 0) + dx;
|
|
367
|
+
l.y = (l.y ?? 0) + dy;
|
|
368
|
+
} else if (l.mask && isShapeMask(l.mask)) {
|
|
369
|
+
l.mask.x += dx;
|
|
370
|
+
l.mask.y += dy;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
if (copy.name) copy.name = `${copy.name} copy`;
|
|
375
|
+
loc.container.splice(loc.index + 1, 0, copy);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "groupLayers": {
|
|
379
|
+
if (!Array.isArray(op.ids) || !op.ids.length) throw opError(i, "invalid", "\"ids\" (layers to group) is required.");
|
|
380
|
+
if (!isValidId(op.id) || allLayerIds(d).includes(op.id)) throw opError(i, "conflict", `Group id ${JSON.stringify(op.id)} is invalid or taken.`);
|
|
381
|
+
const locs = [...new Set(op.ids)].map((id) => need(d, id, i));
|
|
382
|
+
const container = locs[0].container;
|
|
383
|
+
if (locs.some((l) => l.container !== container)) throw opError(i, "invalid", "Grouped layers must be siblings (same parent).");
|
|
384
|
+
const sorted = [...locs].sort((a, b) => a.index - b.index);
|
|
385
|
+
const at = sorted[0].index;
|
|
386
|
+
const children = sorted.map((l) => l.layer);
|
|
387
|
+
for (const l of [...sorted].reverse()) container.splice(l.index, 1);
|
|
388
|
+
container.splice(at, 0, {
|
|
389
|
+
id: op.id,
|
|
390
|
+
type: "group",
|
|
391
|
+
...op.name ? { name: op.name } : {},
|
|
392
|
+
children
|
|
393
|
+
});
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
case "ungroup": {
|
|
397
|
+
const loc = need(d, op.id, i);
|
|
398
|
+
if (loc.layer.type !== "group") throw opError(i, "invalid", `"${op.id}" is not a group.`);
|
|
399
|
+
loc.container.splice(loc.index, 1, ...loc.layer.children ?? []);
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case "addFilter": {
|
|
403
|
+
const loc = need(d, op.id, i);
|
|
404
|
+
if (!isObj(op.filter) || typeof op.filter.type !== "string") throw opError(i, "invalid", "\"filter\" ({ type, ...params }) is required.", "e.g. { \"type\": \"brightnessContrast\", \"contrast\": 20 } — catalog_get section \"filters\".");
|
|
405
|
+
const list = filtersOf(loc, i);
|
|
406
|
+
const at = op.index === void 0 ? list.length : Math.max(0, Math.min(list.length, op.index));
|
|
407
|
+
list.splice(at, 0, clone(op.filter));
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "updateFilter": {
|
|
411
|
+
const list = filtersOf(need(d, op.id, i), i);
|
|
412
|
+
const k = filterIndex(list, op.index, i, op.id);
|
|
413
|
+
if (!isObj(op.set)) throw opError(i, "invalid", "\"set\" is required.");
|
|
414
|
+
list[k] = mergePatch(list[k], op.set);
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
case "removeFilter": {
|
|
418
|
+
const list = filtersOf(need(d, op.id, i), i);
|
|
419
|
+
list.splice(filterIndex(list, op.index, i, op.id), 1);
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
case "setMask": {
|
|
423
|
+
const loc = need(d, op.id, i);
|
|
424
|
+
if (op.mask === null || op.mask === void 0) delete loc.layer.mask;
|
|
425
|
+
else loc.layer.mask = clone(op.mask);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case "crop":
|
|
429
|
+
d = cropDoc(d, op);
|
|
430
|
+
break;
|
|
431
|
+
case "resizeCanvas":
|
|
432
|
+
if (op.anchor !== void 0 && !ANCHORS.includes(op.anchor)) throw opError(i, "invalid", `Unknown anchor "${op.anchor}".`, `Anchors: ${ANCHORS.join(", ")}.`);
|
|
433
|
+
if (!(op.width >= 1) || !(op.height >= 1)) throw opError(i, "invalid", "resizeCanvas needs width ≥ 1 and height ≥ 1.");
|
|
434
|
+
d = resizeCanvasDoc(d, Math.round(op.width), Math.round(op.height), op.anchor);
|
|
435
|
+
break;
|
|
436
|
+
case "resizeImage": {
|
|
437
|
+
const W = d.canvas.width;
|
|
438
|
+
const H = d.canvas.height;
|
|
439
|
+
let sx;
|
|
440
|
+
let sy;
|
|
441
|
+
if (typeof op.scale === "number") sx = sy = op.scale;
|
|
442
|
+
else if (typeof op.width === "number" && typeof op.height === "number") [sx, sy] = [op.width / W, op.height / H];
|
|
443
|
+
else if (typeof op.width === "number") sx = sy = op.width / W;
|
|
444
|
+
else if (typeof op.height === "number") sx = sy = op.height / H;
|
|
445
|
+
else throw opError(i, "invalid", "resizeImage needs scale, width and/or height.");
|
|
446
|
+
d = scaleDoc(d, sx, sy, sizes);
|
|
447
|
+
break;
|
|
448
|
+
}
|
|
449
|
+
case "rotateCanvas":
|
|
450
|
+
d = rotateDoc(d, op.angle, sizes);
|
|
451
|
+
break;
|
|
452
|
+
case "flipCanvas":
|
|
453
|
+
d = flipDoc(d, op.axis, sizes);
|
|
454
|
+
break;
|
|
455
|
+
case "fitLayer":
|
|
456
|
+
need(d, op.id, i);
|
|
457
|
+
d = fitLayerDoc(d, op.id, op.box, {
|
|
458
|
+
subject: op.subject,
|
|
459
|
+
anchor: op.anchor,
|
|
460
|
+
sizes
|
|
461
|
+
});
|
|
462
|
+
break;
|
|
463
|
+
default: throw opError(i, "invalid", `Unknown op "${op.op}".`, `${didYouMean(op.op, PATCH_OPS)}Ops: ${PATCH_OPS.join(", ")}.`);
|
|
464
|
+
}
|
|
465
|
+
} catch (e) {
|
|
466
|
+
if (e instanceof OpsError) {
|
|
467
|
+
if (e.details?.opIndex !== void 0) throw e;
|
|
468
|
+
throw new OpsError(e.code, `ops[${i}] (${op.op}): ${e.message}`, e.hint, { opIndex: i });
|
|
469
|
+
}
|
|
470
|
+
if (e instanceof PathError) throw new OpsError("invalid", `ops[${i}] (${op.op}): ${e.message}`, e.hint ?? "Paths look like \"canvas.background\", \"layers#title.size\", \"layers#photo.filters[0].radius\".", { opIndex: i });
|
|
471
|
+
throw e;
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
return d;
|
|
475
|
+
}
|
|
476
|
+
async function sourceSizes(fs, doc, readLibrary) {
|
|
477
|
+
const { imageInfo } = await import("./imageInfo-QxAfu8R6.mjs").then((n) => n.r);
|
|
478
|
+
const out = {};
|
|
479
|
+
const srcs = /* @__PURE__ */ new Set();
|
|
480
|
+
walkLayers(doc.layers, (l) => {
|
|
481
|
+
if (l.type === "image") srcs.add(l.src);
|
|
482
|
+
});
|
|
483
|
+
for (const src of srcs) try {
|
|
484
|
+
const bytes = isLibraryRef(src) ? readLibrary ? await readLibrary(src.slice(8)) : null : await fs.readBytes(src);
|
|
485
|
+
const info = bytes ? imageInfo(bytes) : null;
|
|
486
|
+
if (info) out[src] = {
|
|
487
|
+
width: info.width,
|
|
488
|
+
height: info.height
|
|
489
|
+
};
|
|
490
|
+
} catch {}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
async function patchDoc(fs, ops, opts = {}) {
|
|
494
|
+
const before = await getDoc(fs);
|
|
495
|
+
const doc = applyPatchOps(before, ops, opts.sizes ?? (Array.isArray(ops) && ops.some((o) => isObj(o) && GEOMETRY_OPS.has(o.op)) ? await sourceSizes(fs, before, opts.readLibrary) : {}));
|
|
496
|
+
const issues = validateDoc(doc);
|
|
497
|
+
const plan = diffDocs(before, doc);
|
|
498
|
+
if (hasErrors(issues) || opts.dryRun || plan.empty) return {
|
|
499
|
+
plan,
|
|
500
|
+
issues,
|
|
501
|
+
applied: false,
|
|
502
|
+
doc: hasErrors(issues) ? before : doc
|
|
503
|
+
};
|
|
504
|
+
await saveDoc(fs, doc);
|
|
505
|
+
return {
|
|
506
|
+
plan,
|
|
507
|
+
issues,
|
|
508
|
+
applied: true,
|
|
509
|
+
doc
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
export { patchDoc as a, getDoc as i, applyDoc as n, summarizeDoc as o, doc_exports as r, PATCH_OPS as t };
|