1- // editor.js - TinyMDE editor functions
1+ // editor.ts - TinyMDE editor functions
22const TinyMDE = window . TinyMDE ;
33import { showToast } from "./notifications.js" ;
44import { showSavingIndicator , hideSavingIndicator } from "./ui.js" ;
55
6- let tinyMdeEditor = null ;
6+ let tinyMdeEditor : any = null ;
77
8- export function initializeTinyMDE ( debounceCallback , attempts = 3 ) {
9- const editorContainer = document . getElementById ( "tinymde-editor" ) ;
8+ export function initializeTinyMDE ( debounceCallback : ( ) => void , attempts : number = 3 ) : void {
9+ const editorContainer = document . getElementById ( "tinymde-editor" ) as HTMLElement ;
1010 if ( ! editorContainer ) {
1111 if ( attempts > 0 ) {
12- setTimeout ( ( ) => initializeTinyMDE ( attempts - 1 ) , 100 ) ;
12+ setTimeout ( ( ) => initializeTinyMDE ( debounceCallback , attempts - 1 ) , 100 ) ;
1313 return ;
1414 }
1515 showToast ( "Editor container not found." , "error" ) ;
@@ -51,75 +51,75 @@ export function initializeTinyMDE(debounceCallback, attempts = 3) {
5151 editor : tinyMdeEditor ,
5252 } ) ;
5353
54- const observer = new MutationObserver ( ( mutations ) => {
54+ const observer = new MutationObserver ( ( mutations : MutationRecord [ ] ) => {
5555 if (
5656 mutations . some (
5757 ( m ) =>
5858 m . type === "childList" ||
5959 ( m . type === "characterData" &&
60- m . target . parentNode ?. closest ( ".TinyMDE" ) )
60+ ( m . target as Element ) . parentElement ?. closest ( ".TinyMDE" ) )
6161 )
6262 ) {
63- if ( ! window . isSettingEditorContent ) debounceCallback ( ) ;
63+ if ( ! ( window as any ) . isSettingEditorContent ) debounceCallback ( ) ;
6464 }
6565 } ) ;
6666
67- const editorContent = editorContainer . querySelector ( ".TinyMDE" ) ;
67+ const editorContent = editorContainer . querySelector ( ".TinyMDE" ) as Element ;
6868 if ( editorContent ) {
6969 observer . observe ( editorContent , {
7070 childList : true ,
7171 subtree : true ,
7272 characterData : true ,
7373 } ) ;
7474 }
75- window . editorObserver = observer ;
75+ ( window as any ) . editorObserver = observer ;
7676 } catch ( error ) {
7777 console . error ( "Error initializing TinyMDE:" , error ) ;
7878 showToast ( "Error initializing editor." , "error" ) ;
7979 }
8080}
8181
82- export function setEditorContent ( content ) {
82+ export function setEditorContent ( content : string ) : void {
8383 if ( ! tinyMdeEditor ) return ;
8484 try {
85- window . isSettingEditorContent = true ;
85+ ( window as any ) . isSettingEditorContent = true ;
8686 tinyMdeEditor . setContent ( content || "" ) ;
87- setTimeout ( ( ) => ( window . isSettingEditorContent = false ) , 100 ) ;
87+ setTimeout ( ( ) => ( ( window as any ) . isSettingEditorContent = false ) , 100 ) ;
8888 } catch ( error ) {
8989 console . error ( "Error setting editor content:" , error ) ;
90- window . isSettingEditorContent = false ;
90+ ( window as any ) . isSettingEditorContent = false ;
9191 }
9292}
9393
94- export function getEditorContent ( ) {
94+ export function getEditorContent ( ) : string {
9595 return tinyMdeEditor ? tinyMdeEditor . getContent ( ) . trim ( ) : "" ;
9696}
9797
98- export function enableEditor ( ) {
98+ export function enableEditor ( ) : void {
9999 if ( tinyMdeEditor && typeof tinyMdeEditor . enable === "function" ) {
100100 tinyMdeEditor . enable ( ) ;
101101 }
102102}
103103
104- export function focusEditor ( ) {
104+ export function focusEditor ( ) : void {
105105 if ( tinyMdeEditor && tinyMdeEditor . codemirror ) {
106106 tinyMdeEditor . codemirror . focus ( ) ;
107107 }
108108}
109109
110- export function createDebounceAutosave ( handleSaveNoteCallback ) {
111- let timeout ;
110+ export function createDebounceAutosave ( handleSaveNoteCallback : ( showNotification : boolean ) => Promise < void > ) : ( ) => void {
111+ let timeout : number ;
112112 let lastContent = "" ;
113113 let lastTitle = "" ;
114114 let lastTags = "" ;
115- let saveQueue = [ ] ;
115+ let saveQueue : { currentContent : string ; currentTitle : string ; currentTags : string } [ ] = [ ] ;
116116 let isSaving = false ;
117117
118118 return ( ) => {
119119 clearTimeout ( timeout ) ;
120120 const currentContent = getEditorContent ( ) ;
121- const noteTitleInput = document . getElementById ( "noteTitle" ) ;
122- const noteTagsInput = document . getElementById ( "noteTags" ) ;
121+ const noteTitleInput = document . getElementById ( "noteTitle" ) as HTMLInputElement ;
122+ const noteTagsInput = document . getElementById ( "noteTags" ) as HTMLInputElement ;
123123 const currentTitle = noteTitleInput ? noteTitleInput . value . trim ( ) : "" ;
124124 const currentTags = noteTagsInput ? noteTagsInput . value . trim ( ) : "" ;
125125
@@ -135,7 +135,7 @@ export function createDebounceAutosave(handleSaveNoteCallback) {
135135 const processQueue = async ( ) => {
136136 if ( saveQueue . length === 0 || isSaving ) return ;
137137 isSaving = true ;
138- const addNoteBtn = document . getElementById ( "addNoteBtn" ) ;
138+ const addNoteBtn = document . getElementById ( "addNoteBtn" ) as HTMLButtonElement ;
139139 addNoteBtn . disabled = true ;
140140 addNoteBtn . setAttribute ( "aria-disabled" , "true" ) ;
141141 const timeoutId = setTimeout ( ( ) => {
@@ -147,10 +147,7 @@ export function createDebounceAutosave(handleSaveNoteCallback) {
147147 }
148148 } , 5000 ) ;
149149
150- const { currentContent, currentTitle, currentTags } = saveQueue . shift ( ) ;
151- const { showSavingIndicator, hideSavingIndicator } = await import (
152- "./ui.js"
153- ) ;
150+ const { currentContent, currentTitle, currentTags } = saveQueue . shift ( ) ! ;
154151 showSavingIndicator ( ) ;
155152 try {
156153 await handleSaveNoteCallback ( false ) ;
0 commit comments