import { r as registerInstance, c as createEvent, h, H as Host } from './index-acdc1ac0.js';

const dvnRichEditorHeadingBlockCss = "dvn-rich-editor-heading-block{display:block;flex:1}dvn-rich-editor-heading-block h1{font-size:2rem;margin:0;display:block}dvn-rich-editor-heading-block h2{font-size:1.6rem;margin:0;display:block}dvn-rich-editor-heading-block h3{font-size:1.2rem;margin:0;display:block}";

const DvnRichEditorHeadingBlock = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.change = createEvent(this, "change", 7);
    this.enterKeyDown = createEvent(this, "enterKeyDown", 7);
    this.lastBackspaceKeyDown = createEvent(this, "lastBackspaceKeyDown", 7);
    this.arrowDown = createEvent(this, "arrowDown", 7);
    this.arrowUp = createEvent(this, "arrowUp", 7);
    this.isContentCleared = false;
  }
  componentShouldUpdate(newValue) {
    return this.input.innerHTML !== newValue.text;
  }
  async focusOnInput() {
    this.input.focus();
    placeCaretAtEnd(this.input);
  }
  render() {
    const Tag = this.type || 'h1';
    return (h(Host, null, h(Tag, { ref: el => this.input = el, id: `${Tag}-${this.id}`, key: `${Tag}-${this.id}`, contenteditable: true, onInput: (e) => {
        const text = e.target.innerHTML;
        this.change.emit({
          id: this.id,
          value: {
            text,
          }
        });
        if (text.length === 0) {
          // set flag after a while. onKeyDown callback on host should ignore the
          // first key down because it would be the same event that clears the content
          // we want to delete this component once it's blank and user presses backspace
          setTimeout(() => this.isContentCleared = true, 100);
        }
      }, onKeyDown: (e) => {
        if (e.key === 'Enter') {
          e.preventDefault();
          this.enterKeyDown.emit(e);
        }
        else if (e.key === 'Backspace' && this.isContentCleared) {
          this.lastBackspaceKeyDown.emit(e);
        }
        else if (e.key === 'ArrowDown') {
          // focus on the next component if the cursor is at the end and down arrow is pressed
          if (this.value.text.length === window.getSelection().getRangeAt(0).endOffset) {
            this.arrowDown.emit(e);
          }
        }
        else if (e.key === 'ArrowUp') {
          // focus on the previous component if the cursor is at the beginning and up arrow is pressed
          if (window.getSelection().getRangeAt(0).endOffset === 0) {
            this.arrowUp.emit(e);
          }
        }
      } }, this.value.text)));
  }
};
function placeCaretAtEnd(el) {
  el.focus();
  if (typeof window.getSelection != 'undefined' && typeof document.createRange != 'undefined') {
    const range = document.createRange();
    range.selectNodeContents(el);
    range.collapse(false);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);
  }
  else if (typeof document.body.createTextRange != 'undefined') {
    const textRange = document.body.createTextRange();
    textRange.moveToElementText(el);
    textRange.collapse(false);
    textRange.select();
  }
}
DvnRichEditorHeadingBlock.style = dvnRichEditorHeadingBlockCss;

export { DvnRichEditorHeadingBlock as dvn_rich_editor_heading_block };
