自定义属性在从已设计的模板插入到Tiptap编辑器时被移除。

huangapple go评论72阅读模式
英文:

Custom attributes gets removed when inserted from already designed template in to tiptap editor

问题

描述
我尝试插入一个预定义的模板,其中包含根据元素需要的自定义属性,比如data-element-name,在插入到tiptap编辑器中时被删除。此外,一些HTML元素也以类似的方式被删除。

我们如何防止这些元素属性被删除?

屏幕截图、视频或GIF

  1. 预期插入到tiptap编辑器时的元素结构。
    自定义属性在从已设计的模板插入到Tiptap编辑器时被移除。
  2. 当tiptap编辑器修改它时的元素结构。
    自定义属性在从已设计的模板插入到Tiptap编辑器时被移除。

初始内容是通过在配置内容中使用content键插入的。

英文:

Description
I was trying to insert a pre-defined template which contains custom attributes for each elements based on the element needs like data-element-name are getting removed when inserted inside the tiptap editor. Also some of the html elements are also getting removed in the similar manner.

How can we prevent the removal of these element attributes from being getting removed?

Screenshot, video, or GIF

  1. Element structure which expected to be as when data is being inserted to tiptap editor.
    自定义属性在从已设计的模板插入到Tiptap编辑器时被移除。
  2. Element structure when tiptap editor modifies it.
    自定义属性在从已设计的模板插入到Tiptap编辑器时被移除。

Initial Content was inserted from the template by using content key in the configure content

答案1

得分: 1

Tiptap(以及底层的ProseMirror)编辑器确保在加载到编辑器中时,任何未显式列入白名单的标记,包括属性、标签等,都会被去除。这种行为有助于保持标记的干净。这意味着您需要显式列入白名单这些属性(只列入您需要的属性)。

您可能正在使用Tiptap的标题扩展来处理标题标记,您可以扩展Tiptap扩展以将这些属性列入白名单 -

import { Heading as TipTapHeading } from "@tiptap/extension-heading";

export const Heading = TipTapHeading.extend({
  addAttributes() {
    return {
      id: {
        default: null, // 如果您需要不同的默认值,请更改此值
        parseHTML: (element: HTMLElement) => {
          // 如果存在标记值,则从标记中获取属性值,否则将属性值设为null
          return element.hasAttribute("id") ? element.getAttribute("id") : null;
        },
      },
      "data-element-name": {
        default: null, // 如果您需要不同的默认值,请更改此值
        parseHTML: (element: HTMLElement) => {
          // 如果存在标记值,则从标记中获取属性值,否则将属性值设为null
          return element.hasAttribute("data-element-name") ? element.getAttribute("data-element-name") : null;
        },
      }
    };
  }
});

然后在编辑器扩展列表中使用这个扩展,并从Tiptap中删除Heading扩展 -

import { Editor } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
// import Heading from '@tiptap/extension-heading';
import Heading from './path/to/your/extension/file';

const editorInstance = new Editor({
  extensions: [
    Document,
    Paragraph,
    Text,
    Heading.configure({
      levels: [1, 2, 3],
    }),
  ],
});

同样,对于从最终标记中移除的标记,您需要检查相关扩展是否在编辑器配置中(参见NodesMarks),如果没有,请添加它们。如果找不到支持您的标记的任何扩展,那么您需要创建自己的扩展。

此外,请查看自定义扩展文档扩展文档,了解在扩展现有扩展或创建新扩展时可以使用的其他选项。

英文:

Tiptap (and the underlying ProseMirror) editor ensures that any markup, including attributes, tags etc., that is not explicitly whitelisted gets stripped off when it's loaded in the editor. This behaviour is desirable to keep your markup clean. That means you will need to whitelist these attributes explicitly (only the ones you want).

You are probably using the Heading extension from Tiptap to handle your heading tags, you can extend the Tiptap extension to whitelist these attributes -

import { Heading as TipTapHeading } from "@tiptap/extension-heading";

export const Heading = TipTapHeading.extend({
  addAttributes() {
    return {
      id: {
        default: null, // change this if you want a different default value
        parseHTML: (element: HTMLElement) => {
          // Get the attribute value from markup if it's available, otherwise assign null as the attribute value
          return element.hasAttribute("id") ? element.getAttribute("id") : null;
        },
      },
      "data-element-name": {
        default: null, // change this if you want a different default value
        parseHTML: (element: HTMLElement) => {
          // Get the attribute value from markup if it's available, otherwise assign null as the attribute value
          return element.hasAttribute("data-element-name") ? element.getAttribute("data-element-name") : null;
        },
      }
    };
  }
});

Then use this extension in editor extensions list and remove the Heading extension from Tiptap -

import { Editor } from '@tiptap/core';
import Document from '@tiptap/extension-document';
import Paragraph from '@tiptap/extension-paragraph';
import Text from '@tiptap/extension-text';
// import Heading from '@tiptap/extension-heading';
import Heading from './path/to/your/extension/file';

const editorInstance = new Editor({
  extensions: [
    Document,
    Paragraph,
    Text,
    Heading.configure({
      levels: [1, 2, 3],
    }),
  ],
});

Similarly, for the tags that are removed from the final markup, you'll need to check if the relevant extensions (see Nodes and Marks) are part of the editor config, if not add them. If you can't find any extension that supports your tag then you'll need to create your own extension.

Also, go through the custom extension documentation and extensions documentation to see other options you can use while extending an existing extension or creating a new one.

huangapple
  • 本文由 发表于 2023年3月31日 17:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定