1 parent
b3e8709272
commit
efba56c8c0
22 files changed
+814
-339
No files matched your search
@@ -0,0 +1,204 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onUnmounted, ref, watch, type CSSProperties } from "vue";
|
||||
import { Copy } from "@lucide/vue";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import VirtualScrollArea from "@/components/common/VirtualScrollArea.vue";
|
||||
import { buildTextDiff, type TextDiffKind } from "@/lib/common/textDiff";
|
||||
|
||||
export type TextDiffSide = "before" | "after";
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
beforeText: string;
|
||||
afterText: string;
|
||||
beforeLabel: string;
|
||||
afterLabel: string;
|
||||
copyBeforeTitle: string;
|
||||
copyAfterTitle: string;
|
||||
beforeAvailable?: boolean;
|
||||
afterAvailable?: boolean;
|
||||
}>(),
|
||||
{
|
||||
beforeAvailable: true,
|
||||
afterAvailable: true,
|
||||
},
|
||||
);
|
||||
|
||||
const emit = defineEmits<{
|
||||
copy: [side: TextDiffSide];
|
||||
}>();
|
||||
|
||||
const beforeScrollArea = ref<InstanceType<typeof VirtualScrollArea> | null>(null);
|
||||
const afterScrollArea = ref<InstanceType<typeof VirtualScrollArea> | null>(null);
|
||||
const scrollTop = ref(0);
|
||||
const viewportHeight = ref(0);
|
||||
let scrollSyncing = false;
|
||||
let scrollSyncFrame = 0;
|
||||
|
||||
const ROW_HEIGHT = 22;
|
||||
const ROW_BUFFER = 12;
|
||||
|
||||
const diffRows = computed(() => {
|
||||
const rows = buildTextDiff(props.beforeText, props.afterText);
|
||||
if (!props.beforeAvailable && props.afterAvailable) return rows.map((row) => ({ ...row, kind: "added" as const }));
|
||||
if (!props.afterAvailable && props.beforeAvailable) return rows.map((row) => ({ ...row, kind: "removed" as const }));
|
||||
return rows;
|
||||
});
|
||||
const visibleStart = computed(() => Math.max(0, Math.floor(scrollTop.value / ROW_HEIGHT) - ROW_BUFFER));
|
||||
const visibleEnd = computed(() => Math.min(diffRows.value.length, Math.ceil((scrollTop.value + viewportHeight.value) / ROW_HEIGHT) + ROW_BUFFER));
|
||||
const visibleRows = computed(() => diffRows.value.slice(visibleStart.value, visibleEnd.value).map((row, offset) => ({ row, index: visibleStart.value + offset })));
|
||||
const longestLineLength = computed(() => {
|
||||
let maximum = 0;
|
||||
for (const row of diffRows.value) maximum = Math.max(maximum, row.beforeText.length, row.afterText.length);
|
||||
return Math.min(20_000, maximum);
|
||||
});
|
||||
const textContentStyle = computed<CSSProperties>(() => ({
|
||||
height: `${Math.max(1, diffRows.value.length * ROW_HEIGHT)}px`,
|
||||
width: `max(100%, ${Math.max(40, longestLineLength.value + 8)}ch)`,
|
||||
}));
|
||||
|
||||
function rowClass(kind: TextDiffKind, side: TextDiffSide): string {
|
||||
if (kind === "modified") return "side-by-side-text-row-modified";
|
||||
if (kind === "added" && side === "after") return "side-by-side-text-row-added";
|
||||
if (kind === "removed" && side === "before") return "side-by-side-text-row-removed";
|
||||
return "";
|
||||
}
|
||||
|
||||
function updateViewport(element: HTMLElement) {
|
||||
scrollTop.value = element.scrollTop;
|
||||
viewportHeight.value = element.clientHeight;
|
||||
}
|
||||
|
||||
function syncScroll(side: TextDiffSide, source: HTMLElement) {
|
||||
updateViewport(source);
|
||||
if (scrollSyncing) return;
|
||||
const target = side === "before" ? afterScrollArea.value?.scrollerElement() : beforeScrollArea.value?.scrollerElement();
|
||||
if (!target) return;
|
||||
scrollSyncing = true;
|
||||
target.scrollTop = source.scrollTop;
|
||||
target.scrollLeft = source.scrollLeft;
|
||||
if (scrollSyncFrame) cancelAnimationFrame(scrollSyncFrame);
|
||||
scrollSyncFrame = requestAnimationFrame(() => {
|
||||
scrollSyncFrame = 0;
|
||||
scrollSyncing = false;
|
||||
});
|
||||
}
|
||||
|
||||
function onResize(element: HTMLElement) {
|
||||
viewportHeight.value = Math.max(viewportHeight.value, element.clientHeight);
|
||||
}
|
||||
|
||||
async function resetScroll() {
|
||||
scrollTop.value = 0;
|
||||
await nextTick();
|
||||
beforeScrollArea.value?.scrollerElement()?.scrollTo({ top: 0, left: 0 });
|
||||
afterScrollArea.value?.scrollerElement()?.scrollTo({ top: 0, left: 0 });
|
||||
}
|
||||
|
||||
watch(() => [props.beforeText, props.afterText], resetScroll);
|
||||
|
||||
onUnmounted(() => {
|
||||
if (scrollSyncFrame) cancelAnimationFrame(scrollSyncFrame);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="grid min-h-0 flex-1 grid-cols-2 overflow-hidden">
|
||||
<section class="flex min-h-0 min-w-0 flex-col border-r">
|
||||
<div class="flex h-8 shrink-0 items-center gap-2 border-b bg-muted/20 px-2 text-[11px]">
|
||||
<span class="min-w-0 flex-1 truncate font-mono text-muted-foreground" :title="beforeLabel">{{ beforeLabel }}</span>
|
||||
<Button variant="ghost" size="icon" class="h-6 w-6" :disabled="!beforeAvailable" :title="copyBeforeTitle" @click="emit('copy', 'before')"><Copy class="h-3 w-3" /></Button>
|
||||
</div>
|
||||
<VirtualScrollArea ref="beforeScrollArea" class="min-h-0 flex-1" scroller-class="side-by-side-text-before-scroller" @scroll="syncScroll('before', $event)" @resize="onResize">
|
||||
<div class="side-by-side-text-content" :style="textContentStyle">
|
||||
<div v-for="visible in visibleRows" :key="`before-${visible.index}`" class="side-by-side-text-row" :class="rowClass(visible.row.kind, 'before')" :style="{ top: `${visible.index * ROW_HEIGHT}px`, height: `${ROW_HEIGHT}px` }">
|
||||
<span class="side-by-side-text-line-number">{{ visible.row.beforeLineNumber ?? "" }}</span>
|
||||
<code class="side-by-side-text-code"
|
||||
><template v-for="(segment, index) in visible.row.beforeSegments" :key="index"
|
||||
><span :class="{ 'side-by-side-text-segment-changed': segment.changed }">{{ segment.text }}</span></template
|
||||
> </code
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</VirtualScrollArea>
|
||||
</section>
|
||||
|
||||
<section class="flex min-h-0 min-w-0 flex-col">
|
||||
<div class="flex h-8 shrink-0 items-center gap-2 border-b bg-muted/20 px-2 text-[11px]">
|
||||
<span class="min-w-0 flex-1 truncate font-mono text-muted-foreground" :title="afterLabel">{{ afterLabel }}</span>
|
||||
<Button variant="ghost" size="icon" class="h-6 w-6" :disabled="!afterAvailable" :title="copyAfterTitle" @click="emit('copy', 'after')"><Copy class="h-3 w-3" /></Button>
|
||||
</div>
|
||||
<VirtualScrollArea ref="afterScrollArea" class="min-h-0 flex-1" scroller-class="side-by-side-text-after-scroller" @scroll="syncScroll('after', $event)" @resize="onResize">
|
||||
<div class="side-by-side-text-content" :style="textContentStyle">
|
||||
<div v-for="visible in visibleRows" :key="`after-${visible.index}`" class="side-by-side-text-row" :class="rowClass(visible.row.kind, 'after')" :style="{ top: `${visible.index * ROW_HEIGHT}px`, height: `${ROW_HEIGHT}px` }">
|
||||
<span class="side-by-side-text-line-number">{{ visible.row.afterLineNumber ?? "" }}</span>
|
||||
<code class="side-by-side-text-code"
|
||||
><template v-for="(segment, index) in visible.row.afterSegments" :key="index"
|
||||
><span :class="{ 'side-by-side-text-segment-changed': segment.changed }">{{ segment.text }}</span></template
|
||||
> </code
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</VirtualScrollArea>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.side-by-side-text-content {
|
||||
position: relative;
|
||||
min-width: 100%;
|
||||
font-family: var(--font-mono);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.side-by-side-text-row {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
display: flex;
|
||||
width: 100%;
|
||||
min-width: 100%;
|
||||
align-items: center;
|
||||
border-bottom: 1px solid color-mix(in srgb, var(--border) 45%, transparent);
|
||||
line-height: 21px;
|
||||
}
|
||||
|
||||
.side-by-side-text-line-number {
|
||||
position: sticky;
|
||||
left: 0;
|
||||
z-index: 1;
|
||||
width: 48px;
|
||||
height: 100%;
|
||||
flex: 0 0 48px;
|
||||
border-right: 1px solid var(--border);
|
||||
background: var(--background);
|
||||
padding-right: 8px;
|
||||
text-align: right;
|
||||
color: var(--muted-foreground);
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.side-by-side-text-code {
|
||||
display: block;
|
||||
min-width: 0;
|
||||
padding: 0 8px;
|
||||
white-space: pre;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.side-by-side-text-row-modified {
|
||||
background: rgb(217 119 6 / 0.11);
|
||||
}
|
||||
|
||||
.side-by-side-text-row-added {
|
||||
background: rgb(22 163 74 / 0.11);
|
||||
}
|
||||
|
||||
.side-by-side-text-row-removed {
|
||||
background: rgb(220 38 38 / 0.1);
|
||||
}
|
||||
|
||||
.side-by-side-text-row-modified .side-by-side-text-segment-changed {
|
||||
background: rgb(217 119 6 / 0.32);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,272 @@
|
||||
<script setup lang="ts">
|
||||
import { nextTick, onMounted, onUnmounted, ref } from "vue";
|
||||
|
||||
const props = defineProps<{
|
||||
scrollerClass?: string;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
scroll: [element: HTMLElement];
|
||||
resize: [element: HTMLElement];
|
||||
}>();
|
||||
|
||||
const scroller = ref<HTMLElement | null>(null);
|
||||
const horizontalThumb = ref<HTMLElement | null>(null);
|
||||
const verticalThumb = ref<HTMLElement | null>(null);
|
||||
const hasHorizontalOverflow = ref(false);
|
||||
const hasVerticalOverflow = ref(false);
|
||||
let horizontalThumbLeft = 0;
|
||||
let horizontalThumbWidth = 100;
|
||||
let verticalThumbTop = 0;
|
||||
let verticalThumbHeight = 100;
|
||||
let resizeObserver: ResizeObserver | undefined;
|
||||
let mutationObserver: MutationObserver | undefined;
|
||||
let refreshFrame = 0;
|
||||
let dragFrame = 0;
|
||||
let pendingDragPosition = 0;
|
||||
let dragState:
|
||||
| {
|
||||
axis: "horizontal" | "vertical";
|
||||
track: HTMLElement;
|
||||
trackRect: DOMRect;
|
||||
thumbOffset: number;
|
||||
maxScroll: number;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
function applyThumbStyles() {
|
||||
if (horizontalThumb.value) {
|
||||
horizontalThumb.value.style.left = `${horizontalThumbLeft}%`;
|
||||
horizontalThumb.value.style.width = `${horizontalThumbWidth}%`;
|
||||
}
|
||||
if (verticalThumb.value) {
|
||||
verticalThumb.value.style.top = `${verticalThumbTop}%`;
|
||||
verticalThumb.value.style.height = `${verticalThumbHeight}%`;
|
||||
}
|
||||
}
|
||||
|
||||
function updateMetrics(emitResize = false) {
|
||||
const element = scroller.value;
|
||||
if (!element) return;
|
||||
const maxScrollLeft = Math.max(0, element.scrollWidth - element.clientWidth);
|
||||
const maxScrollTop = Math.max(0, element.scrollHeight - element.clientHeight);
|
||||
hasHorizontalOverflow.value = maxScrollLeft > 1;
|
||||
hasVerticalOverflow.value = maxScrollTop > 1;
|
||||
|
||||
const rawHorizontalWidth = element.scrollWidth > 0 ? (element.clientWidth / element.scrollWidth) * 100 : 100;
|
||||
horizontalThumbWidth = Math.min(100, Math.max(6, rawHorizontalWidth));
|
||||
horizontalThumbLeft = maxScrollLeft > 0 ? (element.scrollLeft / maxScrollLeft) * Math.max(0, 100 - horizontalThumbWidth) : 0;
|
||||
const rawVerticalHeight = element.scrollHeight > 0 ? (element.clientHeight / element.scrollHeight) * 100 : 100;
|
||||
verticalThumbHeight = Math.min(100, Math.max(6, rawVerticalHeight));
|
||||
verticalThumbTop = maxScrollTop > 0 ? (element.scrollTop / maxScrollTop) * Math.max(0, 100 - verticalThumbHeight) : 0;
|
||||
nextTick(applyThumbStyles);
|
||||
if (emitResize) emit("resize", element);
|
||||
}
|
||||
|
||||
function scheduleMetricsRefresh(emitResize = false) {
|
||||
if (refreshFrame) cancelAnimationFrame(refreshFrame);
|
||||
refreshFrame = requestAnimationFrame(() => {
|
||||
refreshFrame = 0;
|
||||
updateMetrics(emitResize);
|
||||
});
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
const element = scroller.value;
|
||||
if (!element) return;
|
||||
updateMetrics();
|
||||
emit("scroll", element);
|
||||
}
|
||||
|
||||
function applyDrag() {
|
||||
dragFrame = 0;
|
||||
const state = dragState;
|
||||
const element = scroller.value;
|
||||
if (!state || !element) return;
|
||||
const trackLength = state.axis === "horizontal" ? state.trackRect.width : state.trackRect.height;
|
||||
const thumbPercent = state.axis === "horizontal" ? horizontalThumbWidth : verticalThumbHeight;
|
||||
const thumbLength = trackLength * (thumbPercent / 100);
|
||||
const maximumThumbPosition = Math.max(1, trackLength - thumbLength);
|
||||
const pointerPosition = pendingDragPosition - (state.axis === "horizontal" ? state.trackRect.left : state.trackRect.top);
|
||||
const thumbPosition = Math.min(maximumThumbPosition, Math.max(0, pointerPosition - state.thumbOffset));
|
||||
const scrollPosition = (thumbPosition / maximumThumbPosition) * state.maxScroll;
|
||||
if (state.axis === "horizontal") element.scrollLeft = scrollPosition;
|
||||
else element.scrollTop = scrollPosition;
|
||||
onScroll();
|
||||
}
|
||||
|
||||
function onDragMove(event: PointerEvent) {
|
||||
if (!dragState) return;
|
||||
event.preventDefault();
|
||||
pendingDragPosition = dragState.axis === "horizontal" ? event.clientX : event.clientY;
|
||||
if (!dragFrame) dragFrame = requestAnimationFrame(applyDrag);
|
||||
}
|
||||
|
||||
function stopDrag() {
|
||||
if (!dragState) return;
|
||||
if (dragFrame) {
|
||||
cancelAnimationFrame(dragFrame);
|
||||
applyDrag();
|
||||
}
|
||||
dragState.track.classList.remove("dolt-scrollbar-dragging");
|
||||
dragState = undefined;
|
||||
document.body.style.userSelect = "";
|
||||
window.removeEventListener("pointermove", onDragMove, true);
|
||||
window.removeEventListener("pointerup", stopDrag, true);
|
||||
window.removeEventListener("pointercancel", stopDrag, true);
|
||||
}
|
||||
|
||||
function startDrag(axis: "horizontal" | "vertical", event: PointerEvent) {
|
||||
const element = scroller.value;
|
||||
const track = event.currentTarget as HTMLElement;
|
||||
if (!element) return;
|
||||
const maxScroll = axis === "horizontal" ? element.scrollWidth - element.clientWidth : element.scrollHeight - element.clientHeight;
|
||||
if (maxScroll <= 1) return;
|
||||
const trackRect = track.getBoundingClientRect();
|
||||
const trackLength = axis === "horizontal" ? trackRect.width : trackRect.height;
|
||||
const thumbPercent = axis === "horizontal" ? horizontalThumbWidth : verticalThumbHeight;
|
||||
const thumbStartPercent = axis === "horizontal" ? horizontalThumbLeft : verticalThumbTop;
|
||||
const thumbLength = trackLength * (thumbPercent / 100);
|
||||
const thumbStart = trackLength * (thumbStartPercent / 100);
|
||||
const pointerPosition = axis === "horizontal" ? event.clientX - trackRect.left : event.clientY - trackRect.top;
|
||||
const pointerInsideThumb = pointerPosition >= thumbStart && pointerPosition <= thumbStart + thumbLength;
|
||||
dragState = {
|
||||
axis,
|
||||
track,
|
||||
trackRect,
|
||||
thumbOffset: pointerInsideThumb ? pointerPosition - thumbStart : thumbLength / 2,
|
||||
maxScroll,
|
||||
};
|
||||
pendingDragPosition = axis === "horizontal" ? event.clientX : event.clientY;
|
||||
track.classList.add("dolt-scrollbar-dragging");
|
||||
document.body.style.userSelect = "none";
|
||||
window.addEventListener("pointermove", onDragMove, true);
|
||||
window.addEventListener("pointerup", stopDrag, true);
|
||||
window.addEventListener("pointercancel", stopDrag, true);
|
||||
event.preventDefault();
|
||||
applyDrag();
|
||||
}
|
||||
|
||||
function scrollerElement(): HTMLElement | null {
|
||||
return scroller.value;
|
||||
}
|
||||
|
||||
defineExpose({ scrollerElement, refresh: scheduleMetricsRefresh });
|
||||
|
||||
onMounted(() => {
|
||||
updateMetrics(true);
|
||||
resizeObserver = new ResizeObserver(() => scheduleMetricsRefresh(true));
|
||||
if (scroller.value) resizeObserver.observe(scroller.value);
|
||||
mutationObserver = new MutationObserver(() => scheduleMetricsRefresh(true));
|
||||
if (scroller.value) mutationObserver.observe(scroller.value, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ["class", "style"] });
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
stopDrag();
|
||||
resizeObserver?.disconnect();
|
||||
mutationObserver?.disconnect();
|
||||
if (refreshFrame) cancelAnimationFrame(refreshFrame);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="virtual-scroll-area">
|
||||
<div ref="scroller" class="dolt-scroll-area-scroller" :class="props.scrollerClass" @scroll.passive="onScroll">
|
||||
<slot />
|
||||
</div>
|
||||
<div v-if="hasHorizontalOverflow" class="dolt-horizontal-scrollbar" @pointerdown="startDrag('horizontal', $event)">
|
||||
<div ref="horizontalThumb" class="dolt-horizontal-scrollbar-thumb" />
|
||||
</div>
|
||||
<div v-if="hasVerticalOverflow" class="dolt-vertical-scrollbar" :class="{ 'dolt-vertical-scrollbar-with-horizontal': hasHorizontalOverflow }" @pointerdown="startDrag('vertical', $event)">
|
||||
<div ref="verticalThumb" class="dolt-vertical-scrollbar-thumb" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.virtual-scroll-area {
|
||||
position: relative;
|
||||
display: flex;
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.dolt-scroll-area-scroller {
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
flex: 1;
|
||||
overflow: auto;
|
||||
overscroll-behavior: none;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
|
||||
.dolt-scroll-area-scroller::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.dolt-horizontal-scrollbar {
|
||||
position: relative;
|
||||
height: 10px;
|
||||
flex: 0 0 10px;
|
||||
cursor: pointer;
|
||||
touch-action: none;
|
||||
background: var(--background);
|
||||
}
|
||||
|
||||
.dolt-horizontal-scrollbar-thumb {
|
||||
position: absolute;
|
||||
top: 3px;
|
||||
height: 4px;
|
||||
min-width: 24px;
|
||||
border-radius: 999px;
|
||||
background: color-mix(in oklab, var(--foreground) 30%, transparent);
|
||||
transition:
|
||||
top 120ms ease,
|
||||
height 120ms ease,
|
||||
background-color 120ms ease;
|
||||
}
|
||||
|
||||
.dolt-horizontal-scrollbar:hover .dolt-horizontal-scrollbar-thumb,
|
||||
.dolt-horizontal-scrollbar.dolt-scrollbar-dragging .dolt-horizontal-scrollbar-thumb {
|
||||
top: 2px;
|
||||
height: 6px;
|
||||
background: color-mix(in oklab, var(--foreground) 48%, transparent);
|
||||
}
|
||||
|
||||
.dolt-vertical-scrollbar {
|
||||
position: absolute;
|
||||
top: 2px;
|
||||
right: 0;
|
||||
bottom: 2px;
|
||||
z-index: 5;
|
||||
width: 10px;
|
||||
cursor: pointer;
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
.dolt-vertical-scrollbar-with-horizontal {
|
||||
bottom: 12px;
|
||||
}
|
||||
|
||||
.dolt-vertical-scrollbar-thumb {
|
||||
position: absolute;
|
||||
right: 3px;
|
||||
width: 4px;
|
||||
min-height: 24px;
|
||||
border-radius: 999px;
|
||||
background: color-mix(in oklab, var(--foreground) 30%, transparent);
|
||||
transition:
|
||||
right 120ms ease,
|
||||
width 120ms ease,
|
||||
background-color 120ms ease;
|
||||
}
|
||||
|
||||
.dolt-vertical-scrollbar:hover .dolt-vertical-scrollbar-thumb,
|
||||
.dolt-vertical-scrollbar.dolt-scrollbar-dragging .dolt-vertical-scrollbar-thumb {
|
||||
right: 2px;
|
||||
width: 6px;
|
||||
background: color-mix(in oklab, var(--foreground) 48%, transparent);
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user