1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/SyncTargetRegistry.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

import type BaseSyncTarget from './BaseSyncTarget';
2021-08-16 16:20:14 +02:00
export interface SyncTargetInfo {
id: number;
name: string;
label: string;
supportsSelfHosted: boolean;
supportsConfigCheck: boolean;
supportsRecursiveLinkedNotes: boolean;
supportsShare: boolean;
2021-08-16 16:20:14 +02:00
description: string;
classRef: typeof BaseSyncTarget;
2021-08-16 16:20:14 +02:00
}
export default class SyncTargetRegistry {
private static reg_: Record<number, typeof BaseSyncTarget> = {};
2021-08-16 16:20:14 +02:00
private static get reg() {
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;
}
2021-08-16 16:20:14 +02:00
public static infoByName(name: string): SyncTargetInfo {
for (const [, SyncTargetClass] of Object.entries(this.reg)) {
if (SyncTargetClass.targetName() === name) {
const output: SyncTargetInfo = {
id: SyncTargetClass.id(),
name: SyncTargetClass.targetName(),
label: SyncTargetClass.label(),
classRef: SyncTargetClass,
description: SyncTargetClass.description(),
supportsSelfHosted: SyncTargetClass.supportsSelfHosted(),
supportsConfigCheck: SyncTargetClass.supportsConfigCheck(),
supportsRecursiveLinkedNotes: SyncTargetClass.supportsRecursiveLinkedNotes(),
supportsShare: SyncTargetClass.supportsShare(),
};
return output;
}
2021-08-16 16:20:14 +02:00
}
throw new Error(`Unknown name: ${name}`);
}
public static infoById(id: number): SyncTargetInfo {
return this.infoByName(this.idToName(id));
}
public static addClass(SyncTargetClass: typeof BaseSyncTarget) {
this.reg[SyncTargetClass.id()] = SyncTargetClass;
}
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].targetName() === 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) {
return this.infoById(id);
}
2021-08-16 16:20:14 +02:00
public static idToName(id: number) {
return this.reg[id].targetName();
}
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.infoById(this.reg[n].id());
if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) {
continue;
}
output[n] = info.label;
}
return output;
}
public static optionsOrder(): string[] {
return [
'0', // None
'10', // Joplin Cloud
'7', // Dropbox
'3', // OneDrive
];
}
}