admin管理员组文章数量:1320614
I'm stuck trying to acplish what I'd imagine is a very simple task in TipTap 2.0. I'm trying to add a class to a selected paragraph. The code I'm trying is below:
this.editor.chain().focus().updateAttributes('paragraph', {class:'lead'});
I want this to be editable per paragraph. I don't want all paragraphs to have the same class added.
I'm stuck trying to acplish what I'd imagine is a very simple task in TipTap 2.0. I'm trying to add a class to a selected paragraph. The code I'm trying is below:
this.editor.chain().focus().updateAttributes('paragraph', {class:'lead'});
I want this to be editable per paragraph. I don't want all paragraphs to have the same class added.
Share Improve this question asked Feb 27, 2023 at 14:09 alexmcfarlanealexmcfarlane 1,1482 gold badges16 silver badges37 bronze badges2 Answers
Reset to default 7So after some further investigation, I have found a solution for this. My initial assumption was incorrect. I thought that you use updateAttributes to directly add a class to the paragraph. But you need to extend Paragraph with your own 'class' attribute, then you can amend the HTML using renderHTML.
const CustomParagraph = Paragraph.extend({
addAttributes() {
return {
class: {
default: null,
// Take the attribute values
renderHTML: attributes => {
// … and return an object with HTML attributes.
return {
class: `${attributes.class}`,
}
},
},
}
},
})
this.editor = new Editor({
element: this.tiptapEditorTarget,
extensions: [
CustomParagraph,
],
});
this.editor.mands.updateAttributes('paragraph', {class:'lead'});
Alternatively, if you'd like decorate only the focused paragraph, you can use the existing "focus" extension, with minimal setup: https://tiptap.dev/docs/editor/extensions/functionality/focus
// extensions configuration
focus.configure({
className: "focused",
}),
/** CSS file */
p.focused {
color: black
}
本文标签: javascriptAdd a class to a paragraph node in TipTapStack Overflow
版权声明:本文标题:javascript - Add a class to a paragraph node in TipTap - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742073037a2419243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论