Issue #2188Opened August 11, 2019by TheComputerM4 reactions

[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)

TheComputerMAugust 11, 20192 reactions

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,
    },
],
BuddhiliveMay 6, 20242 reactions

@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,
                        },
                    ],
                },
            },
});
hosseinghsSeptember 18, 20220 reactions

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.