Skip to content

Commit 1239306

Browse files
committed
Paste selection for firefox. Cleanup.
1 parent 4419673 commit 1239306

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

webdriver-ts-results/src/Common.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ export class ResultTableData {
335335
}
336336

337337
computeFactors(benchmark: Benchmark): Array<TableResultValueEntry|null> {
338-
debugger;
339338
const benchmarkResults = this.frameworksForFactors.map(f => this.results(benchmark, f));
340339
const selectFn = (result: Result|null) => {
341340
if (result===null) return 0;

webdriver-ts-results/src/selection/CopyPasteSelection.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as React from 'react';
2+
import { useCallback } from 'react';
3+
import { useEffect } from 'react';
24
import { useDispatch, useSelector } from 'react-redux';
35
import { categories } from '../Common';
46
import { setStateFromClipboard, State } from '../reducer';
@@ -9,6 +11,28 @@ const CopyPasteSelection = (): JSX.Element =>
911
const dispatch = useDispatch();
1012
const state = useSelector<State, State>(state => state);
1113

14+
const performPaste = useCallback((text: string) => {
15+
try {
16+
const jsonState = JSON.parse(text);
17+
dispatch(setStateFromClipboard(jsonState));
18+
} catch (e) {
19+
alert("Sorry - couldn't parse pasted selection");
20+
console.log("pasting state failed", e);
21+
}
22+
}, [dispatch]);
23+
24+
useEffect(() => {
25+
const eh = (e: ClipboardEvent) => {
26+
e.preventDefault();
27+
const text = e.clipboardData?.getData('text/plain');
28+
if (text) performPaste(text);
29+
};
30+
document.addEventListener("paste", eh);
31+
return () => {
32+
document.removeEventListener("paste", eh);
33+
}
34+
}, [performPaste]);
35+
1236
const copy = () => {
1337
const serializedState = {
1438
frameworks: state.frameworks.filter(f => state.selectedFrameworksDropDown.has(f)).map(f => f.dir),
@@ -20,26 +44,25 @@ const CopyPasteSelection = (): JSX.Element =>
2044
navigator.clipboard.writeText(json);
2145
window.location.hash = btoa(json);
2246
};
23-
const paste = async () => {
47+
const paste = useCallback(async () => {
2448
try {
2549
const text = await navigator.clipboard.readText();
26-
const jsonState = JSON.parse(text);
27-
dispatch(setStateFromClipboard(jsonState));
50+
performPaste(text);
2851
} catch (e) {
2952
alert("Sorry - couldn't parse pasted selection");
3053
console.log("pasting state failed", e);
3154
}
32-
};
55+
}, [performPaste]);
3356
return <>
3457
<p>Copy/paste current selection</p>
3558
<div className="hspan" />
3659
<button className='iconbutton' onClick={copy}>
3760
{/* svg from https://ionic.io/ionicons */}
38-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><title>Copy</title><rect x="128" y="128" width="336" height="336" rx="57" ry="57" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/><path d="M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24" fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="32"/></svg>
61+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><title>Copy selected frameworks and benchmarks</title><rect x="128" y="128" width="336" height="336" rx="57" ry="57" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/><path d="M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24" fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="32"/></svg>
3962
</button>
4063
<button className='iconbutton' onClick={paste}>
4164
{/* svg from https://ionic.io/ionicons */}
42-
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><title>Clipboard</title><path d="M336 64h32a48 48 0 0148 48v320a48 48 0 01-48 48H144a48 48 0 01-48-48V112a48 48 0 0148-48h32" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/><rect x="176" y="32" width="160" height="64" rx="26.13" ry="26.13" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/></svg>
65+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><title>Paste selected items (or use ctrl/cmd + v for firefox)</title><path d="M336 64h32a48 48 0 0148 48v320a48 48 0 01-48 48H144a48 48 0 01-48-48V112a48 48 0 0148-48h32" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/><rect x="176" y="32" width="160" height="64" rx="26.13" ry="26.13" fill="none" stroke="currentColor" strokeLinejoin="round" strokeWidth="32"/></svg>
4366
</button>
4467
</>
4568
}

0 commit comments

Comments
 (0)