1 import {EditorContainerUiElement, EditorUiElement} from "./core";
2 import {el} from "../../helpers";
4 export type EditorContextToolbarDefinition = {
6 content: EditorUiElement[],
7 displayTargetLocator?: (originalTarget: HTMLElement) => HTMLElement;
10 export class EditorContextToolbar extends EditorContainerUiElement {
12 protected target: HTMLElement;
14 constructor(target: HTMLElement, children: EditorUiElement[]) {
19 protected buildDOM(): HTMLElement {
21 class: 'editor-context-toolbar',
22 }, this.getChildren().map(child => child.getDOMElement()));
26 const editorBounds = this.getContext().scrollDOM.getBoundingClientRect();
27 const targetBounds = this.target.getBoundingClientRect();
28 const dom = this.getDOMElement();
29 const domBounds = dom.getBoundingClientRect();
31 const showing = targetBounds.bottom > editorBounds.top
32 && targetBounds.top < editorBounds.bottom;
34 dom.hidden = !showing;
40 const showAbove: boolean = targetBounds.bottom + 6 + domBounds.height > editorBounds.bottom;
41 dom.classList.toggle('is-above', showAbove);
43 const targetMid = targetBounds.left + (targetBounds.width / 2);
44 const targetLeft = targetMid - (domBounds.width / 2);
46 dom.style.top = (targetBounds.top - 6 - domBounds.height) + 'px';
48 dom.style.top = (targetBounds.bottom + 6) + 'px';
50 dom.style.left = targetLeft + 'px';
53 insert(children: EditorUiElement[]) {
54 this.addChildren(...children);
55 const dom = this.getDOMElement();
56 dom.append(...children.map(child => child.getDOMElement()));
60 const children = this.getChildren();
61 for (const child of children) {
62 child.getDOMElement().remove();
64 this.removeChildren(...children);
69 this.getDOMElement().remove();