1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Fix issue processwire/processwire-issues#1818 fix for front-end editor (PageFrontEdit) plain text paste

Co-authored-by: BernhardBaumrock <office@baumrock.com>
This commit is contained in:
Ryan Cramer
2023-11-15 11:48:30 -05:00
parent c86d39f146
commit 8343fd2365
2 changed files with 40 additions and 1 deletions

View File

@@ -379,6 +379,43 @@ function PageFrontEditInit($) {
}); });
} }
/**
* Paste event
*
* @param e
*/
function pasteEvent(e) {
// only intercept pasting to frontend editable text fields
if(!document.activeElement.isContentEditable) return;
var wrap = $(document.activeElement).closest('.pw-edit');
if(!wrap.length) return;
var usePlainText = false;
var plainTextTypes = [ 'Text', 'Textarea', 'PageTitle', 'Email', 'Float', 'Integer', 'URL' ];
for(var n = 0; n < plainTextTypes.length; n++) {
usePlainText = wrap.hasClass('pw-edit-Inputfield' + plainTextTypes[n]);
if(usePlainText) break;
}
if(usePlainText) {
// Prevent the default paste action
e.preventDefault();
// Get the clipboard data as plain text
var clipboardData = (e.originalEvent || e).clipboardData;
var pastedText = clipboardData.getData('text/plain');
// Convert any markup in the pasted text to entity-encoded plain text
var plainText = $('<textarea />').text(pastedText).html();
// Insert the plain text into the contenteditable element or textarea
document.execCommand('insertHTML', false, plainText);
}
}
/** /**
* Initialize PageFrontEdit * Initialize PageFrontEdit
* *
@@ -452,6 +489,8 @@ function PageFrontEditInit($) {
inlineSaveClickEvent(); inlineSaveClickEvent();
}, 250); }, 250);
}); });
document.addEventListener('paste', pasteEvent);
} }
init(); init();

File diff suppressed because one or more lines are too long