import{Command as h,Plugin as f}from"@ckeditor/ckeditor5-core";import{ModelDocumentSelection as C}from"@ckeditor/ckeditor5-engine";import{getLanguageDirection as A,Collection as P}from"@ckeditor/ckeditor5-utils";import{createDropdown as T,addListToDropdown as V,MenuBarMenuView as y,MenuBarMenuListView as v,ListSeparatorView as D,MenuBarMenuListItemView as M,MenuBarMenuListItemButtonView as I,UIModel as b}from"@ckeditor/ckeditor5-ui";/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/function m(c,e){return e=e||A(c),`${c}:${e}`}function x(c){const[e,t]=c.split(":");return{languageCode:e,textDirection:t}}/**
* @license Copyright (c) 2003-2026, 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 r=this.editor.model,o=r.document.selection,u=e?m(e,t):!1;r.change(g=>{if(o.isCollapsed)u?g.setSelectionAttribute("language",u):g.removeSelectionAttribute("language");else{const a=r.schema.getValidRanges(o.getRanges(),"language",{includeEmptyRanges:!0});for(const i of a){let n=i,l="language";i.isCollapsed&&(n=i.start.parent,l=C._getStoreAttributeKey("language")),u?g.setAttribute(l,u,n):g.removeAttribute(l,n)}}})}_getValueFromFirstAllowedNode(){const e=this.editor.model,t=e.schema,r=e.document.selection;if(r.isCollapsed)return r.getAttribute("language")||!1;for(const s of r.getRanges())for(const o of s.getItems())if(t.checkAttribute(o,"language"))return o.getAttribute("language")||!1;return!1}}/**
* @license Copyright (c) 2003-2026, 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 r=t.getAttribute("lang"),s=t.getAttribute("dir");return m(r,s)}},view:{name:"span",attributes:{lang:/[\s\S]+/}}}),e.for("downcast").attributeToElement({model:"language",view:(t,{writer:r},s)=>{if(!t||!s.item.is("$textProxy")&&!s.item.is("documentSelection"))return;const{languageCode:o,textDirection:u}=x(t);return r.createAttributeElement("span",{lang:o,dir:u})}})}}/**
* @license Copyright (c) 2003-2026, 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,r=t("Choose language"),s=t("Language");e.ui.componentFactory.add("textPartLanguage",o=>{const{definitions:u,titles:g}=this._getItemMetadata(),a=e.commands.get("textPartLanguage"),i=T(o);return V(i,u,{ariaLabel:s,role:"menu"}),i.buttonView.set({ariaLabel:s,ariaLabelledBy:void 0,isOn:!1,withText:!0,tooltip:s}),i.extendTemplate({attributes:{class:["ck-text-fragment-language-dropdown"]}}),i.bind("isEnabled").to(a,"isEnabled"),i.buttonView.bind("label").to(a,"value",n=>n&&g[n]||r),i.buttonView.bind("ariaLabel").to(a,"value",n=>{const l=n&&g[n];return l?`${l}, ${s}`:s}),this.listenTo(i,"execute",n=>{a.execute({languageCode:n.source.languageCode,textDirection:n.source.textDirection}),e.editing.view.focus()}),i}),e.ui.componentFactory.add("menuBar:textPartLanguage",o=>{const{definitions:u}=this._getItemMetadata(),g=e.commands.get("textPartLanguage"),a=new y(o);a.buttonView.set({label:s});const i=new v(o);i.set({ariaLabel:t("Language"),role:"menu"});for(const n of u){if(n.type!="button"){i.items.add(new D(o));continue}const l=new M(o,a),d=new I(o);d.set({role:"menuitemradio",isToggleable:!0}),d.bind(...Object.keys(n.model)).to(n.model),d.delegate("execute").to(a),l.children.add(d),i.items.add(l)}return a.bind("isEnabled").to(g,"isEnabled"),a.panelView.children.add(i),a.on("execute",n=>{g.execute({languageCode:n.source.languageCode,textDirection:n.source.textDirection}),e.editing.view.focus()}),a})}_getItemMetadata(){const e=this.editor,t=new P,r={},s=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 a of o){const i={type:"button",model:new b({label:a.title,languageCode:a.languageCode,role:"menuitemradio",textDirection:a.textDirection,withText:!0})},n=m(a.languageCode,a.textDirection);i.model.bind("isOn").to(s,"value",l=>l===n),t.add(i),r[n]=a.title}return{definitions:t,titles:r}}}/**
* @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
*/class E extends f{static get requires(){return[w,L]}static get pluginName(){return"TextPartLanguage"}static get isOfficialPlugin(){return!0}}export{E as TextPartLanguage,p as TextPartLanguageCommand,w as TextPartLanguageEditing,L as TextPartLanguageUI,x as _parseLanguageAttribute,m as _stringifyLanguageAttribute};
