diff --git a/packages/app-cli/tests/support/plugins/clipboard/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/clipboard/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/clipboard/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/clipboard/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/clipboard/api/types.ts b/packages/app-cli/tests/support/plugins/clipboard/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/clipboard/api/types.ts +++ b/packages/app-cli/tests/support/plugins/clipboard/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/codemirror_content_script/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/codemirror_content_script/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/codemirror_content_script/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/codemirror_content_script/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/codemirror_content_script/api/types.ts b/packages/app-cli/tests/support/plugins/codemirror_content_script/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/codemirror_content_script/api/types.ts +++ b/packages/app-cli/tests/support/plugins/codemirror_content_script/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/content_script/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/content_script/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/content_script/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/content_script/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/content_script/api/types.ts b/packages/app-cli/tests/support/plugins/content_script/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/content_script/api/types.ts +++ b/packages/app-cli/tests/support/plugins/content_script/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/dialog/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/dialog/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/dialog/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/dialog/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/dialog/api/types.ts b/packages/app-cli/tests/support/plugins/dialog/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/dialog/api/types.ts +++ b/packages/app-cli/tests/support/plugins/dialog/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/editor_context_menu/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/editor_context_menu/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/editor_context_menu/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/editor_context_menu/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/editor_context_menu/api/types.ts b/packages/app-cli/tests/support/plugins/editor_context_menu/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/editor_context_menu/api/types.ts +++ b/packages/app-cli/tests/support/plugins/editor_context_menu/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/events/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/events/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/events/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/events/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/events/api/types.ts b/packages/app-cli/tests/support/plugins/events/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/events/api/types.ts +++ b/packages/app-cli/tests/support/plugins/events/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/external_assets/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/external_assets/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/external_assets/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/external_assets/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/external_assets/api/types.ts b/packages/app-cli/tests/support/plugins/external_assets/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/external_assets/api/types.ts +++ b/packages/app-cli/tests/support/plugins/external_assets/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/imaging/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/imaging/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/imaging/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/imaging/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/imaging/api/types.ts b/packages/app-cli/tests/support/plugins/imaging/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/imaging/api/types.ts +++ b/packages/app-cli/tests/support/plugins/imaging/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/imaging/src/index.ts b/packages/app-cli/tests/support/plugins/imaging/src/index.ts index 878cfceae2..598187e6df 100644 --- a/packages/app-cli/tests/support/plugins/imaging/src/index.ts +++ b/packages/app-cli/tests/support/plugins/imaging/src/index.ts @@ -22,39 +22,19 @@ joplin.plugins.register({ const result = await joplin.data.get(['notes', noteId, 'resources']); if (result.items.length <= 0) return; const resource = result.items[0]; - const file = await joplin.data.get(['resources', resource.id, 'file']); - + // --------------------------------------------------------------- // Create an image object and resize it // --------------------------------------------------------------- - const imageHandle = await joplin.imaging.createFromBuffer(file.body); - const resizedImageHandle = await joplin.imaging.resize(imageHandle, { width: 100 }); - + const imageHandle = await joplin.imaging.createFromResource(resource.id); + const resizedImageHandle = await joplin.imaging.resize(imageHandle, { width: 100 }); + // --------------------------------------------------------------- - // Save the image to file + // Convert the image to a resource and add it to the note // --------------------------------------------------------------- - const tempFilePath = (await joplin.plugins.dataDir()) + '/' + Date.now(); - const base64 = await joplin.imaging.toBase64(resizedImageHandle); - const fs = await joplin.require('fs-extra'); - await fs.writeFile(tempFilePath, base64, { encoding: 'base64' }); - - // --------------------------------------------------------------- - // Create a resource for the thumbnail and add it to the note - // --------------------------------------------------------------- - - const newResource = await joplin.data.post( - ["resources"], - null, - { title: "Thumbnail" }, - [ - { - path: tempFilePath, - }, - ] - ); - + const newResource = await joplin.imaging.toJpgResource(resizedImageHandle, { title: "Thumbnail" }); await joplin.commands.execute('insertText', '\n![](:/' + newResource.id + ')'); // --------------------------------------------------------------- diff --git a/packages/app-cli/tests/support/plugins/jpl_test/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/jpl_test/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/jpl_test/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/jpl_test/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/jpl_test/api/types.ts b/packages/app-cli/tests/support/plugins/jpl_test/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/jpl_test/api/types.ts +++ b/packages/app-cli/tests/support/plugins/jpl_test/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/json_export/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/json_export/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/json_export/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/json_export/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/json_export/api/types.ts b/packages/app-cli/tests/support/plugins/json_export/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/json_export/api/types.ts +++ b/packages/app-cli/tests/support/plugins/json_export/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/load_css/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/load_css/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/load_css/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/load_css/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/load_css/api/types.ts b/packages/app-cli/tests/support/plugins/load_css/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/load_css/api/types.ts +++ b/packages/app-cli/tests/support/plugins/load_css/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/menu/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/menu/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/menu/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/menu/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/menu/api/types.ts b/packages/app-cli/tests/support/plugins/menu/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/menu/api/types.ts +++ b/packages/app-cli/tests/support/plugins/menu/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/multi_selection/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/multi_selection/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/multi_selection/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/multi_selection/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/multi_selection/api/types.ts b/packages/app-cli/tests/support/plugins/multi_selection/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/multi_selection/api/types.ts +++ b/packages/app-cli/tests/support/plugins/multi_selection/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/nativeModule/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/nativeModule/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/nativeModule/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/nativeModule/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/nativeModule/api/types.ts b/packages/app-cli/tests/support/plugins/nativeModule/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/nativeModule/api/types.ts +++ b/packages/app-cli/tests/support/plugins/nativeModule/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/post_messages/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/post_messages/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/post_messages/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/post_messages/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/post_messages/api/types.ts b/packages/app-cli/tests/support/plugins/post_messages/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/post_messages/api/types.ts +++ b/packages/app-cli/tests/support/plugins/post_messages/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/register_command/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/register_command/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/register_command/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/register_command/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/register_command/api/types.ts b/packages/app-cli/tests/support/plugins/register_command/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/register_command/api/types.ts +++ b/packages/app-cli/tests/support/plugins/register_command/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/selected_text/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/selected_text/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/selected_text/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/selected_text/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/selected_text/api/types.ts b/packages/app-cli/tests/support/plugins/selected_text/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/selected_text/api/types.ts +++ b/packages/app-cli/tests/support/plugins/selected_text/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/settings/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/settings/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/settings/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/settings/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/settings/api/types.ts b/packages/app-cli/tests/support/plugins/settings/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/settings/api/types.ts +++ b/packages/app-cli/tests/support/plugins/settings/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/toc/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/toc/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/toc/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/toc/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/toc/api/types.ts b/packages/app-cli/tests/support/plugins/toc/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/toc/api/types.ts +++ b/packages/app-cli/tests/support/plugins/toc/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/user_data/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/user_data/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/user_data/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/user_data/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/user_data/api/types.ts b/packages/app-cli/tests/support/plugins/user_data/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/user_data/api/types.ts +++ b/packages/app-cli/tests/support/plugins/user_data/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/app-cli/tests/support/plugins/withExternalModules/api/JoplinImaging.d.ts b/packages/app-cli/tests/support/plugins/withExternalModules/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/app-cli/tests/support/plugins/withExternalModules/api/JoplinImaging.d.ts +++ b/packages/app-cli/tests/support/plugins/withExternalModules/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/app-cli/tests/support/plugins/withExternalModules/api/types.ts b/packages/app-cli/tests/support/plugins/withExternalModules/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/app-cli/tests/support/plugins/withExternalModules/api/types.ts +++ b/packages/app-cli/tests/support/plugins/withExternalModules/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/generator-joplin/generators/app/templates/api/JoplinImaging.d.ts b/packages/generator-joplin/generators/app/templates/api/JoplinImaging.d.ts index 31f8179cb4..764525987d 100644 --- a/packages/generator-joplin/generators/app/templates/api/JoplinImaging.d.ts +++ b/packages/generator-joplin/generators/app/templates/api/JoplinImaging.d.ts @@ -1,3 +1,4 @@ +import { Rectangle } from "./types"; export interface Implementation { nativeImage: any; } @@ -31,10 +32,47 @@ export default class JoplinImaging { private createImageHandle; private imageByHandle; private cacheImage; + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise; + createFromPath(filePath: string): Promise; + createFromResource(resourceId: string): Promise; + getSize(handle: Handle): Promise; resize(handle: Handle, options?: ResizeOptions): Promise; + crop(handle: Handle, rectange: Rectangle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toDataUrl(handle: Handle): Promise; + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ toBase64(handle: Handle): Promise; + toPngFile(handle: Handle, filePath: string): Promise; + /** + * Quality is between 0 and 100 + */ + toJpgFile(handle: Handle, filePath: string, quality?: number): Promise; + private tempFilePath; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + toJpgResource(handle: Handle, resourceProps: any, quality?: number): Promise; + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + toPngResource(handle: Handle, resourceProps: any): Promise; /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/generator-joplin/generators/app/templates/api/types.ts b/packages/generator-joplin/generators/app/templates/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/generator-joplin/generators/app/templates/api/types.ts +++ b/packages/generator-joplin/generators/app/templates/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // ================================================================= diff --git a/packages/lib/services/plugins/api/JoplinImaging.ts b/packages/lib/services/plugins/api/JoplinImaging.ts index a8cffd103e..61a254e344 100644 --- a/packages/lib/services/plugins/api/JoplinImaging.ts +++ b/packages/lib/services/plugins/api/JoplinImaging.ts @@ -1,5 +1,10 @@ /* eslint-disable multiline-comment-style */ +import Resource from '../../../models/Resource'; +import Setting from '../../../models/Setting'; +import shim from '../../../shim'; +import { Rectangle } from './types'; + export interface Implementation { nativeImage: any; } @@ -63,21 +68,61 @@ export default class JoplinImaging { return handle; } + /** + * Create an image from a buffer - however only use this for very small + * images. It requires transferring the full image data from the plugin to + * the app, which is extremely slow and will freeze the app. Instead, use + * `createFromPath` or `createFromResource`, which will manipulate the image + * data directly from the main process. + */ public async createFromBuffer(buffer: any, options: CreateFromBufferOptions = null): Promise { return this.cacheImage(this.implementation_.nativeImage.createFromBuffer(buffer, options)); } + public async createFromPath(filePath: string): Promise { + return this.cacheImage(this.implementation_.nativeImage.createFromPath(filePath)); + } + + public async createFromResource(resourceId: string): Promise { + const resource = await Resource.load(resourceId); + if (!resource) throw new Error(`No such resource: ${resourceId}`); + const resourcePath = await Resource.fullPath(resource); + if (!(await shim.fsDriver().exists(resourcePath))) throw new Error(`Could not load resource path: ${resourcePath}`); + return this.createFromPath(resourcePath); + } + + public async getSize(handle: Handle) { + const image = this.imageByHandle(handle); + return image.data.getSize(); + } + public async resize(handle: Handle, options: ResizeOptions = null) { const image = this.imageByHandle(handle); const resizedImage = image.data.resize(options); return this.cacheImage(resizedImage); } + public async crop(handle: Handle, rectange: Rectangle) { + const image = this.imageByHandle(handle); + const croppedImage = image.data.crop(rectange); + return this.cacheImage(croppedImage); + } + + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ public async toDataUrl(handle: Handle): Promise { const image = this.imageByHandle(handle); return image.data.toDataURL(); } + /** + * Warnings: requires transferring the complete image from the app to the + * plugin which may freeze the app. Consider using one of the `toXxxFile()` + * or `toXxxResource()` methods instead. + */ public async toBase64(handle: Handle) { const dataUrl = await this.toDataUrl(handle); const s = dataUrl.split('base64,'); @@ -85,6 +130,49 @@ export default class JoplinImaging { return s[1]; } + public async toPngFile(handle: Handle, filePath: string) { + const image = this.imageByHandle(handle); + const data = image.data.toPNG(); + await shim.fsDriver().writeFile(filePath, data, 'buffer'); + } + + /** + * Quality is between 0 and 100 + */ + public async toJpgFile(handle: Handle, filePath: string, quality = 80) { + const image = this.imageByHandle(handle); + const data = image.data.toJPEG(quality); + await shim.fsDriver().writeFile(filePath, data, 'buffer'); + } + + private tempFilePath(ext: string) { + return `${Setting.value('tempDir')}/${Date.now()}_${Math.random()}.${ext}`; + } + + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a JPEG. + */ + public async toJpgResource(handle: Handle, resourceProps: any, quality = 80) { + const tempFilePath = this.tempFilePath('jpg'); + await this.toJpgFile(handle, tempFilePath, quality); + const newResource = await shim.createResourceFromPath(tempFilePath, resourceProps, { resizeLargeImages: 'never' }); + await shim.fsDriver().remove(tempFilePath); + return newResource; + } + + /** + * Creates a new Joplin resource from the image data. The image will be + * first converted to a PNG. + */ + public async toPngResource(handle: Handle, resourceProps: any) { + const tempFilePath = this.tempFilePath('png'); + await this.toPngFile(handle, tempFilePath); + const newResource = await shim.createResourceFromPath(tempFilePath, resourceProps, { resizeLargeImages: 'never' }); + await shim.fsDriver().remove(tempFilePath); + return newResource; + } + /** * Image data is not automatically deleted by Joplin so make sure you call * this method on the handle once you are done. diff --git a/packages/lib/services/plugins/api/types.ts b/packages/lib/services/plugins/api/types.ts index faef491874..e6c8b31afd 100644 --- a/packages/lib/services/plugins/api/types.ts +++ b/packages/lib/services/plugins/api/types.ts @@ -359,6 +359,13 @@ export interface DialogResult { formData?: any; } +export interface Rectangle { + x?: number; + y?: number; + width?: number; + height?: number; +} + // ================================================================= // Settings types // =================================================================