import{Command as h,Plugin as f}from"@ckeditor/ckeditor5-core";import{getLanguageDirection as C,Collection as A}from"@ckeditor/ckeditor5-utils";import{createDropdown as P,addListToDropdown as T,MenuBarMenuView as V,MenuBarMenuListView as v,ListSeparatorView as y,MenuBarMenuListItemView as D,MenuBarMenuListItemButtonView as M,UIModel as b}from"@ckeditor/ckeditor5-ui";/**
* @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 m(l,e){return e=e||C(l),`${l}:${e}`}function x(l){const[e,t]=l.split(":");return{languageCode:e,textDirection: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 p extends h{refresh(){const e=this.editor.model,t=e.document;this.value=this._getValueFromFirstAllowedNode(),this.isEnabled=e.schema.checkAttributeInSelection(t.selection,"language")}execute({languageCode:e,textDirection:t}={}){const s=this.editor.model,o=s.document.selection,u=e?m(e,t):!1;s.change(g=>{if(o.isCollapsed)u?g.setSelectionAttribute("language",u):g.removeSelectionAttribute("language");else{const n=s.schema.getValidRanges(o.getRanges(),"language");for(const a of n)u?g.setAttribute("language",u,a):g.removeAttribute("language",a)}})}_getValueFromFirstAllowedNode(){const e=this.editor.model,t=e.schema,s=e.document.selection;if(s.isCollapsed)return s.getAttribute("language")||!1;for(const r of s.getRanges())for(const o of r.getItems())if(t.checkAttribute(o,"language"))return o.getAttribute("language")||!1;return!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
*/class w extends f{static get pluginName(){return"TextPartLanguageEditing"}static get isOfficialPlugin(){return!0}constructor(e){super(e),e.config.define("language",{textPartLanguage:[{title:"Arabic",languageCode:"ar"},{title:"French",languageCode:"fr"},{title:"Spanish",languageCode:"es"}]})}init(){const e=this.editor;e.model.schema.extend("$text",{allowAttributes:"language"}),e.model.schema.setAttributeProperties("language",{copyOnEnter:!0}),this._defineConverters(),e.commands.add("textPartLanguage",new p(e))}_defineConverters(){const e=this.editor.conversion;e.for("upcast").elementToAttribute({model:{key:"language",value:t=>{const s=t.getAttribute("lang"),r=t.getAttribute("dir");return m(s,r)}},view:{name:"span",attributes:{lang:/[\s\S]+/}}}),e.for("downcast").attributeToElement({model:"language",view:(t,{writer:s},r)=>{if(!t||!r.item.is("$textProxy")&&!r.item.is("documentSelection"))return;const{languageCode:o,textDirection:u}=x(t);return s.createAttributeElement("span",{lang:o,dir:u})}})}}/**
* @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 L extends f{static get pluginName(){return"TextPartLanguageUI"}static get isOfficialPlugin(){return!0}init(){const e=this.editor,t=e.t,s=t("Choose language"),r=t("Language");e.ui.componentFactory.add("textPartLanguage",o=>{const{definitions:u,titles:g}=this._getItemMetadata(),n=e.commands.get("textPartLanguage"),a=P(o);return T(a,u,{ariaLabel:r,role:"menu"}),a.buttonView.set({ariaLabel:r,ariaLabelledBy:void 0,isOn:!1,withText:!0,tooltip:r}),a.extendTemplate({attributes:{class:["ck-text-fragment-language-dropdown"]}}),a.bind("isEnabled").to(n,"isEnabled"),a.buttonView.bind("label").to(n,"value",i=>i&&g[i]||s),a.buttonView.bind("ariaLabel").to(n,"value",i=>{const c=i&&g[i];return c?`${c}, ${r}`:r}),this.listenTo(a,"execute",i=>{n.execute({languageCode:i.source.languageCode,textDirection:i.source.textDirection}),e.editing.view.focus()}),a}),e.ui.componentFactory.add("menuBar:textPartLanguage",o=>{const{definitions:u}=this._getItemMetadata(),g=e.commands.get("textPartLanguage"),n=new V(o);n.buttonView.set({label:r});const a=new v(o);a.set({ariaLabel:t("Language"),role:"menu"});for(const i of u){if(i.type!="button"){a.items.add(new y(o));continue}const c=new D(o,n),d=new M(o);d.set({role:"menuitemradio",isToggleable:!0}),d.bind(...Object.keys(i.model)).to(i.model),d.delegate("execute").to(n),c.children.add(d),a.items.add(c)}return n.bind("isEnabled").to(g,"isEnabled"),n.panelView.children.add(a),n.on("execute",i=>{g.execute({languageCode:i.source.languageCode,textDirection:i.source.textDirection}),e.editing.view.focus()}),n})}_getItemMetadata(){const e=this.editor,t=new A,s={},r=e.commands.get("textPartLanguage"),o=e.config.get("language.textPartLanguage"),u=e.locale.t,g=u("Remove language");t.add({type:"button",model:new b({label:g,languageCode:!1,withText:!0})}),t.add({type:"separator"});for(const n of o){const a={type:"button",model:new b({label:n.title,languageCode:n.languageCode,role:"menuitemradio",textDirection:n.textDirection,withText:!0})},i=m(n.languageCode,n.textDirection);a.model.bind("isOn").to(r,"value",c=>c===i),t.add(a),s[i]=n.title}return{definitions:t,titles: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 I extends f{static get requires(){return[w,L]}static get pluginName(){return"TextPartLanguage"}static get isOfficialPlugin(){return!0}}export{I as TextPartLanguage,p as TextPartLanguageCommand,w as TextPartLanguageEditing,L as TextPartLanguageUI,x as _parseLanguageAttribute,m as _stringifyLanguageAttribute};
