diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-10-13 22:15:16 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-13 22:15:16 +0900 |
commit | 8b72aac09af476e19da7e482e43769d47d1969b2 (patch) | |
tree | 7b5628784afc557e3c887e32c36e5bd49bac90d5 /src/content/components/common/mark.js | |
parent | b09a4d1bae85eea537d80a5077cdd17d849cfaa5 (diff) | |
parent | 3c40b74a3e8d87ba310b46e24d6465d48766e3e8 (diff) |
Merge pull request #486 from ueokande/jump-marks
Support jump marks
Diffstat (limited to 'src/content/components/common/mark.js')
-rw-r--r-- | src/content/components/common/mark.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/content/components/common/mark.js b/src/content/components/common/mark.js new file mode 100644 index 0000000..1ed636b --- /dev/null +++ b/src/content/components/common/mark.js @@ -0,0 +1,74 @@ +import * as markActions from 'content/actions/mark'; +import * as scrolls from 'content/scrolls'; +import * as consoleFrames from 'content/console-frames'; +import * as properties from 'shared/settings/properties'; + +const cancelKey = (key) => { + return key.key === 'Esc' || key.key === '[' && key.ctrlKey; +}; + +const globalKey = (key) => { + return (/^[A-Z0-9]$/).test(key); +}; + +export default class MarkComponent { + constructor(body, store) { + this.body = body; + this.store = store; + } + + // eslint-disable-next-line max-statements + key(key) { + let { mark: markStage, setting } = this.store.getState(); + let smoothscroll = setting.properties.smoothscroll || + properties.defaults.smoothscroll; + + if (!markStage.setMode && !markStage.jumpMode) { + return false; + } + + if (cancelKey(key)) { + this.store.dispatch(markActions.cancel()); + return true; + } + + if (key.ctrlKey || key.metaKey || key.altKey) { + consoleFrames.postError(window.document, 'Unknown mark'); + } else if (globalKey(key.key) && markStage.setMode) { + this.doSetGlobal(key); + } else if (globalKey(key.key) && markStage.jumpMode) { + this.doJumpGlobal(key); + } else if (markStage.setMode) { + this.doSet(key); + } else if (markStage.jumpMode) { + this.doJump(markStage.marks, key, smoothscroll); + } + + this.store.dispatch(markActions.cancel()); + return true; + } + + doSet(key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setLocal(key.key, x, y)); + } + + doJump(marks, key, smoothscroll) { + if (!marks[key.key]) { + consoleFrames.postError(window.document, 'Mark is not set'); + return; + } + + let { x, y } = marks[key.key]; + scrolls.scrollTo(x, y, smoothscroll); + } + + doSetGlobal(key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setGlobal(key.key, x, y)); + } + + doJumpGlobal(key) { + this.store.dispatch(markActions.jumpGlobal(key.key)); + } +} |