@@ -6,41 +6,38 @@ import FileManager from './FileManager';
66DocumentEditorContainerComponent . Inject ( DocumentEditorToolbar ) ;
77
88const DocumentEditor = ( ) => {
9- const [ maxDocId , setMaxDocId ] = useState ( null ) ;
10- const containerRef = useRef ( null ) ;
11- const [ selectedDocId , setSelectedDocId ] = useState ( null ) ;
12- const [ selectedDocName , setSelectedDocName ] = useState ( null ) ;
13- const [ showDialog , setShowDialog ] = useState ( false ) ;
14- const [ showFileManager , setShowFileManager ] = React . useState ( true ) ;
15- const fileManagerRef = useRef ( null ) ;
16- const inputRef = useRef ( null ) ;
17- const contentChanged = useRef ( false ) ;
18- const [ errorMessage , setErrorMessage ] = useState ( '' ) ;
19- const randomDefaultName = 'New Document'
20-
21- const newToolItem = {
22- prefixIcon : "e-de-ctnr-new" ,
23- tooltipText : "New" ,
24- text : "New" ,
25- id : "CreateNewDoc"
26- } ;
27- const openToolItem = {
28- prefixIcon : "e-de-ctnr-open" ,
29- tooltipText : "Open file manager" ,
30- text : "Open" ,
31- id : "OpenFileManager"
32- } ;
33- const downloadToolItem = {
34- prefixIcon : "e-de-ctnr-download" ,
35- tooltipText : "Download" ,
36- text : "Download" ,
37- id : "DownloadToLocal"
38- } ;
9+ // State and refs
10+ const [ maxDocId , setMaxDocId ] = useState ( null ) ; // Track maximum doc ID for new document creation
11+ const containerRef = useRef ( null ) ; // Reference to DocumentEditor container
12+ const [ selectedDocId , setSelectedDocId ] = useState ( null ) ; // Selected document ID
13+ const [ selectedDocName , setSelectedDocName ] = useState ( null ) ; // Selected document name
14+ const [ showDialog , setShowDialog ] = useState ( false ) ; // Controls "New Document" dialog
15+ const [ showFileManager , setShowFileManager ] = useState ( true ) ; // Controls FileManager dialog visibility
16+ const fileManagerRef = useRef ( null ) ; // Reference to FileManager
17+ const inputRef = useRef ( null ) ; // Input reference for new document name
18+ const contentChanged = useRef ( false ) ; // Flag to track content changes
19+ const [ errorMessage , setErrorMessage ] = useState ( '' ) ; // Error message for name validation
20+ const randomDefaultName = 'New_Document' ; // Default document name
21+
22+ // Toolbar custom items
23+ const newToolItem = { prefixIcon : "e-de-ctnr-new" , tooltipText : "New" , text : "New" , id : "CreateNewDoc" } ;
24+ const openToolItem = { prefixIcon : "e-de-ctnr-open" , tooltipText : "Open file manager" , text : "Open" , id : "OpenFileManager" } ;
25+ const downloadToolItem = { prefixIcon : "e-de-ctnr-download" , tooltipText : "Download" , text : "Download" , id : "DownloadToLocal" } ;
3926
4027 const hostUrl = "https://localhost:44305/" ;
41- const toolbarItems = [ newToolItem , openToolItem , downloadToolItem , 'Separator' , 'Undo' , 'Redo' , 'Separator' , 'Image' , 'Table' , 'Hyperlink' , 'Bookmark' , 'TableOfContents' , 'Separator' , 'Header' , 'Footer' , 'PageSetup' , 'PageNumber' , 'Break' , 'InsertFootnote' , 'InsertEndnote' , 'Separator' , 'Find' , 'Separator' , 'Comments' , 'TrackChanges' , 'Separator' , 'LocalClipboard' , 'RestrictEditing' , 'Separator' , 'FormFields' , 'UpdateFields' , 'ContentControl' ] ;
4228
43- const SaveDocument = ( ) => {
29+ // Complete toolbar items list
30+ const toolbarItems = [
31+ newToolItem , openToolItem , downloadToolItem , 'Separator' ,
32+ 'Undo' , 'Redo' , 'Separator' , 'Image' , 'Table' , 'Hyperlink' , 'Bookmark' ,
33+ 'TableOfContents' , 'Separator' , 'Header' , 'Footer' , 'PageSetup' , 'PageNumber' ,
34+ 'Break' , 'InsertFootnote' , 'InsertEndnote' , 'Separator' , 'Find' , 'Separator' ,
35+ 'Comments' , 'TrackChanges' , 'Separator' , 'LocalClipboard' , 'RestrictEditing' ,
36+ 'Separator' , 'FormFields' , 'UpdateFields' , 'ContentControl'
37+ ] ;
38+
39+ // Save document to the server as base64
40+ const autoSaveDocument = ( ) => {
4441 const editor = containerRef . current ?. documentEditor ;
4542 editor . saveAsBlob ( 'Docx' ) . then ( ( blob ) => {
4643 const reader = new FileReader ( ) ;
@@ -52,13 +49,8 @@ const DocumentEditor = () => {
5249 try {
5350 await fetch ( hostUrl + `api/documents/${ selectedDocId } /saveDocumentAsync` , {
5451 method : 'POST' ,
55- headers : {
56- 'Content-Type' : 'application/json'
57- } ,
58- body : JSON . stringify ( {
59- Base64Content : base64Data ,
60- FileName : fileName
61- } )
52+ headers : { 'Content-Type' : 'application/json' } ,
53+ body : JSON . stringify ( { Base64Content : base64Data , FileName : fileName } )
6254 } ) ;
6355 } catch ( err ) {
6456 console . error ( 'Auto-save error:' , err ) ;
@@ -68,7 +60,8 @@ const DocumentEditor = () => {
6860 } ) ;
6961 } ;
7062
71- const handleDialogSave = async ( ) => {
63+ // Create new document logic from dialog
64+ const handleFileNamePromptOk = async ( ) => {
7265 const documentName = inputRef . current ?. value ?. trim ( ) ;
7366 if ( ! documentName ) {
7467 setErrorMessage ( "Document name cannot be empty." ) ;
@@ -86,7 +79,7 @@ const DocumentEditor = () => {
8679 return ;
8780 }
8881
89- // Proceed to save
82+ // Proceed with creation
9083 setErrorMessage ( "" ) ;
9184 setShowDialog ( false ) ;
9285 const newId = maxDocId + 1 ;
@@ -97,7 +90,7 @@ const DocumentEditor = () => {
9790 containerRef . current . documentEditor . openBlank ( ) ;
9891 } ;
9992
100- // Check if a document with a given name already exists on the Azure storage
93+ // Check if a document with a given name already exists on database
10194 const checkDocumentExistence = async ( fileName ) => {
10295 try {
10396 const response = await fetch ( hostUrl + 'api/documents/CheckDocumentExistence' , {
@@ -116,9 +109,11 @@ const DocumentEditor = () => {
116109 }
117110 } ;
118111
112+ // Handle toolbar item clicks
119113 const handleToolbarItemClick = ( args ) => {
120114 let documentName = containerRef . current . documentEditor . documentName ;
121115 const baseDocName = documentName . replace ( / \. [ ^ / . ] + $ / , '' ) ;
116+
122117 switch ( args . item . id ) {
123118 case 'CreateNewDoc' :
124119 setSelectedDocId ( 0 ) ;
@@ -146,26 +141,31 @@ const DocumentEditor = () => {
146141 }
147142 } ;
148143
144+ // Auto-save effect runs every second
149145 React . useEffect ( ( ) => {
150146 const intervalId = setInterval ( ( ) => {
151147 if ( contentChanged . current ) {
152- SaveDocument ( ) ;
148+ autoSaveDocument ( ) ;
153149 contentChanged . current = false ;
154150 }
155151 } , 1000 ) ;
156152 return ( ) => clearInterval ( intervalId ) ;
157153 } ) ;
158154
155+ // Auto-focus on dialog input
159156 React . useEffect ( ( ) => {
160- if ( showDialog && inputRef . current ) {
157+ if ( showDialog && inputRef . current ) {
161158 inputRef . current . focus ( ) ;
162159 inputRef . current . select ( ) ;
163160 }
164- } , [ showDialog ] ) ;
161+ } , [ showDialog ] ) ;
165162
163+ // Handle content changes
166164 const handleContentChange = ( ) => {
167165 contentChanged . current = true ;
168166 } ;
167+
168+ // Load document from FileManager selection
169169 const loadFileFromFileManager = ( fileId , fileName ) => {
170170 setSelectedDocId ( fileId ) ;
171171 containerRef . current . documentEditor . documentName = fileName ;
@@ -174,6 +174,7 @@ const DocumentEditor = () => {
174174
175175 return (
176176 < div >
177+ { /* FileManager dialog for opening files */ }
177178 < FileManager
178179 onFileSelect = { loadFileFromFileManager }
179180 onFileManagerLoaded = { ( id ) => setMaxDocId ( id ) }
@@ -182,9 +183,13 @@ const DocumentEditor = () => {
182183 visible = { showFileManager }
183184 setVisible = { setShowFileManager }
184185 />
186+
187+ { /* Document name display */ }
185188 < div id = "document-header" >
186189 { selectedDocName || ( inputRef ?. current ?. value ? inputRef . current . value + '.docx' : '' ) }
187190 </ div >
191+
192+ { /* Main document editor container */ }
188193 < DocumentEditorContainerComponent
189194 ref = { containerRef }
190195 height = "calc(100vh - 65px)"
@@ -194,6 +199,8 @@ const DocumentEditor = () => {
194199 toolbarClick = { handleToolbarItemClick }
195200 contentChange = { handleContentChange }
196201 />
202+
203+ { /* Dialog for creating new documents */ }
197204 < DialogComponent
198205 visible = { showDialog }
199206 header = 'New Document'
@@ -203,8 +210,8 @@ const DocumentEditor = () => {
203210 close = { ( ) => setShowDialog ( false ) }
204211 buttons = { [
205212 {
206- click : handleDialogSave ,
207- buttonModel : { content : 'Save ' , isPrimary : true }
213+ click : handleFileNamePromptOk ,
214+ buttonModel : { content : 'Ok ' , isPrimary : true }
208215 } ,
209216 {
210217 click : ( ) => setShowDialog ( false ) ,
0 commit comments