Hi, first time developing on Discourse, so sorry for the silly questions.
I’m building a theme component to display PGNs (chessboard and matches).
The component is not theoretically difficult, there is a js component for this (PGNViewer.js) and I was able to translate the [wrap] tags into the proper <script>var x='pgn here'; ...</script> and <div class='pgn-blahblah'></div> blocks.
So far so good, but now I need to load the js.
I found that I can use import loadScript from "discourse/lib/load-script"; and await loadScript(settings.theme_uploads_local.pgnviewer_js); to load it at initialization time from the /assets/ directory.
But I have a few problems here.
Despite I’m able to load the component on the dev installation, I can’t on my production server, because it says the asset is too big. Is there a way to fix this?
Second, on the dev installation, I see a warning stating it couldn’t load /theme-javascripts/dist.js.map. I have no idea where it comes from. Is there something I should provide?
Overall, is this approach correct, or would it be better to implement a plugin instead?
Thanks
kind regards
I’m in doubt, probably because I’ve zero experience with such platforms.
My doubt is:
my approach is to “embed” the small snippet of javascript needed to fill the div block with the content, but I see that maybe I could execute it from a function “cookPgn”, like it is done in the marmaid component:
This way, if I understand correctly, I don’t need to load the script from the page (using loadScript perhaps??) but I can just import it in the theme js? Or likely I’m totally mislead!
I mean, what’s the difference between the loadScript and a simple import?
Can you address me to some web resource where I can better understand this?
Sorry again if the questions are trivial or stupid!
Another thing, the limit is by default to 10M, but my asset is around 300kb, and the whole zip is 337kb, still during upload it says it is mre than 512kb.
Thanks @pfaffman , indeed I took inspiration on it, and I noticed that it does exactly what I need and in the correct way, without injecting the code as suggested above.
Thanks all, I’ll try with your suggestions and get back with results. Of course my code is open sourced, so any contribution or critic is very welcome.
Ok I did fix everything and the component does work! But we have a problem, an “unsafe eval” in the library I’m using. I guess I should fix the upstream library, I have no idea how to pack it.
For those interested, I pushed all my changes in my repo above, and here are the details of the error.
Ok I did it, just I needed to use the ’ … lol… Anyway, My script almost worked, I pushed everything on main and it somehow worked, it seems i have an issue with rendering but at least the main component is called (I suspect where the problem is).
Thanks all… I’ll be back soon with more questions LOL. Thanks!
Yes, defintely. I’m unable to fix it at the moment, but my plan is to first fix my component, then fix the upstream library and remove the CSP override.
Thanks for the advice!
I’m almost done. I was able to display “something” but it is working randomly.
The js library I’m using works this way:
First we need to define a <div id='board'> block with a unique id. then we need to launch a function to populate the div, something like this. PGNV.pgnBoard('board', {});. The script requires the same id to populate it.
If I understand correctly, I cannot do this while decorating the cooked block, because the document is not yet populated and the PGNV object is unable to find the block. So I used first a widget (doesn’t work, because the HTML function must return the block, and I don’t have it), then I switched to a debounced function: debounceFunction(this, renderPgn, attrs, 200);.
debounceFunction delay calling the target method until the debounce period has elapsed with no additional debounce calls, it sometimes won’t work may still because
Hi Hawn, thank your for your hint!
It seems exactly what I was looking for. I tried two different approach:
first use the normal “decorateCooked” to parse and attach the div block and then the afterAdopt to render by calling PGNV.pgnviewer() or
use only the afterAdopt option to both attach and render
none worked for yet another issue that I’m, unable to fully grasp, it seems the inner library crashes with an error, unable to find className attribute apparently in a DOM element. I’m a bit lost . I’ve seen that someone use loadScript, but I just import the library and using the debounce function just worked. What is the difference between the two methods?
Hi @hawm , I’ve found why it doesn’t work and I’m stuck.
Of course I’m totally new on Ember and on recent js in general. So I studied a bit and find out the reason.
PgnViewerJs expect to work on real DOM, not on Virtual DOM. So even after the cooked element has been adopted, it isn’t present in the real DOM, and PGNV search for IDs by using document.getElementById.
I studied everything I could find out: ember components seems to fit my needs, but I’m unsure how to handle it.
Or perhaps API.onAppEvent ?