⚑ Plugin Bridge & Event Lifecycle

πŸ”Œ Bridge Architecture (SPA ⇄ Editor)

  • Outbound (Editor β†’ Host): Intercept changes via view.dispatch() ➑️ broadcast external state.
  • Inbound (Host β†’ Editor): Dispatch transactions imperatively via view.dispatch(tr) ➑️ update doc state.

πŸ”„ Event Registry & Lifecycle

[Event Trigger] ➑️ [Capture: High-Precedence Listeners] ➑️ [Bubble/Dispatch] ➑️ [Destruction: Cleanup]
  • DOM Listeners: Bind directly via view.dom.addEventListener().
  • Editor Handlers: Declare via EditorView.domEventHandlers({...}) for internal event mapping.
  • 🧹 Teardown: ALWAYS remove DOM listeners in the plugin’s destroy() hook to prevent memory leaks.

βš–οΈ State Sync & Reconciliation

  • ⏱️ Debounce: Batch outbound updates to prevent input lag and performance thrashing.
  • πŸ”„ Reconciliation: Diff incoming host state against view.state.doc before applying updates.
  • βš”οΈ Conflict Resolution: Employ LWW (Last-Writer-Wins) for simple state, or OT (Operational Transformation) for concurrent edits.