aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/top-content/follow-controller.ts
blob: 2fcf3651745050a1754c77e2c2ddddfbb2cb8717 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as followControllerActions from '../../actions/follow-controller';
import * as messages from '../../../shared/messages';
import MessageListener, { WebMessageSender } from '../../MessageListener';
import HintKeyProducer from '../../hint-key-producer';

import { SettingRepositoryImpl } from '../../repositories/SettingRepository';

let settingRepository = new SettingRepositoryImpl();

const broadcastMessage = (win: Window, message: messages.Message): void => {
  let json = JSON.stringify(message);
  let frames = [win.self].concat(Array.from(win.frames as any));
  frames.forEach(frame => frame.postMessage(json, '*'));
};

export default class FollowController {
  private win: Window;

  private store: any;

  private state: {
    enabled?: boolean;
    newTab?: boolean;
    background?: boolean;
    keys?: string,
  };

  private keys: string[];

  private producer: HintKeyProducer | null;

  constructor(win: Window, store: any) {
    this.win = win;
    this.store = store;
    this.state = {};
    this.keys = [];
    this.producer = null;

    new MessageListener().onWebMessage(this.onMessage.bind(this));

    store.subscribe(() => {
      this.update();
    });
  }

  onMessage(message: messages.Message, sender: WebMessageSender) {
    switch (message.type) {
    case messages.FOLLOW_START:
      return this.store.dispatch(
        followControllerActions.enable(message.newTab, message.background));
    case messages.FOLLOW_RESPONSE_COUNT_TARGETS:
      return this.create(message.count, sender);
    case messages.FOLLOW_KEY_PRESS:
      return this.keyPress(message.key, message.ctrlKey);
    }
  }

  update(): void {
    let prevState = this.state;
    this.state = this.store.getState().followController;

    if (!prevState.enabled && this.state.enabled) {
      this.count();
    } else if (prevState.enabled && !this.state.enabled) {
      this.remove();
    } else if (prevState.keys !== this.state.keys) {
      this.updateHints();
    }
  }

  updateHints(): void {
    let shown = this.keys.filter((key) => {
      return key.startsWith(this.state.keys as string);
    });
    if (shown.length === 1) {
      this.activate();
      this.store.dispatch(followControllerActions.disable());
    }

    broadcastMessage(this.win, {
      type: messages.FOLLOW_SHOW_HINTS,
      keys: this.state.keys as string,
    });
  }

  activate(): void {
    broadcastMessage(this.win, {
      type: messages.FOLLOW_ACTIVATE,
      keys: this.state.keys as string,
    });
  }

  keyPress(key: string, ctrlKey: boolean): boolean {
    if (key === '[' && ctrlKey) {
      this.store.dispatch(followControllerActions.disable());
      return true;
    }
    switch (key) {
    case 'Enter':
      this.activate();
      this.store.dispatch(followControllerActions.disable());
      break;
    case 'Esc':
      this.store.dispatch(followControllerActions.disable());
      break;
    case 'Backspace':
    case 'Delete':
      this.store.dispatch(followControllerActions.backspace());
      break;
    default:
      if (this.hintchars().includes(key)) {
        this.store.dispatch(followControllerActions.keyPress(key));
      }
      break;
    }
    return true;
  }

  count() {
    this.producer = new HintKeyProducer(this.hintchars());
    let doc = this.win.document;
    let viewWidth = this.win.innerWidth || doc.documentElement.clientWidth;
    let viewHeight = this.win.innerHeight || doc.documentElement.clientHeight;
    let frameElements = this.win.document.querySelectorAll('frame,iframe');

    this.win.postMessage(JSON.stringify({
      type: messages.FOLLOW_REQUEST_COUNT_TARGETS,
      viewSize: { width: viewWidth, height: viewHeight },
      framePosition: { x: 0, y: 0 },
    }), '*');
    frameElements.forEach((ele) => {
      let { left: frameX, top: frameY } = ele.getBoundingClientRect();
      let message = JSON.stringify({
        type: messages.FOLLOW_REQUEST_COUNT_TARGETS,
        viewSize: { width: viewWidth, height: viewHeight },
        framePosition: { x: frameX, y: frameY },
      });
      if (ele instanceof HTMLFrameElement && ele.contentWindow ||
        ele instanceof HTMLIFrameElement && ele.contentWindow) {
        ele.contentWindow.postMessage(message, '*');
      }
    });
  }

  create(count: number, sender: WebMessageSender) {
    let produced = [];
    for (let i = 0; i < count; ++i) {
      produced.push((this.producer as HintKeyProducer).produce());
    }
    this.keys = this.keys.concat(produced);

    (sender as Window).postMessage(JSON.stringify({
      type: messages.FOLLOW_CREATE_HINTS,
      keysArray: produced,
      newTab: this.state.newTab,
      background: this.state.background,
    }), '*');
  }

  remove() {
    this.keys = [];
    broadcastMessage(this.win, {
      type: messages.FOLLOW_REMOVE_HINTS,
    });
  }

  private hintchars() {
    return settingRepository.get().properties.hintchars;
  }
}