diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-07 21:16:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 21:16:47 +0900 |
commit | 05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch) | |
tree | 2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/content/components/common/mark.ts | |
parent | 457d954e08923b4accd28a919c72d0b61db1bb98 (diff) | |
parent | 27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff) |
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/content/components/common/mark.ts')
-rw-r--r-- | src/content/components/common/mark.ts | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/content/components/common/mark.ts b/src/content/components/common/mark.ts new file mode 100644 index 0000000..1237385 --- /dev/null +++ b/src/content/components/common/mark.ts @@ -0,0 +1,79 @@ +import * as markActions from '../../actions/mark'; +import * as scrolls from '../..//scrolls'; +import * as consoleFrames from '../..//console-frames'; +import * as keyUtils from '../../../shared/utils/keys'; +import Mark from '../../Mark'; + +const cancelKey = (key: keyUtils.Key): boolean => { + return key.key === 'Esc' || key.key === '[' && Boolean(key.ctrlKey); +}; + +const globalKey = (key: string): boolean => { + return (/^[A-Z0-9]$/).test(key); +}; + +export default class MarkComponent { + private store: any; + + constructor(store: any) { + this.store = store; + } + + // eslint-disable-next-line max-statements + key(key: keyUtils.Key) { + let { mark: markState, setting } = this.store.getState(); + let smoothscroll = setting.properties.smoothscroll; + + if (!markState.setMode && !markState.jumpMode) { + return false; + } + + if (cancelKey(key)) { + this.store.dispatch(markActions.cancel()); + return true; + } + + if (key.ctrlKey || key.metaKey || key.altKey) { + consoleFrames.postError('Unknown mark'); + } else if (globalKey(key.key) && markState.setMode) { + this.doSetGlobal(key); + } else if (globalKey(key.key) && markState.jumpMode) { + this.doJumpGlobal(key); + } else if (markState.setMode) { + this.doSet(key); + } else if (markState.jumpMode) { + this.doJump(markState.marks, key, smoothscroll); + } + + this.store.dispatch(markActions.cancel()); + return true; + } + + doSet(key: keyUtils.Key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setLocal(key.key, x, y)); + } + + doJump( + marks: { [key: string]: Mark }, + key: keyUtils.Key, + smoothscroll: boolean, + ) { + if (!marks[key.key]) { + consoleFrames.postError('Mark is not set'); + return; + } + + let { x, y } = marks[key.key]; + scrolls.scrollTo(x, y, smoothscroll); + } + + doSetGlobal(key: keyUtils.Key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setGlobal(key.key, x, y)); + } + + doJumpGlobal(key: keyUtils.Key) { + this.store.dispatch(markActions.jumpGlobal(key.key)); + } +} |