Codox provides a separate plugin for sync, following a lexical approach for designing external features and plugins. Plugin should be initialized as any other lexical plugin.
Codox plugin provides api to control sync start/stop via built-in React ref
Codox plugin implements a rollback mechanism after lexical core errors. Rollback is restoring editor state to previous. Plugin catches errors thrown from lexical editor. If client’s callback “onError” was passed into editor config, plugin will allow it to trigger and then will perform rollback.
Codox sync supports undo/redo. It relies on a built-in lexical undo/redo implemented with lexical HistoryPlugin provided by the official package “@lexical/react/LexicalHistoryPlugin”. For undo/redo support in editor, should init HistoryPlugin along with Codox plugin. Any other custom client’s implementations of undo/redo can be unstable with respect to sync.
Codox sync implements the restrictions of inserted content - the blacklisted content combinations, which are not allowed to be inserted into the editor. Codox plugin prevents inserting such combinations. See Content Blacklisting section for blacklist details.
Codox sync supports sync of all lexical nodes types from lexical core and all custom nodes from the official playground. Dynamic support of other custom nodes is in progress.
Codox plugin accepts init state during sync start and applies it to editor, then on content changes, plugin will trigger client’s callback passed as part of plugin configuration. Client’s app should not do any direct update ops to editor state. Read ops are not limited.
Codox provides an additional helper util to validate editor state json. Checks if state is valid for lexical. Throws error if state is invalid
Codox plugin has built-in mechanism to validate and fix lexical tables structures, if lexical core produces any incorrect table structure. This logic is encapsulated and prevents output of broken table structures from lexical
Requirements for Codox sync integration
Codox plugin must be initialized in the scope Lexical Composer context. If it’s not, plugin with throw error.
Initial state, fetched from client’s backend, should be passed to codox plugin when invoking sync start. Client’s app must delegate applying init state to codox plugin and should not apply it directly to sync - this may cause incorrect sync.
Sync must be started only after init state has already been fetched from backend
All lexical nodes which are to be registered in lexical, must be passed through codox util “registerNodesWithCodox” before passing it to lexical editor initial configuration. This step is critical for further correct sync work.
Installation of codox sync provider
Install codox provider npm package with codox components into project dependencies:
Editor intitial config update for sync integration
Import register nodes util from codox provider
Wrap lexical nodes into registerNodesWithCodox() util and pass wrapped nodes into lexical init configuration for lexical editor:
Codox plugin config reference
Codox plugin accepts config and ref props. Config contains all attributes to configure codox for sync launch and ref is provided to be able to access codox start/stop api to control when to start and stop codox sync
onEditorStateChange
Codox plugin accepts the callback from client’s app, which will be triggered each time, when content state is changed in editor. This callback should be used to listen to state changes and update app’s database.
It is recommended to update database only on local changes trigger.
onBlacklistedInsert
Optional callback which is triggered each time, when any blacklisted content combination was found and was blocked. Client’s app can listen to such cases and somehow notify users that operation was blacklisted, e.g. show some modal or pop-up.
Client’s app must not stop codox in such cases or do any other operations with editor - all required logic is done internally by codox plugin.
Codox plugin sync start/stop api reference
Codox plugin provides an api to control start/stop of sync via React refs mechanism. Created ref should be passed to codox plugin and later can be used to invoke start/stop of codox sync.
Codox sync blacklisting policy
Codox plugin defines a set of content combinations which are not allowed. Such content create/insert is blocked and the “onBlacklistedInsert” callback is invoked, if provided.
Purpose of blacklisting is to prevent any weird content combinations including those which does not make any sense.
Current content combinations blacklist:
Target (child lexical node types)
Forbidden to insert into (parent lexical node type)
Should read table as following (1st row as example):
“Table is forbidden to insert into table, image, inline-image and sticky”
Codox state validation util for editor content state
Codox provides extra helper util to validate json state object and ensure it is valid for lexical. Util can be used at any time and any place of client’s app code.
It is recommended to validate initial state, received from app’s backend.
Validation will throw error if state is not valid - should wrap it into try-catch.
Validation requires the same list of registered nodes that is passed into initial editor config
Codox extra plugins for Lexical Editor
Codox provider lib provides few extra plugins for extra functionality:
CodoxCommentPlugin
Separate plugin for support of comments. Has build-in ui for displaying comments and add/remove comments and threads. Along with plugin, the special editor command is exposed - to invoke comments creation from any place or other plugin
Comments plugin has built-in ui:
A button in top-right of page, click on it opens sidebar on the right. Sidebar renders all current active comment threads and has functionality to remove threads and add/remove comments.
when any range of content is selected, extra button is rendered on the right side of the page, click on it opens a modal to create new comment for selected content range
Extra command is exposed from codox provider lib to allow to trigger comment add from any part of editor ui, e.g. from any floating sidebars, etc
All comments are exposed in editor state json obj along with root, when state is exposed with “onEditorStateChange” callback:
CodoxFillBGColorPlugin
Extra plugin to change text background fill color.
Plugin comes from built-in ui - button click on which opens a color palette for background color pick. Can be utilized in any other lexical plugin, for example in toolbar plugin.
Plugin has set of optional props for css customization and listen to color changes:
CodoxFontColorPlugin
Extra plugin to change text font color.
Plugin comes from built-in ui - button click on which opens a color palette for text color pick. Can be utilized in any other lexical plugin, for example in toolbar plugin.
Plugin has set of optional props for css customization and listen to color changes:
Undo/Redo support for Codox sync
Codox sync supports undo/redo out-of-the-box. To enable undo/redo support, should use built-in
lexical HistoryPlugin:
Sync for undo/redo relies on history plugin built-in implementation of undo/redo stacks.
Important: It is not guaranteed to have correct and stable sync for undo/redo in case of using any custom undo/redo implementations
Basic Codox sync intengration example code snippet