mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	client-ts: Port services/options
This commit is contained in:
		
							parent
							
								
									5875aa3bef
								
							
						
					
					
						commit
						78f929ee69
					
				@ -1,61 +0,0 @@
 | 
				
			|||||||
import server from "./server.js";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Options {
 | 
					 | 
				
			||||||
    constructor() {
 | 
					 | 
				
			||||||
        this.initializedPromise = server.get('options').then(data => this.load(data));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    load(arr) {
 | 
					 | 
				
			||||||
        this.arr = arr;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    get(key) {
 | 
					 | 
				
			||||||
        return this.arr[key];
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    getNames() {
 | 
					 | 
				
			||||||
        return Object.keys(this.arr);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    getJson(key) {
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            return JSON.parse(this.arr[key]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        catch (e) {
 | 
					 | 
				
			||||||
            return null;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    getInt(key) {
 | 
					 | 
				
			||||||
        return parseInt(this.arr[key]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    getFloat(key) {
 | 
					 | 
				
			||||||
        return parseFloat(this.arr[key]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    is(key) {
 | 
					 | 
				
			||||||
        return this.arr[key] === 'true';
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    set(key, value) {
 | 
					 | 
				
			||||||
        this.arr[key] = value;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async save(key, value) {
 | 
					 | 
				
			||||||
        this.set(key, value);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const payload = {};
 | 
					 | 
				
			||||||
        payload[key] = value;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        await server.put(`options`, payload);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    async toggle(key) {
 | 
					 | 
				
			||||||
        await this.save(key, (!this.is(key)).toString());
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const options = new Options();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default options;
 | 
					 | 
				
			||||||
							
								
								
									
										79
									
								
								src/public/app/services/options.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/public/app/services/options.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,79 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					import server from "./server.js";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type OptionValue = string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Options {
 | 
				
			||||||
 | 
					    private initializedPromise: Promise<void>;
 | 
				
			||||||
 | 
					    private arr!: Record<string, OptionValue>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    constructor() {
 | 
				
			||||||
 | 
					        this.initializedPromise = server.get<Record<string, OptionValue>>('options').then(data => this.load(data));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    load(arr: Record<string, OptionValue>) {
 | 
				
			||||||
 | 
					        this.arr = arr;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get(key: string) {
 | 
				
			||||||
 | 
					        return this.arr?.[key];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getNames() {
 | 
				
			||||||
 | 
					        return Object.keys(this.arr || []);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getJson(key: string) {                
 | 
				
			||||||
 | 
					        const value = this.arr?.[key];
 | 
				
			||||||
 | 
					        if (typeof value !== "string") {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            return JSON.parse(value);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        catch (e) {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getInt(key: string) {
 | 
				
			||||||
 | 
					        const value = this.arr?.[key];
 | 
				
			||||||
 | 
					        if (typeof value !== "string") {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return parseInt(value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getFloat(key: string) {
 | 
				
			||||||
 | 
					        const value = this.arr?.[key];
 | 
				
			||||||
 | 
					        if (typeof value !== "string") {
 | 
				
			||||||
 | 
					            return null;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return parseFloat(value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    is(key: string) {
 | 
				
			||||||
 | 
					        return this.arr[key] === 'true';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    set(key: string, value: OptionValue) {
 | 
				
			||||||
 | 
					        this.arr[key] = value;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async save(key: string, value: OptionValue) {
 | 
				
			||||||
 | 
					        this.set(key, value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const payload: Record<string, OptionValue> = {};
 | 
				
			||||||
 | 
					        payload[key] = value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await server.put(`options`, payload);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    async toggle(key: string) {
 | 
				
			||||||
 | 
					        await this.save(key, (!this.is(key)).toString());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const options = new Options();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default options;
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user