mirror of
https://github.com/meineerde/redmine.git
synced 2026-04-03 22:41:39 +00:00
Scale pasted images to appropriate size on HiDPI displays using devicePixelRatio (#38504).
Patch by Go MAEDA (user:maeda). git-svn-id: https://svn.redmine.org/redmine/trunk@23344 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
d99b67467d
commit
096fcbf4aa
@ -229,15 +229,60 @@ function setupFileDrop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getImageWidth(file) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (file.type.startsWith("image/")) {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = function(event) {
|
||||||
|
const img = new Image();
|
||||||
|
img.onload = function() {
|
||||||
|
resolve(img.width);
|
||||||
|
};
|
||||||
|
img.onerror = reject;
|
||||||
|
img.src = event.target.result;
|
||||||
|
};
|
||||||
|
reader.onerror = reject;
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
} else {
|
||||||
|
resolve(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getInlineAttachmentMarkup(file) {
|
||||||
|
const sanitizedFilename = file.name.replace(/[\/\?\%\*\:\|\"\'<>\n\r]+/g, '_');
|
||||||
|
const inlineFilename = encodeURIComponent(sanitizedFilename)
|
||||||
|
.replace(/[!()]/g, function(match) { return "%" + match.charCodeAt(0).toString(16) });
|
||||||
|
|
||||||
|
const isFromClipboard = /^clipboard-\d{12}-[a-z0-9]{5}\.\w+$/.test(file.name);
|
||||||
|
let imageDisplayWidth;
|
||||||
|
if (isFromClipboard) {
|
||||||
|
const imageWidth = await getImageWidth(file).catch(() => 0);
|
||||||
|
imageDisplayWidth = Math.round(imageWidth / window.devicePixelRatio);
|
||||||
|
}
|
||||||
|
const hasValidWidth = isFromClipboard && imageDisplayWidth > 0;
|
||||||
|
|
||||||
|
switch (document.body.getAttribute('data-text-formatting')) {
|
||||||
|
case 'textile':
|
||||||
|
return hasValidWidth
|
||||||
|
? `!{width: ${imageDisplayWidth}px}.${inlineFilename}!`
|
||||||
|
: `!${inlineFilename}!`;
|
||||||
|
case 'common_mark':
|
||||||
|
return hasValidWidth
|
||||||
|
? `<img style="width: ${imageDisplayWidth}px;" src="${inlineFilename}"><br>`
|
||||||
|
: ``;
|
||||||
|
default:
|
||||||
|
// Text formatting is "none" or unknown
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function addInlineAttachmentMarkup(file) {
|
function addInlineAttachmentMarkup(file) {
|
||||||
// insert uploaded image inline if dropped area is currently focused textarea
|
// insert uploaded image inline if dropped area is currently focused textarea
|
||||||
if($(handleFileDropEvent.target).hasClass('wiki-edit') && $.inArray(file.type, window.wikiImageMimeTypes) > -1) {
|
if($(handleFileDropEvent.target).hasClass('wiki-edit') && $.inArray(file.type, window.wikiImageMimeTypes) > -1) {
|
||||||
var $textarea = $(handleFileDropEvent.target);
|
var $textarea = $(handleFileDropEvent.target);
|
||||||
var cursorPosition = $textarea.prop('selectionStart');
|
var cursorPosition = $textarea.prop('selectionStart');
|
||||||
var description = $textarea.val();
|
var description = $textarea.val();
|
||||||
var sanitizedFilename = file.name.replace(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_');
|
|
||||||
var inlineFilename = encodeURIComponent(sanitizedFilename)
|
|
||||||
.replace(/[!()]/g, function(match) { return "%" + match.charCodeAt(0).toString(16) });
|
|
||||||
var newLineBefore = true;
|
var newLineBefore = true;
|
||||||
var newLineAfter = true;
|
var newLineAfter = true;
|
||||||
if(cursorPosition === 0 || description.substr(cursorPosition-1,1).match(/\r|\n/)) {
|
if(cursorPosition === 0 || description.substr(cursorPosition-1,1).match(/\r|\n/)) {
|
||||||
@ -247,20 +292,16 @@ function addInlineAttachmentMarkup(file) {
|
|||||||
newLineAfter = false;
|
newLineAfter = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$textarea.val(
|
getInlineAttachmentMarkup(file)
|
||||||
description.substring(0, cursorPosition)
|
.then(imageMarkup => {
|
||||||
+ (newLineBefore ? '\n' : '')
|
$textarea.val(
|
||||||
+ inlineFilename
|
description.substring(0, cursorPosition)
|
||||||
+ (newLineAfter ? '\n' : '')
|
+ (newLineBefore ? '\n' : '')
|
||||||
+ description.substring(cursorPosition, description.length)
|
+ imageMarkup
|
||||||
);
|
+ (newLineAfter ? '\n' : '')
|
||||||
|
+ description.substring(cursorPosition, description.length)
|
||||||
$textarea.prop({
|
);
|
||||||
'selectionStart': cursorPosition + newLineBefore,
|
})
|
||||||
'selectionEnd': cursorPosition + inlineFilename.length + newLineBefore
|
|
||||||
});
|
|
||||||
$textarea.parents('.jstBlock')
|
|
||||||
.find('.jstb_img').click();
|
|
||||||
|
|
||||||
// move cursor into next line
|
// move cursor into next line
|
||||||
cursorPosition = $textarea.prop('selectionStart');
|
cursorPosition = $textarea.prop('selectionStart');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user