lit-intlayer 9.9.3 → 9.9.4
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/cjs/client/IntlayerBinding.cjs +93 -1
- package/dist/cjs/client/index.cjs +28 -1
- package/dist/cjs/client/installIntlayer.cjs +6 -1
- package/dist/cjs/client/useDictionary.cjs +24 -1
- package/dist/cjs/client/useDictionaryDynamic.cjs +47 -1
- package/dist/cjs/client/useIntlayer.cjs +30 -1
- package/dist/cjs/client/useLocale.cjs +90 -1
- package/dist/cjs/client/useLocaleStorage.cjs +56 -1
- package/dist/cjs/client/useRewriteURL.cjs +48 -1
- package/dist/cjs/editor/useEditor.cjs +3 -1
- package/dist/cjs/format/index.cjs +19 -1
- package/dist/cjs/format/useCompact.cjs +34 -1
- package/dist/cjs/format/useCurrency.cjs +34 -1
- package/dist/cjs/format/useDate.cjs +52 -1
- package/dist/cjs/format/useIntl.cjs +66 -1
- package/dist/cjs/format/useList.cjs +34 -1
- package/dist/cjs/format/useNumber.cjs +40 -1
- package/dist/cjs/format/usePercentage.cjs +34 -1
- package/dist/cjs/format/useRelativeTime.cjs +34 -1
- package/dist/cjs/format/useUnit.cjs +34 -1
- package/dist/cjs/getDictionary.cjs +14 -1
- package/dist/cjs/getIntlayer.cjs +14 -1
- package/dist/cjs/html/HTMLRenderer.cjs +53 -1
- package/dist/cjs/html/index.cjs +8 -1
- package/dist/cjs/html/installIntlayerHTML.cjs +90 -1
- package/dist/cjs/html/types.cjs +1 -1
- package/dist/cjs/index.cjs +41 -1
- package/dist/cjs/markdown/MarkdownRenderer.cjs +97 -1
- package/dist/cjs/markdown/compiler.cjs +72 -1
- package/dist/cjs/markdown/index.cjs +12 -1
- package/dist/cjs/markdown/installIntlayerMarkdown.cjs +98 -1
- package/dist/cjs/markdown/runtime.cjs +86 -1
- package/dist/cjs/plugins.cjs +217 -3
- package/dist/cjs/renderIntlayerNode.cjs +29 -1
- package/dist/esm/client/IntlayerBinding.mjs +75 -33
- package/dist/esm/client/index.mjs +9 -9
- package/dist/esm/client/installIntlayer.mjs +1 -1
- package/dist/esm/client/useDictionary.mjs +22 -10
- package/dist/esm/client/useDictionaryDynamic.mjs +44 -11
- package/dist/esm/client/useIntlayer.mjs +28 -10
- package/dist/esm/client/useLocale.mjs +71 -19
- package/dist/esm/client/useLocaleStorage.mjs +45 -12
- package/dist/esm/client/useRewriteURL.mjs +36 -18
- package/dist/esm/editor/useEditor.mjs +1 -1
- package/dist/esm/format/index.mjs +10 -10
- package/dist/esm/format/useCompact.mjs +23 -19
- package/dist/esm/format/useCurrency.mjs +23 -19
- package/dist/esm/format/useDate.mjs +37 -27
- package/dist/esm/format/useIntl.mjs +54 -13
- package/dist/esm/format/useList.mjs +23 -19
- package/dist/esm/format/useNumber.mjs +29 -19
- package/dist/esm/format/usePercentage.mjs +23 -19
- package/dist/esm/format/useRelativeTime.mjs +23 -19
- package/dist/esm/format/useUnit.mjs +23 -19
- package/dist/esm/getDictionary.mjs +11 -6
- package/dist/esm/getIntlayer.mjs +11 -6
- package/dist/esm/html/HTMLRenderer.mjs +38 -13
- package/dist/esm/html/index.mjs +3 -3
- package/dist/esm/html/installIntlayerHTML.mjs +82 -13
- package/dist/esm/index.mjs +12 -12
- package/dist/esm/markdown/MarkdownRenderer.mjs +66 -26
- package/dist/esm/markdown/compiler.mjs +68 -22
- package/dist/esm/markdown/index.mjs +4 -4
- package/dist/esm/markdown/installIntlayerMarkdown.mjs +87 -23
- package/dist/esm/markdown/runtime.mjs +58 -28
- package/dist/esm/plugins.mjs +156 -106
- package/dist/esm/renderIntlayerNode.mjs +17 -17
- package/dist/installIntlayer-CCMOc1XT.mjs +174 -0
- package/dist/installIntlayer-HB8aIQ-i.js +203 -0
- package/package.json +7 -7
- package/dist/installIntlayer-CFvvijL7.js +0 -1
- package/dist/installIntlayer-CTpPd7yz.mjs +0 -82
package/dist/esm/plugins.mjs
CHANGED
|
@@ -1,139 +1,189 @@
|
|
|
1
|
-
import { editor
|
|
2
|
-
import { conditionPlugin
|
|
3
|
-
import { compileWithOptions
|
|
4
|
-
import { getIntlayerNodePrototype
|
|
5
|
-
import * as
|
|
6
|
-
import { unsafeHTML
|
|
7
|
-
import { html
|
|
8
|
-
import { r as
|
|
9
|
-
import
|
|
10
|
-
import { renderIntlayerNode
|
|
1
|
+
import { editor, internationalization } from "@intlayer/config/built";
|
|
2
|
+
import { conditionPlugin, enumerationPlugin, fallbackPlugin, filePlugin, genderPlugin, getHTML, isInterpolableWrapperNode, nestedPlugin, pluralPlugin, resolveInsertedSelector, resolveInterpreterLocale, selectPlugin, splitInsertionTemplate, transformInterpolableNode, translationPlugin } from "@intlayer/core/interpreter";
|
|
3
|
+
import { compileWithOptions, getMarkdownMetadata } from "@intlayer/core/markdown";
|
|
4
|
+
import { getIntlayerNodePrototype } from "@intlayer/core/utils";
|
|
5
|
+
import * as NodeTypes from "@intlayer/types/nodeType";
|
|
6
|
+
import { unsafeHTML } from "lit/directives/unsafe-html.js";
|
|
7
|
+
import { html, unsafeStatic } from "lit/static-html.js";
|
|
8
|
+
import { r as getIntlayerClient } from "../installIntlayer-CCMOc1XT.mjs";
|
|
9
|
+
import litRuntime from "./markdown/runtime.mjs";
|
|
10
|
+
import { renderIntlayerNode } from "./renderIntlayerNode.mjs";
|
|
11
11
|
// src/plugins.ts
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Compile markdown to an HTML string using the Lit runtime.
|
|
14
|
+
* Inline here to avoid a cross-entry circular import with markdown/compiler.
|
|
15
|
+
*/
|
|
16
|
+
var compileMarkdown = (markdown = "", options = {}) => compileWithOptions(markdown, litRuntime, options);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a Lit-renderable node for raw HTML/compiled markdown.
|
|
19
|
+
*
|
|
20
|
+
* The returned object is a copy of an `unsafeHTML` DirectiveResult or TemplateResult
|
|
21
|
+
* so that Lit's template engine renders it as HTML, while string
|
|
22
|
+
* coercion (`.toString()`, `String(node)`) still returns the raw source string
|
|
23
|
+
* for editor tooling and serialization.
|
|
24
|
+
*/
|
|
25
|
+
var createLitHTMLNode = (content, rawStr, additionalProps = {}) => {
|
|
26
|
+
const result = typeof content === "string" ? unsafeHTML(content) : Array.isArray(content) ? content : content;
|
|
27
|
+
const isArrayResult = Array.isArray(result);
|
|
28
|
+
const node = Object.assign(isArrayResult ? [...result] : { ...result }, additionalProps, {
|
|
29
|
+
value: rawStr,
|
|
30
|
+
raw: rawStr,
|
|
16
31
|
[Symbol.iterator]: void 0,
|
|
17
32
|
__update: () => {},
|
|
18
|
-
isJSX:
|
|
33
|
+
isJSX: true
|
|
19
34
|
});
|
|
20
|
-
return Object.setPrototypeOf(
|
|
21
|
-
}
|
|
35
|
+
return Object.setPrototypeOf(node, getIntlayerNodePrototype(rawStr, isArrayResult ? Array.prototype : Object.prototype));
|
|
36
|
+
};
|
|
37
|
+
var escapeHtmlAttr = (str) => str.replace(/&/g, "&").replace(/"/g, """);
|
|
38
|
+
var escapeHtmlText = (str) => str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
39
|
+
/**
|
|
40
|
+
* Wraps string/number leaf values in an IntlayerNode so they carry
|
|
41
|
+
* `.value`, `.raw`, `toString()`, and `__update()`.
|
|
42
|
+
* When the editor is enabled, wraps the content with
|
|
43
|
+
* `<intlayer-content-selector-wrapper>` so the visual editor can highlight it.
|
|
44
|
+
*/
|
|
45
|
+
var intlayerNodePlugins = {
|
|
22
46
|
id: "intlayer-node-plugin",
|
|
23
|
-
canHandle: (
|
|
24
|
-
transform: (
|
|
25
|
-
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" &&
|
|
26
|
-
|
|
27
|
-
return
|
|
47
|
+
canHandle: (node) => typeof node === "bigint" || typeof node === "string" || typeof node === "number",
|
|
48
|
+
transform: (_node, { children, keyPath, dictionaryKey }) => {
|
|
49
|
+
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" && editor.enabled) {
|
|
50
|
+
const rawStr = String(children ?? "");
|
|
51
|
+
return createLitHTMLNode(`<intlayer-content-selector-wrapper key-path="${escapeHtmlAttr(JSON.stringify(keyPath ?? []))}" dictionary-key="${escapeHtmlAttr(String(dictionaryKey ?? ""))}">${escapeHtmlText(rawStr)}</intlayer-content-selector-wrapper>`, rawStr);
|
|
28
52
|
}
|
|
29
|
-
return
|
|
30
|
-
value:
|
|
31
|
-
children
|
|
53
|
+
return renderIntlayerNode({
|
|
54
|
+
value: children,
|
|
55
|
+
children
|
|
32
56
|
});
|
|
33
57
|
}
|
|
34
|
-
}
|
|
58
|
+
};
|
|
59
|
+
var insertionPlugin = process.env.INTLAYER_NODE_TYPE_INSERTION === "false" ? fallbackPlugin : {
|
|
35
60
|
id: "insertion-plugin",
|
|
36
|
-
canHandle: (
|
|
37
|
-
transform: (
|
|
38
|
-
|
|
61
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.INSERTION,
|
|
62
|
+
transform: (node, props, deepTransformNode) => {
|
|
63
|
+
const newKeyPath = [...props.keyPath, { type: NodeTypes.INSERTION }];
|
|
64
|
+
const children = node[NodeTypes.INSERTION];
|
|
65
|
+
const insertionStringPlugin = {
|
|
39
66
|
id: "insertion-string-plugin",
|
|
40
|
-
canHandle: (
|
|
41
|
-
transform: (
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
...
|
|
45
|
-
children:
|
|
46
|
-
plugins: (
|
|
67
|
+
canHandle: (node) => typeof node === "string" || isInterpolableWrapperNode(node),
|
|
68
|
+
transform: (node, subProps, deepTransformNode) => {
|
|
69
|
+
if (isInterpolableWrapperNode(node)) return (values) => transformInterpolableNode(node, values, subProps, props.plugins, deepTransformNode);
|
|
70
|
+
const transformedResult = deepTransformNode(node, {
|
|
71
|
+
...subProps,
|
|
72
|
+
children: node,
|
|
73
|
+
plugins: (props.plugins ?? []).filter((plugin) => plugin.id !== "intlayer-node-plugin")
|
|
47
74
|
});
|
|
48
|
-
return (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
75
|
+
return (values) => {
|
|
76
|
+
const result = splitInsertionTemplate(transformedResult, values);
|
|
77
|
+
const resultStr = result.isSimple ? result.parts : result.parts.join("");
|
|
78
|
+
return deepTransformNode(resultStr, {
|
|
79
|
+
...subProps,
|
|
80
|
+
plugins: props.plugins,
|
|
81
|
+
children: resultStr
|
|
54
82
|
});
|
|
55
83
|
};
|
|
56
84
|
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
85
|
+
};
|
|
86
|
+
const transformed = deepTransformNode(children, {
|
|
87
|
+
...props,
|
|
88
|
+
children,
|
|
89
|
+
keyPath: newKeyPath,
|
|
90
|
+
plugins: [insertionStringPlugin, ...props.plugins ?? []]
|
|
62
91
|
});
|
|
63
|
-
return
|
|
92
|
+
return resolveInsertedSelector(children, transformed);
|
|
64
93
|
}
|
|
65
|
-
}
|
|
94
|
+
};
|
|
95
|
+
/** Markdown string plugin. Replaces string node with a component that render the markdown. */
|
|
96
|
+
var markdownStringPlugin = process.env.INTLAYER_NODE_TYPE_MARKDOWN === "false" ? fallbackPlugin : {
|
|
66
97
|
id: "markdown-string-plugin",
|
|
67
|
-
canHandle: (
|
|
68
|
-
transform: (
|
|
69
|
-
|
|
98
|
+
canHandle: (node) => typeof node === "string",
|
|
99
|
+
transform: (node, props, deepTransformNode) => {
|
|
100
|
+
const { plugins: _plugins, ...rest } = props;
|
|
101
|
+
const { keyPath, dictionaryKey } = rest;
|
|
102
|
+
const metadataNodes = deepTransformNode(getMarkdownMetadata(node) ?? {}, {
|
|
70
103
|
plugins: [{
|
|
71
104
|
id: "markdown-metadata-plugin",
|
|
72
|
-
canHandle: (
|
|
73
|
-
transform: (
|
|
74
|
-
...
|
|
75
|
-
value:
|
|
76
|
-
children:
|
|
105
|
+
canHandle: (metadataNode) => typeof metadataNode === "string" || typeof metadataNode === "number" || typeof metadataNode === "boolean" || !metadataNode,
|
|
106
|
+
transform: (metadataNode, subProps) => renderIntlayerNode({
|
|
107
|
+
...subProps,
|
|
108
|
+
value: metadataNode,
|
|
109
|
+
children: node
|
|
77
110
|
})
|
|
78
111
|
}],
|
|
79
|
-
dictionaryKey:
|
|
112
|
+
dictionaryKey: rest.dictionaryKey,
|
|
80
113
|
keyPath: []
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
|
|
114
|
+
});
|
|
115
|
+
const compiled = compileMarkdown(node);
|
|
116
|
+
const use = (components) => {
|
|
117
|
+
const rendered = compileMarkdown(node, { components });
|
|
118
|
+
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" && editor.enabled) return createLitHTMLNode(`<intlayer-content-selector-wrapper key-path="${escapeHtmlAttr(JSON.stringify(keyPath ?? []))}" dictionary-key="${escapeHtmlAttr(String(dictionaryKey ?? ""))}">${rendered}</intlayer-content-selector-wrapper>`, node);
|
|
119
|
+
return createLitHTMLNode(rendered, node);
|
|
84
120
|
};
|
|
85
|
-
|
|
86
|
-
metadata:
|
|
87
|
-
use
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
|
|
121
|
+
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" && editor.enabled) return createLitHTMLNode(`<intlayer-content-selector-wrapper key-path="${escapeHtmlAttr(JSON.stringify(keyPath ?? []))}" dictionary-key="${escapeHtmlAttr(String(dictionaryKey ?? ""))}">${compiled}</intlayer-content-selector-wrapper>`, node, {
|
|
122
|
+
metadata: metadataNodes,
|
|
123
|
+
use
|
|
124
|
+
});
|
|
125
|
+
return createLitHTMLNode(compiled, node, {
|
|
126
|
+
metadata: metadataNodes,
|
|
127
|
+
use
|
|
91
128
|
});
|
|
92
129
|
}
|
|
93
|
-
}
|
|
130
|
+
};
|
|
131
|
+
var markdownPlugin = process.env.INTLAYER_NODE_TYPE_MARKDOWN === "false" ? fallbackPlugin : {
|
|
94
132
|
id: "markdown-plugin",
|
|
95
|
-
canHandle: (
|
|
96
|
-
transform: (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
133
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.MARKDOWN,
|
|
134
|
+
transform: (node, props, deepTransformNode) => {
|
|
135
|
+
const newKeyPath = [...props.keyPath, { type: NodeTypes.MARKDOWN }];
|
|
136
|
+
const children = node[NodeTypes.MARKDOWN];
|
|
137
|
+
return deepTransformNode(children, {
|
|
138
|
+
...props,
|
|
139
|
+
children,
|
|
140
|
+
keyPath: newKeyPath,
|
|
141
|
+
plugins: [markdownStringPlugin, ...props.plugins ?? []]
|
|
103
142
|
});
|
|
104
143
|
}
|
|
105
|
-
}
|
|
144
|
+
};
|
|
145
|
+
var htmlPlugin = process.env.INTLAYER_NODE_TYPE_HTML === "false" ? fallbackPlugin : {
|
|
106
146
|
id: "html-plugin",
|
|
107
|
-
canHandle: (
|
|
108
|
-
transform: (
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
147
|
+
canHandle: (node) => typeof node === "object" && node?.nodeType === NodeTypes.HTML,
|
|
148
|
+
transform: (node, props) => {
|
|
149
|
+
const htmlStr = node[NodeTypes.HTML];
|
|
150
|
+
const { keyPath, dictionaryKey } = props;
|
|
151
|
+
const use = (components = {}) => {
|
|
152
|
+
const userComponents = Object.fromEntries(Object.entries(components).filter(([, Component]) => Component).map(([key, Component]) => [key, (props) => Component(props)]));
|
|
153
|
+
const wrappedComponents = new Proxy(userComponents, { get(target, prop) {
|
|
154
|
+
if (typeof prop === "string" && prop in target) return target[prop];
|
|
155
|
+
if (typeof prop === "string" && /^[a-z][a-z0-9]*$/.test(prop)) return (props) => html`<${unsafeStatic(prop)} ...${props}
|
|
156
|
+
>${props.children}</${unsafeStatic(prop)}
|
|
114
157
|
>`;
|
|
115
|
-
} })
|
|
116
|
-
|
|
158
|
+
} });
|
|
159
|
+
const rendered = getHTML(htmlStr, wrappedComponents);
|
|
160
|
+
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" && editor.enabled) return createLitHTMLNode(`<intlayer-content-selector-wrapper key-path="${escapeHtmlAttr(JSON.stringify(keyPath ?? []))}" dictionary-key="${escapeHtmlAttr(String(dictionaryKey ?? ""))}">${rendered}</intlayer-content-selector-wrapper>`, htmlStr);
|
|
161
|
+
return createLitHTMLNode(rendered, htmlStr);
|
|
117
162
|
};
|
|
118
|
-
|
|
163
|
+
if (process.env.INTLAYER_EDITOR_ENABLED !== "false" && editor.enabled) return createLitHTMLNode(`<intlayer-content-selector-wrapper key-path="${escapeHtmlAttr(JSON.stringify(keyPath ?? []))}" dictionary-key="${escapeHtmlAttr(String(dictionaryKey ?? ""))}">${htmlStr}</intlayer-content-selector-wrapper>`, htmlStr, { use });
|
|
164
|
+
return createLitHTMLNode(htmlStr, htmlStr, { use });
|
|
119
165
|
}
|
|
120
|
-
}, I = /* @__PURE__ */ new Map(), L = (e, s = !0) => {
|
|
121
|
-
let c = e ?? (typeof window < "u" && w()?.locale) ?? f(), d = `${c}_${s}`;
|
|
122
|
-
if (I.has(d)) return I.get(d);
|
|
123
|
-
let m = [
|
|
124
|
-
j,
|
|
125
|
-
g(c, s ? t.defaultLocale : void 0),
|
|
126
|
-
r,
|
|
127
|
-
u(c),
|
|
128
|
-
n,
|
|
129
|
-
l(c),
|
|
130
|
-
a,
|
|
131
|
-
o,
|
|
132
|
-
p,
|
|
133
|
-
M,
|
|
134
|
-
P,
|
|
135
|
-
F
|
|
136
|
-
].filter((e) => e !== i);
|
|
137
|
-
return I.set(d, m), m;
|
|
138
166
|
};
|
|
139
|
-
|
|
167
|
+
var pluginsCache = /* @__PURE__ */ new Map();
|
|
168
|
+
var getPlugins = (locale, fallback = true) => {
|
|
169
|
+
const currentLocale = locale ?? (typeof window !== "undefined" && getIntlayerClient()?.locale) ?? resolveInterpreterLocale();
|
|
170
|
+
const cacheKey = `${currentLocale}_${fallback}`;
|
|
171
|
+
if (pluginsCache.has(cacheKey)) return pluginsCache.get(cacheKey);
|
|
172
|
+
const enabledPlugins = [
|
|
173
|
+
intlayerNodePlugins,
|
|
174
|
+
translationPlugin(currentLocale, fallback ? internationalization.defaultLocale : void 0),
|
|
175
|
+
enumerationPlugin,
|
|
176
|
+
pluralPlugin(currentLocale),
|
|
177
|
+
conditionPlugin,
|
|
178
|
+
nestedPlugin(currentLocale),
|
|
179
|
+
filePlugin,
|
|
180
|
+
genderPlugin,
|
|
181
|
+
selectPlugin,
|
|
182
|
+
insertionPlugin,
|
|
183
|
+
markdownPlugin,
|
|
184
|
+
htmlPlugin
|
|
185
|
+
].filter((plugin) => plugin !== fallbackPlugin);
|
|
186
|
+
pluginsCache.set(cacheKey, enabledPlugins);
|
|
187
|
+
return enabledPlugins;
|
|
188
|
+
};
|
|
189
|
+
export { getPlugins, htmlPlugin, insertionPlugin, intlayerNodePlugins, markdownPlugin, markdownStringPlugin };
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { getIntlayerNodePrototype
|
|
1
|
+
import { getIntlayerNodePrototype } from "@intlayer/core/utils";
|
|
2
2
|
// src/renderIntlayerNode.ts
|
|
3
|
-
var
|
|
4
|
-
let
|
|
3
|
+
var renderIntlayerNode = ({ value, additionalProps = {} }) => {
|
|
4
|
+
let _value = value;
|
|
5
5
|
return {
|
|
6
|
-
__proto__:
|
|
7
|
-
toString: () => String(
|
|
8
|
-
valueOf: () =>
|
|
9
|
-
[Symbol.toPrimitive]: () =>
|
|
6
|
+
__proto__: getIntlayerNodePrototype(value),
|
|
7
|
+
toString: () => String(_value ?? ""),
|
|
8
|
+
valueOf: () => _value,
|
|
9
|
+
[Symbol.toPrimitive]: () => _value,
|
|
10
10
|
[Symbol.iterator]: void 0,
|
|
11
|
-
toJSON: () =>
|
|
11
|
+
toJSON: () => _value,
|
|
12
12
|
get raw() {
|
|
13
|
-
return
|
|
13
|
+
return _value;
|
|
14
14
|
},
|
|
15
|
-
set raw(
|
|
16
|
-
|
|
15
|
+
set raw(value) {
|
|
16
|
+
_value = value;
|
|
17
17
|
},
|
|
18
18
|
get value() {
|
|
19
|
-
return
|
|
19
|
+
return _value;
|
|
20
20
|
},
|
|
21
|
-
__update(
|
|
22
|
-
|
|
21
|
+
__update(next) {
|
|
22
|
+
_value = next?.raw ?? next?.value ?? next;
|
|
23
23
|
},
|
|
24
|
-
isJSX:
|
|
25
|
-
...
|
|
24
|
+
isJSX: true,
|
|
25
|
+
...additionalProps
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
-
export {
|
|
28
|
+
export { renderIntlayerNode };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { internationalization } from "@intlayer/config/built";
|
|
2
|
+
import { setIntlayerIdentifier } from "@intlayer/config/client";
|
|
3
|
+
import { isEnabled } from "@intlayer/editor/isEnabled";
|
|
4
|
+
import { getLocaleInStorage, setLocaleInStorage } from "./esm/client/useLocaleStorage.mjs";
|
|
5
|
+
// src/editor/useEditor.ts
|
|
6
|
+
/**
|
|
7
|
+
* Syncs the client locale into the editor manager and applies the locales
|
|
8
|
+
* requested by the editor. Returns a function stopping both subscriptions.
|
|
9
|
+
*/
|
|
10
|
+
var syncEditorLocale = (manager) => {
|
|
11
|
+
const client = getIntlayerClient();
|
|
12
|
+
manager.currentLocale.set(client.locale);
|
|
13
|
+
const unsubscribeLocale = client.subscribe((newLocale) => {
|
|
14
|
+
manager.currentLocale.set(newLocale);
|
|
15
|
+
});
|
|
16
|
+
const unsubscribeLocaleRequest = manager.onLocaleChangeRequested((requestedLocale) => {
|
|
17
|
+
client.setLocale(requestedLocale);
|
|
18
|
+
setLocaleInStorage(requestedLocale, client.isCookieEnabled);
|
|
19
|
+
});
|
|
20
|
+
return () => {
|
|
21
|
+
unsubscribeLocale();
|
|
22
|
+
unsubscribeLocaleRequest();
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* ReactiveController that initialises the Intlayer visual editor when enabled.
|
|
27
|
+
* Syncs the current locale into the editor manager so it always reflects the
|
|
28
|
+
* locale the app is displaying.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
var EditorController = class {
|
|
33
|
+
host;
|
|
34
|
+
_stopped = false;
|
|
35
|
+
_unsubscribeLocale = null;
|
|
36
|
+
constructor(host) {
|
|
37
|
+
this.host = host;
|
|
38
|
+
host.addController(this);
|
|
39
|
+
}
|
|
40
|
+
hostConnected() {
|
|
41
|
+
this._stopped = false;
|
|
42
|
+
if (process.env.INTLAYER_EDITOR_ENABLED === "false" || !isEnabled) return;
|
|
43
|
+
import("@intlayer/editor").then(({ initEditorClient }) => {
|
|
44
|
+
if (this._stopped) return;
|
|
45
|
+
this._unsubscribeLocale = syncEditorLocale(initEditorClient());
|
|
46
|
+
}).catch(() => {});
|
|
47
|
+
}
|
|
48
|
+
hostDisconnected() {
|
|
49
|
+
this._stopped = true;
|
|
50
|
+
this._unsubscribeLocale?.();
|
|
51
|
+
this._unsubscribeLocale = null;
|
|
52
|
+
import("@intlayer/editor").then(({ stopEditorClient }) => {
|
|
53
|
+
stopEditorClient();
|
|
54
|
+
}).catch(() => {});
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function useEditor(host) {
|
|
58
|
+
if (process.env.INTLAYER_EDITOR_ENABLED === "false" || !isEnabled) return host ? void 0 : () => {};
|
|
59
|
+
if (!host) {
|
|
60
|
+
let stopped = false;
|
|
61
|
+
let unsubscribeLocale = null;
|
|
62
|
+
import("@intlayer/editor").then(({ initEditorClient }) => {
|
|
63
|
+
if (stopped) return;
|
|
64
|
+
unsubscribeLocale = syncEditorLocale(initEditorClient());
|
|
65
|
+
}).catch(() => {});
|
|
66
|
+
return () => {
|
|
67
|
+
stopped = true;
|
|
68
|
+
unsubscribeLocale?.();
|
|
69
|
+
import("@intlayer/editor").then(({ stopEditorClient }) => {
|
|
70
|
+
stopEditorClient();
|
|
71
|
+
}).catch(() => {});
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
new EditorController(host);
|
|
75
|
+
}
|
|
76
|
+
// src/client/installIntlayer.ts
|
|
77
|
+
var IntlayerClient = class {
|
|
78
|
+
_locale;
|
|
79
|
+
_variant;
|
|
80
|
+
_listeners = /* @__PURE__ */ new Set();
|
|
81
|
+
isCookieEnabled;
|
|
82
|
+
constructor(locale, isCookieEnabled = true, variant) {
|
|
83
|
+
const { defaultLocale } = internationalization ?? {};
|
|
84
|
+
setIntlayerIdentifier();
|
|
85
|
+
this._locale = locale ?? getLocaleInStorage() ?? defaultLocale;
|
|
86
|
+
this._variant = variant;
|
|
87
|
+
this.isCookieEnabled = isCookieEnabled;
|
|
88
|
+
}
|
|
89
|
+
get locale() {
|
|
90
|
+
return this._locale;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Ambient variant applied to every dictionary read, the same way `locale` is.
|
|
94
|
+
* Overridden per call by an explicit selector.
|
|
95
|
+
*/
|
|
96
|
+
get variant() {
|
|
97
|
+
return this._variant;
|
|
98
|
+
}
|
|
99
|
+
setLocale(newLocale) {
|
|
100
|
+
this._locale = newLocale;
|
|
101
|
+
for (const listener of this._listeners) listener(newLocale);
|
|
102
|
+
}
|
|
103
|
+
setVariant(newVariant) {
|
|
104
|
+
this._variant = newVariant;
|
|
105
|
+
for (const listener of this._listeners) listener(this._locale);
|
|
106
|
+
}
|
|
107
|
+
subscribe(listener) {
|
|
108
|
+
this._listeners.add(listener);
|
|
109
|
+
return () => {
|
|
110
|
+
this._listeners.delete(listener);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Singleton instance
|
|
116
|
+
*/
|
|
117
|
+
var instance = null;
|
|
118
|
+
/**
|
|
119
|
+
* Create and return a single IntlayerClient instance.
|
|
120
|
+
*/
|
|
121
|
+
var createIntlayerClient = (locale, isCookieEnabled = true, variant) => {
|
|
122
|
+
if (instance) return instance;
|
|
123
|
+
instance = new IntlayerClient(locale, isCookieEnabled, variant);
|
|
124
|
+
return instance;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Get the current IntlayerClient instance, creating one with defaults if needed.
|
|
128
|
+
*/
|
|
129
|
+
var getIntlayerClient = () => {
|
|
130
|
+
if (!instance) instance = new IntlayerClient();
|
|
131
|
+
return instance;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Install Intlayer into your Lit application.
|
|
135
|
+
*
|
|
136
|
+
* Call this once at application startup before any Lit elements are rendered.
|
|
137
|
+
*
|
|
138
|
+
* When called with a `config` argument, `window.INTLAYER_CONFIG` is set so
|
|
139
|
+
* that `@intlayer/config/built` resolves the correct values in the browser
|
|
140
|
+
* without requiring module aliasing in the bundler configuration.
|
|
141
|
+
*
|
|
142
|
+
* @param locale - Initial locale (defaults to config defaultLocale).
|
|
143
|
+
* @param isCookieEnabled - Whether to persist locale in cookies/localStorage.
|
|
144
|
+
* @param variant - Ambient variant applied to every dictionary read — for a
|
|
145
|
+
* dimension fixed for the whole session (tenant, school type, plan tier…)
|
|
146
|
+
* that no call site should have to pass by hand. Accepts a name
|
|
147
|
+
* (`'school1'`), an ordered preference chain (`['school1', 'default']`), or a
|
|
148
|
+
* per-key map (`{ key1: 'school1', default: 'base' }`). A plain object is
|
|
149
|
+
* always the per-key map; nest a structured variant as
|
|
150
|
+
* `{ default: { id: 'prod_abc' } }`. A call-site selector always wins.
|
|
151
|
+
* @param config - Optional Intlayer configuration. When provided, sets
|
|
152
|
+
* `window.INTLAYER_CONFIG` with the browser-safe subset of the config so
|
|
153
|
+
* the application works without a Vite/webpack plugin alias.
|
|
154
|
+
* @returns The IntlayerClient singleton.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* import { installIntlayer } from 'lit-intlayer';
|
|
159
|
+
*
|
|
160
|
+
* // With explicit config (no build plugin alias needed):
|
|
161
|
+
* installIntlayer('en', true, {
|
|
162
|
+
* internationalization: { locales: ['en', 'fr'], defaultLocale: 'en' },
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* // With build plugin (alias handles config automatically):
|
|
166
|
+
* installIntlayer('en');
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
var installIntlayer = ({ locale, isCookieEnabled, variant } = {}) => {
|
|
170
|
+
const client = createIntlayerClient(locale, isCookieEnabled, variant);
|
|
171
|
+
useEditor();
|
|
172
|
+
return client;
|
|
173
|
+
};
|
|
174
|
+
export { useEditor as a, installIntlayer as i, createIntlayerClient as n, getIntlayerClient as r, IntlayerClient as t };
|