[QUESTION] How to change tagName using traits.
Question
Hello, I want to achieve this: When user clicks on a heading tag H1, H2, H3 ... I want a trait to appear (of a select / dropdown type) which can allow the tagName of the heading to be changed
Answers (3)
I solved it after scanning the internet a bit. Add this trait
traits: [
{
type: 'select',
options: [
{ value: 'h1', name: 'Heading 1' },
{ value: 'h2', name: 'Heading 2' },
{ value: 'h3', name: 'Heading 3' },
{ value: 'h4', name: 'Heading 4' },
{ value: 'h5', name: 'Heading 5' },
{ value: 'h6', name: 'Heading 6' },
],
label: 'Size',
name: 'tagName',
changeProp: 1,
},
],
@hosseinghs try this out. This is same as how @TheComputerM implemented the code before. And it works fine.
this.editor.Components.addType('headings', {
isComponent: el => el.tagName.startsWith('H') && Number.parseInt(el.tagName.slice(1)) <= 6,
model: {
defaults: {
traits: [
{
type: 'select',
options: [
{ id: 'h1', value: 'h1', name: 'Heading 1' },
{ id: 'h2', value: 'h2', name: 'Heading 2' },
{ id: 'h3', value: 'h3', name: 'Heading 3' },
{ id: 'h4', value: 'h4', name: 'Heading 4' },
{ id: 'h5', value: 'h5', name: 'Heading 5' },
{ id: 'h6', value: 'h6', name: 'Heading 6' },
],
label: 'Size',
name: 'tagName',
changeProp: true,
},
],
},
},
});
hi, I'm trying to do the same! my problem is when I click one of the options from traits, my heading element won't be updated to new heading! can you help me?
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1780
How to change extra dropdown for link change trait
Hello @artf , I want to give one more functionality to user like user can change any link with default links drop down. In above image i wa...
Issue #2193
[QUESTION] Reset trait on edit the content of a custom component
Hi, I have a custom component which content can be changed by switching trait select options, but also can be edited similar to the text co...
Issue #2404
Question: how to observe children changes in component
Is there an easy way to subscribe/listen to changes in the components/children of my custom component type? Perhaps an event I can subscrib...
Issue #3019
[QUESTION] - Bind inner text/html (like div, p, h1) to Trait
Hi! I've created a custom trait, and I want to be allowed to change the content of the tags (in this case it's a H1 tag) based on the trait...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.