@eldrajs/theme-core 0.0.0 → 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/CHANGELOG.md +301 -0
- package/LICENSE +21 -0
- package/README.md +68 -1
- package/dist/bridge-DfNINXLL.js +314 -0
- package/dist/bridge.d.ts +2 -0
- package/dist/bridge.js +2 -0
- package/dist/design-tokens.d.ts +68 -0
- package/dist/design-tokens.js +486 -0
- package/dist/dynamic-route.d.ts +2 -0
- package/dist/dynamic-route.js +3 -0
- package/dist/dynamicRoute-CxXY-Y_8.js +126 -0
- package/dist/dynamicRoute-pRTBoYT6.d.ts +55 -0
- package/dist/i18n.d.ts +107 -0
- package/dist/i18n.js +192 -0
- package/dist/image-framing.d.ts +2 -0
- package/dist/image-framing.js +109 -0
- package/dist/imageFraming-BUZ-yqho.d.ts +51 -0
- package/dist/index-DxrkKyG4.d.ts +488 -0
- package/dist/index.d.ts +407 -0
- package/dist/index.js +1136 -0
- package/dist/layout-CWq_v6ws.d.ts +244 -0
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +930 -0
- package/dist/links.d.ts +113 -0
- package/dist/links.js +184 -0
- package/dist/overlay-Bgi0tIbN.d.ts +201 -0
- package/dist/overlay-BvkEOiMB.js +2908 -0
- package/dist/overlay.d.ts +2 -0
- package/dist/overlay.js +6 -0
- package/dist/preview-router.d.ts +2 -0
- package/dist/preview-router.js +130 -0
- package/dist/reusable.d.ts +66 -0
- package/dist/reusable.js +92 -0
- package/dist/reusableExpansion-Bqm83nCJ.js +229 -0
- package/dist/reusableExpansion-CXvs_ckx.d.ts +42 -0
- package/dist/stega.d.ts +17 -0
- package/dist/stega.js +69 -0
- package/dist/template-layout.d.ts +3 -0
- package/dist/template-layout.js +328 -0
- package/dist/templateLayout-MjNs0gbF.d.ts +254 -0
- package/package.json +89 -2
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
//#region src/i18n.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Framework-free message-catalogue helpers shared by `@eldrajs/vite-plugin-theme` (the manifest
|
|
4
|
+
* scanner), `@eldrajs/theme-nuxt` (the build-time merge over the public platform read) and the
|
|
5
|
+
* starter's `vue-i18n` wiring.
|
|
6
|
+
*
|
|
7
|
+
* `ThemeMessages` mirrors `@eldrajs/vite-plugin-theme`'s own type of the same name byte for byte —
|
|
8
|
+
* `{ defaultLocale, locales: Record<tag, Record<key, string>> }`, flat dotted keys — but this
|
|
9
|
+
* package cannot import that one (`vite-plugin-theme` depends on `theme-core`, never the other way
|
|
10
|
+
* round), so it declares its own. The two are structurally interchangeable; nothing here converts
|
|
11
|
+
* between them because nothing needs to.
|
|
12
|
+
*/
|
|
13
|
+
/** The theme's own message catalogue, or the result of merging/resolving one — flat dotted keys,
|
|
14
|
+
* one record per locale tag. See the module doc comment for why this is declared here rather than
|
|
15
|
+
* imported from `@eldrajs/vite-plugin-theme`. */
|
|
16
|
+
export interface ThemeMessages {
|
|
17
|
+
defaultLocale: string;
|
|
18
|
+
locales: Record<string, Record<string, string>>;
|
|
19
|
+
}
|
|
20
|
+
/** The fallback catalogue: what a theme with no `i18n/` directory (or no messages at all) serves
|
|
21
|
+
* on `virtual:eldra/messages`. Matches `@eldrajs/vite-plugin-theme`'s own `EMPTY_MESSAGES`
|
|
22
|
+
* literal. */
|
|
23
|
+
export declare const EMPTY_THEME_MESSAGES: ThemeMessages;
|
|
24
|
+
/**
|
|
25
|
+
* Whether `tag` is one of the three names (`__proto__`, `prototype`, `constructor`) that, used as
|
|
26
|
+
* an object key, reassigns something other than an own property — a locale tag's own obvious risk,
|
|
27
|
+
* since it is written straight through to a record keyed by it. Exported so every consumer of an
|
|
28
|
+
* untrusted locale tag (a platform's JSON response, a Studio `postMessage` payload) shares the one
|
|
29
|
+
* definition instead of hand-rolling the same three-name set again.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isForbiddenLocaleTag(tag: string): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Sanitizes one locale's raw message record from an untrusted source: `null` when `tag` itself is
|
|
34
|
+
* forbidden (`isForbiddenLocaleTag`) — the caller drops the whole locale rather than let a write
|
|
35
|
+
* through it corrupt an object's prototype — otherwise a copy holding only `value`'s own
|
|
36
|
+
* string-valued keys, sorted, with any of the same forbidden names dropped at that level too.
|
|
37
|
+
* Shared by `@eldrajs/theme-vue` (`applyThemeMessages`, a live bridge payload) and
|
|
38
|
+
* `@eldrajs/theme-nuxt` (`toThemeMessages`, a platform response) instead of each re-implementing
|
|
39
|
+
* the same guard with its own, possibly drifting, level of protection.
|
|
40
|
+
*/
|
|
41
|
+
export declare function sanitizeLocaleMessages(tag: string, value: Record<string, unknown>): Record<string, string> | null;
|
|
42
|
+
/**
|
|
43
|
+
* Flattens a nested object of strings (vue-i18n's own on-disk shape) into dotted keys
|
|
44
|
+
* (`header.menu`, `cart.empty.title`) — the manifest's and the platform's own wire shape.
|
|
45
|
+
*
|
|
46
|
+
* Lenient, not validating: a leaf that is not a string (a number, a boolean, `null`, an array) is
|
|
47
|
+
* skipped rather than thrown on, and so is any segment named `__proto__`/`prototype`/`constructor`.
|
|
48
|
+
* Key grammar/length and value-length bounds are `@eldrajs/vite-plugin-theme`'s `scanTheme` job, at
|
|
49
|
+
* the one place theme content is first accepted; this helper is a pure structural transform over
|
|
50
|
+
* data that has already passed (or is about to be re-serialized from) that boundary.
|
|
51
|
+
*/
|
|
52
|
+
export declare function flattenMessages(nested: Record<string, unknown>): Record<string, string>;
|
|
53
|
+
/**
|
|
54
|
+
* The inverse of `flattenMessages`: dotted keys back into vue-i18n's nested shape, so a consumer
|
|
55
|
+
* (the starter's `vue-i18n` plugin) can hand `setLocaleMessage` an object it understands rather
|
|
56
|
+
* than a flat `virtual:eldra/messages` record.
|
|
57
|
+
*
|
|
58
|
+
* Keys are applied in sorted order, so a would-be conflict between a shallower and a deeper key
|
|
59
|
+
* (`"a"` and `"a.b"`, which cannot both come from one real nested JSON file but could in principle
|
|
60
|
+
* meet here after a merge) resolves deterministically: the deeper key's object wins, replacing
|
|
61
|
+
* whatever the shallower one wrote. Round-trips with `flattenMessages` for any catalogue that has
|
|
62
|
+
* no such conflict, which every catalogue `@eldrajs/vite-plugin-theme` has validated satisfies.
|
|
63
|
+
*/
|
|
64
|
+
export declare function unflattenMessages(flat: Record<string, string>): Record<string, unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* Merges a platform's theme-message overrides over the theme's own manifest defaults: **platform
|
|
67
|
+
* wins per key**, and a locale the theme never shipped is added **whole** — Studio may translate a
|
|
68
|
+
* key set the theme only declared for its own locales into a locale nobody on the theme side ever
|
|
69
|
+
* wrote a file for.
|
|
70
|
+
*
|
|
71
|
+
* `themeDefaults.defaultLocale` is carried through unchanged: it names the theme's own default
|
|
72
|
+
* locale, the last tier of `resolveMessageCatalogue`'s fallback chain, and merging never changes
|
|
73
|
+
* which locale that is (the platform's own response, when one exists, carries the *organisation's*
|
|
74
|
+
* default locale under the same field name — a different concept, threaded separately into
|
|
75
|
+
* `resolveMessageCatalogue` as `orgDefaultLocale`).
|
|
76
|
+
*
|
|
77
|
+
* `platform: null` (no gateway, or the read failed) is the identity merge: a clone of
|
|
78
|
+
* `themeDefaults`, unchanged.
|
|
79
|
+
*/
|
|
80
|
+
export declare function mergeMessageCatalogues(themeDefaults: ThemeMessages, platform: ThemeMessages | null): ThemeMessages;
|
|
81
|
+
/**
|
|
82
|
+
* Implements the contract's five-tier per-key fallback, to the letter:
|
|
83
|
+
* `override(locale) → theme(locale) → override(orgDefault) → theme(orgDefault) → theme(themeDefault)`.
|
|
84
|
+
*
|
|
85
|
+
* Takes `themeDefaults` and `platform` **separately** — not only a pre-merged catalogue — because
|
|
86
|
+
* tier 5 is explicitly `theme(themeDefault)` with **no** accompanying override tier: every other
|
|
87
|
+
* locale gets both an override and a theme tier, but the terminal safety net deliberately does
|
|
88
|
+
* not, so an override on the theme's own default locale must never leak into a key that falls all
|
|
89
|
+
* the way through to tier 5 for an unrelated locale. Tiers 1–4 *do* collapse into two
|
|
90
|
+
* locale-level lookups, since `mergeMessageCatalogues(themeDefaults, platform)` already fuses
|
|
91
|
+
* override and theme per locale:
|
|
92
|
+
*
|
|
93
|
+
* 1. `merged.locales[orgLocale]` — tiers 1+2 (the organisation's own locale, platform override or
|
|
94
|
+
* theme default, whichever the merge kept);
|
|
95
|
+
* 2. `merged.locales[orgDefaultLocale]` — tiers 3+4;
|
|
96
|
+
* 3. `themeDefaults.locales[themeDefaults.defaultLocale]` — tier 5, the theme's own shipped
|
|
97
|
+
* default-locale value, read from the **unmerged** input, never the platform's override of it.
|
|
98
|
+
*
|
|
99
|
+
* The full key set is the theme's own default-locale keys: every override is validated (Core's
|
|
100
|
+
* `UNKNOWN_KEY`) against that set before it can reach this function, so no key anywhere in the
|
|
101
|
+
* merged catalogue ever falls outside it. Every `orgLocale` — plus `orgDefaultLocale` itself, added
|
|
102
|
+
* even if the caller's list omitted it — comes back with that full key set, which is what lets a
|
|
103
|
+
* **credential-less build** (`platform: null` going in) still produce a complete catalogue for
|
|
104
|
+
* every locale from the manifest alone.
|
|
105
|
+
*/
|
|
106
|
+
export declare function resolveMessageCatalogue(themeDefaults: ThemeMessages, platform: ThemeMessages | null, orgLocales: readonly string[], orgDefaultLocale: string): ThemeMessages;
|
|
107
|
+
//#endregion
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
//#region src/i18n.ts
|
|
2
|
+
/** The fallback catalogue: what a theme with no `i18n/` directory (or no messages at all) serves
|
|
3
|
+
* on `virtual:eldra/messages`. Matches `@eldrajs/vite-plugin-theme`'s own `EMPTY_MESSAGES`
|
|
4
|
+
* literal. */
|
|
5
|
+
const EMPTY_THEME_MESSAGES = {
|
|
6
|
+
defaultLocale: "en-US",
|
|
7
|
+
locales: {}
|
|
8
|
+
};
|
|
9
|
+
const FORBIDDEN_KEYS = /* @__PURE__ */ new Set([
|
|
10
|
+
"__proto__",
|
|
11
|
+
"prototype",
|
|
12
|
+
"constructor"
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* Whether `tag` is one of the three names (`__proto__`, `prototype`, `constructor`) that, used as
|
|
16
|
+
* an object key, reassigns something other than an own property — a locale tag's own obvious risk,
|
|
17
|
+
* since it is written straight through to a record keyed by it. Exported so every consumer of an
|
|
18
|
+
* untrusted locale tag (a platform's JSON response, a Studio `postMessage` payload) shares the one
|
|
19
|
+
* definition instead of hand-rolling the same three-name set again.
|
|
20
|
+
*/
|
|
21
|
+
function isForbiddenLocaleTag(tag) {
|
|
22
|
+
return FORBIDDEN_KEYS.has(tag);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Sanitizes one locale's raw message record from an untrusted source: `null` when `tag` itself is
|
|
26
|
+
* forbidden (`isForbiddenLocaleTag`) — the caller drops the whole locale rather than let a write
|
|
27
|
+
* through it corrupt an object's prototype — otherwise a copy holding only `value`'s own
|
|
28
|
+
* string-valued keys, sorted, with any of the same forbidden names dropped at that level too.
|
|
29
|
+
* Shared by `@eldrajs/theme-vue` (`applyThemeMessages`, a live bridge payload) and
|
|
30
|
+
* `@eldrajs/theme-nuxt` (`toThemeMessages`, a platform response) instead of each re-implementing
|
|
31
|
+
* the same guard with its own, possibly drifting, level of protection.
|
|
32
|
+
*/
|
|
33
|
+
function sanitizeLocaleMessages(tag, value) {
|
|
34
|
+
if (isForbiddenLocaleTag(tag)) return null;
|
|
35
|
+
return sortedStringRecord(value);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Flattens a nested object of strings (vue-i18n's own on-disk shape) into dotted keys
|
|
39
|
+
* (`header.menu`, `cart.empty.title`) — the manifest's and the platform's own wire shape.
|
|
40
|
+
*
|
|
41
|
+
* Lenient, not validating: a leaf that is not a string (a number, a boolean, `null`, an array) is
|
|
42
|
+
* skipped rather than thrown on, and so is any segment named `__proto__`/`prototype`/`constructor`.
|
|
43
|
+
* Key grammar/length and value-length bounds are `@eldrajs/vite-plugin-theme`'s `scanTheme` job, at
|
|
44
|
+
* the one place theme content is first accepted; this helper is a pure structural transform over
|
|
45
|
+
* data that has already passed (or is about to be re-serialized from) that boundary.
|
|
46
|
+
*/
|
|
47
|
+
function flattenMessages(nested) {
|
|
48
|
+
const out = {};
|
|
49
|
+
flattenInto(nested, "", out);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
function flattenInto(node, prefix, out) {
|
|
53
|
+
for (const key of Object.keys(node)) {
|
|
54
|
+
if (FORBIDDEN_KEYS.has(key)) continue;
|
|
55
|
+
const value = node[key];
|
|
56
|
+
const path = prefix === "" ? key : `${prefix}.${key}`;
|
|
57
|
+
if (typeof value === "string") out[path] = value;
|
|
58
|
+
else if (isPlainObject(value)) flattenInto(value, path, out);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The inverse of `flattenMessages`: dotted keys back into vue-i18n's nested shape, so a consumer
|
|
63
|
+
* (the starter's `vue-i18n` plugin) can hand `setLocaleMessage` an object it understands rather
|
|
64
|
+
* than a flat `virtual:eldra/messages` record.
|
|
65
|
+
*
|
|
66
|
+
* Keys are applied in sorted order, so a would-be conflict between a shallower and a deeper key
|
|
67
|
+
* (`"a"` and `"a.b"`, which cannot both come from one real nested JSON file but could in principle
|
|
68
|
+
* meet here after a merge) resolves deterministically: the deeper key's object wins, replacing
|
|
69
|
+
* whatever the shallower one wrote. Round-trips with `flattenMessages` for any catalogue that has
|
|
70
|
+
* no such conflict, which every catalogue `@eldrajs/vite-plugin-theme` has validated satisfies.
|
|
71
|
+
*/
|
|
72
|
+
function unflattenMessages(flat) {
|
|
73
|
+
const out = {};
|
|
74
|
+
for (const key of Object.keys(flat).sort((a, b) => a.localeCompare(b))) {
|
|
75
|
+
const parts = key.split(".");
|
|
76
|
+
if (parts.some((part) => FORBIDDEN_KEYS.has(part) || part === "")) continue;
|
|
77
|
+
let node = out;
|
|
78
|
+
for (let index = 0; index < parts.length - 1; index += 1) {
|
|
79
|
+
const part = parts[index];
|
|
80
|
+
if (!isPlainObject(node[part])) node[part] = {};
|
|
81
|
+
node = node[part];
|
|
82
|
+
}
|
|
83
|
+
node[parts[parts.length - 1]] = flat[key];
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Merges a platform's theme-message overrides over the theme's own manifest defaults: **platform
|
|
89
|
+
* wins per key**, and a locale the theme never shipped is added **whole** — Studio may translate a
|
|
90
|
+
* key set the theme only declared for its own locales into a locale nobody on the theme side ever
|
|
91
|
+
* wrote a file for.
|
|
92
|
+
*
|
|
93
|
+
* `themeDefaults.defaultLocale` is carried through unchanged: it names the theme's own default
|
|
94
|
+
* locale, the last tier of `resolveMessageCatalogue`'s fallback chain, and merging never changes
|
|
95
|
+
* which locale that is (the platform's own response, when one exists, carries the *organisation's*
|
|
96
|
+
* default locale under the same field name — a different concept, threaded separately into
|
|
97
|
+
* `resolveMessageCatalogue` as `orgDefaultLocale`).
|
|
98
|
+
*
|
|
99
|
+
* `platform: null` (no gateway, or the read failed) is the identity merge: a clone of
|
|
100
|
+
* `themeDefaults`, unchanged.
|
|
101
|
+
*/
|
|
102
|
+
function mergeMessageCatalogues(themeDefaults, platform) {
|
|
103
|
+
if (platform === null) return {
|
|
104
|
+
defaultLocale: themeDefaults.defaultLocale,
|
|
105
|
+
locales: cloneLocales(themeDefaults.locales)
|
|
106
|
+
};
|
|
107
|
+
const tags = /* @__PURE__ */ new Set([...Object.keys(themeDefaults.locales), ...Object.keys(platform.locales)]);
|
|
108
|
+
const locales = {};
|
|
109
|
+
for (const tag of [...tags].sort((a, b) => a.localeCompare(b))) {
|
|
110
|
+
if (isForbiddenLocaleTag(tag)) continue;
|
|
111
|
+
const theme = themeDefaults.locales[tag];
|
|
112
|
+
const override = platform.locales[tag];
|
|
113
|
+
locales[tag] = sortedStringRecord({
|
|
114
|
+
...theme,
|
|
115
|
+
...override
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
defaultLocale: themeDefaults.defaultLocale,
|
|
120
|
+
locales
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Implements the contract's five-tier per-key fallback, to the letter:
|
|
125
|
+
* `override(locale) → theme(locale) → override(orgDefault) → theme(orgDefault) → theme(themeDefault)`.
|
|
126
|
+
*
|
|
127
|
+
* Takes `themeDefaults` and `platform` **separately** — not only a pre-merged catalogue — because
|
|
128
|
+
* tier 5 is explicitly `theme(themeDefault)` with **no** accompanying override tier: every other
|
|
129
|
+
* locale gets both an override and a theme tier, but the terminal safety net deliberately does
|
|
130
|
+
* not, so an override on the theme's own default locale must never leak into a key that falls all
|
|
131
|
+
* the way through to tier 5 for an unrelated locale. Tiers 1–4 *do* collapse into two
|
|
132
|
+
* locale-level lookups, since `mergeMessageCatalogues(themeDefaults, platform)` already fuses
|
|
133
|
+
* override and theme per locale:
|
|
134
|
+
*
|
|
135
|
+
* 1. `merged.locales[orgLocale]` — tiers 1+2 (the organisation's own locale, platform override or
|
|
136
|
+
* theme default, whichever the merge kept);
|
|
137
|
+
* 2. `merged.locales[orgDefaultLocale]` — tiers 3+4;
|
|
138
|
+
* 3. `themeDefaults.locales[themeDefaults.defaultLocale]` — tier 5, the theme's own shipped
|
|
139
|
+
* default-locale value, read from the **unmerged** input, never the platform's override of it.
|
|
140
|
+
*
|
|
141
|
+
* The full key set is the theme's own default-locale keys: every override is validated (Core's
|
|
142
|
+
* `UNKNOWN_KEY`) against that set before it can reach this function, so no key anywhere in the
|
|
143
|
+
* merged catalogue ever falls outside it. Every `orgLocale` — plus `orgDefaultLocale` itself, added
|
|
144
|
+
* even if the caller's list omitted it — comes back with that full key set, which is what lets a
|
|
145
|
+
* **credential-less build** (`platform: null` going in) still produce a complete catalogue for
|
|
146
|
+
* every locale from the manifest alone.
|
|
147
|
+
*/
|
|
148
|
+
function resolveMessageCatalogue(themeDefaults, platform, orgLocales, orgDefaultLocale) {
|
|
149
|
+
const merged = mergeMessageCatalogues(themeDefaults, platform);
|
|
150
|
+
const themeDefault = sortedStringRecord(themeDefaults.locales[themeDefaults.defaultLocale] ?? {});
|
|
151
|
+
const keys = Object.keys(themeDefault).sort((a, b) => a.localeCompare(b));
|
|
152
|
+
const orgDefault = merged.locales[orgDefaultLocale];
|
|
153
|
+
const tags = new Set(orgLocales);
|
|
154
|
+
tags.add(orgDefaultLocale);
|
|
155
|
+
const locales = {};
|
|
156
|
+
for (const tag of [...tags].sort((a, b) => a.localeCompare(b))) {
|
|
157
|
+
if (isForbiddenLocaleTag(tag)) continue;
|
|
158
|
+
const own = merged.locales[tag];
|
|
159
|
+
const resolved = {};
|
|
160
|
+
for (const key of keys) {
|
|
161
|
+
const value = own?.[key] ?? orgDefault?.[key] ?? themeDefault[key];
|
|
162
|
+
if (value !== void 0) resolved[key] = value;
|
|
163
|
+
}
|
|
164
|
+
locales[tag] = resolved;
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
defaultLocale: orgDefaultLocale,
|
|
168
|
+
locales
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function cloneLocales(locales) {
|
|
172
|
+
const out = {};
|
|
173
|
+
for (const tag of Object.keys(locales).sort((a, b) => a.localeCompare(b))) {
|
|
174
|
+
if (isForbiddenLocaleTag(tag)) continue;
|
|
175
|
+
out[tag] = sortedStringRecord(locales[tag] ?? {});
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
function sortedStringRecord(value) {
|
|
180
|
+
const out = {};
|
|
181
|
+
for (const key of Object.keys(value).sort((a, b) => a.localeCompare(b))) {
|
|
182
|
+
if (FORBIDDEN_KEYS.has(key)) continue;
|
|
183
|
+
const entry = value[key];
|
|
184
|
+
if (typeof entry === "string") out[key] = entry;
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
function isPlainObject(value) {
|
|
189
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
export { EMPTY_THEME_MESSAGES, flattenMessages, isForbiddenLocaleTag, mergeMessageCatalogues, resolveMessageCatalogue, sanitizeLocaleMessages, unflattenMessages };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as ImageFraming, c as focalBand, d as imageFramingAttrs, f as imageFramingStyle, h as unscaledFrameRect, i as IMAGE_FRAMING_MIN_ZOOM, l as framingDrag, m as parseImageFramingValueAttr, n as FrameRect, o as clampFocal, p as normalizeImageFraming, r as IMAGE_FRAMING_MAX_ZOOM, s as clampImageFraming, t as DEFAULT_IMAGE_FRAMING, u as framingZoom } from "./imageFraming-BUZ-yqho.js";
|
|
2
|
+
export { DEFAULT_IMAGE_FRAMING, FrameRect, IMAGE_FRAMING_MAX_ZOOM, IMAGE_FRAMING_MIN_ZOOM, ImageFraming, clampFocal, clampImageFraming, focalBand, framingDrag, framingZoom, imageFramingAttrs, imageFramingStyle, normalizeImageFraming, parseImageFramingValueAttr, unscaledFrameRect };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
//#region src/imageFraming.ts
|
|
2
|
+
const IMAGE_FRAMING_MIN_ZOOM = 1;
|
|
3
|
+
const IMAGE_FRAMING_MAX_ZOOM = 4;
|
|
4
|
+
const DEFAULT_IMAGE_FRAMING = Object.freeze({
|
|
5
|
+
x: .5,
|
|
6
|
+
y: .5,
|
|
7
|
+
zoom: 1
|
|
8
|
+
});
|
|
9
|
+
const WHEEL_ZOOM_RATE = .002;
|
|
10
|
+
const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
|
|
11
|
+
const finite = (value) => typeof value === "number" && Number.isFinite(value);
|
|
12
|
+
/** Focal band for a zoom: the origin range that keeps the scaled image covering the frame. */
|
|
13
|
+
function focalBand(zoom) {
|
|
14
|
+
const inset = (1 - 1 / clamp(finite(zoom) ? zoom : 1, 1, 4)) / 2;
|
|
15
|
+
return [inset, 1 - inset];
|
|
16
|
+
}
|
|
17
|
+
function clampFocal(value, zoom) {
|
|
18
|
+
const [low, high] = focalBand(zoom);
|
|
19
|
+
return clamp(finite(value) ? value : .5, low, high);
|
|
20
|
+
}
|
|
21
|
+
function clampImageFraming(framing) {
|
|
22
|
+
const zoom = clamp(finite(framing.zoom) ? framing.zoom : 1, 1, 4);
|
|
23
|
+
return {
|
|
24
|
+
x: clampFocal(framing.x, zoom),
|
|
25
|
+
y: clampFocal(framing.y, zoom),
|
|
26
|
+
zoom
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** Accepts unknown input; returns a clamped value or null when the shape is wrong. */
|
|
30
|
+
function normalizeImageFraming(value) {
|
|
31
|
+
if (value === null || typeof value !== "object") return null;
|
|
32
|
+
const { x, y, zoom } = value;
|
|
33
|
+
if (!finite(x) || !finite(y) || !finite(zoom)) return null;
|
|
34
|
+
return {
|
|
35
|
+
x: clamp(x, 0, 1),
|
|
36
|
+
y: clamp(y, 0, 1),
|
|
37
|
+
zoom: clamp(zoom, 1, 4)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** Pointer drag: moving the picture right reveals more of its left side. */
|
|
41
|
+
function framingDrag(framing, dx, dy, frameWidth, frameHeight) {
|
|
42
|
+
if (!(frameWidth > 0) || !(frameHeight > 0)) return framing;
|
|
43
|
+
return clampImageFraming({
|
|
44
|
+
x: framing.x - dx / frameWidth / framing.zoom,
|
|
45
|
+
y: framing.y - dy / frameHeight / framing.zoom,
|
|
46
|
+
zoom: framing.zoom
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Wheel / trackpad pinch: exponential zoom, focal point re-clamped to the new band. */
|
|
50
|
+
function framingZoom(framing, deltaY) {
|
|
51
|
+
const delta = finite(deltaY) ? deltaY : 0;
|
|
52
|
+
return clampImageFraming({
|
|
53
|
+
x: framing.x,
|
|
54
|
+
y: framing.y,
|
|
55
|
+
zoom: framing.zoom * Math.exp(-delta * WHEEL_ZOOM_RATE)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const percent = (value) => `${Number((value * 100).toFixed(4))}%`;
|
|
59
|
+
function imageFramingStyle(framing) {
|
|
60
|
+
const value = framing ? normalizeImageFraming(framing) : null;
|
|
61
|
+
if (value === null) return {
|
|
62
|
+
"object-fit": "cover",
|
|
63
|
+
"object-position": "50% 50%"
|
|
64
|
+
};
|
|
65
|
+
const style = {
|
|
66
|
+
"object-fit": "cover",
|
|
67
|
+
"object-position": `${percent(value.x)} ${percent(value.y)}`
|
|
68
|
+
};
|
|
69
|
+
if (value.zoom > 1) {
|
|
70
|
+
style.transform = `scale(${Number(value.zoom.toFixed(4))})`;
|
|
71
|
+
style["transform-origin"] = `${percent(clampFocal(value.x, value.zoom))} ${percent(clampFocal(value.y, value.zoom))}`;
|
|
72
|
+
}
|
|
73
|
+
return style;
|
|
74
|
+
}
|
|
75
|
+
function imageFramingAttrs(entryId, fieldPath, framing) {
|
|
76
|
+
const value = (framing ? normalizeImageFraming(framing) : null) ?? DEFAULT_IMAGE_FRAMING;
|
|
77
|
+
return {
|
|
78
|
+
style: imageFramingStyle(value),
|
|
79
|
+
"data-eldra-framing": fieldPath,
|
|
80
|
+
"data-eldra-framing-entry": entryId,
|
|
81
|
+
"data-eldra-framing-value": `${value.x},${value.y},${value.zoom}`
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function parseImageFramingValueAttr(value) {
|
|
85
|
+
if (typeof value !== "string") return { ...DEFAULT_IMAGE_FRAMING };
|
|
86
|
+
const [x, y, zoom] = value.split(",").map(Number);
|
|
87
|
+
return normalizeImageFraming({
|
|
88
|
+
x,
|
|
89
|
+
y,
|
|
90
|
+
zoom
|
|
91
|
+
}) ?? { ...DEFAULT_IMAGE_FRAMING };
|
|
92
|
+
}
|
|
93
|
+
/** A scaled <img> reports its transformed box; recover the layout frame. */
|
|
94
|
+
function unscaledFrameRect(rect, framing) {
|
|
95
|
+
const zoom = clamp(finite(framing.zoom) ? framing.zoom : 1, 1, 4);
|
|
96
|
+
if (zoom === 1) return { ...rect };
|
|
97
|
+
const width = rect.width / zoom;
|
|
98
|
+
const height = rect.height / zoom;
|
|
99
|
+
const ox = clampFocal(framing.x, zoom);
|
|
100
|
+
const oy = clampFocal(framing.y, zoom);
|
|
101
|
+
return {
|
|
102
|
+
x: rect.x + ox * (rect.width - width),
|
|
103
|
+
y: rect.y + oy * (rect.height - height),
|
|
104
|
+
width,
|
|
105
|
+
height
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
export { DEFAULT_IMAGE_FRAMING, IMAGE_FRAMING_MAX_ZOOM, IMAGE_FRAMING_MIN_ZOOM, clampFocal, clampImageFraming, focalBand, framingDrag, framingZoom, imageFramingAttrs, imageFramingStyle, normalizeImageFraming, parseImageFramingValueAttr, unscaledFrameRect };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region src/imageFraming.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* §17 image framing: pure maths and style helpers shared by the preview
|
|
4
|
+
* overlay, the static output and Studio (which mirrors this module).
|
|
5
|
+
*/
|
|
6
|
+
interface ImageFraming {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
zoom: number;
|
|
10
|
+
}
|
|
11
|
+
declare const IMAGE_FRAMING_MIN_ZOOM = 1;
|
|
12
|
+
declare const IMAGE_FRAMING_MAX_ZOOM = 4;
|
|
13
|
+
declare const DEFAULT_IMAGE_FRAMING: Readonly<ImageFraming>;
|
|
14
|
+
/** Focal band for a zoom: the origin range that keeps the scaled image covering the frame. */
|
|
15
|
+
declare function focalBand(zoom: number): [number, number];
|
|
16
|
+
declare function clampFocal(value: number, zoom: number): number;
|
|
17
|
+
declare function clampImageFraming(framing: ImageFraming): ImageFraming;
|
|
18
|
+
/** Accepts unknown input; returns a clamped value or null when the shape is wrong. */
|
|
19
|
+
declare function normalizeImageFraming(value: unknown): ImageFraming | null;
|
|
20
|
+
/** Pointer drag: moving the picture right reveals more of its left side. */
|
|
21
|
+
declare function framingDrag(framing: ImageFraming, dx: number, dy: number, frameWidth: number, frameHeight: number): ImageFraming;
|
|
22
|
+
/** Wheel / trackpad pinch: exponential zoom, focal point re-clamped to the new band. */
|
|
23
|
+
declare function framingZoom(framing: ImageFraming, deltaY: number): ImageFraming;
|
|
24
|
+
declare function imageFramingStyle(framing?: ImageFraming | null): Record<string, string>;
|
|
25
|
+
declare function imageFramingAttrs(entryId: string, fieldPath: string, framing?: ImageFraming | null): {
|
|
26
|
+
style: Record<string, string>;
|
|
27
|
+
'data-eldra-framing': string;
|
|
28
|
+
'data-eldra-framing-entry': string;
|
|
29
|
+
'data-eldra-framing-value': string;
|
|
30
|
+
};
|
|
31
|
+
declare function parseImageFramingValueAttr(value: string | null | undefined): ImageFraming;
|
|
32
|
+
interface FrameRect {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
}
|
|
38
|
+
/** A scaled <img> reports its transformed box; recover the layout frame. */
|
|
39
|
+
declare function unscaledFrameRect(rect: {
|
|
40
|
+
x: number;
|
|
41
|
+
y: number;
|
|
42
|
+
width: number;
|
|
43
|
+
height: number;
|
|
44
|
+
}, framing: ImageFraming): {
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
};
|
|
50
|
+
//#endregion
|
|
51
|
+
export { ImageFraming as a, focalBand as c, imageFramingAttrs as d, imageFramingStyle as f, unscaledFrameRect as h, IMAGE_FRAMING_MIN_ZOOM as i, framingDrag as l, parseImageFramingValueAttr as m, FrameRect as n, clampFocal as o, normalizeImageFraming as p, IMAGE_FRAMING_MAX_ZOOM as r, clampImageFraming as s, DEFAULT_IMAGE_FRAMING as t, framingZoom as u };
|