enh: text select copy behaviour
This commit is contained in:
1 parent
38f45a38cb
commit
2de854fa02
1 file changed
+61
-9
@@ -2,7 +2,7 @@
|
||||
import { toast } from 'svelte-sonner';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
import { createEventDispatcher, onDestroy } from 'svelte';
|
||||
import { onMount, tick, getContext } from 'svelte';
|
||||
import type { Writable } from 'svelte/store';
|
||||
import type { i18n as i18nType, t } from 'i18next';
|
||||
@@ -152,6 +152,8 @@
|
||||
export let topPadding = false;
|
||||
|
||||
let citationsElement: HTMLDivElement;
|
||||
|
||||
let contentContainerElement: HTMLDivElement;
|
||||
let buttonsContainerElement: HTMLDivElement;
|
||||
let showDeleteConfirm = false;
|
||||
|
||||
@@ -541,12 +543,8 @@
|
||||
})();
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
// console.log('ResponseMessage mounted');
|
||||
|
||||
await tick();
|
||||
const buttonsWheelHandler = (event: WheelEvent) => {
|
||||
if (buttonsContainerElement) {
|
||||
buttonsContainerElement.addEventListener('wheel', function (event) {
|
||||
if (buttonsContainerElement.scrollWidth <= buttonsContainerElement.clientWidth) {
|
||||
// If the container is not scrollable, horizontal scroll
|
||||
return;
|
||||
@@ -558,7 +556,57 @@
|
||||
buttonsContainerElement.scrollLeft += event.deltaY;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const contentCopyHandler = (e) => {
|
||||
if (contentContainerElement) {
|
||||
e.preventDefault();
|
||||
// Get the selected HTML
|
||||
const selection = window.getSelection();
|
||||
const range = selection.getRangeAt(0);
|
||||
const tempDiv = document.createElement('div');
|
||||
|
||||
// Remove background, color, and font styles
|
||||
tempDiv.appendChild(range.cloneContents());
|
||||
|
||||
tempDiv.querySelectorAll('table').forEach((table) => {
|
||||
table.style.borderCollapse = 'collapse';
|
||||
table.style.width = 'auto';
|
||||
table.style.tableLayout = 'auto';
|
||||
});
|
||||
|
||||
tempDiv.querySelectorAll('th').forEach((th) => {
|
||||
th.style.whiteSpace = 'nowrap';
|
||||
th.style.padding = '4px 8px';
|
||||
});
|
||||
|
||||
// Put cleaned HTML + plain text into clipboard
|
||||
e.clipboardData.setData('text/html', tempDiv.innerHTML);
|
||||
e.clipboardData.setData('text/plain', selection.toString());
|
||||
}
|
||||
};
|
||||
|
||||
onMount(async () => {
|
||||
// console.log('ResponseMessage mounted');
|
||||
|
||||
await tick();
|
||||
if (buttonsContainerElement) {
|
||||
buttonsContainerElement.addEventListener('wheel', buttonsWheelHandler);
|
||||
}
|
||||
|
||||
if (contentContainerElement) {
|
||||
contentContainerElement.addEventListener('copy', contentCopyHandler);
|
||||
}
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
if (buttonsContainerElement) {
|
||||
buttonsContainerElement.removeEventListener('wheel', buttonsWheelHandler);
|
||||
}
|
||||
|
||||
if (contentContainerElement) {
|
||||
contentContainerElement.removeEventListener('copy', contentCopyHandler);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -719,8 +767,13 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="w-full flex flex-col relative" id="response-content-container">
|
||||
{/if}
|
||||
|
||||
<div
|
||||
bind:this={contentContainerElement}
|
||||
class="w-full flex flex-col relative {edit ? 'hidden' : ''}"
|
||||
id="response-content-container"
|
||||
>
|
||||
{#if message.content === '' && !message.error && ((model?.info?.meta?.capabilities?.status_updates ?? true) ? (message?.statusHistory ?? [...(message?.status ? [message?.status] : [])]).length === 0 || (message?.statusHistory?.at(-1)?.hidden ?? false) : true)}
|
||||
<Skeleton />
|
||||
{:else if message.content && message.error !== true}
|
||||
@@ -784,7 +837,6 @@
|
||||
<CodeExecutions codeExecutions={message.code_executions} />
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user