iobroker.javascript
Version:
Rules Engine for ioBroker
16 lines • 7.29 kB
JavaScript
import{f as e,jn as t}from"./_virtual_mf___mfe_internal__iobroker_javascript__mf_owner__1__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-CxICLr5Z.js";import{t as n}from"./applyCodeEdit-D3nb7erO.js";function r(e,t){let n=e===``?[]:e.split(`
`),r=t===``?[]:t.split(`
`),i=n.length,a=r.length;if(i===0)return r.map(e=>({op:`insert`,line:e}));if(a===0)return n.map(e=>({op:`delete`,line:e}));let o=Array.from({length:i+1},()=>Array(a+1).fill(0));for(let e=1;e<=i;e++)for(let t=1;t<=a;t++)o[e][t]=n[e-1]===r[t-1]?o[e-1][t-1]+1:Math.max(o[e-1][t],o[e][t-1]);let s=[],c=i,l=a;for(;c>0&&l>0;)n[c-1]===r[l-1]?(s.unshift({op:`equal`,line:n[c-1]}),c--,l--):o[c-1][l]>o[c][l-1]?(s.unshift({op:`delete`,line:n[c-1]}),c--):(s.unshift({op:`insert`,line:r[l-1]}),l--);for(;c>0;)s.unshift({op:`delete`,line:n[--c]});for(;l>0;)s.unshift({op:`insert`,line:r[--l]});return s}function i(e){let t=[],n=0,r=null,i=()=>{r&&=(t.push(r),null)};for(let t of e){if(t.op===`equal`){i(),n++;continue}r||={originalStart:n,deletedCount:0,insertedLines:[]},t.op===`delete`?(r.deletedCount++,n++):r.insertedLines.push(t.line)}return i(),t}var a=`iobroker.aichat.inlineDiff.toolbar`,o=class{constructor(e,n,r){t(this,`editor`,void 0),t(this,`monaco`,void 0),t(this,`opts`,void 0),t(this,`decorations`,[]),t(this,`viewZoneIds`,[]),t(this,`toolbarSpacerZoneId`,null),t(this,`toolbarWidget`,null),t(this,`disposed`,!1),this.editor=e,this.monaco=n,this.opts=r}show(){if(this.disposed)return;let{range:e,originalText:t,modifiedText:n}=this.opts,a=i(r(t,n));if(!a.length){var o,s;(o=(s=this.opts).onAccepted)==null||o.call(s);return}if(this.renderDeletions(e,a),this.renderInsertions(e,a),this.renderAcceptWidget(e),this.editor.getModel()){let e=this.editor.onDidChangeModelContent(()=>{if(e.dispose(),!this.disposed){var t,n;this.dispose(),(t=(n=this.opts).onRejected)==null||t.call(n)}})}}renderDeletions(e,t){let n=[],r=e.startLine;for(let e of t){if(e.deletedCount===0)continue;let t=r+e.originalStart,i=t+e.deletedCount-1;n.push({range:new this.monaco.Range(t,1,i,1),options:{isWholeLine:!0,className:`iob-aichat-diff-deleted`,linesDecorationsClassName:`iob-aichat-diff-deleted-gutter`,inlineClassName:`iob-aichat-diff-deleted-inline`}})}this.decorations=this.editor.deltaDecorations([],n)}renderInsertions(e,t){this.editor.changeViewZones(n=>{let r=e.startLine;for(let e of t){if(e.insertedLines.length===0)continue;let t=e.deletedCount>0?r+e.originalStart+e.deletedCount-1:r+e.originalStart-1,i=document.createElement(`div`);i.className=`iob-aichat-diff-inserted`,i.style.cssText=[`background: rgba(46, 160, 67, 0.15)`,`border-left: 3px solid rgb(46, 160, 67)`,`padding: 2px 8px 2px 10px`,`font-family: var(--monaco-monospace-font, monospace)`,`white-space: pre`,`overflow-x: auto`,`font-size: 12px`,`line-height: 18px`].join(`;`);for(let t of e.insertedLines){let e=document.createElement(`div`);e.textContent=t.length?t:`\xA0`,i.appendChild(e)}let a=n.addZone({afterLineNumber:Math.max(0,t),heightInLines:e.insertedLines.length,domNode:i});this.viewZoneIds.push(a)}})}renderAcceptWidget(t){let n=this.monaco,r=this.editor,i=!0;try{let e=r.getDomNode();if(e){let t=getComputedStyle(e).backgroundColor.match(/\d+/g);if(t&&t.length>=3){let[e,n,r]=t.map(e=>parseInt(e,10));i=(.299*e+.587*n+.114*r)/255<.5}}}catch{}let o=i?{bg:`rgba(37, 37, 38, 0.97)`,text:`#cccccc`,labelMuted:`#a0a0a0`,border:`rgba(255, 255, 255, 0.12)`,secondaryBg:`rgba(90, 93, 94, 0.4)`,secondaryHoverBg:`rgba(90, 93, 94, 0.6)`,secondaryText:`#cccccc`,primaryBg:`rgb(14, 99, 156)`,primaryHoverBg:`rgb(17, 119, 187)`,primaryText:`#ffffff`,accent:`rgb(79, 149, 255)`}:{bg:`rgba(243, 243, 243, 0.97)`,text:`#1f1f1f`,labelMuted:`#616161`,border:`rgba(0, 0, 0, 0.12)`,secondaryBg:`rgba(0, 0, 0, 0.04)`,secondaryHoverBg:`rgba(0, 0, 0, 0.08)`,secondaryText:`#1f1f1f`,primaryBg:`rgb(0, 98, 165)`,primaryHoverBg:`rgb(0, 120, 200)`,primaryText:`#ffffff`,accent:`rgb(0, 98, 165)`},s=document.createElement(`div`);s.className=`iob-aichat-diff-toolbar`,s.style.cssText=[`display: flex`,`align-items: center`,`gap: 6px`,`padding: 2px 8px`,`height: 22px`,`box-sizing: border-box`,`font-family: var(--monaco-monospace-font, system-ui)`,`font-size: 11px`,`color: ${o.text}`,`background: ${o.bg}`,`border: 1px solid ${o.border}`,`border-left: 3px solid ${o.accent}`,`border-radius: 3px`,`box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25)`,`pointer-events: auto`,`user-select: none`,`cursor: default`,`white-space: nowrap`].join(`;`);let c=document.createElement(`span`);c.textContent=`🤖 ${e.t(`AI suggestion`)}`,c.style.cssText=`color: ${o.labelMuted}; margin-right: 4px;`,s.appendChild(c);let l=(e,t,n)=>{let r=document.createElement(`button`);r.type=`button`,r.textContent=e;let i=t?o.primaryBg:o.secondaryBg,a=t?o.primaryHoverBg:o.secondaryHoverBg,s=t?o.primaryText:o.secondaryText;return r.style.cssText=[`background: ${i}`,`color: ${s}`,`border: 1px solid ${t?`transparent`:o.border}`,`border-radius: 2px`,`padding: 2px 10px`,`cursor: pointer`,`font-family: inherit`,`font-size: 11px`,`line-height: 16px`,`white-space: nowrap`,`pointer-events: auto`].join(`;`),r.addEventListener(`mouseenter`,()=>{r.style.background=a}),r.addEventListener(`mouseleave`,()=>{r.style.background=i}),r.addEventListener(`click`,e=>{e.stopPropagation(),e.preventDefault(),n(e)}),r.addEventListener(`mousedown`,e=>e.stopPropagation(),!0),r},u=l(`✓ ${e.t(`Apply changes`)}`,!0,()=>this.accept()),d=l(`✗ ${e.t(`Discard changes`)}`,!1,()=>this.reject());s.appendChild(u),s.appendChild(d),s.addEventListener(`mousedown`,e=>e.stopPropagation(),!0),s.addEventListener(`click`,e=>e.stopPropagation());let f=document.createElement(`div`);f.style.cssText=`pointer-events: none;`;let p=Math.max(0,t.startLine-1);this.editor.changeViewZones(e=>{this.toolbarSpacerZoneId=e.addZone({afterLineNumber:p,heightInLines:1,domNode:f})});let m=t.startLine,h={getId(){return a},getDomNode(){return s},getPosition(){return{position:{lineNumber:m,column:1},preference:[n.editor.ContentWidgetPositionPreference.ABOVE,n.editor.ContentWidgetPositionPreference.BELOW]}},allowEditorOverflow:!0};this.toolbarWidget=h,r.addContentWidget(h)}accept(){if(this.disposed)return;let e=n(this.editor,this.monaco,this.opts.range,this.opts.modifiedText,{source:`ai-inline-diff`});if(this.dispose(),e){var t,r;(t=(r=this.opts).onAccepted)==null||t.call(r)}}reject(){var e,t;this.disposed||(this.dispose(),(e=(t=this.opts).onRejected)==null||e.call(t))}dispose(){if(!this.disposed){this.disposed=!0;try{this.editor.deltaDecorations(this.decorations,[])}catch{}try{this.editor.changeViewZones(e=>{for(let t of this.viewZoneIds)e.removeZone(t);this.toolbarSpacerZoneId&&e.removeZone(this.toolbarSpacerZoneId)})}catch{}if(this.toolbarWidget){try{this.editor.removeContentWidget(this.toolbarWidget)}catch{}this.toolbarWidget=null}this.toolbarSpacerZoneId=null,this.decorations=[],this.viewZoneIds=[]}}},s=`
.iob-aichat-diff-deleted {
background: rgba(220, 53, 69, 0.15) !important;
}
.iob-aichat-diff-deleted-inline {
text-decoration: line-through rgba(220, 53, 69, 0.9);
}
.iob-aichat-diff-deleted-gutter {
background: rgb(220, 53, 69);
width: 3px !important;
margin-left: 0 !important;
}
.iob-aichat-diff-inserted { pointer-events: auto; }
`;export{s as INLINE_DIFF_CSS,o as InlineDiffController};