@textunited/sanity-plugin 0.1.1 → 0.1.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/CHANGELOG.md +19 -0
- package/dist/{chunk-BEHPJRZM.cjs → chunk-5DQ7NDBQ.cjs} +59 -5
- package/dist/chunk-5DQ7NDBQ.cjs.map +1 -0
- package/dist/{chunk-L37ROFN6.js → chunk-GK4JJHRR.js} +3 -3
- package/dist/{chunk-L37ROFN6.js.map → chunk-GK4JJHRR.js.map} +1 -1
- package/dist/{chunk-TA5JUFXP.js → chunk-H7WDVCKS.js} +59 -5
- package/dist/chunk-H7WDVCKS.js.map +1 -0
- package/dist/{chunk-BC676THN.cjs → chunk-S2E3ALGC.cjs} +58 -58
- package/dist/{chunk-BC676THN.cjs.map → chunk-S2E3ALGC.cjs.map} +1 -1
- package/dist/next/index.cjs +3 -3
- package/dist/next/index.js +2 -2
- package/dist/server/index.cjs +8 -8
- package/dist/server/index.js +2 -2
- package/dist/studio/index.cjs +23 -23
- package/dist/studio/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-BEHPJRZM.cjs.map +0 -1
- package/dist/chunk-TA5JUFXP.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunk5DQ7NDBQ_cjs = require('./chunk-5DQ7NDBQ.cjs');
|
|
4
4
|
|
|
5
5
|
// src/core/textunited/client.ts
|
|
6
6
|
var TextUnitedTimeoutError = class extends Error {
|
|
@@ -171,28 +171,28 @@ function translatedValue(unit, language, translations) {
|
|
|
171
171
|
const status = chunks.map((chunk) => chunk.status).find((s) => s !== "proofread") ?? chunks[0]?.status;
|
|
172
172
|
const contents = chunks.map((chunk) => chunk.content);
|
|
173
173
|
if (unit.kind === "text") {
|
|
174
|
-
return { value:
|
|
174
|
+
return { value: chunk5DQ7NDBQ_cjs.decodeHtmlEntities(chunk5DQ7NDBQ_cjs.joinTextChunks(contents)), status };
|
|
175
175
|
}
|
|
176
|
-
return { value:
|
|
176
|
+
return { value: chunk5DQ7NDBQ_cjs.applyBlockTranslation(unit.source, contents), status };
|
|
177
177
|
}
|
|
178
178
|
function writeValue(unit, language, value, targets, sourceDocument, sourceLanguage) {
|
|
179
179
|
if (targets.model === "documentLevel") {
|
|
180
180
|
const document = targets.documents[language];
|
|
181
181
|
if (!document) throw new Error(`No ${language} document to write the translation to`);
|
|
182
|
-
|
|
182
|
+
chunk5DQ7NDBQ_cjs.setAtPath(document, sourceDocument, [...unit.fieldPath, ...unit.innerPath], value);
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
const intlArray =
|
|
185
|
+
const intlArray = chunk5DQ7NDBQ_cjs.getAtPath(targets.document, unit.fieldPath);
|
|
186
186
|
if (!Array.isArray(intlArray)) throw new Error(`No internationalized field at ${unit.id}`);
|
|
187
|
-
const sourceItem =
|
|
188
|
-
let item =
|
|
187
|
+
const sourceItem = chunk5DQ7NDBQ_cjs.getIntlItem(chunk5DQ7NDBQ_cjs.getAtPath(sourceDocument, unit.fieldPath), sourceLanguage);
|
|
188
|
+
let item = chunk5DQ7NDBQ_cjs.getIntlItem(intlArray, language);
|
|
189
189
|
if (!item) {
|
|
190
190
|
item = {
|
|
191
191
|
_key: language,
|
|
192
|
-
...
|
|
192
|
+
...chunk5DQ7NDBQ_cjs.isPlainObject(sourceItem) && typeof sourceItem._type === "string" ? { _type: sourceItem._type } : {},
|
|
193
193
|
// Rich text starts as a copy of the source, so images and blocks without text are kept;
|
|
194
194
|
// translated blocks replace their originals.
|
|
195
|
-
value: unit.kind === "text" ? "" : Array.isArray(sourceItem?.value) ?
|
|
195
|
+
value: unit.kind === "text" ? "" : Array.isArray(sourceItem?.value) ? chunk5DQ7NDBQ_cjs.cloneForTranslation(sourceItem.value) : []
|
|
196
196
|
};
|
|
197
197
|
intlArray.push(item);
|
|
198
198
|
}
|
|
@@ -201,11 +201,11 @@ function writeValue(unit, language, value, targets, sourceDocument, sourceLangua
|
|
|
201
201
|
return;
|
|
202
202
|
}
|
|
203
203
|
if (!Array.isArray(item.value)) item.value = [];
|
|
204
|
-
|
|
204
|
+
chunk5DQ7NDBQ_cjs.setAtPath(item, { value: sourceItem?.value ?? [] }, ["value", ...unit.innerPath], value);
|
|
205
205
|
}
|
|
206
206
|
function applyTranslations(args) {
|
|
207
|
-
const targets =
|
|
208
|
-
const state =
|
|
207
|
+
const targets = chunk5DQ7NDBQ_cjs.cloneValue(args.targets);
|
|
208
|
+
const state = chunk5DQ7NDBQ_cjs.cloneValue(args.state);
|
|
209
209
|
const changedFields = /* @__PURE__ */ new Map();
|
|
210
210
|
const outcomes = [];
|
|
211
211
|
for (const unit of args.units) {
|
|
@@ -215,20 +215,20 @@ function applyTranslations(args) {
|
|
|
215
215
|
outcomes.push({ unitId: unit.id, language, outcome: "incomplete" });
|
|
216
216
|
continue;
|
|
217
217
|
}
|
|
218
|
-
const current =
|
|
218
|
+
const current = chunk5DQ7NDBQ_cjs.readTargetValue(unit, language, targets);
|
|
219
219
|
const record = state.units[unit.id];
|
|
220
220
|
const appliedHash = record?.languages[language]?.appliedHash;
|
|
221
|
-
const editedByHand = !
|
|
221
|
+
const editedByHand = !chunk5DQ7NDBQ_cjs.isMissingValue(unit, current) && appliedHash !== chunk5DQ7NDBQ_cjs.valueHash(current);
|
|
222
222
|
if (editedByHand && !args.overwrite?.has(unit.id)) {
|
|
223
223
|
outcomes.push({ unitId: unit.id, language, outcome: "keptManualEdit" });
|
|
224
224
|
continue;
|
|
225
225
|
}
|
|
226
226
|
const unitState = state.units[unit.id] ??= { sourceHash: unit.sourceHash, languages: {} };
|
|
227
227
|
unitState.languages[language] = {
|
|
228
|
-
appliedHash:
|
|
228
|
+
appliedHash: chunk5DQ7NDBQ_cjs.valueHash(translated.value),
|
|
229
229
|
status: translated.status
|
|
230
230
|
};
|
|
231
|
-
if (
|
|
231
|
+
if (chunk5DQ7NDBQ_cjs.valueHash(current) === chunk5DQ7NDBQ_cjs.valueHash(translated.value)) {
|
|
232
232
|
outcomes.push({ unitId: unit.id, language, outcome: "unchanged" });
|
|
233
233
|
continue;
|
|
234
234
|
}
|
|
@@ -339,7 +339,7 @@ var VALID_MODES = /* @__PURE__ */ new Set(["missing", "changed", "all"]);
|
|
|
339
339
|
function documentTitle(source, sourceLanguage) {
|
|
340
340
|
const title = source.title;
|
|
341
341
|
if (typeof title === "string") return title;
|
|
342
|
-
const localized =
|
|
342
|
+
const localized = chunk5DQ7NDBQ_cjs.getIntlItem(title, sourceLanguage)?.value;
|
|
343
343
|
return typeof localized === "string" ? localized : "";
|
|
344
344
|
}
|
|
345
345
|
function projectName(source, baseId, sourceLanguage, languages) {
|
|
@@ -375,7 +375,7 @@ async function processTranslationRequest(requestId, context) {
|
|
|
375
375
|
}
|
|
376
376
|
]);
|
|
377
377
|
} catch (error) {
|
|
378
|
-
if (error instanceof
|
|
378
|
+
if (error instanceof chunk5DQ7NDBQ_cjs.SanityConflictError) return { status: "ignored", reason: "Already being processed" };
|
|
379
379
|
throw error;
|
|
380
380
|
}
|
|
381
381
|
try {
|
|
@@ -400,7 +400,7 @@ async function processTranslationRequest(requestId, context) {
|
|
|
400
400
|
async function runRequest(request, context, newId) {
|
|
401
401
|
const { config } = context;
|
|
402
402
|
const now = context.now ?? (() => /* @__PURE__ */ new Date());
|
|
403
|
-
const baseId =
|
|
403
|
+
const baseId = chunk5DQ7NDBQ_cjs.getBaseDocumentId(String(request.documentId ?? ""));
|
|
404
404
|
const mode = String(request.mode ?? "missing");
|
|
405
405
|
const reset = request.reset === true;
|
|
406
406
|
if (!baseId) throw new Error("The request has no document id");
|
|
@@ -411,18 +411,18 @@ async function runRequest(request, context, newId) {
|
|
|
411
411
|
if (request.action === "stop" || request.action === "resume") {
|
|
412
412
|
return changeStoppedLanguages(baseId, requestedLanguages, request.action, context);
|
|
413
413
|
}
|
|
414
|
-
const stored = await
|
|
414
|
+
const stored = await chunk5DQ7NDBQ_cjs.loadState(context.store, baseId);
|
|
415
415
|
const languages = requestedLanguages.filter((language) => !stored.stopped.includes(language));
|
|
416
416
|
if (languages.length === 0) {
|
|
417
417
|
throw new Error("Translation is stopped for the requested languages. Resume them in the TextUnited panel first.");
|
|
418
418
|
}
|
|
419
|
-
const source = await
|
|
419
|
+
const source = await chunk5DQ7NDBQ_cjs.getPreferredDocument(context.store, baseId);
|
|
420
420
|
if (!source) throw new Error(`Document ${baseId} not found`);
|
|
421
|
-
const typeConfig =
|
|
421
|
+
const typeConfig = chunk5DQ7NDBQ_cjs.getDocumentTypeConfig(config, String(source._type));
|
|
422
422
|
if (!typeConfig) throw new Error(`Documents of type ${String(source._type)} are not configured for translation`);
|
|
423
|
-
const units =
|
|
424
|
-
const loaded = await
|
|
425
|
-
const baseState =
|
|
423
|
+
const units = chunk5DQ7NDBQ_cjs.extractTranslationUnits(source, typeConfig, config);
|
|
424
|
+
const loaded = await chunk5DQ7NDBQ_cjs.loadTargets({ store: context.store, source, typeConfig, languages });
|
|
425
|
+
const baseState = chunk5DQ7NDBQ_cjs.recordBaseline(stored.state, units);
|
|
426
426
|
const currentProjects = reset ? [] : stored.projects;
|
|
427
427
|
const archived = reset ? [...stored.archived, ...stored.projects] : stored.archived;
|
|
428
428
|
const groups = planGroups({
|
|
@@ -446,10 +446,10 @@ async function runRequest(request, context, newId) {
|
|
|
446
446
|
summary.textsSent = new Set(groups.flatMap((group) => group.sendUnits.map((unit) => unit.id))).size;
|
|
447
447
|
if (groups.every((group) => group.sendUnits.length === 0)) {
|
|
448
448
|
try {
|
|
449
|
-
const pending =
|
|
450
|
-
await context.store.mutate([
|
|
449
|
+
const pending = chunk5DQ7NDBQ_cjs.hasPendingTranslations(baseState, chunk5DQ7NDBQ_cjs.activeProjects(stored.projects, stored.stopped), isFinalStatus);
|
|
450
|
+
await context.store.mutate([chunk5DQ7NDBQ_cjs.saveStateMutation(baseId, { ...stored, state: baseState }, pending)]);
|
|
451
451
|
} catch (error) {
|
|
452
|
-
if (!(error instanceof
|
|
452
|
+
if (!(error instanceof chunk5DQ7NDBQ_cjs.SanityConflictError)) throw error;
|
|
453
453
|
}
|
|
454
454
|
return summary;
|
|
455
455
|
}
|
|
@@ -474,7 +474,7 @@ async function runRequest(request, context, newId) {
|
|
|
474
474
|
if (index >= 0) projects[index] = result.project;
|
|
475
475
|
else projects.push(result.project);
|
|
476
476
|
for (const sent of result.sent) {
|
|
477
|
-
sentState =
|
|
477
|
+
sentState = chunk5DQ7NDBQ_cjs.recordSentUnits(sentState, sent.units, sentAt, sent.taskId, result.project.customId);
|
|
478
478
|
}
|
|
479
479
|
}
|
|
480
480
|
const selected = /* @__PURE__ */ new Map();
|
|
@@ -506,7 +506,7 @@ async function runRequest(request, context, newId) {
|
|
|
506
506
|
}
|
|
507
507
|
function planGroups(args) {
|
|
508
508
|
const groups = [];
|
|
509
|
-
const plan = (languages, projectCustomId, mode) =>
|
|
509
|
+
const plan = (languages, projectCustomId, mode) => chunk5DQ7NDBQ_cjs.planTranslation({ units: args.units, languages, targets: args.targets, state: args.state, mode, projectCustomId });
|
|
510
510
|
const unitsToSend = (sendPlan) => {
|
|
511
511
|
if (args.append) return sendPlan.selected.map(({ unit }) => unit);
|
|
512
512
|
const taskIds = new Set(sendPlan.tasks.map((task) => task.taskId));
|
|
@@ -529,7 +529,7 @@ function planGroups(args) {
|
|
|
529
529
|
return groups;
|
|
530
530
|
}
|
|
531
531
|
function projectCustomIdFor(context, baseId, existingCount) {
|
|
532
|
-
const customId =
|
|
532
|
+
const customId = chunk5DQ7NDBQ_cjs.createProjectCustomId({
|
|
533
533
|
sanityProjectId: context.sanityProjectId,
|
|
534
534
|
dataset: context.dataset,
|
|
535
535
|
documentId: baseId
|
|
@@ -628,19 +628,19 @@ async function sendToProject(args) {
|
|
|
628
628
|
}
|
|
629
629
|
async function changeStoppedLanguages(baseId, languages, action, context) {
|
|
630
630
|
for (let attempt = 1; ; attempt += 1) {
|
|
631
|
-
const stored = await
|
|
631
|
+
const stored = await chunk5DQ7NDBQ_cjs.loadState(context.store, baseId);
|
|
632
632
|
const stopped = action === "stop" ? [.../* @__PURE__ */ new Set([...stored.stopped, ...languages])] : stored.stopped.filter((language) => !languages.includes(language));
|
|
633
633
|
try {
|
|
634
634
|
await context.store.mutate([
|
|
635
|
-
|
|
635
|
+
chunk5DQ7NDBQ_cjs.saveStateMutation(
|
|
636
636
|
baseId,
|
|
637
637
|
{ ...stored, stopped },
|
|
638
|
-
|
|
638
|
+
chunk5DQ7NDBQ_cjs.hasPendingTranslations(stored.state, chunk5DQ7NDBQ_cjs.activeProjects(stored.projects, stopped), isFinalStatus)
|
|
639
639
|
)
|
|
640
640
|
]);
|
|
641
641
|
return { tasksSent: [], unitsSelected: 0, textsSent: 0, outcomes: {}, projectCreated: false, action, languages };
|
|
642
642
|
} catch (error) {
|
|
643
|
-
if (!(error instanceof
|
|
643
|
+
if (!(error instanceof chunk5DQ7NDBQ_cjs.SanityConflictError) || attempt >= 3) throw error;
|
|
644
644
|
}
|
|
645
645
|
}
|
|
646
646
|
}
|
|
@@ -680,7 +680,7 @@ function assertProjectLanguages(existing, languages, codeOf) {
|
|
|
680
680
|
async function finishRequest(args) {
|
|
681
681
|
const { context, source, typeConfig, loaded, languages, units, stored, baseId, newId, summary } = args;
|
|
682
682
|
const { config } = context;
|
|
683
|
-
const createdLanguages =
|
|
683
|
+
const createdLanguages = chunk5DQ7NDBQ_cjs.createMissingTranslationDocuments({
|
|
684
684
|
targets: loaded.targets,
|
|
685
685
|
source,
|
|
686
686
|
typeConfig,
|
|
@@ -698,7 +698,7 @@ async function finishRequest(args) {
|
|
|
698
698
|
overwrite: args.overwrite
|
|
699
699
|
});
|
|
700
700
|
summary.outcomes = countOutcomes(applied.outcomes);
|
|
701
|
-
const targetMutations =
|
|
701
|
+
const targetMutations = chunk5DQ7NDBQ_cjs.writeTargetsMutations({
|
|
702
702
|
loaded,
|
|
703
703
|
targets: applied.targets,
|
|
704
704
|
source,
|
|
@@ -711,20 +711,20 @@ async function finishRequest(args) {
|
|
|
711
711
|
const touchedUnits = units.map((unit) => unit.id);
|
|
712
712
|
let current = stored;
|
|
713
713
|
for (let attempt = 1; ; attempt += 1) {
|
|
714
|
-
const state =
|
|
714
|
+
const state = chunk5DQ7NDBQ_cjs.mergeState(current.state, applied.state, touchedUnits);
|
|
715
715
|
try {
|
|
716
716
|
await context.store.mutate([
|
|
717
717
|
...targetMutations,
|
|
718
|
-
|
|
718
|
+
chunk5DQ7NDBQ_cjs.saveStateMutation(
|
|
719
719
|
baseId,
|
|
720
720
|
{ state, projects: args.projects, archived: args.archived, stopped: current.stopped, revision: current.revision },
|
|
721
|
-
|
|
721
|
+
chunk5DQ7NDBQ_cjs.hasPendingTranslations(state, chunk5DQ7NDBQ_cjs.activeProjects(args.projects, current.stopped), isFinalStatus)
|
|
722
722
|
)
|
|
723
723
|
]);
|
|
724
724
|
return summary;
|
|
725
725
|
} catch (error) {
|
|
726
|
-
if (!(error instanceof
|
|
727
|
-
current = await
|
|
726
|
+
if (!(error instanceof chunk5DQ7NDBQ_cjs.SanityConflictError) || attempt >= 3) throw error;
|
|
727
|
+
current = await chunk5DQ7NDBQ_cjs.loadState(context.store, baseId);
|
|
728
728
|
}
|
|
729
729
|
}
|
|
730
730
|
}
|
|
@@ -781,17 +781,17 @@ var MAX_ATTEMPTS = 3;
|
|
|
781
781
|
async function syncDocument(baseId, context) {
|
|
782
782
|
const newId = context.newId ?? (() => crypto.randomUUID());
|
|
783
783
|
for (let attempt = 1; ; attempt += 1) {
|
|
784
|
-
const stored = await
|
|
784
|
+
const stored = await chunk5DQ7NDBQ_cjs.loadState(context.store, baseId);
|
|
785
785
|
if (stored.projects.length === 0) return { status: "ignored", reason: "No TextUnited project for this document" };
|
|
786
|
-
const projects =
|
|
787
|
-
const languages =
|
|
788
|
-
const source = await
|
|
786
|
+
const projects = chunk5DQ7NDBQ_cjs.activeProjects(stored.projects, stored.stopped);
|
|
787
|
+
const languages = chunk5DQ7NDBQ_cjs.projectLanguages(projects);
|
|
788
|
+
const source = await chunk5DQ7NDBQ_cjs.getPreferredDocument(context.store, baseId);
|
|
789
789
|
if (!source) return { status: "ignored", reason: "Document not found" };
|
|
790
|
-
const typeConfig =
|
|
790
|
+
const typeConfig = chunk5DQ7NDBQ_cjs.getDocumentTypeConfig(context.config, String(source._type));
|
|
791
791
|
if (!typeConfig) return { status: "ignored", reason: "Document type is not configured" };
|
|
792
|
-
const units =
|
|
793
|
-
const loaded = await
|
|
794
|
-
const createdLanguages =
|
|
792
|
+
const units = chunk5DQ7NDBQ_cjs.extractTranslationUnits(source, typeConfig, context.config);
|
|
793
|
+
const loaded = await chunk5DQ7NDBQ_cjs.loadTargets({ store: context.store, source, typeConfig, languages });
|
|
794
|
+
const createdLanguages = chunk5DQ7NDBQ_cjs.createMissingTranslationDocuments({
|
|
795
795
|
targets: loaded.targets,
|
|
796
796
|
source,
|
|
797
797
|
typeConfig,
|
|
@@ -804,7 +804,7 @@ async function syncDocument(baseId, context) {
|
|
|
804
804
|
const outcomes = [];
|
|
805
805
|
const changedUnits = /* @__PURE__ */ new Set();
|
|
806
806
|
for (const project of projects) {
|
|
807
|
-
const sentOf = (unit) =>
|
|
807
|
+
const sentOf = (unit) => chunk5DQ7NDBQ_cjs.sentRecord(state.units[unit.id], project.customId);
|
|
808
808
|
const taskOf = /* @__PURE__ */ new Map();
|
|
809
809
|
for (const unit of units) {
|
|
810
810
|
const taskId = sentOf(unit)?.taskId;
|
|
@@ -842,10 +842,10 @@ async function syncDocument(baseId, context) {
|
|
|
842
842
|
changedFields.set(key, all);
|
|
843
843
|
}
|
|
844
844
|
}
|
|
845
|
-
const pending =
|
|
845
|
+
const pending = chunk5DQ7NDBQ_cjs.hasPendingTranslations(state, projects, isFinalStatus);
|
|
846
846
|
try {
|
|
847
847
|
await context.store.mutate([
|
|
848
|
-
...
|
|
848
|
+
...chunk5DQ7NDBQ_cjs.writeTargetsMutations({
|
|
849
849
|
loaded,
|
|
850
850
|
targets,
|
|
851
851
|
source,
|
|
@@ -855,11 +855,11 @@ async function syncDocument(baseId, context) {
|
|
|
855
855
|
createdLanguages,
|
|
856
856
|
newId
|
|
857
857
|
}),
|
|
858
|
-
|
|
858
|
+
chunk5DQ7NDBQ_cjs.saveStateMutation(baseId, { ...stored, state }, pending)
|
|
859
859
|
]);
|
|
860
860
|
return { status: "synced", outcomes: countOutcomes(outcomes), sourceChanged: changedUnits.size, pending };
|
|
861
861
|
} catch (error) {
|
|
862
|
-
if (!(error instanceof
|
|
862
|
+
if (!(error instanceof chunk5DQ7NDBQ_cjs.SanityConflictError) || attempt >= MAX_ATTEMPTS) throw error;
|
|
863
863
|
}
|
|
864
864
|
}
|
|
865
865
|
}
|
|
@@ -910,7 +910,7 @@ function createTextUnitedRequestHandler(options) {
|
|
|
910
910
|
});
|
|
911
911
|
return languagesPromise;
|
|
912
912
|
};
|
|
913
|
-
const store = options.store ?? new
|
|
913
|
+
const store = options.store ?? new chunk5DQ7NDBQ_cjs.HttpSanityStore({
|
|
914
914
|
projectId: options.sanity.projectId,
|
|
915
915
|
dataset: options.sanity.dataset,
|
|
916
916
|
token: options.sanity.token,
|
|
@@ -1055,5 +1055,5 @@ exports.TextUnitedApiError = TextUnitedApiError;
|
|
|
1055
1055
|
exports.TextUnitedClient = TextUnitedClient;
|
|
1056
1056
|
exports.TextUnitedTimeoutError = TextUnitedTimeoutError;
|
|
1057
1057
|
exports.createTextUnitedRequestHandler = createTextUnitedRequestHandler;
|
|
1058
|
-
//# sourceMappingURL=chunk-
|
|
1059
|
-
//# sourceMappingURL=chunk-
|
|
1058
|
+
//# sourceMappingURL=chunk-S2E3ALGC.cjs.map
|
|
1059
|
+
//# sourceMappingURL=chunk-S2E3ALGC.cjs.map
|