var __awaiter=this&&this.__awaiter||function(e,t,n,i){function r(e){return e instanceof n?e:new n((function(t){t(e)}))}return new(n||(n=Promise))((function(n,o){function a(e){try{s(i.next(e))}catch(e){o(e)}}function c(e){try{s(i["throw"](e))}catch(e){o(e)}}function s(e){e.done?n(e.value):r(e.value).then(a,c)}s((i=i.apply(e,t||[])).next())}))};var __generator=this&&this.__generator||function(e,t){var n={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},i,r,o,a;return a={next:c(0),throw:c(1),return:c(2)},typeof Symbol==="function"&&(a[Symbol.iterator]=function(){return this}),a;function c(e){return function(t){return s([e,t])}}function s(a){if(i)throw new TypeError("Generator is already executing.");while(n)try{if(i=1,r&&(o=a[0]&2?r["return"]:a[0]?r["throw"]||((o=r["return"])&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;if(r=0,o)a=[a[0]&2,o.value];switch(a[0]){case 0:case 1:o=a;break;case 4:n.label++;return{value:a[1],done:false};case 5:n.label++;r=a[1];a=[0];continue;case 7:a=n.ops.pop();n.trys.pop();continue;default:if(!(o=n.trys,o=o.length>0&&o[o.length-1])&&(a[0]===6||a[0]===2)){n=0;continue}if(a[0]===3&&(!o||a[1]>o[0]&&a[1]<o[3])){n.label=a[1];break}if(a[0]===6&&n.label<o[1]){n.label=o[1];o=a;break}if(o&&n.label<o[2]){n.label=o[2];n.ops.push(a);break}if(o[2])n.ops.pop();n.trys.pop();continue}a=t.call(e,n)}catch(e){a=[6,e];r=0}finally{i=o=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:true}}};import{r as registerInstance,c as createEvent,h,H as Host}from"./index-ecdfb761.js";var dvnRichEditorHeadingBlockCss="dvn-rich-editor-heading-block{display:block;-ms-flex:1;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}";var DvnRichEditorHeadingBlock=function(){function e(e){var t=this;registerInstance(this,e);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;this.lastCursorPosition=0;this.retainPosition=function(e){t.lastCursorPosition=getCaretCharacterOffsetWithin(e.target)}}e.prototype.componentShouldUpdate=function(e){return this.input.innerHTML!==e.text};e.prototype.focusOnInput=function(){return __awaiter(this,void 0,void 0,(function(){return __generator(this,(function(e){this.input.focus();placeCaret(this.input,this.lastCursorPosition);return[2]}))}))};e.prototype.render=function(){var e=this;var t=this.type||"h1";return h(Host,null,h(t,{ref:function(t){return e.input=t},id:"".concat(t,"-").concat(this.id),key:"".concat(t,"-").concat(this.id),contenteditable:true,onInput:function(t){e.retainPosition(t);var n=t.target.innerHTML;e.change.emit({id:e.id,value:{text:n}});if(n.length===0){setTimeout((function(){return e.isContentCleared=true}),100)}},onKeyDown:function(t){if(t.key==="Enter"){t.preventDefault();e.enterKeyDown.emit(t)}else if(t.key==="Backspace"&&e.isContentCleared){e.lastBackspaceKeyDown.emit(t)}else if(t.key==="ArrowDown"){if(e.value.text.length===window.getSelection().getRangeAt(0).endOffset){e.arrowDown.emit(t)}}else if(t.key==="ArrowUp"){if(window.getSelection().getRangeAt(0).endOffset===0){e.arrowUp.emit(t)}}}},this.value.text))};return e}();function placeCaret(e,t){e.focus();if(typeof window.getSelection!="undefined"&&typeof document.createRange!="undefined"){var n=document.createRange();n.selectNodeContents(e);if(t!==0){n.setStart(e.childNodes[0],t)}n.collapse(true);var i=window.getSelection();i.removeAllRanges();i.addRange(n)}else if(typeof document.body.createTextRange!="undefined"){var r=document.body.createTextRange();r.moveToElementText(e);r.collapse(false);r.select()}}function getCaretCharacterOffsetWithin(e){var t=0;var n=e.ownerDocument||e.document;var i=n.defaultView||n.parentWindow;var r;if(typeof i.getSelection!="undefined"){r=i.getSelection();if(r.rangeCount>0){var o=i.getSelection().getRangeAt(0);var a=o.cloneRange();a.selectNodeContents(e);a.setEnd(o.endContainer,o.endOffset);t=a.toString().length}}else if((r=n.selection)&&r.type!="Control"){var c=r.createRange();var s=n.body.createTextRange();s.moveToElementText(e);s.setEndPoint("EndToEnd",c);t=s.text.length}return t}DvnRichEditorHeadingBlock.style=dvnRichEditorHeadingBlockCss;export{DvnRichEditorHeadingBlock as dvn_rich_editor_heading_block};