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. 32
      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. 49
      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 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) {

@ -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;

@ -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 {

@ -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 {

@ -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 {

@ -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;

@ -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;

@ -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);
}
}

@ -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 {
public readonly key: string;
@ -63,25 +51,6 @@ 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) {
return this.key === key.key &&
this.ctrl === key.ctrl &&
@ -90,4 +59,3 @@ export default class Key {
this.shift === key.shift;
}
}

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

@ -1,6 +1,6 @@
import InputDriver from '../../src/content/InputDriver';
import InputDriver, {keyFromKeyboardEvent} from '../../src/content/InputDriver';
import { expect } from 'chai';
import Key from '../../src/content/domains/Key';
import Key from '../../src/shared/settings/Key';
describe('InputDriver', () => {
let target: HTMLElement;
@ -127,3 +127,50 @@ describe('InputDriver', () => {
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 }
from '../../../src/content/repositories/KeymapRepository';
import Key from '../../../src/content/domains/Key'
import { expect } from 'chai';
import Key from "../../../src/shared/settings/Key";
describe('KeymapRepositoryImpl', () => {
let sut: KeymapRepository;

@ -1,54 +1,7 @@
import Key from '../../../src/content/domains/Key';
import { expect } from 'chai'
import Key from '../../../src/shared/settings/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', () => {
it('return for X', () => {
let key = Key.fromMapKey('x');

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