mirror of
https://github.com/keymanapp/keyman.git
synced 2026-08-07 09:25:33 +00:00
Fixes #6297. Five issues fixed from #6297. Note that the fix for point 5 (`font-family` should be keyboard-specific, and it is not being used per-keyboard in the keyboard registration yet) opts to use a cleaned identifier for the font filename, but not one that is keyboard-specific, at this time.
121 lines
2.8 KiB
TypeScript
121 lines
2.8 KiB
TypeScript
import fs = require('fs');
|
|
import { configuration } from './config';
|
|
|
|
export interface DebugObject {
|
|
id: string;
|
|
filename: string;
|
|
sha256: string;
|
|
lastUse: Date;
|
|
filenameFromId(id: string): string;
|
|
};
|
|
|
|
export class DebugKeyboard implements DebugObject {
|
|
id: string;
|
|
filename: string;
|
|
sha256: string;
|
|
lastUse: Date;
|
|
path: string;
|
|
version: string;
|
|
oskFontFace?: string;
|
|
fontFace?: string;
|
|
|
|
filenameFromId(id: string): string {
|
|
return id + '.js';
|
|
}
|
|
|
|
toRegistrationBlob() {
|
|
let o = {
|
|
KN: this.id,
|
|
KI: 'Keyboard_'+this.id,
|
|
KL: this.id,
|
|
KLC: 'en',
|
|
KR: 'Europe',
|
|
KRC: 'eu',
|
|
KFont: this.fontFace ? { family: this.fontFace } : undefined,
|
|
KOskFont: this.oskFontFace ? { family: this.oskFontFace } : undefined,
|
|
KF: this.id+'.js'
|
|
};
|
|
|
|
return JSON.stringify(o, null, 2) + '/*'+this.sha256+'*/';
|
|
};
|
|
};
|
|
|
|
export class DebugModel implements DebugObject {
|
|
id: string;
|
|
filename: string;
|
|
sha256: string;
|
|
lastUse: Date;
|
|
|
|
filenameFromId(id: string): string {
|
|
return id + '.model.js';
|
|
}
|
|
|
|
toRegistrationBlob() {
|
|
return JSON.stringify(this.id) + ', ' + JSON.stringify(this.id + '.model.js') + '/*'+this.sha256+'*/';
|
|
}
|
|
};
|
|
|
|
export class DebugPackage implements DebugObject {
|
|
id: string;
|
|
filename: string;
|
|
sha256: string;
|
|
lastUse: Date;
|
|
name: string;
|
|
|
|
filenameFromId(id: string): string {
|
|
return id + '.kmp';
|
|
}
|
|
};
|
|
|
|
export class DebugFont implements DebugObject {
|
|
id: string;
|
|
filename: string;
|
|
sha256: string;
|
|
lastUse: Date;
|
|
facename: string;
|
|
|
|
filenameFromId(id: string): string {
|
|
return simplifyId(id) + '.ttf';
|
|
}
|
|
};
|
|
|
|
export class SiteData {
|
|
keyboards: { [ id: string ]: DebugKeyboard } = {};
|
|
models: { [ id: string ]: DebugModel } = {};
|
|
packages: { [ id: string ]: DebugPackage } = {};
|
|
fonts: { [ id: string ]: DebugFont } = {};
|
|
|
|
constructor() {
|
|
this.loadState();
|
|
}
|
|
|
|
private loadDebugObject(type: any, source: any, dest: any) {
|
|
for(let id in source) {
|
|
dest[id] = Object.assign(new type(), source[id]);
|
|
}
|
|
}
|
|
|
|
private loadState() {
|
|
const state = fs.existsSync(configuration.cacheStateFilename) ? JSON.parse(fs.readFileSync(configuration.cacheStateFilename, 'utf-8')) : null;
|
|
this.loadDebugObject(DebugKeyboard, state?.keyboards, this.keyboards);
|
|
this.loadDebugObject(DebugModel, state?.models, this.models);
|
|
this.loadDebugObject(DebugFont, state?.fonts, this.fonts);
|
|
this.loadDebugObject(DebugPackage, state?.packages, this.packages);
|
|
// todo cleanup of missing files
|
|
}
|
|
|
|
public saveState() {
|
|
fs.writeFileSync(configuration.cacheStateFilename, JSON.stringify(this, null, 2), 'utf-8');
|
|
}
|
|
};
|
|
|
|
export let data: SiteData = new SiteData();
|
|
|
|
export function isValidId(id: string) {
|
|
return !id.match(/\.\.|\/|\\/);
|
|
}
|
|
|
|
export function simplifyId(id: string) {
|
|
return id.replace(/[^_a-zA-Z0-9-]/g, '_');
|
|
}
|
|
|