From c60d0e7392fc708e961614d6b756a045de74f458 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Tue, 30 Apr 2019 14:00:07 +0900 Subject: Rename .js/.jsx to .ts/.tsx --- src/content/components/top-content/find.js | 41 ------ src/content/components/top-content/find.ts | 41 ++++++ .../components/top-content/follow-controller.js | 147 --------------------- .../components/top-content/follow-controller.ts | 147 +++++++++++++++++++++ src/content/components/top-content/index.js | 41 ------ src/content/components/top-content/index.ts | 41 ++++++ 6 files changed, 229 insertions(+), 229 deletions(-) delete mode 100644 src/content/components/top-content/find.js create mode 100644 src/content/components/top-content/find.ts delete mode 100644 src/content/components/top-content/follow-controller.js create mode 100644 src/content/components/top-content/follow-controller.ts delete mode 100644 src/content/components/top-content/index.js create mode 100644 src/content/components/top-content/index.ts (limited to 'src/content/components/top-content') diff --git a/src/content/components/top-content/find.js b/src/content/components/top-content/find.js deleted file mode 100644 index 4d46d79..0000000 --- a/src/content/components/top-content/find.js +++ /dev/null @@ -1,41 +0,0 @@ -import * as findActions from 'content/actions/find'; -import messages from 'shared/messages'; - -export default class FindComponent { - constructor(win, store) { - this.win = win; - this.store = store; - - messages.onMessage(this.onMessage.bind(this)); - } - - onMessage(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) { - let state = this.store.getState().find; - - if (text.length === 0) { - return this.store.dispatch(findActions.next(state.keyword, true)); - } - return this.store.dispatch(findActions.next(text, true)); - } - - next() { - let state = this.store.getState().find; - return this.store.dispatch(findActions.next(state.keyword, false)); - } - - prev() { - let state = this.store.getState().find; - return this.store.dispatch(findActions.prev(state.keyword, false)); - } -} diff --git a/src/content/components/top-content/find.ts b/src/content/components/top-content/find.ts new file mode 100644 index 0000000..4d46d79 --- /dev/null +++ b/src/content/components/top-content/find.ts @@ -0,0 +1,41 @@ +import * as findActions from 'content/actions/find'; +import messages from 'shared/messages'; + +export default class FindComponent { + constructor(win, store) { + this.win = win; + this.store = store; + + messages.onMessage(this.onMessage.bind(this)); + } + + onMessage(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) { + let state = this.store.getState().find; + + if (text.length === 0) { + return this.store.dispatch(findActions.next(state.keyword, true)); + } + return this.store.dispatch(findActions.next(text, true)); + } + + next() { + let state = this.store.getState().find; + return this.store.dispatch(findActions.next(state.keyword, false)); + } + + prev() { + let state = this.store.getState().find; + return this.store.dispatch(findActions.prev(state.keyword, false)); + } +} diff --git a/src/content/components/top-content/follow-controller.js b/src/content/components/top-content/follow-controller.js deleted file mode 100644 index 7f36604..0000000 --- a/src/content/components/top-content/follow-controller.js +++ /dev/null @@ -1,147 +0,0 @@ -import * as followControllerActions from 'content/actions/follow-controller'; -import messages from 'shared/messages'; -import HintKeyProducer from 'content/hint-key-producer'; -import * as properties from 'shared/settings/properties'; - -const broadcastMessage = (win, message) => { - let json = JSON.stringify(message); - let frames = [window.self].concat(Array.from(window.frames)); - frames.forEach(frame => frame.postMessage(json, '*')); -}; - -export default class FollowController { - constructor(win, store) { - this.win = win; - this.store = store; - this.state = {}; - this.keys = []; - this.producer = null; - - messages.onMessage(this.onMessage.bind(this)); - - store.subscribe(() => { - this.update(); - }); - } - - onMessage(message, sender) { - 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() { - 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() { - let shown = this.keys.filter(key => key.startsWith(this.state.keys)); - if (shown.length === 1) { - this.activate(); - this.store.dispatch(followControllerActions.disable()); - } - - broadcastMessage(this.win, { - type: messages.FOLLOW_SHOW_HINTS, - keys: this.state.keys, - }); - } - - activate() { - broadcastMessage(this.win, { - type: messages.FOLLOW_ACTIVATE, - keys: this.state.keys, - }); - } - - keyPress(key, ctrlKey) { - 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((element) => { - let { left: frameX, top: frameY } = element.getBoundingClientRect(); - let message = JSON.stringify({ - type: messages.FOLLOW_REQUEST_COUNT_TARGETS, - viewSize: { width: viewWidth, height: viewHeight }, - framePosition: { x: frameX, y: frameY }, - }); - element.contentWindow.postMessage(message, '*'); - }); - } - - create(count, sender) { - let produced = []; - for (let i = 0; i < count; ++i) { - produced.push(this.producer.produce()); - } - this.keys = this.keys.concat(produced); - - sender.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 || - properties.defaults.hintchars; - } -} diff --git a/src/content/components/top-content/follow-controller.ts b/src/content/components/top-content/follow-controller.ts new file mode 100644 index 0000000..7f36604 --- /dev/null +++ b/src/content/components/top-content/follow-controller.ts @@ -0,0 +1,147 @@ +import * as followControllerActions from 'content/actions/follow-controller'; +import messages from 'shared/messages'; +import HintKeyProducer from 'content/hint-key-producer'; +import * as properties from 'shared/settings/properties'; + +const broadcastMessage = (win, message) => { + let json = JSON.stringify(message); + let frames = [window.self].concat(Array.from(window.frames)); + frames.forEach(frame => frame.postMessage(json, '*')); +}; + +export default class FollowController { + constructor(win, store) { + this.win = win; + this.store = store; + this.state = {}; + this.keys = []; + this.producer = null; + + messages.onMessage(this.onMessage.bind(this)); + + store.subscribe(() => { + this.update(); + }); + } + + onMessage(message, sender) { + 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() { + 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() { + let shown = this.keys.filter(key => key.startsWith(this.state.keys)); + if (shown.length === 1) { + this.activate(); + this.store.dispatch(followControllerActions.disable()); + } + + broadcastMessage(this.win, { + type: messages.FOLLOW_SHOW_HINTS, + keys: this.state.keys, + }); + } + + activate() { + broadcastMessage(this.win, { + type: messages.FOLLOW_ACTIVATE, + keys: this.state.keys, + }); + } + + keyPress(key, ctrlKey) { + 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((element) => { + let { left: frameX, top: frameY } = element.getBoundingClientRect(); + let message = JSON.stringify({ + type: messages.FOLLOW_REQUEST_COUNT_TARGETS, + viewSize: { width: viewWidth, height: viewHeight }, + framePosition: { x: frameX, y: frameY }, + }); + element.contentWindow.postMessage(message, '*'); + }); + } + + create(count, sender) { + let produced = []; + for (let i = 0; i < count; ++i) { + produced.push(this.producer.produce()); + } + this.keys = this.keys.concat(produced); + + sender.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 || + properties.defaults.hintchars; + } +} diff --git a/src/content/components/top-content/index.js b/src/content/components/top-content/index.js deleted file mode 100644 index 1aaef1b..0000000 --- a/src/content/components/top-content/index.js +++ /dev/null @@ -1,41 +0,0 @@ -import CommonComponent from '../common'; -import FollowController from './follow-controller'; -import FindComponent from './find'; -import * as consoleFrames from '../../console-frames'; -import messages from 'shared/messages'; -import * as scrolls from 'content/scrolls'; - -export default class TopContent { - - constructor(win, store) { - 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(win, store); // eslint-disable-line no-new - - // TODO make component - consoleFrames.initialize(this.win.document); - - messages.onMessage(this.onMessage.bind(this)); - } - - onMessage(message) { - let addonState = this.store.getState().addon; - - switch (message.type) { - case messages.CONSOLE_UNFOCUS: - this.win.focus(); - consoleFrames.blur(window.document); - return Promise.resolve(); - 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); - } - } -} diff --git a/src/content/components/top-content/index.ts b/src/content/components/top-content/index.ts new file mode 100644 index 0000000..1aaef1b --- /dev/null +++ b/src/content/components/top-content/index.ts @@ -0,0 +1,41 @@ +import CommonComponent from '../common'; +import FollowController from './follow-controller'; +import FindComponent from './find'; +import * as consoleFrames from '../../console-frames'; +import messages from 'shared/messages'; +import * as scrolls from 'content/scrolls'; + +export default class TopContent { + + constructor(win, store) { + 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(win, store); // eslint-disable-line no-new + + // TODO make component + consoleFrames.initialize(this.win.document); + + messages.onMessage(this.onMessage.bind(this)); + } + + onMessage(message) { + let addonState = this.store.getState().addon; + + switch (message.type) { + case messages.CONSOLE_UNFOCUS: + this.win.focus(); + consoleFrames.blur(window.document); + return Promise.resolve(); + 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); + } + } +} -- cgit v1.2.3