playwright/src/web/recorder/recorder.tsx

112 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-01-28 23:25:10 +01: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.
*/
import './recorder.css';
import * as React from 'react';
import { Toolbar } from '../components/toolbar';
import { ToolbarButton } from '../components/toolbarButton';
import { Source as SourceView } from '../components/source';
2021-02-13 03:53:46 +01:00
import type { CallLog, Mode, Source } from '../../server/supplements/recorder/recorderTypes';
import { SplitView } from '../components/splitView';
import { CallLogView } from './callLog';
declare global {
interface Window {
playwrightSetFile: (file: string) => void;
}
}
2021-01-28 23:25:10 +01:00
export interface RecorderProps {
sources: Source[],
paused: boolean,
log: Map<number, CallLog>,
mode: Mode
2021-01-28 23:25:10 +01:00
}
export const Recorder: React.FC<RecorderProps> = ({
sources,
paused,
log,
mode
2021-01-28 23:25:10 +01:00
}) => {
const [f, setFile] = React.useState<string | undefined>();
window.playwrightSetFile = setFile;
const file = f || sources[0]?.file;
const source = sources.find(s => s.file === file) || {
text: '',
language: 'javascript',
file: '',
highlight: []
};
2021-02-13 03:53:46 +01:00
const messagesEndRef = React.createRef<HTMLDivElement>();
React.useLayoutEffect(() => {
messagesEndRef.current?.scrollIntoView({ block: 'center', inline: 'nearest' });
}, [messagesEndRef]);
return <div className='recorder'>
2021-01-28 23:25:10 +01:00
<Toolbar>
2021-02-13 03:53:46 +01:00
<ToolbarButton icon='record' title='Record' toggled={mode == 'recording'} onClick={() => {
window.dispatch({ event: 'setMode', params: { mode: mode === 'recording' ? 'none' : 'recording' }}).catch(() => { });
}}></ToolbarButton>
2021-02-13 03:53:46 +01:00
<ToolbarButton icon='question' title='Inspect' toggled={mode == 'inspecting'} onClick={() => {
window.dispatch({ event: 'setMode', params: { mode: mode === 'inspecting' ? 'none' : 'inspecting' }}).catch(() => { });
}}></ToolbarButton>
<ToolbarButton icon='files' title='Copy' disabled={!source || !source.text} onClick={() => {
copy(source.text);
}}></ToolbarButton>
2021-02-13 03:53:46 +01:00
<ToolbarButton icon='debug-continue' title='Resume' disabled={!paused} onClick={() => {
window.dispatch({ event: 'resume' }).catch(() => {});
}}></ToolbarButton>
2021-02-13 03:53:46 +01:00
<ToolbarButton icon='debug-pause' title='Pause' disabled={paused} onClick={() => {
window.dispatch({ event: 'pause' }).catch(() => {});
}}></ToolbarButton>
2021-02-13 03:53:46 +01:00
<ToolbarButton icon='debug-step-over' title='Step over' disabled={!paused} onClick={() => {
window.dispatch({ event: 'step' }).catch(() => {});
}}></ToolbarButton>
<select className='recorder-chooser' hidden={!sources.length} onChange={event => {
setFile(event.target.selectedOptions[0].value);
}}>{
sources.map(s => {
const title = s.file.replace(/.*[/\\]([^/\\]+)/, '$1');
return <option key={s.file} value={s.file} selected={s.file === file}>{title}</option>;
})
}
</select>
2021-02-13 03:53:46 +01:00
<div style={{flex: 'auto'}}></div>
<ToolbarButton icon='clear-all' title='Clear' disabled={!source || !source.text} onClick={() => {
window.dispatch({ event: 'clear' }).catch(() => {});
}}></ToolbarButton>
2021-01-28 23:25:10 +01:00
</Toolbar>
2021-02-13 03:53:46 +01:00
<SplitView sidebarSize={200}>
<SourceView text={source.text} language={source.language} highlight={source.highlight} revealLine={source.revealLine}></SourceView>
<CallLogView log={[...log.values()]}/>
2021-02-13 03:53:46 +01:00
</SplitView>
2021-01-28 23:25:10 +01:00
</div>;
};
function copy(text: string) {
const textArea = document.createElement('textarea');
textArea.style.position = 'absolute';
textArea.style.zIndex = '-1000';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
textArea.remove();
}