import{Command as T,Plugin as f}from"@ckeditor/ckeditor5-core";import{logWarning as B,CKEditorError as b,first as E}from"@ckeditor/ckeditor5-utils";import{ButtonView as D,createDropdown as k,addToolbarToDropdown as N,MenuBarMenuView as _,MenuBarMenuListView as I,MenuBarMenuListItemView as V,MenuBarMenuListItemButtonView as M}from"@ckeditor/ckeditor5-ui";import{IconAlignLeft as O,IconAlignRight as L,IconAlignCenter as C,IconAlignJustify as P}from"@ckeditor/ckeditor5-icons";/**
* @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 d=["left","right","center","justify"];function h(i){return d.includes(i)}function p(i,e){return e.contentLanguageDirection=="rtl"?i==="right":i==="left"}function A(i){const e=i.map(t=>{let o;return typeof t=="string"?o={name:t}:o=t,o}).filter(t=>{const o=d.includes(t.name);return o||B("alignment-config-name-not-recognized",{option:t}),o}),n=e.filter(t=>!!t.className).length;if(n&&n<e.length)throw new b("alignment-config-classnames-are-missing",{configuredOptions:i});return e.forEach((t,o,a)=>{const s=a.slice(o+1);if(s.some(r=>r.name==t.name))throw new b("alignment-config-name-already-defined",{option:t,configuredOptions:i});if(t.className&&s.some(l=>l.className==t.className))throw new b("alignment-config-classname-already-defined",{option:t,configuredOptions:i})}),e}/**
* @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 w="alignment";class v extends T{refresh(){const n=this.editor.locale,t=E(this.editor.model.document.selection.getSelectedBlocks());this.isEnabled=!!t&&this._canBeAligned(t),this.isEnabled&&t.hasAttribute("alignment")?this.value=t.getAttribute("alignment"):this.value=n.contentLanguageDirection==="rtl"?"right":"left"}execute(e={}){const n=this.editor,t=n.locale,o=n.model,a=o.document,s=e.value;o.change(c=>{const r=Array.from(a.selection.getSelectedBlocks()).filter(g=>this._canBeAligned(g)),l=r[0].getAttribute("alignment");p(s,t)||l===s||!s?S(r,c):z(r,c,s)})}_canBeAligned(e){return this.editor.model.schema.checkAttribute(e,w)}}function S(i,e){for(const n of i)e.removeAttribute(w,n)}function z(i,e,n){for(const t of i)e.setAttribute(w,n,t)}/**
* @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 y extends f{static get pluginName(){return"AlignmentEditing"}static get isOfficialPlugin(){return!0}constructor(e){super(e),e.config.define("alignment",{options:d.map(n=>({name:n}))})}init(){const e=this.editor,n=e.locale,t=e.model.schema,a=A(e.config.get("alignment.options")).filter(l=>h(l.name)&&!p(l.name,n)),s=a.some(l=>!!l.className);t.extend("$block",{allowAttributes:"alignment"}),e.model.schema.setAttributeProperties("alignment",{isFormatting:!0}),s?e.conversion.attributeToAttribute(G(a)):e.conversion.for("downcast").attributeToAttribute(F(a));const c=U(a);for(const l of c)e.conversion.for("upcast").attributeToAttribute(l);const r=j(a);for(const l of r)e.conversion.for("upcast").attributeToAttribute(l);e.commands.add("alignment",new v(e))}}function F(i){const e={};for(const{name:t}of i)e[t]={key:"style",value:{"text-align":t}};return{model:{key:"alignment",values:i.map(t=>t.name)},view:e}}function U(i){const e=[];for(const{name:n}of i)e.push({view:{key:"style",value:{"text-align":n}},model:{key:"alignment",value:n}});return e}function j(i){const e=[];for(const{name:n}of i)e.push({view:{key:"align",value:n},model:{key:"alignment",value:n}});return e}function G(i){const e={};for(const t of i)e[t.name]={key:"class",value:t.className};return{model:{key:"alignment",values:i.map(t=>t.name)},view:e}}/**
* @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 u=new Map([["left",O],["right",L],["center",C],["justify",P]]);class x extends f{get localizedOptionTitles(){const e=this.editor.t;return{left:e("Align left"),right:e("Align right"),center:e("Align center"),justify:e("Justify")}}static get pluginName(){return"AlignmentUI"}static get isOfficialPlugin(){return!0}init(){const e=this.editor,n=A(e.config.get("alignment.options"));n.map(t=>t.name).filter(h).forEach(t=>this._addButton(t)),this._addToolbarDropdown(n),this._addMenuBarMenu(n)}_addButton(e){this.editor.ui.componentFactory.add(`alignment:${e}`,t=>this._createButton(t,e))}_createButton(e,n,t={}){const o=this.editor,a=o.commands.get("alignment"),s=new D(e);return s.set({label:this.localizedOptionTitles[n],icon:u.get(n),tooltip:!0,isToggleable:!0,...t}),s.bind("isEnabled").to(a),s.bind("isOn").to(a,"value",c=>c===n),this.listenTo(s,"execute",()=>{o.execute("alignment",{value:n}),o.editing.view.focus()}),s}_addToolbarDropdown(e){const n=this.editor;n.ui.componentFactory.add("alignment",o=>{const a=k(o),s=o.uiLanguageDirection==="rtl"?"w":"e",c=o.t;N(a,()=>e.map(m=>this._createButton(o,m.name,{tooltipPosition:s})),{enableActiveItemFocusOnDropdownOpen:!0,isVertical:!0,ariaLabel:c("Text alignment toolbar")}),a.buttonView.set({label:c("Text alignment"),tooltip:!0}),a.extendTemplate({attributes:{class:"ck-alignment-dropdown"}});const r=o.contentLanguageDirection==="rtl"?u.get("right"):u.get("left"),l=n.commands.get("alignment");return a.buttonView.bind("icon").to(l,"value",m=>u.get(m)||r),a.bind("isEnabled").to(l,"isEnabled"),this.listenTo(a,"execute",()=>{n.editing.view.focus()}),a})}_addMenuBarMenu(e){const n=this.editor;n.ui.componentFactory.add("menuBar:alignment",t=>{const o=n.commands.get("alignment"),a=t.t,s=new _(t),c=new I(t);s.bind("isEnabled").to(o),c.set({ariaLabel:a("Text alignment"),role:"menu"}),s.buttonView.set({label:a("Text alignment")});for(const r of e){const l=new V(t,s),m=new M(t);m.delegate("execute").to(s),m.set({label:this.localizedOptionTitles[r.name],icon:u.get(r.name),role:"menuitemcheckbox",isToggleable:!0}),m.on("execute",()=>{n.execute("alignment",{value:r.name}),n.editing.view.focus()}),m.bind("isOn").to(o,"value",g=>g===r.name),m.bind("isEnabled").to(o,"isEnabled"),l.children.add(m),c.items.add(l)}return s.panelView.children.add(c),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 J extends f{static get requires(){return[y,x]}static get pluginName(){return"Alignment"}static get isOfficialPlugin(){return!0}}export{J as Alignment,v as AlignmentCommand,y as AlignmentEditing,x as AlignmentUI,d as _ALIGNMENT_SUPPORTED_OPTIONS,h as _isAlignmentSupported,p as _isDefaultAlignment,A as _normalizeAlignmentOptions};
