Skip to content

Commit d80abe7

Browse files
committed
apply ImagePaste patch from @tyroneyeh
1 parent 6660f9a commit d80abe7

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

web_src/js/features/comp/ImagePaste.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,44 @@ function clipboardPastedImages(e) {
3131

3232

3333
function insertAtCursor(field, value) {
34-
if (field.selectionStart || field.selectionStart === 0) {
35-
const startPos = field.selectionStart;
36-
const endPos = field.selectionEnd;
37-
field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
38-
field.selectionStart = startPos + value.length;
39-
field.selectionEnd = startPos + value.length;
34+
if (field.getTextArea().selectionStart || field.getTextArea().selectionStart === 0) {
35+
const startPos = field.getTextArea().selectionStart;
36+
const endPos = field.getTextArea().selectionEnd;
37+
field.setValue(field.getValue().substring(0, startPos) + value + field.getValue().substring(endPos, field.getValue().length));
38+
field.getTextArea().electionStart = startPos + value.length;
39+
field.getTextArea().selectionEnd = startPos + value.length;
4040
} else {
41-
field.value += value;
41+
field.setValue(field.getValue() + value);
4242
}
4343
}
4444

4545
function replaceAndKeepCursor(field, oldval, newval) {
46-
if (field.selectionStart || field.selectionStart === 0) {
47-
const startPos = field.selectionStart;
48-
const endPos = field.selectionEnd;
49-
field.value = field.value.replace(oldval, newval);
50-
field.selectionStart = startPos + newval.length - oldval.length;
51-
field.selectionEnd = endPos + newval.length - oldval.length;
46+
if (field.getTextArea().selectionStart || field.getTextArea().selectionStart === 0) {
47+
const startPos = field.getTextArea().selectionStart;
48+
const endPos = field.getTextArea().selectionEnd;
49+
field.setValue(field.getValue().replace(oldval, newval));
50+
field.getTextArea().selectionStart = startPos + newval.length - oldval.length;
51+
field.getTextArea().selectionEnd = endPos + newval.length - oldval.length;
5252
} else {
53-
field.value = field.value.replace(oldval, newval);
53+
field.setValue(field.getValue().replace(oldval, newval));
5454
}
5555
}
5656

5757
export function initCompImagePaste($target) {
58-
$target.each(function () {
59-
const dropzone = this.querySelector('.dropzone');
60-
if (!dropzone) {
61-
return;
62-
}
63-
const uploadUrl = dropzone.getAttribute('data-upload-url');
64-
const dropzoneFiles = dropzone.querySelector('.files');
65-
for (const textarea of this.querySelectorAll('textarea')) {
66-
textarea.addEventListener('paste', async (e) => {
67-
for (const img of clipboardPastedImages(e)) {
68-
const name = img.name.slice(0, img.name.lastIndexOf('.'));
69-
insertAtCursor(textarea, `![${name}]()`);
70-
const data = await uploadFile(img, uploadUrl);
71-
replaceAndKeepCursor(textarea, `![${name}]()`, `![${name}](/attachments/${data.uuid})`);
72-
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
73-
dropzoneFiles.appendChild(input[0]);
74-
}
75-
}, false);
76-
}
58+
const dropzone = $target[0].querySelector('.dropzone');
59+
if (!dropzone) {
60+
return;
61+
}
62+
const uploadUrl = dropzone.getAttribute('data-upload-url');
63+
const dropzoneFiles = dropzone.querySelector('.files');
64+
$(document).on('paste', '.CodeMirror', async function (e) {
65+
const img = clipboardPastedImages(e.originalEvent);
66+
const name = img[0].name.substring(0, img[0].name.lastIndexOf('.'));
67+
insertAtCursor(this.CodeMirror, `![${name}]()`);
68+
const data = await uploadFile(img[0], uploadUrl);
69+
replaceAndKeepCursor(this.CodeMirror, `![${name}]()`, `![${name}](/attachments/${data.uuid})`);
70+
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
71+
dropzoneFiles.appendChild(input[0]);
7772
});
7873
}
7974

0 commit comments

Comments
 (0)