Skip to content

Commit

Permalink
Keyboard shortcut improvements (#44)
Browse files Browse the repository at this point in the history
* revert to open@8 to avoid ESM

* Ignore Cmd-1 / Ctrl-1, etc.

* support custom shortcuts in classify-images
  • Loading branch information
danvk authored Sep 22, 2024
1 parent 952113e commit e122045
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 58 deletions.
18 changes: 14 additions & 4 deletions classify-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface CLIArgs {
port?: number;
output: string;
labels: string[];
shortcuts: string[] | null;
max_width?: number;
randomOrder?: boolean;
}
Expand All @@ -44,6 +45,7 @@ program
'Path to output CSV file (default output.csv)', 'output.csv')
.option('-l, --labels <csv>',
'Comma-separated list of choices of labels', list, ['Yes', 'No'])
.option('--shortcuts <a,b,c>', 'Comma-separated list of keyboard shortcuts for labels. Default is 1, 2, etc.', list, null)
.option('-w, --max_width <pixels>',
'Make the images this width when displaying in-browser', parseInt)
.option('-r, --random-order',
Expand All @@ -56,7 +58,13 @@ if (program.args.length == 0) {
program.help(); // exits
}
const options = program.opts<CLIArgs>();
console.log(options.labels);
let {shortcuts} = options;
if (!shortcuts) {
shortcuts = options.labels.map((_, idx) => (idx + 1).toString());
} else if (shortcuts.length !== options.labels.length) {
console.error('Number of shortcuts must match number of labels');
process.exit(1);
}

if (fs.existsSync(options.output)) {
console.warn(dedent`
Expand Down Expand Up @@ -88,8 +96,8 @@ fs.closeSync(csvInfo.fd);

// Add keyboard shortcuts. 1=first button, etc.
const buttonsHtml = options.labels.map((label, idx) => {
const buttonText = `${label} (${1 + idx})`;
return `<button type="submit" data-key='${1+idx}' name="label" value="${label}">${escape(buttonText)}</button>`;
const buttonText = `${label} (${shortcuts[idx]})`;
return `<button type="submit" data-key='${shortcuts[idx]}' name="label" value="${label}">${escape(buttonText)}</button>`;
}).join('&nbsp;');

const widthHtml = options.max_width ? ` width="${options.max_width}"` : '';
Expand Down Expand Up @@ -119,6 +127,8 @@ if (options.port) {
if (options.randomOrder) {
opts.push('--random-order');
}
const args = ['localturk', ...opts, templateInfo.path, csvInfo.path, options.output];
const bin = ['localturk'];
// const bin = ['yarn', 'ts-node', 'localturk.ts'];
const args = [...bin, ...opts, templateInfo.path, csvInfo.path, options.output];
console.log('Running ', args.join(' '));
child_process.spawn(args[0], args.slice(1), {stdio: 'inherit'});
1 change: 1 addition & 0 deletions localturk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async function renderTemplate({task, numCompleted, numTotal}: TaskStats) {
// Support keyboard shortcuts via, e.g. <.. data-key="1" />
window.addEventListener("keydown", function(e) {
if (document.activeElement !== document.body) return;
if (e.altKey || e.metaKey || e.ctrlKey) return;
var key = e.key;
const el = document.querySelector('[data-key="' + key + '"]');
if (el) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"express": "^4.16.2",
"fs-extra": "^4.0.2",
"lodash": "^4.17.4",
"open": "^10.1.0",
"open": "8",
"reservoir": "^0.1.2",
"temp": "^0.8.3"
},
Expand Down
73 changes: 20 additions & 53 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,6 @@ [email protected]:
version "1.3.0"
resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.0.tgz#f351d32969d32fa5d7a5567154263d928ae3bd1f"

bundle-name@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/bundle-name/-/bundle-name-4.1.0.tgz#f3b96b34160d6431a19d7688135af7cfb8797889"
integrity sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==
dependencies:
run-applescript "^7.0.0"

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048"
Expand Down Expand Up @@ -285,23 +278,10 @@ deep-eql@^3.0.0:
dependencies:
type-detect "^4.0.0"

default-browser-id@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/default-browser-id/-/default-browser-id-5.0.0.tgz#a1d98bf960c15082d8a3fa69e83150ccccc3af26"
integrity sha512-A6p/pu/6fyBcA1TRz/GqWYPViplrftcW2gZC9q79ngNCKAeR/X3gcEdXQHl4KNXV+3wgIJ1CPkJQ3IHM6lcsyA==

default-browser@^5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/default-browser/-/default-browser-5.2.1.tgz#7b7ba61204ff3e425b556869ae6d3e9d9f1712cf"
integrity sha512-WY/3TUME0x3KPYdRRxEJJvXRHV4PyPoUsxtZa78lwItwRQRHhd2U9xOscaT/YTf8uCXIAjeJOFBVEh/7FtD8Xg==
dependencies:
bundle-name "^4.1.0"
default-browser-id "^5.0.0"

define-lazy-prop@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/define-lazy-prop/-/define-lazy-prop-3.0.0.tgz#dbb19adfb746d7fc6d734a06b72f4a00d021255f"
integrity sha512-N+MeXYoqr3pOgn8xfyRPREN7gHakLYjhsHhWGT3fWAiL4IkAt0iDw14QiiEm2bE30c5XX5q0FtAA3CK5f9/BUg==
define-lazy-prop@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/define-lazy-prop/-/define-lazy-prop-2.0.0.tgz#3f7ae421129bcaaac9bc74905c98a0009ec9ee7f"
integrity sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==

[email protected], depd@~1.1.1:
version "1.1.1"
Expand Down Expand Up @@ -473,24 +453,17 @@ [email protected]:
version "1.5.2"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.5.2.tgz#d4b505bde9946987ccf0fc58d9010ff9607e3fa0"

is-docker@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-3.0.0.tgz#90093aa3106277d8a77a5910dbae71747e15a200"
integrity sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==

is-inside-container@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-inside-container/-/is-inside-container-1.0.0.tgz#e81fba699662eb31dbdaf26766a61d4814717ea4"
integrity sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA==
dependencies:
is-docker "^3.0.0"
is-docker@^2.0.0, is-docker@^2.1.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/is-docker/-/is-docker-2.2.1.tgz#33eeabe23cfe86f14bde4408a02c0cfb853acdaa"
integrity sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==

is-wsl@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-3.1.0.tgz#e1c657e39c10090afcbedec61720f6b924c3cbd2"
integrity sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw==
is-wsl@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-2.2.0.tgz#74a4c76e77ca9fd3f932f290c17ea326cd157271"
integrity sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==
dependencies:
is-inside-container "^1.0.0"
is-docker "^2.0.0"

jsonfile@^4.0.0:
version "4.0.0"
Expand Down Expand Up @@ -583,15 +556,14 @@ once@^1.3.0:
dependencies:
wrappy "1"

open@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/open/-/open-10.1.0.tgz#a7795e6e5d519abe4286d9937bb24b51122598e1"
integrity sha512-mnkeQ1qP5Ue2wd+aivTD3NHd/lZ96Lu0jgf0pwktLPtx6cTZiH7tyeGRRHs0zX0rbrahXPnXlUnbeXyaBBuIaw==
open@8:
version "8.4.2"
resolved "https://registry.yarnpkg.com/open/-/open-8.4.2.tgz#5b5ffe2a8f793dcd2aad73e550cb87b59cb084f9"
integrity sha512-7x81NCL719oNbsq/3mh+hVrAWmFuEYUqrq/Iw3kUzH8ReypT9QQ0BLoJS7/G9k6N81XjW4qHWtjWwe/9eLy1EQ==
dependencies:
default-browser "^5.2.1"
define-lazy-prop "^3.0.0"
is-inside-container "^1.0.0"
is-wsl "^3.1.0"
define-lazy-prop "^2.0.0"
is-docker "^2.1.1"
is-wsl "^2.2.0"

os-tmpdir@^1.0.0:
version "1.0.2"
Expand Down Expand Up @@ -645,11 +617,6 @@ rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"

run-applescript@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/run-applescript/-/run-applescript-7.0.0.tgz#e5a553c2bffd620e169d276c1cd8f1b64778fbeb"
integrity sha512-9by4Ij99JUr/MCFBUkDKLWK3G9HVXmabKz9U5MlIAIuvuzkiOicRYs8XJLxX+xahD+mLiiCYDqF9dKAgtzKP1A==

[email protected]:
version "5.1.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853"
Expand Down

0 comments on commit e122045

Please sign in to comment.