diff options
Diffstat (limited to 'src/background')
-rw-r--r-- | src/background/controllers/mark.js | 15 | ||||
-rw-r--r-- | src/background/domains/global-mark.js | 19 | ||||
-rw-r--r-- | src/background/infrastructures/content-message-listener.js | 14 | ||||
-rw-r--r-- | src/background/repositories/mark.js | 33 | ||||
-rw-r--r-- | src/background/usecases/mark.js | 29 |
5 files changed, 110 insertions, 0 deletions
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); + } +} |