import{Plugin as y}from"@ckeditor/ckeditor5-core";import{Delete as P}from"@ckeditor/ckeditor5-typing";import{ModelLiveRange as B,ModelSchemaContext as S}from"@ckeditor/ckeditor5-engine";import{first as q}from"@ckeditor/ckeditor5-utils";/**
* @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 g(o,t,e,s){let a,n=null;typeof s=="function"?a=s:(n=o.commands.get(s),a=()=>{o.execute(s)}),o.model.document.on("change:data",(h,l)=>{if(n&&!n.isEnabled||!t.isEnabled)return;const r=q(o.model.document.selection.getRanges());if(!r.isCollapsed||l.isUndo||!l.isLocal)return;const f=Array.from(o.model.document.differ.getChanges()),c=f[0];if(f.length!=1||c.type!=="insert"||c.name!="$text"||c.length!=1)return;const i=c.position.parent;if(i.is("element","codeBlock")||i.is("element","listItem")&&typeof s!="function"&&!["numberedList","bulletedList","todoList"].includes(s)||n&&n.value===!0)return;const u=i.getChild(0),A=o.model.createRangeOn(u);if(!A.containsRange(r)&&!r.end.isEqual(A.end))return;const p=e.exec(u.data.substr(0,r.end.offset));p&&o.model.enqueueChange(d=>{const m=o.model.document.selection,L=d.createPositionAt(i,0),$=d.createPositionAt(i,p[0].length),k=new B(L,$);if(a({match:p})!==!1){const x=Array.from(m.getAttributes());d.remove(k);const C=m.getFirstRange(),v=d.createRangeIn(i);i.isEmpty&&!v.isEqual(C)&&!v.containsRange(C,!0)&&d.remove(i),F(d,m,x)}k.detach(),o.model.enqueueChange(()=>{o.plugins.get("Delete").requestUndoOnBackspace()})})})}function F(o,t,e){const s=o.model.schema,a=t.getFirstPosition();let n=new S(a);s.checkChild(n,"$text")&&(n=n.push("$text"));for(const[h,l]of e)s.checkAttribute(n,h)&&o.setSelectionAttribute(h,l)}/**
* @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(o,t,e,s){let a,n;e instanceof RegExp?a=e:n=e,n=n||(h=>{let l;const r=[],f=[];for(;(l=a.exec(h))!==null&&!(l&&l.length<4);){let{index:c,"1":i,"2":u,"3":A}=l;const p=i+u+A;c+=l[0].length-p.length;const d=[c,c+i.length],m=[c+i.length+u.length,c+i.length+u.length+A.length];r.push(d),r.push(m),f.push([c+i.length,c+i.length+u.length])}return{remove:r,format:f}}),o.model.document.on("change:data",(h,l)=>{if(l.isUndo||!l.isLocal||!t.isEnabled)return;const r=o.model,f=r.document.selection;if(!f.isCollapsed)return;const c=Array.from(r.document.differ.getChanges()),i=c[0];if(c.length!=1||i.type!=="insert"||i.name!="$text"||i.length!=1)return;const u=f.focus,A=u.parent,{text:p,range:d}=I(r.createRange(r.createPositionAt(A,0),u),r),m=n(p),L=R(d.start,m.format,r),$=R(d.start,m.remove,r);L.length&&$.length&&r.enqueueChange(k=>{if(s(k,L)!==!1){for(const x of $.reverse())k.remove(x);r.enqueueChange(()=>{o.plugins.get("Delete").requestUndoOnBackspace()})}})})}function R(o,t,e){return t.filter(s=>s[0]!==void 0&&s[1]!==void 0).map(s=>e.createRange(o.getShiftedBy(s[0]),o.getShiftedBy(s[1])))}function I(o,t){let e=o.start;return{text:Array.from(o.getItems()).reduce((a,n)=>!(n.is("$text")||n.is("$textProxy"))||n.getAttribute("code")?(e=t.createPositionAfter(n),""):a+n.data,""),range:t.createRange(e,o.end)}}/**
* @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 T extends y{static get requires(){return[P]}static get pluginName(){return"Autoformat"}static get isOfficialPlugin(){return!0}afterInit(){const t=this.editor,e=this.editor.t;this._addListAutoformats(),this._addBasicStylesAutoformats(),this._addHeadingAutoformats(),this._addBlockQuoteAutoformats(),this._addCodeBlockAutoformats(),this._addHorizontalLineAutoformats(),t.accessibility.addKeystrokeInfos({keystrokes:[{label:e("Revert autoformatting action"),keystroke:"Backspace"}]})}_addListAutoformats(){const t=this.editor.commands;t.get("bulletedList")&&g(this.editor,this,/^[*-]\s$/,"bulletedList"),t.get("numberedList")&&g(this.editor,this,/^1[.|)]\s$/,"numberedList"),t.get("todoList")&&g(this.editor,this,/^\[\s?\]\s$/,"todoList"),t.get("checkTodoList")&&g(this.editor,this,/^\[\s?x\s?\]\s$/,()=>{this.editor.execute("todoList"),this.editor.execute("checkTodoList")})}_addBasicStylesAutoformats(){const t=this.editor.commands;if(t.get("bold")){const e=_(this.editor,"bold");b(this.editor,this,/(?:^|\s)(\*\*)([^*]+)(\*\*)$/g,e),b(this.editor,this,/(?:^|\s)(__)([^_]+)(__)$/g,e)}if(t.get("italic")){const e=_(this.editor,"italic");b(this.editor,this,/(?:^|\s)(\*)([^*_]+)(\*)$/g,e),b(this.editor,this,/(?:^|\s)(_)([^_]+)(_)$/g,e)}if(t.get("code")){const e=_(this.editor,"code");b(this.editor,this,/(`)([^`]+)(`)$/g,e)}if(t.get("strikethrough")){const e=_(this.editor,"strikethrough");b(this.editor,this,/(~~)([^~]+)(~~)$/g,e)}}_addHeadingAutoformats(){const t=this.editor.commands.get("heading");t&&t.modelElements.filter(e=>e.match(/^heading[1-6]$/)).forEach(e=>{const s=e[7],a=new RegExp(`^(#{${s}})\\s$`);g(this.editor,this,a,()=>{if(!t.isEnabled||t.value===e)return!1;this.editor.execute("heading",{value:e})})})}_addBlockQuoteAutoformats(){this.editor.commands.get("blockQuote")&&g(this.editor,this,/^>\s$/,"blockQuote")}_addCodeBlockAutoformats(){const t=this.editor,e=t.model.document.selection;t.commands.get("codeBlock")&&g(t,this,/^```$/,()=>{if(e.getFirstPosition().parent.is("element","listItem"))return!1;this.editor.execute("codeBlock",{usePreviousLanguageChoice:!0})})}_addHorizontalLineAutoformats(){this.editor.commands.get("horizontalLine")&&g(this.editor,this,/^---$/,"horizontalLine")}}function _(o,t){return(e,s)=>{if(!o.commands.get(t).isEnabled)return!1;const n=o.model.schema.getValidRanges(s,t);for(const h of n)e.setAttribute(t,!0,h);e.removeSelectionAttribute(t)}}export{T as Autoformat,g as blockAutoformatEditing,b as inlineAutoformatEditing};
