From d9f4951c1c3a8fabc79a35216d286cf4239d33d5 Mon Sep 17 00:00:00 2001 From: poroyo <132068975+poroyo@users.noreply.github.com> Date: Sat, 11 Jan 2025 14:33:06 +0900 Subject: [PATCH 1/2] feat: add prompt comparison alert for identical prompts --- src/lib/Setting/botpreset.svelte | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib/Setting/botpreset.svelte b/src/lib/Setting/botpreset.svelte index d4b94a7b..8c43096b 100644 --- a/src/lib/Setting/botpreset.svelte +++ b/src/lib/Setting/botpreset.svelte @@ -111,7 +111,7 @@ return prompt } - async function checkDiff(prompt1: string, prompt2: string): Promise { + async function checkDiff(prompt1: string, prompt2: string): Promise { const { diffLines } = await import('diff') const lineDiffs = diffLines(prompt1, prompt2) @@ -139,13 +139,17 @@ } if(lineDiffs.length === 1 && !lineDiffs[0].added && !lineDiffs[0].removed) { - resultHtml = `
No differences detected.
` + resultHtml + const userResponse = await alertConfirm('The two prompts are identical. Would you like to review the content?') + + if(userResponse){ + resultHtml = `
No differences detected.
` + resultHtml + alertMd(resultHtml) + } } else{ resultHtml = `
Differences detected. Please review the changes.
` + resultHtml + alertMd(resultHtml) } - - return resultHtml } async function highlightChanges(string1: string, string2: string) { @@ -194,8 +198,8 @@ } else{ alertWait("Loading...") - const result = await checkDiff(selectedPrompts[0], prompt) - alertMd(result) + await checkDiff(selectedPrompts[0], prompt) + selectedDiffPreset = -1 selectedPrompts = [] } From 2ee72ca86fe456e5e936a908671aeb246085e033 Mon Sep 17 00:00:00 2001 From: poroyo <132068975+poroyo@users.noreply.github.com> Date: Sat, 11 Jan 2025 18:30:45 +0900 Subject: [PATCH 2/2] feat: add tooltip for differences overview --- src/lib/Setting/botpreset.svelte | 67 ++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/src/lib/Setting/botpreset.svelte b/src/lib/Setting/botpreset.svelte index 8c43096b..e633bec7 100644 --- a/src/lib/Setting/botpreset.svelte +++ b/src/lib/Setting/botpreset.svelte @@ -116,6 +116,10 @@ const lineDiffs = diffLines(prompt1, prompt2) let resultHtml = ''; + let changedLines: string[] = [] + let modifiedLinesCount = 0 + let addedLinesCount = 0 + let removedLinesCount = 0 for (let i = 0; i < lineDiffs.length; i++) { const linePart = lineDiffs[i] @@ -123,15 +127,24 @@ if(linePart.removed){ const nextPart = lineDiffs[i + 1] if(nextPart?.added){ - resultHtml += `
${await highlightChanges(linePart.value, nextPart.value)}
` + const modifiedLine = `
${await highlightChanges(linePart.value, nextPart.value)}
` + changedLines.push(modifiedLine) + resultHtml += modifiedLine i++; + modifiedLinesCount += 1 } else{ - resultHtml += `
${escapeHtml(linePart.value)}
` + const removedLine = `
${escapeHtml(linePart.value)}
` + changedLines.push(removedLine) + resultHtml += removedLine + removedLinesCount += 1 } } else if(linePart.added){ - resultHtml += `
${escapeHtml(linePart.value)}
` + const addedLine = `
${escapeHtml(linePart.value)}
` + changedLines.push(addedLine) + resultHtml += addedLine + addedLinesCount += 1 } else{ resultHtml += `
${escapeHtml(linePart.value)}
` @@ -147,8 +160,20 @@ } } else{ - resultHtml = `
Differences detected. Please review the changes.
` + resultHtml + const modifiedCount = `${modifiedLinesCount}` + const addedCount = `${addedLinesCount}` + const removedCount = `${removedLinesCount}` + const diffCounts = `
${modifiedCount}${addedCount}${removedCount}
` + + resultHtml = `
Differences detected. Please review the changes.${diffCounts}
` + resultHtml alertMd(resultHtml) + + setTimeout(() => { + const differencesDetected = document.querySelector('#differences-detected'); + if (differencesDetected) { + differencesTooltip(changedLines) + } + }, 0) } } @@ -172,6 +197,40 @@ }) .join('') } + + function differencesTooltip(changedLines: string[]) { + const differencesDetected = document.querySelector('#differences-detected') + if(!differencesDetected){ + return + } + + const tooltip = document.createElement('div') + tooltip.id = 'diff-tooltip' + tooltip.style.display = 'none' + tooltip.style.position = 'absolute' + tooltip.style.backgroundColor = '#282a36' + tooltip.style.padding = '10px' + tooltip.style.borderRadius = '5px' + tooltip.style.boxShadow = '0px 5px 5px rgba(0, 0, 0, 1)' + tooltip.style.maxWidth = '500px' + tooltip.style.overflowY = 'auto' + tooltip.style.maxHeight = '300px' + tooltip.style.textAlign = 'initial' + + differencesDetected.appendChild(tooltip) + + differencesDetected.addEventListener('mouseenter', () => { + const tooltipContent = !changedLines.length ? '' : `
Changed Lines
+
${changedLines.join('
')}
` + + tooltip.innerHTML = tooltipContent; + tooltip.style.display = 'block' + }) + + differencesDetected.addEventListener('mouseleave', () => { + tooltip.style.display = 'none' + }) + } async function handleDiffMode(id: number) {