import{Command as C,Plugin as v}from"@ckeditor/ckeditor5-core";import{ShiftEnter as j}from"@ckeditor/ckeditor5-enter";import{ViewUpcastWriter as U}from"@ckeditor/ckeditor5-engine";import{ClipboardPipeline as H}from"@ckeditor/ckeditor5-clipboard";import{first as S,Collection as J}from"@ckeditor/ckeditor5-utils";import{createDropdown as K,SplitButtonView as G,addListToDropdown as X,MenuBarMenuView as Q,MenuBarMenuListView as Y,MenuBarMenuListItemView as Z,MenuBarMenuListItemButtonView as ee,UIModel as te}from"@ckeditor/ckeditor5-ui";import{IconCodeBlock as T}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
*/function p(i){const e=i.t,n=i.config.get("codeBlock.languages");for(const o of n)o.label==="Plain text"&&(o.label=e("Plain text")),o.class===void 0&&(o.class=`language-${o.language}`);return n}function h(i,e,n){const o={};for(const c of i)if(e==="class"){const s=c[e].split(" ").shift();o[s]=c[n]}else o[c[e]]=c[n];return o}function b(i){return i.data.match(/^(\s*)/)[0]}function I(i,e){const n=i.createDocumentFragment(),o=e.split(`
`),c=o.reduce((s,t,a)=>(s.push(t),a<o.length-1&&s.push(i.createElement("br")),s),[]);return i.appendChild(c,n),n}function B(i){const e=i.document.selection,n=[];if(e.isCollapsed)return[e.anchor];const o=e.getFirstRange().getWalker({ignoreElementEnd:!0,direction:"backward"});for(const{item:c}of o){let s=c.is("$textProxy")?c.textNode:c;const t=s.parent;if(!t.is("element","codeBlock")||s.is("element","softBreak"))continue;for(;s.previousSibling&&!s.previousSibling.is("element","softBreak");)s=s.previousSibling;const a=s.is("$text")?s.startOffset+b(s).length:s.startOffset,r=i.createPositionAt(t,a);n.every(l=>!l.isEqual(r))&&n.push(r)}return n}function x(i){const e=S(i.getSelectedBlocks());return!!e&&e.is("element","codeBlock")}function w(i,e){return e.is("rootElement")||i.isLimit(e)?!1:i.checkChild(e.parent,"codeBlock")}function A(i,e,n,o){const c=h(e,"language","label"),s=n.getAttribute("language");if(s in c){const t=c[s];return i(o==="enter"?"Entering %0 code snippet":"Leaving %0 code snippet",t)}return i(o==="enter"?"Entering code snippet":"Leaving code snippet")}function L(i,e){for(i.textNode&&(i=e.createPositionBefore(i.textNode));i.nodeBefore&&!i.nodeBefore.is("element","softBreak");)i=e.createPositionBefore(i.nodeBefore);const n=i.nodeAfter;return n&&n.is("$text")?n:null}/**
* @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 C{_lastLanguage;constructor(e){super(e),this._lastLanguage=null}refresh(){this.value=this._getValue(),this.isEnabled=this._checkEnabled()}execute(e={}){const n=this.editor,o=n.model,c=o.document.selection,t=p(n)[0],a=Array.from(c.getSelectedBlocks()),r=e.forceValue==null?!this.value:e.forceValue,l=ne(e,this._lastLanguage,t.language);o.change(d=>{r?this._applyCodeBlock(d,a,l):this._removeCodeBlock(d,a)})}_getValue(){const e=this.editor.model.document.selection,n=S(e.getSelectedBlocks());return!!n?.is("element","codeBlock")?n.getAttribute("language"):!1}_checkEnabled(){if(this.value)return!0;const e=this.editor.model.document.selection,n=this.editor.model.schema,o=S(e.getSelectedBlocks());return o?w(n,o):!1}_applyCodeBlock(e,n,o){this._lastLanguage=o;const c=this.editor.model.schema,s=n.filter(t=>w(c,t));for(const t of s)e.rename(t,"codeBlock"),e.setAttribute("language",o,t),c.removeDisallowedAttributes([t],e),Array.from(t.getChildren()).filter(a=>!c.checkChild(t,a)).forEach(a=>e.remove(a));s.reverse().forEach((t,a)=>{const r=s[a+1];t.previousSibling===r&&(e.appendElement("softBreak",r),e.merge(e.createPositionBefore(t)))})}_removeCodeBlock(e,n){const o=n.filter(c=>c.is("element","codeBlock"));for(const c of o){const s=e.createRangeOn(c);for(const t of Array.from(s.getItems()).reverse())if(t.is("element","softBreak")&&t.parent.is("element","codeBlock")){const{position:a}=e.split(e.createPositionBefore(t)),r=a.nodeAfter;e.rename(r,"paragraph"),e.removeAttribute("language",r),e.remove(t)}e.rename(c,"paragraph"),e.removeAttribute("language",c)}}}function ne(i,e,n){return i.language?i.language:i.usePreviousLanguageChoice&&e?e:n}/**
* @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 D extends C{_indentSequence;constructor(e){super(e),this._indentSequence=e.config.get("codeBlock.indentSequence")}refresh(){this.isEnabled=this._checkEnabled()}execute(){const n=this.editor.model;n.change(o=>{const c=B(n);for(const s of c){const t=o.createText(this._indentSequence);n.insertContent(t,s)}})}_checkEnabled(){return this._indentSequence?x(this.editor.model.document.selection):!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 V extends C{_indentSequence;constructor(e){super(e),this._indentSequence=e.config.get("codeBlock.indentSequence")}refresh(){this.isEnabled=this._checkEnabled()}execute(){const n=this.editor.model;n.change(()=>{const o=B(n);for(const c of o){const s=N(n,c,this._indentSequence);s&&n.deleteContent(n.createSelection(s))}})}_checkEnabled(){if(!this._indentSequence)return!1;const e=this.editor.model;return x(e.document.selection)?B(e).some(n=>N(e,n,this._indentSequence)):!1}}function N(i,e,n){const o=L(e,i);if(!o)return null;const c=b(o),s=c.lastIndexOf(n);if(s+n.length!==c.length||s===-1)return null;const{parent:t,startOffset:a}=o;return i.createRange(i.createPositionAt(t,a+s),i.createPositionAt(t,a+s+n.length))}/**
* @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 _(i,e,n=!1){const o=h(e,"language","class"),c=h(e,"language","label");return(s,t,a)=>{const{writer:r,mapper:l,consumable:d}=a;if(!d.consume(t.item,"insert"))return;const u=t.item.getAttribute("language"),g=l.toViewPosition(i.createPositionBefore(t.item)),m={};n&&(m["data-language"]=c[u],m.spellcheck="false");const k=o[u]?{class:o[u]}:void 0,E=r.createContainerElement("code",k),W=r.createContainerElement("pre",m,E);r.insert(g,W),l.bindElements(t.item,E)}}function O(i){return(e,n,o)=>{if(n.item.parent.name!=="codeBlock")return;const{writer:c,mapper:s,consumable:t}=o;if(!t.consume(n.item,"insert"))return;const a=s.toViewPosition(i.createPositionBefore(n.item));c.insert(a,c.createText(`
`))}}function M(i,e){const n=h(e,"class","language"),o=e[0].language;return(c,s,t)=>{const a=s.viewItem,r=a.parent;if(!r||!r.is("element","pre")||s.modelCursor.findAncestor("codeBlock"))return;const{consumable:l,writer:d}=t;if(!l.test(a,{name:!0}))return;const u=d.createElement("codeBlock"),g=[...a.getClassNames()];g.length||g.push("");for(const m of g){const k=n[m];if(k){l.consume(a,{classes:[m]}),d.setAttribute("language",k,u);break}}u.hasAttribute("language")||d.setAttribute("language",o,u),t.convertChildren(a,u),t.safeInsert(u,s.modelCursor)&&(l.consume(a,{name:!0}),t.updateConversionResult(u,s))}}function R(){return(i,e,{consumable:n,writer:o})=>{let c=e.modelCursor;if(!n.test(e.viewItem)||!c.findAncestor("codeBlock"))return;n.consume(e.viewItem);const t=e.viewItem.data.split(`
`).map(r=>o.createText(r)),a=t[t.length-1];for(const r of t)if(o.insert(r,c),c=c.getShiftedBy(r.offsetSize),r!==a){const l=o.createElement("softBreak");o.insert(l,c),c=o.createPositionAfter(l)}e.modelRange=o.createRange(e.modelCursor,c),e.modelCursor=c}}function q(){return(i,e,{consumable:n})=>{const o=e.viewItem;if(o.findAncestor("pre"))return;const c=Array.from(o.getChildren()),s=c.find(t=>t.is("element","code"));if(s)for(const t of c)t===s||!t.is("$text")||n.consume(t,{name:!0})}}/**
* @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 F="paragraph";class z extends v{static get pluginName(){return"CodeBlockEditing"}static get isOfficialPlugin(){return!0}static get requires(){return[j]}constructor(e){super(e),e.config.define("codeBlock",{languages:[{language:"plaintext",label:"Plain text"},{language:"c",label:"C"},{language:"cs",label:"C#"},{language:"cpp",label:"C++"},{language:"css",label:"CSS"},{language:"diff",label:"Diff"},{language:"go",label:"Go"},{language:"html",label:"HTML"},{language:"java",label:"Java"},{language:"javascript",label:"JavaScript"},{language:"php",label:"PHP"},{language:"python",label:"Python"},{language:"ruby",label:"Ruby"},{language:"typescript",label:"TypeScript"},{language:"xml",label:"XML"}],indentSequence:"	"})}init(){const e=this.editor,n=e.model.schema,o=e.model,c=e.editing.view,s=p(e);e.commands.add("codeBlock",new y(e)),e.commands.add("indentCodeBlock",new D(e)),e.commands.add("outdentCodeBlock",new V(e)),this.listenTo(c.document,"tab",(t,a)=>{const r=a.shiftKey?"outdentCodeBlock":"indentCodeBlock";e.commands.get(r).isEnabled&&(e.execute(r),a.stopPropagation(),a.preventDefault(),t.stop())},{context:"pre"}),n.register("codeBlock",{allowWhere:"$block",allowChildren:"$text",disallowChildren:"$inlineObject",allowAttributes:["language"],allowAttributesOf:"$listItem",isBlock:!0}),n.addAttributeCheck((t,a)=>{const r=t.getItem(t.length-2);if(n.getAttributeProperties(a).isFormatting&&r&&r.name=="codeBlock")return!1}),e.editing.downcastDispatcher.on("insert:codeBlock",_(o,s,!0)),e.data.downcastDispatcher.on("insert:codeBlock",_(o,s)),e.data.downcastDispatcher.on("insert:softBreak",O(o),{priority:"high"}),e.data.upcastDispatcher.on("element:code",M(c,s)),e.data.upcastDispatcher.on("text",R()),e.data.upcastDispatcher.on("element:pre",q(),{priority:"high"}),this.listenTo(e.editing.view.document,"clipboardInput",(t,a)=>{let r=o.createRange(o.document.selection.anchor);if(a.targetRanges&&(r=e.editing.mapper.toModelRange(a.targetRanges[0])),!r.start.parent.is("element","codeBlock"))return;const l=a.dataTransfer.getData("text/plain"),d=new U(e.editing.view.document);a.content=I(d,l)}),e.plugins.has("ClipboardPipeline")&&e.plugins.get(H).on("contentInsertion",(t,a)=>{const r=e.model,l=r.document.selection;l.anchor.parent.is("element","codeBlock")&&r.change(d=>{const u=d.createRangeIn(a.content);for(const g of[...u.getItems()])g.is("node")&&!n.checkChild(l.anchor,g)&&d.remove(g)})}),this.listenTo(o,"getSelectedContent",(t,[a])=>{const r=a.anchor;a.isCollapsed||!r.parent.is("element","codeBlock")||!r.hasSameParentAs(a.focus)||o.change(l=>{const d=t.return;if(r.parent.is("element")&&(d.childCount>1||a.containsEntireContent(r.parent))){const g=l.createElement("codeBlock",r.parent.getAttributes());l.append(d,g);const m=l.createDocumentFragment();l.append(g,m),t.return=m;return}const u=d.getChild(0);n.checkAttribute(u,"code")&&l.setAttribute("code",!0,u)})})}afterInit(){const e=this.editor,n=e.commands,o=n.get("indent"),c=n.get("outdent");o&&o.registerChildCommand(n.get("indentCodeBlock"),{priority:"highest"}),c&&c.registerChildCommand(n.get("outdentCodeBlock")),this.listenTo(e.editing.view.document,"enter",(s,t)=>{e.model.document.selection.getLastPosition().parent.is("element","codeBlock")&&(!ie(e,t.isSoft)&&!se(e,t.isSoft)&&oe(e),t.preventDefault(),s.stop())},{context:"pre"}),this._initAriaAnnouncements()}_initAriaAnnouncements(){const{model:e,ui:n,t:o}=this.editor,c=p(this.editor);let s=null;e.document.selection.on("change:range",()=>{const t=e.document.selection.focus.parent;!n||s===t||!t.is("element")||(s&&s.is("element","codeBlock")&&n.ariaLiveAnnouncer.announce(A(o,c,s,"leave")),t.is("element","codeBlock")&&n.ariaLiveAnnouncer.announce(A(o,c,t,"enter")),s=t)})}}function oe(i){const e=i.model,n=e.document,o=n.selection.getLastPosition();let c;const s=L(o,e);s&&s.is("$text")&&(c=b(s)),i.model.change(t=>{i.execute("shiftEnter"),c&&t.insertText(c,n.selection.anchor)})}function ie(i,e){const o=i.model.document,c=i.editing.view,s=o.selection.getLastPosition(),t=s.nodeAfter;return e||!o.selection.isCollapsed||!s.isAtStart||!f(t)?!1:(i.model.change(a=>{i.execute("enter");const r=o.selection.anchor.parent.previousSibling;a.rename(r,F),a.setSelection(r,"in"),i.model.schema.removeDisallowedAttributes([r],a),a.remove(t)}),c.scrollToTheSelection(),!0)}function se(i,e){const n=i.model,o=n.document,c=i.editing.view,s=o.selection.getLastPosition(),t=s.nodeBefore;let a;if(e||!o.selection.isCollapsed||!s.isAtEnd||!t||!t.previousSibling)return!1;if(f(t)&&f(t.previousSibling))a=n.createRange(n.createPositionBefore(t.previousSibling),n.createPositionAfter(t));else if(P(t)&&f(t.previousSibling)&&f(t.previousSibling.previousSibling))a=n.createRange(n.createPositionBefore(t.previousSibling.previousSibling),n.createPositionAfter(t));else if(P(t)&&f(t.previousSibling)&&P(t.previousSibling.previousSibling)&&t.previousSibling.previousSibling&&f(t.previousSibling.previousSibling.previousSibling))a=n.createRange(n.createPositionBefore(t.previousSibling.previousSibling.previousSibling),n.createPositionAfter(t));else return!1;return i.model.change(r=>{r.remove(a),i.execute("enter");const l=o.selection.anchor.parent;r.rename(l,F),i.model.schema.removeDisallowedAttributes([l],r)}),c.scrollToTheSelection(),!0}function P(i){return i&&i.is("$text")&&!i.data.match(/\S/)}function f(i){return i&&i.is("element","softBreak")}function ce(i,{insertAt:e}={}){if(typeof document>"u")return;const n=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css",window.litNonce&&o.setAttribute("nonce",window.litNonce),e==="top"&&n.firstChild?n.insertBefore(o,n.firstChild):n.appendChild(o),o.styleSheet?o.styleSheet.cssText=i:o.appendChild(document.createTextNode(i))}ce(".ck-content pre{background:hsla(0,0%,78%,.3);border:1px solid #c4c4c4;border-radius:2px;color:#353535;direction:ltr;font-style:normal;min-width:200px;padding:1em;tab-size:4;text-align:left;white-space:pre-wrap}.ck-content pre code{background:unset;border-radius:0;padding:0}.ck.ck-editor__editable pre{position:relative}.ck.ck-editor__editable pre[data-language]:after{content:attr(data-language);position:absolute}:root{--ck-color-code-block-label-background:#757575}.ck.ck-editor__editable pre[data-language]:after{background:var(--ck-color-code-block-label-background);color:#fff;font-family:var(--ck-font-face);font-size:10px;line-height:16px;padding:var(--ck-spacing-tiny) var(--ck-spacing-medium);right:10px;top:-1px;white-space:nowrap}.ck.ck-code-block-dropdown .ck-dropdown__panel{max-height:250px;overflow-x:hidden;overflow-y:auto}");/**
* @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 $ extends v{static get pluginName(){return"CodeBlockUI"}static get isOfficialPlugin(){return!0}init(){const e=this.editor,n=e.t,o=e.ui.componentFactory,c=p(e),s=this._getLanguageListItemDefinitions(c),t=e.commands.get("codeBlock");o.add("codeBlock",a=>{const r=K(a,G),l=r.buttonView,d=n("Insert code block");return l.set({label:d,tooltip:!0,icon:T,isToggleable:!0}),l.bind("isOn").to(t,"value",u=>!!u),l.on("execute",()=>{e.execute("codeBlock",{usePreviousLanguageChoice:!0}),e.editing.view.focus()}),r.on("execute",u=>{e.execute("codeBlock",{language:u.source._codeBlockLanguage,forceValue:!0}),e.editing.view.focus()}),r.class="ck-code-block-dropdown",r.bind("isEnabled").to(t),X(r,s,{role:"menu",ariaLabel:d}),r}),o.add("menuBar:codeBlock",a=>{const r=new Q(a);r.buttonView.set({role:"menuitem",label:n("Code block"),icon:T}),r.bind("isEnabled").to(t);const l=new Y(a);l.set({ariaLabel:n("Insert code block")});for(const d of s){const u=new Z(a,r),g=new ee(a);g.bind(...Object.keys(d.model)).to(d.model),g.set({isToggleable:!0,role:"menuitemcheckbox"}),g.delegate("execute").to(r),g.on("execute",()=>{e.execute("codeBlock",{language:d.model._codeBlockLanguage,forceValue:t.value!=d.model._codeBlockLanguage}),e.editing.view.focus()}),u.children.add(g),l.items.add(u)}return r.panelView.children.add(l),r})}_getLanguageListItemDefinitions(e){const o=this.editor.commands.get("codeBlock"),c=new J;for(const s of e){const t={type:"button",model:new te({_codeBlockLanguage:s.language,label:s.label,role:"menuitemradio",withText:!0})};t.model.bind("isOn").to(o,"value",a=>a===t.model._codeBlockLanguage),c.add(t)}return c}}/**
* @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 ae extends v{static get requires(){return[z,$]}static get pluginName(){return"CodeBlock"}static get isOfficialPlugin(){return!0}}export{ae as CodeBlock,y as CodeBlockCommand,z as CodeBlockEditing,$ as CodeBlockUI,D as IndentCodeBlockCommand,V as OutdentCodeBlockCommand,w as _canBeCodeBlock,M as _dataViewToModelCodeBlockInsertion,q as _dataViewToModelCodeBlockOrphanNodeConsumer,R as _dataViewToModelCodeBlockTextNewlinesInsertion,A as _getCodeBlockAriaAnnouncement,B as _getCodeBlockIndentOutdentPositions,b as _getCodeBlockLeadingWhiteSpaces,h as _getCodeBlockPropertyAssociation,L as _getCodeBlockTextNodeAtLineStart,p as _getNormalizedAndLocalizedCodeBlockLanguageDefinitions,x as _isModelSelectionInCodeBlock,O as _modelToDataViewCodeBlockSoftBreakInsertion,_ as _modelToViewCodeBlockInsertion,I as _rawCodeBlockSnippetTextToViewDocumentFragment};
