From 8343fd23656a65cc77a004cf1e588563c1a7fcf1 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Wed, 15 Nov 2023 11:48:30 -0500 Subject: [PATCH] Fix issue processwire/processwire-issues#1818 fix for front-end editor (PageFrontEdit) plain text paste Co-authored-by: BernhardBaumrock --- .../Page/PageFrontEdit/PageFrontEdit.js | 39 +++++++++++++++++++ .../Page/PageFrontEdit/PageFrontEdit.min.js | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/wire/modules/Page/PageFrontEdit/PageFrontEdit.js b/wire/modules/Page/PageFrontEdit/PageFrontEdit.js index d9b69c71..9a53e61f 100644 --- a/wire/modules/Page/PageFrontEdit/PageFrontEdit.js +++ b/wire/modules/Page/PageFrontEdit/PageFrontEdit.js @@ -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 = $('