Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes persistence of template vars #1109

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/RichTextEditor/RichTextEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const RichTextEditor = ({
templateVariables,
}) => {
const oneLineExtension = isOneLine ? [OneLineLimit] : [];
const hasTemplateVariables = templateVariables.length > 0;

const requiredExtensions = [
Document,
Expand All @@ -75,7 +76,7 @@ const RichTextEditor = ({
}),
];

const templateVariablesExtension = templateVariables.length > 0 ?
const templateVariablesExtension = hasTemplateVariables ?
[TemplateVariable.configure({
HTMLAttributes: {
class: 'RichTextEditor__TemplateVariable',
Expand Down Expand Up @@ -134,6 +135,19 @@ const RichTextEditor = ({
options.allowedTags = allowedTags;
}

// When using template variables, we need to whitelist some specific attributes so that
// the editor can re-initialize correctly from db state
if (hasTemplateVariables) {
options.allowedAttributes = {
...(options.allowedAttributes || {}),
span: ['class', 'data-id', 'data-type'],
};

if (options.allowedTags && !options.allowedTags.includes('span')) {
options.allowedTags.push('span');
}
}

const sanitizedHtml = sanitizeHtml(html, options);

onChange(sanitizedHtml);
Expand Down
38 changes: 12 additions & 26 deletions src/RichTextEditor/TemplateVariable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@
useState,
} from 'react';
import { mergeAttributes, Node } from '@tiptap/core';
import { DOMOutputSpec, Node as ProseMirrorNode } from '@tiptap/pm/model';
import { Node as ProseMirrorNode } from '@tiptap/pm/model';
import { PluginKey } from '@tiptap/pm/state';
import Suggestion, { SuggestionOptions } from '@tiptap/suggestion';
import { ReactRenderer } from '@tiptap/react';

import './TemplateVariable.scss';

export type TemplateVariableOptions = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
HTMLAttributes: Record<string, any>;

Check warning on line 18 in src/RichTextEditor/TemplateVariable.tsx

View workflow job for this annotation

GitHub Actions / js-lint

Unexpected any. Specify a different type
renderText: (props: { options: TemplateVariableOptions; node: ProseMirrorNode }) => string;
renderHTML: (props: { options: TemplateVariableOptions; node: ProseMirrorNode }) => DOMOutputSpec;
renderLabel: (props: { options: TemplateVariableOptions; node: ProseMirrorNode }) => string;
suggestion: Omit<SuggestionOptions, 'editor'>;
}

Expand All @@ -30,16 +28,9 @@
addOptions() {
return {
HTMLAttributes: {},
renderText({ node }) {
renderLabel({ node }) {
return `{{ ${node.attrs.label ?? node.attrs.id} }}`;
},
renderHTML({ node }) {
return [
'span',
this.HTMLAttributes,
`{{ ${node.attrs.label ?? node.attrs.id} }}`,
];
},
suggestion: {
char: '/',
pluginKey: TemplateVariablePluginKey,
Expand Down Expand Up @@ -127,23 +118,18 @@
},

renderHTML({ node, HTMLAttributes }) {
const html = this.options.renderHTML({
options: this.options,
node,
});

if (typeof html === 'string') {
return [
'span',
mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
html,
];
}
return html;
return [
'span',
mergeAttributes({ 'data-type': this.name }, this.options.HTMLAttributes, HTMLAttributes),
this.options.renderLabel({
options: this.options,
node,
}),
];
},

renderText({ node }) {
return this.options.renderText({
return this.options.renderLabel({
options: this.options,
node,
});
Expand Down
Loading