import{Command as B,Plugin as p}from"@ckeditor/ckeditor5-core";import{Paragraph as N}from"@ckeditor/ckeditor5-paragraph";import{first as E,priorities as I,Collection as R}from"@ckeditor/ckeditor5-utils";import{UIModel as M,createDropdown as O,addListToDropdown as A,MenuBarMenuView as D,MenuBarMenuListView as F,MenuBarMenuListItemView as L,MenuBarMenuListItemButtonView as S,ButtonView as $}from"@ckeditor/ckeditor5-ui";import{IconHeading6 as z,IconHeading5 as U,IconHeading4 as W,IconHeading3 as q,IconHeading2 as j,IconHeading1 as G}from"@ckeditor/ckeditor5-icons";import{ViewDowncastWriter as J,enableViewPlaceholder as K,hideViewPlaceholder as v,needsViewPlaceholder as Q,showViewPlaceholder as X}from"@ckeditor/ckeditor5-engine";/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class C extends B{modelElements;constructor(e,t){super(e),this.modelElements=t}refresh(){const e=E(this.editor.model.document.selection.getSelectedBlocks());this.value=!!e&&this.modelElements.includes(e.name)&&e.name,this.isEnabled=!!e&&this.modelElements.some(t=>y(e,t,this.editor.model.schema))}execute(e){const t=this.editor.model,n=t.document,o=e.value;t.change(i=>{const a=Array.from(n.selection.getSelectedBlocks()).filter(s=>y(s,o,t.schema));for(const s of a)s.is("element",o)||i.rename(s,o)})}}function y(r,e,t){return t.checkChild(r.parent,e)&&!t.isObject(r)}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/const k="paragraph";class x extends p{static get pluginName(){return"HeadingEditing"}static get isOfficialPlugin(){return!0}constructor(e){super(e),e.config.define("heading",{options:[{model:"paragraph",title:"Paragraph",class:"ck-heading_paragraph"},{model:"heading1",view:"h2",title:"Heading 1",class:"ck-heading_heading1"},{model:"heading2",view:"h3",title:"Heading 2",class:"ck-heading_heading2"},{model:"heading3",view:"h4",title:"Heading 3",class:"ck-heading_heading3"}]})}static get requires(){return[N]}init(){const e=this.editor,t=e.config.get("heading.options"),n=[];for(const o of t)o.model!=="paragraph"&&(e.model.schema.register(o.model,{inheritAllFrom:"$block"}),e.conversion.elementToElement(o),n.push(o.model));this._addDefaultH1Conversion(e),e.commands.add("heading",new C(e,n))}afterInit(){const e=this.editor,t=e.commands.get("enter"),n=e.config.get("heading.options");t&&this.listenTo(t,"afterExecute",(o,i)=>{const a=e.model.document.selection.getFirstPosition().parent;n.some(d=>a.is("element",d.model))&&!a.is("element",k)&&a.childCount===0&&i.writer.rename(a,k)})}_addDefaultH1Conversion(e){e.conversion.for("upcast").elementToElement({model:"heading1",view:"h1",converterPriority:I.low+1})}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/function b(r){const e=r.t,t={Paragraph:e("Paragraph"),"Heading 1":e("Heading 1"),"Heading 2":e("Heading 2"),"Heading 3":e("Heading 3"),"Heading 4":e("Heading 4"),"Heading 5":e("Heading 5"),"Heading 6":e("Heading 6")};return r.config.get("heading.options").map(n=>{const o=t[n.title];return o&&o!=n.title&&(n.title=o),n})}function Y(r,{insertAt:e}={}){if(typeof document>"u")return;const t=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css",window.litNonce&&n.setAttribute("nonce",window.litNonce),e==="top"&&t.firstChild?t.insertBefore(n,t.firstChild):t.appendChild(n),n.styleSheet?n.styleSheet.cssText=r:n.appendChild(document.createTextNode(r))}Y(".ck.ck-heading_heading1 .ck-button__label{font-size:20px}.ck.ck-heading_heading2 .ck-button__label{font-size:17px}.ck.ck-heading_heading3 .ck-button__label{font-size:14px}.ck[class*=ck-heading_heading]{font-weight:700}.ck.ck-dropdown.ck-heading-dropdown .ck-dropdown__button .ck-button__label{width:8em}.ck.ck-dropdown.ck-heading-dropdown .ck-dropdown__panel .ck-list__item{min-width:18em}");/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class P extends p{static get pluginName(){return"HeadingUI"}static get isOfficialPlugin(){return!0}init(){const e=this.editor,t=e.t,n=b(e),o=t("Choose heading"),i=t("Heading");e.ui.componentFactory.add("heading",a=>{const s={},d=new R,c=e.commands.get("heading"),h=e.commands.get("paragraph"),u=[c];for(const m of n){const g={type:"button",model:new M({label:m.title,class:m.class,role:"menuitemradio",withText:!0})};m.model==="paragraph"?(g.model.bind("isOn").to(h,"value"),g.model.set("commandName","paragraph"),u.push(h)):(g.model.bind("isOn").to(c,"value",f=>f===m.model),g.model.set({commandName:"heading",commandValue:m.model})),d.add(g),s[m.model]=m.title}const l=O(a);return A(l,d,{ariaLabel:i,role:"menu"}),l.buttonView.set({ariaLabel:i,ariaLabelledBy:void 0,isOn:!1,withText:!0,tooltip:i}),l.extendTemplate({attributes:{class:["ck-heading-dropdown"]}}),l.bind("isEnabled").toMany(u,"isEnabled",(...m)=>m.some(g=>g)),l.buttonView.bind("label").to(c,"value",h,"value",(m,g)=>{const f=g?"paragraph":m;return typeof f=="boolean"||!s[f]?o:s[f]}),l.buttonView.bind("ariaLabel").to(c,"value",h,"value",(m,g)=>{const f=g?"paragraph":m;return typeof f=="boolean"||!s[f]?i:`${s[f]}, ${i}`}),this.listenTo(l,"execute",m=>{const{commandName:g,commandValue:f}=m.source;e.execute(g,f?{value:f}:void 0),e.editing.view.focus()}),l}),e.ui.componentFactory.add("menuBar:heading",a=>{const s=new D(a),d=e.commands.get("heading"),c=e.commands.get("paragraph"),h=[d],u=new F(a);s.set({class:"ck-heading-dropdown"}),u.set({ariaLabel:t("Heading"),role:"menu"}),s.buttonView.set({label:t("Heading")}),s.panelView.children.add(u);for(const l of n){const m=new L(a,s),g=new S(a);m.children.add(g),u.items.add(m),g.set({isToggleable:!0,label:l.title,role:"menuitemradio",class:l.class}),g.delegate("execute").to(s),g.on("execute",()=>{const f=l.model==="paragraph"?"paragraph":"heading";e.execute(f,{value:l.model}),e.editing.view.focus()}),l.model==="paragraph"?(g.bind("isOn").to(c,"value"),h.push(c)):g.bind("isOn").to(d,"value",f=>f===l.model)}return s.bind("isEnabled").toMany(h,"isEnabled",(...l)=>l.some(m=>m)),s})}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class Z extends p{static get requires(){return[x,P]}static get pluginName(){return"Heading"}static get isOfficialPlugin(){return!0}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/const ee={heading1:G,heading2:j,heading3:q,heading4:W,heading5:U,heading6:z};class te extends p{init(){b(this.editor).filter(t=>t.model!=="paragraph").map(t=>this._createButton(t))}_createButton(e){const t=this.editor;t.ui.componentFactory.add(e.model,n=>{const o=new $(n),i=t.commands.get("heading");return o.label=e.title,o.icon=e.icon||ee[e.model],o.tooltip=!0,o.isToggleable=!0,o.bind("isEnabled").to(i),o.bind("isOn").to(i,"value",a=>a==e.model),o.on("execute",()=>{t.execute("heading",{value:e.model}),t.editing.view.focus()}),o})}}/**
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/const _=new Set(["paragraph","heading1","heading2","heading3","heading4","heading5","heading6"]);class ne extends p{_bodyPlaceholder=new Map;static get pluginName(){return"Title"}static get isOfficialPlugin(){return!0}static get requires(){return["Paragraph"]}init(){const e=this.editor,t=e.model;t.schema.register("title",{isBlock:!0,allowIn:"$root"}),t.schema.register("title-content",{isBlock:!0,allowIn:"title",allowAttributes:["alignment"]}),t.schema.extend("$text",{allowIn:"title-content"}),t.schema.addAttributeCheck(n=>{if(n.endsWith("title-content $text"))return!1}),e.editing.mapper.on("modelToViewPosition",T(e.editing.view)),e.data.mapper.on("modelToViewPosition",T(e.editing.view)),e.conversion.for("downcast").elementToElement({model:"title-content",view:"h1"}),e.conversion.for("downcast").add(n=>n.on("insert:title",(o,i,a)=>{a.consumable.consume(i.item,o.name)})),e.data.upcastDispatcher.on("element:h1",w,{priority:"high"}),e.data.upcastDispatcher.on("element:h2",w,{priority:"high"}),e.data.upcastDispatcher.on("element:h3",w,{priority:"high"}),t.document.registerPostFixer(n=>this._fixTitleContent(n)),t.document.registerPostFixer(n=>this._fixTitleElement(n)),t.document.registerPostFixer(n=>this._fixBodyElement(n)),t.document.registerPostFixer(n=>this._fixExtraParagraph(n)),this._attachPlaceholders(),this._attachTabPressHandling()}getTitle(e={}){const t=e.rootName?e.rootName:void 0,o=this._getTitleElement(t).getChild(0);return this.editor.data.stringify(o,e)}getBody(e={}){const t=this.editor,n=t.data,o=t.model,i=e.rootName?e.rootName:void 0,a=t.model.document.getRoot(i),s=t.editing.view,d=new J(s.document),c=o.createRangeIn(a),h=d.createDocumentFragment(),u=o.createPositionAfter(a.getChild(0)),l=o.createRange(u,o.createPositionAt(a,"end")),m=new Map;for(const g of o.markers){const f=l.getIntersection(g.getRange());f&&m.set(g.name,f)}return n.mapper.clearBindings(),n.mapper.bindElements(a,h),n.downcastDispatcher.convert(c,m,d,e),d.remove(d.createRangeOn(h.getChild(0))),t.data.processor.toData(h)}_getTitleElement(e){const t=this.editor.model.document.getRoot(e);for(const n of t.getChildren())if(H(n))return n}_fixTitleContent(e){let t=!1;for(const n of this.editor.model.document.getRootNames()){const o=this._getTitleElement(n);if(!o||o.maxOffset===1)continue;const i=Array.from(o.getChildren());i.shift();for(const a of i)e.move(e.createRangeOn(a),o,"after"),e.rename(a,"paragraph");t=!0}return t}_fixTitleElement(e){let t=!1;const n=this.editor.model;for(const o of this.editor.model.document.getRoots()){const i=Array.from(o.getChildren()).filter(H),a=i[0],s=o.getChild(0);if(s.is("element","title")){i.length>1&&(V(i,e,n),t=!0);continue}if(!a&&!_.has(s.name)){const d=e.createElement("title");e.insert(d,o),e.insertElement("title-content",d),t=!0;continue}_.has(s.name)?oe(s,e,n):e.move(e.createRangeOn(a),o,0),V(i,e,n),t=!0}return t}_fixBodyElement(e){let t=!1;for(const n of this.editor.model.document.getRootNames()){const o=this.editor.model.document.getRoot(n);if(o.childCount<2){const i=e.createElement("paragraph");e.insert(i,o,1),this._bodyPlaceholder.set(n,i),t=!0}}return t}_fixExtraParagraph(e){let t=!1;for(const n of this.editor.model.document.getRootNames()){const o=this.editor.model.document.getRoot(n),i=this._bodyPlaceholder.get(n);ae(i,o)&&(this._bodyPlaceholder.delete(n),e.remove(i),t=!0)}return t}_attachPlaceholders(){const e=this.editor,t=e.t,n=e.editing.view,o=e.sourceElement,i=e.config.get("title.placeholder")||t("Type your title"),a=e.config.get("placeholder")||o&&o.tagName.toLowerCase()==="textarea"&&o.getAttribute("placeholder")||t("Type or paste your content here.");e.editing.downcastDispatcher.on("insert:title-content",(d,c,h)=>{const u=h.mapper.toViewElement(c.item);u.placeholder=i,K({view:n,element:u,keepOnFocus:!0})});const s=new Map;n.document.registerPostFixer(d=>{let c=!1;for(const h of n.document.roots){if(h.isEmpty)continue;const u=h.getChild(1),l=s.get(h.rootName);u!==l&&(l&&(v(d,l),d.removeAttribute("data-placeholder",l)),d.setAttribute("data-placeholder",a,u),s.set(h.rootName,u),c=!0),Q(u,!0)&&h.childCount===2&&u.name==="p"?c=X(d,u)?!0:c:c=v(d,u)?!0:c}return c})}_attachTabPressHandling(){const e=this.editor,t=e.model;e.keystrokes.set("TAB",(n,o)=>{t.change(i=>{const a=t.document.selection,s=Array.from(a.getSelectedBlocks());if(s.length===1&&s[0].is("element","title-content")){const c=a.getFirstPosition().root.getChild(1);i.setSelection(c,0),o()}})}),e.keystrokes.set("SHIFT + TAB",(n,o)=>{t.change(i=>{const a=t.document.selection;if(!a.isCollapsed)return;const s=E(a.getSelectedBlocks()),d=a.getFirstPosition(),c=e.model.document.getRoot(d.root.rootName),h=c.getChild(0),u=c.getChild(1);s===u&&d.isAtStart&&(i.setSelection(h.getChild(0),0),o())})})}}function w(r,e,t){const n=e.modelCursor,o=e.viewItem;if(!n.isAtStart||!n.parent.is("element","$root")||!t.consumable.consume(o,{name:!0}))return;const i=t.writer,a=i.createElement("title"),s=i.createElement("title-content");i.append(s,a),i.insert(a,n),t.convertChildren(o,s),t.updateConversionResult(a,e)}function T(r){return(e,t)=>{const n=t.modelPosition.parent;if(!n.is("element","title"))return;const o=n.parent,i=t.mapper.toViewElement(o);t.viewPosition=r.createPositionAt(i,0),e.stop()}}function H(r){return r.is("element","title")}function oe(r,e,t){const n=e.createElement("title");e.insert(n,r,"before"),e.insert(r,n,0),e.rename(r,"title-content"),t.schema.removeDisallowedAttributes([r],e)}function V(r,e,t){let n=!1;for(const o of r)o.index!==0&&(ie(o,e,t),n=!0);return n}function ie(r,e,t){const n=r.getChild(0);if(n.isEmpty){e.remove(r);return}e.move(e.createRangeOn(n),r,"before"),e.rename(n,"paragraph"),e.remove(r),t.schema.removeDisallowedAttributes([n],e)}function ae(r,e){return!(!r||!r.is("element","paragraph")||r.childCount||e.childCount<=2||e.getChild(e.childCount-1)!==r)}export{Z as Heading,te as HeadingButtonsUI,C as HeadingCommand,x as HeadingEditing,P as HeadingUI,ne as Title,b as _getLocalizedHeadingOptions};
