pull/8900/head
Laurent Cozic 2023-09-17 11:37:24 +01:00
parent b2e2c3cdb6
commit 766f9de318
24 changed files with 576 additions and 142 deletions

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -1,28 +1,65 @@
import joplin from 'api';
import { ToolbarButtonLocation } from 'api/types';
import { writeFile } from 'fs-extra';
joplin.plugins.register({
onStart: async function() {
await joplin.commands.register({
name: 'makeThumbnail',
execute: async () => {
// ---------------------------------------------------------------
// Get the current note
// ---------------------------------------------------------------
const noteIds = await joplin.workspace.selectedNoteIds();
if (noteIds.length !== 1) return;
const noteId = noteIds[0];
// ---------------------------------------------------------------
// Get the top resource in that note (if any)
// ---------------------------------------------------------------
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']);
const imageHandle = await joplin.imaging.createFromBuffer(file.body, {});
const resizedImageHandle = await joplin.imaging.resize(imageHandle, {
width: 100,
height: 100,
quality: 'good',
});
const dataUrl = await joplin.imaging.toDataUrl(resizedImageHandle);
await joplin.commands.execute('insertText', '\n![](' + dataUrl + ')');
// ---------------------------------------------------------------
// Create an image object and resize it
// ---------------------------------------------------------------
const imageHandle = await joplin.imaging.createFromBuffer(file.body);
const resizedImageHandle = await joplin.imaging.resize(imageHandle, { width: 100 });
// ---------------------------------------------------------------
// Save the image to file
// ---------------------------------------------------------------
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,
},
]
);
await joplin.commands.execute('insertText', '\n![](:/' + newResource.id + ')');
// ---------------------------------------------------------------
// Free up the image objects at the end
// ---------------------------------------------------------------
await joplin.imaging.free(imageHandle);
await joplin.imaging.free(resizedImageHandle);

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -7,11 +7,23 @@ export interface CreateFromBufferOptions {
scaleFactor?: number;
}
export interface ResizeOptions {
width: number;
height: number;
quality: 'good' | 'better' | 'best';
width?: number;
height?: number;
quality?: 'good' | 'better' | 'best';
}
export type Handle = string;
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_;
private images_;
@ -19,8 +31,13 @@ export default class JoplinImaging {
private createImageHandle;
private imageByHandle;
private cacheImage;
createFromBuffer(buffer: any, options: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<any>;
createFromBuffer(buffer: any, options?: CreateFromBufferOptions): Promise<Handle>;
resize(handle: Handle, options?: ResizeOptions): Promise<string>;
toDataUrl(handle: Handle): Promise<string>;
toBase64(handle: Handle): Promise<string>;
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
free(handle: Handle): Promise<void>;
}

View File

@ -23,6 +23,18 @@ interface Image {
data: any;
}
/**
* Provides imaging functions to resize or process images. You create an image
* using one of the `createFrom` functions, then use the other functions to
* process the image.
*
* Images are associated with a handle which is what will be available to the
* plugin. Once you are done with an image, free it using the `free()` function.
*
* [View the
* example](https://github.com/laurent22/joplin/blob/dev/packages/app-cli/tests/support/plugins/imaging/src/index.ts)
*
*/
export default class JoplinImaging {
private implementation_: Implementation;
@ -61,11 +73,22 @@ export default class JoplinImaging {
return this.cacheImage(resizedImage);
}
public async toDataUrl(handle: Handle) {
public async toDataUrl(handle: Handle): Promise<string> {
const image = this.imageByHandle(handle);
return image.data.toDataURL();
}
public async toBase64(handle: Handle) {
const dataUrl = await this.toDataUrl(handle);
const s = dataUrl.split('base64,');
if (s.length !== 2) throw new Error('Could not convert to base64');
return s[1];
}
/**
* Image data is not automatically deleted by Joplin so make sure you call
* this method on the handle once you are done.
*/
public async free(handle: Handle) {
const index = this.images_.findIndex(i => i.handle === handle);
if (index >= 0) this.images_.splice(index, 1);