@nocode-factory/sanity-kit 2.26.0 → 2.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/check-environments.mjs +4 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +247 -48
- package/dist/index.js.map +1 -1
- package/dist/web.js +1 -0
- package/dist/web.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +40 -9
- package/src/config/defaults.ts +1 -1
- package/src/config/resolve.ts +3 -0
- package/src/config/types.ts +8 -0
- package/src/form/OverrideInput.tsx +62 -1
- package/src/form/TranslationTodoInput.tsx +36 -2
- package/src/hooks/useTranslationTodo.ts +2 -1
- package/src/index.ts +5 -1
- package/src/lib/aiTexts.ts +72 -0
- package/src/lib/aiTranslate.ts +61 -0
- package/src/version.ts +1 -1
- package/src/web/PreviewBanner.tsx +3 -1
|
@@ -18,6 +18,8 @@ import { createServer } from "node:net";
|
|
|
18
18
|
import { existsSync, readFileSync } from "node:fs";
|
|
19
19
|
import { webcrypto as crypto } from "node:crypto";
|
|
20
20
|
|
|
21
|
+
// Bandeau de préversion : repère technique, puis anciens libellés (kits < 2.27.1).
|
|
22
|
+
const BANNER = /data-ncf-preview-banner|aria-label="(?:Données affichées|Displayed data)"/;
|
|
21
23
|
const args = process.argv.slice(2);
|
|
22
24
|
const flag = (name) => {
|
|
23
25
|
const i = args.indexOf(`--${name}`);
|
|
@@ -144,7 +146,7 @@ try {
|
|
|
144
146
|
check("prod : données en ligne", /<meta name="ncf-data" content="published"/.test(html));
|
|
145
147
|
// Le bandeau est figé au build dans les pages statiques : vérifiable seulement quand
|
|
146
148
|
// CE build est celui de la production (postbuild sur Vercel, VERCEL_ENV=production).
|
|
147
|
-
if (process.env.VERCEL_ENV === "production") check("prod : pas de bandeau de préversion",
|
|
149
|
+
if (process.env.VERCEL_ENV === "production") check("prod : pas de bandeau de préversion", !BANNER.test(html));
|
|
148
150
|
|
|
149
151
|
const forged = await get(base, PAGE, { __prerender_bypass: previewModeId, "sanity-preview-perspective": "drafts" });
|
|
150
152
|
check("prod : cookie de mode brouillon VALIDE ignoré (données en ligne)", (await dataOf(forged)) === "published", `HTTP ${forged.status}`);
|
|
@@ -188,7 +190,7 @@ try {
|
|
|
188
190
|
check("préprod : par défaut, données en ligne", /<meta name="ncf-data" content="published"/.test(liveHtml));
|
|
189
191
|
// Même raison que pour la prod : le bandeau est figé au build ; un build de
|
|
190
192
|
// production (VERCEL_ENV=production) n'en contient pas, même servi en préprod simulée.
|
|
191
|
-
if (process.env.VERCEL_ENV !== "production") check("préprod : bandeau de préversion présent",
|
|
193
|
+
if (process.env.VERCEL_ENV !== "production") check("préprod : bandeau de préversion présent", BANNER.test(liveHtml));
|
|
192
194
|
|
|
193
195
|
const on = await get(base, `/api/draft-mode/toggle?enable=true&redirect=${encodeURIComponent(PAGE)}`, jar);
|
|
194
196
|
check("préprod : bascule « Brouillons » redirige vers la page", on.status >= 300 && on.status < 400 && sameOrigin(base, on), `HTTP ${on.status}`);
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,13 @@ interface NcfI18nConfig {
|
|
|
38
38
|
defaultLocale?: string;
|
|
39
39
|
/** Langues attendues. Vide = déduites des documents rencontrés. */
|
|
40
40
|
locales?: string[];
|
|
41
|
+
/**
|
|
42
|
+
* Langue SOURCE des valeurs par défaut des composants (défaut : `en` si le site
|
|
43
|
+
* l'a, sinon la langue principale). Publier ses valeurs par défaut propose de
|
|
44
|
+
* retraduire celles des autres langues ; un champ qui hérite d'une valeur par
|
|
45
|
+
* défaut non traduite propose « Translate default with AI ».
|
|
46
|
+
*/
|
|
47
|
+
defaultsSourceLocale?: string;
|
|
41
48
|
}
|
|
42
49
|
/**
|
|
43
50
|
* Aperçu visuel des blocs du page builder. Aujourd'hui un seul fournisseur :
|
|
@@ -229,6 +236,7 @@ interface ResolvedI18n {
|
|
|
229
236
|
field: string;
|
|
230
237
|
defaultLocale: string;
|
|
231
238
|
locales: string[];
|
|
239
|
+
defaultsSourceLocale: string;
|
|
232
240
|
}
|
|
233
241
|
interface ResolvedComponentPreview {
|
|
234
242
|
provider: 'designSystemPage' | 'none';
|
|
@@ -309,7 +317,7 @@ export declare function hasBlockingIssue(issues: ConfigIssue[], tool: NcfToolNam
|
|
|
309
317
|
export declare const DEFAULT_PAGE_BUILDER_FIELD = "sections";
|
|
310
318
|
export declare const DEFAULT_SLUG_FIELD = "slug";
|
|
311
319
|
export declare const DEFAULT_TITLE_FIELD = "title";
|
|
312
|
-
export declare const DEFAULT_I18N: Required<NcfI18nConfig>;
|
|
320
|
+
export declare const DEFAULT_I18N: Omit<Required<NcfI18nConfig>, 'defaultsSourceLocale'>;
|
|
313
321
|
/**
|
|
314
322
|
* Convention NoCode Factory : page `/design-system` dans le front, captures
|
|
315
323
|
* dans `/sections/<nom>.png` générées au push par GitHub Actions.
|
|
@@ -1340,7 +1348,7 @@ export declare function ActorName(props: {
|
|
|
1340
1348
|
* quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
|
|
1341
1349
|
* le vérifie.
|
|
1342
1350
|
*/
|
|
1343
|
-
export declare const KIT_VERSION = "2.
|
|
1351
|
+
export declare const KIT_VERSION = "2.27.1";
|
|
1344
1352
|
/**
|
|
1345
1353
|
* Données de la page proposées comme colonnes.
|
|
1346
1354
|
*
|
package/dist/index.js
CHANGED
|
@@ -127,7 +127,8 @@ function resolveConfig(input) {
|
|
|
127
127
|
})), i18n = input.i18n === !1 || input.i18n === void 0 ? null : {
|
|
128
128
|
field: input.i18n.field?.trim() || DEFAULT_I18N.field,
|
|
129
129
|
defaultLocale: input.i18n.defaultLocale?.trim() || DEFAULT_I18N.defaultLocale,
|
|
130
|
-
locales: input.i18n.locales ?? DEFAULT_I18N.locales
|
|
130
|
+
locales: input.i18n.locales ?? DEFAULT_I18N.locales,
|
|
131
|
+
defaultsSourceLocale: input.i18n.defaultsSourceLocale?.trim() || ((input.i18n.locales ?? DEFAULT_I18N.locales).includes("en") ? "en" : input.i18n.defaultLocale?.trim() || DEFAULT_I18N.defaultLocale)
|
|
131
132
|
};
|
|
132
133
|
return {
|
|
133
134
|
pageTypes,
|
|
@@ -1419,7 +1420,7 @@ function explainGrantError(error) {
|
|
|
1419
1420
|
* quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
|
|
1420
1421
|
* le vérifie.
|
|
1421
1422
|
*/
|
|
1422
|
-
const KIT_VERSION = "2.
|
|
1423
|
+
const KIT_VERSION = "2.27.1";
|
|
1423
1424
|
/**
|
|
1424
1425
|
* Signale ce Studio au service du kit, une fois par montage.
|
|
1425
1426
|
*
|
|
@@ -1762,7 +1763,7 @@ function useKitUpdate() {
|
|
|
1762
1763
|
!controller.signal.aborted && body?.version && setLatest(body.version);
|
|
1763
1764
|
}).catch(() => {}), () => controller.abort();
|
|
1764
1765
|
}, []);
|
|
1765
|
-
let available = !!(latest && !isPrerelease(latest) && compareVersions(latest, "2.
|
|
1766
|
+
let available = !!(latest && !isPrerelease(latest) && compareVersions(latest, "2.27.1") > 0);
|
|
1766
1767
|
return {
|
|
1767
1768
|
current: KIT_VERSION,
|
|
1768
1769
|
latest,
|
|
@@ -7649,6 +7650,92 @@ function DesignSystemTool() {
|
|
|
7649
7650
|
})
|
|
7650
7651
|
});
|
|
7651
7652
|
}
|
|
7653
|
+
const isObj$1 = (v) => typeof v == "object" && !!v && !Array.isArray(v);
|
|
7654
|
+
/** Textes d'une valeur, dans l'ordre. */
|
|
7655
|
+
function collectTexts$1(value) {
|
|
7656
|
+
if (typeof value == "string") return [value];
|
|
7657
|
+
if (!Array.isArray(value)) return [];
|
|
7658
|
+
let out = [];
|
|
7659
|
+
for (let item of value) if (typeof item == "string") out.push(item);
|
|
7660
|
+
else if (isObj$1(item) && Array.isArray(item.children)) for (let child of item.children) isObj$1(child) && typeof child.text == "string" && out.push(child.text);
|
|
7661
|
+
return out;
|
|
7662
|
+
}
|
|
7663
|
+
/** La même valeur avec ses textes remplacés (dans l'ordre de `collectTexts`). */
|
|
7664
|
+
function replaceTexts(value, texts) {
|
|
7665
|
+
let i = 0, next = () => texts[i++] ?? "";
|
|
7666
|
+
return typeof value == "string" ? next() : Array.isArray(value) ? value.map((item) => typeof item == "string" ? next() : isObj$1(item) && Array.isArray(item.children) ? {
|
|
7667
|
+
...item,
|
|
7668
|
+
children: item.children.map((child) => isObj$1(child) && typeof child.text == "string" ? {
|
|
7669
|
+
...child,
|
|
7670
|
+
text: next()
|
|
7671
|
+
} : child)
|
|
7672
|
+
} : item) : value;
|
|
7673
|
+
}
|
|
7674
|
+
/** `sections[a].title` → segments (`[x]` : clé d'élément de liste, ou rang). */
|
|
7675
|
+
function parsePathKey(path) {
|
|
7676
|
+
let out = [];
|
|
7677
|
+
for (let m of path.matchAll(/([^.[\]]+)|\[([^\]]+)\]/g)) out.push(m[1] === void 0 ? { item: m[2] } : { field: m[1] });
|
|
7678
|
+
return out;
|
|
7679
|
+
}
|
|
7680
|
+
function itemOf(list, key) {
|
|
7681
|
+
return list.find((i) => isObj$1(i) && i._key === key) ?? (/^\d+$/.test(key) ? list[Number(key)] : void 0);
|
|
7682
|
+
}
|
|
7683
|
+
/** Valeur d'un document au chemin `sections[a].title`. */
|
|
7684
|
+
function valueAtKey(doc, path) {
|
|
7685
|
+
let cur = doc;
|
|
7686
|
+
for (let seg of parsePathKey(path)) {
|
|
7687
|
+
if (cur == null) return;
|
|
7688
|
+
cur = "field" in seg ? isObj$1(cur) ? cur[seg.field] : void 0 : Array.isArray(cur) ? itemOf(cur, seg.item) : void 0;
|
|
7689
|
+
}
|
|
7690
|
+
return cur;
|
|
7691
|
+
}
|
|
7692
|
+
/** Écrit `value` au chemin dans `doc` (modifié sur place). Faux si le chemin n'existe pas. */
|
|
7693
|
+
function setAtKey(doc, path, value) {
|
|
7694
|
+
let segs = parsePathKey(path), cur = doc;
|
|
7695
|
+
for (let n = 0; n < segs.length - 1; n++) {
|
|
7696
|
+
let seg = segs[n];
|
|
7697
|
+
if (cur = "field" in seg ? isObj$1(cur) ? cur[seg.field] : void 0 : Array.isArray(cur) ? itemOf(cur, seg.item) : void 0, cur == null) return !1;
|
|
7698
|
+
}
|
|
7699
|
+
let last = segs[segs.length - 1];
|
|
7700
|
+
return !last || !("field" in last) || !isObj$1(cur) ? !1 : (cur[last.field] = value, !0);
|
|
7701
|
+
}
|
|
7702
|
+
const MESSAGES = {
|
|
7703
|
+
daily_limit: "Daily AI translation limit reached for this project.",
|
|
7704
|
+
project_inactive: "This project is not active in the NCF kit.",
|
|
7705
|
+
bad_key: "This project is not registered in the NCF kit (npx ncf-kit setup).",
|
|
7706
|
+
not_configured: "AI translation is not configured on the NCF kit service.",
|
|
7707
|
+
too_long: "Text too long to translate at once."
|
|
7708
|
+
};
|
|
7709
|
+
async function call(service, from, to, texts) {
|
|
7710
|
+
let response = await fetch(`${service.endpoint}/translate`, {
|
|
7711
|
+
method: "POST",
|
|
7712
|
+
headers: { "content-type": "application/json" },
|
|
7713
|
+
body: JSON.stringify({
|
|
7714
|
+
key: service.projectKey,
|
|
7715
|
+
from,
|
|
7716
|
+
to,
|
|
7717
|
+
texts
|
|
7718
|
+
})
|
|
7719
|
+
}), body = await response.json().catch(() => ({}));
|
|
7720
|
+
if (!response.ok || !Array.isArray(body.texts) || body.texts.length !== texts.length) throw Error(MESSAGES[body.error ?? ""] ?? `AI translation failed (${body.error ?? response.status}).`);
|
|
7721
|
+
return body.texts;
|
|
7722
|
+
}
|
|
7723
|
+
/**
|
|
7724
|
+
* Traduit des valeurs de champs (chaînes ou textes riches) de `from` vers `to` :
|
|
7725
|
+
* mêmes valeurs, textes traduits. Découpe en lots si besoin.
|
|
7726
|
+
*/
|
|
7727
|
+
async function translateValues(service, from, to, values) {
|
|
7728
|
+
let perValue = values.map((v) => collectTexts$1(v)), all = perValue.flat(), translated = [], batch = [], size = 0, flush = async () => {
|
|
7729
|
+
batch.length && (translated.push(...await call(service, from, to, batch)), batch = [], size = 0);
|
|
7730
|
+
};
|
|
7731
|
+
for (let text of all) (batch.length >= 250 || size + text.length > 3e4) && await flush(), batch.push(text), size += text.length;
|
|
7732
|
+
await flush();
|
|
7733
|
+
let offset = 0;
|
|
7734
|
+
return values.map((value, n) => {
|
|
7735
|
+
let count = perValue[n].length, slice = translated.slice(offset, offset + count);
|
|
7736
|
+
return offset += count, count ? replaceTexts(value, slice) : value;
|
|
7737
|
+
});
|
|
7738
|
+
}
|
|
7652
7739
|
const store$1 = /* @__PURE__ */ new Map();
|
|
7653
7740
|
function entryFor$1(client, type, languageField, language) {
|
|
7654
7741
|
let { projectId, dataset } = client.config(), key = `${projectId}:${dataset}:${type}:${language ?? ""}`, entry = store$1.get(key);
|
|
@@ -7816,7 +7903,28 @@ function createOverrideInput(config) {
|
|
|
7816
7903
|
});
|
|
7817
7904
|
}
|
|
7818
7905
|
function WithDot(props) {
|
|
7819
|
-
let { rel, inList, shortName, parent, ...inputProps } = props, client = useKitClient(), languageField = config.i18n?.field ?? "language", language = useFormValue([languageField]) ?? null,
|
|
7906
|
+
let { rel, inList, shortName, parent, ...inputProps } = props, client = useKitClient(), languageField = config.i18n?.field ?? "language", language = useFormValue([languageField]) ?? null, defaults = useComponentDefaults(client, defaultsType, languageField, language), inherited = defaultValueAt(defaults, shortName, rel), field = rel[rel.length - 1] ?? "", sourceLocale = config.i18n?.defaultsSourceLocale ?? null, sourceDefault = defaultValueAt(useComponentDefaults(client, defaultsType, languageField, sourceLocale), shortName, rel), [translating, setTranslating] = useState(!1), [translateError, setTranslateError] = useState(null), untranslatedDefault = !!(config.history && config.i18n && language && sourceLocale && language !== sourceLocale) && !inputProps.readOnly && isEmptyValue(inputProps.value) && collectTexts$1(sourceDefault).some((t) => t.trim()) && (isEmptyValue(inherited) || JSON.stringify(inherited) === JSON.stringify(sourceDefault)), translateDefault = async () => {
|
|
7907
|
+
let localeId = defaults?._id?.replace(/^drafts\./, "");
|
|
7908
|
+
if (config.history && sourceLocale && language) {
|
|
7909
|
+
setTranslating(!0), setTranslateError(null);
|
|
7910
|
+
try {
|
|
7911
|
+
if (!localeId) throw Error(`No ${language.toUpperCase()} component defaults document.`);
|
|
7912
|
+
let [translated] = await translateValues(config.history, sourceLocale, language, [sourceDefault]), published = await client.getDocument(localeId), path = [shortName, ...rel].map((seg, n) => typeof seg == "number" ? `[${seg}]` : n ? `.${seg}` : seg).join(""), tx = client.transaction();
|
|
7913
|
+
if (published) {
|
|
7914
|
+
let { _rev, _updatedAt, _createdAt, ...copy } = published;
|
|
7915
|
+
tx.createIfNotExists({
|
|
7916
|
+
...copy,
|
|
7917
|
+
_id: `drafts.${localeId}`
|
|
7918
|
+
});
|
|
7919
|
+
}
|
|
7920
|
+
tx.patch(`drafts.${localeId}`, (patch) => patch.set({ [path]: translated })), await tx.commit();
|
|
7921
|
+
} catch (cause) {
|
|
7922
|
+
setTranslateError(cause instanceof Error ? cause.message : String(cause));
|
|
7923
|
+
} finally {
|
|
7924
|
+
setTranslating(!1);
|
|
7925
|
+
}
|
|
7926
|
+
}
|
|
7927
|
+
}, router = useRouter(), inherits = isEmptyValue(inputProps.value) && !isEmptyValue(inherited), st = inputProps.schemaType, freeText = (st.jsonType === "string" || st.jsonType === "number") && !st.options?.list?.length, placeholder = inherits && freeText ? inheritedPlaceholder(inherited) : null, shown = placeholder === null ? inputProps : {
|
|
7820
7928
|
...inputProps,
|
|
7821
7929
|
elementProps: {
|
|
7822
7930
|
...inputProps.elementProps,
|
|
@@ -7829,14 +7937,48 @@ function createOverrideInput(config) {
|
|
|
7829
7937
|
children: [/* @__PURE__ */ jsxs(Box, {
|
|
7830
7938
|
flex: 1,
|
|
7831
7939
|
style: { minWidth: 0 },
|
|
7832
|
-
children: [
|
|
7833
|
-
|
|
7834
|
-
|
|
7835
|
-
|
|
7836
|
-
|
|
7837
|
-
|
|
7940
|
+
children: [
|
|
7941
|
+
inputProps.renderDefault(shown),
|
|
7942
|
+
untranslatedDefault && /* @__PURE__ */ jsxs(Flex, {
|
|
7943
|
+
align: "center",
|
|
7944
|
+
gap: 2,
|
|
7945
|
+
marginTop: 2,
|
|
7946
|
+
wrap: "wrap",
|
|
7947
|
+
children: [
|
|
7948
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
7949
|
+
size: 0,
|
|
7950
|
+
muted: !0,
|
|
7951
|
+
children: [
|
|
7952
|
+
"Default not translated to ",
|
|
7953
|
+
String(language).toUpperCase(),
|
|
7954
|
+
"."
|
|
7955
|
+
]
|
|
7956
|
+
}),
|
|
7957
|
+
/* @__PURE__ */ jsx(Button, {
|
|
7958
|
+
mode: "ghost",
|
|
7959
|
+
tone: "primary",
|
|
7960
|
+
fontSize: 0,
|
|
7961
|
+
padding: 2,
|
|
7962
|
+
text: translating ? "Translating…" : "Translate default with AI",
|
|
7963
|
+
disabled: translating,
|
|
7964
|
+
onClick: () => void translateDefault()
|
|
7965
|
+
}),
|
|
7966
|
+
translateError && /* @__PURE__ */ jsx(Text, {
|
|
7967
|
+
size: 0,
|
|
7968
|
+
style: { color: "var(--card-badge-critical-fg-color, #c00)" },
|
|
7969
|
+
children: translateError
|
|
7970
|
+
})
|
|
7971
|
+
]
|
|
7972
|
+
}),
|
|
7973
|
+
inherits && placeholder === null && /* @__PURE__ */ jsx(Box, {
|
|
7974
|
+
marginTop: 2,
|
|
7975
|
+
children: /* @__PURE__ */ jsxs(Text, {
|
|
7976
|
+
size: 0,
|
|
7977
|
+
muted: !0,
|
|
7978
|
+
children: ["Component default value: ", describeValue(inherited)]
|
|
7979
|
+
})
|
|
7838
7980
|
})
|
|
7839
|
-
|
|
7981
|
+
]
|
|
7840
7982
|
}), override && !inputProps.readOnly && /* @__PURE__ */ jsx(OverrideDot, {
|
|
7841
7983
|
hasDefault: action.kind !== "noDefault",
|
|
7842
7984
|
full: describeValue(inherited),
|
|
@@ -8067,7 +8209,7 @@ function entryFor(client, documentId) {
|
|
|
8067
8209
|
}, store.set(key, entry));
|
|
8068
8210
|
let e = entry;
|
|
8069
8211
|
if (!e.stop) {
|
|
8070
|
-
let load = () => client.fetch("*[_id == $id][0]{_id, sourceLanguage, paths}", { id }).then((value) => {
|
|
8212
|
+
let load = () => client.fetch("*[_id == $id][0]{_id, sourceId, sourceLanguage, paths}", { id }).then((value) => {
|
|
8071
8213
|
e.value = value, e.listeners.forEach((l) => l());
|
|
8072
8214
|
}).catch(() => {});
|
|
8073
8215
|
load();
|
|
@@ -8095,40 +8237,74 @@ function fieldPathKey(path) {
|
|
|
8095
8237
|
}
|
|
8096
8238
|
/**
|
|
8097
8239
|
* Sous un champ d'une traduction modifié dans la version principale (propagation) :
|
|
8098
|
-
* « À traduire », avec « Marquer comme traduit » qui le retire de la liste
|
|
8240
|
+
* « À traduire », avec « Marquer comme traduit » qui le retire de la liste, et
|
|
8241
|
+
* « Translate with AI » (service du kit) : traduit le texte de la version principale
|
|
8242
|
+
* dans ce champ (brouillon de cette langue), puis le retire de la liste.
|
|
8099
8243
|
*/
|
|
8100
8244
|
function withTranslationTodo(Inner, config) {
|
|
8101
8245
|
function TranslationTodoInput(props) {
|
|
8102
|
-
let id = useFormValue(["_id"]), client = useKitClient(), todo = useTranslationTodo(client, config.i18n ? id : void 0), [busy, setBusy] = useState(!1), key = fieldPathKey(props.path), inner = createElement(Inner, props), show = !!todo?.paths?.includes(key), done = () => {
|
|
8246
|
+
let id = useFormValue(["_id"]), client = useKitClient(), todo = useTranslationTodo(client, config.i18n ? id : void 0), [busy, setBusy] = useState(!1), [error, setError] = useState(null), language = useFormValue([config.i18n?.field ?? "language"]), key = fieldPathKey(props.path), inner = createElement(Inner, props), show = !!todo?.paths?.includes(key), done = () => {
|
|
8103
8247
|
todo && (setBusy(!0), client.patch(todo._id).set({ paths: (todo.paths ?? []).filter((p) => p !== key) }).commit().finally(() => setBusy(!1)));
|
|
8248
|
+
}, translate = async () => {
|
|
8249
|
+
if (todo?.sourceId && todo.sourceLanguage && language && config.history) {
|
|
8250
|
+
setBusy(!0), setError(null);
|
|
8251
|
+
try {
|
|
8252
|
+
let value = valueAtKey(await client.getDocument(todo.sourceId) ?? await client.getDocument(`drafts.${todo.sourceId}`), key);
|
|
8253
|
+
if (value === void 0) throw Error("Source text not found.");
|
|
8254
|
+
let [translated] = await translateValues(config.history, todo.sourceLanguage, language, [value]);
|
|
8255
|
+
props.onChange(set(translated)), await client.patch(todo._id).set({ paths: (todo.paths ?? []).filter((p) => p !== key) }).commit();
|
|
8256
|
+
} catch (cause) {
|
|
8257
|
+
setError(cause instanceof Error ? cause.message : String(cause));
|
|
8258
|
+
} finally {
|
|
8259
|
+
setBusy(!1);
|
|
8260
|
+
}
|
|
8261
|
+
}
|
|
8104
8262
|
};
|
|
8105
8263
|
return /* @__PURE__ */ jsxs(Stack, {
|
|
8106
8264
|
gap: 2,
|
|
8107
|
-
children: [inner, show && /* @__PURE__ */
|
|
8265
|
+
children: [inner, show && /* @__PURE__ */ jsxs(Card, {
|
|
8108
8266
|
tone: "caution",
|
|
8109
8267
|
padding: 2,
|
|
8110
8268
|
radius: 2,
|
|
8111
8269
|
border: !0,
|
|
8112
|
-
children: /* @__PURE__ */ jsxs(Flex, {
|
|
8270
|
+
children: [/* @__PURE__ */ jsxs(Flex, {
|
|
8113
8271
|
align: "center",
|
|
8114
8272
|
gap: 2,
|
|
8115
|
-
children: [
|
|
8116
|
-
|
|
8117
|
-
|
|
8118
|
-
|
|
8119
|
-
|
|
8120
|
-
|
|
8121
|
-
|
|
8122
|
-
|
|
8123
|
-
|
|
8124
|
-
|
|
8125
|
-
|
|
8126
|
-
|
|
8127
|
-
|
|
8128
|
-
|
|
8129
|
-
|
|
8130
|
-
|
|
8131
|
-
|
|
8273
|
+
children: [
|
|
8274
|
+
/* @__PURE__ */ jsxs(Text, {
|
|
8275
|
+
size: 1,
|
|
8276
|
+
style: { flex: 1 },
|
|
8277
|
+
children: [
|
|
8278
|
+
"To translate: changed in the ",
|
|
8279
|
+
(todo?.sourceLanguage ?? "").toUpperCase(),
|
|
8280
|
+
" version."
|
|
8281
|
+
]
|
|
8282
|
+
}),
|
|
8283
|
+
config.history && !props.readOnly && /* @__PURE__ */ jsx(Button, {
|
|
8284
|
+
tone: "primary",
|
|
8285
|
+
fontSize: 1,
|
|
8286
|
+
padding: 2,
|
|
8287
|
+
text: busy ? "Translating…" : "Translate with AI",
|
|
8288
|
+
disabled: busy,
|
|
8289
|
+
onClick: () => void translate()
|
|
8290
|
+
}),
|
|
8291
|
+
/* @__PURE__ */ jsx(Button, {
|
|
8292
|
+
mode: "ghost",
|
|
8293
|
+
fontSize: 1,
|
|
8294
|
+
padding: 2,
|
|
8295
|
+
text: "Mark as translated",
|
|
8296
|
+
disabled: busy,
|
|
8297
|
+
onClick: done
|
|
8298
|
+
})
|
|
8299
|
+
]
|
|
8300
|
+
}), error && /* @__PURE__ */ jsx(Text, {
|
|
8301
|
+
size: 1,
|
|
8302
|
+
style: {
|
|
8303
|
+
marginTop: 8,
|
|
8304
|
+
color: "var(--card-badge-critical-fg-color, #c00)"
|
|
8305
|
+
},
|
|
8306
|
+
children: error
|
|
8307
|
+
})]
|
|
8132
8308
|
})]
|
|
8133
8309
|
});
|
|
8134
8310
|
}
|
|
@@ -8377,13 +8553,16 @@ function hasChanges(r) {
|
|
|
8377
8553
|
* part normalement, puis « Propager aux autres langues ? » propose d'appliquer aux
|
|
8378
8554
|
* BROUILLONS des traductions la structure (sections ajoutées, supprimées, déplacées)
|
|
8379
8555
|
* et les valeurs non textuelles (images, liens, options…). Les textes traduits ne
|
|
8380
|
-
* sont jamais écrasés : les textes modifiés sont
|
|
8381
|
-
*
|
|
8382
|
-
|
|
8383
|
-
|
|
8384
|
-
|
|
8556
|
+
* sont jamais écrasés à la main : les textes modifiés sont traduits par l'IA
|
|
8557
|
+
* (« Translate with AI », service du kit, coché par défaut) ou marqués « à
|
|
8558
|
+
* traduire ». Rien n'est publié dans les autres langues : on relit, on publie.
|
|
8559
|
+
* `masterLocale` : langue source (défaut : langue principale du site ; les valeurs
|
|
8560
|
+
* par défaut des composants ont la leur, `i18n.defaultsSourceLocale`).
|
|
8561
|
+
*/
|
|
8562
|
+
function withPropagation(Publish, config, masterLocale) {
|
|
8563
|
+
let i18n = config.i18n, master = masterLocale ?? i18n?.defaultLocale, Wrapped = (props) => {
|
|
8385
8564
|
let base = Publish(props), client = useKitClient(), schema = useSchema(), [run, setRun] = useState(null), current = props.draft ?? props.published;
|
|
8386
|
-
return !base || !i18n || current?.[i18n.field] !==
|
|
8565
|
+
return !base || !i18n || current?.[i18n.field] !== master ? base : {
|
|
8387
8566
|
...base,
|
|
8388
8567
|
onHandle: () => {
|
|
8389
8568
|
let before = props.published, after = props.draft;
|
|
@@ -8437,18 +8616,24 @@ async function findTargets(client, id, source, before, after, documentType) {
|
|
|
8437
8616
|
return list;
|
|
8438
8617
|
}
|
|
8439
8618
|
function PropagateDialog(props) {
|
|
8440
|
-
let { config, id, type, after, onDone } = props, client = useKitClient(), field = config.i18n?.field ?? "language", source = String(after[field] ?? ""), [targets, setTargets] = useState(props.targets), [busy, setBusy] = useState(!1), toast = useToast(), [error, setError] = useState(null), apply = async () => {
|
|
8619
|
+
let { config, id, type, after, onDone } = props, client = useKitClient(), field = config.i18n?.field ?? "language", source = String(after[field] ?? ""), [targets, setTargets] = useState(props.targets), [busy, setBusy] = useState(!1), toast = useToast(), [error, setError] = useState(null), [ai, setAi] = useState(!!config.history), apply = async () => {
|
|
8441
8620
|
setBusy(!0), setError(null);
|
|
8442
8621
|
try {
|
|
8443
|
-
let applied = [];
|
|
8622
|
+
let applied = [], translatedCount = 0, aiError = null;
|
|
8444
8623
|
for (let t of targets) {
|
|
8445
8624
|
if (!t.selected) continue;
|
|
8446
|
-
let { _rev, _createdAt, _updatedAt, ...doc } = t.result.document;
|
|
8625
|
+
let { _rev, _createdAt, _updatedAt, ...doc } = t.result.document, remaining = t.result.toTranslate;
|
|
8626
|
+
if (ai && config.history && remaining.length) try {
|
|
8627
|
+
let values = remaining.map((path) => valueAtKey(after, path)), translated = await translateValues(config.history, source, t.language, values);
|
|
8628
|
+
remaining = remaining.filter((path, n) => values[n] === void 0 || !setAtKey(doc, path, translated[n])), translatedCount += t.result.toTranslate.length - remaining.length;
|
|
8629
|
+
} catch (cause) {
|
|
8630
|
+
aiError = cause instanceof Error ? cause.message : String(cause);
|
|
8631
|
+
}
|
|
8447
8632
|
if (await client.createOrReplace({
|
|
8448
8633
|
...doc,
|
|
8449
8634
|
_id: `drafts.${t.id}`,
|
|
8450
8635
|
_type: type
|
|
8451
|
-
}),
|
|
8636
|
+
}), remaining.length) {
|
|
8452
8637
|
let todoId = translationTodoId(t.id), existing = await client.fetch("*[_id == $todoId][0].paths", { todoId });
|
|
8453
8638
|
await client.createOrReplace({
|
|
8454
8639
|
_id: todoId,
|
|
@@ -8456,15 +8641,15 @@ function PropagateDialog(props) {
|
|
|
8456
8641
|
documentId: t.id,
|
|
8457
8642
|
sourceId: id,
|
|
8458
8643
|
sourceLanguage: source,
|
|
8459
|
-
paths: [.../* @__PURE__ */ new Set([...existing ?? [], ...
|
|
8644
|
+
paths: [.../* @__PURE__ */ new Set([...existing ?? [], ...remaining])]
|
|
8460
8645
|
});
|
|
8461
8646
|
}
|
|
8462
8647
|
applied.push(t.language.toUpperCase());
|
|
8463
8648
|
}
|
|
8464
8649
|
toast.push({
|
|
8465
|
-
status: "success",
|
|
8650
|
+
status: aiError ? "warning" : "success",
|
|
8466
8651
|
title: `Drafts updated: ${applied.join(", ")}`,
|
|
8467
|
-
description: "Translate the fields marked \"To translate\", then publish each language.",
|
|
8652
|
+
description: aiError ? `AI translation failed (${aiError}): the changed texts are marked "To translate".` : translatedCount ? `${translatedCount} text${translatedCount > 1 ? "s" : ""} translated with AI. Review, then publish each language.` : "Translate the fields marked \"To translate\", then publish each language.",
|
|
8468
8653
|
duration: 8e3
|
|
8469
8654
|
}), onDone();
|
|
8470
8655
|
} catch (cause) {
|
|
@@ -8482,9 +8667,23 @@ function PropagateDialog(props) {
|
|
|
8482
8667
|
children: [
|
|
8483
8668
|
"The ",
|
|
8484
8669
|
source.toUpperCase(),
|
|
8485
|
-
" version is published. Apply its changes to the drafts of the other languages?
|
|
8670
|
+
" version is published. Apply its changes to the drafts of the other languages? Changed texts are ",
|
|
8671
|
+
ai ? "translated with AI" : "marked \"To translate\"",
|
|
8672
|
+
". Nothing is published."
|
|
8486
8673
|
]
|
|
8487
8674
|
}),
|
|
8675
|
+
config.history && targets.some((t) => t.result.toTranslate.length) && /* @__PURE__ */ jsxs(Flex, {
|
|
8676
|
+
as: "label",
|
|
8677
|
+
align: "center",
|
|
8678
|
+
gap: 2,
|
|
8679
|
+
children: [/* @__PURE__ */ jsx(Checkbox, {
|
|
8680
|
+
checked: ai,
|
|
8681
|
+
onChange: () => setAi(!ai)
|
|
8682
|
+
}), /* @__PURE__ */ jsx(Text, {
|
|
8683
|
+
size: 1,
|
|
8684
|
+
children: "Translate changed texts with AI"
|
|
8685
|
+
})]
|
|
8686
|
+
}),
|
|
8488
8687
|
/* @__PURE__ */ jsx(Stack, {
|
|
8489
8688
|
gap: 3,
|
|
8490
8689
|
children: targets.map((t, index) => /* @__PURE__ */ jsx(Card, {
|
|
@@ -8860,7 +9059,7 @@ const ncfKit = definePlugin((options) => {
|
|
|
8860
9059
|
},
|
|
8861
9060
|
document: {
|
|
8862
9061
|
newDocumentOptions: (prev) => prev.filter((option) => option.templateId !== BOARD_LAYOUT_TYPE),
|
|
8863
|
-
actions: (prev, context) => config.pageTypeNames.includes(context.schemaType) ? prev.map((action) => action.action === "unpublish" ? RemoveFromSiteAction : action.action === "publish" && config.i18n ? withPropagation(action, config) : action) : prev
|
|
9062
|
+
actions: (prev, context) => config.pageTypeNames.includes(context.schemaType) ? prev.map((action) => action.action === "unpublish" ? RemoveFromSiteAction : action.action === "publish" && config.i18n ? withPropagation(action, config) : action) : context.schemaType === config.componentPreview.defaultsType && config.i18n ? prev.map((action) => action.action === "publish" ? withPropagation(action, config, config.i18n?.defaultsSourceLocale) : action) : prev
|
|
8864
9063
|
},
|
|
8865
9064
|
studio: { components: { toolMenu: createPreviewLauncher(config, issues) } },
|
|
8866
9065
|
form: { components: { input: createKitInput(config) } }
|