1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/app-mobile/plugins/PlatformImplementation.ts

115 lines
2.9 KiB
TypeScript

import { VersionInfo } from '@joplin/lib/services/plugins/api/types';
import { Implementation as WindowImplementation } from '@joplin/lib/services/plugins/api/JoplinWindow';
import Setting from '@joplin/lib/models/Setting';
import { reg } from '@joplin/lib/registry';
import BasePlatformImplementation, { Joplin } from '@joplin/lib/services/plugins/BasePlatformImplementation';
import { Implementation as ImagingImplementation } from '@joplin/lib/services/plugins/api/JoplinImaging';
import RNVersionInfo from 'react-native-version-info';
import { _ } from '@joplin/lib/locale';
import shim from '@joplin/lib/shim';
import Clipboard from '@react-native-community/clipboard';
interface Components {
[key: string]: any;
}
// PlatformImplementation provides access to platform specific dependencies,
// such as the clipboard, message dialog, etc. It allows having the same plugin
// API for all platforms, but with different implementations.
export default class PlatformImplementation extends BasePlatformImplementation {
private static instance_: PlatformImplementation;
private joplin_: Joplin;
private components_: Components;
public static instance(): PlatformImplementation {
if (!this.instance_) this.instance_ = new PlatformImplementation();
return this.instance_;
}
public get versionInfo(): VersionInfo {
return {
version: RNVersionInfo.appVersion,
syncVersion: Setting.value('syncVersion'),
profileVersion: reg.db().version(),
platform: 'mobile',
};
}
public constructor() {
super();
this.components_ = {};
this.joplin_ = {
views: {
dialogs: {
showMessageBox: async (message: string) => {
return await shim.showMessageBox(
message,
{ title: _('Plugin message') },
);
},
showOpenDialog: async (_options) => {
throw new Error('Not implemented: showOpenDialog');
},
},
},
};
}
public registerComponent(name: string, component: any) {
this.components_[name] = component;
}
public unregisterComponent(name: string) {
delete this.components_[name];
}
public get joplin(): Joplin {
return this.joplin_;
}
public get imaging(): ImagingImplementation {
return {
nativeImage: null,
getPdfInfo: async () => {
throw new Error('Not implemented: getPdfInfo');
},
};
}
public get nativeImage(): any {
return null;
}
public get clipboard() {
// Deny access to the clipboard on iOS, as per AppStore guidelines
// (as of March 2024).
if (shim.mobilePlatform() === 'ios') {
return {
readText: () => {
throw new Error('Not available on iOS');
},
writeText: () => {
throw new Error('Not available on iOS');
},
availableFormats: (): string[] => [],
};
}
return {
readText: () => Clipboard.getString(),
writeText: (text: string) => Clipboard.setString(text),
availableFormats: () => ['text/plain'],
};
}
public get window(): WindowImplementation {
return {
injectCustomStyles: null,
};
}
}