I’m trying to write a wrapper Component for Summernote (https://summernote.org/) - an HTML editor component.
I’ve written standalone Lit-based WebComponents for Vaadin already, including a corresponding Java Component. But Summernote requires jQuery and Bootstrap and I’m not really sure how to hook them up. Or if it can even work. E.g. to initialize the Summernote component it wants me to call
I assume that it would go into Lit’s firstUpdated() function, but I suspect that a re-render of the Lit component would remove everything that summernote(); added.
But I don’t event get this far. I’ve set up the basic Java type and corresponding .ts file and added several @NpmPackage annotations for Summernote, Bootstrap and jQuery. But at runtime it doesn’t find $. Or the summernote() function (if I replace $('#summernote') with this.shadowRoot.getElementById('summernote')).
Does anyone have experience with this or does anyone know other OSS projects that do something like that (use jQuery and/or Bootstrap)?
Oh, because summernote() isn’t a function somehow attached to the div, but provided by that jQuery-object that wraps the div!
@query(‘#summernote’)
div: HTMLDivElement;
And later:
$(this.div).summernote();
seems to work or at least do something now :) Not sure if it will ultimately work properly like this way, but I can still fallback to do things globally for this application, I suppose. And find a different library that doesn’t use jQuery to create a reusable component.
The jQuery is a bit a trouble maker as it is starts to be legacy and does not behave that well alongside other modern stuff. One option to use it is like in this add-on, where I am importing it as static resource instead of having it in frontend bundle. The negative side effect of this is that you will have trouble if you have more than one jQuery based add-on due possible version conflicts etc.
And if multiple add-ons use @NpmPackage to download the same package, but a different version, all add-ons will use the resolved version and not necessarily their own version, right?
Even with a frontend bundle, would it even be possible to use several versions of jQuery in the application?
No, it shouldn’t be, as npm will install only the newest of the dependent versions. But there will be likely be a problem with vite not able to process old libraries to bundle.
But npm should be able to download and install only one version or you can force it (like we did for the vaadin components), the rest (if the library is working with this version) is magic