2023-12-23 16:19:06 +02:00
|
|
|
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;
|
2022-04-03 20:19:24 +02:00
|
|
|
supportsRecursiveLinkedNotes: boolean;
|
2021-08-16 16:20:14 +02:00
|
|
|
description: string;
|
2023-12-23 16:19:06 +02:00
|
|
|
classRef: typeof BaseSyncTarget;
|
2021-08-16 16:20:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class SyncTargetRegistry {
|
|
|
|
|
2023-12-23 16:19:06 +02:00
|
|
|
private static reg_: Record<number, typeof BaseSyncTarget> = {};
|
2021-08-16 16:20:14 +02:00
|
|
|
|
2021-08-16 17:18:32 +02:00
|
|
|
private static get reg() {
|
|
|
|
return this.reg_;
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static classById(syncTargetId: number) {
|
2021-08-16 17:18:32 +02:00
|
|
|
const info = SyncTargetRegistry.reg[syncTargetId];
|
2019-09-19 23:51:18 +02:00
|
|
|
if (!info) throw new Error(`Invalid id: ${syncTargetId}`);
|
2023-12-23 16:19:06 +02:00
|
|
|
return info;
|
2017-11-24 20:59:16 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static infoByName(name: string): SyncTargetInfo {
|
2023-12-23 16:19:06 +02:00
|
|
|
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(),
|
|
|
|
};
|
|
|
|
return output;
|
|
|
|
}
|
2021-08-16 16:20:14 +02:00
|
|
|
}
|
|
|
|
throw new Error(`Unknown name: ${name}`);
|
|
|
|
}
|
|
|
|
|
2022-04-03 20:19:24 +02:00
|
|
|
public static infoById(id: number): SyncTargetInfo {
|
|
|
|
return this.infoByName(this.idToName(id));
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:19:06 +02:00
|
|
|
public static addClass(SyncTargetClass: typeof BaseSyncTarget) {
|
|
|
|
this.reg[SyncTargetClass.id()] = SyncTargetClass;
|
2017-11-24 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static allIds() {
|
2021-08-16 17:18:32 +02:00
|
|
|
return Object.keys(this.reg);
|
2021-05-03 12:55:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static nameToId(name: string) {
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const n in this.reg) {
|
|
|
|
if (!this.reg.hasOwnProperty(n)) continue;
|
2023-12-23 16:19:06 +02:00
|
|
|
if (this.reg[n].targetName() === name) return this.reg[n].id();
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2021-06-03 17:12:07 +02:00
|
|
|
throw new Error(`Name not found: ${name}. Was the sync target registered?`);
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idToMetadata(id: number) {
|
2023-12-23 16:19:06 +02:00
|
|
|
return this.infoById(id);
|
2018-01-25 21:01:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idToName(id: number) {
|
2023-12-23 16:19:06 +02:00
|
|
|
return this.reg[id].targetName();
|
2018-01-25 23:15:58 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static idAndLabelPlainObject(os: string) {
|
|
|
|
const output: Record<string, string> = {};
|
2021-08-16 17:18:32 +02:00
|
|
|
for (const n in this.reg) {
|
|
|
|
if (!this.reg.hasOwnProperty(n)) continue;
|
2023-12-23 16:19:06 +02:00
|
|
|
const info = this.infoById(this.reg[n].id());
|
2020-02-08 13:59:19 +02:00
|
|
|
if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output[n] = info.label;
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2021-08-16 17:18:32 +02:00
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
return output;
|
2022-05-05 14:19:23 +02:00
|
|
|
}
|
2021-08-16 17:18:32 +02:00
|
|
|
|
2022-05-05 14:19:23 +02:00
|
|
|
public static optionsOrder(): string[] {
|
|
|
|
return [
|
|
|
|
'0', // None
|
|
|
|
'10', // Joplin Cloud
|
|
|
|
'7', // Dropbox
|
|
|
|
'3', // OneDrive
|
|
|
|
];
|
2017-11-24 21:21:30 +02:00
|
|
|
}
|
2022-05-05 14:19:23 +02:00
|
|
|
|
2017-11-24 20:37:40 +02:00
|
|
|
}
|