aboutsummaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
Diffstat (limited to 'src/content')
-rw-r--r--src/content/InputDriver.ts36
-rw-r--r--src/content/client/FollowMasterClient.ts2
-rw-r--r--src/content/controllers/FollowKeyController.ts2
-rw-r--r--src/content/controllers/KeymapController.ts2
-rw-r--r--src/content/controllers/MarkKeyController.ts2
-rw-r--r--src/content/domains/Key.ts93
-rw-r--r--src/content/domains/KeySequence.ts54
-rw-r--r--src/content/repositories/KeymapRepository.ts4
-rw-r--r--src/content/usecases/FollowSlaveUseCase.ts2
-rw-r--r--src/content/usecases/KeymapUseCase.ts11
10 files changed, 46 insertions, 162 deletions
diff --git a/src/content/InputDriver.ts b/src/content/InputDriver.ts
index e77d857..bc184d2 100644
--- a/src/content/InputDriver.ts
+++ b/src/content/InputDriver.ts
@@ -1,5 +1,5 @@
import * as dom from '../shared/utils/dom';
-import Key from './domains/Key';
+import Key from '../shared/settings/Key';
const cancelKey = (e: KeyboardEvent): boolean => {
if (e.key === 'Escape') {
@@ -11,6 +11,38 @@ const cancelKey = (e: KeyboardEvent): boolean => {
return false;
};
+const modifiedKeyName = (name: string): string => {
+ if (name === ' ') {
+ return 'Space';
+ }
+ if (name.length === 1) {
+ return name;
+ } else if (name === 'Escape') {
+ return 'Esc';
+ }
+ return name;
+};
+
+// visible for testing
+export const keyFromKeyboardEvent = (e: KeyboardEvent): Key => {
+ let key = modifiedKeyName(e.key);
+ let shift = e.shiftKey;
+ if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) {
+ // make shift false for symbols to enable key bindings by symbold keys.
+ // But this limits key bindings by symbol keys with Shift
+ // (such as Shift+$>.
+ shift = false;
+ }
+
+ return new Key({
+ key: modifiedKeyName(e.key),
+ shift: shift,
+ ctrl: e.ctrlKey,
+ alt: e.altKey,
+ meta: e.metaKey,
+ });
+};
+
export default class InputDriver {
private pressed: {[key: string]: string} = {};
@@ -66,7 +98,7 @@ export default class InputDriver {
return;
}
- let key = Key.fromKeyboardEvent(e);
+ let key = keyFromKeyboardEvent(e);
for (let listener of this.onKeyListeners) {
let stop = listener(key);
if (stop) {
diff --git a/src/content/client/FollowMasterClient.ts b/src/content/client/FollowMasterClient.ts
index f79c8b0..6681e8a 100644
--- a/src/content/client/FollowMasterClient.ts
+++ b/src/content/client/FollowMasterClient.ts
@@ -1,5 +1,5 @@
import * as messages from '../../shared/messages';
-import Key from '../domains/Key';
+import Key from '../../shared/settings/Key';
export default interface FollowMasterClient {
startFollow(newTab: boolean, background: boolean): void;
diff --git a/src/content/controllers/FollowKeyController.ts b/src/content/controllers/FollowKeyController.ts
index 59d2271..0fd94ff 100644
--- a/src/content/controllers/FollowKeyController.ts
+++ b/src/content/controllers/FollowKeyController.ts
@@ -1,6 +1,6 @@
import { injectable } from 'tsyringe';
import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase';
-import Key from '../domains/Key';
+import Key from '../../shared/settings/Key';
@injectable()
export default class FollowKeyController {
diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts
index fcfaff1..6157a71 100644
--- a/src/content/controllers/KeymapController.ts
+++ b/src/content/controllers/KeymapController.ts
@@ -9,7 +9,7 @@ import ClipboardUseCase from '../usecases/ClipboardUseCase';
import OperationClient from '../client/OperationClient';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import FollowMasterClient from '../client/FollowMasterClient';
-import Key from '../domains/Key';
+import Key from '../../shared/settings/Key';
@injectable()
export default class KeymapController {
diff --git a/src/content/controllers/MarkKeyController.ts b/src/content/controllers/MarkKeyController.ts
index 886e5ff..e7653ee 100644
--- a/src/content/controllers/MarkKeyController.ts
+++ b/src/content/controllers/MarkKeyController.ts
@@ -1,7 +1,7 @@
import { injectable } from 'tsyringe';
import MarkUseCase from '../usecases/MarkUseCase';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
-import Key from '../domains/Key';
+import Key from '../../shared/settings/Key';
@injectable()
export default class MarkKeyController {
diff --git a/src/content/domains/Key.ts b/src/content/domains/Key.ts
deleted file mode 100644
index 669edfc..0000000
--- a/src/content/domains/Key.ts
+++ /dev/null
@@ -1,93 +0,0 @@
-const modifiedKeyName = (name: string): string => {
- if (name === ' ') {
- return 'Space';
- }
- if (name.length === 1) {
- return name;
- } else if (name === 'Escape') {
- return 'Esc';
- }
- return name;
-};
-
-export default class Key {
- public readonly key: string;
-
- public readonly shift: boolean;
-
- public readonly ctrl: boolean;
-
- public readonly alt: boolean;
-
- public readonly meta: boolean;
-
- constructor({ key, shift, ctrl, alt, meta }: {
- key: string;
- shift: boolean;
- ctrl: boolean;
- alt: boolean;
- meta: boolean;
- }) {
- this.key = key;
- this.shift = shift;
- this.ctrl = ctrl;
- this.alt = alt;
- this.meta = meta;
- }
-
- static fromMapKey(str: string): Key {
- if (str.startsWith('<') && str.endsWith('>')) {
- let inner = str.slice(1, -1);
- let shift = inner.includes('S-');
- let base = inner.slice(inner.lastIndexOf('-') + 1);
- if (shift && base.length === 1) {
- base = base.toUpperCase();
- } else if (!shift && base.length === 1) {
- base = base.toLowerCase();
- }
- return new Key({
- key: base,
- shift: shift,
- ctrl: inner.includes('C-'),
- alt: inner.includes('A-'),
- meta: inner.includes('M-'),
- });
- }
-
- return new Key({
- key: str,
- shift: str.toLowerCase() !== str,
- ctrl: false,
- alt: false,
- meta: false,
- });
- }
-
- static fromKeyboardEvent(e: KeyboardEvent): Key {
- let key = modifiedKeyName(e.key);
- let shift = e.shiftKey;
- if (key.length === 1 && key.toUpperCase() === key.toLowerCase()) {
- // make shift false for symbols to enable key bindings by symbold keys.
- // But this limits key bindings by symbol keys with Shift
- // (such as Shift+$>.
- shift = false;
- }
-
- return new Key({
- key: modifiedKeyName(e.key),
- shift: shift,
- ctrl: e.ctrlKey,
- alt: e.altKey,
- meta: e.metaKey,
- });
- }
-
- equals(key: Key) {
- return this.key === key.key &&
- this.ctrl === key.ctrl &&
- this.meta === key.meta &&
- this.alt === key.alt &&
- this.shift === key.shift;
- }
-}
-
diff --git a/src/content/domains/KeySequence.ts b/src/content/domains/KeySequence.ts
deleted file mode 100644
index abae61a..0000000
--- a/src/content/domains/KeySequence.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import Key from './Key';
-
-export default class KeySequence {
- constructor(
- public readonly keys: Key[],
- ) {
- }
-
- push(key: Key): number {
- return this.keys.push(key);
- }
-
- length(): number {
- return this.keys.length;
- }
-
- startsWith(o: KeySequence): boolean {
- if (this.keys.length < o.keys.length) {
- return false;
- }
- for (let i = 0; i < o.keys.length; ++i) {
- if (!this.keys[i].equals(o.keys[i])) {
- return false;
- }
- }
- return true;
- }
-
- static fromMapKeys(keys: string): KeySequence {
- const fromMapKeysRecursive = (
- remaining: string, mappedKeys: Key[],
- ): Key[] => {
- if (remaining.length === 0) {
- return mappedKeys;
- }
-
- let nextPos = 1;
- if (remaining.startsWith('<')) {
- let ltPos = remaining.indexOf('>');
- if (ltPos > 0) {
- nextPos = ltPos + 1;
- }
- }
-
- return fromMapKeysRecursive(
- remaining.slice(nextPos),
- mappedKeys.concat([Key.fromMapKey(remaining.slice(0, nextPos))])
- );
- };
-
- let data = fromMapKeysRecursive(keys, []);
- return new KeySequence(data);
- }
-}
diff --git a/src/content/repositories/KeymapRepository.ts b/src/content/repositories/KeymapRepository.ts
index 4463678..3391229 100644
--- a/src/content/repositories/KeymapRepository.ts
+++ b/src/content/repositories/KeymapRepository.ts
@@ -1,5 +1,5 @@
-import Key from '../domains/Key';
-import KeySequence from '../domains/KeySequence';
+import Key from '../../shared/settings/Key';
+import KeySequence from '../../shared/settings/KeySequence';
export default interface KeymapRepository {
enqueueKey(key: Key): KeySequence;
diff --git a/src/content/usecases/FollowSlaveUseCase.ts b/src/content/usecases/FollowSlaveUseCase.ts
index 2bd16ee..d471adb 100644
--- a/src/content/usecases/FollowSlaveUseCase.ts
+++ b/src/content/usecases/FollowSlaveUseCase.ts
@@ -4,7 +4,7 @@ import FollowPresenter from '../presenters/FollowPresenter';
import TabsClient from '../client/TabsClient';
import FollowMasterClient from '../client/FollowMasterClient';
import { LinkHint, InputHint } from '../presenters/Hint';
-import Key from '../domains/Key';
+import Key from '../../shared/settings/Key';
interface Size {
width: number;
diff --git a/src/content/usecases/KeymapUseCase.ts b/src/content/usecases/KeymapUseCase.ts
index 62cd04c..495f6d0 100644
--- a/src/content/usecases/KeymapUseCase.ts
+++ b/src/content/usecases/KeymapUseCase.ts
@@ -3,9 +3,9 @@ import KeymapRepository from '../repositories/KeymapRepository';
import SettingRepository from '../repositories/SettingRepository';
import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
import * as operations from '../../shared/operations';
-import Key from '../domains/Key';
-import KeySequence from '../domains/KeySequence';
import Keymaps from '../../shared/settings/Keymaps';
+import Key from '../../shared/settings/Key';
+import KeySequence from '../../shared/settings/KeySequence';
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
@@ -66,10 +66,9 @@ export default class KeymapUseCase {
private keymapEntityMap(): KeymapEntityMap {
let keymaps = this.settingRepository.get().keymaps.combine(reservedKeymaps);
- let entries = keymaps.entries().map(entry => [
- KeySequence.fromMapKeys(entry[0]),
- entry[1],
- ]) as [KeySequence, operations.Operation][];
+ let entries = keymaps.entries().map(
+ ([keys, op]) => [KeySequence.fromMapKeys(keys), op]
+ ) as [KeySequence, operations.Operation][];
return new Map<KeySequence, operations.Operation>(entries);
}
}