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';
|
2021-02-12 19:11:30 +01:00
|
|
|
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';
|
2021-02-17 23:05:41 +01:00
|
|
|
import { CallLogView } from './callLog';
|
2021-02-05 23:24:27 +01:00
|
|
|
|
2021-02-01 01:37:13 +01:00
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
2021-02-17 23:05:41 +01:00
|
|
|
playwrightSetFile: (file: string) => void;
|
2021-02-19 16:25:08 +01:00
|
|
|
playwrightSetSelector: (selector: string, focus?: boolean) => void;
|
|
|
|
|
dispatch(data: any): Promise<void>;
|
2021-02-01 01:37:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-28 23:25:10 +01:00
|
|
|
export interface RecorderProps {
|
2021-02-17 23:05:41 +01:00
|
|
|
sources: Source[],
|
|
|
|
|
paused: boolean,
|
2021-04-23 18:28:18 +02:00
|
|
|
log: Map<string, CallLog>,
|
2021-02-19 16:25:08 +01:00
|
|
|
mode: Mode,
|
|
|
|
|
initialSelector?: string,
|
2021-01-28 23:25:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Recorder: React.FC<RecorderProps> = ({
|
2021-02-17 23:05:41 +01:00
|
|
|
sources,
|
|
|
|
|
paused,
|
|
|
|
|
log,
|
2021-02-19 16:25:08 +01:00
|
|
|
mode,
|
|
|
|
|
initialSelector,
|
2021-01-28 23:25:10 +01:00
|
|
|
}) => {
|
2021-02-19 16:25:08 +01:00
|
|
|
const [selector, setSelector] = React.useState(initialSelector || '');
|
|
|
|
|
const [focusSelectorInput, setFocusSelectorInput] = React.useState(false);
|
|
|
|
|
window.playwrightSetSelector = (selector: string, focus?: boolean) => {
|
|
|
|
|
setSelector(selector);
|
|
|
|
|
setFocusSelectorInput(!!focus);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-17 23:05:41 +01:00
|
|
|
const [f, setFile] = React.useState<string | undefined>();
|
|
|
|
|
window.playwrightSetFile = setFile;
|
|
|
|
|
const file = f || sources[0]?.file;
|
2021-02-05 23:24:27 +01:00
|
|
|
|
2021-02-17 23:05:41 +01:00
|
|
|
const source = sources.find(s => s.file === file) || {
|
2021-02-17 03:13:26 +01:00
|
|
|
text: '',
|
|
|
|
|
language: 'javascript',
|
2021-02-17 23:05:41 +01:00
|
|
|
file: '',
|
2021-02-17 03:13:26 +01:00
|
|
|
highlight: []
|
|
|
|
|
};
|
2021-02-13 03:53:46 +01:00
|
|
|
|
|
|
|
|
const messagesEndRef = React.createRef<HTMLDivElement>();
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ block: 'center', inline: 'nearest' });
|
|
|
|
|
}, [messagesEndRef]);
|
2021-02-19 16:25:08 +01:00
|
|
|
|
|
|
|
|
const selectorInputRef = React.createRef<HTMLInputElement>();
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
|
if (focusSelectorInput && selectorInputRef.current) {
|
|
|
|
|
selectorInputRef.current.select();
|
|
|
|
|
selectorInputRef.current.focus();
|
|
|
|
|
setFocusSelectorInput(false);
|
|
|
|
|
}
|
|
|
|
|
}, [focusSelectorInput, selectorInputRef]);
|
|
|
|
|
|
2021-02-13 03:53:46 +01:00
|
|
|
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={() => {
|
2021-03-01 21:20:04 +01:00
|
|
|
window.dispatch({ event: 'setMode', params: { mode: mode === 'recording' ? 'none' : 'recording' }});
|
2021-02-18 02:28:02 +01:00
|
|
|
}}>Record</ToolbarButton>
|
2021-02-17 23:05:41 +01:00
|
|
|
<ToolbarButton icon='files' title='Copy' disabled={!source || !source.text} onClick={() => {
|
2021-02-05 23:24:27 +01:00
|
|
|
copy(source.text);
|
2021-02-01 01:37:13 +01:00
|
|
|
}}></ToolbarButton>
|
2021-02-13 03:53:46 +01:00
|
|
|
<ToolbarButton icon='debug-continue' title='Resume' disabled={!paused} onClick={() => {
|
2021-03-01 21:20:04 +01:00
|
|
|
window.dispatch({ event: 'resume' });
|
2021-02-12 19:11:30 +01:00
|
|
|
}}></ToolbarButton>
|
2021-02-13 03:53:46 +01:00
|
|
|
<ToolbarButton icon='debug-pause' title='Pause' disabled={paused} onClick={() => {
|
2021-03-01 21:20:04 +01:00
|
|
|
window.dispatch({ event: 'pause' });
|
2021-02-12 19:11:30 +01:00
|
|
|
}}></ToolbarButton>
|
2021-02-13 03:53:46 +01:00
|
|
|
<ToolbarButton icon='debug-step-over' title='Step over' disabled={!paused} onClick={() => {
|
2021-03-01 21:20:04 +01:00
|
|
|
window.dispatch({ event: 'step' });
|
2021-02-12 19:11:30 +01:00
|
|
|
}}></ToolbarButton>
|
2021-02-18 02:28:02 +01:00
|
|
|
<select className='recorder-chooser' hidden={!sources.length} value={file} onChange={event => {
|
2021-02-17 23:05:41 +01:00
|
|
|
setFile(event.target.selectedOptions[0].value);
|
|
|
|
|
}}>{
|
|
|
|
|
sources.map(s => {
|
|
|
|
|
const title = s.file.replace(/.*[/\\]([^/\\]+)/, '$1');
|
2021-02-18 02:28:02 +01:00
|
|
|
return <option key={s.file} value={s.file}>{title}</option>;
|
2021-02-17 23:05:41 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
</select>
|
2021-02-13 03:53:46 +01:00
|
|
|
<div style={{flex: 'auto'}}></div>
|
2021-02-17 23:05:41 +01:00
|
|
|
<ToolbarButton icon='clear-all' title='Clear' disabled={!source || !source.text} onClick={() => {
|
2021-03-01 21:20:04 +01:00
|
|
|
window.dispatch({ event: 'clear' });
|
2021-02-05 23:24:27 +01:00
|
|
|
}}></ToolbarButton>
|
2021-01-28 23:25:10 +01:00
|
|
|
</Toolbar>
|
2021-02-23 00:35:38 +01:00
|
|
|
<SplitView sidebarSize={200} sidebarHidden={mode === 'recording'}>
|
2021-02-13 03:53:46 +01:00
|
|
|
<SourceView text={source.text} language={source.language} highlight={source.highlight} revealLine={source.revealLine}></SourceView>
|
2021-02-19 16:25:08 +01:00
|
|
|
<div className='vbox'>
|
|
|
|
|
<Toolbar>
|
2021-02-23 00:35:38 +01:00
|
|
|
<ToolbarButton icon='microscope' title='Explore' toggled={mode == 'inspecting'} onClick={() => {
|
2021-02-19 16:25:08 +01:00
|
|
|
window.dispatch({ event: 'setMode', params: { mode: mode === 'inspecting' ? 'none' : 'inspecting' }}).catch(() => { });
|
|
|
|
|
}}>Explore</ToolbarButton>
|
|
|
|
|
<input ref={selectorInputRef} className='selector-input' placeholder='Playwright Selector' value={selector} disabled={mode !== 'none'} onChange={event => {
|
|
|
|
|
setSelector(event.target.value);
|
|
|
|
|
window.dispatch({ event: 'selectorUpdated', params: { selector: event.target.value } });
|
|
|
|
|
}} />
|
|
|
|
|
</Toolbar>
|
2021-06-04 19:47:19 +02:00
|
|
|
<CallLogView log={Array.from(log.values())}/>
|
2021-02-19 16:25:08 +01:00
|
|
|
</div>
|
2021-02-13 03:53:46 +01:00
|
|
|
</SplitView>
|
2021-01-28 23:25:10 +01:00
|
|
|
</div>;
|
|
|
|
|
};
|
2021-02-01 01:37:13 +01:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|