1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00

Mobile, Desktop: Resolves #8566: Add an option to disable the image resizing prompt (#8575)

Co-authored-by: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com>
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
This commit is contained in:
Hubert 2023-08-08 11:49:54 -03:00 committed by GitHub
parent 434c890686
commit 5f7e130ff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 50 deletions

View File

@ -78,7 +78,7 @@ export async function commandAttachFileToBody(body: string, filePaths: string[]
logger.info(`Attaching ${filePath}`);
const newBody = await shim.attachFileToNoteBody(body, filePath, options.position, {
createFileURL: options.createFileURL,
resizeLargeImages: 'ask',
resizeLargeImages: Setting.value('imageResizing'),
});
if (!newBody) {

View File

@ -572,29 +572,16 @@ class NoteScreenComponent extends BaseScreenComponent {
public async resizeImage(localFilePath: string, targetPath: string, mimeType: string) {
const maxSize = Resource.IMAGE_MAX_DIMENSION;
const dimensions: any = await this.imageDimensions(localFilePath);
reg.logger().info('Original dimensions ', dimensions);
let mustResize = dimensions.width > maxSize || dimensions.height > maxSize;
if (mustResize) {
const buttonId = await dialogs.pop(this, _('You are about to attach a large image (%dx%d pixels). Would you like to resize it down to %d pixels before attaching it?', dimensions.width, dimensions.height, maxSize), [
{ text: _('Yes'), id: 'yes' },
{ text: _('No'), id: 'no' },
{ text: _('Cancel'), id: 'cancel' },
]);
if (buttonId === 'cancel') return false;
mustResize = buttonId === 'yes';
}
if (mustResize) {
const saveOriginalImage = async () => {
await shim.fsDriver().copy(localFilePath, targetPath);
return true;
};
const saveResizedImage = async () => {
dimensions.width = maxSize;
dimensions.height = maxSize;
reg.logger().info('New dimensions ', dimensions);
const format = mimeType === 'image/png' ? 'PNG' : 'JPEG';
@ -612,11 +599,27 @@ class NoteScreenComponent extends BaseScreenComponent {
} catch (error) {
reg.logger().warn('Error when unlinking cached file: ', error);
}
} else {
await shim.fsDriver().copy(localFilePath, targetPath);
return true;
};
const canResize = dimensions.width > maxSize || dimensions.height > maxSize;
if (canResize) {
const resizeLargeImages = Setting.value('imageResizing');
if (resizeLargeImages === 'alwaysAsk') {
const userAnswer = await dialogs.pop(this, `${_('You are about to attach a large image (%dx%d pixels). Would you like to resize it down to %d pixels before attaching it?', dimensions.width, dimensions.height, maxSize)}\n\n${_('(You may disable this prompt in the options)')}`, [
{ text: _('Yes'), id: 'yes' },
{ text: _('No'), id: 'no' },
{ text: _('Cancel'), id: 'cancel' },
]);
if (userAnswer === 'yes') return await saveResizedImage();
if (userAnswer === 'no') return await saveOriginalImage();
if (userAnswer === 'cancel') return false;
} else if (resizeLargeImages === 'alwaysResize') {
return await saveResizedImage();
}
}
return true;
return await saveOriginalImage();
}
public async attachFile(pickerResponse: any, fileType: string) {

View File

@ -1121,7 +1121,25 @@ class Setting extends BaseModel {
storage: SettingStorage.File,
isGlobal: true,
},
imageResizing: {
value: 'alwaysAsk',
type: SettingItemType.String,
section: 'note',
isEnum: true,
public: true,
appTypes: [AppType.Mobile, AppType.Desktop],
label: () => _('Resize large images:'),
description: () => _('Shrink large images before adding them to notes to save storage space.'),
options: () => {
return {
alwaysAsk: _('Always ask'),
alwaysResize: _('Always resize'),
neverResize: _('Never resize'),
};
},
storage: SettingStorage.File,
isGlobal: true,
},
'plugins.states': {
value: '',
type: SettingItemType.Object,

View File

@ -184,38 +184,42 @@ function shimInit(options = null) {
if (shim.isElectron()) {
// For Electron
const nativeImage = require('electron').nativeImage;
let image = nativeImage.createFromPath(filePath);
const image = nativeImage.createFromPath(filePath);
if (image.isEmpty()) throw new Error(`Image is invalid or does not exist: ${filePath}`);
const size = image.getSize();
let mustResize = size.width > maxDim || size.height > maxDim;
if (mustResize && resizeLargeImages === 'ask') {
const answer = shim.showMessageBox(_('You are about to attach a large image (%dx%d pixels). Would you like to resize it down to %d pixels before attaching it?', size.width, size.height, maxDim), {
buttons: [_('Yes'), _('No'), _('Cancel')],
});
if (answer === 2) return false;
mustResize = answer === 0;
}
if (!mustResize) {
const saveOriginalImage = async () => {
await shim.fsDriver().copy(filePath, targetPath);
return true;
};
const saveResizedImage = async () => {
const options = {};
if (size.width > size.height) {
options.width = maxDim;
} else {
options.height = maxDim;
}
const resizedImage = image.resize(options);
await shim.writeImageToFile(resizedImage, mime, targetPath);
return true;
};
const canResize = size.width > maxDim || size.height > maxDim;
if (canResize) {
if (resizeLargeImages === 'alwaysAsk') {
const Yes = 0, No = 1, Cancel = 2;
const userAnswer = shim.showMessageBox(`${_('You are about to attach a large image (%dx%d pixels). Would you like to resize it down to %d pixels before attaching it?', size.width, size.height, maxDim)}\n\n${_('(You may disable this prompt in the options)')}`, {
buttons: [_('Yes'), _('No'), _('Cancel')],
});
if (userAnswer === Yes) return await saveResizedImage();
if (userAnswer === No) return await saveOriginalImage();
if (userAnswer === Cancel) return false;
} else if (resizeLargeImages === 'alwaysResize') {
return await saveResizedImage();
}
}
const options = {};
if (size.width > size.height) {
options.width = maxDim;
} else {
options.height = maxDim;
}
image = image.resize(options);
await shim.writeImageToFile(image, mime, targetPath);
return await saveOriginalImage();
} else {
// For the CLI tool
const image = sharp(filePath);
@ -241,8 +245,6 @@ function shimInit(options = null) {
});
});
}
return true;
};
// This is a bit of an ugly method that's used to both create a new resource