@fleox/agentphoto 0.0.0-stage → 0.1.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/LICENSE +143 -0
- package/README.md +229 -2
- package/changelog.json +17 -0
- package/dist/agent/agentphoto.mjs +3307 -0
- package/dist/agent/chunks/assets-CDQo3VqD.mjs +151 -0
- package/dist/agent/chunks/catalog-DIZ5dnH5.mjs +953 -0
- package/dist/agent/chunks/context-DoLoLF9f.mjs +75 -0
- package/dist/agent/chunks/doc-BjY_j4Su.mjs +773 -0
- package/dist/agent/chunks/export-CLmpFART.mjs +140 -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-C4i_hicr.mjs +387 -0
- package/dist/agent/chunks/output-3F12IR9O.mjs +130 -0
- package/dist/agent/chunks/package-DSmAxrla.mjs +71 -0
- package/dist/agent/chunks/project-CUykGysG.mjs +198 -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--YGpGuHh.mjs +40 -0
- package/dist/agent/chunks/tools-Dnh8g6F9.mjs +1354 -0
- package/dist/agent/chunks/tree-DO7N0UDV.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 +2777 -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-Be8JnaFy.js +28 -0
- package/dist/studio/assets/index-DwPms2S5.css +1 -0
- package/dist/studio/assets/transformers.web-DgnCIB_X.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 +132 -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 +66 -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,773 @@
|
|
|
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$1, 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, r as isGradientMask } from "./types-DOK7tAAu.mjs";
|
|
13
|
+
import { i as hasErrors, o as readJson, r as OpsError } from "./context-DoLoLF9f.mjs";
|
|
14
|
+
import { c as filterDef, i as EFFECTS } from "./catalog-DIZ5dnH5.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-DO7N0UDV.mjs";
|
|
16
|
+
import { a as saveDoc, n as DOC_FILE } from "./project-CUykGysG.mjs";
|
|
17
|
+
// src/ops/geometry.ts
|
|
18
|
+
var geometry_exports = /* @__PURE__ */ __exportAll({ cropDoc: () => cropDoc });
|
|
19
|
+
var ANCHORS = [
|
|
20
|
+
"top-left",
|
|
21
|
+
"top",
|
|
22
|
+
"top-right",
|
|
23
|
+
"left",
|
|
24
|
+
"center",
|
|
25
|
+
"right",
|
|
26
|
+
"bottom-left",
|
|
27
|
+
"bottom",
|
|
28
|
+
"bottom-right"
|
|
29
|
+
];
|
|
30
|
+
var clone = (v) => JSON.parse(JSON.stringify(v));
|
|
31
|
+
var r2 = (v) => Math.round(v * 100) / 100;
|
|
32
|
+
function estimateTextBox(l) {
|
|
33
|
+
const size = l.size ?? 64;
|
|
34
|
+
const lines = String(l.text ?? "").split("\n");
|
|
35
|
+
const longest = Math.max(1, ...lines.map((s) => s.length));
|
|
36
|
+
return {
|
|
37
|
+
width: l.width ?? longest * size * .55 + Math.max(0, longest - 1) * (l.letterSpacing ?? 0),
|
|
38
|
+
height: l.height ?? lines.length * size * (l.lineHeight ?? 1.2)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function layerBox(l, sizes = {}) {
|
|
42
|
+
if (!hasBox(l)) return null;
|
|
43
|
+
const x = l.x ?? 0;
|
|
44
|
+
const y = l.y ?? 0;
|
|
45
|
+
if (l.type === "text") return {
|
|
46
|
+
x,
|
|
47
|
+
y,
|
|
48
|
+
...estimateTextBox(l)
|
|
49
|
+
};
|
|
50
|
+
let w = l.width;
|
|
51
|
+
let h = l.height;
|
|
52
|
+
if (l.type === "image" && (w === void 0 || h === void 0)) {
|
|
53
|
+
const nat = sizes[l.src];
|
|
54
|
+
const src = l.crop ? {
|
|
55
|
+
width: l.crop.width,
|
|
56
|
+
height: l.crop.height
|
|
57
|
+
} : nat;
|
|
58
|
+
if (src) {
|
|
59
|
+
if (w === void 0 && h === void 0) [w, h] = [src.width, src.height];
|
|
60
|
+
else if (w === void 0) w = h * src.width / src.height;
|
|
61
|
+
else h = w * src.height / src.width;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (w === void 0 || h === void 0) return null;
|
|
65
|
+
return {
|
|
66
|
+
x,
|
|
67
|
+
y,
|
|
68
|
+
width: w,
|
|
69
|
+
height: h
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function pinSize(l, sizes) {
|
|
73
|
+
if (l.type !== "image" || l.width !== void 0 && l.height !== void 0) return;
|
|
74
|
+
const b = layerBox(l, sizes);
|
|
75
|
+
if (!b) throw new OpsError("invalid", `The size of image layer "${l.id}" is unknown (its source ${l.src} could not be read).`, "Give the layer an explicit width and height, or fix its src (assets_list).");
|
|
76
|
+
l.width = r2(b.width);
|
|
77
|
+
l.height = r2(b.height);
|
|
78
|
+
}
|
|
79
|
+
function eachLayer(layers, fn) {
|
|
80
|
+
for (const l of layers) {
|
|
81
|
+
fn(l);
|
|
82
|
+
if (l.type === "group") eachLayer(l.children ?? [], fn);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function translateMask(m, dx, dy) {
|
|
86
|
+
if (m && isShapeMask(m)) {
|
|
87
|
+
m.x = r2(m.x + dx);
|
|
88
|
+
m.y = r2(m.y + dy);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function translateAll(layers, dx, dy) {
|
|
92
|
+
eachLayer(layers, (l) => {
|
|
93
|
+
if (hasBox(l)) {
|
|
94
|
+
l.x = r2((l.x ?? 0) + dx);
|
|
95
|
+
l.y = r2((l.y ?? 0) + dy);
|
|
96
|
+
} else translateMask(l.mask, dx, dy);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function cropDoc(doc, rect) {
|
|
100
|
+
const { x, y, width, height } = rect;
|
|
101
|
+
if (![
|
|
102
|
+
x,
|
|
103
|
+
y,
|
|
104
|
+
width,
|
|
105
|
+
height
|
|
106
|
+
].every(Number.isFinite) || width < 1 || height < 1) throw new OpsError("invalid", "crop needs x, y, width ≥ 1, height ≥ 1 (canvas pixels).");
|
|
107
|
+
const out = clone(doc);
|
|
108
|
+
translateAll(out.layers, -x, -y);
|
|
109
|
+
out.canvas = {
|
|
110
|
+
...out.canvas,
|
|
111
|
+
width: Math.round(width),
|
|
112
|
+
height: Math.round(height)
|
|
113
|
+
};
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
function anchorOffset(anchor, dw, dh) {
|
|
117
|
+
const ax = anchor.endsWith("left") ? 0 : anchor.endsWith("right") ? 1 : .5;
|
|
118
|
+
const ay = anchor.startsWith("top") ? 0 : anchor.startsWith("bottom") ? 1 : .5;
|
|
119
|
+
return [dw * ax, dh * ay];
|
|
120
|
+
}
|
|
121
|
+
function resizeCanvasDoc(doc, width, height, anchor = "center") {
|
|
122
|
+
if (!ANCHORS.includes(anchor)) throw new OpsError("invalid", `Unknown anchor "${anchor}".`, `Anchors: ${ANCHORS.join(", ")}.`);
|
|
123
|
+
const [ox, oy] = anchorOffset(anchor, width - doc.canvas.width, height - doc.canvas.height);
|
|
124
|
+
return cropDoc(doc, {
|
|
125
|
+
x: -ox,
|
|
126
|
+
y: -oy,
|
|
127
|
+
width,
|
|
128
|
+
height
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function scalePx(obj, keys, k) {
|
|
132
|
+
if (!obj) return;
|
|
133
|
+
for (const key of keys) if (typeof obj[key] === "number") obj[key] = r2(obj[key] * k);
|
|
134
|
+
}
|
|
135
|
+
function scaleFilters(filters, k) {
|
|
136
|
+
for (const f of filters ?? []) scalePx(f, (filterDef(f.type)?.params ?? []).filter((p) => p.px).map((p) => p.name), k);
|
|
137
|
+
}
|
|
138
|
+
function scaleDoc(doc, sx, sy, sizes = {}) {
|
|
139
|
+
if (!(sx > 0) || !(sy > 0)) throw new OpsError("invalid", "The scale must be > 0.");
|
|
140
|
+
const out = clone(doc);
|
|
141
|
+
const k = Math.sqrt(sx * sy);
|
|
142
|
+
eachLayer(out.layers, (l) => {
|
|
143
|
+
pinSize(l, sizes);
|
|
144
|
+
if (hasBox(l)) {
|
|
145
|
+
l.x = r2((l.x ?? 0) * sx);
|
|
146
|
+
l.y = r2((l.y ?? 0) * sy);
|
|
147
|
+
if (l.width !== void 0) l.width = r2(l.width * sx);
|
|
148
|
+
if (l.height !== void 0) l.height = r2(l.height * sy);
|
|
149
|
+
} else if (l.mask && isShapeMask(l.mask)) {
|
|
150
|
+
const m = l.mask;
|
|
151
|
+
m.x = r2(m.x * sx);
|
|
152
|
+
m.y = r2(m.y * sy);
|
|
153
|
+
m.width = r2(m.width * sx);
|
|
154
|
+
m.height = r2(m.height * sy);
|
|
155
|
+
}
|
|
156
|
+
if (l.mask) {
|
|
157
|
+
if (hasBox(l) && isShapeMask(l.mask)) {
|
|
158
|
+
const m = l.mask;
|
|
159
|
+
m.x = r2(m.x * sx);
|
|
160
|
+
m.y = r2(m.y * sy);
|
|
161
|
+
m.width = r2(m.width * sx);
|
|
162
|
+
m.height = r2(m.height * sy);
|
|
163
|
+
}
|
|
164
|
+
scalePx(l.mask, ["feather", "radius"], k);
|
|
165
|
+
}
|
|
166
|
+
if (l.type === "text") {
|
|
167
|
+
scalePx(l, ["size", "letterSpacing"], k);
|
|
168
|
+
if (l.background) {
|
|
169
|
+
const p = l.background.padding;
|
|
170
|
+
l.background.padding = Array.isArray(p) ? [r2(p[0] * k), r2(p[1] * k)] : typeof p === "number" ? r2(p * k) : p;
|
|
171
|
+
scalePx(l.background, ["radius"], k);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (l.type === "shape") {
|
|
175
|
+
scalePx(l, ["radius"], k);
|
|
176
|
+
if (l.stroke) scalePx(l.stroke, ["width"], k);
|
|
177
|
+
}
|
|
178
|
+
scaleFilters(l.filters, k);
|
|
179
|
+
if (l.effects) for (const def of EFFECTS) {
|
|
180
|
+
const fx = l.effects[def.id];
|
|
181
|
+
scalePx(fx, def.params.filter((p) => p.px).map((p) => p.name), k);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
out.canvas = {
|
|
185
|
+
...out.canvas,
|
|
186
|
+
width: Math.max(1, Math.round(doc.canvas.width * sx)),
|
|
187
|
+
height: Math.max(1, Math.round(doc.canvas.height * sy))
|
|
188
|
+
};
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
var normAngle = (deg) => {
|
|
192
|
+
const a = ((deg + 180) % 360 + 360) % 360 - 180;
|
|
193
|
+
return a === -180 ? 180 : a;
|
|
194
|
+
};
|
|
195
|
+
function transformPaint(p, fn) {
|
|
196
|
+
if (!p || typeof p !== "object") return;
|
|
197
|
+
if (p.type === "linear") p.angle = r2(normAngle(fn.angle(p.angle ?? 90)));
|
|
198
|
+
else if (p.type === "radial") {
|
|
199
|
+
const [cx, cy] = fn.point(p.cx ?? .5, p.cy ?? .5);
|
|
200
|
+
p.cx = r2(cx);
|
|
201
|
+
p.cy = r2(cy);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function transformCanvasPaints(doc, fn) {
|
|
205
|
+
transformPaint(doc.canvas.background, fn);
|
|
206
|
+
eachLayer(doc.layers, (l) => {
|
|
207
|
+
if (hasBox(l)) return;
|
|
208
|
+
if (l.type === "fill") transformPaint(l.fill, fn);
|
|
209
|
+
if (l.mask && isGradientMask(l.mask)) transformPaint(l.mask.gradient, fn);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function rotateDoc(doc, angle, sizes = {}) {
|
|
213
|
+
const a = (Math.round(angle / 90) * 90 % 360 + 360) % 360;
|
|
214
|
+
if (angle % 90 !== 0) throw new OpsError("invalid", `Canvas rotation must be a multiple of 90°, got ${angle}.`, "To straighten a photo by a few degrees, set the image layer's rotation instead (and crop).");
|
|
215
|
+
if (a === 0) return clone(doc);
|
|
216
|
+
const out = clone(doc);
|
|
217
|
+
const W = doc.canvas.width;
|
|
218
|
+
const H = doc.canvas.height;
|
|
219
|
+
const map = (x, y) => a === 90 ? [H - y, x] : a === 180 ? [W - x, H - y] : [y, W - x];
|
|
220
|
+
eachLayer(out.layers, (l) => {
|
|
221
|
+
pinSize(l, sizes);
|
|
222
|
+
const box = layerBox(l, sizes);
|
|
223
|
+
if (box) {
|
|
224
|
+
const [cx, cy] = map(box.x + box.width / 2, box.y + box.height / 2);
|
|
225
|
+
const bl = l;
|
|
226
|
+
bl.x = r2(cx - box.width / 2);
|
|
227
|
+
bl.y = r2(cy - box.height / 2);
|
|
228
|
+
bl.rotation = normAngle((bl.rotation ?? 0) + a);
|
|
229
|
+
if (bl.rotation === 0) delete bl.rotation;
|
|
230
|
+
} else if (l.mask && isShapeMask(l.mask)) {
|
|
231
|
+
const m = l.mask;
|
|
232
|
+
const [cx, cy] = map(m.x + m.width / 2, m.y + m.height / 2);
|
|
233
|
+
const [w, h] = a === 180 ? [m.width, m.height] : [m.height, m.width];
|
|
234
|
+
Object.assign(m, {
|
|
235
|
+
x: r2(cx - w / 2),
|
|
236
|
+
y: r2(cy - h / 2),
|
|
237
|
+
width: w,
|
|
238
|
+
height: h
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
transformCanvasPaints(out, {
|
|
243
|
+
angle: (deg) => deg + a,
|
|
244
|
+
point: (fx, fy) => a === 90 ? [1 - fy, fx] : a === 180 ? [1 - fx, 1 - fy] : [fy, 1 - fx]
|
|
245
|
+
});
|
|
246
|
+
if (a !== 180) out.canvas = {
|
|
247
|
+
...out.canvas,
|
|
248
|
+
width: H,
|
|
249
|
+
height: W
|
|
250
|
+
};
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
function flipDoc(doc, axis, sizes = {}) {
|
|
254
|
+
if (axis !== "horizontal" && axis !== "vertical") throw new OpsError("invalid", `axis must be "horizontal" or "vertical".`);
|
|
255
|
+
const out = clone(doc);
|
|
256
|
+
const W = doc.canvas.width;
|
|
257
|
+
const H = doc.canvas.height;
|
|
258
|
+
eachLayer(out.layers, (l) => {
|
|
259
|
+
pinSize(l, sizes);
|
|
260
|
+
const box = layerBox(l, sizes);
|
|
261
|
+
if (box) {
|
|
262
|
+
const bl = l;
|
|
263
|
+
if (axis === "horizontal") {
|
|
264
|
+
bl.x = r2(W - box.x - box.width);
|
|
265
|
+
if (l.type !== "text") bl.flipX = !bl.flipX || void 0;
|
|
266
|
+
} else {
|
|
267
|
+
bl.y = r2(H - box.y - box.height);
|
|
268
|
+
if (l.type !== "text") bl.flipY = !bl.flipY || void 0;
|
|
269
|
+
}
|
|
270
|
+
if (bl.rotation) bl.rotation = -bl.rotation;
|
|
271
|
+
if (!bl.flipX) delete bl.flipX;
|
|
272
|
+
if (!bl.flipY) delete bl.flipY;
|
|
273
|
+
} else if (l.mask && isShapeMask(l.mask)) {
|
|
274
|
+
const m = l.mask;
|
|
275
|
+
if (axis === "horizontal") m.x = r2(W - m.x - m.width);
|
|
276
|
+
else m.y = r2(H - m.y - m.height);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
transformCanvasPaints(out, axis === "horizontal" ? {
|
|
280
|
+
angle: (deg) => 180 - deg,
|
|
281
|
+
point: (fx, fy) => [1 - fx, fy]
|
|
282
|
+
} : {
|
|
283
|
+
angle: (deg) => -deg,
|
|
284
|
+
point: (fx, fy) => [fx, 1 - fy]
|
|
285
|
+
});
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
// src/ops/doc.ts
|
|
289
|
+
var doc_exports = /* @__PURE__ */ __exportAll({
|
|
290
|
+
DOC_FILE: () => DOC_FILE,
|
|
291
|
+
GEOMETRY_OPS: () => GEOMETRY_OPS,
|
|
292
|
+
PATCH_OPS: () => PATCH_OPS,
|
|
293
|
+
applyDoc: () => applyDoc,
|
|
294
|
+
applyPatchOps: () => applyPatchOps,
|
|
295
|
+
diffDocs: () => diffDocs,
|
|
296
|
+
findLayer: () => findLayer,
|
|
297
|
+
flattenLayers: () => flattenLayers,
|
|
298
|
+
getDoc: () => getDoc,
|
|
299
|
+
layerDetail: () => layerDetail,
|
|
300
|
+
maskKind: () => maskKind,
|
|
301
|
+
patchDoc: () => patchDoc,
|
|
302
|
+
sourceSizes: () => sourceSizes,
|
|
303
|
+
summarizeDoc: () => summarizeDoc
|
|
304
|
+
});
|
|
305
|
+
async function getDoc(fs) {
|
|
306
|
+
return normalizeDoc(await readJson(fs, DOC_FILE, "The document"));
|
|
307
|
+
}
|
|
308
|
+
function summarizeLayer(l, f) {
|
|
309
|
+
const { parent: _p, depth: _d, index: _i, locked: _l, ...rest } = f;
|
|
310
|
+
const out = { ...rest };
|
|
311
|
+
if (l.type === "group") out.children = [...l.children ?? []].reverse().map((c, k, arr) => summarizeLayer(c, flatOf(c, l, arr.length - 1 - k)));
|
|
312
|
+
return out;
|
|
313
|
+
}
|
|
314
|
+
var flatOf = (l, parent, index) => {
|
|
315
|
+
return {
|
|
316
|
+
...flattenLayers({ layers: [l] })[0],
|
|
317
|
+
parent: parent?.id ?? null,
|
|
318
|
+
index
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
function summarizeDoc(doc) {
|
|
322
|
+
const byType = {};
|
|
323
|
+
let hidden = 0;
|
|
324
|
+
let masks = 0;
|
|
325
|
+
let filters = 0;
|
|
326
|
+
const sources = /* @__PURE__ */ new Set();
|
|
327
|
+
walkLayers(doc.layers, (l) => {
|
|
328
|
+
byType[l.type] = (byType[l.type] ?? 0) + 1;
|
|
329
|
+
if (l.visible === false) hidden++;
|
|
330
|
+
if (l.mask) masks++;
|
|
331
|
+
filters += l.filters?.length ?? 0;
|
|
332
|
+
if (l.type === "image") sources.add(l.src);
|
|
333
|
+
if (l.mask && "src" in l.mask) sources.add(l.mask.src);
|
|
334
|
+
});
|
|
335
|
+
const bg = doc.canvas?.background;
|
|
336
|
+
return {
|
|
337
|
+
id: doc.id,
|
|
338
|
+
...doc.title ? { title: doc.title } : {},
|
|
339
|
+
canvas: {
|
|
340
|
+
width: doc.canvas?.width,
|
|
341
|
+
height: doc.canvas?.height,
|
|
342
|
+
background: bg === void 0 || bg === null ? "transparent" : typeof bg === "string" ? bg : `${bg.type} gradient`
|
|
343
|
+
},
|
|
344
|
+
layers: [...Array.isArray(doc.layers) ? doc.layers : []].reverse().map((l, k, arr) => summarizeLayer(l, flatOf(l, null, arr.length - 1 - k))),
|
|
345
|
+
counts: {
|
|
346
|
+
layers: Object.values(byType).reduce((a, b) => a + b, 0),
|
|
347
|
+
byType,
|
|
348
|
+
hidden,
|
|
349
|
+
masks,
|
|
350
|
+
filters
|
|
351
|
+
},
|
|
352
|
+
sources: [...sources],
|
|
353
|
+
...doc.export ? { export: doc.export } : {}
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function diffDocs(before, after) {
|
|
357
|
+
const map = (d) => {
|
|
358
|
+
const m = /* @__PURE__ */ new Map();
|
|
359
|
+
if (!d) return m;
|
|
360
|
+
const visit = (layers, parent) => {
|
|
361
|
+
if (!Array.isArray(layers)) return;
|
|
362
|
+
layers.forEach((l, i) => {
|
|
363
|
+
if (!l || typeof l !== "object") return;
|
|
364
|
+
m.set(l.id, {
|
|
365
|
+
layer: l,
|
|
366
|
+
where: `${parent}[${i}]`
|
|
367
|
+
});
|
|
368
|
+
if (l.type === "group") visit(l.children, l.id);
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
visit(d.layers, "root");
|
|
372
|
+
return m;
|
|
373
|
+
};
|
|
374
|
+
const a = map(before);
|
|
375
|
+
const b = map(after);
|
|
376
|
+
const added = [...b.keys()].filter((id) => !a.has(id));
|
|
377
|
+
const removed = [...a.keys()].filter((id) => !b.has(id));
|
|
378
|
+
const changed = [];
|
|
379
|
+
const movedIds = /* @__PURE__ */ new Set();
|
|
380
|
+
const parentOf = (where) => where.slice(0, where.lastIndexOf("["));
|
|
381
|
+
const indexOf = (where) => Number(where.slice(where.lastIndexOf("[") + 1, -1));
|
|
382
|
+
const sameParent = /* @__PURE__ */ new Map();
|
|
383
|
+
for (const [id, { layer, where }] of b) {
|
|
384
|
+
const old = a.get(id);
|
|
385
|
+
if (!old) continue;
|
|
386
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(old.layer), ...Object.keys(layer)]);
|
|
387
|
+
keys.delete("children");
|
|
388
|
+
const fields = [...keys].filter((k) => !deepEqual(old.layer[k], layer[k]));
|
|
389
|
+
if (fields.length) changed.push({
|
|
390
|
+
id,
|
|
391
|
+
fields
|
|
392
|
+
});
|
|
393
|
+
if (parentOf(old.where) !== parentOf(where)) movedIds.add(id);
|
|
394
|
+
else {
|
|
395
|
+
const list = sameParent.get(parentOf(where)) ?? [];
|
|
396
|
+
list.push({
|
|
397
|
+
id,
|
|
398
|
+
from: indexOf(old.where),
|
|
399
|
+
to: indexOf(where)
|
|
400
|
+
});
|
|
401
|
+
sameParent.set(parentOf(where), list);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
for (const list of sameParent.values()) {
|
|
405
|
+
list.sort((x, y) => x.from - y.from);
|
|
406
|
+
const keep = longestIncreasing(list.map((x) => x.to));
|
|
407
|
+
list.forEach((x, k) => {
|
|
408
|
+
if (!keep.has(k)) movedIds.add(x.id);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const realMoves = [...b.keys()].filter((id) => movedIds.has(id)).map((id) => ({
|
|
412
|
+
id,
|
|
413
|
+
from: a.get(id).where,
|
|
414
|
+
to: b.get(id).where
|
|
415
|
+
}));
|
|
416
|
+
const sections = [
|
|
417
|
+
"canvas",
|
|
418
|
+
"title",
|
|
419
|
+
"description",
|
|
420
|
+
"export",
|
|
421
|
+
"id",
|
|
422
|
+
"version"
|
|
423
|
+
].filter((k) => !deepEqual(before?.[k], after[k]));
|
|
424
|
+
const parts = [];
|
|
425
|
+
if (added.length) parts.push(`added ${added.join(", ")}`);
|
|
426
|
+
if (removed.length) parts.push(`removed ${removed.join(", ")}`);
|
|
427
|
+
if (changed.length) parts.push(`changed ${changed.map((c) => `${c.id} (${c.fields.join(", ")})`).join("; ")}`);
|
|
428
|
+
if (realMoves.length) parts.push(`moved ${realMoves.map((m) => m.id).join(", ")}`);
|
|
429
|
+
if (sections.length) parts.push(`updated ${sections.join(", ")}`);
|
|
430
|
+
const empty = !parts.length;
|
|
431
|
+
return {
|
|
432
|
+
empty,
|
|
433
|
+
layers: {
|
|
434
|
+
added,
|
|
435
|
+
removed,
|
|
436
|
+
changed,
|
|
437
|
+
moved: realMoves
|
|
438
|
+
},
|
|
439
|
+
sections,
|
|
440
|
+
summary: empty ? "No change." : `${parts.join("; ")}.`
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
function longestIncreasing(seq) {
|
|
444
|
+
const len = [];
|
|
445
|
+
const prev = [];
|
|
446
|
+
let best = -1;
|
|
447
|
+
for (let i = 0; i < seq.length; i++) {
|
|
448
|
+
len[i] = 1;
|
|
449
|
+
prev[i] = -1;
|
|
450
|
+
for (let j = 0; j < i; j++) if (seq[j] < seq[i] && len[j] + 1 > len[i]) {
|
|
451
|
+
len[i] = len[j] + 1;
|
|
452
|
+
prev[i] = j;
|
|
453
|
+
}
|
|
454
|
+
if (best < 0 || len[i] > len[best]) best = i;
|
|
455
|
+
}
|
|
456
|
+
const out = /* @__PURE__ */ new Set();
|
|
457
|
+
for (let i = best; i >= 0; i = prev[i]) out.add(i);
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
async function applyDoc(fs, next, opts = {}) {
|
|
461
|
+
const before = await fs.exists("photo.json") ? await getDoc(fs).catch(() => null) : null;
|
|
462
|
+
const doc = normalizeDoc(next);
|
|
463
|
+
const issues = validateDoc(doc);
|
|
464
|
+
const plan = diffDocs(before, doc);
|
|
465
|
+
if (hasErrors(issues) || opts.dryRun || plan.empty) return {
|
|
466
|
+
plan,
|
|
467
|
+
issues,
|
|
468
|
+
applied: false
|
|
469
|
+
};
|
|
470
|
+
await saveDoc(fs, doc, { allowInvalid: false });
|
|
471
|
+
return {
|
|
472
|
+
plan,
|
|
473
|
+
issues,
|
|
474
|
+
applied: true
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
var PATCH_OPS = [
|
|
478
|
+
"set",
|
|
479
|
+
"merge",
|
|
480
|
+
"remove",
|
|
481
|
+
"addLayer",
|
|
482
|
+
"updateLayer",
|
|
483
|
+
"removeLayer",
|
|
484
|
+
"moveLayer",
|
|
485
|
+
"duplicateLayer",
|
|
486
|
+
"groupLayers",
|
|
487
|
+
"ungroup",
|
|
488
|
+
"addFilter",
|
|
489
|
+
"updateFilter",
|
|
490
|
+
"removeFilter",
|
|
491
|
+
"setMask",
|
|
492
|
+
"crop",
|
|
493
|
+
"resizeCanvas",
|
|
494
|
+
"resizeImage",
|
|
495
|
+
"rotateCanvas",
|
|
496
|
+
"flipCanvas"
|
|
497
|
+
];
|
|
498
|
+
var GEOMETRY_OPS = /* @__PURE__ */ new Set([
|
|
499
|
+
"resizeImage",
|
|
500
|
+
"rotateCanvas",
|
|
501
|
+
"flipCanvas"
|
|
502
|
+
]);
|
|
503
|
+
var isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
504
|
+
function opError(i, code, message, hint) {
|
|
505
|
+
return new OpsError(code === "notFound" ? "notFound" : code, `ops[${i}]: ${message}`, hint, { opIndex: i });
|
|
506
|
+
}
|
|
507
|
+
function need(doc, id, i) {
|
|
508
|
+
if (typeof id !== "string") throw opError(i, "invalid", "\"id\" (a layer id) is required.");
|
|
509
|
+
const loc = findLayer(doc.layers, id);
|
|
510
|
+
if (!loc) throw opError(i, "notFound", `No layer "${id}".`, `${didYouMean(id, allLayerIds(doc))}doc_summary / layer_list list the layer ids.`);
|
|
511
|
+
return loc;
|
|
512
|
+
}
|
|
513
|
+
function target(doc, pos, i, moving) {
|
|
514
|
+
const ref = pos.above ?? pos.below;
|
|
515
|
+
if (ref !== void 0) {
|
|
516
|
+
const loc = need(doc, ref, i);
|
|
517
|
+
if (moving && (loc.layer === moving || isDescendant(moving, ref))) throw opError(i, "invalid", `Cannot place "${moving.id}" relative to itself or its own child.`);
|
|
518
|
+
return {
|
|
519
|
+
container: loc.container,
|
|
520
|
+
index: pos.above !== void 0 ? loc.index + 1 : loc.index
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
let container = doc.layers;
|
|
524
|
+
if (pos.parent !== void 0 && pos.parent !== null) {
|
|
525
|
+
const loc = need(doc, pos.parent, i);
|
|
526
|
+
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).");
|
|
527
|
+
if (moving && (loc.layer === moving || isDescendant(moving, loc.layer.id))) throw opError(i, "invalid", `Cannot move "${moving.id}" into itself.`);
|
|
528
|
+
container = loc.layer.children ??= [];
|
|
529
|
+
}
|
|
530
|
+
const index = pos.index === void 0 ? container.length : Math.max(0, Math.min(container.length, Math.round(pos.index)));
|
|
531
|
+
return {
|
|
532
|
+
container,
|
|
533
|
+
index
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
function checkNewIds(doc, layer, i) {
|
|
537
|
+
const taken = new Set(allLayerIds(doc));
|
|
538
|
+
const visit = (l) => {
|
|
539
|
+
if (!isObj(l)) throw opError(i, "invalid", "layer must be an object.");
|
|
540
|
+
if (!isValidId(l.id)) throw opError(i, "invalid", `Invalid layer id ${JSON.stringify(l.id)}.`, "Ids start with a letter: \"photo\", \"title\", \"badge-2\".");
|
|
541
|
+
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)}").`);
|
|
542
|
+
taken.add(l.id);
|
|
543
|
+
if (l.type === "group") (l.children ?? []).forEach(visit);
|
|
544
|
+
};
|
|
545
|
+
visit(layer);
|
|
546
|
+
}
|
|
547
|
+
function renameTree(l, taken, rootId) {
|
|
548
|
+
const copy = clone$1(l);
|
|
549
|
+
const visit = (x, isRoot) => {
|
|
550
|
+
x.id = isRoot && rootId ? rootId : uniqueId(`${x.id}-copy`, taken);
|
|
551
|
+
taken.add(x.id);
|
|
552
|
+
if (x.type === "group") (x.children ?? []).forEach((c) => visit(c, false));
|
|
553
|
+
};
|
|
554
|
+
visit(copy, true);
|
|
555
|
+
return copy;
|
|
556
|
+
}
|
|
557
|
+
function filtersOf(loc, i) {
|
|
558
|
+
const l = loc.layer;
|
|
559
|
+
if (!Array.isArray(l.filters)) l.filters = [];
|
|
560
|
+
return l.filters;
|
|
561
|
+
}
|
|
562
|
+
function filterIndex(list, index, i, id) {
|
|
563
|
+
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.");
|
|
564
|
+
return index;
|
|
565
|
+
}
|
|
566
|
+
function applyPatchOps(doc, ops, sizes = {}) {
|
|
567
|
+
if (!Array.isArray(ops)) throw new OpsError("invalid", "ops must be an array of patch operations.");
|
|
568
|
+
let d = clone$1(doc);
|
|
569
|
+
ops.forEach((op, i) => {
|
|
570
|
+
if (!isObj(op) || typeof op.op !== "string") throw opError(i, "invalid", "Each op is an object with an \"op\" field.", `Ops: ${PATCH_OPS.join(", ")}.`);
|
|
571
|
+
try {
|
|
572
|
+
switch (op.op) {
|
|
573
|
+
case "set":
|
|
574
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
575
|
+
setAt(d, op.path, clone$1(op.value));
|
|
576
|
+
break;
|
|
577
|
+
case "merge": {
|
|
578
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
579
|
+
let cur;
|
|
580
|
+
try {
|
|
581
|
+
cur = getAt(d, op.path);
|
|
582
|
+
} catch {
|
|
583
|
+
cur = void 0;
|
|
584
|
+
}
|
|
585
|
+
setAt(d, op.path, mergePatch(cur, op.value));
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
case "remove":
|
|
589
|
+
if (typeof op.path !== "string") throw opError(i, "invalid", "\"path\" is required.");
|
|
590
|
+
if (!removeAt(d, op.path)) throw opError(i, "notFound", `Nothing at "${op.path}".`);
|
|
591
|
+
break;
|
|
592
|
+
case "addLayer": {
|
|
593
|
+
if (!isObj(op.layer)) throw opError(i, "invalid", "\"layer\" (a layer object) is required.");
|
|
594
|
+
checkNewIds(d, op.layer, i);
|
|
595
|
+
const t = target(d, op, i);
|
|
596
|
+
t.container.splice(t.index, 0, clone$1(op.layer));
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
case "updateLayer": {
|
|
600
|
+
const loc = need(d, op.id, i);
|
|
601
|
+
if (!isObj(op.set)) throw opError(i, "invalid", "\"set\" (an object of fields to change) is required.");
|
|
602
|
+
for (const k of [
|
|
603
|
+
"id",
|
|
604
|
+
"type",
|
|
605
|
+
"children"
|
|
606
|
+
]) 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.");
|
|
607
|
+
const next = mergePatch(loc.layer, op.set);
|
|
608
|
+
loc.container[loc.index] = next;
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case "removeLayer": {
|
|
612
|
+
const loc = need(d, op.id, i);
|
|
613
|
+
loc.container.splice(loc.index, 1);
|
|
614
|
+
break;
|
|
615
|
+
}
|
|
616
|
+
case "moveLayer": {
|
|
617
|
+
const loc = need(d, op.id, i);
|
|
618
|
+
const layer = loc.layer;
|
|
619
|
+
target(d, op, i, layer);
|
|
620
|
+
loc.container.splice(loc.index, 1);
|
|
621
|
+
const t = target(d, op, i, layer);
|
|
622
|
+
t.container.splice(t.index, 0, layer);
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case "duplicateLayer": {
|
|
626
|
+
const loc = need(d, op.id, i);
|
|
627
|
+
const taken = new Set(allLayerIds(d));
|
|
628
|
+
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.`);
|
|
629
|
+
const copy = renameTree(loc.layer, taken, op.newId);
|
|
630
|
+
if (op.offset) {
|
|
631
|
+
const [dx, dy] = op.offset;
|
|
632
|
+
if (!Number.isFinite(dx) || !Number.isFinite(dy)) throw opError(i, "invalid", "offset must be [dx, dy] in px.");
|
|
633
|
+
walkLayers([copy], (l) => {
|
|
634
|
+
if (hasBox(l)) {
|
|
635
|
+
l.x = (l.x ?? 0) + dx;
|
|
636
|
+
l.y = (l.y ?? 0) + dy;
|
|
637
|
+
} else if (l.mask && isShapeMask(l.mask)) {
|
|
638
|
+
l.mask.x += dx;
|
|
639
|
+
l.mask.y += dy;
|
|
640
|
+
}
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
if (copy.name) copy.name = `${copy.name} copy`;
|
|
644
|
+
loc.container.splice(loc.index + 1, 0, copy);
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
case "groupLayers": {
|
|
648
|
+
if (!Array.isArray(op.ids) || !op.ids.length) throw opError(i, "invalid", "\"ids\" (layers to group) is required.");
|
|
649
|
+
if (!isValidId(op.id) || allLayerIds(d).includes(op.id)) throw opError(i, "conflict", `Group id ${JSON.stringify(op.id)} is invalid or taken.`);
|
|
650
|
+
const locs = [...new Set(op.ids)].map((id) => need(d, id, i));
|
|
651
|
+
const container = locs[0].container;
|
|
652
|
+
if (locs.some((l) => l.container !== container)) throw opError(i, "invalid", "Grouped layers must be siblings (same parent).");
|
|
653
|
+
const sorted = [...locs].sort((a, b) => a.index - b.index);
|
|
654
|
+
const at = sorted[0].index;
|
|
655
|
+
const children = sorted.map((l) => l.layer);
|
|
656
|
+
for (const l of [...sorted].reverse()) container.splice(l.index, 1);
|
|
657
|
+
container.splice(at, 0, {
|
|
658
|
+
id: op.id,
|
|
659
|
+
type: "group",
|
|
660
|
+
...op.name ? { name: op.name } : {},
|
|
661
|
+
children
|
|
662
|
+
});
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
case "ungroup": {
|
|
666
|
+
const loc = need(d, op.id, i);
|
|
667
|
+
if (loc.layer.type !== "group") throw opError(i, "invalid", `"${op.id}" is not a group.`);
|
|
668
|
+
loc.container.splice(loc.index, 1, ...loc.layer.children ?? []);
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
case "addFilter": {
|
|
672
|
+
const loc = need(d, op.id, i);
|
|
673
|
+
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\".");
|
|
674
|
+
const list = filtersOf(loc, i);
|
|
675
|
+
const at = op.index === void 0 ? list.length : Math.max(0, Math.min(list.length, op.index));
|
|
676
|
+
list.splice(at, 0, clone$1(op.filter));
|
|
677
|
+
break;
|
|
678
|
+
}
|
|
679
|
+
case "updateFilter": {
|
|
680
|
+
const list = filtersOf(need(d, op.id, i), i);
|
|
681
|
+
const k = filterIndex(list, op.index, i, op.id);
|
|
682
|
+
if (!isObj(op.set)) throw opError(i, "invalid", "\"set\" is required.");
|
|
683
|
+
list[k] = mergePatch(list[k], op.set);
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
case "removeFilter": {
|
|
687
|
+
const list = filtersOf(need(d, op.id, i), i);
|
|
688
|
+
list.splice(filterIndex(list, op.index, i, op.id), 1);
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
case "setMask": {
|
|
692
|
+
const loc = need(d, op.id, i);
|
|
693
|
+
if (op.mask === null || op.mask === void 0) delete loc.layer.mask;
|
|
694
|
+
else loc.layer.mask = clone$1(op.mask);
|
|
695
|
+
break;
|
|
696
|
+
}
|
|
697
|
+
case "crop":
|
|
698
|
+
d = cropDoc(d, op);
|
|
699
|
+
break;
|
|
700
|
+
case "resizeCanvas":
|
|
701
|
+
if (op.anchor !== void 0 && !ANCHORS.includes(op.anchor)) throw opError(i, "invalid", `Unknown anchor "${op.anchor}".`, `Anchors: ${ANCHORS.join(", ")}.`);
|
|
702
|
+
if (!(op.width >= 1) || !(op.height >= 1)) throw opError(i, "invalid", "resizeCanvas needs width ≥ 1 and height ≥ 1.");
|
|
703
|
+
d = resizeCanvasDoc(d, Math.round(op.width), Math.round(op.height), op.anchor);
|
|
704
|
+
break;
|
|
705
|
+
case "resizeImage": {
|
|
706
|
+
const W = d.canvas.width;
|
|
707
|
+
const H = d.canvas.height;
|
|
708
|
+
let sx;
|
|
709
|
+
let sy;
|
|
710
|
+
if (typeof op.scale === "number") sx = sy = op.scale;
|
|
711
|
+
else if (typeof op.width === "number" && typeof op.height === "number") [sx, sy] = [op.width / W, op.height / H];
|
|
712
|
+
else if (typeof op.width === "number") sx = sy = op.width / W;
|
|
713
|
+
else if (typeof op.height === "number") sx = sy = op.height / H;
|
|
714
|
+
else throw opError(i, "invalid", "resizeImage needs scale, width and/or height.");
|
|
715
|
+
d = scaleDoc(d, sx, sy, sizes);
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
case "rotateCanvas":
|
|
719
|
+
d = rotateDoc(d, op.angle, sizes);
|
|
720
|
+
break;
|
|
721
|
+
case "flipCanvas":
|
|
722
|
+
d = flipDoc(d, op.axis, sizes);
|
|
723
|
+
break;
|
|
724
|
+
default: throw opError(i, "invalid", `Unknown op "${op.op}".`, `${didYouMean(op.op, PATCH_OPS)}Ops: ${PATCH_OPS.join(", ")}.`);
|
|
725
|
+
}
|
|
726
|
+
} catch (e) {
|
|
727
|
+
if (e instanceof OpsError) {
|
|
728
|
+
if (e.details?.opIndex !== void 0) throw e;
|
|
729
|
+
throw new OpsError(e.code, `ops[${i}] (${op.op}): ${e.message}`, e.hint, { opIndex: i });
|
|
730
|
+
}
|
|
731
|
+
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 });
|
|
732
|
+
throw e;
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
return d;
|
|
736
|
+
}
|
|
737
|
+
async function sourceSizes(fs, doc, readLibrary) {
|
|
738
|
+
const { imageInfo } = await import("./imageInfo-QxAfu8R6.mjs").then((n) => n.r);
|
|
739
|
+
const out = {};
|
|
740
|
+
const srcs = /* @__PURE__ */ new Set();
|
|
741
|
+
walkLayers(doc.layers, (l) => {
|
|
742
|
+
if (l.type === "image") srcs.add(l.src);
|
|
743
|
+
});
|
|
744
|
+
for (const src of srcs) try {
|
|
745
|
+
const bytes = isLibraryRef(src) ? readLibrary ? await readLibrary(src.slice(8)) : null : await fs.readBytes(src);
|
|
746
|
+
const info = bytes ? imageInfo(bytes) : null;
|
|
747
|
+
if (info) out[src] = {
|
|
748
|
+
width: info.width,
|
|
749
|
+
height: info.height
|
|
750
|
+
};
|
|
751
|
+
} catch {}
|
|
752
|
+
return out;
|
|
753
|
+
}
|
|
754
|
+
async function patchDoc(fs, ops, opts = {}) {
|
|
755
|
+
const before = await getDoc(fs);
|
|
756
|
+
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) : {}));
|
|
757
|
+
const issues = validateDoc(doc);
|
|
758
|
+
const plan = diffDocs(before, doc);
|
|
759
|
+
if (hasErrors(issues) || opts.dryRun || plan.empty) return {
|
|
760
|
+
plan,
|
|
761
|
+
issues,
|
|
762
|
+
applied: false,
|
|
763
|
+
doc: hasErrors(issues) ? before : doc
|
|
764
|
+
};
|
|
765
|
+
await saveDoc(fs, doc);
|
|
766
|
+
return {
|
|
767
|
+
plan,
|
|
768
|
+
issues,
|
|
769
|
+
applied: true,
|
|
770
|
+
doc
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
export { patchDoc as a, layerBox as c, getDoc as i, applyDoc as n, summarizeDoc as o, doc_exports as r, geometry_exports as s, PATCH_OPS as t };
|