Hi - I have used the widget tutorial to successfully create a simple widget - all works great and tool bar button inserts the widget. However when I insert the same HTML from my own button ( using insertHTML ) - the widget doesn't exists until I switch to source mode and then back.
How do I "force" this to happen?
I have a similar situation when initially loading with externally created HTML widgets.
ta
You can completely customize
You can completely customize this process, however, depending on how much, it may also not be very easy :).
First step - if you want to use your own button, but insert widget in a standard way (as it would be done by the default widget button), just execute command named like a button. This command is created automatically for each widget:
In this case Widget System will take care of using template, creating widget element, wrapping it, initializing widget, opening dialog for it and inserting into document. All those steps you can find here: https://github.com/ckeditor/ckeditor-dev/blob/major/plugins/widget/plugin.js#L1239-L1341
If you want to customize this even more, then you'll need to execute these steps from your own plugin.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
thanks plus a few more questions...
Thanks, the highlighted code looks like it will point me in the right direction.. but I have a few more questions..
I am using the widget mechanism to manage some custom locked/read-only "tokens" that will be inside email templates. the tokens will be very small single <strong> tags with an ID and some text. So ID and text need to be parameters per instance.
So...
- I presume I can extend the code you pointed me at to take in the parameters and construct a specific instance of the widget.
- When my saved template is loaded can I hook into a loaded event and trigger the instantiation of all the widgets from the included HTML? - If I switch to source view then back to wysiwyg mode they do get created! but I can't figure out what event I need to manually trigger to perform what that did ( some sort of full re-parse? ).
Note - I have put a hack in my POC model that programatically does a mode change to source then back to wysiwyg mode whenever I insertHTML or load the editor - this actually works OK! (very quick screen flicker ) but there must be a better way to achieve the "re-parse"?
Thanks again
- I presume I can extend the
The widgetRepository#initOn method accepts startup data as a 3rd parameter. You can pass there your id and then in widgetDefinition#data use it to set some attribute on your element.
This is what Widget System will do for you when loading data if you properly definied upcasting. During runtime you need to initialize widgets on elements which you just inserted manually using the initOn method I mentioned earlier. If you're not using dialog, then it's pretty simple:
That's it.
Piotrek (Reinmar) Koszuliński
CKEditor JavaScript Developer
--
CKSource - http://cksource.com
--
Follow CKEditor on: Twitter | Facebook | Google+
thanks and a poss bug
Thanks - my read-only widgets are now getting created from my external button click PLUS after insertion the cursor is preserved at the correct place to continue typing - magic.
However, noticed that when creating the widget from the toolbar button the cursor gets lost.