1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Clipper: Support clipping screenshots

This commit is contained in:
Laurent Cozic
2018-05-25 08:51:54 +01:00
parent 4640d6d6e3
commit 264ee4f319
8 changed files with 132 additions and 44 deletions

View File

@ -5,6 +5,7 @@ const { FileApiDriverLocal } = require('lib/file-api-driver-local.js');
const { time } = require('lib/time-utils.js');
const { setLocale, defaultLocale, closestSupportedLocale } = require('lib/locale.js');
const { FsDriverNode } = require('lib/fs-driver-node.js');
const mimeUtils = require('lib/mime-utils.js').mime;
const urlValidator = require('valid-url');
function shimInit() {
@ -35,21 +36,24 @@ function shimInit() {
return locale;
}
// For Electron only
shim.writeImageToFile = async function(nativeImage, mime, targetPath) {
let buffer = null;
if (shim.isElectron()) { // For Electron
let buffer = null;
mime = mime.toLowerCase();
mime = mime.toLowerCase();
if (mime === 'image/png') {
buffer = nativeImage.toPNG();
} else if (mime === 'image/jpg' || mime === 'image/jpeg') {
buffer = nativeImage.toJPEG(90);
if (mime === 'image/png') {
buffer = nativeImage.toPNG();
} else if (mime === 'image/jpg' || mime === 'image/jpeg') {
buffer = nativeImage.toJPEG(90);
}
if (!buffer) throw new Error('Cannot resize image because mime type "' + mime + '" is not supported: ' + targetPath);
await shim.fsDriver().writeFile(targetPath, buffer, 'buffer');
} else {
throw new Error('Node support not implemented');
}
if (!buffer) throw new Error('Cannot reisze image because mime type "' + mime + '" is not supported: ' + targetPath);
await shim.fsDriver().writeFile(targetPath, buffer, 'buffer');
}
const resizeImage_ = async function(filePath, targetPath, mime) {
@ -146,7 +150,7 @@ function shimInit() {
}
shim.attachFileToNote = async function(note, filePath, position = null) {
const resource = shim.createResourceFromPath(filePath);
const resource = await shim.createResourceFromPath(filePath);
const newBody = [];
@ -164,6 +168,20 @@ function shimInit() {
return await Note.save(newNote);
}
shim.imageFromDataUrl = async function(imageDataUrl, filePath, options = null) {
if (options === null) options = {};
if (shim.isElectron()) {
const nativeImage = require('electron').nativeImage;
let image = nativeImage.createFromDataURL(imageDataUrl);
if (options.cropRect) image = image.crop(options.cropRect);
const mime = mimeUtils.fromDataUrl(imageDataUrl);
await shim.writeImageToFile(image, mime, filePath);
} else {
throw new Error('Node support not implemented');
}
}
const nodeFetch = require('node-fetch');
shim.readLocalFileBase64 = (path) => {