Move Key to settings

jh-changes
Shin'ya UEOKA 5 years ago
parent 8428671a0a
commit da3ce77aa0
  1. 36
      src/content/InputDriver.ts
  2. 2
      src/content/client/FollowMasterClient.ts
  3. 2
      src/content/controllers/FollowKeyController.ts
  4. 2
      src/content/controllers/KeymapController.ts
  5. 2
      src/content/controllers/MarkKeyController.ts
  6. 4
      src/content/repositories/KeymapRepository.ts
  7. 2
      src/content/usecases/FollowSlaveUseCase.ts
  8. 11
      src/content/usecases/KeymapUseCase.ts
  9. 40
      src/shared/settings/Key.ts
  10. 2
      src/shared/settings/KeySequence.ts
  11. 51
      test/content/InputDriver.test.ts
  12. 2
      test/content/repositories/KeymapRepository.test.ts
  13. 51
      test/shared/settings/Key.test.ts
  14. 4
      test/shared/settings/KeySequence.test.ts

@ -1,5 +1,5 @@
import * as dom from '../shared/utils/dom'; import * as dom from '../shared/utils/dom';
import Key from './domains/Key'; import Key from '../shared/settings/Key';
const cancelKey = (e: KeyboardEvent): boolean => { const cancelKey = (e: KeyboardEvent): boolean => {
if (e.key === 'Escape') { if (e.key === 'Escape') {
@ -11,6 +11,38 @@ const cancelKey = (e: KeyboardEvent): boolean => {
return false; 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 { export default class InputDriver {
private pressed: {[key: string]: string} = {}; private pressed: {[key: string]: string} = {};
@ -66,7 +98,7 @@ export default class InputDriver {
return; return;
} }
let key = Key.fromKeyboardEvent(e); let key = keyFromKeyboardEvent(e);
for (let listener of this.onKeyListeners) { for (let listener of this.onKeyListeners) {
let stop = listener(key); let stop = listener(key);
if (stop) { if (stop) {

@ -1,5 +1,5 @@
import * as messages from '../../shared/messages'; import * as messages from '../../shared/messages';
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
export default interface FollowMasterClient { export default interface FollowMasterClient {
startFollow(newTab: boolean, background: boolean): void; startFollow(newTab: boolean, background: boolean): void;

@ -1,6 +1,6 @@
import { injectable } from 'tsyringe'; import { injectable } from 'tsyringe';
import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase'; import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase';
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
@injectable() @injectable()
export default class FollowKeyController { export default class FollowKeyController {

@ -9,7 +9,7 @@ import ClipboardUseCase from '../usecases/ClipboardUseCase';
import OperationClient from '../client/OperationClient'; import OperationClient from '../client/OperationClient';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import FollowMasterClient from '../client/FollowMasterClient'; import FollowMasterClient from '../client/FollowMasterClient';
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
@injectable() @injectable()
export default class KeymapController { export default class KeymapController {

@ -1,7 +1,7 @@
import { injectable } from 'tsyringe'; import { injectable } from 'tsyringe';
import MarkUseCase from '../usecases/MarkUseCase'; import MarkUseCase from '../usecases/MarkUseCase';
import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase';
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
@injectable() @injectable()
export default class MarkKeyController { export default class MarkKeyController {

@ -1,5 +1,5 @@
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
import KeySequence from '../domains/KeySequence'; import KeySequence from '../../shared/settings/KeySequence';
export default interface KeymapRepository { export default interface KeymapRepository {
enqueueKey(key: Key): KeySequence; enqueueKey(key: Key): KeySequence;

@ -4,7 +4,7 @@ import FollowPresenter from '../presenters/FollowPresenter';
import TabsClient from '../client/TabsClient'; import TabsClient from '../client/TabsClient';
import FollowMasterClient from '../client/FollowMasterClient'; import FollowMasterClient from '../client/FollowMasterClient';
import { LinkHint, InputHint } from '../presenters/Hint'; import { LinkHint, InputHint } from '../presenters/Hint';
import Key from '../domains/Key'; import Key from '../../shared/settings/Key';
interface Size { interface Size {
width: number; width: number;

@ -3,9 +3,9 @@ import KeymapRepository from '../repositories/KeymapRepository';
import SettingRepository from '../repositories/SettingRepository'; import SettingRepository from '../repositories/SettingRepository';
import AddonEnabledRepository from '../repositories/AddonEnabledRepository'; import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
import * as operations from '../../shared/operations'; import * as operations from '../../shared/operations';
import Key from '../domains/Key';
import KeySequence from '../domains/KeySequence';
import Keymaps from '../../shared/settings/Keymaps'; import Keymaps from '../../shared/settings/Keymaps';
import Key from '../../shared/settings/Key';
import KeySequence from '../../shared/settings/KeySequence';
type KeymapEntityMap = Map<KeySequence, operations.Operation>; type KeymapEntityMap = Map<KeySequence, operations.Operation>;
@ -66,10 +66,9 @@ export default class KeymapUseCase {
private keymapEntityMap(): KeymapEntityMap { private keymapEntityMap(): KeymapEntityMap {
let keymaps = this.settingRepository.get().keymaps.combine(reservedKeymaps); let keymaps = this.settingRepository.get().keymaps.combine(reservedKeymaps);
let entries = keymaps.entries().map(entry => [ let entries = keymaps.entries().map(
KeySequence.fromMapKeys(entry[0]), ([keys, op]) => [KeySequence.fromMapKeys(keys), op]
entry[1], ) as [KeySequence, operations.Operation][];
]) as [KeySequence, operations.Operation][];
return new Map<KeySequence, operations.Operation>(entries); return new Map<KeySequence, operations.Operation>(entries);
} }
} }

@ -1,15 +1,3 @@
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 { export default class Key {
public readonly key: string; public readonly key: string;
@ -63,31 +51,11 @@ export default class Key {
}); });
} }
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) { equals(key: Key) {
return this.key === key.key && return this.key === key.key &&
this.ctrl === key.ctrl && this.ctrl === key.ctrl &&
this.meta === key.meta && this.meta === key.meta &&
this.alt === key.alt && this.alt === key.alt &&
this.shift === key.shift; this.shift === key.shift;
} }
} }

@ -1,4 +1,4 @@
import Key from './Key'; import Key from '../../shared/settings/Key';
export default class KeySequence { export default class KeySequence {
constructor( constructor(

@ -1,6 +1,6 @@
import InputDriver from '../../src/content/InputDriver'; import InputDriver, {keyFromKeyboardEvent} from '../../src/content/InputDriver';
import { expect } from 'chai'; import { expect } from 'chai';
import Key from '../../src/content/domains/Key'; import Key from '../../src/shared/settings/Key';
describe('InputDriver', () => { describe('InputDriver', () => {
let target: HTMLElement; let target: HTMLElement;
@ -127,3 +127,50 @@ describe('InputDriver', () => {
div.dispatchEvent(new KeyboardEvent('keydown', { key: 'x' })); div.dispatchEvent(new KeyboardEvent('keydown', { key: 'x' }));
}); });
}); });
describe("#keyFromKeyboardEvent", () => {
it('returns from keyboard input Ctrl+X', () => {
let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true,
}));
expect(k.key).to.equal('x');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.true;
});
it('returns from keyboard input Shift+Esc', () => {
let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true
}));
expect(k.key).to.equal('Esc');
expect(k.shift).to.be.true;
expect(k.ctrl).to.be.false;
expect(k.alt).to.be.false;
expect(k.meta).to.be.true;
});
it('returns from keyboard input Ctrl+$', () => {
// $ required shift pressing on most keyboards
let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', {
key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false
}));
expect(k.key).to.equal('$');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.false;
});
it('returns from keyboard input Crtl+Space', () => {
let k = keyFromKeyboardEvent(new KeyboardEvent('keydown', {
key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false
}));
expect(k.key).to.equal('Space');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.false;
});
});

@ -1,7 +1,7 @@
import KeymapRepository, { KeymapRepositoryImpl } import KeymapRepository, { KeymapRepositoryImpl }
from '../../../src/content/repositories/KeymapRepository'; from '../../../src/content/repositories/KeymapRepository';
import Key from '../../../src/content/domains/Key'
import { expect } from 'chai'; import { expect } from 'chai';
import Key from "../../../src/shared/settings/Key";
describe('KeymapRepositoryImpl', () => { describe('KeymapRepositoryImpl', () => {
let sut: KeymapRepository; let sut: KeymapRepository;

@ -1,54 +1,7 @@
import Key from '../../../src/content/domains/Key';
import { expect } from 'chai' import { expect } from 'chai'
import Key from '../../../src/shared/settings/Key';
describe("Key", () => { describe("Key", () => {
describe('fromKeyboardEvent', () => {
it('returns from keyboard input Ctrl+X', () => {
let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'x', shiftKey: false, ctrlKey: true, altKey: false, metaKey: true,
}));
expect(k.key).to.equal('x');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.true;
});
it('returns from keyboard input Shift+Esc', () => {
let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: 'Escape', shiftKey: true, ctrlKey: false, altKey: false, metaKey: true
}));
expect(k.key).to.equal('Esc');
expect(k.shift).to.be.true;
expect(k.ctrl).to.be.false;
expect(k.alt).to.be.false;
expect(k.meta).to.be.true;
});
it('returns from keyboard input Ctrl+$', () => {
// $ required shift pressing on most keyboards
let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: '$', shiftKey: true, ctrlKey: true, altKey: false, metaKey: false
}));
expect(k.key).to.equal('$');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.false;
});
it('returns from keyboard input Crtl+Space', () => {
let k = Key.fromKeyboardEvent(new KeyboardEvent('keydown', {
key: ' ', shiftKey: false, ctrlKey: true, altKey: false, metaKey: false
}));
expect(k.key).to.equal('Space');
expect(k.shift).to.be.false;
expect(k.ctrl).to.be.true;
expect(k.alt).to.be.false;
expect(k.meta).to.be.false;
});
});
describe('fromMapKey', () => { describe('fromMapKey', () => {
it('return for X', () => { it('return for X', () => {
let key = Key.fromMapKey('x'); let key = Key.fromMapKey('x');
@ -125,7 +78,7 @@ describe("Key", () => {
describe('equals', () => { describe('equals', () => {
expect(new Key({ expect(new Key({
key: 'x', shift: false, ctrl: true, alt: false, meta: false, key: 'x', shift: false, ctrl: true, alt: false, meta: false,
}).equals(new Key({ }).equals(new Key({
key: 'x', shift: false, ctrl: true, alt: false, meta: false, key: 'x', shift: false, ctrl: true, alt: false, meta: false,
}))).to.be.true; }))).to.be.true;

@ -1,6 +1,6 @@
import KeySequence from '../../../src/content/domains/KeySequence'; import KeySequence from '../../../src/shared/settings/KeySequence';
import Key from '../../../src/content/domains/Key';
import { expect } from 'chai' import { expect } from 'chai'
import Key from "../../../src/shared/settings/Key";
describe("KeySequence", () => { describe("KeySequence", () => {
describe('#push', () => { describe('#push', () => {