1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Chore: Refactor sync target code to allow labels and descriptions to be translated

This commit is contained in:
Laurent Cozic 2023-12-23 14:19:06 +00:00
parent 5341501d53
commit 7345cd4678
2 changed files with 37 additions and 27 deletions

View File

@ -5,6 +5,11 @@ import shim from './shim';
import ResourceService from './services/ResourceService'; import ResourceService from './services/ResourceService';
import ShareService from './services/share/ShareService'; import ShareService from './services/share/ShareService';
interface CheckConfigResult {
ok: boolean;
errorMessage: string;
}
export default class BaseSyncTarget { export default class BaseSyncTarget {
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
@ -73,17 +78,17 @@ export default class BaseSyncTarget {
return null; return null;
} }
public static id() { public static id(): number {
throw new Error('id() not implemented'); throw new Error('id() not implemented');
} }
// Note: it cannot be called just "name()" because that's a reserved keyword and // Note: it cannot be called just "name()" because that's a reserved keyword and
// it would throw an obscure error in React Native. // it would throw an obscure error in React Native.
public static targetName() { public static targetName(): string {
throw new Error('targetName() not implemented'); throw new Error('targetName() not implemented');
} }
public static label() { public static label(): string {
throw new Error('label() not implemented'); throw new Error('label() not implemented');
} }
@ -144,6 +149,10 @@ export default class BaseSyncTarget {
} }
} }
public static async checkConfig(_options: any): Promise<CheckConfigResult> {
throw new Error('Not implemented');
}
public async syncStarted() { public async syncStarted() {
if (!this.synchronizer_) return false; if (!this.synchronizer_) return false;
if (!(await this.isAuthenticated())) return false; if (!(await this.isAuthenticated())) return false;

View File

@ -1,3 +1,5 @@
import type BaseSyncTarget from './BaseSyncTarget';
export interface SyncTargetInfo { export interface SyncTargetInfo {
id: number; id: number;
name: string; name: string;
@ -6,12 +8,12 @@ export interface SyncTargetInfo {
supportsConfigCheck: boolean; supportsConfigCheck: boolean;
supportsRecursiveLinkedNotes: boolean; supportsRecursiveLinkedNotes: boolean;
description: string; description: string;
classRef: any; classRef: typeof BaseSyncTarget;
} }
export default class SyncTargetRegistry { export default class SyncTargetRegistry {
private static reg_: Record<number, SyncTargetInfo> = {}; private static reg_: Record<number, typeof BaseSyncTarget> = {};
private static get reg() { private static get reg() {
return this.reg_; return this.reg_;
@ -20,12 +22,24 @@ export default class SyncTargetRegistry {
public static classById(syncTargetId: number) { public static classById(syncTargetId: number) {
const info = SyncTargetRegistry.reg[syncTargetId]; const info = SyncTargetRegistry.reg[syncTargetId];
if (!info) throw new Error(`Invalid id: ${syncTargetId}`); if (!info) throw new Error(`Invalid id: ${syncTargetId}`);
return info.classRef; return info;
} }
public static infoByName(name: string): SyncTargetInfo { public static infoByName(name: string): SyncTargetInfo {
for (const [, info] of Object.entries(this.reg)) { for (const [, SyncTargetClass] of Object.entries(this.reg)) {
if (info.name === name) return info; 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;
}
} }
throw new Error(`Unknown name: ${name}`); throw new Error(`Unknown name: ${name}`);
} }
@ -34,17 +48,8 @@ export default class SyncTargetRegistry {
return this.infoByName(this.idToName(id)); return this.infoByName(this.idToName(id));
} }
public static addClass(SyncTargetClass: any) { public static addClass(SyncTargetClass: typeof BaseSyncTarget) {
this.reg[SyncTargetClass.id()] = { this.reg[SyncTargetClass.id()] = SyncTargetClass;
id: SyncTargetClass.id(),
name: SyncTargetClass.targetName(),
label: SyncTargetClass.label(),
classRef: SyncTargetClass,
description: SyncTargetClass.description(),
supportsSelfHosted: SyncTargetClass.supportsSelfHosted(),
supportsConfigCheck: SyncTargetClass.supportsConfigCheck(),
supportsRecursiveLinkedNotes: SyncTargetClass.supportsRecursiveLinkedNotes(),
};
} }
public static allIds() { public static allIds() {
@ -54,28 +59,24 @@ export default class SyncTargetRegistry {
public static nameToId(name: string) { public static nameToId(name: string) {
for (const n in this.reg) { for (const n in this.reg) {
if (!this.reg.hasOwnProperty(n)) continue; if (!this.reg.hasOwnProperty(n)) continue;
if (this.reg[n].name === name) return this.reg[n].id; if (this.reg[n].targetName() === name) return this.reg[n].id();
} }
throw new Error(`Name not found: ${name}. Was the sync target registered?`); throw new Error(`Name not found: ${name}. Was the sync target registered?`);
} }
public static idToMetadata(id: number) { public static idToMetadata(id: number) {
for (const n in this.reg) { return this.infoById(id);
if (!this.reg.hasOwnProperty(n)) continue;
if (this.reg[n].id === id) return this.reg[n];
}
throw new Error(`ID not found: ${id}`);
} }
public static idToName(id: number) { public static idToName(id: number) {
return this.idToMetadata(id).name; return this.reg[id].targetName();
} }
public static idAndLabelPlainObject(os: string) { public static idAndLabelPlainObject(os: string) {
const output: Record<string, string> = {}; const output: Record<string, string> = {};
for (const n in this.reg) { for (const n in this.reg) {
if (!this.reg.hasOwnProperty(n)) continue; if (!this.reg.hasOwnProperty(n)) continue;
const info = this.reg[n]; const info = this.infoById(this.reg[n].id());
if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) { if (info.classRef.unsupportedPlatforms().indexOf(os) >= 0) {
continue; continue;
} }