I was intrigued by the idea that if a user pasted text into the composer a theme component could be made to highlight the text and let the reader know the author had pasted in the text so i decided to let ask.discourse take a crack at it
Here is what I learned. ask.discourse came to conclusion that making it work in the rich text editor would require a plugin because:
Theme components run in a sandboxed JS environment that only allows importing from a whitelist of
discourse/*modules. Raw npm packages likeprosemirror-stateandprosemirror-modelare not available to theme components. The full Ember build pipeline (which bundles ProseMirror) is only accessible to plugins.
^^^ that was pasted in from ask.discourse
So anyhow, I could not get it to work in the Theme Creator because I kept getting an error
An error occurred: You are not permitted to view the requested resource.
so i tried it on my own site. Here is the js that ask.discourse gave me
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
document.addEventListener("paste", function (e) {
const target = e.target;
if (!target || !target.matches(".d-editor-input")) return;
const pasted = e.clipboardData?.getData("text/plain");
if (!pasted) return;
e.preventDefault();
const wrapped =
"\n\n[wrap=pasted-text]\n\n" + pasted + "\n\n[/wrap]\n\n";
const inserted = document.execCommand("insertText", false, wrapped);
if (!inserted) {
const start = target.selectionStart;
const end = target.selectionEnd;
target.setRangeText(wrapped, start, end, "end");
target.dispatchEvent(new Event("input", { bubbles: true }));
}
}, true);
});
and here is the css
[data-wrap="pasted-text"] {
position: relative;
border: 1px solid var(--primary-low);
border-left: 4px solid var(--tertiary);
border-radius: 6px;
padding: 1.2em 1em 1em;
margin: 0.75em 0;
background: var(--primary-very-low);
}
[data-wrap="pasted-text"]::before {
content: "📋 Pasted text";
position: absolute;
top: -0.6em;
left: 12px;
background: var(--tertiary);
color: var(--secondary);
font-size: 0.7em;
font-weight: 700;
padding: 0.15em 0.65em;
border-radius: 3px;
letter-spacing: 0.3px;
text-transform: uppercase;
}
and here is the end results
Note: I don’t actually need this plugin or component!
my site isn’t very busy… I don’t even get bots trying to join ![]()
just thought it would be a fun exercise on a lazy Sunday afternoon
question for the community:
Do you think users would think twice about pasting text if they knew it was going to be obvious to everyone reading it that the text was pasted? And is this really the problem? when a bot creates a user and posts garbage, do they use the api and thus would bypass all my efforts?
