ckeditor_taxonomy_glossary-1.0.0-alpha1/js/build/glossaryLink.js
js/build/glossaryLink.js
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CKEditor5=t():(e.CKEditor5=e.CKEditor5||{},e.CKEditor5.glossaryLink=t())}(self,()=>(()=>{var e={"ckeditor5/src/core.js":(e,t,i)=>{e.exports=i("dll-reference CKEditor5.dll")("./src/core.js")},"ckeditor5/src/ui.js":(e,t,i)=>{e.exports=i("dll-reference CKEditor5.dll")("./src/ui.js")},"ckeditor5/src/utils.js":(e,t,i)=>{e.exports=i("dll-reference CKEditor5.dll")("./src/utils.js")},"dll-reference CKEditor5.dll":e=>{"use strict";e.exports=CKEditor5.dll}},t={};function i(s){var o=t[s];if(void 0!==o)return o.exports;var r=t[s]={exports:{}};return e[s](r,r.exports,i),r.exports}i.d=(e,t)=>{for(var s in t)i.o(t,s)&&!i.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})},i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var s={};return(()=>{"use strict";i.d(s,{default:()=>u});var e=i("ckeditor5/src/core.js");class t extends e.Command{execute(e){const t=this.editor.model,i=t.document.selection;t.change(s=>{const o=t.schema.getValidRanges(i.getRanges(),"glossaryLink");for(const t of o)e?s.setAttribute("glossaryLink",e,t):s.removeAttribute("glossaryLink",t)})}refresh(){const e=this.editor.model,t=e.document.selection;this.isEnabled=e.schema.checkAttributeInSelection(t,"glossaryLink"),this.value=t.getAttribute("glossaryLink")||null}}class o extends e.Plugin{static get pluginName(){return"GlossaryLinkEditing"}init(){this._defineSchema(),this._defineConverters(),this._defineCommands()}_defineSchema(){this.editor.model.schema.extend("$text",{allowAttributes:["glossaryLink"]})}_defineConverters(){const e=this.editor.conversion;e.for("downcast").attributeToElement({model:"glossaryLink",view:(e,{writer:t})=>t.createAttributeElement("a",{class:"glossary-link","data-glossary-id":e},{priority:5})}),e.for("upcast").elementToAttribute({view:{name:"a",classes:"glossary-link",attributes:{"data-glossary-id":!0}},model:{key:"glossaryLink",value:e=>e.getAttribute("data-glossary-id")}})}_defineCommands(){this.editor.commands.add("glossaryLink",new t(this.editor))}}var r=i("ckeditor5/src/ui.js"),n=i("ckeditor5/src/utils.js");class a extends r.View{constructor(e,t={}){super(e),this.options=t,this.keystrokes=new n.KeystrokeHandler,this.focusTracker=new n.FocusTracker,this.set("isEditingExistingLink",!1),this.termIdInputView=this._createTermIdInput(),this.saveButtonView=this._createSaveButton(),this.cancelButtonView=this._createCancelButton(),this.removeLinkButtonView=this._createRemoveLinkButton(),this.childViews=this.createCollection([this.termIdInputView,this.saveButtonView,this.cancelButtonView,this.removeLinkButtonView]),this._setUpForm()}render(){super.render(),(0,r.submitHandler)({view:this}),this.keystrokes.listenTo(this.element),this.childViews.map(e=>{this.focusTracker.add(e.element)})}focus(){this.termIdInputView.focus()}setEditingMode(e){this.isEditingExistingLink=e,this.set("isEditingExistingLink",e)}_createTermIdInput(){const e=this.locale,t=new r.LabeledFieldView(e,r.createLabeledInputText);return t.label=Drupal.t("Glossary Term"),t.fieldView.placeholder=Drupal.t("Start typing to search for terms..."),t}_createSaveButton(){const e=this.locale,t=new r.ButtonView(e);return t.set({label:Drupal.t("Save"),icon:'<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="m8.5 12.8-3.42-3.42a1 1 0 1 0-1.42 1.42l4.13 4.13a1 1 0 0 0 1.42 0l8.38-8.38a1 1 0 1 0-1.42-1.42L8.5 12.8z"/></svg>',class:"ck-button-save",type:"submit",withText:!0}),t}_createCancelButton(){const e=this.locale,t=new r.ButtonView(e);return t.set({label:Drupal.t("Cancel"),icon:'<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="m11.591 10.177 4.243 4.242a1 1 0 0 1-1.415 1.415l-4.242-4.243-4.243 4.243a1 1 0 0 1-1.414-1.415l4.243-4.242L4.52 5.934A1 1 0 0 1 5.934 4.52l4.243 4.243 4.242-4.243a1 1 0 1 1 1.415 1.414l-4.243 4.243z"/></svg>',class:"ck-button-cancel",withText:!0}),t.on("execute",()=>{this.fire("cancel")}),t}_createRemoveLinkButton(){const e=this.locale,t=new r.ButtonView(e);return t.set({label:Drupal.t("Remove Link"),icon:'<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M11.077 15h.543c1.7 0 3.08-1.38 3.08-3.08s-1.38-3.08-3.08-3.08H9.08A3.08 3.08 0 0 0 6 11.92c0 .874.368 1.663.957 2.22l-.717.717A4.08 4.08 0 0 1 5 11.92 4.08 4.08 0 0 1 9.08 7.84h2.54a4.08 4.08 0 0 1 4.08 4.08 4.08 4.08 0 0 1-4.08 4.08h-.543v-1z"/><path d="M8.923 5h-.543c-1.7 0-3.08 1.38-3.08 3.08s1.38 3.08 3.08 3.08H10.92A3.08 3.08 0 0 0 14 8.08c0-.874-.368-1.663-.957-2.22l.717-.717A4.08 4.08 0 0 1 15 8.08 4.08 4.08 0 0 1 10.92 12.16H8.38a4.08 4.08 0 0 1-4.08-4.08A4.08 4.08 0 0 1 8.38 4.08h.543v1z"/><path d="m13 10.5-1.5-1.5-1.5 1.5 1.5 1.5L13 10.5z"/></svg>',class:"ck-button-remove-link",withText:!0}),t.on("execute",()=>{this.fire("removeLink")}),t}_setUpForm(){this.removeLinkButtonView.bind("isVisible").to(this,"isEditingExistingLink"),this.setTemplate({tag:"form",attributes:{class:["ck","ck-glossary-link-form"],tabindex:"-1"},children:[{tag:"div",attributes:{class:["ck","ck-glossary-link-form__row"]},children:[this.termIdInputView]},{tag:"div",attributes:{class:["ck","ck-glossary-link-form__actions"]},children:[this.saveButtonView,this.cancelButtonView,this.removeLinkButtonView]}]}),this.keystrokes.set("Esc",(e,t)=>{this.fire("cancel"),t()})}}const l=jQuery;class c extends e.Plugin{static get requires(){return[r.ContextualBalloon]}static get pluginName(){return"GlossaryLinkUI"}init(){this._createToolbarButton(),this._createFormView()}_createToolbarButton(){const e=this.editor,t=e.commands.get("glossaryLink");e.ui.componentFactory.add("glossaryLink",e=>{const i=new r.ButtonView(e);return i.set({label:Drupal.t("Glossary Link"),icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="20px" height="20px">\n <path\n d="M 3 7 C 1.3550302 7 0 8.3550302 0 10 L 0 40 C 0 41.64497 1.3550302 43 3 43 L 22.779297 43 C 23.328373 43.609333 24.115084 44 25 44 C 25.884916 44 26.671627 43.609333 27.220703 43 L 47 43 C 48.64497 43 50 41.64497 50 40 L 50 10 C 50 8.3550302 48.64497 7 47 7 L 27 7 C 26.223035 7 25.535366 7.3416196 25 7.8339844 C 24.464634 7.3416196 23.776965 7 23 7 L 3 7 z M 3 9 L 23 9 C 23.56503 9 24 9.4349698 24 10 L 24 41 L 22 41 L 3 41 C 2.4349698 41 2 40.56503 2 40 L 2 10 C 2 9.4349698 2.4349698 9 3 9 z M 27 9 L 47 9 C 47.56503 9 48 9.4349698 48 10 L 48 40 C 48 40.56503 47.56503 41 47 41 L 28 41 L 26 41 L 26 10 C 26 9.4349698 26.43497 9 27 9 z M 12.298828 19 L 7.7910156 32 L 10.423828 32 L 11.408203 29 L 15.626953 29 L 16.597656 32 L 19.369141 32 L 14.851562 19 L 12.298828 19 z M 32.878906 19 L 32.878906 21.009766 L 39.376953 21.009766 L 39.376953 21.162109 L 32.693359 30.414062 L 32.693359 32 L 42.351562 32 L 42.351562 29.990234 L 35.529297 29.990234 L 35.529297 29.837891 L 42.185547 20.585938 L 42.185547 19 L 32.878906 19 z M 13.455078 22 L 13.595703 22 L 15.205078 27 L 11.830078 27 L 13.455078 22 z" />\n</svg>',tooltip:!0,isToggleable:!0}),i.bind("isOn").to(t,"value",e=>!!e),i.bind("isEnabled").to(t,"isEnabled"),i.on("execute",()=>{this._showForm()}),i})}_createFormView(){const e=this.editor,t=e.config.get("ckeditorTaxonomyGlossary")||{};this.formView=new a(e.locale,t),this.formView.on("submit",()=>{const t=l(this.formView.termIdInputView.fieldView.element).data("glossary-id")||this.formView.termIdInputView.fieldView.element.value;if(t){if(!/^\d+$/.test(t))return void("undefined"!=typeof Drupal&&Drupal.announce&&Drupal.announce(Drupal.t("Please select a valid glossary term from the autocomplete suggestions."),"assertive"));e.execute("glossaryLink",t)}this._hideForm()}),this.formView.on("cancel",()=>{this._hideForm()}),this.formView.on("removeLink",()=>{e.execute("glossaryLink",null),this._hideForm()})}async _showForm(){this.editor;const e=this.editor.plugins.get(r.ContextualBalloon);e.hasView(this.formView)||(await this._initializeFormValues(),this._addBalloonCloseListeners(),e.add({view:this.formView,position:this._getBalloonPositionData()}),this.formView.focus(),this._initializeAutocomplete())}async _initializeFormValues(){const e=this.editor.commands.get("glossaryLink"),t=l(this.formView.termIdInputView.fieldView.element);if(e.refresh(),e.value&&this.formView.termIdInputView&&this.formView.termIdInputView.fieldView)try{const i=await fetch(`/glossary/term/${e.value}`);if(i.ok){const e=await i.json(),s=`${e.name} (${e.id})`;this.formView.termIdInputView.fieldView.set("value",s),t.data("glossary-id",e.id)}else this.formView.termIdInputView.fieldView.set("value",e.value)}catch(t){console.warn("Failed to fetch term name:",t),this.formView.termIdInputView.fieldView.set("value",e.value)}this.formView.setEditingMode(!!e.value)}_addBalloonCloseListeners(){this._boundOnEscapeKey=this._onEscapeKey.bind(this),this._boundOnClickOutside=this._onClickOutside.bind(this),setTimeout(()=>{document.addEventListener("keydown",this._boundOnEscapeKey),document.addEventListener("click",this._boundOnClickOutside)},100)}_removeBalloonCloseListeners(){this._boundOnEscapeKey&&(document.removeEventListener("keydown",this._boundOnEscapeKey),this._boundOnEscapeKey=null),this._boundOnClickOutside&&(document.removeEventListener("click",this._boundOnClickOutside),this._boundOnClickOutside=null)}_onEscapeKey(e){"Escape"===e.key&&this._isBalloonVisible()&&(e.preventDefault(),e.stopPropagation(),this._hideForm())}_onClickOutside(e){if(!this._isBalloonVisible())return;const t=this.editor.plugins.get(r.ContextualBalloon).view.element;if(t?.contains(e.target))return;const i=document.querySelector('[data-cke-tooltip-text="Glossary Link"]');if(i&&(i===e.target||i.contains(e.target)))return;const s=document.querySelector(".ui-autocomplete");if(s?.contains(e.target))return;e.target.closest(".glossary-autocomplete-item, .glossary-autocomplete-wrapper, .ui-menu-item")||this._hideForm()}_isBalloonVisible(){const e=this.editor.plugins.get(r.ContextualBalloon);return e?.hasView(this.formView)}_initializeAutocomplete(){const e=l(this.formView.termIdInputView.fieldView.element);e.hasClass("ui-autocomplete-input")&&e.autocomplete("destroy"),e.removeClass("glossary-autocomplete ui-autocomplete-input ck-input_focused"),e.removeData("glossary-id ui-autocomplete autocomplete"),e.off(".autocomplete"),e.val(""),e.data("ckeditor-instance",this.editor),e.data("form-view",this.formView),e.data("ui-instance",this);const t=e.autocomplete({source:(e,t)=>{l.ajax({url:`/glossary/autocomplete/${encodeURIComponent(e.term)}`,dataType:"json",success:i=>{!i.some(t=>t.label.toLowerCase()===e.term.toLowerCase())&&"undefined"!=typeof Drupal&&"undefined"!=typeof drupalSettings&&drupalSettings.user?.permissions&&-1!==drupalSettings.user.permissions.indexOf("create glossary terms via editor")&&e.term.length>=2&&i.push({id:"create_new",label:Drupal.t('Create new term: "@term"',{"@term":e.term}),description:Drupal.t("Click to create a new glossary term"),is_create_option:!0,term_name:e.term}),t(i)},error:()=>{t([])}})},minLength:2,delay:300,select:(t,i)=>i.item.is_create_option?(t.preventDefault(),"undefined"!=typeof Drupal&&Drupal.glossaryAutocomplete&&Drupal.glossaryAutocomplete.showCreateTermModal(i.item.term_name,e),!1):(e.data("glossary-id",i.item.id),e.val(`${i.item.label} (${i.item.id})`),!1),focus:(t,i)=>(i.item.is_create_option||e.val(i.item.label),!1)});t.autocomplete("widget").menu("option","items","> :not(.ui-autocomplete-category)"),t.data("ui-autocomplete")._renderItem=(e,t)=>{const i=l("<li>"),s=l("<div class='glossary-autocomplete-wrapper ui-menu-item-wrapper'>");if(t.is_create_option)s.addClass("create-new-option"),s.html(`<strong style='color: #0073aa;'>+ ${t.label}</strong><br><small>${t.description}</small>`);else{let e="";if(t.langcode){e+=`<span class='glossary-autocomplete-language'>[${t.langcode.toUpperCase()}]</span>`}e+=`<span class='glossary-autocomplete-label'>${t.label}</span>`;let i=`<div class='glossary-autocomplete-header'>${e}</div>`;t.description&&(i+=`<div class='glossary-autocomplete-description'>${t.description}</div>`),s.html(i)}return i.append(s),i.appendTo(e)}}_hideForm(){const e=this.editor.plugins.get(r.ContextualBalloon);if(this.formView?.termIdInputView?.fieldView){this.formView.termIdInputView.fieldView.set("value","");l(this.formView.termIdInputView.fieldView.element).removeData("glossary-id")}e.hasView(this.formView)&&e.remove(this.formView),this.editor.editing.view.focus(),this._removeBalloonCloseListeners()}_getBalloonPositionData(){const e=this.editor.editing.view,t=e.document.selection;return{target:()=>e.domConverter.viewRangeToDom(t.getFirstRange())}}}class d extends e.Plugin{static get requires(){return[o,c]}static get pluginName(){return"GlossaryLink"}}const u={GlossaryLink:d}})(),s=s.default})());