diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-23 07:25:41 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-23 07:29:04 -0700 |
commit | d50ac287e7cc00793657422dbfa36d9c71e659be (patch) | |
tree | 8d237fde461b8b50cfb0d6ff602f764e9fa46fd1 /web/static/dartboat.js | |
parent | 63d17a12c2a894944ab3760e9b5f3dbf6acb95b5 (diff) | |
download | dartboat-d50ac287e7cc00793657422dbfa36d9c71e659be.tar.gz dartboat-d50ac287e7cc00793657422dbfa36d9c71e659be.tar.xz |
web: add undo ability; simplify prompt handlers
Undo currently works only for ongoing matches.
Diffstat (limited to 'web/static/dartboat.js')
-rw-r--r-- | web/static/dartboat.js | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js index 07c8997..6a0d07b 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -32,7 +32,7 @@ function _promptGet() { function _promptGetAndClear() { let val = _promptGet(); - promptClear(); + promptHandle('clear'); return val; } @@ -78,6 +78,19 @@ let prompt_handlers = { Module.ccall('user_visit_to_rem', 'number', ['number'], [rem]); }, + undo() { + let cl = document.getElementById('key_undo').classList; + if (cl.contains('active')) + Module.ccall('user_undo'); + cl.toggle('active'); + }, + + pre(action) { + clearOi(); + if (action != 'undo') + document.getElementById('key_undo').classList.remove('active'); + }, + _update_rem(val) { Module.ccall('update_user_rem_from_pts', null, ['number'], [val || _promptGet()]); @@ -103,12 +116,20 @@ let prompt_handlers = { let n = _promptGetAndClear(); if (n) Module.ccall('resp_numdarts', null, ['number'], [n]); + }, + + pre(action) { + clearOi(); } }, init: { submit() { Module.ccall('start_match'); + }, + + pre(action) { + clearOi(); } } } @@ -117,29 +138,9 @@ function setPromptHandler(ptr) { prompt_handler = prompt_handlers[UTF8ToString(ptr)]; } -function promptAppend(...args) { - clearOi(); - prompt_handler?.append?.(...args); -} - -function promptClear(...args) { - clearOi(); - prompt_handler?.clear?.(...args); -} - -function promptBackspace(...args) { - clearOi(); - prompt_handler?.backspace?.(...args); -} - -function promptSubmit(...args) { - clearOi(); - prompt_handler?.submit?.(...args); -} - -function promptSubmitRem(...args) { - clearOi(); - prompt_handler?.submit_rem?.(...args); +function promptHandle(action, ...args) { + prompt_handler?.pre?.(action); + prompt_handler?.[action]?.(...args); } function setPromptInput(ptr) { @@ -239,11 +240,13 @@ document.addEventListener('keydown', e => { return; if (isFinite(e.key)) - promptAppend(e.key); + promptHandle('append', e.key); else if (e.key == 'Enter') - promptSubmit(); + promptHandle('submit'); else if (e.key == 'Backspace') - promptBackspace(); + promptHandle('backspace'); else if (e.key == 'r') - promptSubmitRem(); + promptHandle('submit_rem'); + else if (e.key == 'u') + promptHandle('undo'); }); |