class EimiScriptCodeBlockDiffer { constructor({eimiApi}) { this.eimiApi = eimiApi; this.scriptState = window._scriptCodeDiffer = window._scriptCodeDiffer || {}; this.linesToKeep = 2; // top+bottom this.addDiffLibrary(); this.eimiApi.createEmptyWindow({ title: "Code Block Diff", height: window.innerHeight * 0.98, left: 50, onReady: (arg) => { this.windowArgs = arg; arg.el.innerHTML = `
Focus on a message with code blocks to see diff
No code blocks found in messages
'; return; } const code1 = firstCodeBlocks[firstCodeBlocks.length - 1]; const code2 = secondCodeBlocks[secondCodeBlocks.length - 1]; // Preserve scroll position of the entire window let scrollTop = 0; const diffContentEl = this.windowArgs.el.querySelector('#diff-content'); if (diffContentEl && diffContentEl.firstElementChild) { scrollTop = diffContentEl.firstElementChild.scrollTop; } diffContentEl.innerHTML = this.createDiffView(code1, code2); // Restore scroll position after updating content if (diffContentEl && diffContentEl.firstElementChild) { diffContentEl.firstElementChild.scrollTop = scrollTop; } } extractCodeBlocks(content) { const codeBlockRegex = /```[\s\S]*?```/g; const matches = content.match(codeBlockRegex) || []; return matches.map(block => block.slice(3, -3).trim()); } createDiffView(code1, code2) { if (typeof Diff === 'undefined') { return "Diff library not loaded. Please try again.
"; } const escapeHtml = (unsafe) => { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }; const truncateNonChangedBlock = (lines) => { if (lines.length <= (this.linesToKeep * 2) + 1) { return lines; } const top = lines.slice(0, this.linesToKeep); const bottom = lines.slice(-this.linesToKeep-1); return [...top, '[...]', ...bottom]; }; const diff = Diff.diffLines(code1, code2); let result = '