verbaly 0.16.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/devtools.cjs +7 -4
- package/dist/devtools.cjs.map +1 -1
- package/dist/devtools.d.cts +1 -1
- package/dist/devtools.d.ts +1 -1
- package/dist/devtools.js +7 -4
- package/dist/devtools.js.map +1 -1
- package/dist/index.cjs +150 -94
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +147 -95
- package/dist/index.js.map +1 -1
- package/dist/{types-B4g2nUEf.d.ts → types-BTWCYu7Y.d.cts} +2 -2
- package/dist/{types-B4g2nUEf.d.cts → types-BTWCYu7Y.d.ts} +2 -2
- package/package.json +2 -2
package/dist/devtools.cjs
CHANGED
|
@@ -6,6 +6,9 @@ const HOTKEY = {
|
|
|
6
6
|
shift: "shiftKey",
|
|
7
7
|
meta: "metaKey"
|
|
8
8
|
};
|
|
9
|
+
function statusOf(info, currentLocale) {
|
|
10
|
+
return !info ? "miss" : info.from === currentLocale ? "hit" : "fallback";
|
|
11
|
+
}
|
|
9
12
|
function attachDevtools(instance, options = {}) {
|
|
10
13
|
if (typeof document === "undefined") throw new Error("[verbaly] devtools requires a DOM");
|
|
11
14
|
const root = options.root ?? document.body;
|
|
@@ -31,8 +34,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
31
34
|
out.push({
|
|
32
35
|
el,
|
|
33
36
|
key,
|
|
34
|
-
status:
|
|
35
|
-
from: info?.
|
|
37
|
+
status: statusOf(info, instance.locale),
|
|
38
|
+
from: info?.from,
|
|
36
39
|
source: info?.source
|
|
37
40
|
});
|
|
38
41
|
}
|
|
@@ -57,8 +60,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
57
60
|
}
|
|
58
61
|
const key = el.getAttribute(attr);
|
|
59
62
|
const info = instance.inspect(key);
|
|
60
|
-
const status =
|
|
61
|
-
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.
|
|
63
|
+
const status = statusOf(info, instance.locale);
|
|
64
|
+
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ""}</div>` + (info ? `<div class="verbaly-dt-src">${esc(info.source)}</div>` : "");
|
|
62
65
|
tip.hidden = false;
|
|
63
66
|
tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;
|
|
64
67
|
tip.style.top = `${e.clientY + 14}px`;
|
package/dist/devtools.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devtools.cjs","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status:
|
|
1
|
+
{"version":3,"file":"devtools.cjs","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\nfunction statusOf(\n info: { from: string } | undefined,\n currentLocale: string,\n): ResolveStatus {\n return !info ? 'miss' : info.from === currentLocale ? 'hit' : 'fallback';\n}\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status: statusOf(info, instance.locale),\n from: info?.from,\n source: info?.source,\n });\n }\n return out;\n }\n\n function render(): void {\n const bound = scan();\n const miss = bound.filter((b) => b.status === 'miss');\n const fb = bound.filter((b) => b.status === 'fallback');\n const rows = miss\n .map(\n (b) =>\n `<li><code>${esc(b.key)}</code><span>add to <code>${esc(dir)}/${esc(instance.locale)}.json</code></span></li>`,\n )\n .join('');\n panel.innerHTML =\n `<div class=\"verbaly-dt-hd\"><b>verbaly</b> · ${esc(instance.locale)}</div>` +\n `<div class=\"verbaly-dt-stats\">` +\n `<span class=\"ok\">${bound.length - miss.length - fb.length} ok</span>` +\n `<span class=\"fb\">${fb.length} fallback</span>` +\n `<span class=\"miss\">${miss.length} missing</span></div>` +\n (rows ? `<ul class=\"verbaly-dt-miss\">${rows}</ul>` : '') +\n `<div class=\"verbaly-dt-hint\">hold <kbd>${options.hotkey ?? 'Alt'}</kbd> + hover to inspect</div>`;\n }\n\n function onMove(e: MouseEvent): void {\n if (!e[modifier]) {\n tip.hidden = true;\n return;\n }\n const el = (e.target as Element | null)?.closest?.(`[${attr}]`);\n if (!el) {\n tip.hidden = true;\n return;\n }\n const key = el.getAttribute(attr)!;\n const info = instance.inspect(key);\n const status = statusOf(info, instance.locale);\n tip.innerHTML =\n `<div class=\"verbaly-dt-key\">${esc(key)}</div>` +\n `<div class=\"verbaly-dt-row ${status}\">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ''}</div>` +\n (info ? `<div class=\"verbaly-dt-src\">${esc(info.source)}</div>` : '');\n tip.hidden = false;\n tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;\n tip.style.top = `${e.clientY + 14}px`;\n }\n\n const unsub = instance.subscribe(render);\n // own panel/tip writes must not re-trigger render (infinite microtask loop)\n const observer = new MutationObserver((records) => {\n if (records.every((r) => panel.contains(r.target) || tip.contains(r.target))) return;\n render();\n });\n observer.observe(root instanceof Node ? root : document.body, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: [attr],\n });\n document.addEventListener('mousemove', onMove);\n render();\n\n return () => {\n unsub();\n observer.disconnect();\n document.removeEventListener('mousemove', onMove);\n panel.remove();\n tip.remove();\n style.remove();\n };\n}\n\nfunction esc(text: string): string {\n return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n}\n\nconst CSS = `\n.verbaly-dt{position:fixed;bottom:12px;right:12px;z-index:2147483646;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:10px;padding:10px;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-hd{font-size:11px;opacity:.85;margin-bottom:6px}\n.verbaly-dt-stats{display:flex;gap:8px;margin-bottom:6px}\n.verbaly-dt-stats span{padding:2px 6px;border-radius:6px;background:#152016}\n.verbaly-dt-stats .ok{color:#bef12d}\n.verbaly-dt-stats .fb{color:#f1c40f}\n.verbaly-dt-stats .miss{color:#ff6b6b}\n.verbaly-dt-miss{list-style:none;margin:0 0 6px;padding:0;max-height:180px;overflow:auto}\n.verbaly-dt-miss li{display:flex;flex-direction:column;gap:2px;padding:4px 0;border-top:1px solid #1c281d}\n.verbaly-dt-miss span{opacity:.6;font-size:11px}\n.verbaly-dt-hint{opacity:.55;font-size:11px}\n.verbaly-dt-hint kbd{background:#152016;border-radius:4px;padding:0 4px}\n.verbaly-dt-tip{position:fixed;z-index:2147483647;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:8px;padding:8px;pointer-events:none;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-key{color:#bef12d;font-weight:600;margin-bottom:4px}\n.verbaly-dt-row{margin-bottom:4px}\n.verbaly-dt-row.miss{color:#ff6b6b}\n.verbaly-dt-row.fallback{color:#f1c40f}\n.verbaly-dt-row.hit{color:#7bd88f}\n.verbaly-dt-src{opacity:.75;white-space:pre-wrap;word-break:break-word}\n`;\n"],"mappings":";;AAiBA,MAAM,SAA2E;CAC/E,KAAK;CACL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AAa,MAAM;CAC7C,OAAO;CAEP,aAAa;EACX,MAAM;EACN,SAAS,WAAW;EACpB,SAAS,oBAAoB,aAAa,MAAM;EAChD,MAAM,OAAO;EACb,IAAI,OAAO;EACX,MAAM,OAAO;CACf;AACF;AAEA,SAAS,IAAI,MAAsB;CACjC,OAAO,KAAK,QAAQ,MAAM,OAAO,CAAC,CAAC,QAAQ,MAAM,MAAM,CAAC,CAAC,QAAQ,MAAM,MAAM;AAC/E;AAEA,MAAM,MAAM"}
|
package/dist/devtools.d.cts
CHANGED
package/dist/devtools.d.ts
CHANGED
package/dist/devtools.js
CHANGED
|
@@ -5,6 +5,9 @@ const HOTKEY = {
|
|
|
5
5
|
shift: "shiftKey",
|
|
6
6
|
meta: "metaKey"
|
|
7
7
|
};
|
|
8
|
+
function statusOf(info, currentLocale) {
|
|
9
|
+
return !info ? "miss" : info.from === currentLocale ? "hit" : "fallback";
|
|
10
|
+
}
|
|
8
11
|
function attachDevtools(instance, options = {}) {
|
|
9
12
|
if (typeof document === "undefined") throw new Error("[verbaly] devtools requires a DOM");
|
|
10
13
|
const root = options.root ?? document.body;
|
|
@@ -30,8 +33,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
30
33
|
out.push({
|
|
31
34
|
el,
|
|
32
35
|
key,
|
|
33
|
-
status:
|
|
34
|
-
from: info?.
|
|
36
|
+
status: statusOf(info, instance.locale),
|
|
37
|
+
from: info?.from,
|
|
35
38
|
source: info?.source
|
|
36
39
|
});
|
|
37
40
|
}
|
|
@@ -56,8 +59,8 @@ function attachDevtools(instance, options = {}) {
|
|
|
56
59
|
}
|
|
57
60
|
const key = el.getAttribute(attr);
|
|
58
61
|
const info = instance.inspect(key);
|
|
59
|
-
const status =
|
|
60
|
-
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.
|
|
62
|
+
const status = statusOf(info, instance.locale);
|
|
63
|
+
tip.innerHTML = `<div class="verbaly-dt-key">${esc(key)}</div><div class="verbaly-dt-row ${status}">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ""}</div>` + (info ? `<div class="verbaly-dt-src">${esc(info.source)}</div>` : "");
|
|
61
64
|
tip.hidden = false;
|
|
62
65
|
tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;
|
|
63
66
|
tip.style.top = `${e.clientY + 14}px`;
|
package/dist/devtools.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status:
|
|
1
|
+
{"version":3,"file":"devtools.js","names":[],"sources":["../src/devtools.ts"],"sourcesContent":["import type { ResolveStatus, Verbaly } from './types';\n\nexport interface DevtoolsOptions {\n root?: ParentNode;\n attribute?: string;\n hotkey?: 'alt' | 'ctrl' | 'shift' | 'meta';\n catalogDir?: string;\n}\n\ninterface Bound {\n el: Element;\n key: string;\n status: ResolveStatus;\n from?: string;\n source?: string;\n}\n\nconst HOTKEY: Record<NonNullable<DevtoolsOptions['hotkey']>, keyof MouseEvent> = {\n alt: 'altKey',\n ctrl: 'ctrlKey',\n shift: 'shiftKey',\n meta: 'metaKey',\n};\n\nfunction statusOf(\n info: { from: string } | undefined,\n currentLocale: string,\n): ResolveStatus {\n return !info ? 'miss' : info.from === currentLocale ? 'hit' : 'fallback';\n}\n\n// dev-only inspector — \"what key is this text?\" in the browser (opt-in, tree-shakeable)\nexport function attachDevtools(instance: Verbaly, options: DevtoolsOptions = {}): () => void {\n if (typeof document === 'undefined') throw new Error('[verbaly] devtools requires a DOM');\n const root = options.root ?? document.body;\n const attr = options.attribute ?? 'data-verbaly';\n const modifier = HOTKEY[options.hotkey ?? 'alt'];\n const dir = options.catalogDir ?? 'locales';\n\n const style = document.createElement('style');\n style.textContent = CSS;\n document.head.appendChild(style);\n\n const panel = document.createElement('div');\n panel.className = 'verbaly-dt';\n root.appendChild(panel);\n\n const tip = document.createElement('div');\n tip.className = 'verbaly-dt-tip';\n tip.hidden = true;\n root.appendChild(tip);\n\n function scan(): Bound[] {\n const out: Bound[] = [];\n for (const el of root.querySelectorAll(`[${attr}]`)) {\n const key = el.getAttribute(attr);\n if (!key) continue;\n const info = instance.inspect(key);\n out.push({\n el,\n key,\n status: statusOf(info, instance.locale),\n from: info?.from,\n source: info?.source,\n });\n }\n return out;\n }\n\n function render(): void {\n const bound = scan();\n const miss = bound.filter((b) => b.status === 'miss');\n const fb = bound.filter((b) => b.status === 'fallback');\n const rows = miss\n .map(\n (b) =>\n `<li><code>${esc(b.key)}</code><span>add to <code>${esc(dir)}/${esc(instance.locale)}.json</code></span></li>`,\n )\n .join('');\n panel.innerHTML =\n `<div class=\"verbaly-dt-hd\"><b>verbaly</b> · ${esc(instance.locale)}</div>` +\n `<div class=\"verbaly-dt-stats\">` +\n `<span class=\"ok\">${bound.length - miss.length - fb.length} ok</span>` +\n `<span class=\"fb\">${fb.length} fallback</span>` +\n `<span class=\"miss\">${miss.length} missing</span></div>` +\n (rows ? `<ul class=\"verbaly-dt-miss\">${rows}</ul>` : '') +\n `<div class=\"verbaly-dt-hint\">hold <kbd>${options.hotkey ?? 'Alt'}</kbd> + hover to inspect</div>`;\n }\n\n function onMove(e: MouseEvent): void {\n if (!e[modifier]) {\n tip.hidden = true;\n return;\n }\n const el = (e.target as Element | null)?.closest?.(`[${attr}]`);\n if (!el) {\n tip.hidden = true;\n return;\n }\n const key = el.getAttribute(attr)!;\n const info = instance.inspect(key);\n const status = statusOf(info, instance.locale);\n tip.innerHTML =\n `<div class=\"verbaly-dt-key\">${esc(key)}</div>` +\n `<div class=\"verbaly-dt-row ${status}\">${status}${info && info.from !== instance.locale ? ` · from ${esc(info.from)}` : ''}</div>` +\n (info ? `<div class=\"verbaly-dt-src\">${esc(info.source)}</div>` : '');\n tip.hidden = false;\n tip.style.left = `${Math.min(e.clientX + 14, window.innerWidth - 320)}px`;\n tip.style.top = `${e.clientY + 14}px`;\n }\n\n const unsub = instance.subscribe(render);\n // own panel/tip writes must not re-trigger render (infinite microtask loop)\n const observer = new MutationObserver((records) => {\n if (records.every((r) => panel.contains(r.target) || tip.contains(r.target))) return;\n render();\n });\n observer.observe(root instanceof Node ? root : document.body, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: [attr],\n });\n document.addEventListener('mousemove', onMove);\n render();\n\n return () => {\n unsub();\n observer.disconnect();\n document.removeEventListener('mousemove', onMove);\n panel.remove();\n tip.remove();\n style.remove();\n };\n}\n\nfunction esc(text: string): string {\n return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n}\n\nconst CSS = `\n.verbaly-dt{position:fixed;bottom:12px;right:12px;z-index:2147483646;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:10px;padding:10px;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-hd{font-size:11px;opacity:.85;margin-bottom:6px}\n.verbaly-dt-stats{display:flex;gap:8px;margin-bottom:6px}\n.verbaly-dt-stats span{padding:2px 6px;border-radius:6px;background:#152016}\n.verbaly-dt-stats .ok{color:#bef12d}\n.verbaly-dt-stats .fb{color:#f1c40f}\n.verbaly-dt-stats .miss{color:#ff6b6b}\n.verbaly-dt-miss{list-style:none;margin:0 0 6px;padding:0;max-height:180px;overflow:auto}\n.verbaly-dt-miss li{display:flex;flex-direction:column;gap:2px;padding:4px 0;border-top:1px solid #1c281d}\n.verbaly-dt-miss span{opacity:.6;font-size:11px}\n.verbaly-dt-hint{opacity:.55;font-size:11px}\n.verbaly-dt-hint kbd{background:#152016;border-radius:4px;padding:0 4px}\n.verbaly-dt-tip{position:fixed;z-index:2147483647;max-width:300px;font:12px/1.4 ui-monospace,monospace;color:#e6f0e0;background:#0b120c;border:1px solid #2a3a2c;border-radius:8px;padding:8px;pointer-events:none;box-shadow:0 8px 30px rgba(0,0,0,.4)}\n.verbaly-dt-key{color:#bef12d;font-weight:600;margin-bottom:4px}\n.verbaly-dt-row{margin-bottom:4px}\n.verbaly-dt-row.miss{color:#ff6b6b}\n.verbaly-dt-row.fallback{color:#f1c40f}\n.verbaly-dt-row.hit{color:#7bd88f}\n.verbaly-dt-src{opacity:.75;white-space:pre-wrap;word-break:break-word}\n`;\n"],"mappings":";AAiBA,MAAM,SAA2E;CAC/E,KAAK;CACL,MAAM;CACN,OAAO;CACP,MAAM;AACR;AAEA,SAAS,SACP,MACA,eACe;CACf,OAAO,CAAC,OAAO,SAAS,KAAK,SAAS,gBAAgB,QAAQ;AAChE;AAGA,SAAgB,eAAe,UAA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O;CACf;AACF;AAEA,SAAS,IAAI,MAAsB;CACjC,OAAO,KAAK,QAAQ,MAAM,OAAO,CAAC,CAAC,QAAQ,MAAM,MAAM,CAAC,CAAC,QAAQ,MAAM,MAAM;AAC/E;AAEA,MAAM,MAAM"}
|
package/dist/index.cjs
CHANGED
|
@@ -84,12 +84,27 @@ function applyFormat(value, format, arg, ctx) {
|
|
|
84
84
|
case "percent": return numberFormat(locale, { style: "percent" }).format(Number(value));
|
|
85
85
|
case "currency":
|
|
86
86
|
if (!arg) return String(value);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
try {
|
|
88
|
+
return numberFormat(locale, {
|
|
89
|
+
style: "currency",
|
|
90
|
+
currency: arg
|
|
91
|
+
}).format(Number(value));
|
|
92
|
+
} catch {
|
|
93
|
+
warnOnce(`invalid currency "${arg}"`);
|
|
94
|
+
return String(value);
|
|
95
|
+
}
|
|
96
|
+
case "date": try {
|
|
97
|
+
return dateTimeFormat(locale, arg ? { dateStyle: arg } : void 0).format(toDate(value));
|
|
98
|
+
} catch {
|
|
99
|
+
warnOnce(`invalid date "${arg ?? value}"`);
|
|
100
|
+
return String(value);
|
|
101
|
+
}
|
|
102
|
+
case "time": try {
|
|
103
|
+
return dateTimeFormat(locale, { timeStyle: arg ?? "short" }).format(toDate(value));
|
|
104
|
+
} catch {
|
|
105
|
+
warnOnce(`invalid time "${arg ?? value}"`);
|
|
106
|
+
return String(value);
|
|
107
|
+
}
|
|
93
108
|
case "relative": return formatRelative(value, arg, locale);
|
|
94
109
|
case "list":
|
|
95
110
|
if (!Array.isArray(value)) return String(value);
|
|
@@ -153,6 +168,99 @@ function toDate(value) {
|
|
|
153
168
|
return value instanceof Date ? value : new Date(value);
|
|
154
169
|
}
|
|
155
170
|
//#endregion
|
|
171
|
+
//#region src/locale.ts
|
|
172
|
+
const LOCALE_STORAGE_KEY = "verbaly-locale";
|
|
173
|
+
function narrowLocales(lang) {
|
|
174
|
+
const out = [];
|
|
175
|
+
const parts = lang.split("-");
|
|
176
|
+
while (parts.length > 0) {
|
|
177
|
+
out.push(parts.join("-"));
|
|
178
|
+
parts.pop();
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
function resolveLocale(options) {
|
|
183
|
+
const { supported, fallback = supported[0] ?? "en", storageKey = LOCALE_STORAGE_KEY } = options;
|
|
184
|
+
if (storageKey) {
|
|
185
|
+
const stored = getStorage()?.getItem(storageKey);
|
|
186
|
+
const match = stored && matchSupported(stored, supported);
|
|
187
|
+
if (match) return match;
|
|
188
|
+
}
|
|
189
|
+
for (const lang of preferredLanguages()) {
|
|
190
|
+
const match = matchSupported(lang, supported);
|
|
191
|
+
if (match) return match;
|
|
192
|
+
}
|
|
193
|
+
return fallback;
|
|
194
|
+
}
|
|
195
|
+
function matchSupported(lang, supported) {
|
|
196
|
+
for (const candidate of narrowLocales(lang)) if (supported.includes(candidate)) return candidate;
|
|
197
|
+
}
|
|
198
|
+
function negotiateLocale(header, supported, fallback = supported[0] ?? "en") {
|
|
199
|
+
if (!header) return fallback;
|
|
200
|
+
const ranges = [];
|
|
201
|
+
for (const part of header.split(",")) {
|
|
202
|
+
const [rawTag, ...params] = part.trim().split(";");
|
|
203
|
+
const tag = rawTag?.trim();
|
|
204
|
+
if (!tag || tag === "*") continue;
|
|
205
|
+
let q = 1;
|
|
206
|
+
for (const param of params) {
|
|
207
|
+
const [name, value] = param.split("=");
|
|
208
|
+
if (name?.trim().toLowerCase() === "q") {
|
|
209
|
+
const parsed = Number(value?.trim());
|
|
210
|
+
q = Number.isFinite(parsed) ? parsed : 1;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (q > 0) ranges.push({
|
|
214
|
+
tag,
|
|
215
|
+
q
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
ranges.sort((a, b) => b.q - a.q);
|
|
219
|
+
for (const { tag } of ranges) {
|
|
220
|
+
const match = matchSupportedLoose(tag, supported);
|
|
221
|
+
if (match) return match;
|
|
222
|
+
}
|
|
223
|
+
return fallback;
|
|
224
|
+
}
|
|
225
|
+
function matchSupportedLoose(lang, supported) {
|
|
226
|
+
for (const candidate of narrowLocales(lang.toLowerCase())) {
|
|
227
|
+
const match = supported.find((s) => s.toLowerCase() === candidate);
|
|
228
|
+
if (match) return match;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function resolveRequestLocale(options) {
|
|
232
|
+
const { supported, cookie, header, fallback = supported[0] ?? "en" } = options;
|
|
233
|
+
if (cookie) {
|
|
234
|
+
const match = negotiateLocale(cookie, supported, "");
|
|
235
|
+
if (match) return match;
|
|
236
|
+
}
|
|
237
|
+
return negotiateLocale(header, supported, fallback);
|
|
238
|
+
}
|
|
239
|
+
const YEAR = 31536e3;
|
|
240
|
+
async function switchLocale(instance, locale, options = {}) {
|
|
241
|
+
const { cookie = LOCALE_STORAGE_KEY, maxAge = YEAR } = options;
|
|
242
|
+
await instance.loadLocale(locale);
|
|
243
|
+
instance.setLocale(locale);
|
|
244
|
+
if (typeof document === "undefined") return;
|
|
245
|
+
if (cookie) document.cookie = `${cookie}=${encodeURIComponent(locale)}; path=/; max-age=${maxAge}; samesite=lax`;
|
|
246
|
+
document.documentElement.lang = locale;
|
|
247
|
+
}
|
|
248
|
+
function persistLocale(locale, storageKey = LOCALE_STORAGE_KEY) {
|
|
249
|
+
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
250
|
+
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
251
|
+
}
|
|
252
|
+
function preferredLanguages() {
|
|
253
|
+
if (typeof document === "undefined" || typeof navigator === "undefined") return [];
|
|
254
|
+
return navigator.languages?.length ? navigator.languages : [navigator.language];
|
|
255
|
+
}
|
|
256
|
+
function getStorage() {
|
|
257
|
+
try {
|
|
258
|
+
return typeof localStorage === "undefined" ? null : localStorage;
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
//#endregion
|
|
156
264
|
//#region src/icu.ts
|
|
157
265
|
const ICU_ARG = /\{\s*[a-zA-Z0-9_]+\s*,\s*(?:plural|selectordinal|select|number|date|time)\b/;
|
|
158
266
|
function isIcu(message) {
|
|
@@ -474,12 +582,7 @@ function createVerbaly(options = {}) {
|
|
|
474
582
|
if (options.messages) for (const [loc, tree] of Object.entries(options.messages)) dict[loc] = flatten(tree);
|
|
475
583
|
function chain() {
|
|
476
584
|
if (chainCache) return chainCache;
|
|
477
|
-
const result =
|
|
478
|
-
const parts = locale.split("-");
|
|
479
|
-
while (parts.length > 0) {
|
|
480
|
-
result.push(parts.join("-"));
|
|
481
|
-
parts.pop();
|
|
482
|
-
}
|
|
585
|
+
const result = narrowLocales(locale);
|
|
483
586
|
for (const fb of fallbacks) if (!result.includes(fb)) result.push(fb);
|
|
484
587
|
chainCache = result;
|
|
485
588
|
return result;
|
|
@@ -539,12 +642,9 @@ function createVerbaly(options = {}) {
|
|
|
539
642
|
});
|
|
540
643
|
t.id = () => (strings, ...values) => tagged(strings, values);
|
|
541
644
|
function pendingLoader(target) {
|
|
542
|
-
const
|
|
543
|
-
while (parts.length > 0) {
|
|
544
|
-
const candidate = parts.join("-");
|
|
645
|
+
for (const candidate of narrowLocales(target)) {
|
|
545
646
|
if (loaders[candidate] && !loaded.has(candidate)) return candidate;
|
|
546
647
|
if (loaders[candidate]) return void 0;
|
|
547
|
-
parts.pop();
|
|
548
648
|
}
|
|
549
649
|
}
|
|
550
650
|
function loadLocale(target) {
|
|
@@ -607,7 +707,7 @@ function createVerbaly(options = {}) {
|
|
|
607
707
|
inspect(key) {
|
|
608
708
|
const hit = lookup(key);
|
|
609
709
|
return hit ? {
|
|
610
|
-
|
|
710
|
+
from: hit.from,
|
|
611
711
|
source: hit.msg
|
|
612
712
|
} : void 0;
|
|
613
713
|
}
|
|
@@ -659,6 +759,8 @@ function parseTags(text) {
|
|
|
659
759
|
//#endregion
|
|
660
760
|
//#region src/dom.ts
|
|
661
761
|
const UNSAFE_HREF = /^\s*(javascript|data|vbscript):/i;
|
|
762
|
+
const URL_ATTR = /^(href|src|xlink:href|action|formaction)$/;
|
|
763
|
+
const BLOCKED_ATTR = /^(style|srcdoc)$/;
|
|
662
764
|
function safeHref(href) {
|
|
663
765
|
if (UNSAFE_HREF.test(href)) {
|
|
664
766
|
console.warn(`[verbaly] blocked unsafe href: ${href}`);
|
|
@@ -666,6 +768,14 @@ function safeHref(href) {
|
|
|
666
768
|
}
|
|
667
769
|
return href;
|
|
668
770
|
}
|
|
771
|
+
function normalizeLink(link) {
|
|
772
|
+
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
773
|
+
return {
|
|
774
|
+
href: safeHref(href),
|
|
775
|
+
target,
|
|
776
|
+
rel
|
|
777
|
+
};
|
|
778
|
+
}
|
|
669
779
|
const RICH_TAGS = [
|
|
670
780
|
"em",
|
|
671
781
|
"strong",
|
|
@@ -697,6 +807,7 @@ function bindDom(instance, options = {}) {
|
|
|
697
807
|
const globalLinks = options.richLinks;
|
|
698
808
|
const argsCache = /* @__PURE__ */ new WeakMap();
|
|
699
809
|
const linksCache = /* @__PURE__ */ new WeakMap();
|
|
810
|
+
const attrsCache = /* @__PURE__ */ new WeakMap();
|
|
700
811
|
function cachedArgs(el) {
|
|
701
812
|
const raw = el.getAttribute(argsAttr);
|
|
702
813
|
const hit = argsCache.get(el);
|
|
@@ -727,11 +838,9 @@ function bindDom(instance, options = {}) {
|
|
|
727
838
|
function renderRich(el, nodes, links) {
|
|
728
839
|
for (const node of nodes) if (typeof node === "string") el.append(node);
|
|
729
840
|
else if (links?.[node.name] !== void 0) {
|
|
730
|
-
const
|
|
731
|
-
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
841
|
+
const { href, target, rel } = normalizeLink(links[node.name]);
|
|
732
842
|
const a = el.ownerDocument.createElement("a");
|
|
733
|
-
|
|
734
|
-
if (safe !== void 0) a.setAttribute("href", safe);
|
|
843
|
+
if (href !== void 0) a.setAttribute("href", href);
|
|
735
844
|
if (target) a.setAttribute("target", target);
|
|
736
845
|
if (rel) a.setAttribute("rel", rel);
|
|
737
846
|
renderRich(a, node.children, links);
|
|
@@ -752,10 +861,23 @@ function bindDom(instance, options = {}) {
|
|
|
752
861
|
renderRich(el, parseTags(text), cachedLinks(el));
|
|
753
862
|
} else el.textContent = text;
|
|
754
863
|
}
|
|
755
|
-
const
|
|
864
|
+
const rawAttrs = el.getAttribute(attrsAttr);
|
|
865
|
+
const attrsHit = attrsCache.get(el);
|
|
866
|
+
const attrMap = attrsHit && attrsHit.raw === rawAttrs ? attrsHit.map : parseArgs(rawAttrs);
|
|
867
|
+
if (!attrsHit || attrsHit.raw !== rawAttrs) attrsCache.set(el, {
|
|
868
|
+
raw: rawAttrs,
|
|
869
|
+
map: attrMap
|
|
870
|
+
});
|
|
756
871
|
if (attrMap) for (const [name, attrKey] of Object.entries(attrMap)) {
|
|
757
|
-
|
|
758
|
-
|
|
872
|
+
const lower = name.toLowerCase();
|
|
873
|
+
if (typeof attrKey !== "string" || lower.startsWith("on") || BLOCKED_ATTR.test(lower)) continue;
|
|
874
|
+
let value = t(attrKey, args);
|
|
875
|
+
if (URL_ATTR.test(lower)) {
|
|
876
|
+
const safe = safeHref(value);
|
|
877
|
+
if (safe === void 0) continue;
|
|
878
|
+
value = safe;
|
|
879
|
+
}
|
|
880
|
+
el.setAttribute(name, value);
|
|
759
881
|
}
|
|
760
882
|
}
|
|
761
883
|
const selector = `[${attr}], [${attrsAttr}]`;
|
|
@@ -798,85 +920,19 @@ function parseArgs(raw) {
|
|
|
798
920
|
}
|
|
799
921
|
}
|
|
800
922
|
//#endregion
|
|
801
|
-
|
|
802
|
-
const DEFAULT_KEY = "verbaly-locale";
|
|
803
|
-
function resolveLocale(options) {
|
|
804
|
-
const { supported, fallback = supported[0] ?? "en", storageKey = DEFAULT_KEY } = options;
|
|
805
|
-
if (storageKey) {
|
|
806
|
-
const stored = getStorage()?.getItem(storageKey);
|
|
807
|
-
const match = stored && matchSupported(stored, supported);
|
|
808
|
-
if (match) return match;
|
|
809
|
-
}
|
|
810
|
-
for (const lang of preferredLanguages()) {
|
|
811
|
-
const match = matchSupported(lang, supported);
|
|
812
|
-
if (match) return match;
|
|
813
|
-
}
|
|
814
|
-
return fallback;
|
|
815
|
-
}
|
|
816
|
-
function matchSupported(lang, supported) {
|
|
817
|
-
if (supported.includes(lang)) return lang;
|
|
818
|
-
const base = lang.split("-")[0];
|
|
819
|
-
return supported.includes(base) ? base : void 0;
|
|
820
|
-
}
|
|
821
|
-
function negotiateLocale(header, supported, fallback = supported[0] ?? "en") {
|
|
822
|
-
if (!header) return fallback;
|
|
823
|
-
const ranges = [];
|
|
824
|
-
for (const part of header.split(",")) {
|
|
825
|
-
const [rawTag, ...params] = part.trim().split(";");
|
|
826
|
-
const tag = rawTag?.trim();
|
|
827
|
-
if (!tag || tag === "*") continue;
|
|
828
|
-
let q = 1;
|
|
829
|
-
for (const param of params) {
|
|
830
|
-
const [name, value] = param.split("=");
|
|
831
|
-
if (name?.trim().toLowerCase() === "q") {
|
|
832
|
-
const parsed = Number(value?.trim());
|
|
833
|
-
q = Number.isFinite(parsed) ? parsed : 1;
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
if (q > 0) ranges.push({
|
|
837
|
-
tag,
|
|
838
|
-
q
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
ranges.sort((a, b) => b.q - a.q);
|
|
842
|
-
for (const { tag } of ranges) {
|
|
843
|
-
const match = matchSupportedLoose(tag, supported);
|
|
844
|
-
if (match) return match;
|
|
845
|
-
}
|
|
846
|
-
return fallback;
|
|
847
|
-
}
|
|
848
|
-
function matchSupportedLoose(lang, supported) {
|
|
849
|
-
const lower = lang.toLowerCase();
|
|
850
|
-
const exact = supported.find((s) => s.toLowerCase() === lower);
|
|
851
|
-
if (exact) return exact;
|
|
852
|
-
const base = lower.split("-")[0];
|
|
853
|
-
return supported.find((s) => s.toLowerCase() === base);
|
|
854
|
-
}
|
|
855
|
-
function persistLocale(locale, storageKey = DEFAULT_KEY) {
|
|
856
|
-
if (storageKey) getStorage()?.setItem(storageKey, locale);
|
|
857
|
-
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
858
|
-
}
|
|
859
|
-
function preferredLanguages() {
|
|
860
|
-
if (typeof document === "undefined" || typeof navigator === "undefined") return [];
|
|
861
|
-
return navigator.languages?.length ? navigator.languages : [navigator.language];
|
|
862
|
-
}
|
|
863
|
-
function getStorage() {
|
|
864
|
-
try {
|
|
865
|
-
return typeof localStorage === "undefined" ? null : localStorage;
|
|
866
|
-
} catch {
|
|
867
|
-
return null;
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
//#endregion
|
|
923
|
+
exports.LOCALE_STORAGE_KEY = LOCALE_STORAGE_KEY;
|
|
871
924
|
exports.RICH_TAGS = RICH_TAGS;
|
|
872
925
|
exports.bindDom = bindDom;
|
|
873
926
|
exports.createVerbaly = createVerbaly;
|
|
874
927
|
exports.flatten = flatten;
|
|
875
928
|
exports.negotiateLocale = negotiateLocale;
|
|
929
|
+
exports.normalizeLink = normalizeLink;
|
|
876
930
|
exports.parse = parse;
|
|
877
931
|
exports.parseTags = parseTags;
|
|
878
932
|
exports.persistLocale = persistLocale;
|
|
879
933
|
exports.resolveLocale = resolveLocale;
|
|
934
|
+
exports.resolveRequestLocale = resolveRequestLocale;
|
|
880
935
|
exports.safeHref = safeHref;
|
|
936
|
+
exports.switchLocale = switchLocale;
|
|
881
937
|
|
|
882
938
|
//# sourceMappingURL=index.cjs.map
|