1+ import { useEffect , useRef } from "react" ;
2+ import { DocumentEditorContainerComponent , Toolbar as DocumentEditorToolbar } from "@syncfusion/ej2-react-documenteditor" ;
3+ import TitleBar from "./Titlebar" ;
4+
5+ // Inject the toolbar to the document editor component
6+ DocumentEditorContainerComponent . Inject ( DocumentEditorToolbar ) ;
7+
8+ function DocumentEditor ( { user, onLogout } ) {
9+ let defaultDocument = '' ;
10+ // Configure toolbar items based on user role
11+ const toolbarConfig = {
12+ Lawyer : [ "New" , "Open" , "Separator" , "Undo" , "Redo" , "Separator" , "Image" , "Table" , "Hyperlink" , "Bookmark" , "TableOfContents" , "Separator" , "Header" , "Footer" , "PageSetup" , "PageNumber" , "Break" , "InsertFootnote" , "InsertEndnote" , "Separator" , "Find" , "Separator" , "Comments" , "TrackChanges" , "LocalClipboard" , "RestrictEditing" , "Separator" , "FormFields" , "UpdateFields" , "ContentControl" , "XML Mapping" ] ,
13+ Paralegal : [ "New" , "Open" , "Separator" , "Undo" , "Redo" , "Separator" , "Image" , "Table" , "Hyperlink" , "Bookmark" , "TableOfContents" , "Separator" , "Header" , "Footer" , "PageSetup" , "PageNumber" , "Break" , "InsertFootnote" , "InsertEndnote" , "Separator" , "Find" , "Separator" , "Comments" , "TrackChanges" , "LocalClipboard" , "RestrictEditing" , "Separator" , "FormFields" , "UpdateFields" , "ContentControl" , "XML Mapping" ] ,
14+ Client : [ "Comments" , "Find" ] ,
15+ Reviewer : [ "Comments" , "Find" , "TrackChanges" ] ,
16+ Admin : [ "New" , "Open" , "Separator" , "Undo" , "Redo" , "Separator" , "Image" , "Table" , "Hyperlink" , "Bookmark" , "TableOfContents" , "Separator" , "Header" , "Footer" , "PageSetup" , "PageNumber" , "Break" , "InsertFootnote" , "InsertEndnote" , "Separator" , "Find" , "Separator" , "Comments" , "TrackChanges" , "LocalClipboard" , "RestrictEditing" , "Separator" , "FormFields" , "UpdateFields" , "ContentControl" , "XML Mapping" ]
17+ } ;
18+
19+ // Create references for title bar and container
20+ const titleBarRef = useRef ( null ) ;
21+ const containerRef = useRef ( null ) ;
22+
23+ // Effect to initialize or update the document editor when the user changes
24+ useEffect ( ( ) => {
25+ if ( user && containerRef . current ) {
26+ convertDocxToSfdt ( ) ;
27+ containerRef . current . documentEditor . documentName = "Document" ;
28+ containerRef . current . documentEditor . currentUser = user . email ;
29+
30+ // Update document title when changes are made
31+ containerRef . current . documentChange = ( ) => {
32+ titleBarRef . current . updateDocumentTitle ( ) ;
33+ containerRef . current . documentEditor . focusIn ( ) ;
34+ } ;
35+ if ( ! titleBarRef . current ) {
36+ titleBarRef . current = new TitleBar ( document . getElementById ( "documenteditor_titlebar" ) , containerRef . current . documentEditor , true ) ;
37+ titleBarRef . current . updateDocumentTitle ( ) ;
38+ }
39+ containerRef . current . toolbarItems = toolbarConfig [ user . username ] ;
40+ }
41+ } , [ user ] ) ;
42+
43+ // Convert GitHub Raw document to SFDT and load in Editor.
44+ const convertDocxToSfdt = async ( ) => {
45+ try {
46+ const docxResponse = await fetch ( 'https://raw.githubusercontent.com/SyncfusionExamples/Role-Based-Toolbar-Customization-in-Syncfusion-Document-Editor-for-Legal-Workflows/master/Client-side/public/docs/Legal_Notice.docx' ) ;
47+ const docxBlob = await docxResponse . blob ( ) ;
48+
49+ const formData = new FormData ( ) ;
50+ formData . append ( 'files' , docxBlob , 'Legal_Notice.docx' ) ;
51+
52+ const importResponse = await fetch ( 'https://ej2services.syncfusion.com/production/web-services/api/documenteditor/Import' , {
53+ method : 'POST' ,
54+ body : formData ,
55+ } ) ;
56+
57+ if ( importResponse . ok ) {
58+ defaultDocument = await importResponse . text ( ) ;
59+ containerRef . current . documentEditor . open ( defaultDocument ) ;
60+ } else {
61+ console . error ( `Failed to import document: ${ importResponse . statusText } ` ) ;
62+ }
63+ } catch ( error ) {
64+ console . error ( 'Error converting document:' , error ) ;
65+ }
66+ } ;
67+
68+ return (
69+ < div >
70+ < div className = "main-titlebar" style = { { display : "flex" , justifyContent : "space-between" , padding : "8px 12px" , alignItems : "center" , background : "#f0f0f0" } } >
71+ < p className = "welcome-text" style = { { margin : 0 } } > Welcome, { user . username || user . email } </ p >
72+ < button className = "logout-btn" onClick = { onLogout } > Logout</ button >
73+ </ div >
74+ < div id = "documenteditor_titlebar" className = "e-de-ctn-title" > </ div >
75+ < style >
76+ { `.e-toolbar-items {
77+ display: flex !important;
78+ justify-content: center !important;
79+ }` }
80+ </ style >
81+ < DocumentEditorContainerComponent
82+ ref = { containerRef }
83+ id = "container"
84+ height = "calc(100vh - 92px)"
85+ enableToolbar = { true }
86+ />
87+ </ div >
88+ ) ;
89+ }
90+
91+ export default DocumentEditor ;
0 commit comments