/* Copyright (c) Microsoft Corporation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import type { TestAttachment } from '@playwright-test/reporters/html'; import * as React from 'react'; import { AttachmentLink } from './links'; import type { TabbedPaneTab } from './tabbedPane'; import { TabbedPane } from './tabbedPane'; import './imageDiffView.css'; import './tabbedPane.css'; export type ImageDiff = { name: string, expected?: { attachment: TestAttachment, title: string }, actual?: { attachment: TestAttachment }, diff?: { attachment: TestAttachment }, }; export const ImageDiffView: React.FunctionComponent<{ imageDiff: ImageDiff, }> = ({ imageDiff: diff }) => { // Pre-select a tab called "actual", if any. const [selectedTab, setSelectedTab] = React.useState('actual'); const diffElement = React.useRef(null); const imageElement = React.useRef(null); const [sliderPosition, setSliderPosition] = React.useState(0); const onImageLoaded = (side?: 'left' | 'right') => { if (diffElement.current) diffElement.current.style.minHeight = diffElement.current.offsetHeight + 'px'; if (side && diffElement.current && imageElement.current) { const gap = Math.max(0, (diffElement.current.offsetWidth - imageElement.current.offsetWidth) / 2 - 20); if (side === 'left') setSliderPosition(gap); else if (side === 'right') setSliderPosition(diffElement.current.offsetWidth - gap); } }; const tabs: TabbedPaneTab[] = []; if (diff.diff) { tabs.push({ id: 'actual', title: 'Actual', render: () => onImageLoaded('right')} ref={imageElement} /> , }); tabs.push({ id: 'expected', title: diff.expected!.title, render: () => onImageLoaded('left')} ref={imageElement} /> , }); } else { tabs.push({ id: 'actual', title: 'Actual', render: () => onImageLoaded()} /> }); tabs.push({ id: 'expected', title: diff.expected!.title, render: () => onImageLoaded()} /> }); } if (diff.diff) { tabs.push({ id: 'diff', title: 'Diff', render: () => onImageLoaded()} /> }); } return
{diff.diff && }
; }; export const ImageDiffSlider: React.FC void, }>> = ({ children, sliderPosition, setSliderPosition }) => { const [resizing, setResizing] = React.useState<{ offset: number, size: number } | null>(null); const size = sliderPosition; const childrenArray = React.Children.toArray(children); document.body.style.userSelect = resizing ? 'none' : 'inherit'; const gripStyle: React.CSSProperties = { ...absolute, zIndex: 100, cursor: 'ew-resize', left: resizing ? 0 : size - 4, right: resizing ? 0 : undefined, width: resizing ? 'initial' : 8, }; return <> {childrenArray[0]}
{childrenArray[1]}
setResizing({ offset: event.clientX, size })} onMouseUp={() => setResizing(null)} onMouseMove={event => { if (!event.buttons) { setResizing(null); } else if (resizing) { const offset = event.clientX; const delta = offset - resizing.offset; const newSize = resizing.size + delta; const splitView = (event.target as HTMLElement).parentElement!; const rect = splitView.getBoundingClientRect(); const size = Math.min(Math.max(0, newSize), rect.width); setSliderPosition(size); } }} >
; }; const absolute: React.CSSProperties = { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, };