Feature #38504 » 0001-Display-pasted-images-in-appropriate-size-on-HiDPI-d.patch
| app/assets/javascripts/attachments.js | ||
|---|---|---|
|
}
|
||
|
}
|
||
|
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) {
|
||
|
// insert uploaded image inline if dropped area is currently focused textarea
|
||
|
if($(handleFileDropEvent.target).hasClass('wiki-edit') && $.inArray(file.type, window.wikiImageMimeTypes) > -1) {
|
||
|
var $textarea = $(handleFileDropEvent.target);
|
||
|
var cursorPosition = $textarea.prop('selectionStart');
|
||
|
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 newLineAfter = true;
|
||
|
if(cursorPosition === 0 || description.substr(cursorPosition-1,1).match(/\r|\n/)) {
|
||
| ... | ... | |
|
newLineAfter = false;
|
||
|
}
|
||
|
$textarea.val(
|
||
|
description.substring(0, cursorPosition)
|
||
|
+ (newLineBefore ? '\n' : '')
|
||
|
+ inlineFilename
|
||
|
+ (newLineAfter ? '\n' : '')
|
||
|
+ description.substring(cursorPosition, description.length)
|
||
|
);
|
||
|
$textarea.prop({
|
||
|
'selectionStart': cursorPosition + newLineBefore,
|
||
|
'selectionEnd': cursorPosition + inlineFilename.length + newLineBefore
|
||
|
});
|
||
|
$textarea.parents('.jstBlock')
|
||
|
.find('.jstb_img').click();
|
||
|
getInlineAttachmentMarkup(file)
|
||
|
.then(imageMarkup => {
|
||
|
$textarea.val(
|
||
|
description.substring(0, cursorPosition)
|
||
|
+ (newLineBefore ? '\n' : '')
|
||
|
+ imageMarkup
|
||
|
+ (newLineAfter ? '\n' : '')
|
||
|
+ description.substring(cursorPosition, description.length)
|
||
|
);
|
||
|
})
|
||
|
// move cursor into next line
|
||
|
cursorPosition = $textarea.prop('selectionStart');
|
||
- « Previous
- 1
- …
- 3
- 4
- 5
- Next »