aboutsummaryrefslogtreecommitdiff
path: root/src/content/components/top-content
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/components/top-content')
-rw-r--r--src/content/components/top-content/find.ts46
-rw-r--r--src/content/components/top-content/follow-controller.ts166
-rw-r--r--src/content/components/top-content/index.ts51
3 files changed, 0 insertions, 263 deletions
diff --git a/src/content/components/top-content/find.ts b/src/content/components/top-content/find.ts
deleted file mode 100644
index 74b95bc..0000000
--- a/src/content/components/top-content/find.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import * as findActions from '../../actions/find';
-import * as messages from '../../../shared/messages';
-import MessageListener from '../../MessageListener';
-
-export default class FindComponent {
- private store: any;
-
- constructor(store: any) {
- this.store = store;
-
- new MessageListener().onWebMessage(this.onMessage.bind(this));
- }
-
- onMessage(message: messages.Message) {
- switch (message.type) {
- case messages.CONSOLE_ENTER_FIND:
- return this.start(message.text);
- case messages.FIND_NEXT:
- return this.next();
- case messages.FIND_PREV:
- return this.prev();
- }
- }
-
- start(text: string) {
- let state = this.store.getState().find;
-
- if (text.length === 0) {
- return this.store.dispatch(
- findActions.next(state.keyword as string, true));
- }
- return this.store.dispatch(findActions.next(text, true));
- }
-
- next() {
- let state = this.store.getState().find;
- return this.store.dispatch(
- findActions.next(state.keyword as string, false));
- }
-
- prev() {
- let state = this.store.getState().find;
- return this.store.dispatch(
- findActions.prev(state.keyword as string, false));
- }
-}
diff --git a/src/content/components/top-content/follow-controller.ts b/src/content/components/top-content/follow-controller.ts
deleted file mode 100644
index d49b22a..0000000
--- a/src/content/components/top-content/follow-controller.ts
+++ /dev/null
@@ -1,166 +0,0 @@
-import * as followControllerActions from '../../actions/follow-controller';
-import * as messages from '../../../shared/messages';
-import MessageListener, { WebMessageSender } from '../../MessageListener';
-import HintKeyProducer from '../../hint-key-producer';
-
-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,
- });
- }
-
- hintchars() {
- return this.store.getState().setting.properties.hintchars;
- }
-}
diff --git a/src/content/components/top-content/index.ts b/src/content/components/top-content/index.ts
deleted file mode 100644
index ac95ea9..0000000
--- a/src/content/components/top-content/index.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import CommonComponent from '../common';
-import FollowController from './follow-controller';
-import FindComponent from './find';
-import * as consoleFrames from '../../console-frames';
-import * as messages from '../../../shared/messages';
-import MessageListener from '../../MessageListener';
-import * as scrolls from '../../scrolls';
-
-export default class TopContent {
- private win: Window;
-
- private store: any;
-
- constructor(win: Window, store: any) {
- this.win = win;
- this.store = store;
-
- new CommonComponent(win, store); // eslint-disable-line no-new
- new FollowController(win, store); // eslint-disable-line no-new
- new FindComponent(store); // eslint-disable-line no-new
-
- // TODO make component
- consoleFrames.initialize(this.win.document);
-
- new MessageListener().onWebMessage(this.onWebMessage.bind(this));
- new MessageListener().onBackgroundMessage(
- this.onBackgroundMessage.bind(this));
- }
-
- onWebMessage(message: messages.Message) {
- switch (message.type) {
- case messages.CONSOLE_UNFOCUS:
- this.win.focus();
- consoleFrames.blur(window.document);
- }
- }
-
- onBackgroundMessage(message: messages.Message) {
- let addonState = this.store.getState().addon;
-
- switch (message.type) {
- case messages.ADDON_ENABLED_QUERY:
- return Promise.resolve({
- type: messages.ADDON_ENABLED_RESPONSE,
- enabled: addonState.enabled,
- });
- case messages.TAB_SCROLL_TO:
- return scrolls.scrollTo(message.x, message.y, false);
- }
- }
-}