{
  "$schema": "https://ai.syncfusion.com/schema/registry-item.json",
  "name": "docx-editor-compare-documents",
  "framework": "react",
  "type": "registry:component",
  "component": "DocxEditor",
  "variant": "compare-documents",
  "dependencies": [
    "@syncfusion/ej2-react-documenteditor",
    "@syncfusion/ej2-react-buttons"
  ],
  "description": "Word editor compares original and revised document uploads side by side and shows revisions.",
  "meta": {
    "essentialStudioRelease": "2026 Volume 3 (v35.1.37)",
    "license": "commercial-or-community",
    "licenseNote": "The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md",
    "requiresLicenseKey": true,
    "requiresServerEndpoint": true,
    "platformIndex": "https://ai.syncfusion.com/react/llms.txt",
    "skillPack": "syncfusion/react-ui-components-skills",
    "docs": "https://ai.syncfusion.com/gallery/react/ts/docx-editor-compare-documents.md"
  },
  "files": [
    {
      "target": "src/components/docx-editor-compare-documents/DocxEditor.tsx",
      "content": "import * as React from 'react';\nimport { useEffect, useRef, useState } from 'react';\nimport { DocumentEditorContainerComponent, Ribbon, Toolbar } from '@syncfusion/ej2-react-documenteditor';\nimport { ButtonComponent } from '@syncfusion/ej2-react-buttons';\nimport { originalDocument, revisedDocument } from '../datasource/docx-editor-data';\nimport '../default.component.css';\n\nDocumentEditorContainerComponent.Inject(Toolbar, Ribbon);\n\nconst CompareDocuments = () => {\n\n  const editorRef1 = useRef<DocumentEditorContainerComponent | null>(null);\n  const editorRef2 = useRef<DocumentEditorContainerComponent | null>(null);\n\n  const [originalFile, setOriginalFile] = useState<File | null>(null);\n  const [revisedFile, setRevisedFile] = useState<File | null>(null);\n  const [showResult, setShowResult] = useState<boolean>(true);\n  const [compareClicked, setCompareClicked] = useState(false);\n  const [showRevisions, setShowRevisions] = useState(false);\n  \n  let serviceUrl: string = 'https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/'; \n\n  const openFileInEditor = async (\n    file: File,\n    editorRef: React.RefObject<DocumentEditorContainerComponent | null>\n  ) => {\n    if (file.name.endsWith('.sfdt')) {\n      const reader = new FileReader();\n      reader.onload = e => {\n        const docData = e.target?.result as string | undefined;\n        if (docData && editorRef.current?.documentEditor) {\n          editorRef.current.documentEditor.showRevisions = false;\n          editorRef.current.documentEditor.open(docData);\n        }\n      };\n      reader.readAsText(file);\n    }\n    else {\n      const formData = new FormData();\n      formData.append('file', file);\n      try {\n        const response = await fetch(serviceUrl + 'Import', {\n          method: 'POST',\n          body: formData\n        });\n        const sfdtString = await response.text();\n        let sfdtObject: any = null;\n        try {\n          sfdtObject = JSON.parse(sfdtString);\n        } catch (e) {\n          alert(\"Failed to process the compared document due to no valid JSON. Please try again.\");\n        }\n        if (sfdtObject && editorRef.current?.documentEditor) {\n          editorRef.current.documentEditor.showRevisions = false;\n          editorRef.current.documentEditor.open(sfdtObject);\n        } else {\n          alert(\"Unable to display the compared document. Please try again.\");        \n        }\n      } catch (e) {\n        alert('This Compare Documents demo supports only DOCX and SFDT file formats. Please select a valid DOCX or SFDT document and try again.');\n      }\n    }\n  };\n\n  const onCompare = async () => {\n    setCompareClicked(false); // Reset to re-mount editors\n    setShowRevisions(false);\n    setTimeout(async () => {\n      if(originalFile != null && revisedFile != null){\n        const isOriginalSupported =\n          originalFile.name.endsWith('.sfdt') || originalFile.name.endsWith('.docx');\n\n        const isRevisedSupported =\n          revisedFile.name.endsWith('.sfdt') || revisedFile.name.endsWith('.docx');\n\n        if (isOriginalSupported && isRevisedSupported) {\n          setCompareClicked(true);\n          showHideWaitingIndicator(true);\n          if (showResult) {\n            if (originalFile) await openFileInEditor(originalFile, editorRef1);\n            if (originalFile && revisedFile) {\n              await loadComparedDocumentAndOpen(editorRef2 as React.RefObject<DocumentEditorContainerComponent>, originalFile, revisedFile);\n            }\n          } else {\n            if (originalFile) await openFileInEditor(originalFile, editorRef1);\n            if (revisedFile) await openFileInEditor(revisedFile, editorRef2);\n          }\n          showHideWaitingIndicator(false);\n        }\n        else{\n          alert('This Compare Documents demo supports only DOCX and SFDT file formats. Please select a valid DOCX or SFDT document and try again.');\n        }     \n      }\n    }, 0);\n  };\n\n  // open the compared document in the editor (right editor)\n  async function loadComparedDocumentAndOpen(\n    editorRef2: React.RefObject<DocumentEditorContainerComponent>,\n    originalFile: File,\n    revisedFile: File\n  ) \n  {\n    const formData = new FormData();\n    formData.append(\"originalFile\", originalFile);\n    formData.append(\"revisedFile\", revisedFile);\n    formData.append(\"author\", \"Author\");\n    formData.append(\"dateTime\", new Date().toISOString());\n    const response = await fetch(serviceUrl + 'CompareDocuments', {\n      method: 'POST',\n      body: formData\n    });\n    if (!response.ok) {\n      alert(\"Failed to compare the selected documents. Please try again.\");      \n      return;\n    }\n    const sfdtString = await response.text();\n    let sfdtObject: any = null;\n    try {\n      sfdtObject = JSON.parse(sfdtString);\n    } catch {\n      alert(\"The comparison could not be completed due to no valid JSON. Please try again.\");\n      return;\n    }\n    if (sfdtObject && editorRef2.current?.documentEditor) {\n      editorRef2.current.documentEditor.showRevisions = false;\n      editorRef2.current.documentEditor.open(sfdtObject);\n    } else {\n      alert(\"Unable to display the comparison result. Please try again.\");    \n    }\n  }\n\n  const showHideWaitingIndicator = (show: boolean) => {\n    let waitingPopUp: HTMLElement | null = document.getElementById(\"waiting-popup\");\n    let inActiveDiv: HTMLElement | null = document.getElementById(\"popup-overlay\");\n    if(waitingPopUp && inActiveDiv){\n      inActiveDiv.style.display = show ? \"block\" : \"none\";\n      waitingPopUp.style.display = show ? \"block\" : \"none\";\n    }\n  };\n\n  // Download the revised/result file (right editor)\n  const onDownload = () => {\n    if (editorRef2.current?.documentEditor) {\n      editorRef2.current.documentEditor.save('Result', 'Docx');\n    }\n  };\n\n  useEffect(() => {\n    const editor1 = editorRef1.current?.documentEditor;\n    const editor2 = editorRef2.current?.documentEditor;\n    if (editor1 && editor2) {\n      editor1.open(JSON.stringify(originalDocument));\n      editor2.showRevisions = false;\n      editor2.open(JSON.stringify(revisedDocument));      \n      editor1.viewChange = () => {\n        const pos = editor1.selection.getScrollPosition();\n        editor2.selection.setScrollPosition(pos);\n      };\n      editor2.viewChange = () => {\n        const pos = editor2.selection.getScrollPosition();\n        editor1.selection.setScrollPosition(pos);\n      };\n    }\n }, []);\n\n  const toggleRevisionPane = () => {\n    setShowRevisions(!showRevisions);\n    let editor2 = editorRef2.current?.documentEditor;\n    if (editor2) {\n      editor2.showRevisions = !showRevisions;\n    }\n  };\n    return (\n    <div className=\"control-pane\">\n      <div className=\"control-section\" style={{ width: '100%' }}>\n\n        {/* Shared Container */}\n        <div\n          style={{\n            width: '760px',\n            margin: '10px auto'\n          }}>\n          {/* Upload Row */}\n          <div style={{ display: 'flex', gap: '6px' }}>\n\n            {/* Original */}\n            <div\n              style={{\n                width: '360px',\n                borderRadius: 6,\n                padding: 14,\n                border: '1px dashed #ccc'\n              }}>\n              {/* Top row */}\n              <div style={{ display: 'flex', alignItems: 'center', gap: '10px' }}>\n                <span>Original document</span>\n\n                <label\n                  style={{\n                    padding: '4px 10px',\n                    border: '1px solid #ccc',\n                    borderRadius: 4,\n                    cursor: 'pointer'\n                  }}>\n                  Choose file\n                  <input\n                    type=\"file\"\n                    style={{ display: 'none' }}\n                    accept=\".docx,.sfdt\"\n                    onChange={(e) => {\n                      const file = e.target.files?.[0];\n                      if (file) {\n                        setOriginalFile(file);\n                        setCompareClicked(false);\n                      }\n                    }}\n                  />\n                </label>\n              </div>\n\n              {/* Filename + Delete */}\n              <div\n                style={{\n                  display: 'flex',\n                  justifyContent: 'space-between',\n                  alignItems: 'center',\n                  marginTop: 6,\n                  fontSize: 12,\n                  color: '#666'\n                }}>\n                <span>\n                  {originalFile\n                    ? `${originalFile.name} (${Math.round(originalFile.size / 1024)} KB)`\n                    : 'Supported formats: SFDT, DOCX'}\n                </span>\n\n                {originalFile && (\n                  <span\n                    className=\"e-icons e-trash\"\n                    onClick={() => setOriginalFile(null)}\n                    style={{\n                      cursor: 'pointer'\n                    }}\n                    title=\"Remove file\"\n                  />\n                )}\n              </div>\n            </div>\n\n            {/* Revised */}\n            <div\n              style={{\n                width: 360,\n                borderRadius: 6,\n                padding: 14,\n                border: '1px dashed #ccc'\n              }}>\n              {/* Top row */}\n              <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>\n                <span>Revised document</span>\n\n                <label\n                  style={{\n                    padding: '4px 10px',\n                    border: '1px solid #ccc',\n                    borderRadius: 4,\n                    cursor: 'pointer'\n                  }}>\n                  Choose file\n                  <input\n                    type=\"file\"\n                    style={{ display: 'none' }}\n                    accept=\".docx,.sfdt\"\n                    onChange={(e) => {\n                      const file = e.target.files?.[0];\n                      if (file) {\n                        setRevisedFile(file);\n                        setCompareClicked(false);\n                      }\n                    }}\n                  />\n                </label>\n              </div>\n\n              {/* Filename + Delete */}\n              <div\n                style={{\n                  display: 'flex',\n                  justifyContent: 'space-between',\n                  alignItems: 'center',\n                  marginTop: 6,\n                  fontSize: 12,\n                  color: '#666'\n                }}>\n                <span>\n                  {revisedFile\n                    ? `${revisedFile.name} (${Math.round(revisedFile.size / 1024)} KB)`\n                    : 'Supported formats: SFDT, DOCX'}\n                </span>\n\n                {revisedFile && (\n                  <span\n                    className=\"e-icons e-trash\"\n                    onClick={() => setRevisedFile(null)}\n                    style={{\n                      cursor: 'pointer'\n                    }}\n                    title=\"Remove file\"\n                  />\n                )}\n              </div>\n            </div>\n          </div>\n\n          {/* Controls */}\n          <div\n            style={{\n              display: 'flex',\n              justifyContent: 'space-between',\n              alignItems: 'center',\n              marginTop: 12\n            }}>\n            {/* Checkbox */}\n            <div>\n              <label\n                htmlFor=\"showResultCheckbox\"\n                style={{ display: 'flex', alignItems: 'center', cursor: 'pointer' }}\n              >\n                <input\n                  id=\"showResultCheckbox\"\n                  type=\"checkbox\"\n                  checked={showResult}\n                  onChange={(e) => setShowResult(e.target.checked)}\n                  style={{ marginRight: 6 }}\n                />\n                <span>Show comparison results with tracked changes</span>\n              </label>\n            </div>\n\n            {/* Buttons */}\n            <div style={{ display: 'flex', gap: 10, marginRight: 32 }}>\n              <ButtonComponent\n                cssClass=\"e-primary\"\n                disabled={!originalFile || !revisedFile}\n                onClick={onCompare}>\n                Compare\n              </ButtonComponent>\n\n              <ButtonComponent\n                cssClass=\"e-outline e-flat e-primary\"\n                disabled={!compareClicked}\n                onClick={onDownload}\n                iconCss=\"e-icons e-download\">\n                Download Result Document\n              </ButtonComponent>\n            </div>\n          </div>\n        </div>\n\n        {/* Editors */}\n        <div\n          style={{\n            display: 'flex',\n            gap: 6,\n            height: \"calc(100vh - 330px)\",\n            padding: '0px 10px'\n          }}>\n          {/* Left Editor */}\n          <div\n            style={{\n              width: '50%',\n              display: 'flex',\n              flexDirection: 'column',\n              border: '1px solid #ddd',\n              overflow: 'hidden'\n            }}>\n            <div className=\"e-de-ctn-title\" style={{ padding: '8px', fontWeight: 600 }}>\n              Original Document\n            </div>\n\n            <div style={{ flex: 1, display:\"block\", height: \"calc(100vh - 330px)\"}}>\n              <DocumentEditorContainerComponent\n                id=\"editor1\"\n                ref={editorRef1}\n                serviceUrl={serviceUrl}\n                height=\"100%\"\n                width=\"100%\"\n                enableToolbar={false}\n                showPropertiesPane={false}\n              />\n            </div>\n          </div>\n\n          {/* Right Editor */}\n          <div\n            style={{\n              width: '50%',\n              display: 'flex',\n              flexDirection: 'column',\n              border: '1px solid #ddd',\n              overflow: 'hidden'\n            }}>\n            <div className=\"e-de-ctn-title\" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center'}}>\n              <div style={{ float: 'left', padding: '8px', fontWeight: 600, flex: 1  }}>\n                {showResult\n                  ? 'Result Document (with tracked changes)'\n                  : 'Revised Document'}\n              </div>\n              {showResult &&\n                <ButtonComponent style={{ float: 'right', fontWeight: 400, \n                        background: 'transparent', boxShadow: 'none',\n                        borderColor: 'transparent', borderRadius: '0px',\n                        color: 'inherit', fontSize: '12px'}}\n                    className='e-de-ctn-title'\n                    title=\"Show/Hide Revisions Pane\"\n                    onClick={toggleRevisionPane}>\n                    <span className='e-icons e-eye'></span>\n                    {showRevisions ? ' Hide Review Pane' : ' Show Review Pane'}\n                </ButtonComponent>\n              }\n            </div>\n\n            <div style={{ flex: 1, display:\"block\", height: \"calc(100vh - 330px)\"}}>\n              <DocumentEditorContainerComponent\n                id=\"editor2\"\n                ref={editorRef2}\n                serviceUrl={serviceUrl}\n                height=\"100%\"\n                width=\"100%\"\n                enableToolbar={false}\n                showPropertiesPane={false}\n              />\n            </div>\n          </div>\n        </div>\n\n        {/* Loading Progress */}\n        <div className=\"overlay\" id=\"popup-overlay\"></div>\n        <div id=\"waiting-popup\">\n          <svg className=\"circular\" height=\"40\" width=\"40\">\n            <circle\n              className=\"circle-path\"\n              cx=\"25\"\n              cy=\"25\"\n              r=\"20\"\n              fill=\"none\"\n              strokeWidth=\"6\"\n              strokeMiterlimit=\"10\" \n            />\n          </svg>\n        </div>    \n      </div>          \n    </div>   \n  );\n}\nexport default CompareDocuments;\n"
    }
  ]
}