docx-diff-editor 1.0.33 → 1.0.37

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.mjs CHANGED
@@ -725,17 +725,11 @@ var DocxDiffEditor = forwardRef(
725
725
  const editorId = `dde-editor-${instanceId.current}`;
726
726
  const toolbarId = `dde-toolbar-${instanceId.current}`;
727
727
  const setEditorContent = useCallback((editor, json) => {
728
- if (editor.commands?.setContent) {
729
- editor.commands.setContent(json);
730
- } else if (editor.setContent) {
731
- editor.setContent(json);
732
- } else {
733
- const { state, view } = editor;
734
- if (state?.doc && view && json.content) {
735
- const newDoc = state.schema.nodeFromJSON(json);
736
- const tr = state.tr.replaceWith(0, state.doc.content.size, newDoc.content);
737
- view.dispatch(tr);
738
- }
728
+ const { state, view } = editor;
729
+ if (state?.doc && view && json.content) {
730
+ const newDoc = state.schema.nodeFromJSON(json);
731
+ const tr = state.tr.replaceWith(0, state.doc.content.size, newDoc.content);
732
+ view.dispatch(tr);
739
733
  }
740
734
  }, []);
741
735
  const enableReviewMode = useCallback((sd) => {
@@ -917,9 +911,26 @@ var DocxDiffEditor = forwardRef(
917
911
  useImperativeHandle(
918
912
  ref,
919
913
  () => ({
914
+ /**
915
+ * Update content in the existing editor without recreating SuperDoc instance.
916
+ * Preserves the DOCX template/styling. Ideal for replacing content with translated JSON.
917
+ */
918
+ updateContent(json) {
919
+ const editor = superdocRef.current?.activeEditor;
920
+ if (!editor) {
921
+ throw new Error("Editor not ready");
922
+ }
923
+ setEditorContent(editor, json);
924
+ setSourceJson(json);
925
+ setMergedJson(null);
926
+ setDiffResult(null);
927
+ onSourceLoaded?.(json);
928
+ },
920
929
  /**
921
930
  * Set the source/base document.
922
931
  * Accepts File (DOCX), HTML string, or ProseMirror JSON.
932
+ * Note: This destroys and recreates the SuperDoc instance.
933
+ * For JSON content updates, prefer updateContent() to preserve the existing template.
923
934
  */
924
935
  async setSource(content) {
925
936
  if (!SuperDocRef.current) {
@@ -1158,6 +1169,65 @@ var DocxDiffEditor = forwardRef(
1158
1169
  */
1159
1170
  isReady() {
1160
1171
  return readyRef.current;
1172
+ },
1173
+ /**
1174
+ * Get the current page count from the presentation editor.
1175
+ * Returns 0 if editor is not ready or pages are unavailable.
1176
+ */
1177
+ getPages() {
1178
+ if (!readyRef.current || !superdocRef.current) {
1179
+ return 0;
1180
+ }
1181
+ try {
1182
+ const sd = superdocRef.current;
1183
+ const doc = sd.superdocStore?.documents?.[0];
1184
+ if (!doc) {
1185
+ return 0;
1186
+ }
1187
+ const presentationEditor = doc.getPresentationEditor?.();
1188
+ const pages = presentationEditor?.getPages?.();
1189
+ return pages?.length ?? 0;
1190
+ } catch (err) {
1191
+ console.warn("[DocxDiffEditor] Failed to get page count:", err);
1192
+ return 0;
1193
+ }
1194
+ },
1195
+ /**
1196
+ * Get combined document metadata and statistics.
1197
+ * Returns null if editor is not ready.
1198
+ */
1199
+ getDocumentInfo() {
1200
+ if (!readyRef.current || !superdocRef.current) {
1201
+ return null;
1202
+ }
1203
+ try {
1204
+ const sd = superdocRef.current;
1205
+ const doc = sd.superdocStore?.documents?.[0];
1206
+ if (!doc) {
1207
+ return null;
1208
+ }
1209
+ const editor = doc.getEditor?.();
1210
+ const metadata = editor?.getMetadata?.() ?? {};
1211
+ const stats = editor?.commands?.getDocumentStats?.() ?? {};
1212
+ const presentationEditor = doc.getPresentationEditor?.();
1213
+ const pages = presentationEditor?.getPages?.();
1214
+ const pageCount = pages?.length ?? 0;
1215
+ return {
1216
+ // Metadata
1217
+ documentGuid: metadata.documentGuid ?? null,
1218
+ isModified: metadata.isModified ?? false,
1219
+ version: metadata.version ?? null,
1220
+ // Stats
1221
+ words: stats.words ?? 0,
1222
+ characters: stats.characters ?? 0,
1223
+ paragraphs: stats.paragraphs ?? 0,
1224
+ // Pages
1225
+ pages: pageCount
1226
+ };
1227
+ } catch (err) {
1228
+ console.warn("[DocxDiffEditor] Failed to get document info:", err);
1229
+ return null;
1230
+ }
1161
1231
  }
1162
1232
  }),
1163
1233
  [