1 import {EditorContainerUiElement, EditorUiElement} from "./core";
3 import {el} from "../../utils/dom";
5 export type EditorContextToolbarDefinition = {
7 content: EditorUiElement[],
8 displayTargetLocator?: (originalTarget: HTMLElement) => HTMLElement;
11 export class EditorContextToolbar extends EditorContainerUiElement {
13 protected target: HTMLElement;
15 constructor(target: HTMLElement, children: EditorUiElement[]) {
20 protected buildDOM(): HTMLElement {
22 class: 'editor-context-toolbar',
23 }, this.getChildren().map(child => child.getDOMElement()));
27 const editorBounds = this.getContext().scrollDOM.getBoundingClientRect();
28 const targetBounds = this.target.getBoundingClientRect();
29 const dom = this.getDOMElement();
30 const domBounds = dom.getBoundingClientRect();
32 const showing = targetBounds.bottom > editorBounds.top
33 && targetBounds.top < editorBounds.bottom;
35 dom.hidden = !showing;
41 const showAbove: boolean = targetBounds.bottom + 6 + domBounds.height > editorBounds.bottom;
42 dom.classList.toggle('is-above', showAbove);
44 const targetMid = targetBounds.left + (targetBounds.width / 2);
45 const targetLeft = targetMid - (domBounds.width / 2);
47 dom.style.top = (targetBounds.top - 6 - domBounds.height) + 'px';
49 dom.style.top = (targetBounds.bottom + 6) + 'px';
51 dom.style.left = targetLeft + 'px';
54 insert(children: EditorUiElement[]) {
55 this.addChildren(...children);
56 const dom = this.getDOMElement();
57 dom.append(...children.map(child => child.getDOMElement()));
61 const children = this.getChildren();
62 for (const child of children) {
63 child.getDOMElement().remove();
65 this.removeChildren(...children);
70 this.getDOMElement().remove();