jspreadsheet / ce

Jspreadsheet is a lightweight vanilla javascript plugin to create amazing web-based interactive tables and spreadsheets compatible with other spreadsheet software.
https://bossanova.uk/jspreadsheet/v4
MIT License
6.66k stars 818 forks source link

API added async support #1667

Closed minettiandrea closed 3 months ago

minettiandrea commented 5 months ago

Hi, I needed to use some async libraries in combination with jspreadsheet, i.e. to do some validation on cell content change, but with the actual API was not possible, so I've tried to implement it and with a small effort I was able to add it to the library, it should be backward compatible.

The only glitch that I found is that by using heavily Promise.resolve when we call setValue and just after getValue setting the value is postponed by the event loop so you wont get the expected value (see data.js test) that's why I've added a Promise as return value of setValue if someone really need to know when the value is actually set, but I would argue that even in the previous API that wasn't guaranteed.

Hope that my contribution could be helpful and looking forward to integrate jspreadsheet on my project

hodeware commented 5 months ago

We will consider that suggestion. But implementing it might require significant changes in the next version which are under the way. However, there is an alternative approach. By using onbeforechange, you can either return a temporary value or cancel the action and then trigger an API. The promise will subsequently update what is necessary. Essentially, this method allows you to block native updates and instead trigger your custom requirements. For a better understanding, here's a very basic example: https://jsfiddle.net/6j3tfzum/2/.

hodeware commented 5 months ago

Additionally, the functionality will be expanded to include plugins in the next version. These plugins will allow for the creation of custom behavior encapsulated within individual plugins. This means that methods like 'onBeforeChange' will be included within the plugin, removing the need to declare events each time.