From 6e6e306275a8ee5632a22b1e30c807bd5ae9cc7e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 11 Oct 2018 17:42:38 +0900 Subject: Store local marks --- src/content/components/common/index.js | 7 ++-- src/content/components/common/mark.js | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/content/components/common/mark.js (limited to 'src/content/components') diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index a1e71a1..bcab4fa 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -1,6 +1,7 @@ import InputComponent from './input'; -import KeymapperComponent from './keymapper'; import FollowComponent from './follow'; +import MarkComponent from './mark'; +import KeymapperComponent from './keymapper'; import * as settingActions from 'content/actions/setting'; import messages from 'shared/messages'; import * as addonActions from '../../actions/addon'; @@ -8,11 +9,13 @@ import * as blacklists from 'shared/blacklists'; export default class Common { constructor(win, store) { - const follow = new FollowComponent(win, store); const input = new InputComponent(win.document.body, store); + const follow = new FollowComponent(win, store); + const mark = new MarkComponent(win.document.body, store); const keymapper = new KeymapperComponent(store); input.onKey(key => follow.key(key)); + input.onKey(key => mark.key(key)); input.onKey(key => keymapper.key(key)); this.win = win; diff --git a/src/content/components/common/mark.js b/src/content/components/common/mark.js new file mode 100644 index 0000000..06b2657 --- /dev/null +++ b/src/content/components/common/mark.js @@ -0,0 +1,59 @@ +import * as markActions from 'content/actions/mark'; +import * as scrolls from 'content/scrolls'; +import * as consoleFrames from 'content/console-frames'; +import * as properties from 'shared/settings/properties'; + +const cancelKey = (key) => { + return key.key === 'Esc' || key.key === '[' && key.ctrlKey; +}; + +export default class MarkComponent { + constructor(body, store) { + this.body = body; + this.store = store; + } + + // eslint-disable-next-line max-statements + key(key) { + let { mark: markStage, setting } = this.store.getState(); + let smoothscroll = setting.properties.smoothscroll || + properties.defaults.smoothscroll; + + if (!markStage.setMode && !markStage.jumpMode) { + return false; + } + + if (cancelKey(key)) { + this.store.dispatch(markActions.cancel()); + return true; + } + + if (key.ctrlKey || key.metaKey || key.altKey) { + consoleFrames.postError(window.document, 'Unknown mark'); + } else if (key.shiftKey) { + consoleFrames.postError(window.document, 'Globa marks not supported'); + } else if (markStage.setMode) { + this.doSet(key); + } else if (markStage.jumpMode) { + this.doJump(markStage.marks, key, smoothscroll); + } + + this.store.dispatch(markActions.cancel()); + return true; + } + + doSet(key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setLocal(key.key, x, y)); + } + + doJump(marks, key, smoothscroll) { + if (!marks[key.key]) { + consoleFrames.postError(window.document, 'Mark is not set'); + return; + } + + let { x, y } = marks[key.key]; + scrolls.scrollTo(x, y, smoothscroll); + } +} -- cgit v1.2.3 From 003742ec51aa7aea9214442bc0b611e2eb5eaf6e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 12 Oct 2018 10:14:33 +0900 Subject: Support global marks which select a tab --- src/background/controllers/mark.js | 15 ++++++++++ src/background/domains/global-mark.js | 19 +++++++++++++ .../infrastructures/content-message-listener.js | 14 +++++++++ src/background/repositories/mark.js | 33 ++++++++++++++++++++++ src/background/usecases/mark.js | 29 +++++++++++++++++++ src/content/actions/mark.js | 20 +++++++++++++ src/content/components/common/mark.js | 15 ++++++++-- src/shared/messages.js | 3 ++ test/background/domains/global-mark.test.js | 10 +++++++ .../infrastructures/memory-storage.test.js | 2 -- test/background/repositories/mark.test.js | 23 +++++++++++++++ 11 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 src/background/controllers/mark.js create mode 100644 src/background/domains/global-mark.js create mode 100644 src/background/repositories/mark.js create mode 100644 src/background/usecases/mark.js create mode 100644 test/background/domains/global-mark.test.js create mode 100644 test/background/repositories/mark.test.js (limited to 'src/content/components') diff --git a/src/background/controllers/mark.js b/src/background/controllers/mark.js new file mode 100644 index 0000000..8d0cefd --- /dev/null +++ b/src/background/controllers/mark.js @@ -0,0 +1,15 @@ +import MarkInteractor from '../usecases/mark'; + +export default class MarkController { + constructor() { + this.markInteractor = new MarkInteractor(); + } + + setGlobal(key, x, y) { + this.markInteractor.setGlobal(key, x, y); + } + + jumpGlobal(key) { + this.markInteractor.jumpGlobal(key); + } +} diff --git a/src/background/domains/global-mark.js b/src/background/domains/global-mark.js new file mode 100644 index 0000000..77afdba --- /dev/null +++ b/src/background/domains/global-mark.js @@ -0,0 +1,19 @@ +export default class GlobalMark { + constructor(tabId, x, y) { + this.tabId0 = tabId; + this.x0 = x; + this.y0 = y; + } + + get tabId() { + return this.tabId0; + } + + get x() { + return this.x0; + } + + get y() { + return this.y0; + } +} diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js index 4fcc6a6..beb52fe 100644 --- a/src/background/infrastructures/content-message-listener.js +++ b/src/background/infrastructures/content-message-listener.js @@ -5,6 +5,7 @@ import FindController from '../controllers/find'; import AddonEnabledController from '../controllers/addon-enabled'; import LinkController from '../controllers/link'; import OperationController from '../controllers/operation'; +import MarkController from '../controllers/mark'; export default class ContentMessageListener { constructor() { @@ -14,6 +15,7 @@ export default class ContentMessageListener { this.addonEnabledController = new AddonEnabledController(); this.linkController = new LinkController(); this.backgroundOperationController = new OperationController(); + this.markController = new MarkController(); } run() { @@ -59,6 +61,10 @@ export default class ContentMessageListener { message.newTab, message.url, sender.tab.id, message.background); case messages.BACKGROUND_OPERATION: return this.onBackgroundOperation(message.operation); + case messages.MARK_SET_GLOBAL: + return this.onMarkSetGlobal(message.key, message.x, message.y); + case messages.MARK_JUMP_GLOBAL: + return this.onMarkJumpGlobal(message.key); } } @@ -102,4 +108,12 @@ export default class ContentMessageListener { onBackgroundOperation(operation) { return this.backgroundOperationController.exec(operation); } + + onMarkSetGlobal(key, x, y) { + return this.markController.setGlobal(key, x, y); + } + + onMarkJumpGlobal(key) { + return this.markController.jumpGlobal(key); + } } diff --git a/src/background/repositories/mark.js b/src/background/repositories/mark.js new file mode 100644 index 0000000..a1f6a16 --- /dev/null +++ b/src/background/repositories/mark.js @@ -0,0 +1,33 @@ +import MemoryStorage from '../infrastructures/memory-storage'; +import GlobalMark from 'background/domains/global-mark'; + +const MARK_KEY = 'mark'; + +export default class MarkRepository { + constructor() { + this.cache = new MemoryStorage(); + } + + getMark(key) { + let marks = this.getOrEmptyMarks(); + let data = marks[key]; + if (!data) { + return Promise.resolve(undefined); + } + let mark = new GlobalMark(data.tabId, data.x, data.y); + return Promise.resolve(mark); + } + + setMark(key, mark) { + let marks = this.getOrEmptyMarks(); + marks[key] = { tabId: mark.tabId, x: mark.x, y: mark.y }; + this.cache.set(MARK_KEY, marks); + + return Promise.resolve(); + } + + getOrEmptyMarks() { + return this.cache.get(MARK_KEY) || {}; + } +} + diff --git a/src/background/usecases/mark.js b/src/background/usecases/mark.js new file mode 100644 index 0000000..2cb3b45 --- /dev/null +++ b/src/background/usecases/mark.js @@ -0,0 +1,29 @@ +import GlobalMark from '../domains/global-mark'; +import TabPresenter from '../presenters/tab'; +import MarkRepository from '../repositories/mark'; +import ConsolePresenter from '../presenters/console'; + +export default class MarkInteractor { + constructor() { + this.tabPresenter = new TabPresenter(); + this.markRepository = new MarkRepository(); + this.consolePresenter = new ConsolePresenter(); + } + + async setGlobal(key, x, y) { + let tab = await this.tabPresenter.getCurrent(); + let mark = new GlobalMark(tab.id, x, y); + return this.markRepository.setMark(key, mark); + } + + async jumpGlobal(key) { + let current = await this.tabPresenter.getCurrent(); + + let mark = await this.markRepository.getMark(key); + if (!mark) { + return this.consolePresenter.showError(current.id, 'Mark is not set'); + } + // TODO scroll pages and handle if tab is gone + return this.tabPresenter.select(mark.tabId); + } +} diff --git a/src/content/actions/mark.js b/src/content/actions/mark.js index 1f5174e..712a811 100644 --- a/src/content/actions/mark.js +++ b/src/content/actions/mark.js @@ -1,4 +1,5 @@ import actions from 'content/actions'; +import messages from 'shared/messages'; const startSet = () => { return { type: actions.MARK_START_SET }; @@ -21,6 +22,25 @@ const setLocal = (key, x, y) => { }; }; +const setGlobal = (key, x, y) => { + browser.runtime.sendMessage({ + type: messages.MARK_SET_GLOBAL, + key, + x, + y, + }); + return { type: '' }; +}; + +const jumpGlobal = (key) => { + browser.runtime.sendMessage({ + type: messages.MARK_JUMP_GLOBAL, + key, + }); + return { type: '' }; +}; + export { startSet, startJump, cancel, setLocal, + setGlobal, jumpGlobal, }; diff --git a/src/content/components/common/mark.js b/src/content/components/common/mark.js index 06b2657..ce35afa 100644 --- a/src/content/components/common/mark.js +++ b/src/content/components/common/mark.js @@ -30,8 +30,10 @@ export default class MarkComponent { if (key.ctrlKey || key.metaKey || key.altKey) { consoleFrames.postError(window.document, 'Unknown mark'); - } else if (key.shiftKey) { - consoleFrames.postError(window.document, 'Globa marks not supported'); + } else if (key.shiftKey && markStage.setMode) { + this.doSetGlobal(key); + } else if (key.shiftKey && markStage.jumpMode) { + this.doJumpGlobal(key); } else if (markStage.setMode) { this.doSet(key); } else if (markStage.jumpMode) { @@ -56,4 +58,13 @@ export default class MarkComponent { let { x, y } = marks[key.key]; scrolls.scrollTo(x, y, smoothscroll); } + + doSetGlobal(key) { + let { x, y } = scrolls.getScroll(); + this.store.dispatch(markActions.setGlobal(key.key, x, y)); + } + + doJumpGlobal(key) { + this.store.dispatch(markActions.jumpGlobal(key.key)); + } } diff --git a/src/shared/messages.js b/src/shared/messages.js index 1f9c816..cd076ee 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -43,6 +43,9 @@ export default { FOLLOW_ACTIVATE: 'follow.activate', FOLLOW_KEY_PRESS: 'follow.key.press', + MARK_SET_GLOBAL: 'mark.set.global', + MARK_JUMP_GLOBAL: 'mark.jump.global', + FIND_NEXT: 'find.next', FIND_PREV: 'find.prev', FIND_GET_KEYWORD: 'find.get.keyword', diff --git a/test/background/domains/global-mark.test.js b/test/background/domains/global-mark.test.js new file mode 100644 index 0000000..15d492c --- /dev/null +++ b/test/background/domains/global-mark.test.js @@ -0,0 +1,10 @@ +import GlobalMark from 'background/domains/global-mark'; + +describe("background/domains/global-mark", () => { + describe("constructor and getter", () => { + let mark = new GlobalMark(1, 10, 30); + expect(mark.tabId).to.equal(1); + expect(mark.x).to.equal(10); + expect(mark.y).to.equal(30); + }); +}); diff --git a/test/background/infrastructures/memory-storage.test.js b/test/background/infrastructures/memory-storage.test.js index 0fea895..8871749 100644 --- a/test/background/infrastructures/memory-storage.test.js +++ b/test/background/infrastructures/memory-storage.test.js @@ -1,8 +1,6 @@ import MemoryStorage from 'background/infrastructures/memory-storage'; describe("background/infrastructures/memory-storage", () => { - let versionRepository; - it('stores values', () => { let cache = new MemoryStorage(); cache.set('number', 123); diff --git a/test/background/repositories/mark.test.js b/test/background/repositories/mark.test.js new file mode 100644 index 0000000..94048ea --- /dev/null +++ b/test/background/repositories/mark.test.js @@ -0,0 +1,23 @@ +import MarkRepository from 'background/repositories/mark'; +import GlobalMark from 'background/domains/global-mark'; + +describe("background/repositories/version", () => { + let repository; + + beforeEach(() => { + repository = new MarkRepository; + }); + + it('get and set', async() => { + let mark = new GlobalMark(1, 10, 30); + + repository.setMark('A', mark); + + let got = await repository.getMark('A'); + expect(got).to.be.a('object'); + expect(got.tabId).to.equal(1); + + got = await repository.getMark('B'); + expect(got).to.be.undefined; + }); +}); -- cgit v1.2.3 From e248477ecb46596af734589615118ba573971dc0 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 12 Oct 2018 14:59:45 +0900 Subject: Scroll on global mark and handle gone tab --- src/background/domains/global-mark.js | 7 ++++++- src/background/infrastructures/content-message-client.js | 8 ++++++++ src/background/repositories/mark.js | 4 ++-- src/background/usecases/mark.js | 16 +++++++++++++--- src/content/components/common/mark.js | 8 ++++++-- src/content/components/top-content/index.js | 3 +++ src/shared/messages.js | 2 ++ test/background/domains/global-mark.test.js | 7 ++++--- test/background/repositories/mark.test.js | 7 +++++-- 9 files changed, 49 insertions(+), 13 deletions(-) (limited to 'src/content/components') diff --git a/src/background/domains/global-mark.js b/src/background/domains/global-mark.js index 77afdba..f0586f1 100644 --- a/src/background/domains/global-mark.js +++ b/src/background/domains/global-mark.js @@ -1,6 +1,7 @@ export default class GlobalMark { - constructor(tabId, x, y) { + constructor(tabId, url, x, y) { this.tabId0 = tabId; + this.url0 = url; this.x0 = x; this.y0 = y; } @@ -9,6 +10,10 @@ export default class GlobalMark { return this.tabId0; } + get url() { + return this.url0; + } + get x() { return this.x0; } diff --git a/src/background/infrastructures/content-message-client.js b/src/background/infrastructures/content-message-client.js index d659560..7e7e602 100644 --- a/src/background/infrastructures/content-message-client.js +++ b/src/background/infrastructures/content-message-client.js @@ -22,4 +22,12 @@ export default class ContentMessageClient { type: messages.ADDON_TOGGLE_ENABLED, }); } + + scrollTo(tabId, x, y) { + return browser.tabs.sendMessage(tabId, { + type: messages.TAB_SCROLL_TO, + x, + y, + }); + } } diff --git a/src/background/repositories/mark.js b/src/background/repositories/mark.js index a1f6a16..339a660 100644 --- a/src/background/repositories/mark.js +++ b/src/background/repositories/mark.js @@ -14,13 +14,13 @@ export default class MarkRepository { if (!data) { return Promise.resolve(undefined); } - let mark = new GlobalMark(data.tabId, data.x, data.y); + let mark = new GlobalMark(data.tabId, data.url, data.x, data.y); return Promise.resolve(mark); } setMark(key, mark) { let marks = this.getOrEmptyMarks(); - marks[key] = { tabId: mark.tabId, x: mark.x, y: mark.y }; + marks[key] = { tabId: mark.tabId, url: mark.url, x: mark.x, y: mark.y }; this.cache.set(MARK_KEY, marks); return Promise.resolve(); diff --git a/src/background/usecases/mark.js b/src/background/usecases/mark.js index 2cb3b45..34b8a74 100644 --- a/src/background/usecases/mark.js +++ b/src/background/usecases/mark.js @@ -2,17 +2,19 @@ import GlobalMark from '../domains/global-mark'; import TabPresenter from '../presenters/tab'; import MarkRepository from '../repositories/mark'; import ConsolePresenter from '../presenters/console'; +import ContentMessageClient from '../infrastructures/content-message-client'; export default class MarkInteractor { constructor() { this.tabPresenter = new TabPresenter(); this.markRepository = new MarkRepository(); this.consolePresenter = new ConsolePresenter(); + this.contentMessageClient = new ContentMessageClient(); } async setGlobal(key, x, y) { let tab = await this.tabPresenter.getCurrent(); - let mark = new GlobalMark(tab.id, x, y); + let mark = new GlobalMark(tab.id, tab.url, x, y); return this.markRepository.setMark(key, mark); } @@ -23,7 +25,15 @@ export default class MarkInteractor { if (!mark) { return this.consolePresenter.showError(current.id, 'Mark is not set'); } - // TODO scroll pages and handle if tab is gone - return this.tabPresenter.select(mark.tabId); + + return this.contentMessageClient.scrollTo( + mark.tabId, mark.x, mark.y + ).then(() => { + return this.tabPresenter.select(mark.tabId); + }).catch(async() => { + let tab = await this.tabPresenter.create(mark.url); + let mark2 = new GlobalMark(tab.id, mark.url, mark.x, mark.y); + return this.markRepository.setMark(key, mark2); + }); } } diff --git a/src/content/components/common/mark.js b/src/content/components/common/mark.js index ce35afa..1ed636b 100644 --- a/src/content/components/common/mark.js +++ b/src/content/components/common/mark.js @@ -7,6 +7,10 @@ const cancelKey = (key) => { return key.key === 'Esc' || key.key === '[' && key.ctrlKey; }; +const globalKey = (key) => { + return (/^[A-Z0-9]$/).test(key); +}; + export default class MarkComponent { constructor(body, store) { this.body = body; @@ -30,9 +34,9 @@ export default class MarkComponent { if (key.ctrlKey || key.metaKey || key.altKey) { consoleFrames.postError(window.document, 'Unknown mark'); - } else if (key.shiftKey && markStage.setMode) { + } else if (globalKey(key.key) && markStage.setMode) { this.doSetGlobal(key); - } else if (key.shiftKey && markStage.jumpMode) { + } else if (globalKey(key.key) && markStage.jumpMode) { this.doJumpGlobal(key); } else if (markStage.setMode) { this.doSet(key); diff --git a/src/content/components/top-content/index.js b/src/content/components/top-content/index.js index e22e957..1aaef1b 100644 --- a/src/content/components/top-content/index.js +++ b/src/content/components/top-content/index.js @@ -3,6 +3,7 @@ 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 { @@ -33,6 +34,8 @@ export default class TopContent { 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/shared/messages.js b/src/shared/messages.js index cd076ee..dad2b7a 100644 --- a/src/shared/messages.js +++ b/src/shared/messages.js @@ -46,6 +46,8 @@ export default { MARK_SET_GLOBAL: 'mark.set.global', MARK_JUMP_GLOBAL: 'mark.jump.global', + TAB_SCROLL_TO: 'tab.scroll.to', + FIND_NEXT: 'find.next', FIND_PREV: 'find.prev', FIND_GET_KEYWORD: 'find.get.keyword', diff --git a/test/background/domains/global-mark.test.js b/test/background/domains/global-mark.test.js index 15d492c..bdf1ea6 100644 --- a/test/background/domains/global-mark.test.js +++ b/test/background/domains/global-mark.test.js @@ -1,9 +1,10 @@ import GlobalMark from 'background/domains/global-mark'; -describe("background/domains/global-mark", () => { - describe("constructor and getter", () => { - let mark = new GlobalMark(1, 10, 30); +describe('background/domains/global-mark', () => { + describe('constructor and getter', () => { + let mark = new GlobalMark(1, 'http://example.com', 10, 30); expect(mark.tabId).to.equal(1); + expect(mark.url).to.equal('http://example.com'); expect(mark.x).to.equal(10); expect(mark.y).to.equal(30); }); diff --git a/test/background/repositories/mark.test.js b/test/background/repositories/mark.test.js index 94048ea..4f71f62 100644 --- a/test/background/repositories/mark.test.js +++ b/test/background/repositories/mark.test.js @@ -1,7 +1,7 @@ import MarkRepository from 'background/repositories/mark'; import GlobalMark from 'background/domains/global-mark'; -describe("background/repositories/version", () => { +describe('background/repositories/mark', () => { let repository; beforeEach(() => { @@ -9,13 +9,16 @@ describe("background/repositories/version", () => { }); it('get and set', async() => { - let mark = new GlobalMark(1, 10, 30); + let mark = new GlobalMark(1, 'http://example.com', 10, 30); repository.setMark('A', mark); let got = await repository.getMark('A'); expect(got).to.be.a('object'); expect(got.tabId).to.equal(1); + expect(got.url).to.equal('http://example.com'); + expect(got.x).to.equal(10); + expect(got.y).to.equal(30); got = await repository.getMark('B'); expect(got).to.be.undefined; -- cgit v1.2.3