@@ -25,6 +25,14 @@ const defaultCustomTextSettings: CustomTextSettings = {
2525 pipeDelimiter : false ,
2626} ;
2727
28+ let cachedCurrentText = defaultCustomTextSettings . text ;
29+ export async function initCurrentText ( ) : Promise < void > {
30+ const db = await getDB ( ) ;
31+ cachedCurrentText =
32+ ( await db . get ( "currentText" , "_currentText_" ) ) ??
33+ defaultCustomTextSettings . text ;
34+ }
35+
2836const customTextSettings = new LocalStorageWithSchema ( {
2937 key : "customTextSettings" ,
3038 schema : CustomTextSettingsSchema ,
@@ -59,6 +67,7 @@ type CustomTextDB = DBSchema & {
5967 progress : number ;
6068 } ;
6169 } ;
70+ currentText : { key : "_currentText_" ; value : string [ ] } ;
6271} ;
6372
6473export async function getDB ( ) : Promise < IDBPDatabase < CustomTextDB > > {
@@ -89,11 +98,11 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
8998 //create objectStores
9099 await db . createObjectStore ( "customTexts" ) ;
91100 await db . createObjectStore ( "customLongTexts" ) ;
92- // await db.createObjectStore("currentSettings ");
101+ await db . createObjectStore ( "currentText " ) ;
93102
94103 const ctStore = tx . objectStore ( "customTexts" ) ;
95104 const longCtStore = tx . objectStore ( "customLongTexts" ) ;
96- // const currentSettingsStore = tx.objectStore("currentSettings ");
105+ const currentTextStore = tx . objectStore ( "currentText " ) ;
97106
98107 //copy from old localStorage
99108 await Promise . all ( [
@@ -103,10 +112,7 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
103112 ...Object . entries ( customTextLongLS . get ( ) ) . map ( async ( [ key , value ] ) =>
104113 longCtStore . add ( { text : value . text , progress : value . progress } , key )
105114 ) ,
106- /* currentSettingsStore.put(
107- customTextSettings.get(),
108- "_currentSettings_"
109- ),*/
115+ currentTextStore . put ( customTextSettings . get ( ) . text , "_currentText_" ) ,
110116 tx . done ,
111117 ] ) ;
112118
@@ -115,6 +121,10 @@ export async function getDB(): Promise<IDBPDatabase<CustomTextDB>> {
115121 //customTextLS.destroy();
116122 //customTextLongLS.destroy();
117123 //customTextSettings.destroy();
124+ customTextSettings . set ( {
125+ ...customTextSettings . get ( ) ,
126+ text : [ "outdated" ] ,
127+ } ) ;
118128 }
119129 } ,
120130 } ) ;
@@ -124,19 +134,19 @@ window.globalThis["db"] = {
124134 get : getDB ,
125135 getText : getCustomText ,
126136 setText : setCustomText ,
137+ getData : getData ,
127138} ;
128139
129140export function getText ( ) : string [ ] {
130- return customTextSettings . get ( ) . text ;
141+ return cachedCurrentText ;
131142}
132143
133144export function setText ( txt : string [ ] ) : void {
134- const currentSettings = customTextSettings . get ( ) ;
135- customTextSettings . set ( {
136- ...currentSettings ,
137- text : txt ,
138- limit : { value : txt . length , mode : currentSettings . limit . mode } ,
139- } ) ;
145+ cachedCurrentText = txt ;
146+ setTimeout ( async ( ) => {
147+ const db = await getDB ( ) ;
148+ await db . put ( "currentText" , cachedCurrentText , "_currentText_" ) ;
149+ } , 0 ) ;
140150}
141151
142152export function getMode ( ) : CustomTextMode {
@@ -197,7 +207,7 @@ export function setPipeDelimiter(val: boolean): void {
197207}
198208
199209export function getData ( ) : CustomTextSettings {
200- return customTextSettings . get ( ) ;
210+ return { ... customTextSettings . get ( ) , text : cachedCurrentText } ;
201211}
202212
203213export async function getCustomText (
0 commit comments