1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/lib/SyncTargetRegistry.ts

111 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-08-16 16:20:14 +02:00
export interface SyncTargetInfo {
id: number;
name: string;
label: string;
supportsSelfHosted: boolean;
supportsConfigCheck: boolean;
description: string;
classRef: any;
}
// const syncTargetOrder = [
// 'joplinCloud',
// 'dropbox',
// 'onedrive',
// ];
2021-08-16 16:20:14 +02:00
export default class SyncTargetRegistry {
private static reg_: Record<number, SyncTargetInfo> = {};
private static get reg() {
2021-08-16 19:05:22 +02:00
// if (!this.reg_[0]) {
// this.reg_[0] = {
// id: 0,
// name: SyncTargetNone.targetName(),
// label: SyncTargetNone.label(),
// classRef: SyncTargetNone,
// description: SyncTargetNone.description(),
// supportsSelfHosted: false,
// supportsConfigCheck: false,
// };
// }
return this.reg_;
}
2021-08-16 16:20:14 +02:00
public static classById(syncTargetId: number) {
const info = SyncTargetRegistry.reg[syncTargetId];
2019-09-19 23:51:18 +02:00
if (!info) throw new Error(`Invalid id: ${syncTargetId}`);
return info.classRef;
}
2021-08-16 16:20:14 +02:00
public static infoByName(name: string): SyncTargetInfo {
for (const [, info] of Object.entries(this.reg)) {
2021-08-16 16:20:14 +02:00
if (info.name === name) return info;
}
throw new Error(`Unknown name: ${name}`);
}
public static addClass(SyncTargetClass: any) {
this.reg[SyncTargetClass.id()] = {
id: SyncTargetClass.id(),
name: SyncTargetClass.targetName(),
label: SyncTargetClass.label(),
classRef: SyncTargetClass,
2021-08-16 16:20:14 +02:00
description: SyncTargetClass.description(),
supportsSelfHosted: SyncTargetClass.supportsSelfHosted(),
supportsConfigCheck: SyncTargetClass.supportsConfigCheck(),
};
}
2021-08-16 16:20:14 +02:00
public static allIds() {
return Object.keys(this.reg);
}
2021-08-16 16:20:14 +02:00
public static nameToId(name: string) {
for (const n in this.reg) {
if (!this.reg.hasOwnProperty(n)) continue;
if (this.reg[n].name === name) return this.reg[n].id;
}
2021-06-03 17:12:07 +02:00
throw new Error(`Name not found: ${name}. Was the sync target registered?`);
}
2021-08-16 16:20:14 +02:00
public static idToMetadata(id: number) {
for (const n in this.reg) {
if (!this.reg.hasOwnProperty(n)) continue;
if (this.reg[n].id === id) return this.reg[n];
}
2019-09-19 23:51:18 +02:00
throw new Error(`ID not found: ${id}`);
}
2021-08-16 16:20:14 +02:00
public static idToName(id: number) {
return this.idToMetadata(id).name;
}
2021-08-16 16:20:14 +02:00
public static idAndLabelPlainObject(os: string) {
const output: Record<string, string> = {};
for (const n in this.reg) {
if (!this.reg.hasOwnProperty(n)) continue;
const info = this.reg[n];
if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) {
continue;
}
output[n] = info.label;
}
return output;
// const sorted: Record<string, string> = {};
// for (const o of syncTargetOrder) {
// sorted[o] = output[o];
// }
// for (const [name, value] of Object.entries(output)) {
// if (!sorted[name]) sorted[name] = value;
// }
// return sorted;
}
}