2020-10-09 19:35:46 +02:00
|
|
|
import bridge from '../bridge';
|
2021-07-23 12:05:21 +02:00
|
|
|
import { Implementation as WindowImplementation } from '@joplin/lib/services/plugins/api/JoplinWindow';
|
|
|
|
import { injectCustomStyles } from '@joplin/lib/CssUtils';
|
2022-07-03 15:32:29 +02:00
|
|
|
import { VersionInfo } from '@joplin/lib/services/plugins/api/types';
|
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import { reg } from '@joplin/lib/registry';
|
|
|
|
import BasePlatformImplementation, { Joplin } from '@joplin/lib/services/plugins/BasePlatformImplementation';
|
2024-03-27 20:53:24 +02:00
|
|
|
import { CreateFromPdfOptions, Implementation as ImagingImplementation } from '@joplin/lib/services/plugins/api/JoplinImaging';
|
|
|
|
import shim from '@joplin/lib/shim';
|
|
|
|
import { join } from 'path';
|
2024-04-27 12:35:49 +02:00
|
|
|
import uuid, { uuidgen } from '@joplin/lib/uuid';
|
|
|
|
import { hasProtocol } from '@joplin/utils/url';
|
|
|
|
import { fileExtension } from '@joplin/utils/path';
|
2021-06-20 14:46:50 +02:00
|
|
|
const { clipboard, nativeImage } = require('electron');
|
2022-07-03 15:32:29 +02:00
|
|
|
const packageInfo = require('../../packageInfo');
|
2020-10-09 19:35:46 +02:00
|
|
|
|
|
|
|
interface Components {
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:29:22 +02:00
|
|
|
[key: string]: any;
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 14:46:50 +02:00
|
|
|
// 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.
|
2022-07-03 15:32:29 +02:00
|
|
|
export default class PlatformImplementation extends BasePlatformImplementation {
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private static instance_: PlatformImplementation;
|
|
|
|
private joplin_: Joplin;
|
|
|
|
private components_: Components;
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public static instance(): PlatformImplementation {
|
2020-10-09 19:35:46 +02:00
|
|
|
if (!this.instance_) this.instance_ = new PlatformImplementation();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
2022-07-03 15:32:29 +02:00
|
|
|
public get versionInfo(): VersionInfo {
|
|
|
|
return {
|
|
|
|
version: packageInfo.version,
|
|
|
|
syncVersion: Setting.value('syncVersion'),
|
|
|
|
profileVersion: reg.db().version(),
|
2024-03-09 13:03:57 +02:00
|
|
|
platform: 'desktop',
|
2022-07-03 15:32:29 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-20 14:46:50 +02:00
|
|
|
public get clipboard() {
|
|
|
|
return clipboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get nativeImage() {
|
|
|
|
return nativeImage;
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:05:21 +02:00
|
|
|
public get window(): WindowImplementation {
|
|
|
|
return {
|
|
|
|
injectCustomStyles: injectCustomStyles,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public constructor() {
|
2022-07-03 15:32:29 +02:00
|
|
|
super();
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
this.components_ = {};
|
|
|
|
|
|
|
|
this.joplin_ = {
|
|
|
|
views: {
|
|
|
|
dialogs: {
|
2023-10-31 17:29:19 +02:00
|
|
|
showMessageBox: async (message: string) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
return bridge().showMessageBox(message);
|
|
|
|
},
|
2023-10-31 17:29:19 +02:00
|
|
|
showOpenDialog: async (options) => {
|
|
|
|
return bridge().showOpenDialog(options);
|
|
|
|
},
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-07-23 12:05:21 +02:00
|
|
|
public registerComponent(name: string, component: any) {
|
2020-10-09 19:35:46 +02:00
|
|
|
this.components_[name] = component;
|
|
|
|
}
|
|
|
|
|
2021-07-23 12:05:21 +02:00
|
|
|
public unregisterComponent(name: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
delete this.components_[name];
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
public get joplin(): Joplin {
|
2020-10-09 19:35:46 +02:00
|
|
|
return this.joplin_;
|
|
|
|
}
|
|
|
|
|
2023-09-17 12:40:50 +02:00
|
|
|
public get imaging(): ImagingImplementation {
|
2024-03-27 20:53:24 +02:00
|
|
|
const createFromPdf = async (path: string, options: CreateFromPdfOptions) => {
|
|
|
|
const tempDir = join(Setting.value('tempDir'), uuid.createNano());
|
|
|
|
await shim.fsDriver().mkdir(tempDir);
|
|
|
|
try {
|
|
|
|
const paths = await shim.pdfToImages(path, tempDir, options);
|
|
|
|
return paths.map(path => nativeImage.createFromPath(path));
|
|
|
|
} finally {
|
|
|
|
await shim.fsDriver().remove(tempDir);
|
|
|
|
}
|
|
|
|
};
|
2024-04-27 12:35:49 +02:00
|
|
|
|
2023-09-17 12:40:50 +02:00
|
|
|
return {
|
2024-04-27 12:35:49 +02:00
|
|
|
createFromPath: async (path: string) => {
|
|
|
|
let pathToProcess = path;
|
|
|
|
let ext = fileExtension(path).toLowerCase();
|
|
|
|
|
|
|
|
if (hasProtocol(path, ['http', 'https', 'file'])) {
|
|
|
|
ext = fileExtension((new URL(path)).pathname);
|
|
|
|
const tempFilePath = `${Setting.value('tempDir')}/${uuidgen()}${ext ? `.${ext}` : ''}`;
|
|
|
|
await shim.fetchBlob(path, { path: tempFilePath });
|
|
|
|
pathToProcess = tempFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext === 'pdf') {
|
|
|
|
const images = await createFromPdf(pathToProcess, { minPage: 1, maxPage: 1 });
|
|
|
|
|
|
|
|
if (images.length === 0) {
|
|
|
|
// Match the behavior or Electron's nativeImage when reading an invalid image.
|
|
|
|
return nativeImage.createEmpty();
|
2024-03-27 20:53:24 +02:00
|
|
|
}
|
2024-04-27 12:35:49 +02:00
|
|
|
|
|
|
|
return images[0];
|
|
|
|
} else {
|
|
|
|
return nativeImage.createFromPath(pathToProcess);
|
|
|
|
}
|
2024-03-27 20:53:24 +02:00
|
|
|
},
|
2024-04-27 12:35:49 +02:00
|
|
|
createFromPdf,
|
2024-03-27 20:53:24 +02:00
|
|
|
getPdfInfo(path: string) {
|
|
|
|
return shim.pdfInfo(path);
|
|
|
|
},
|
2023-09-17 12:40:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
}
|