@superdoc-dev/cli 0.17.0-next.51 → 0.17.0-next.52

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.
Files changed (2) hide show
  1. package/dist/index.js +742 -450
  2. package/package.json +7 -7
package/dist/index.js CHANGED
@@ -226935,7 +226935,7 @@ var init_remark_gfm_DCND_V_3_es = __esm(() => {
226935
226935
  init_remark_gfm_BUJjZJLy_es();
226936
226936
  });
226937
226937
 
226938
- // ../../packages/superdoc/dist/chunks/src-D32RsXxf.es.js
226938
+ // ../../packages/superdoc/dist/chunks/src-VH8r3Wbu.es.js
226939
226939
  function deleteProps(obj, propOrProps) {
226940
226940
  const props = typeof propOrProps === "string" ? [propOrProps] : propOrProps;
226941
226941
  const removeNested = (target, pathParts, index2 = 0) => {
@@ -227756,6 +227756,390 @@ function createPartPublisher(editor, ydoc, options = {}) {
227756
227756
  destroy
227757
227757
  };
227758
227758
  }
227759
+ function getLocalName$12(name) {
227760
+ if (!name || typeof name !== "string")
227761
+ return "";
227762
+ const i3 = name.indexOf(":");
227763
+ return i3 >= 0 ? name.slice(i3 + 1) : name;
227764
+ }
227765
+ function findFirstElement(parent, localName$2) {
227766
+ if (!parent?.elements?.length)
227767
+ return null;
227768
+ return parent.elements.find((el) => el?.type === "element" && getLocalName$12(el.name) === localName$2) ?? null;
227769
+ }
227770
+ function findAllElements(parent, localName$2) {
227771
+ if (!parent?.elements?.length)
227772
+ return [];
227773
+ return parent.elements.filter((el) => el?.type === "element" && getLocalName$12(el.name) === localName$2);
227774
+ }
227775
+ function partNameFromIndex(index2) {
227776
+ return `customXml/item${index2}.xml`;
227777
+ }
227778
+ function propsPartNameFromIndex(index2) {
227779
+ return `customXml/itemProps${index2}.xml`;
227780
+ }
227781
+ function indexFromPartName(partName) {
227782
+ const m$1 = /^customXml\/item(\d+)\.xml$/i.exec(partName ?? "");
227783
+ return m$1 ? Number.parseInt(m$1[1], 10) : null;
227784
+ }
227785
+ function indexFromPropsPartName(propsPartName) {
227786
+ const m$1 = /^customXml\/itemProps(\d+)\.xml$/i.exec(propsPartName ?? "");
227787
+ return m$1 ? Number.parseInt(m$1[1], 10) : null;
227788
+ }
227789
+ function isCustomXmlStoragePartName(partName) {
227790
+ return indexFromPartName(partName) != null;
227791
+ }
227792
+ function listCustomXmlStoragePartNames(convertedXml) {
227793
+ if (!convertedXml || typeof convertedXml !== "object")
227794
+ return [];
227795
+ const indexes = [];
227796
+ for (const path2 of Object.keys(convertedXml)) {
227797
+ const idx = indexFromPartName(path2);
227798
+ if (idx != null)
227799
+ indexes.push(idx);
227800
+ }
227801
+ indexes.sort((a2, b$1) => a2 - b$1);
227802
+ return indexes.map(partNameFromIndex);
227803
+ }
227804
+ function findPropsPartFor(convertedXml, partName) {
227805
+ if (!convertedXml)
227806
+ return null;
227807
+ const idx = indexFromPartName(partName);
227808
+ if (idx == null)
227809
+ return null;
227810
+ const relsRoot = convertedXml[`customXml/_rels/item${idx}.xml.rels`]?.elements?.find((el) => getLocalName$12(el?.name) === "Relationships");
227811
+ if (relsRoot?.elements?.length)
227812
+ for (const rel of relsRoot.elements) {
227813
+ if (rel?.attributes?.Type !== "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps")
227814
+ continue;
227815
+ const target = rel?.attributes?.Target;
227816
+ if (typeof target !== "string" || target.length === 0)
227817
+ continue;
227818
+ const candidate = resolveOpcTargetPath(target, "customXml");
227819
+ if (candidate && convertedXml[candidate])
227820
+ return candidate;
227821
+ }
227822
+ const indexCandidate = propsPartNameFromIndex(idx);
227823
+ return convertedXml[indexCandidate] ? indexCandidate : null;
227824
+ }
227825
+ function parsePropsPart(propsDoc) {
227826
+ const root3 = propsDoc?.elements?.find((el) => el?.type === "element" && getLocalName$12(el.name) === "datastoreItem");
227827
+ if (!root3)
227828
+ return null;
227829
+ const itemId = root3.attributes?.["ds:itemID"] ?? root3.attributes?.itemID ?? null;
227830
+ const schemaRefs = findAllElements(findFirstElement(root3, "schemaRefs"), "schemaRef").map((el) => el.attributes?.["ds:uri"] ?? el.attributes?.uri ?? null).filter((uri) => typeof uri === "string" && uri.length > 0);
227831
+ return {
227832
+ itemId: typeof itemId === "string" && itemId.length > 0 ? itemId : null,
227833
+ schemaRefs
227834
+ };
227835
+ }
227836
+ function parseStoragePartRootNamespace(storageDoc) {
227837
+ const root3 = storageDoc?.elements?.find((el) => el?.type === "element");
227838
+ if (!root3)
227839
+ return null;
227840
+ const xmlns2 = root3.attributes?.xmlns;
227841
+ if (typeof xmlns2 === "string" && xmlns2.length > 0)
227842
+ return xmlns2;
227843
+ const elementName = root3.name ?? "";
227844
+ const colonIdx = elementName.indexOf(":");
227845
+ if (colonIdx > 0) {
227846
+ const prefixedAttr = `xmlns:${elementName.slice(0, colonIdx)}`;
227847
+ const prefixedValue = root3.attributes?.[prefixedAttr];
227848
+ if (typeof prefixedValue === "string" && prefixedValue.length > 0)
227849
+ return prefixedValue;
227850
+ }
227851
+ return null;
227852
+ }
227853
+ function serializeXmlDoc(xmlDoc) {
227854
+ if (!xmlDoc)
227855
+ return "";
227856
+ return import_lib4.js2xml(xmlDoc, {
227857
+ compact: false,
227858
+ spaces: 0
227859
+ });
227860
+ }
227861
+ function readCustomXmlPart(convertedXml, target) {
227862
+ if (!target || !convertedXml)
227863
+ return null;
227864
+ let partName = null;
227865
+ let itemId = null;
227866
+ if (typeof target.partName === "string" && target.partName.length > 0) {
227867
+ if (!isCustomXmlStoragePartName(target.partName))
227868
+ return null;
227869
+ partName = target.partName;
227870
+ } else if (typeof target.id === "string" && target.id.length > 0) {
227871
+ itemId = target.id;
227872
+ for (const candidatePartName of listCustomXmlStoragePartNames(convertedXml)) {
227873
+ const propsName = findPropsPartFor(convertedXml, candidatePartName);
227874
+ if (!propsName)
227875
+ continue;
227876
+ if (parsePropsPart(convertedXml[propsName])?.itemId === itemId) {
227877
+ partName = candidatePartName;
227878
+ break;
227879
+ }
227880
+ }
227881
+ if (!partName)
227882
+ return null;
227883
+ } else
227884
+ return null;
227885
+ const storageDoc = convertedXml[partName];
227886
+ if (!storageDoc)
227887
+ return null;
227888
+ const propsPartName = findPropsPartFor(convertedXml, partName);
227889
+ const props = propsPartName ? parsePropsPart(convertedXml[propsPartName]) : null;
227890
+ return {
227891
+ id: props?.itemId ?? null,
227892
+ partName,
227893
+ propsPartName: propsPartName ?? null,
227894
+ rootNamespace: parseStoragePartRootNamespace(storageDoc),
227895
+ schemaRefs: props?.schemaRefs ?? [],
227896
+ content: serializeXmlDoc(storageDoc)
227897
+ };
227898
+ }
227899
+ function listCustomXmlParts(convertedXml) {
227900
+ return listCustomXmlStoragePartNames(convertedXml).map((partName) => {
227901
+ const propsPartName = findPropsPartFor(convertedXml, partName);
227902
+ const props = propsPartName ? parsePropsPart(convertedXml[propsPartName]) : null;
227903
+ return {
227904
+ id: props?.itemId ?? null,
227905
+ partName,
227906
+ propsPartName: propsPartName ?? null,
227907
+ rootNamespace: parseStoragePartRootNamespace(convertedXml[partName]),
227908
+ schemaRefs: props?.schemaRefs ?? []
227909
+ };
227910
+ });
227911
+ }
227912
+ function nextCustomXmlItemIndex(convertedXml, converter) {
227913
+ const used = /* @__PURE__ */ new Set;
227914
+ for (const path2 of Object.keys(convertedXml ?? {})) {
227915
+ const idx = indexFromPartName(path2) ?? indexFromPropsPartName(path2);
227916
+ if (idx != null)
227917
+ used.add(idx);
227918
+ }
227919
+ let candidate = 1;
227920
+ while (used.has(candidate))
227921
+ candidate += 1;
227922
+ return candidate;
227923
+ }
227924
+ function createXmlDocument2(rootElement$1, declaration) {
227925
+ const nextDeclaration = declaration ?? DEFAULT_XML_DECLARATION;
227926
+ return {
227927
+ declaration: {
227928
+ ...nextDeclaration,
227929
+ attributes: { ...nextDeclaration.attributes }
227930
+ },
227931
+ elements: [rootElement$1]
227932
+ };
227933
+ }
227934
+ function parseContentToRootElement(content3) {
227935
+ const parsed = import_lib4.xml2js(content3, { compact: false });
227936
+ const root3 = (parsed.elements ?? []).find((el) => el?.type === "element");
227937
+ if (!root3)
227938
+ throw new Error("Custom XML content is missing a root element.");
227939
+ return {
227940
+ root: root3,
227941
+ declaration: parsed.declaration ?? null
227942
+ };
227943
+ }
227944
+ function ensureDocumentRelationshipsRoot2(convertedXml) {
227945
+ if (!convertedXml["word/_rels/document.xml.rels"])
227946
+ convertedXml["word/_rels/document.xml.rels"] = createXmlDocument2({
227947
+ type: "element",
227948
+ name: "Relationships",
227949
+ attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
227950
+ elements: []
227951
+ });
227952
+ const relsData = convertedXml["word/_rels/document.xml.rels"];
227953
+ relsData.elements ??= [];
227954
+ let relsRoot = relsData.elements.find((el) => getLocalName$12(el?.name) === "Relationships");
227955
+ if (!relsRoot) {
227956
+ relsRoot = {
227957
+ type: "element",
227958
+ name: "Relationships",
227959
+ attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
227960
+ elements: []
227961
+ };
227962
+ relsData.elements.push(relsRoot);
227963
+ }
227964
+ relsRoot.elements ??= [];
227965
+ return relsRoot;
227966
+ }
227967
+ function getNextRelationshipId2(relsRoot) {
227968
+ const used = (relsRoot?.elements ?? []).map((rel) => {
227969
+ const id2 = rel?.attributes?.Id;
227970
+ const m$1 = typeof id2 === "string" ? /^rId(\d+)$/.exec(id2) : null;
227971
+ return m$1 ? Number.parseInt(m$1[1], 10) : NaN;
227972
+ }).filter((n) => Number.isFinite(n));
227973
+ return `rId${(used.length > 0 ? Math.max(...used) : 0) + 1}`;
227974
+ }
227975
+ function buildDocumentRelTarget(partName) {
227976
+ return partName.startsWith("customXml/") ? `../${partName}` : partName;
227977
+ }
227978
+ function buildItemPropsRoot(itemId, schemaRefs) {
227979
+ return [{
227980
+ type: "element",
227981
+ name: "ds:datastoreItem",
227982
+ attributes: {
227983
+ "ds:itemID": itemId,
227984
+ "xmlns:ds": CUSTOM_XML_DATASTORE_NAMESPACE
227985
+ },
227986
+ elements: schemaRefs === undefined ? [] : [{
227987
+ type: "element",
227988
+ name: "ds:schemaRefs",
227989
+ elements: schemaRefs.map((uri) => ({
227990
+ type: "element",
227991
+ name: "ds:schemaRef",
227992
+ attributes: { "ds:uri": uri }
227993
+ }))
227994
+ }]
227995
+ }][0];
227996
+ }
227997
+ function buildItemRelsRoot(propsPartFileName) {
227998
+ return {
227999
+ type: "element",
228000
+ name: "Relationships",
228001
+ attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
228002
+ elements: [{
228003
+ type: "element",
228004
+ name: "Relationship",
228005
+ attributes: {
228006
+ Id: "rId1",
228007
+ Type: CUSTOM_XML_PROPS_RELATIONSHIP_TYPE$1,
228008
+ Target: propsPartFileName
228009
+ }
228010
+ }]
228011
+ };
228012
+ }
228013
+ function resolveTargetPartName(convertedXml, target) {
228014
+ if (!target)
228015
+ return null;
228016
+ if (typeof target.partName === "string" && target.partName.length > 0) {
228017
+ if (!isCustomXmlStoragePartName(target.partName))
228018
+ return null;
228019
+ return convertedXml[target.partName] ? target.partName : null;
228020
+ }
228021
+ if (typeof target.id === "string" && target.id.length > 0)
228022
+ for (const partName of listCustomXmlStoragePartNames(convertedXml)) {
228023
+ const propsName = findPropsPartFor(convertedXml, partName);
228024
+ if (!propsName)
228025
+ continue;
228026
+ if (parsePropsPart(convertedXml[propsName])?.itemId === target.id)
228027
+ return partName;
228028
+ }
228029
+ return null;
228030
+ }
228031
+ function createCustomXmlPart(convertedXml, { content: content3, schemaRefs }, converter) {
228032
+ const { root: root3, declaration } = parseContentToRootElement(content3);
228033
+ const index2 = nextCustomXmlItemIndex(convertedXml, converter);
228034
+ const partName = partNameFromIndex(index2);
228035
+ const propsPartName = propsPartNameFromIndex(index2);
228036
+ const itemRelsPath = `customXml/_rels/item${index2}.xml.rels`;
228037
+ const itemId = `{${v4_default().toUpperCase()}}`;
228038
+ convertedXml[partName] = createXmlDocument2(root3, declaration);
228039
+ convertedXml[propsPartName] = createXmlDocument2(buildItemPropsRoot(itemId, schemaRefs));
228040
+ convertedXml[itemRelsPath] = createXmlDocument2(buildItemRelsRoot(`itemProps${index2}.xml`));
228041
+ const relsRoot = ensureDocumentRelationshipsRoot2(convertedXml);
228042
+ relsRoot.elements.push({
228043
+ type: "element",
228044
+ name: "Relationship",
228045
+ attributes: {
228046
+ Id: getNextRelationshipId2(relsRoot),
228047
+ Type: CUSTOM_XML_DATA_RELATIONSHIP_TYPE,
228048
+ Target: buildDocumentRelTarget(partName)
228049
+ }
228050
+ });
228051
+ if (converter?.removedCustomXmlPaths instanceof Set) {
228052
+ converter.removedCustomXmlPaths.delete(partName);
228053
+ converter.removedCustomXmlPaths.delete(propsPartName);
228054
+ converter.removedCustomXmlPaths.delete(itemRelsPath);
228055
+ }
228056
+ return {
228057
+ id: itemId,
228058
+ partName,
228059
+ propsPartName
228060
+ };
228061
+ }
228062
+ function patchCustomXmlPart(convertedXml, target, { content: content3, schemaRefs }, converter) {
228063
+ const partName = resolveTargetPartName(convertedXml, target);
228064
+ if (!partName)
228065
+ return null;
228066
+ if (content3 !== undefined) {
228067
+ const { root: root3, declaration } = parseContentToRootElement(content3);
228068
+ convertedXml[partName] = createXmlDocument2(root3, convertedXml[partName]?.declaration ?? declaration);
228069
+ }
228070
+ let resolvedId = null;
228071
+ if (schemaRefs !== undefined) {
228072
+ let propsPartName = findPropsPartFor(convertedXml, partName);
228073
+ if (propsPartName)
228074
+ resolvedId = parsePropsPart(convertedXml[propsPartName])?.itemId ?? null;
228075
+ if (!propsPartName) {
228076
+ const idx = indexFromPartName(partName);
228077
+ if (idx == null)
228078
+ return null;
228079
+ propsPartName = propsPartNameFromIndex(idx);
228080
+ const itemRelsPath = `customXml/_rels/item${idx}.xml.rels`;
228081
+ resolvedId = `{${v4_default().toUpperCase()}}`;
228082
+ convertedXml[itemRelsPath] = createXmlDocument2(buildItemRelsRoot(`itemProps${idx}.xml`));
228083
+ }
228084
+ if (!resolvedId)
228085
+ resolvedId = `{${v4_default().toUpperCase()}}`;
228086
+ const existingDecl = convertedXml[propsPartName]?.declaration;
228087
+ convertedXml[propsPartName] = createXmlDocument2(buildItemPropsRoot(resolvedId, schemaRefs), existingDecl);
228088
+ } else {
228089
+ const propsPartName = findPropsPartFor(convertedXml, partName);
228090
+ if (propsPartName)
228091
+ resolvedId = parsePropsPart(convertedXml[propsPartName])?.itemId ?? null;
228092
+ }
228093
+ if (converter)
228094
+ invalidateConverterCachesForPath(converter, partName);
228095
+ return resolvedId ? {
228096
+ partName,
228097
+ id: resolvedId
228098
+ } : { partName };
228099
+ }
228100
+ function removeCustomXmlPart(convertedXml, target, converter) {
228101
+ const partName = resolveTargetPartName(convertedXml, target);
228102
+ if (!partName)
228103
+ return false;
228104
+ const index2 = indexFromPartName(partName);
228105
+ const removedPaths = [
228106
+ partName,
228107
+ findPropsPartFor(convertedXml, partName),
228108
+ index2 == null ? null : `customXml/_rels/item${index2}.xml.rels`
228109
+ ].filter((path2) => typeof path2 === "string" && path2.length > 0);
228110
+ for (const path2 of removedPaths)
228111
+ delete convertedXml[path2];
228112
+ const relsRoot = convertedXml["word/_rels/document.xml.rels"]?.elements?.find((el) => getLocalName$12(el?.name) === "Relationships");
228113
+ if (relsRoot?.elements?.length)
228114
+ relsRoot.elements = relsRoot.elements.filter((rel) => {
228115
+ if (rel?.attributes?.Type !== "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml")
228116
+ return true;
228117
+ return resolveOpcTargetPath(rel?.attributes?.Target, "word") !== partName;
228118
+ });
228119
+ if (converter) {
228120
+ if (!(converter.removedCustomXmlPaths instanceof Set))
228121
+ converter.removedCustomXmlPaths = /* @__PURE__ */ new Set;
228122
+ for (const path2 of removedPaths)
228123
+ converter.removedCustomXmlPaths.add(path2);
228124
+ invalidateConverterCachesForPath(converter, partName);
228125
+ }
228126
+ return true;
228127
+ }
228128
+ function invalidateConverterCachesForPath(converter, partName) {
228129
+ if (!converter || typeof partName !== "string")
228130
+ return;
228131
+ const biblio = converter.bibliographyPart;
228132
+ if (biblio && biblio.partPath === partName)
228133
+ converter.bibliographyPart = {
228134
+ sources: [],
228135
+ partPath: null,
228136
+ itemPropsPath: null,
228137
+ itemRelsPath: null,
228138
+ selectedStyle: null,
228139
+ styleName: null,
228140
+ version: null
228141
+ };
228142
+ }
227759
228143
  function isApplyingRemotePartChanges() {
227760
228144
  return isApplyingRemoteParts;
227761
228145
  }
@@ -227766,6 +228150,8 @@ function createPartConsumer(editor, ydoc) {
227766
228150
  if (transaction.local)
227767
228151
  return;
227768
228152
  const operations = [];
228153
+ const removedCustomXmlPaths = /* @__PURE__ */ new Set;
228154
+ const writtenCustomXmlPaths = /* @__PURE__ */ new Set;
227769
228155
  const relsData = decodeYjsToEnvelope(partsMap.get("word/_rels/document.xml.rels"))?.data ?? null;
227770
228156
  event.changes.keys.forEach((change, key2) => {
227771
228157
  if (EXCLUDED_PART_IDS.has(key2))
@@ -227774,6 +228160,8 @@ function createPartConsumer(editor, ydoc) {
227774
228160
  const sectionId = ensureHeaderFooterSectionId(partId, relsData, editor);
227775
228161
  try {
227776
228162
  if (change.action === "delete") {
228163
+ if (isCustomXmlTombstonePath(editor, key2))
228164
+ removedCustomXmlPaths.add(key2);
227777
228165
  if (hasPart(editor, partId))
227778
228166
  operations.push({
227779
228167
  editor,
@@ -227801,7 +228189,7 @@ function createPartConsumer(editor, ydoc) {
227801
228189
  operation: "mutate",
227802
228190
  source: SOURCE_COLLAB_REMOTE_PARTS,
227803
228191
  mutate: ({ part }) => {
227804
- replacePartData(part, envelope.data);
228192
+ replacePartData$1(part, envelope.data);
227805
228193
  }
227806
228194
  });
227807
228195
  else
@@ -227813,14 +228201,18 @@ function createPartConsumer(editor, ydoc) {
227813
228201
  source: SOURCE_COLLAB_REMOTE_PARTS,
227814
228202
  initial: envelope.data
227815
228203
  });
228204
+ if (isCustomXmlPartPath(key2))
228205
+ writtenCustomXmlPaths.add(key2);
227816
228206
  failedParts.delete(key2);
227817
228207
  } catch (err) {
227818
228208
  console.error(`[part-sync] Error processing remote part "${key2}":`, err);
227819
228209
  trackFailure(failedParts, key2, partsMap);
227820
228210
  }
227821
228211
  });
227822
- if (operations.length === 0)
228212
+ if (operations.length === 0) {
228213
+ applyCustomXmlTombstoneChanges(editor, removedCustomXmlPaths, writtenCustomXmlPaths);
227823
228214
  return;
228215
+ }
227824
228216
  isApplyingRemoteParts = true;
227825
228217
  try {
227826
228218
  mutateParts({
@@ -227828,6 +228220,7 @@ function createPartConsumer(editor, ydoc) {
227828
228220
  source: SOURCE_COLLAB_REMOTE_PARTS,
227829
228221
  operations
227830
228222
  });
228223
+ applyCustomXmlTombstoneChanges(editor, removedCustomXmlPaths, writtenCustomXmlPaths);
227831
228224
  } catch (err) {
227832
228225
  console.error("[part-sync] Failed to apply remote part changes:", err);
227833
228226
  } finally {
@@ -227840,7 +228233,7 @@ function createPartConsumer(editor, ydoc) {
227840
228233
  failedParts.clear();
227841
228234
  } };
227842
228235
  }
227843
- function replacePartData(target, source) {
228236
+ function replacePartData$1(target, source) {
227844
228237
  if (!target || typeof target !== "object" || !source || typeof source !== "object")
227845
228238
  return;
227846
228239
  const tgt = target;
@@ -227858,6 +228251,96 @@ function ensureHeaderFooterSectionId(partId, relsData, editor) {
227858
228251
  ensureHeaderFooterDescriptor(partId, sectionId);
227859
228252
  return sectionId;
227860
228253
  }
228254
+ function getCustomXmlTombstoneConverter(editor) {
228255
+ return editor.converter;
228256
+ }
228257
+ function isCustomXmlPartPath(path2) {
228258
+ return /^customxml\//i.test(path2);
228259
+ }
228260
+ function isCustomXmlTombstonePath(editor, path2) {
228261
+ return /^customXml\/item\d+\.xml$/i.test(path2) || /^customXml\/itemProps\d+\.xml$/i.test(path2) || /^customXml\/_rels\/item\d+\.xml\.rels$/i.test(path2) || isLinkedCustomXmlPropsPath(editor, path2);
228262
+ }
228263
+ function isLinkedCustomXmlPropsPath(editor, path2) {
228264
+ if (!isCustomXmlPartPath(path2) || !/\.xml$/i.test(path2))
228265
+ return false;
228266
+ const convertedXml = getCustomXmlTombstoneConverter(editor)?.convertedXml;
228267
+ if (!convertedXml)
228268
+ return false;
228269
+ for (const [relsPath, relsDoc] of Object.entries(convertedXml)) {
228270
+ if (!/^customXml\/_rels\/item\d+\.xml\.rels$/i.test(relsPath))
228271
+ continue;
228272
+ for (const target of getCustomXmlPropsTargets(relsDoc))
228273
+ if (target === path2)
228274
+ return true;
228275
+ }
228276
+ return false;
228277
+ }
228278
+ function getCustomXmlPropsTargets(relsDoc) {
228279
+ const relationshipsRoot = getElements(relsDoc).find((element3) => getLocalName2(getName(element3)) === "Relationships");
228280
+ if (!relationshipsRoot)
228281
+ return [];
228282
+ return getElements(relationshipsRoot).map((relationship) => {
228283
+ const attributes = getAttributes(relationship);
228284
+ if (attributes.Type !== CUSTOM_XML_PROPS_RELATIONSHIP_TYPE2)
228285
+ return null;
228286
+ return resolveCustomXmlRelationshipTarget(attributes.Target);
228287
+ }).filter((target) => typeof target === "string");
228288
+ }
228289
+ function getElements(value) {
228290
+ if (!value || typeof value !== "object")
228291
+ return [];
228292
+ const elements = value.elements;
228293
+ return Array.isArray(elements) ? elements : [];
228294
+ }
228295
+ function getName(value) {
228296
+ if (!value || typeof value !== "object")
228297
+ return "";
228298
+ const name = value.name;
228299
+ return typeof name === "string" ? name : "";
228300
+ }
228301
+ function getAttributes(value) {
228302
+ if (!value || typeof value !== "object")
228303
+ return {};
228304
+ const attributes = value.attributes;
228305
+ return attributes && typeof attributes === "object" && !Array.isArray(attributes) ? attributes : {};
228306
+ }
228307
+ function getLocalName2(name) {
228308
+ const separatorIndex = name.indexOf(":");
228309
+ return separatorIndex >= 0 ? name.slice(separatorIndex + 1) : name;
228310
+ }
228311
+ function resolveCustomXmlRelationshipTarget(target) {
228312
+ if (typeof target !== "string" || target.length === 0 || target.includes("://"))
228313
+ return null;
228314
+ if (target.startsWith("/"))
228315
+ return target.slice(1);
228316
+ const resolved = [];
228317
+ for (const segment of `customXml/${target}`.split("/"))
228318
+ if (segment === "..")
228319
+ resolved.pop();
228320
+ else if (segment !== "." && segment !== "")
228321
+ resolved.push(segment);
228322
+ return resolved.join("/");
228323
+ }
228324
+ function applyCustomXmlTombstoneChanges(editor, removedPaths, writtenPaths) {
228325
+ const converter = getCustomXmlTombstoneConverter(editor);
228326
+ if (!converter)
228327
+ return;
228328
+ for (const path2 of writtenPaths) {
228329
+ if (converter.removedCustomXmlPaths instanceof Set)
228330
+ converter.removedCustomXmlPaths.delete(path2);
228331
+ invalidateConverterCachesForPath(converter, path2);
228332
+ }
228333
+ if (removedPaths.size === 0)
228334
+ return;
228335
+ for (const path2 of removedPaths)
228336
+ recordCustomXmlTombstone(converter, path2);
228337
+ }
228338
+ function recordCustomXmlTombstone(converter, path2) {
228339
+ if (!(converter.removedCustomXmlPaths instanceof Set))
228340
+ converter.removedCustomXmlPaths = /* @__PURE__ */ new Set;
228341
+ converter.removedCustomXmlPaths.add(path2);
228342
+ invalidateConverterCachesForPath(converter, path2);
228343
+ }
227861
228344
  function trackFailure(failedParts, key2, partsMap) {
227862
228345
  const envelope = decodeYjsToEnvelope(partsMap.get(key2));
227863
228346
  if (envelope)
@@ -227974,7 +228457,7 @@ function updateMigrationStatus(metaMap, status, error3, ydoc) {
227974
228457
  };
227975
228458
  metaMap.set(META_PARTS_MIGRATION_KEY, updated);
227976
228459
  }
227977
- function getConverter$92(editor) {
228460
+ function getConverter$102(editor) {
227978
228461
  return editor.converter;
227979
228462
  }
227980
228463
  function bucketFor(type) {
@@ -228025,7 +228508,7 @@ function publishSessionManagedNoteIds(ydoc, registry2) {
228025
228508
  publishNoteTombstone(ydoc, "endnote", noteId);
228026
228509
  }
228027
228510
  function hydrateNoteTombstonesFromMeta(editor, ydoc) {
228028
- const converter = getConverter$92(editor);
228511
+ const converter = getConverter$102(editor);
228029
228512
  if (!converter)
228030
228513
  return;
228031
228514
  const metaMap = ydoc.getMap(META_MAP_KEY$1);
@@ -228051,7 +228534,7 @@ function registerNoteTombstoneSync(editor, ydoc) {
228051
228534
  };
228052
228535
  editor.on?.(NOTE_TOMBSTONE_EVENT, onTombstoned);
228053
228536
  const observer = (event) => {
228054
- const converter = getConverter$92(editor);
228537
+ const converter = getConverter$102(editor);
228055
228538
  if (!converter)
228056
228539
  return;
228057
228540
  let registry2 = null;
@@ -228207,9 +228690,12 @@ function hydrateFromPartsMap(editor, ydoc, partsMap) {
228207
228690
  const operations = [];
228208
228691
  const criticalFailures = [];
228209
228692
  const relsData = decodeYjsToEnvelope(partsMap.get("word/_rels/document.xml.rels"))?.data ?? null;
228693
+ const presentKeys = /* @__PURE__ */ new Set;
228694
+ const hydratedCustomXmlPaths = /* @__PURE__ */ new Set;
228210
228695
  for (const [key2, value] of partsMap.entries()) {
228211
228696
  if (EXCLUDED_PART_IDS.has(key2))
228212
228697
  continue;
228698
+ presentKeys.add(key2);
228213
228699
  const partId = key2;
228214
228700
  const sectionId = isHeaderFooterPartId(key2) ? resolveHeaderFooterRId(key2, relsData, editor) ?? key2 : undefined;
228215
228701
  try {
@@ -228234,6 +228720,8 @@ function hydrateFromPartsMap(editor, ydoc, partsMap) {
228234
228720
  source: SOURCE_COLLAB_REMOTE_PARTS,
228235
228721
  initial: envelope.data
228236
228722
  });
228723
+ if (isCustomXmlPartPath(key2))
228724
+ hydratedCustomXmlPaths.add(key2);
228237
228725
  } catch (err) {
228238
228726
  const errorMsg = err instanceof Error ? err.message : String(err);
228239
228727
  if (CRITICAL_PART_IDS.has(key2))
@@ -228249,17 +228737,21 @@ function hydrateFromPartsMap(editor, ydoc, partsMap) {
228249
228737
  failures: criticalFailures
228250
228738
  };
228251
228739
  }
228252
- if (operations.length === 0)
228740
+ const customXmlPathsToTombstone = pruneAbsentCustomXmlParts(editor, presentKeys, operations);
228741
+ if (operations.length === 0) {
228742
+ applyCustomXmlHydrationTombstoneChanges(editor, customXmlPathsToTombstone, hydratedCustomXmlPaths);
228253
228743
  return {
228254
228744
  ok: true,
228255
228745
  failures: []
228256
228746
  };
228747
+ }
228257
228748
  try {
228258
228749
  mutateParts({
228259
228750
  editor,
228260
228751
  source: SOURCE_COLLAB_REMOTE_PARTS,
228261
228752
  operations
228262
228753
  });
228754
+ applyCustomXmlHydrationTombstoneChanges(editor, customXmlPathsToTombstone, hydratedCustomXmlPaths);
228263
228755
  return {
228264
228756
  ok: true,
228265
228757
  failures: []
@@ -228273,6 +228765,48 @@ function hydrateFromPartsMap(editor, ydoc, partsMap) {
228273
228765
  };
228274
228766
  }
228275
228767
  }
228768
+ function pruneAbsentCustomXmlParts(editor, presentKeys, operations) {
228769
+ const pathsToTombstone = /* @__PURE__ */ new Set;
228770
+ if (presentKeys.size === 0)
228771
+ return pathsToTombstone;
228772
+ const converter = getCustomXmlTombstoneConverter(editor);
228773
+ const convertedXml = converter?.convertedXml;
228774
+ if (!converter || !convertedXml)
228775
+ return pathsToTombstone;
228776
+ for (const key2 of Object.keys(convertedXml)) {
228777
+ if (EXCLUDED_PART_IDS.has(key2))
228778
+ continue;
228779
+ if (presentKeys.has(key2))
228780
+ continue;
228781
+ if (!isCustomXmlTombstonePath(editor, key2))
228782
+ continue;
228783
+ pathsToTombstone.add(key2);
228784
+ if (hasPart(editor, key2))
228785
+ operations.push({
228786
+ editor,
228787
+ partId: key2,
228788
+ operation: "delete",
228789
+ source: SOURCE_COLLAB_REMOTE_PARTS
228790
+ });
228791
+ }
228792
+ return pathsToTombstone;
228793
+ }
228794
+ function applyCustomXmlHydrationTombstoneChanges(editor, pathsToTombstone, pathsToClear) {
228795
+ if (pathsToTombstone.size === 0 && pathsToClear.size === 0)
228796
+ return;
228797
+ const converter = getCustomXmlTombstoneConverter(editor);
228798
+ if (!converter)
228799
+ return;
228800
+ for (const path2 of pathsToClear) {
228801
+ if (!converter.convertedXml || !Object.prototype.hasOwnProperty.call(converter.convertedXml, path2))
228802
+ continue;
228803
+ if (converter.removedCustomXmlPaths instanceof Set)
228804
+ converter.removedCustomXmlPaths.delete(path2);
228805
+ invalidateConverterCachesForPath(converter, path2);
228806
+ }
228807
+ for (const path2 of pathsToTombstone)
228808
+ recordCustomXmlTombstone(converter, path2);
228809
+ }
228276
228810
  function createNoopHandle() {
228277
228811
  return {
228278
228812
  publisher: null,
@@ -228301,7 +228835,7 @@ function backfillCapability(metaMap, ydoc) {
228301
228835
  metaMap.set(META_PARTS_CAPABILITY_KEY, capability);
228302
228836
  }
228303
228837
  function createReplacer(data) {
228304
- return ({ part }) => replacePartData(part, data);
228838
+ return ({ part }) => replacePartData$1(part, data);
228305
228839
  }
228306
228840
  function normalizeYjsFragmentForSchema(fragment2) {
228307
228841
  if (!isTraversableYjsXml(fragment2))
@@ -229064,11 +229598,11 @@ function syncSplitParagraphRunProperties(attrs, runProperties) {
229064
229598
  paragraphProperties: nextParagraphProperties
229065
229599
  };
229066
229600
  }
229067
- function getConverter$82(editor) {
229601
+ function getConverter$92(editor) {
229068
229602
  return editor.converter;
229069
229603
  }
229070
229604
  function readTranslatedLinkedStyles(editor) {
229071
- return getConverter$82(editor)?.translatedLinkedStyles ?? null;
229605
+ return getConverter$92(editor)?.translatedLinkedStyles ?? null;
229072
229606
  }
229073
229607
  function findRangeById(doc$12, id2) {
229074
229608
  let from$1 = null, to = null;
@@ -233730,18 +234264,18 @@ function readStringAttr2(node3, key2) {
233730
234264
  function attrsEqual(left$1, right$1) {
233731
234265
  const keys$1 = new Set([...Object.keys(left$1), ...Object.keys(right$1)]);
233732
234266
  for (const key2 of keys$1)
233733
- if (!valuesEqual(left$1[key2], right$1[key2]))
234267
+ if (!valuesEqual$1(left$1[key2], right$1[key2]))
233734
234268
  return false;
233735
234269
  return true;
233736
234270
  }
233737
- function valuesEqual(left$1, right$1) {
234271
+ function valuesEqual$1(left$1, right$1) {
233738
234272
  if (Object.is(left$1, right$1))
233739
234273
  return true;
233740
234274
  if (!isRecord$3(left$1) || !isRecord$3(right$1))
233741
234275
  return false;
233742
234276
  const keys$1 = new Set([...Object.keys(left$1), ...Object.keys(right$1)]);
233743
234277
  for (const key2 of keys$1)
233744
- if (!valuesEqual(left$1[key2], right$1[key2]))
234278
+ if (!valuesEqual$1(left$1[key2], right$1[key2]))
233745
234279
  return false;
233746
234280
  return true;
233747
234281
  }
@@ -239161,7 +239695,7 @@ function syncStylesDiffToConvertedXml(converter, stylesDiff) {
239161
239695
  }
239162
239696
  return true;
239163
239697
  }
239164
- function cloneValue2(value) {
239698
+ function cloneValue$1(value) {
239165
239699
  if (value === null || typeof value !== "object")
239166
239700
  return value;
239167
239701
  return structuredClone(value);
@@ -239185,7 +239719,7 @@ function setByPath(target, path2, value) {
239185
239719
  return;
239186
239720
  const leaf = segments.pop();
239187
239721
  const parent = ensureParentObject(target, segments);
239188
- parent[leaf] = cloneValue2(value);
239722
+ parent[leaf] = cloneValue$1(value);
239189
239723
  }
239190
239724
  function pruneEmptyParents(root3, segments) {
239191
239725
  for (let idx = segments.length - 1;idx >= 0; idx -= 1) {
@@ -244412,7 +244946,7 @@ function makeTrackedChangeAnchorKey(ref$1) {
244412
244946
  function makeCommentAnchorKey(commentId) {
244413
244947
  return `${COMMENT_ANCHOR_KEY_PREFIX}${commentId}`;
244414
244948
  }
244415
- function getConverter$72(editor) {
244949
+ function getConverter$82(editor) {
244416
244950
  return editor.converter;
244417
244951
  }
244418
244952
  function toRevisionCapableNoteId(note) {
@@ -244429,7 +244963,7 @@ function enumerateRevisionCapableStories(editor) {
244429
244963
  kind: "story",
244430
244964
  storyType: "body"
244431
244965
  }];
244432
- const converter = getConverter$72(editor);
244966
+ const converter = getConverter$82(editor);
244433
244967
  if (!converter)
244434
244968
  return stories;
244435
244969
  if (converter.headers)
@@ -255949,10 +256483,10 @@ function registerBuiltInExecutors() {
255949
256483
  };
255950
256484
  } });
255951
256485
  }
255952
- function getConverter$62(editor) {
256486
+ function getConverter$72(editor) {
255953
256487
  return editor.converter;
255954
256488
  }
255955
- function getConverter$52(editor) {
256489
+ function getConverter$62(editor) {
255956
256490
  return editor.converter;
255957
256491
  }
255958
256492
  function createEmptyHeaderFooterJson() {
@@ -255965,7 +256499,7 @@ function createEmptyHeaderFooterJson() {
255965
256499
  };
255966
256500
  }
255967
256501
  function syncHeaderFooterCaches(editor, part) {
255968
- const converter = getConverter$52(editor);
256502
+ const converter = getConverter$62(editor);
255969
256503
  if (!converter)
255970
256504
  return;
255971
256505
  const relsRoot = getRelationshipsRoot(part);
@@ -256027,7 +256561,7 @@ function createTableWrapper(editor, input2, options) {
256027
256561
  });
256028
256562
  return adapterResult;
256029
256563
  }
256030
- function getConverter$42(editor) {
256564
+ function getConverter$52(editor) {
256031
256565
  return editor.converter;
256032
256566
  }
256033
256567
  function toSectionFailure2(code6, message) {
@@ -256095,7 +256629,7 @@ function buildSectionMarginsForAttrs2(sectPr) {
256095
256629
  };
256096
256630
  }
256097
256631
  function syncConverterBodySection2(editor, sectPr) {
256098
- const converter = getConverter$42(editor);
256632
+ const converter = getConverter$52(editor);
256099
256633
  if (!converter)
256100
256634
  return;
256101
256635
  converter.bodySectPr = cloneXmlElement(sectPr);
@@ -256215,7 +256749,7 @@ function createSectionBreakNode(editor, breakParagraphId, input2) {
256215
256749
  return paragraphNode;
256216
256750
  }
256217
256751
  function updateGlobalTitlePageFlag(editor) {
256218
- const converter = getConverter$42(editor);
256752
+ const converter = getConverter$52(editor);
256219
256753
  if (!converter)
256220
256754
  return;
256221
256755
  const anyTitlePage = resolveSectionProjections(editor).some((entry) => entry.domain.titlePage === true);
@@ -256299,7 +256833,7 @@ function sectionsSetTitlePageAdapter(editor, input2, options) {
256299
256833
  }
256300
256834
  function sectionsSetOddEvenHeadersFootersAdapter(editor, input2, options) {
256301
256835
  rejectTrackedMode("sections.setOddEvenHeadersFooters", options);
256302
- const converter = getConverter$42(editor);
256836
+ const converter = getConverter$52(editor);
256303
256837
  if (!converter)
256304
256838
  throw new DocumentApiAdapterError("CAPABILITY_UNAVAILABLE", "sections.setOddEvenHeadersFooters requires an active document converter.");
256305
256839
  return mutatePart({
@@ -256335,13 +256869,13 @@ function sectionsSetSectionDirectionAdapter(editor, input2, options) {
256335
256869
  }
256336
256870
  function sectionsSetHeaderFooterRefAdapter(editor, input2, options) {
256337
256871
  return sectionMutationBySectPr$1(editor, input2, options, "sections.setHeaderFooterRef", (sectPr, _projection, _sections, dryRun) => {
256338
- const converter = getConverter$42(editor) ?? null;
256872
+ const converter = getConverter$52(editor) ?? null;
256339
256873
  return setHeaderFooterRefMutation(sectPr, input2.kind, input2.variant, input2.refId, converter, "sections.setHeaderFooterRef", dryRun);
256340
256874
  });
256341
256875
  }
256342
256876
  function sectionsClearHeaderFooterRefAdapter(editor, input2, options) {
256343
256877
  return sectionMutationBySectPr$1(editor, input2, options, "sections.clearHeaderFooterRef", (sectPr, _projection, _sections, dryRun) => {
256344
- const converter = getConverter$42(editor) ?? null;
256878
+ const converter = getConverter$52(editor) ?? null;
256345
256879
  clearHeaderFooterRefMutation(sectPr, input2.kind, input2.variant, converter, dryRun);
256346
256880
  });
256347
256881
  }
@@ -260190,11 +260724,11 @@ function createContentControlsAdapter(editor) {
260190
260724
  create: (input2, options) => createWrapper(editor, input2, options)
260191
260725
  };
260192
260726
  }
260193
- function getConverter$32(editor) {
260727
+ function getConverter$42(editor) {
260194
260728
  return editor.converter;
260195
260729
  }
260196
260730
  function requireConverter$1(editor, operationName) {
260197
- const converter = getConverter$32(editor);
260731
+ const converter = getConverter$42(editor);
260198
260732
  if (!converter)
260199
260733
  throw new DocumentApiAdapterError("CAPABILITY_UNAVAILABLE", `${operationName} requires an active document converter.`);
260200
260734
  return converter;
@@ -260300,7 +260834,7 @@ function headerFootersResolveAdapter(editor, input2) {
260300
260834
  function headerFootersRefsSetAdapter(editor, input2, options) {
260301
260835
  const { section, headerFooterKind, variant } = input2.target;
260302
260836
  const result = sectionMutationBySectPr(editor, { target: section }, options, "headerFooters.refs.set", (sectPr, _projection, _sections, dryRun) => {
260303
- const converter = getConverter$32(editor) ?? null;
260837
+ const converter = getConverter$42(editor) ?? null;
260304
260838
  return setHeaderFooterRefMutation(sectPr, headerFooterKind, variant, input2.refId, converter, "headerFooters.refs.set", dryRun);
260305
260839
  });
260306
260840
  invalidateSlotRuntimesAfterRefChange(editor, result, options);
@@ -260309,7 +260843,7 @@ function headerFootersRefsSetAdapter(editor, input2, options) {
260309
260843
  function headerFootersRefsClearAdapter(editor, input2, options) {
260310
260844
  const { section, headerFooterKind, variant } = input2.target;
260311
260845
  const result = sectionMutationBySectPr(editor, { target: section }, options, "headerFooters.refs.clear", (sectPr, _projection, _sections, dryRun) => {
260312
- clearHeaderFooterRefMutation(sectPr, headerFooterKind, variant, getConverter$32(editor) ?? null, dryRun);
260846
+ clearHeaderFooterRefMutation(sectPr, headerFooterKind, variant, getConverter$42(editor) ?? null, dryRun);
260313
260847
  });
260314
260848
  invalidateSlotRuntimesAfterRefChange(editor, result, options);
260315
260849
  return result;
@@ -261130,389 +261664,136 @@ function executeOutOfBandMutation(editor, mutateFn, options) {
261130
261664
  }
261131
261665
  return result.payload;
261132
261666
  }
261133
- function getLocalName2(name) {
261134
- if (!name || typeof name !== "string")
261135
- return "";
261136
- const i3 = name.indexOf(":");
261137
- return i3 >= 0 ? name.slice(i3 + 1) : name;
261138
- }
261139
- function findFirstElement(parent, localName$2) {
261140
- if (!parent?.elements?.length)
261141
- return null;
261142
- return parent.elements.find((el) => el?.type === "element" && getLocalName2(el.name) === localName$2) ?? null;
261143
- }
261144
- function findAllElements(parent, localName$2) {
261145
- if (!parent?.elements?.length)
261146
- return [];
261147
- return parent.elements.filter((el) => el?.type === "element" && getLocalName2(el.name) === localName$2);
261148
- }
261149
- function partNameFromIndex(index2) {
261150
- return `customXml/item${index2}.xml`;
261151
- }
261152
- function propsPartNameFromIndex(index2) {
261153
- return `customXml/itemProps${index2}.xml`;
261667
+ function getConverter$32(editor) {
261668
+ const converter = editor.converter;
261669
+ if (!converter?.convertedXml)
261670
+ throw new Error("Custom XML part mutation requires editor.converter.convertedXml.");
261671
+ return converter;
261154
261672
  }
261155
- function indexFromPartName(partName) {
261156
- const m$1 = /^customXml\/item(\d+)\.xml$/i.exec(partName ?? "");
261157
- return m$1 ? Number.parseInt(m$1[1], 10) : null;
261673
+ function cloneValue2(value) {
261674
+ return structuredClone(value);
261158
261675
  }
261159
- function indexFromPropsPartName(propsPartName) {
261160
- const m$1 = /^customXml\/itemProps(\d+)\.xml$/i.exec(propsPartName ?? "");
261161
- return m$1 ? Number.parseInt(m$1[1], 10) : null;
261676
+ function hasOwn(record, key2) {
261677
+ return Object.prototype.hasOwnProperty.call(record, key2);
261162
261678
  }
261163
- function isCustomXmlStoragePartName(partName) {
261164
- return indexFromPartName(partName) != null;
261679
+ function valuesEqual(left$1, right$1) {
261680
+ return JSON.stringify(left$1) === JSON.stringify(right$1);
261165
261681
  }
261166
- function listCustomXmlStoragePartNames(convertedXml) {
261167
- if (!convertedXml || typeof convertedXml !== "object")
261168
- return [];
261169
- const indexes = [];
261170
- for (const path2 of Object.keys(convertedXml)) {
261171
- const idx = indexFromPartName(path2);
261172
- if (idx != null)
261173
- indexes.push(idx);
261174
- }
261175
- indexes.sort((a2, b$1) => a2 - b$1);
261176
- return indexes.map(partNameFromIndex);
261682
+ function toPartId(path2) {
261683
+ return path2;
261177
261684
  }
261178
- function findPropsPartFor(convertedXml, partName) {
261179
- if (!convertedXml)
261180
- return null;
261181
- const idx = indexFromPartName(partName);
261182
- if (idx == null)
261183
- return null;
261184
- const relsRoot = convertedXml[`customXml/_rels/item${idx}.xml.rels`]?.elements?.find((el) => getLocalName2(el?.name) === "Relationships");
261185
- if (relsRoot?.elements?.length)
261186
- for (const rel of relsRoot.elements) {
261187
- if (rel?.attributes?.Type !== "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps")
261188
- continue;
261189
- const target = rel?.attributes?.Target;
261190
- if (typeof target !== "string" || target.length === 0)
261191
- continue;
261192
- const candidate = resolveOpcTargetPath(target, "customXml");
261193
- if (candidate && convertedXml[candidate])
261194
- return candidate;
261685
+ function replacePartData(target, source) {
261686
+ if (!target || typeof target !== "object" || !source || typeof source !== "object")
261687
+ throw new Error("Custom XML part mutation can only replace object-shaped XML parts.");
261688
+ const targetRecord = target;
261689
+ const sourceRecord = source;
261690
+ for (const key2 of Object.keys(targetRecord))
261691
+ if (!hasOwn(sourceRecord, key2))
261692
+ delete targetRecord[key2];
261693
+ for (const [key2, value] of Object.entries(sourceRecord))
261694
+ targetRecord[key2] = cloneValue2(value);
261695
+ }
261696
+ function createSandboxConverter(converter) {
261697
+ const sandbox = { convertedXml: cloneValue2(converter.convertedXml ?? {}) };
261698
+ if (converter.removedCustomXmlPaths instanceof Set)
261699
+ sandbox.removedCustomXmlPaths = new Set(converter.removedCustomXmlPaths);
261700
+ if (converter.bibliographyPart !== undefined)
261701
+ sandbox.bibliographyPart = cloneValue2(converter.bibliographyPart);
261702
+ return sandbox;
261703
+ }
261704
+ function createDeltaOperations(editor, source, before2, after2) {
261705
+ const paths = new Set([...Object.keys(before2), ...Object.keys(after2)]);
261706
+ const operations = [];
261707
+ for (const path2 of paths) {
261708
+ const existedBefore = hasOwn(before2, path2);
261709
+ const existsAfter = hasOwn(after2, path2);
261710
+ const partId = toPartId(path2);
261711
+ if (!existedBefore && existsAfter) {
261712
+ operations.push({
261713
+ editor,
261714
+ partId,
261715
+ operation: "create",
261716
+ source,
261717
+ initial: cloneValue2(after2[path2])
261718
+ });
261719
+ continue;
261195
261720
  }
261196
- const indexCandidate = propsPartNameFromIndex(idx);
261197
- return convertedXml[indexCandidate] ? indexCandidate : null;
261198
- }
261199
- function parsePropsPart(propsDoc) {
261200
- const root3 = propsDoc?.elements?.find((el) => el?.type === "element" && getLocalName2(el.name) === "datastoreItem");
261201
- if (!root3)
261202
- return null;
261203
- const itemId = root3.attributes?.["ds:itemID"] ?? root3.attributes?.itemID ?? null;
261204
- const schemaRefs = findAllElements(findFirstElement(root3, "schemaRefs"), "schemaRef").map((el) => el.attributes?.["ds:uri"] ?? el.attributes?.uri ?? null).filter((uri) => typeof uri === "string" && uri.length > 0);
261205
- return {
261206
- itemId: typeof itemId === "string" && itemId.length > 0 ? itemId : null,
261207
- schemaRefs
261208
- };
261209
- }
261210
- function parseStoragePartRootNamespace(storageDoc) {
261211
- const root3 = storageDoc?.elements?.find((el) => el?.type === "element");
261212
- if (!root3)
261213
- return null;
261214
- const xmlns2 = root3.attributes?.xmlns;
261215
- if (typeof xmlns2 === "string" && xmlns2.length > 0)
261216
- return xmlns2;
261217
- const elementName = root3.name ?? "";
261218
- const colonIdx = elementName.indexOf(":");
261219
- if (colonIdx > 0) {
261220
- const prefixedAttr = `xmlns:${elementName.slice(0, colonIdx)}`;
261221
- const prefixedValue = root3.attributes?.[prefixedAttr];
261222
- if (typeof prefixedValue === "string" && prefixedValue.length > 0)
261223
- return prefixedValue;
261224
- }
261225
- return null;
261226
- }
261227
- function serializeXmlDoc(xmlDoc) {
261228
- if (!xmlDoc)
261229
- return "";
261230
- return import_lib4.js2xml(xmlDoc, {
261231
- compact: false,
261232
- spaces: 0
261233
- });
261234
- }
261235
- function readCustomXmlPart(convertedXml, target) {
261236
- if (!target || !convertedXml)
261237
- return null;
261238
- let partName = null;
261239
- let itemId = null;
261240
- if (typeof target.partName === "string" && target.partName.length > 0) {
261241
- if (!isCustomXmlStoragePartName(target.partName))
261242
- return null;
261243
- partName = target.partName;
261244
- } else if (typeof target.id === "string" && target.id.length > 0) {
261245
- itemId = target.id;
261246
- for (const candidatePartName of listCustomXmlStoragePartNames(convertedXml)) {
261247
- const propsName = findPropsPartFor(convertedXml, candidatePartName);
261248
- if (!propsName)
261249
- continue;
261250
- if (parsePropsPart(convertedXml[propsName])?.itemId === itemId) {
261251
- partName = candidatePartName;
261252
- break;
261253
- }
261721
+ if (existedBefore && !existsAfter) {
261722
+ operations.push({
261723
+ editor,
261724
+ partId,
261725
+ operation: "delete",
261726
+ source
261727
+ });
261728
+ continue;
261729
+ }
261730
+ if (existedBefore && existsAfter && !valuesEqual(before2[path2], after2[path2])) {
261731
+ const nextPart = cloneValue2(after2[path2]);
261732
+ operations.push({
261733
+ editor,
261734
+ partId,
261735
+ operation: "mutate",
261736
+ source,
261737
+ mutate: ({ part }) => replacePartData(part, nextPart)
261738
+ });
261254
261739
  }
261255
- if (!partName)
261256
- return null;
261257
- } else
261258
- return null;
261259
- const storageDoc = convertedXml[partName];
261260
- if (!storageDoc)
261261
- return null;
261262
- const propsPartName = findPropsPartFor(convertedXml, partName);
261263
- const props = propsPartName ? parsePropsPart(convertedXml[propsPartName]) : null;
261264
- return {
261265
- id: props?.itemId ?? null,
261266
- partName,
261267
- propsPartName: propsPartName ?? null,
261268
- rootNamespace: parseStoragePartRootNamespace(storageDoc),
261269
- schemaRefs: props?.schemaRefs ?? [],
261270
- content: serializeXmlDoc(storageDoc)
261271
- };
261272
- }
261273
- function listCustomXmlParts(convertedXml) {
261274
- return listCustomXmlStoragePartNames(convertedXml).map((partName) => {
261275
- const propsPartName = findPropsPartFor(convertedXml, partName);
261276
- const props = propsPartName ? parsePropsPart(convertedXml[propsPartName]) : null;
261277
- return {
261278
- id: props?.itemId ?? null,
261279
- partName,
261280
- propsPartName: propsPartName ?? null,
261281
- rootNamespace: parseStoragePartRootNamespace(convertedXml[partName]),
261282
- schemaRefs: props?.schemaRefs ?? []
261283
- };
261284
- });
261285
- }
261286
- function nextCustomXmlItemIndex(convertedXml, converter) {
261287
- const used = /* @__PURE__ */ new Set;
261288
- for (const path2 of Object.keys(convertedXml ?? {})) {
261289
- const idx = indexFromPartName(path2) ?? indexFromPropsPartName(path2);
261290
- if (idx != null)
261291
- used.add(idx);
261292
- }
261293
- let candidate = 1;
261294
- while (used.has(candidate))
261295
- candidate += 1;
261296
- return candidate;
261297
- }
261298
- function createXmlDocument2(rootElement$1, declaration) {
261299
- const nextDeclaration = declaration ?? DEFAULT_XML_DECLARATION;
261300
- return {
261301
- declaration: {
261302
- ...nextDeclaration,
261303
- attributes: { ...nextDeclaration.attributes }
261304
- },
261305
- elements: [rootElement$1]
261306
- };
261307
- }
261308
- function parseContentToRootElement(content3) {
261309
- const parsed = import_lib4.xml2js(content3, { compact: false });
261310
- const root3 = (parsed.elements ?? []).find((el) => el?.type === "element");
261311
- if (!root3)
261312
- throw new Error("Custom XML content is missing a root element.");
261313
- return {
261314
- root: root3,
261315
- declaration: parsed.declaration ?? null
261316
- };
261317
- }
261318
- function ensureDocumentRelationshipsRoot2(convertedXml) {
261319
- if (!convertedXml["word/_rels/document.xml.rels"])
261320
- convertedXml["word/_rels/document.xml.rels"] = createXmlDocument2({
261321
- type: "element",
261322
- name: "Relationships",
261323
- attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
261324
- elements: []
261325
- });
261326
- const relsData = convertedXml["word/_rels/document.xml.rels"];
261327
- relsData.elements ??= [];
261328
- let relsRoot = relsData.elements.find((el) => getLocalName2(el?.name) === "Relationships");
261329
- if (!relsRoot) {
261330
- relsRoot = {
261331
- type: "element",
261332
- name: "Relationships",
261333
- attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
261334
- elements: []
261335
- };
261336
- relsData.elements.push(relsRoot);
261337
261740
  }
261338
- relsRoot.elements ??= [];
261339
- return relsRoot;
261340
- }
261341
- function getNextRelationshipId2(relsRoot) {
261342
- const used = (relsRoot?.elements ?? []).map((rel) => {
261343
- const id2 = rel?.attributes?.Id;
261344
- const m$1 = typeof id2 === "string" ? /^rId(\d+)$/.exec(id2) : null;
261345
- return m$1 ? Number.parseInt(m$1[1], 10) : NaN;
261346
- }).filter((n) => Number.isFinite(n));
261347
- return `rId${(used.length > 0 ? Math.max(...used) : 0) + 1}`;
261348
- }
261349
- function buildDocumentRelTarget(partName) {
261350
- return partName.startsWith("customXml/") ? `../${partName}` : partName;
261351
- }
261352
- function buildItemPropsRoot(itemId, schemaRefs) {
261353
- return [{
261354
- type: "element",
261355
- name: "ds:datastoreItem",
261356
- attributes: {
261357
- "ds:itemID": itemId,
261358
- "xmlns:ds": CUSTOM_XML_DATASTORE_NAMESPACE
261359
- },
261360
- elements: schemaRefs === undefined ? [] : [{
261361
- type: "element",
261362
- name: "ds:schemaRefs",
261363
- elements: schemaRefs.map((uri) => ({
261364
- type: "element",
261365
- name: "ds:schemaRef",
261366
- attributes: { "ds:uri": uri }
261367
- }))
261368
- }]
261369
- }][0];
261741
+ return operations;
261370
261742
  }
261371
- function buildItemRelsRoot(propsPartFileName) {
261743
+ function applyConverterState(editor, prepared) {
261744
+ const converter = getConverter$32(editor);
261745
+ const nextRemovedCustomXmlPaths = prepared.removedCustomXmlPaths ? new Set(prepared.removedCustomXmlPaths) : undefined;
261746
+ const nextBibliographyPart = prepared.hasBibliographyPart ? cloneValue2(prepared.bibliographyPart) : undefined;
261747
+ if (nextRemovedCustomXmlPaths)
261748
+ converter.removedCustomXmlPaths = nextRemovedCustomXmlPaths;
261749
+ if (prepared.hasBibliographyPart)
261750
+ converter.bibliographyPart = nextBibliographyPart;
261751
+ }
261752
+ function prepareCustomXmlPartMutation(editor, mutate, source = "customXml.parts") {
261753
+ const converter = getConverter$32(editor);
261754
+ const before2 = converter.convertedXml ?? {};
261755
+ const sandbox = createSandboxConverter(converter);
261756
+ const result = mutate(sandbox.convertedXml, sandbox);
261757
+ const operations = createDeltaOperations(editor, source, before2, sandbox.convertedXml);
261372
261758
  return {
261373
- type: "element",
261374
- name: "Relationships",
261375
- attributes: { xmlns: "http://schemas.openxmlformats.org/package/2006/relationships" },
261376
- elements: [{
261377
- type: "element",
261378
- name: "Relationship",
261379
- attributes: {
261380
- Id: "rId1",
261381
- Type: CUSTOM_XML_PROPS_RELATIONSHIP_TYPE2,
261382
- Target: propsPartFileName
261383
- }
261384
- }]
261759
+ result,
261760
+ operations,
261761
+ affectedParts: operations.map((operation) => operation.partId),
261762
+ removedCustomXmlPaths: sandbox.removedCustomXmlPaths,
261763
+ bibliographyPart: sandbox.bibliographyPart,
261764
+ hasBibliographyPart: Object.prototype.hasOwnProperty.call(sandbox, "bibliographyPart")
261385
261765
  };
261386
261766
  }
261387
- function resolveTargetPartName(convertedXml, target) {
261388
- if (!target)
261389
- return null;
261390
- if (typeof target.partName === "string" && target.partName.length > 0) {
261391
- if (!isCustomXmlStoragePartName(target.partName))
261392
- return null;
261393
- return convertedXml[target.partName] ? target.partName : null;
261767
+ function commitPreparedCustomXmlPartMutation(editor, prepared, options) {
261768
+ if (prepared.operations.length === 0) {
261769
+ checkRevision(editor, options.expectedRevision);
261770
+ applyConverterState(editor, prepared);
261771
+ return prepared.result;
261394
261772
  }
261395
- if (typeof target.id === "string" && target.id.length > 0)
261396
- for (const partName of listCustomXmlStoragePartNames(convertedXml)) {
261397
- const propsName = findPropsPartFor(convertedXml, partName);
261398
- if (!propsName)
261399
- continue;
261400
- if (parsePropsPart(convertedXml[propsName])?.itemId === target.id)
261401
- return partName;
261773
+ if (compoundMutation({
261774
+ editor,
261775
+ source: options.source,
261776
+ affectedParts: prepared.affectedParts,
261777
+ execute() {
261778
+ return mutateParts({
261779
+ editor,
261780
+ source: options.source,
261781
+ expectedRevision: options.expectedRevision,
261782
+ operations: prepared.operations.map((operation) => ({
261783
+ ...operation,
261784
+ source: options.source
261785
+ }))
261786
+ }).changed;
261402
261787
  }
261403
- return null;
261788
+ }).success)
261789
+ applyConverterState(editor, prepared);
261790
+ return prepared.result;
261404
261791
  }
261405
- function createCustomXmlPart(convertedXml, { content: content3, schemaRefs }, converter) {
261406
- const { root: root3, declaration } = parseContentToRootElement(content3);
261407
- const index2 = nextCustomXmlItemIndex(convertedXml, converter);
261408
- const partName = partNameFromIndex(index2);
261409
- const propsPartName = propsPartNameFromIndex(index2);
261410
- const itemRelsPath = `customXml/_rels/item${index2}.xml.rels`;
261411
- const itemId = `{${v4_default().toUpperCase()}}`;
261412
- convertedXml[partName] = createXmlDocument2(root3, declaration);
261413
- convertedXml[propsPartName] = createXmlDocument2(buildItemPropsRoot(itemId, schemaRefs));
261414
- convertedXml[itemRelsPath] = createXmlDocument2(buildItemRelsRoot(`itemProps${index2}.xml`));
261415
- const relsRoot = ensureDocumentRelationshipsRoot2(convertedXml);
261416
- relsRoot.elements.push({
261417
- type: "element",
261418
- name: "Relationship",
261419
- attributes: {
261420
- Id: getNextRelationshipId2(relsRoot),
261421
- Type: CUSTOM_XML_DATA_RELATIONSHIP_TYPE,
261422
- Target: buildDocumentRelTarget(partName)
261423
- }
261792
+ function mutateCustomXmlParts(editor, source, mutate, options) {
261793
+ return commitPreparedCustomXmlPartMutation(editor, prepareCustomXmlPartMutation(editor, mutate, source), {
261794
+ source,
261795
+ expectedRevision: options?.expectedRevision
261424
261796
  });
261425
- if (converter?.removedCustomXmlPaths instanceof Set) {
261426
- converter.removedCustomXmlPaths.delete(partName);
261427
- converter.removedCustomXmlPaths.delete(propsPartName);
261428
- converter.removedCustomXmlPaths.delete(itemRelsPath);
261429
- }
261430
- return {
261431
- id: itemId,
261432
- partName,
261433
- propsPartName
261434
- };
261435
- }
261436
- function patchCustomXmlPart(convertedXml, target, { content: content3, schemaRefs }, converter) {
261437
- const partName = resolveTargetPartName(convertedXml, target);
261438
- if (!partName)
261439
- return null;
261440
- if (content3 !== undefined) {
261441
- const { root: root3, declaration } = parseContentToRootElement(content3);
261442
- convertedXml[partName] = createXmlDocument2(root3, convertedXml[partName]?.declaration ?? declaration);
261443
- }
261444
- let resolvedId = null;
261445
- if (schemaRefs !== undefined) {
261446
- let propsPartName = findPropsPartFor(convertedXml, partName);
261447
- if (propsPartName)
261448
- resolvedId = parsePropsPart(convertedXml[propsPartName])?.itemId ?? null;
261449
- if (!propsPartName) {
261450
- const idx = indexFromPartName(partName);
261451
- if (idx == null)
261452
- return null;
261453
- propsPartName = propsPartNameFromIndex(idx);
261454
- const itemRelsPath = `customXml/_rels/item${idx}.xml.rels`;
261455
- resolvedId = `{${v4_default().toUpperCase()}}`;
261456
- convertedXml[itemRelsPath] = createXmlDocument2(buildItemRelsRoot(`itemProps${idx}.xml`));
261457
- }
261458
- if (!resolvedId)
261459
- resolvedId = `{${v4_default().toUpperCase()}}`;
261460
- const existingDecl = convertedXml[propsPartName]?.declaration;
261461
- convertedXml[propsPartName] = createXmlDocument2(buildItemPropsRoot(resolvedId, schemaRefs), existingDecl);
261462
- } else {
261463
- const propsPartName = findPropsPartFor(convertedXml, partName);
261464
- if (propsPartName)
261465
- resolvedId = parsePropsPart(convertedXml[propsPartName])?.itemId ?? null;
261466
- }
261467
- if (converter)
261468
- invalidateConverterCachesForPath(converter, partName);
261469
- return resolvedId ? {
261470
- partName,
261471
- id: resolvedId
261472
- } : { partName };
261473
- }
261474
- function removeCustomXmlPart(convertedXml, target, converter) {
261475
- const partName = resolveTargetPartName(convertedXml, target);
261476
- if (!partName)
261477
- return false;
261478
- const index2 = indexFromPartName(partName);
261479
- const removedPaths = [
261480
- partName,
261481
- findPropsPartFor(convertedXml, partName),
261482
- index2 == null ? null : `customXml/_rels/item${index2}.xml.rels`
261483
- ].filter((path2) => typeof path2 === "string" && path2.length > 0);
261484
- for (const path2 of removedPaths)
261485
- delete convertedXml[path2];
261486
- const relsRoot = convertedXml["word/_rels/document.xml.rels"]?.elements?.find((el) => getLocalName2(el?.name) === "Relationships");
261487
- if (relsRoot?.elements?.length)
261488
- relsRoot.elements = relsRoot.elements.filter((rel) => {
261489
- if (rel?.attributes?.Type !== "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml")
261490
- return true;
261491
- return resolveOpcTargetPath(rel?.attributes?.Target, "word") !== partName;
261492
- });
261493
- if (converter) {
261494
- if (!(converter.removedCustomXmlPaths instanceof Set))
261495
- converter.removedCustomXmlPaths = /* @__PURE__ */ new Set;
261496
- for (const path2 of removedPaths)
261497
- converter.removedCustomXmlPaths.add(path2);
261498
- invalidateConverterCachesForPath(converter, partName);
261499
- }
261500
- return true;
261501
- }
261502
- function invalidateConverterCachesForPath(converter, partName) {
261503
- if (!converter || typeof partName !== "string")
261504
- return;
261505
- const biblio = converter.bibliographyPart;
261506
- if (biblio && biblio.partPath === partName)
261507
- converter.bibliographyPart = {
261508
- sources: [],
261509
- partPath: null,
261510
- itemPropsPath: null,
261511
- itemRelsPath: null,
261512
- selectedStyle: null,
261513
- styleName: null,
261514
- version: null
261515
- };
261516
261797
  }
261517
261798
  function getConverter$22(editor) {
261518
261799
  return editor.converter ?? null;
@@ -261630,20 +261911,20 @@ function customXmlPartsCreateWrapper(editor, input2, options) {
261630
261911
  }
261631
261912
  };
261632
261913
  }
261633
- const probe = safeValidate(() => createCustomXmlPart(getConvertedXml$1(editor), {
261914
+ const probe = safeValidate(() => prepareCustomXmlPartMutation(editor, (convertedXml, converter) => createCustomXmlPart(convertedXml, {
261634
261915
  content: input2.content,
261635
261916
  schemaRefs: input2.schemaRefs
261636
- }, getConverter$22(editor)));
261917
+ }, converter), "customXml.parts.create"));
261637
261918
  if (isWriteFailure(probe))
261638
261919
  return {
261639
261920
  changed: false,
261640
261921
  payload: probe
261641
261922
  };
261642
261923
  return {
261643
- changed: true,
261924
+ changed: false,
261644
261925
  payload: {
261645
261926
  ok: true,
261646
- payload: probe.payload
261927
+ payload: commitPreparedCustomXmlPartMutation(editor, probe.payload, { source: "customXml.parts.create" })
261647
261928
  }
261648
261929
  };
261649
261930
  }, {
@@ -261692,25 +261973,25 @@ function customXmlPartsPatchWrapper(editor, input2, options) {
261692
261973
  changed: false,
261693
261974
  payload: targetNotFound()
261694
261975
  };
261695
- const probe = safeValidate(() => patchCustomXmlPart(getConvertedXml$1(editor), input2.target, {
261976
+ const probe = safeValidate(() => prepareCustomXmlPartMutation(editor, (convertedXml, converter) => patchCustomXmlPart(convertedXml, input2.target, {
261696
261977
  content: input2.content,
261697
261978
  schemaRefs: input2.schemaRefs
261698
- }, getConverter$22(editor)));
261979
+ }, converter), "customXml.parts.patch"));
261699
261980
  if (isWriteFailure(probe))
261700
261981
  return {
261701
261982
  changed: false,
261702
261983
  payload: probe
261703
261984
  };
261704
- if (!probe.payload)
261985
+ if (!probe.payload.result)
261705
261986
  return {
261706
261987
  changed: false,
261707
261988
  payload: targetNotFound()
261708
261989
  };
261709
261990
  return {
261710
- changed: true,
261991
+ changed: false,
261711
261992
  payload: {
261712
261993
  ok: true,
261713
- payload: { id: probe.payload.id ?? null }
261994
+ payload: { id: commitPreparedCustomXmlPartMutation(editor, probe.payload, { source: "customXml.parts.patch" }).id ?? null }
261714
261995
  }
261715
261996
  };
261716
261997
  }, {
@@ -261741,13 +262022,15 @@ function customXmlPartsRemoveWrapper(editor, input2, options) {
261741
262022
  changed: false,
261742
262023
  payload: targetNotFound()
261743
262024
  };
261744
- if (!removeCustomXmlPart(getConvertedXml$1(editor), input2.target, getConverter$22(editor)))
262025
+ const probe = prepareCustomXmlPartMutation(editor, (convertedXml, converter) => removeCustomXmlPart(convertedXml, input2.target, converter), "customXml.parts.remove");
262026
+ if (!probe.result)
261745
262027
  return {
261746
262028
  changed: false,
261747
262029
  payload: targetNotFound()
261748
262030
  };
262031
+ commitPreparedCustomXmlPartMutation(editor, probe, { source: "customXml.parts.remove" });
261749
262032
  return {
261750
- changed: true,
262033
+ changed: false,
261751
262034
  payload: {
261752
262035
  ok: true,
261753
262036
  payload: true
@@ -262047,50 +262330,58 @@ function anchorOverlaps(editor, id2, within$1) {
262047
262330
  function writeEntry(editor, namespace, id2, payload, dryRun) {
262048
262331
  const convertedXml = getConvertedXml2(editor);
262049
262332
  const converter = getConverter$12(editor);
262050
- const existing = findPartByNamespace(convertedXml, namespace);
262051
- const entries2 = existing?.entries.filter((entry) => entry.id !== id2) ?? [];
262052
262333
  const next2 = {
262053
262334
  id: id2,
262054
262335
  namespace,
262055
- partName: existing?.partName ?? predictPartName(convertedXml, converter),
262336
+ partName: findPartByNamespace(convertedXml, namespace)?.partName ?? predictPartName(convertedXml, converter),
262056
262337
  payload
262057
262338
  };
262058
- const xml2 = buildEnvelopeXml(namespace, [...entries2, next2]);
262059
- if (dryRun)
262339
+ if (dryRun) {
262340
+ buildEnvelopeXml(namespace, [next2]);
262060
262341
  return { partName: next2.partName };
262061
- if (existing) {
262062
- patchCustomXmlPart(convertedXml, { partName: existing.partName }, {
262063
- content: xml2,
262064
- schemaRefs: undefined
262065
- }, converter ?? undefined);
262066
- markConverterDirty(editor);
262067
- return { partName: existing.partName };
262068
262342
  }
262069
- const created = createCustomXmlPart(convertedXml, {
262070
- content: xml2,
262071
- schemaRefs: undefined
262072
- }, converter ?? undefined);
262073
- markConverterDirty(editor);
262074
- return { partName: created.partName };
262343
+ return { partName: mutateCustomXmlParts(editor, "metadata.writeEntry", (sandboxXml, sandboxConverter) => {
262344
+ const sandboxExisting = findPartByNamespace(sandboxXml, namespace);
262345
+ const sandboxEntries = sandboxExisting?.entries.filter((entry) => entry.id !== id2) ?? [];
262346
+ const sandboxNext = {
262347
+ id: id2,
262348
+ namespace,
262349
+ partName: sandboxExisting?.partName ?? predictPartName(sandboxXml, sandboxConverter),
262350
+ payload
262351
+ };
262352
+ const sandboxEnvelope = buildEnvelopeXml(namespace, [...sandboxEntries, sandboxNext]);
262353
+ if (sandboxExisting) {
262354
+ patchCustomXmlPart(sandboxXml, { partName: sandboxExisting.partName }, {
262355
+ content: sandboxEnvelope,
262356
+ schemaRefs: undefined
262357
+ }, sandboxConverter);
262358
+ return sandboxExisting.partName;
262359
+ }
262360
+ return createCustomXmlPart(sandboxXml, {
262361
+ content: sandboxEnvelope,
262362
+ schemaRefs: undefined
262363
+ }, sandboxConverter).partName;
262364
+ }) };
262075
262365
  }
262076
262366
  function removeEntry(editor, id2, dryRun) {
262077
- const convertedXml = getConvertedXml2(editor);
262078
- const converter = getConverter$12(editor);
262079
- const part = listMetadataParts(convertedXml).find((candidate) => candidate.entries.some((entry) => entry.id === id2));
262080
- if (!part)
262367
+ if (!listMetadataParts(getConvertedXml2(editor)).find((candidate) => candidate.entries.some((entry) => entry.id === id2)))
262081
262368
  return false;
262082
262369
  if (dryRun)
262083
262370
  return true;
262084
- const remaining = part.entries.filter((entry) => entry.id !== id2);
262085
- if (remaining.length === 0)
262086
- removeCustomXmlPart(convertedXml, { partName: part.partName }, converter ?? undefined);
262087
- else
262088
- patchCustomXmlPart(convertedXml, { partName: part.partName }, {
262089
- content: buildEnvelopeXml(part.namespace, remaining),
262090
- schemaRefs: undefined
262091
- }, converter ?? undefined);
262092
- markConverterDirty(editor);
262093
- return true;
262371
+ return mutateCustomXmlParts(editor, "metadata.removeEntry", (sandboxXml, sandboxConverter) => {
262372
+ const sandboxPart = listMetadataParts(sandboxXml).find((candidate) => candidate.entries.some((entry) => entry.id === id2));
262373
+ if (!sandboxPart)
262374
+ return false;
262375
+ const remaining = sandboxPart.entries.filter((entry) => entry.id !== id2);
262376
+ if (remaining.length === 0)
262377
+ removeCustomXmlPart(sandboxXml, { partName: sandboxPart.partName }, sandboxConverter);
262378
+ else
262379
+ patchCustomXmlPart(sandboxXml, { partName: sandboxPart.partName }, {
262380
+ content: buildEnvelopeXml(sandboxPart.namespace, remaining),
262381
+ schemaRefs: undefined
262382
+ }, sandboxConverter);
262383
+ return true;
262384
+ });
262094
262385
  }
262095
262386
  function toSummary(entry, editor) {
262096
262387
  return {
@@ -262215,7 +262506,7 @@ function metadataUpdateWrapper(editor, input2, options) {
262215
262506
  if (!dryRun)
262216
262507
  writeEntry(editor, existing.namespace, input2.id, input2.payload, false);
262217
262508
  return {
262218
- changed: true,
262509
+ changed: false,
262219
262510
  payload: {
262220
262511
  success: true,
262221
262512
  id: input2.id
@@ -262241,8 +262532,9 @@ function metadataRemoveWrapper(editor, input2, options) {
262241
262532
  }
262242
262533
  if (!anchorExists)
262243
262534
  return executeOutOfBandMutation(editor, (dryRun) => {
262535
+ removeEntry(editor, input2.id, dryRun);
262244
262536
  return {
262245
- changed: removeEntry(editor, input2.id, dryRun),
262537
+ changed: false,
262246
262538
  payload: {
262247
262539
  success: true,
262248
262540
  id: input2.id
@@ -290499,7 +290791,7 @@ var Node$13 = class Node$14 {
290499
290791
  let $pos = doc$12.resolve(this.pos);
290500
290792
  return GapCursor.valid($pos) ? new GapCursor($pos) : Selection.near($pos);
290501
290793
  }
290502
- }, handleKeyDown2, Gapcursor, PARTS_MAP_KEY = "parts", META_MAP_KEY = "meta", META_PARTS_SCHEMA_VERSION_KEY = "partsSchemaVersion", META_PARTS_MIGRATION_KEY = "partsMigration", META_PARTS_LAST_HYDRATED_AT_KEY = "partsLastHydratedAt", META_PARTS_FALLBACK_MODE_KEY = "partsFallbackMode", META_PARTS_CAPABILITY_KEY = "partsCapability", SOURCE_COLLAB_REMOTE_PARTS = "collab:remote:parts", SOURCE_COLLAB_REMOTE_PREFIX = "collab:remote:", EXCLUDED_PART_IDS, CRITICAL_PART_IDS, isApplyingRemoteParts = false, META_MAP_KEY$1 = "meta", NOTE_TOMBSTONE_META_PREFIX = "noteTombstone:", SCHEMA_ATOM_NODE_NAMES, NORMALIZE_YJS_FRAGMENT_ORIGIN, headlessBindingStateByEditor, headlessCleanupRegisteredEditors, META_BODY_SECT_PR_KEY = "bodySectPr", BODY_SECT_PR_SYNC_META_KEY = "bodySectPrSync", collaborationCleanupByEditor, registerHeadlessBindingCleanup = (editor, cleanup) => {
290794
+ }, handleKeyDown2, Gapcursor, PARTS_MAP_KEY = "parts", META_MAP_KEY = "meta", META_PARTS_SCHEMA_VERSION_KEY = "partsSchemaVersion", META_PARTS_MIGRATION_KEY = "partsMigration", META_PARTS_LAST_HYDRATED_AT_KEY = "partsLastHydratedAt", META_PARTS_FALLBACK_MODE_KEY = "partsFallbackMode", META_PARTS_CAPABILITY_KEY = "partsCapability", SOURCE_COLLAB_REMOTE_PARTS = "collab:remote:parts", SOURCE_COLLAB_REMOTE_PREFIX = "collab:remote:", EXCLUDED_PART_IDS, CRITICAL_PART_IDS, import_lib4, CUSTOM_XML_DATA_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml", CUSTOM_XML_PROPS_RELATIONSHIP_TYPE$1 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps", CUSTOM_XML_DATASTORE_NAMESPACE = "http://schemas.openxmlformats.org/officeDocument/2006/customXml", CUSTOM_XML_PROPS_RELATIONSHIP_TYPE2 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps", isApplyingRemoteParts = false, META_MAP_KEY$1 = "meta", NOTE_TOMBSTONE_META_PREFIX = "noteTombstone:", SCHEMA_ATOM_NODE_NAMES, NORMALIZE_YJS_FRAGMENT_ORIGIN, headlessBindingStateByEditor, headlessCleanupRegisteredEditors, META_BODY_SECT_PR_KEY = "bodySectPr", BODY_SECT_PR_SYNC_META_KEY = "bodySectPrSync", collaborationCleanupByEditor, registerHeadlessBindingCleanup = (editor, cleanup) => {
290503
290795
  if (!cleanup || headlessCleanupRegisteredEditors.has(editor))
290504
290796
  return;
290505
290797
  headlessCleanupRegisteredEditors.add(editor);
@@ -307322,7 +307614,7 @@ var Node$13 = class Node$14 {
307322
307614
  return () => {};
307323
307615
  const handle3 = setInterval(callback, intervalMs);
307324
307616
  return () => clearInterval(handle3);
307325
- }, HISTORY_UNSAFE_OPS, CANONICAL_COMMENT_IGNORED_KEYS, INITIAL_HASH, ROUND_CONSTANTS, V1_COVERAGE, V2_COVERAGE, SNAPSHOT_VERSION_V2 = "sd-diff-snapshot/v2", PAYLOAD_VERSION_V1 = "sd-diff-payload/v1", PAYLOAD_VERSION_V2 = "sd-diff-payload/v2", ENGINE_ID = "super-editor", STAGED_CONVERTER_KEYS, DiffServiceError, TC_LEVEL_MIN = 1, TC_LEVEL_MAX = 9, ALLOWED_WRAP_ATTRS, WRAP_TYPES_SUPPORTING_SIDE, WRAP_TYPES_SUPPORTING_DISTANCES, RELATIVE_HEIGHT_MIN = 0, RELATIVE_HEIGHT_MAX = 4294967295, FORBIDDEN_RAW_PATCH_NAMES, CONTROL_TYPE_SDT_PR_ELEMENTS, DEFAULT_CHECKBOX_SYMBOL_FONT2 = "MS Gothic", DEFAULT_CHECKBOX_CHECKED_HEX2 = "2612", DEFAULT_CHECKBOX_UNCHECKED_HEX2 = "2610", VARIANT_ORDER, KIND_ORDER, HEADER_RELATIONSHIP_TYPE3 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", FOOTER_RELATIONSHIP_TYPE3 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", DOCUMENT_RELS_PATH2 = "word/_rels/document.xml.rels", HEADER_FILE_PATTERN2, FOOTER_FILE_PATTERN2, BOOKMARK_SCAN_REVISION_PREFIX = "bookmark-scan:", import_lib4, CUSTOM_XML_DATA_RELATIONSHIP_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml", CUSTOM_XML_PROPS_RELATIONSHIP_TYPE2 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps", CUSTOM_XML_DATASTORE_NAMESPACE = "http://schemas.openxmlformats.org/officeDocument/2006/customXml", SETTINGS_PART, VALID_DISPLAYS, REFERENCE_BLOCK_PREFIX, CAPTION_STYLE_NAMES, CAPTION_PARAGRAPH_STYLE_ID = "Caption", CAPTION_FORMAT_TO_OOXML, DOCUMENT_STAT_FIELD_TYPES, TOA_LEADER_REVERSE_MAP, EDGE_NODE_TYPES, CONTENT_TYPES_PART_ID = "[Content_Types].xml", CONTENT_TYPES_NS = "http://schemas.openxmlformats.org/package/2006/content-types", contentTypesPartDescriptor, empty_exports, init_empty, CURRENT_APP_VERSION2 = "1.44.1", PIXELS_PER_INCH2 = 96, MAX_HEIGHT_BUFFER_PX = 50, MAX_WIDTH_BUFFER_PX = 20, TRACKED_REVIEW_MARK_NAMES2, isTrackedReviewMark = (mark2) => Boolean(mark2?.type?.name && TRACKED_REVIEW_MARK_NAMES2.has(mark2.type.name)), trackedReviewMarkKey = (mark2) => {
307617
+ }, HISTORY_UNSAFE_OPS, CANONICAL_COMMENT_IGNORED_KEYS, INITIAL_HASH, ROUND_CONSTANTS, V1_COVERAGE, V2_COVERAGE, SNAPSHOT_VERSION_V2 = "sd-diff-snapshot/v2", PAYLOAD_VERSION_V1 = "sd-diff-payload/v1", PAYLOAD_VERSION_V2 = "sd-diff-payload/v2", ENGINE_ID = "super-editor", STAGED_CONVERTER_KEYS, DiffServiceError, TC_LEVEL_MIN = 1, TC_LEVEL_MAX = 9, ALLOWED_WRAP_ATTRS, WRAP_TYPES_SUPPORTING_SIDE, WRAP_TYPES_SUPPORTING_DISTANCES, RELATIVE_HEIGHT_MIN = 0, RELATIVE_HEIGHT_MAX = 4294967295, FORBIDDEN_RAW_PATCH_NAMES, CONTROL_TYPE_SDT_PR_ELEMENTS, DEFAULT_CHECKBOX_SYMBOL_FONT2 = "MS Gothic", DEFAULT_CHECKBOX_CHECKED_HEX2 = "2612", DEFAULT_CHECKBOX_UNCHECKED_HEX2 = "2610", VARIANT_ORDER, KIND_ORDER, HEADER_RELATIONSHIP_TYPE3 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header", FOOTER_RELATIONSHIP_TYPE3 = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer", DOCUMENT_RELS_PATH2 = "word/_rels/document.xml.rels", HEADER_FILE_PATTERN2, FOOTER_FILE_PATTERN2, BOOKMARK_SCAN_REVISION_PREFIX = "bookmark-scan:", SETTINGS_PART, VALID_DISPLAYS, REFERENCE_BLOCK_PREFIX, CAPTION_STYLE_NAMES, CAPTION_PARAGRAPH_STYLE_ID = "Caption", CAPTION_FORMAT_TO_OOXML, DOCUMENT_STAT_FIELD_TYPES, TOA_LEADER_REVERSE_MAP, EDGE_NODE_TYPES, CONTENT_TYPES_PART_ID = "[Content_Types].xml", CONTENT_TYPES_NS = "http://schemas.openxmlformats.org/package/2006/content-types", contentTypesPartDescriptor, empty_exports, init_empty, CURRENT_APP_VERSION2 = "1.44.1", PIXELS_PER_INCH2 = 96, MAX_HEIGHT_BUFFER_PX = 50, MAX_WIDTH_BUFFER_PX = 20, TRACKED_REVIEW_MARK_NAMES2, isTrackedReviewMark = (mark2) => Boolean(mark2?.type?.name && TRACKED_REVIEW_MARK_NAMES2.has(mark2.type.name)), trackedReviewMarkKey = (mark2) => {
307326
307618
  if (!isTrackedReviewMark(mark2))
307327
307619
  return null;
307328
307620
  const id2 = typeof mark2.attrs?.id === "string" ? mark2.attrs.id : "";
@@ -328209,7 +328501,7 @@ menclose::after {
328209
328501
  return;
328210
328502
  console.log(...args$1);
328211
328503
  }, HEADER_FOOTER_INIT_BUDGET_MS = 200, MAX_ZOOM_WARNING_THRESHOLD = 10, MAX_SELECTION_RECTS_PER_USER = 100, SEMANTIC_RESIZE_DEBOUNCE_MS = 120, MIN_SEMANTIC_CONTENT_WIDTH_PX = 1, GLOBAL_PERFORMANCE, PresentationEditor, ICONS, TEXTS, tableActionsOptions, TRACKED_MARK_NAMES;
328212
- var init_src_D32RsXxf_es = __esm(() => {
328504
+ var init_src_VH8r3Wbu_es = __esm(() => {
328213
328505
  init_rolldown_runtime_Bg48TavK_es();
328214
328506
  init_SuperConverter_8tQYSE1r_es();
328215
328507
  init_jszip_C49i9kUs_es();
@@ -335343,6 +335635,7 @@ ${err.toString()}`);
335343
335635
  "word/_rels/document.xml.rels",
335344
335636
  "[Content_Types].xml"
335345
335637
  ]);
335638
+ import_lib4 = /* @__PURE__ */ __toESM2(require_lib(), 1);
335346
335639
  SCHEMA_ATOM_NODE_NAMES = new Set(["crossReference", "citation"]);
335347
335640
  NORMALIZE_YJS_FRAGMENT_ORIGIN = Symbol.for("superdoc/yjs-fragment-normalize");
335348
335641
  new PluginKey("collaboration");
@@ -359606,7 +359899,7 @@ function print() { __p += __j.call(arguments, '') }
359606
359899
  stylesPartDescriptor = {
359607
359900
  id: STYLES_PART_ID,
359608
359901
  ensurePart(editor) {
359609
- const converter = getConverter$62(editor);
359902
+ const converter = getConverter$72(editor);
359610
359903
  if (converter?.convertedXml[STYLES_PART_ID])
359611
359904
  return converter.convertedXml[STYLES_PART_ID];
359612
359905
  return {
@@ -359621,7 +359914,7 @@ function print() { __p += __j.call(arguments, '') }
359621
359914
  },
359622
359915
  afterCommit(ctx$1) {
359623
359916
  if (ctx$1.source.startsWith("collab:remote:") || ctx$1.source === "templates.apply") {
359624
- const converter = getConverter$62(ctx$1.editor);
359917
+ const converter = getConverter$72(ctx$1.editor);
359625
359918
  if (converter)
359626
359919
  try {
359627
359920
  converter.translatedLinkedStyles = translateStyleDefinitions(converter.convertedXml);
@@ -360014,7 +360307,6 @@ function print() { __p += __j.call(arguments, '') }
360014
360307
  KIND_ORDER = ["header", "footer"];
360015
360308
  HEADER_FILE_PATTERN2 = /header(\d+)\.xml$/;
360016
360309
  FOOTER_FILE_PATTERN2 = /footer(\d+)\.xml$/;
360017
- import_lib4 = /* @__PURE__ */ __toESM2(require_lib(), 1);
360018
360310
  SETTINGS_PART = SETTINGS_PART_PATH;
360019
360311
  VALID_DISPLAYS = new Set([
360020
360312
  "content",
@@ -371817,7 +372109,7 @@ var init_zipper_BxRAi0_5_es = __esm(() => {
371817
372109
 
371818
372110
  // ../../packages/superdoc/dist/super-editor.es.js
371819
372111
  var init_super_editor_es = __esm(() => {
371820
- init_src_D32RsXxf_es();
372112
+ init_src_VH8r3Wbu_es();
371821
372113
  init_SuperConverter_8tQYSE1r_es();
371822
372114
  init_jszip_C49i9kUs_es();
371823
372115
  init_xml_js_CqGKpaft_es();