GetmeUK / ContentTools

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

Custom ToolBox - mark buttons as disabled / active #392

Closed dirkjacobs closed 6 years ago

dirkjacobs commented 7 years ago

Hi, started to build my own toolbox. I succeeded in calling the Std. Tools from my own buttons. As a next step I would like to 'disable'/'activate' the buttons, based on the current element and possible selection.

I know I should use the ContentTools.Tool.canApply(element, selection) and ContentTools.Tool.isApplied(element, selection) methods.

My question is : which listener should I install on the buttons to call these methods and change the state of the buttons based on the result of both methods. Or is there another method you're using ?

kr, Dirk

anthonyjb commented 7 years ago

Hi Dirk,

As you've identified you should override the canApply and isApplied methods in your custom tools. ContentTools.Tool is a static class that you should inherit from in order to create new tools (or inherit from one of the other tool classes that provides behaviour similar to what you want).

As for updating the tools within your own toolbox my recommendation would be that you take a look here: https://github.com/GetmeUK/ContentTools/blob/master/src/scripts/ui/toolbox.coffee#L481 which shows you how we currently use isApplied and requiresElement to determine if a tool should be enabled/disabled, and here: https://github.com/GetmeUK/ContentTools/blob/master/src/scripts/ui/toolbox.coffee#L178 which shows you how we currently efficiently update the status of each tool within the toolbox, as you can see at the end of this block (line 227) we're just calling this function periodically (every 10th of a second).

I hope that answers your question or helps but if I can help further perhaps you could provide me with an example of your code and we can go from there.

Ant

dirkjacobs commented 7 years ago

Hi Ant,

starting to get the feeling. I was wondering if there is no possibility to avoid the check every 100ms. Is there eg a possibility to send an event every time 'something changes', which could be the trigger to update the state of the buttons. (through an eventlistener)

D.

anthonyjb commented 7 years ago

Hi @dirkjacobs,

There isn't at the moment - the 100ms update is a compromise made because ultimately I didn't plan the diff/notification functionality well enough :/

Ideally it would be far better to be able to do this whenever something changes (and to be able to stack/buffer those changes into a single update so that it doesn't get triggered 100s of times).

Ant