2021-02-17 17:51:57 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-02-24 13:39:51 -08:00
|
|
|
import { ActionEntry } from '../../../server/trace/viewer/traceModel';
|
2021-04-08 05:32:12 +08:00
|
|
|
import { Size } from '../geometry';
|
2021-02-17 17:51:57 -08:00
|
|
|
import './snapshotTab.css';
|
|
|
|
import * as React from 'react';
|
2021-02-17 22:10:13 -08:00
|
|
|
import { useMeasure } from './helpers';
|
2021-03-10 11:43:26 -08:00
|
|
|
import type { Point } from '../../../common/types';
|
2021-02-17 17:51:57 -08:00
|
|
|
|
|
|
|
export const SnapshotTab: React.FunctionComponent<{
|
|
|
|
actionEntry: ActionEntry | undefined,
|
|
|
|
snapshotSize: Size,
|
2021-04-08 05:32:12 +08:00
|
|
|
}> = ({ actionEntry, snapshotSize }) => {
|
2021-02-17 17:51:57 -08:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
|
|
|
const [snapshotIndex, setSnapshotIndex] = React.useState(0);
|
|
|
|
|
2021-03-10 11:43:26 -08:00
|
|
|
const snapshots = actionEntry ? (actionEntry.snapshots || []) : [];
|
2021-02-17 17:51:57 -08:00
|
|
|
|
|
|
|
const iframeRef = React.createRef<HTMLIFrameElement>();
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!iframeRef.current)
|
|
|
|
return;
|
2021-03-08 19:49:57 -08:00
|
|
|
let snapshotUri = undefined;
|
2021-03-10 11:43:26 -08:00
|
|
|
let point: Point | undefined = undefined;
|
2021-04-08 05:32:12 +08:00
|
|
|
if (actionEntry) {
|
2021-02-17 17:51:57 -08:00
|
|
|
const snapshot = snapshots[snapshotIndex];
|
2021-03-10 11:43:26 -08:00
|
|
|
if (snapshot && snapshot.snapshotName) {
|
|
|
|
snapshotUri = `${actionEntry.metadata.pageId}?name=${snapshot.snapshotName}`;
|
|
|
|
if (snapshot.snapshotName.includes('action'))
|
|
|
|
point = actionEntry.metadata.point;
|
|
|
|
}
|
2021-02-17 17:51:57 -08:00
|
|
|
}
|
2021-03-11 11:22:59 -08:00
|
|
|
const snapshotUrl = snapshotUri ? `${window.location.origin}/snapshot/${snapshotUri}` : 'data:text/html,<body style="background: #ddd"></body>';
|
2021-02-17 17:51:57 -08:00
|
|
|
try {
|
2021-03-10 11:43:26 -08:00
|
|
|
(iframeRef.current.contentWindow as any).showSnapshot(snapshotUrl, { point });
|
2021-02-17 17:51:57 -08:00
|
|
|
} catch (e) {
|
|
|
|
}
|
2021-04-08 05:32:12 +08:00
|
|
|
}, [actionEntry, snapshotIndex]);
|
2021-02-17 17:51:57 -08:00
|
|
|
|
|
|
|
const scale = Math.min(measure.width / snapshotSize.width, measure.height / snapshotSize.height);
|
2021-03-11 11:22:59 -08:00
|
|
|
const scaledSize = {
|
|
|
|
width: snapshotSize.width * scale,
|
|
|
|
height: snapshotSize.height * scale,
|
|
|
|
};
|
2021-04-19 11:09:50 -10:00
|
|
|
return <div
|
|
|
|
className='snapshot-tab'
|
|
|
|
tabIndex={0}
|
|
|
|
onKeyDown={event => {
|
|
|
|
if (event.key === 'ArrowDown')
|
|
|
|
setSnapshotIndex(Math.min(snapshotIndex + 1, snapshots.length - 1));
|
|
|
|
if (event.key === 'ArrowUp')
|
|
|
|
setSnapshotIndex(Math.max(snapshotIndex - 1, 0));
|
|
|
|
}}
|
|
|
|
><div className='snapshot-controls'>
|
2021-04-08 05:32:12 +08:00
|
|
|
{snapshots.map((snapshot, index) => {
|
2021-02-17 17:51:57 -08:00
|
|
|
return <div
|
2021-03-08 19:49:57 -08:00
|
|
|
key={snapshot.title}
|
2021-02-17 17:51:57 -08:00
|
|
|
className={'snapshot-toggle' + (snapshotIndex === index ? ' toggled' : '')}
|
|
|
|
onClick={() => setSnapshotIndex(index)}>
|
2021-03-08 19:49:57 -08:00
|
|
|
{snapshot.title}
|
2021-02-17 17:51:57 -08:00
|
|
|
</div>
|
|
|
|
})
|
|
|
|
}</div>
|
|
|
|
<div ref={ref} className='snapshot-wrapper'>
|
|
|
|
<div className='snapshot-container' style={{
|
|
|
|
width: snapshotSize.width + 'px',
|
|
|
|
height: snapshotSize.height + 'px',
|
2021-03-11 11:22:59 -08:00
|
|
|
transform: `translate(${-snapshotSize.width * (1 - scale) / 2 + (measure.width - scaledSize.width) / 2}px, ${-snapshotSize.height * (1 - scale) / 2 + (measure.height - scaledSize.height) / 2}px) scale(${scale})`,
|
2021-02-17 17:51:57 -08:00
|
|
|
}}>
|
|
|
|
<iframe ref={iframeRef} id='snapshot' name='snapshot' src='/snapshot/'></iframe>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
};
|