GetmeUK / ContentTools

A JS library for building WYSIWYG editors for HTML content.
http://getcontenttools.com
MIT License
3.94k stars 393 forks source link

Can not edit / delete custom elements #545

Closed Jakuboslav closed 4 years ago

Jakuboslav commented 4 years ago

Hello, I have an issue, and I can not find solution. I created custom tool, for adding related content. It is simple div, with some data. I didn't use new ContentEdit.Static() anywhere in this tool.

When I create element via this tool, I can remove it and it works nice. But when I save the page and load it again, Content tools applies on this element static class and it can not be deleted. Simply it behaves as Static.

I recorded sample video of this behavior. So you can see what I am talking about.

Is there any special property I have to attach to this element to make it editable everytime?

Thanks in advance.

anthonyjb commented 4 years ago

You will need to tell CT (or ContentEdit really) how to recognise this element when parsing the page. By default div wont be mapped to any ContentEdit.Element class and so will be default parsed as a static element.

For example ContentEdit.Text element is assigned to the following HTML tags:

ContentEdit.TagNames.get().register(
    ContentEdit.Text,
    'address',
    'blockquote',
    'h1',
    'h2',
    'h3',
    'h4',
    'h5',
    'h6',
    'p'
)

Ref: https://github.com/GetmeUK/ContentEdit/blob/master/src/scripts/text.coffee#L532

You'd need to do something similar for your element, however, since you're using a div I'd recommend using a custom element type name, for example make sure your div is always rendered with the tag data-ce-tag="mycustomelement":

<div data-ce-tag="mycustomelement">...</div>

And then register your custom tag with your custom element:

ContentEdit.TagNames.get().register(
    MyCustomElement,
    'mycustomelement'
)
Jakuboslav commented 4 years ago

Thanks, its solved my issue.