1+ import { byId , createStatusSetter , debounce } from './utils.js' ;
2+
13export function initCracita ( ) {
2- const cracitaPage = document . getElementById ( 'page-cracita' ) ;
4+ const cracitaPage = byId ( 'page-cracita' ) ;
35 if ( ! cracitaPage ) return ;
46
57 const els = {
6- input : document . getElementById ( 'cracita-inputText' ) ,
7- output : document . getElementById ( 'cracita-outputText' ) ,
8- loadBtn : document . getElementById ( 'cracita-loadBtn' ) ,
9- saveBtn : document . getElementById ( 'cracita-saveBtn' ) ,
10- copyBtn : document . getElementById ( 'cracita-copyBtn' ) ,
11- fileInput : document . getElementById ( 'cracita-fileInput' ) ,
12- status : document . getElementById ( 'cracita-statusField' ) ,
13- stats : document . getElementById ( 'cracita-statsField' ) ,
14- mode : document . getElementById ( 'cracita-mode-select' ) ,
15- regexControls : document . getElementById ( 'cracita-regex-controls' ) ,
16- regexInput : document . getElementById ( 'cracita-regex-input' )
8+ input : byId ( 'cracita-inputText' ) ,
9+ output : byId ( 'cracita-outputText' ) ,
10+ loadBtn : byId ( 'cracita-loadBtn' ) ,
11+ saveBtn : byId ( 'cracita-saveBtn' ) ,
12+ copyBtn : byId ( 'cracita-copyBtn' ) ,
13+ fileInput : byId ( 'cracita-fileInput' ) ,
14+ status : byId ( 'cracita-statusField' ) ,
15+ stats : byId ( 'cracita-statsField' ) ,
16+ mode : byId ( 'cracita-mode-select' ) ,
17+ regexControls : byId ( 'cracita-regex-controls' ) ,
18+ regexInput : byId ( 'cracita-regex-input' )
1719 } ;
18-
19- let conversionTimer = null ;
20+ const setStatus = createStatusSetter ( els . status ) ;
2021
2122 const applyTypographicRules = ( text ) => {
2223 if ( ! text ) return "" ;
@@ -42,11 +43,11 @@ export function initCracita() {
4243
4344 if ( mode === 'Normal' ) {
4445 output = applyTypographicRules ( input ) ;
45- els . status . textContent = "Auto-converted" ;
46+ setStatus ( "Auto-converted" ) ;
4647 } else if ( mode === 'Regex' ) {
4748 if ( ! els . regexInput . value ) {
4849 els . output . value = input ;
49- els . status . textContent = "Waiting for pattern..." ;
50+ setStatus ( "Waiting for pattern..." ) ;
5051 return ;
5152 }
5253 try {
@@ -64,10 +65,10 @@ export function initCracita() {
6465 }
6566 return match ;
6667 } ) ;
67- els . status . textContent = "Targeted conversion complete." ;
68+ setStatus ( "Targeted conversion complete." ) ;
6869 } catch ( e ) {
6970 output = input ;
70- els . status . textContent = `Regex error: ${ e . message } ` ;
71+ setStatus ( `Regex error: ${ e . message } ` ) ;
7172 }
7273 }
7374
@@ -81,13 +82,10 @@ export function initCracita() {
8182 els . stats . textContent = `Words: ${ wordCount } | Chars: ${ text . length } ` ;
8283 } ;
8384
84- const onTextChange = ( ) => {
85- clearTimeout ( conversionTimer ) ;
86- conversionTimer = setTimeout ( ( ) => {
87- performConversion ( ) ;
88- updateStats ( ) ;
89- } , 300 ) ;
90- } ;
85+ const onTextChange = debounce ( ( ) => {
86+ performConversion ( ) ;
87+ updateStats ( ) ;
88+ } , 300 ) ;
9189
9290 els . mode . addEventListener ( 'change' , ( ) => {
9391 const isRegex = els . mode . value === 'Regex' ;
@@ -109,18 +107,18 @@ export function initCracita() {
109107 const blob = new Blob ( [ els . output . value ] , { type : "text/plain" } ) ;
110108 if ( typeof saveAs !== 'undefined' ) {
111109 saveAs ( blob , "converted_text.txt" ) ;
112- els . status . textContent = "File saved." ;
110+ setStatus ( "File saved." ) ;
113111 } else {
114112 console . error ( "FileSaver.js (saveAs) is missing." ) ;
115- els . status . textContent = "Error: Save dependency missing." ;
113+ setStatus ( "Error: Save dependency missing." ) ;
116114 }
117115 } ) ;
118116
119117 els . copyBtn . addEventListener ( 'click' , ( ) => {
120118 if ( ! els . output . value ) return ;
121119 navigator . clipboard . writeText ( els . output . value )
122- . then ( ( ) => els . status . textContent = "Copied to clipboard!" )
123- . catch ( ( ) => els . status . textContent = "Copy failed." ) ;
120+ . then ( ( ) => setStatus ( "Copied to clipboard!" ) )
121+ . catch ( ( ) => setStatus ( "Copy failed." ) ) ;
124122 } ) ;
125123
126124 els . fileInput . addEventListener ( 'change' , ( e ) => {
@@ -130,7 +128,7 @@ export function initCracita() {
130128 reader . onload = ( ev ) => {
131129 els . input . value = ev . target . result ;
132130 onTextChange ( ) ;
133- els . status . textContent = `Loaded: ${ file . name } ` ;
131+ setStatus ( `Loaded: ${ file . name } ` ) ;
134132 els . fileInput . value = '' ;
135133 } ;
136134 reader . readAsText ( file ) ;
0 commit comments