Supporting a headless editor
(@Reinmar asked me to move my comments from the Gitter chat to a GitHub issue. As such, this report does not follow the standard template.)
Here's some feedback regarding our experience with creating a headless editor. We've essentially adapted the inline editor to become headless and we haven't bothered to remove most of the UI dependencies. That increases the size of the bundle we need to distribute, but we're mostly fine with it (364 kB or 100 kB with gzip), so we won't pursue this further.
There does appear to be a requirement that editors have user interfaces from a programmatic point of view in that they need to feature a "ui" property. We didn't have much luck mocking it out as we did with the alpha: https://github.com/ckeditor/ckeditor5/issues/488#issuecomment-335961991. Using a real "ui" implementation seemed like the best course of action, though we take care to ensure that no user interface is actually rendered.
We tried to use the sample headless Bootstrap implementation featured in the documentation, but didn't have much luck with it. Our use case does appear to be different from the one assumed in the documentation -- we want to define the user interface of the editor completely outside of the CKEditor 5 build (and control it through an AngularJS ES5 codebase).
Finally, we had severe difficulties with npm 5 (thanks @Reinmar for pointing out that we should downgrade in https://github.com/ckeditor/ckeditor5/issues/957)
There does appear to be a requirement that editors have user interfaces from a programmatic point of view in that they need to feature a "ui" property. We didn't have much luck mocking it out as we did with the alpha: #488 (comment). Using a real "ui" implementation seemed like the best course of action, though we take care to ensure that no user interface is actually rendered.
That's strange. We use plenty of headless editors in our tests and they are completely stripped out of the editing (view) layer. They have the model, the selection in the model, they operate on that model (execute commands, alter the content), they accept and return data.
We tried to use the sample headless Bootstrap implementation featured in the documentation, but didn't have much luck with it.
Can you tell us more? Errors? Exact problems? Did you use https://ckeditor5.github.io/docs/nightly/ckeditor5/latest/framework/guides/ui/external-ui.html as a starting point?
Olek's right – we do use plenty of completely headless editors. Actually, the editor Olek pointed you to doesn't even render the content (the editable) AFAIR.
This is also reflected in the editor interfaces, which we cleaned up recently. The base class (Editor) doesn't define the UI's entry point. That interface is added later in specific editor implementations (by implementing EditorWithUI).
Now, all features are split into two parts – the FeatureEditing and FeatureUI. The interface of the Editor class is enough for the *Editing part of features. Only the *UI part of features require the EditorWithUI interface.
For example:
BoldEditingimplements schema definition, converters, a command and a keystroke.BoldUIimplements a button.- In order to not force people to always import the above two plugins, there's also
Boldwhich requires these two and hence enables the whole feature.
Note, there's no direct dependency between BoldEditing and BoldUI other than a convention for the command name. You can e.g. take BoldUI but implement a different BoldEditing, or you can take BoldEditing while implementing your own BoldUI or just take BoldEditing and use it programmatically.
You want to implement a headless editor. So, in your case, you don't want to implement the EditorWithUI interface and you don't want to load the *UI part of features. This should completely remove the @ckeditor/ckeditor5-ui dependency from your code.
PS. you can find a bit more about feature split in https://github.com/ckeditor/ckeditor5/issues/488#issuecomment-338616321
BTW, this ticket DUPs a bit https://github.com/ckeditor/ckeditor5/issues/438, so once we'll clarify all the things, let's close it.
Thanks for providing feedback. I instructed Webpack to generate statistics (webpack
--profile --json > /tmp/webpack-stats.json), disabled ModuleConcatenationPlugin in webpack.config.js and analyzed the module dependency graph using an official tool.
It turned out that I was indeed importing a @ckeditor/ckeditor5-ui module and that the culprit was the @ckeditor/ckeditor5-essentials package, which imports @ckeditor/ckeditor5-undo/src/undo which in turn depends on the Undo UI feature. I incorrectly assumed that this package would not bring in UI dependencies. Not bringing in @ckeditor/ckeditor5-ui only saves 12 kB, though (compressed using gzip).
I attempted to follow the Bootstrap guide, but there does appear to be a @ckeditor/ckeditor5-ui dependency there. The ElementReplacer utility class is used to replace the original element with editor.ui.view.editable.element, where the view.editable property of the UI class is of type InlineEditableUIView -- from the @ckeditor/ckeditor5-ui package.
At that point, I gave up and merrily accepted the extra 12 kB that the @ckeditor/ckeditor5-ui package brings.
I attempted to follow the Bootstrap guide, but there does appear to be a @ckeditor/ckeditor5-ui dependency there. The ElementReplacer utility class is used to replace the original element with editor.ui.view.editable.element, where the view.editable property of the UI class is of type InlineEditableUIView -- from the @ckeditor/ckeditor5-ui package.
If the editor has an editable in DOM, it should use the (Inline)EditableUIView class at some point. It brings not only necessary classes (.ck-content .ck-editor__editable), but also focus (watch editor.ui.focusTracker.isFocused) and read–only
(try editor.isReadOnly = true) bindings.
AFAIR (Inline)EditableUIView imports only a subset of the ckeditor5-ui package: View, Template and a couple of helpers; base classes only, no components.
You don't have to use the ElementReplacer if you don't want to. It's not vital for the editor to bootstrap. You can handle it on your own depending on how your editor loads data and where it is used in your application.
Yeah, I forgot about the editable. And I agree with @oleq that the easiest way to implement it is through InlineEditableUIView. This will add ~700SLOC, so you won't notice it :)
Thanks, @oleq and @Reinmar. I don't mind bringing in parts of the ckeditor5-ui package. You're welcome to close this issue now; hopefully our discussion will prove useful to others looking to implement a headless editor.
Thanks! We can continue discussing on remaining things in https://github.com/ckeditor/ckeditor5/issues/438.
I attempted to follow the Bootstrap guide, but there does appear to be a @ckeditor/ckeditor5-ui dependency there. The ElementReplacer utility class is used to replace the original element with editor.ui.view.editable.element, where the view.editable property of the UI class is of type InlineEditableUIView -- from the @ckeditor/ckeditor5-ui package.
If the editor has an editable in DOM, it should use the
(Inline)EditableUIViewclass at some point. It brings not only necessary classes (.ck-content .ck-editor__editable), but also focus (watcheditor.ui.focusTracker.isFocused) and read–only (tryeditor.isReadOnly = true) bindings.AFAIR
(Inline)EditableUIViewimports only a subset of theckeditor5-uipackage:View,Templateand a couple of helpers; base classes only, no components.You don't have to use the
ElementReplacerif you don't want to. It's not vital for the editor to bootstrap. You can handle it on your own depending on how your editor loads data and where it is used in your application.
It is possible to use ckeditor directly on the dom without the EditableUIView, focus tracker, any UI whatsoever. By using editor.editing.view.attachDomRoot(el, rootName). Of course you dont get anything this way, but it is good for some use cases like mine.