Overview
The UI should be a separate layer from the editor core. It subscribes to editor state changes (selection, focus) and updates its position and visibility.
Floating Toolbar
Appears when text is selected. Shows formatting options (Bold, Italic, Link).
- Trigger:
selectionchangeor transaction update. - Positioning: Use
range.getBoundingClientRect(). - Logic: Only show if selection is not collapsed and has content.
updateToolbar(selection) {
if (selection.empty) {
this.hide();
return;
}
const rect = selection.getRangeAt(0).getBoundingClientRect();
this.showAt(rect.left, rect.top - toolbarHeight - 10);
}