Skip to content

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

FEATURE: rich editor extension for the policy plugin (#36631)

Renato Atilio1bfe1075754e
diff --git a/plugins/discourse-policy/assets/javascripts/discourse/initializers/rich-editor-extension.js b/plugins/discourse-policy/assets/javascripts/discourse/initializers/rich-editor-extension.js
new file mode 100644
index 00000000000..0ecb572c41f
--- /dev/null
+++ b/plugins/discourse-policy/assets/javascripts/discourse/initializers/rich-editor-extension.js
@@ -0,0 +1,156 @@
+import { camelize } from "@ember/string";
+import { camelCaseToDash } from "discourse/lib/case-converter";
+import { withPluginApi } from "discourse/lib/plugin-api";
+
+const POLICY_ATTRIBUTES = {
+ group: { default: null },
+ groups: { default: null },
+ version: { default: "1" },
+ accept: { default: null },
+ revoke: { default: null },
+ reminder: { default: null },
+ renewStart: { default: null },
+ private: { default: null },
+};
+
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ policy: {
+ attrs: POLICY_ATTRIBUTES,
+ group: "block",
+ content: "block*",
+ createGapCursor: true,
+ parseDOM: [
+ {
+ tag: "div.policy",
+ getAttrs: (dom) => {
+ const attrs = {};
+ Object.entries(dom.dataset).forEach(([key, value]) => {
+ if (value && value.trim() !== "" && value !== '""') {
+ attrs[camelize(key)] = value;
+ }
+ });
+ return attrs;
+ },
+ },
+ ],
+ toDOM(node) {
+ const attrs = { class: "policy" };
+
+ Object.entries(node.attrs).forEach(([key, value]) => {
+ if (value && value.trim() !== "" && value !== '""') {
+ const dataKey = camelCaseToDash(key);
+ attrs[`data-${dataKey}`] = value;
+ }
+ });
+
+ return ["div", attrs, 0];
+ },
+ },
+ },
+
+ nodeViews: {
+ policy: () => (node) => {
+ const dom = document.createElement("div");
+ dom.classList.add("policy");
+
+ Object.entries(node.attrs).forEach(([key, value]) => {
+ if (value && value.trim() !== "" && value !== '""') {
+ const dataKey = camelCaseToDash(key);
+ dom.setAttribute(`data-${dataKey}`, value);
+ }
+ });
+
+ // Create policy header
+ const header = document.createElement("div");
+ header.classList.add("policy-header");
+ header.textContent = "Policy";
+ dom.appendChild(header);
+
+ // Create content wrapper
+ const contentDOM = document.createElement("div");
+ contentDOM.classList.add("policy-body");
+ dom.appendChild(contentDOM);
+
+ return {
+ dom,
+ contentDOM,
+ update: (updatedNode) => {
+ if (updatedNode.type !== node.type) {
+ return false;
+ }
+
+ Object.entries(updatedNode.attrs).forEach(([key, value]) => {
+ const dataKey = camelCaseToDash(key);
+ if (value && value.trim() !== "" && value !== '""') {
+ dom.setAttribute(`data-${dataKey}`, value);
+ } else {
+ dom.removeAttribute(`data-${dataKey}`);
+ }
+ });
+
+ return true;
+ },
+ };
+ },
+ },
+
+ parse: {
+ wrap_bbcode(state, token) {
+ if (token.tag === "div") {
+ if (token.nesting === -1 && state.top().type.name === "policy") {
+ state.closeNode();
+ return true;
+ }
+
+ if (token.nesting === 1 && token.attrGet("class") === "policy") {
+ const attrs = {};
+ token.attrs?.forEach(([key, value]) => {
+ if (key.startsWith("data-")) {
+ const attrName = camelize(key.slice(5));
+ if (value && value.trim() !== "" && value !== '""') {
+ attrs[attrName] = value;
+ }
+ }
+ });
+
+ state.openNode(state.schema.nodes.policy, attrs);
+ return true;
+ }
+ }
+
+ return false;
+ },
+ },
+
+ serializeNode: {
+ policy(state, node) {
+ state.write("[policy");
+
+ Object.entries(node.attrs).forEach(([key, value]) => {
+ if (value && value.trim() !== "" && value !== '""') {
+ const kebabKey = camelCaseToDash(key);
+ state.write(` ${kebabKey}="${value}"`);
+ }
+ });
+
+ state.write("]\n");
+
+ if (node.content.size > 0) {
+ state.renderContent(node);
+ }
+
+ state.write("[/policy]\n\n");
+ },
+ },
+};
+
+export default {
+ name: "rich-editor-policy-extension",
+ initialize() {
+ withPluginApi((api) => {
+ api.registerRichEditorExtension(extension);
+ });
+ },
+};
diff --git a/plugins/discourse-policy/assets/stylesheets/common/discourse-policy.scss b/plugins/discourse-policy/assets/stylesheets/common/discourse-policy.scss
index 6691a5dd8d1..96b94e5fe41 100644
--- a/plugins/discourse-policy/assets/stylesheets/common/discourse-policy.scss
+++ b/plugins/discourse-policy/assets/stylesheets/common/discourse-policy.scss
@@ -145,12 +145,18 @@
}
// Policy in editor styles
-.d-editor-preview .policy {
+.d-editor-preview .policy,
+.ProseMirror .policy {
padding: 0.5em;
border: 1px solid var(--primary-low);
+ margin-block: var(--space-4);
.policy-preview {
font-size: var(--font-up-1);
font-weight: 700;
}
+
+ .policy-body {
+ padding: var(--space-2) 0;
+ }
}
diff --git a/plugins/discourse-policy/test/javascripts/integration/rich-editor-extension-test.js b/plugins/discourse-policy/test/javascripts/integration/rich-editor-extension-test.js
new file mode 100644
index 00000000000..e2a1f2fd3a6
--- /dev/null
+++ b/plugins/discourse-policy/test/javascripts/integration/rich-editor-extension-test.js
@@ -0,0 +1,121 @@
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { setupRichEditor } from "discourse/tests/helpers/rich-editor-helper";
+
+module(
+ "Integration | Component | prosemirror-editor - policy plugin extension",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.siteSettings.policy_enabled = true;
+ });
+
+ test("policy with multiple groups", async function (assert) {
+ const markdown = `[policy groups="staff,admins" version="2"]\nI accept the terms\n\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).hasAttribute("data-groups", "staff,admins");
+ assert.dom(policyElement).hasAttribute("data-version", "2");
+ assert.dom("p", policyElement).hasText("I accept the terms");
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("policy with all attributes", async function (assert) {
+ const markdown = `[policy group="staff" version="1" accept="true" revoke="false" reminder="daily" renew-start="2023-01-01" private="true"]\nComplex policy\n\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).hasAttribute("data-group", "staff");
+ assert.dom(policyElement).hasAttribute("data-version", "1");
+ assert.dom(policyElement).hasAttribute("data-accept", "true");
+ assert.dom(policyElement).hasAttribute("data-revoke", "false");
+ assert.dom(policyElement).hasAttribute("data-reminder", "daily");
+ assert.dom(policyElement).hasAttribute("data-renew-start", "2023-01-01");
+ assert.dom(policyElement).hasAttribute("data-private", "true");
+ assert.dom("p", policyElement).hasText("Complex policy");
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("policy with content around", async function (assert) {
+ const markdown = `Hello world\n\n[policy group="everyone" version="1"]\nI accept\n\n[/policy]\n\nGoodbye world`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).hasAttribute("data-group", "everyone");
+ assert.dom("p", policyElement).hasText("I accept");
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("empty policy", async function (assert) {
+ const markdown = `[policy group="staff" version="1"]\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).hasAttribute("data-group", "staff");
+ assert.dom(policyElement).hasAttribute("data-version", "1");
+ assert.dom("p", policyElement).doesNotExist();
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("policy with multiline content", async function (assert) {
+ const markdown = `[policy group="staff" version="1"]\nFirst line\n\nSecond paragraph\n\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom("p", policyElement).exists({ count: 2 });
+ assert.dom("p:first-child", policyElement).hasText("First line");
+ assert.dom("p:last-child", policyElement).hasText("Second paragraph");
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("policy filters out empty attributes", async function (assert) {
+ const inputMarkdown = `[policy group="staff" version="1" accept="" revoke=""]\nTest\n[/policy]`;
+ const expectedMarkdown = `[policy group="staff" version="1"]\nTest\n\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, inputMarkdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).hasAttribute("data-group", "staff");
+ assert.dom(policyElement).hasAttribute("data-version", "1");
+ assert.dom(policyElement).doesNotHaveAttribute("data-accept");
+ assert.dom(policyElement).doesNotHaveAttribute("data-revoke");
+ assert.dom("p", policyElement).hasText("Test");
+
+ assert.strictEqual(value, expectedMarkdown);
+ });
+
+ test("policy renders basic structure in rich editor", async function (assert) {
+ const markdown = `[policy group="staff" version="1"]\nI accept this policy\n\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).exists();
+ assert.dom(policyElement).hasClass("policy");
+ assert.dom(policyElement).hasAttribute("data-group", "staff");
+ assert.dom(policyElement).hasAttribute("data-version", "1");
+
+ // Check that content is preserved
+ assert.dom("p", policyElement).hasText("I accept this policy");
+
+ assert.strictEqual(value, markdown);
+ });
+
+ test("policy handles empty content", async function (assert) {
+ const markdown = `[policy groups="admins" version="2"]\n[/policy]\n\n`;
+ const [{ value }] = await setupRichEditor(assert, markdown);
+
+ const policyElement = document.querySelector(".ProseMirror .policy");
+ assert.dom(policyElement).exists();
+ assert.dom(policyElement).hasAttribute("data-groups", "admins");
+ assert.dom(policyElement).hasAttribute("data-version", "2");
+
+ assert.strictEqual(value, markdown);
+ });
+ }
+);