{
  "$schema": "https://ai.syncfusion.com/schema/registry-item.json",
  "name": "docx-editor-mail-merge",
  "framework": "react",
  "type": "registry:component",
  "component": "DocxEditor",
  "variant": "mail-merge",
  "dependencies": [
    "@syncfusion/ej2-react-documenteditor",
    "@syncfusion/ej2-react-popups",
    "@syncfusion/ej2-react-navigations"
  ],
  "description": "Word document editor with mail-merge fields, insert-field dialog, and merge-document toolbar.",
  "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/js/docx-editor-mail-merge.md"
  },
  "files": [
    {
      "target": "src/components/docx-editor-mail-merge/DocxEditor.jsx",
      "content": "import { useEffect, useRef } from 'react';\nimport { DocumentEditorContainerComponent, Toolbar } from '@syncfusion/ej2-react-documenteditor';\nimport { mailmerge } from '../datasource/docx-editor-data';\nimport { DialogUtility, Dialog } from '@syncfusion/ej2-react-popups';\nDocumentEditorContainerComponent.Inject(Toolbar);\nconst MailMerge = () => {\n    useEffect(() => {\n        rendereComplete();\n    }, []);\n    let hostUrl = \"https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/\";\n    let container = useRef(null);\n    const onWrapText = (text) => {\n        let content = \"\";\n        let index = text.lastIndexOf(\" \");\n        content = text.slice(0, index);\n        text.slice(index);\n        content += '<div class=\"e-de-text-wrap\">' + text.slice(index) + \"</div>\";\n        return content;\n    };\n    let toolbarOptions = [\n        \"New\",\n        \"Open\",\n        \"Separator\",\n        \"Undo\",\n        \"Redo\",\n        \"Separator\",\n        {\n            prefixIcon: \"sf-icon-InsertMergeField\",\n            tooltipText: \"Insert Field\",\n            text: onWrapText(\"Insert Field\"),\n            id: \"InsertField\",\n        },\n        {\n            prefixIcon: \"sf-icon-FinishMerge\",\n            tooltipText: \"Merge Document\",\n            text: onWrapText(\"Merge Document\"),\n            id: \"MergeDocument\",\n        },\n        \"Separator\",\n        \"Image\",\n        \"Table\",\n        \"Hyperlink\",\n        \"Bookmark\",\n        \"TableOfContents\",\n        \"Separator\",\n        \"Header\",\n        \"Footer\",\n        \"PageSetup\",\n        \"PageNumber\",\n        \"Break\",\n        \"Separator\",\n        \"Find\",\n        \"Separator\",\n        \"Comments\",\n        \"TrackChanges\",\n        \"Separator\",\n        \"LocalClipboard\",\n        \"RestrictEditing\",\n        \"Separator\",\n        \"FormFields\",\n        \"UpdateFields\",\n    ];\n    const closeFieldDialog = () => {\n        insertFieldDialogObj.hide();\n        container.current?.documentEditor.focusIn();\n    };\n    let insertFieldDialogObj = new Dialog({\n        header: \"Merge Field\",\n        content: '<div class=\"dialogContent\">' +\n            // tslint:disable-next-line:max-line-length\n            '<label class=\"e-insert-field-label\">Name:</label></br><input type=\"text\" id=\"field_text\" class=\"e-input\" placeholder=\"Type a field to insert eg. FirstName\">' +\n            \"</div>\",\n        showCloseIcon: true,\n        isModal: true,\n        width: \"auto\",\n        height: \"auto\",\n        close: closeFieldDialog,\n        buttons: [\n            {\n                click: () => {\n                    let fieldNameTextBox = document.getElementById(\"field_text\");\n                    let fieldName = fieldNameTextBox.value;\n                    if (fieldName !== \"\" && container.current) {\n                        container.current.documentEditor.editor.insertField(\"MERGEFIELD \" + fieldName + \" \\\\* MERGEFORMAT\");\n                    }\n                    insertFieldDialogObj.hide();\n                    container.current?.documentEditor.focusIn();\n                },\n                buttonModel: {\n                    content: \"Ok\",\n                    cssClass: \"e-flat\",\n                    isPrimary: true,\n                },\n            },\n            {\n                click: () => {\n                    insertFieldDialogObj.hide();\n                    container.current?.documentEditor.focusIn();\n                },\n                buttonModel: {\n                    content: \"Cancel\",\n                    cssClass: \"e-flat\",\n                },\n            },\n        ],\n    });\n    const onLoadDefault = () => {\n        if (!container.current)\n            return;\n        container.current.documentEditor.open(JSON.stringify(mailmerge));\n        container.current.documentEditor.documentName = \"Mail Merge\";\n        container.current.documentEditorSettings.showRuler = true;\n        let item = toolbarOptions;\n        container.current.toolbarItems = item;\n        container.current.documentChange = () => {\n            container.current?.documentEditor.focusIn();\n        };\n        container.current.toolbarClick = (args) => {\n            switch (args.item.id) {\n                case \"MergeDocument\":\n                    mergeDocument();\n                    break;\n                case \"InsertField\":\n                    showInsertFielddialog(container);\n                    break;\n            }\n        };\n    };\n    const mergeDocument = () => {\n        if (!container.current)\n            return;\n        container.current.documentEditor.saveAsBlob(\"Docx\").then((_blob) => {\n            let fileReader = new FileReader();\n            fileReader.onload = () => {\n                let base64String = fileReader.result;\n                let responseData = {\n                    fileName: container.current?.documentEditor.documentName + \".docx\",\n                    documentData: base64String,\n                };\n                // let waitingPopUp = document.getElementById('waiting-popup');\n                // let inActiveDiv = document.getElementById('popup-overlay');\n                showHideWaitingIndicator(true);\n                let baseUrl = \"https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/MailMerge\";\n                let httpRequest = new XMLHttpRequest();\n                httpRequest.open(\"POST\", baseUrl, true);\n                httpRequest.setRequestHeader(\"Content-Type\", \"application/json;charset=UTF-8\");\n                httpRequest.onreadystatechange = () => {\n                    if (httpRequest.readyState === 4) {\n                        if (httpRequest.status === 200 || httpRequest.status === 304) {\n                            container.current?.documentEditor.open(httpRequest.responseText);\n                        }\n                        else {\n                            // Failed to merge document\n                            DialogUtility.alert({\n                                title: \"Information\",\n                                content: \"failure to merge document\",\n                                showCloseIcon: true,\n                                closeOnEscape: true,\n                            });\n                        }\n                        showHideWaitingIndicator(false);\n                    }\n                };\n                httpRequest.send(JSON.stringify(responseData));\n            };\n            fileReader.readAsDataURL(_blob);\n        });\n    };\n    const showHideWaitingIndicator = (show) => {\n        let waitingPopUp = document.getElementById(\"waiting-popup\");\n        let inActiveDiv = document.getElementById(\"popup-overlay\");\n        if (inActiveDiv)\n            inActiveDiv.style.display = show ? \"block\" : \"none\";\n        if (waitingPopUp)\n            waitingPopUp.style.display = show ? \"block\" : \"none\";\n    };\n    const showInsertFielddialog = (container) => {\n        if (document.getElementById(\"insert_merge_field\") === null ||\n            document.getElementById(\"insert_merge_field\") === undefined) {\n            let fieldcontainer = document.createElement(\"div\");\n            fieldcontainer.id = \"insert_merge_field\";\n            document.body.appendChild(fieldcontainer);\n            insertFieldDialogObj.appendTo(\"#insert_merge_field\");\n            fieldcontainer.parentElement.style.position = \"fixed\";\n            fieldcontainer.style.width = \"auto\";\n            fieldcontainer.style.height = \"auto\";\n        }\n        insertFieldDialogObj.close = () => {\n            container.documentEditor.focusIn();\n        };\n        insertFieldDialogObj.beforeOpen = () => {\n            container.documentEditor.focusIn();\n        };\n        insertFieldDialogObj.show();\n        let fieldNameTextBox = document.getElementById(\"field_text\");\n        fieldNameTextBox.value = \"\";\n    };\n    const rendereComplete = () => {\n        if (!container.current)\n            return;\n        container.current.documentEditor.pageOutline = \"#E0E0E0\";\n        container.current.documentEditor.acceptTab = true;\n        container.current.documentEditor.resize();\n        onLoadDefault();\n    };\n    return (<div className=\"control-pane\">\n            <div className=\"control-section\">\n                <div className=\"control-section\" style={{ paddingLeft: \"0px\", paddingRight: \"0px\", paddingTop: \"0px\" }}>\n                    <DocumentEditorContainerComponent id=\"container\" ref={container} style={{ display: \"block\" }} height={\"590px\"} serviceUrl={hostUrl} enableToolbar={true} locale=\"en-US\"/>\n                </div>\n            </div>\n\n            <div className=\"overlay\" id=\"popup-overlay\"></div>\n            <div id=\"waiting-popup\">\n                <svg className=\"circular\" height=\"40\" width=\"40\">\n                    <circle className=\"circle-path\" cx=\"25\" cy=\"25\" r=\"20\" fill=\"none\" strokeWidth=\"6\" strokeMiterlimit=\"10\"/>\n                </svg>\n            </div>\n        </div>);\n};\nexport default MailMerge;\n"
    }
  ]
}