docx-diff-editor 1.0.38 → 1.0.40

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/index.js CHANGED
@@ -1255,10 +1255,11 @@ var DocxDiffEditor = react.forwardRef(
1255
1255
  if (!editor) {
1256
1256
  return null;
1257
1257
  }
1258
- const coreProps = editor.getInternalXmlFile?.("docProps/core.xml", "json");
1259
- if (!coreProps?.elements) {
1258
+ const coreXml = editor.converter?.convertedXml?.["docProps/core.xml"];
1259
+ if (!coreXml?.elements?.[0]?.elements) {
1260
1260
  return null;
1261
1261
  }
1262
+ const elements = coreXml.elements[0].elements;
1262
1263
  const xmlToKey = {
1263
1264
  "dc:title": "title",
1264
1265
  "dc:creator": "author",
@@ -1272,7 +1273,7 @@ var DocxDiffEditor = react.forwardRef(
1272
1273
  "dcterms:modified": "modified"
1273
1274
  };
1274
1275
  const props = {};
1275
- for (const el of coreProps.elements) {
1276
+ for (const el of elements) {
1276
1277
  const key = xmlToKey[el.name];
1277
1278
  if (key) {
1278
1279
  const textValue = el.elements?.[0]?.text;
@@ -1294,6 +1295,7 @@ var DocxDiffEditor = react.forwardRef(
1294
1295
  /**
1295
1296
  * Set document core properties (partial update).
1296
1297
  * Only provided properties will be updated; others are preserved.
1298
+ * Preserves XML namespaces and structure for valid DOCX output.
1297
1299
  * Returns true on success, false on failure.
1298
1300
  */
1299
1301
  async setProperties(properties) {
@@ -1310,10 +1312,13 @@ var DocxDiffEditor = react.forwardRef(
1310
1312
  if (!editor) {
1311
1313
  return false;
1312
1314
  }
1313
- const coreProps = editor.getInternalXmlFile?.("docProps/core.xml", "json");
1314
- if (!coreProps?.elements) {
1315
+ const coreXml = editor.converter?.convertedXml?.["docProps/core.xml"];
1316
+ if (!coreXml?.elements?.[0]?.elements) {
1317
+ console.warn("[DocxDiffEditor] docProps/core.xml not found or invalid structure");
1315
1318
  return false;
1316
1319
  }
1320
+ const coreProperties = coreXml.elements[0];
1321
+ const elements = coreProperties.elements;
1317
1322
  const keyToXml = {
1318
1323
  title: "dc:title",
1319
1324
  author: "dc:creator",
@@ -1331,23 +1336,38 @@ var DocxDiffEditor = react.forwardRef(
1331
1336
  const xmlName = keyToXml[key];
1332
1337
  if (!xmlName) continue;
1333
1338
  const textValue = value instanceof Date ? value.toISOString() : String(value);
1334
- let found = false;
1335
- for (const el of coreProps.elements) {
1336
- if (el.name === xmlName) {
1337
- el.elements = [{ type: "text", text: textValue }];
1338
- found = true;
1339
- break;
1339
+ const existingProp = elements.find((el) => el.name === xmlName);
1340
+ if (existingProp) {
1341
+ if (!existingProp.elements) {
1342
+ existingProp.elements = [];
1340
1343
  }
1341
- }
1342
- if (!found) {
1343
- coreProps.elements.push({
1344
+ if (existingProp.elements[0]) {
1345
+ existingProp.elements[0].text = textValue;
1346
+ } else {
1347
+ existingProp.elements.push({ type: "text", text: textValue });
1348
+ }
1349
+ } else {
1350
+ elements.push({
1344
1351
  type: "element",
1345
1352
  name: xmlName,
1346
1353
  elements: [{ type: "text", text: textValue }]
1347
1354
  });
1348
1355
  }
1349
1356
  }
1350
- editor.updateInternalXmlFile?.("docProps/core.xml", { elements: [coreProps] });
1357
+ if (editor.converter) {
1358
+ editor.converter.documentModified = true;
1359
+ }
1360
+ if (editor.converter?.schemaToXml) {
1361
+ const serialized = editor.converter.schemaToXml(coreXml.elements[0]);
1362
+ if (!editor.options) {
1363
+ editor.options = {};
1364
+ }
1365
+ if (!editor.options.customUpdatedFiles) {
1366
+ editor.options.customUpdatedFiles = {};
1367
+ }
1368
+ editor.options.customUpdatedFiles["docProps/core.xml"] = String(serialized);
1369
+ editor.options.isCustomXmlChanged = true;
1370
+ }
1351
1371
  return true;
1352
1372
  } catch (err) {
1353
1373
  console.warn("[DocxDiffEditor] Failed to set properties:", err);