Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add quote rich editor extension (#31151)

Renato Atilioddcbffc8c64e
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/quote.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/quote.js
new file mode 100644
index 00000000000..9d6b95173e1
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/quote.js
@@ -0,0 +1,127 @@
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ quote: {
+ content: "block+",
+ group: "block",
+ inline: false,
+ selectable: true,
+ isolating: true,
+ attrs: {
+ username: {},
+ postNumber: { default: null },
+ topicId: { default: null },
+ full: { default: null },
+ },
+ parseDOM: [
+ {
+ tag: "aside.quote",
+ contentElement: "blockquote",
+ getAttrs(dom) {
+ return {
+ username: dom.getAttribute("data-username"),
+ postNumber: dom.getAttribute("data-post"),
+ topicId: dom.getAttribute("data-topic"),
+ full: dom.getAttribute("data-full"),
+ };
+ },
+ },
+ ],
+ toDOM(node) {
+ const { username, postNumber, topicId, full } = node.attrs;
+ const attrs = { class: "quote" };
+ attrs["data-username"] = username;
+ attrs["data-post"] = postNumber;
+ attrs["data-topic"] = topicId;
+ attrs["data-full"] = full ? "true" : "false";
+
+ const domSpec = ["aside", attrs];
+
+ if (username) {
+ domSpec.push(["div", { class: "title" }, `${username}:`]);
+ }
+
+ domSpec.push(["blockquote", 0]);
+
+ return domSpec;
+ },
+ },
+ },
+
+ parse: {
+ quote_header_open(state, token, tokens, i) {
+ // removing the text child, this depends on the current token order:
+ // quote_header_open quote_controls_open quote_controls_close text quote_header_close
+ // otherwise it's hard to get a "quote_title" node to behave the way we need
+ // (a contentEditable=false node breaks the keyboard nav, among other issues)
+ tokens[i + 3].content = "";
+ },
+ quote_header_close() {},
+ quote_controls: { ignore: true },
+ bbcode_open(state, token) {
+ if (token.tag === "aside") {
+ state.openNode(state.schema.nodes.quote, {
+ username: token.attrGet("data-username"),
+ postNumber: token.attrGet("data-post"),
+ topicId: token.attrGet("data-topic"),
+ full: token.attrGet("data-full"),
+ });
+ return true;
+ }
+
+ // ignore the token (no-op), return as handled
+ if (token.tag === "blockquote") {
+ return true;
+ }
+ },
+ bbcode_close(state, token) {
+ if (token.tag === "aside") {
+ state.closeNode();
+ return true;
+ }
+
+ // ignore the token (no-op), return as handled
+ if (token.tag === "blockquote") {
+ return true;
+ }
+ },
+ },
+
+ serializeNode: {
+ quote(state, node) {
+ const postNumber = node.attrs.postNumber
+ ? `, post:${node.attrs.postNumber}`
+ : "";
+ const topicId = node.attrs.topicId ? `, topic:${node.attrs.topicId}` : "";
+ const quoteValue = node.attrs.username
+ ? `="${node.attrs.username}${postNumber}${topicId}"`
+ : "";
+
+ state.write(`[quote${quoteValue}]\n`);
+ state.renderContent(node);
+ state.write("[/quote]\n\n");
+ },
+ },
+ plugins({ pmState: { Plugin, NodeSelection } }) {
+ return new Plugin({
+ props: {
+ handleClickOn(view, pos, node, nodePos, event) {
+ if (
+ node.type.name === "quote" &&
+ event.target.classList.contains("title")
+ ) {
+ view.dispatch(
+ view.state.tr.setSelection(
+ NodeSelection.create(view.state.doc, nodePos)
+ )
+ );
+
+ return true;
+ }
+ },
+ },
+ });
+ },
+};
+
+export default extension;
diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
index a46e86c8bb8..83fa195368e 100644
--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/register-default.js
@@ -5,6 +5,7 @@ import heading from "./heading";
import image from "./image";
import link from "./link";
import mention from "./mention";
+import quote from "./quote";
import underline from "./underline";
/**
@@ -18,6 +19,7 @@ const defaultExtensions = [
image,
link,
heading,
+ quote,
hashtag,
mention,
underline,
diff --git a/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/quote-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/quote-test.gjs
new file mode 100644
index 00000000000..16cb61bdf29
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/integration/components/prosemirror-editor/quote-test.gjs
@@ -0,0 +1,38 @@
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
+
+module(
+ "Integration | Component | prosemirror-editor - quote extension",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ Object.entries({
+ "basic quote": [
+ `[quote]\nThis is a quote.\n\n[/quote]\n\nThis is not`,
+ `<aside class="quote" data-full="false"><blockquote><p>This is a quote.</p></blockquote></aside><p>This is not</p>`,
+ `[quote]\nThis is a quote.\n\n[/quote]\n\nThis is not`,
+ ],
+ "quote with username": [
+ `[quote="User"]\nQuoted text.\n\n[/quote]`,
+ `<aside class="quote" data-username="User" data-full="false"><div class="title">User:</div><blockquote><p>Quoted text.</p></blockquote></aside>`,
+ `[quote="User"]\nQuoted text.\n\n[/quote]\n\n`,
+ ],
+ "quote with topic ID": [
+ `[quote="User, topic:456"]\nQuoted from a topic.\n\n[/quote]`,
+ `<aside class="quote" data-username="User" data-topic="456" data-full="false"><div class="title">User:</div><blockquote><p>Quoted from a topic.</p></blockquote></aside>`,
+ `[quote="User, topic:456"]\nQuoted from a topic.\n\n[/quote]\n\n`,
+ ],
+ "quote with topic ID and post number": [
+ `[quote="User, post:123, topic:456"]\nFull quote example.\n\n[/quote]`,
+ `<aside class="quote" data-username="User" data-post="123" data-topic="456" data-full="false"><div class="title">User:</div><blockquote><p>Full quote example.</p></blockquote></aside>`,
+ `[quote="User, post:123, topic:456"]\nFull quote example.\n\n[/quote]\n\n`,
+ ],
+ }).forEach(([name, [markdown, html, expectedMarkdown]]) => {
+ test(name, async function (assert) {
+ this.siteSettings.rich_editor = true;
+ await testMarkdown(assert, markdown, html, expectedMarkdown);
+ });
+ });
+ }
+);