aboutsummaryrefslogtreecommitdiff
path: root/src/background
diff options
context:
space:
mode:
Diffstat (limited to 'src/background')
-rw-r--r--src/background/controllers/mark.js15
-rw-r--r--src/background/domains/global-mark.js24
-rw-r--r--src/background/infrastructures/content-message-client.js8
-rw-r--r--src/background/infrastructures/content-message-listener.js14
-rw-r--r--src/background/repositories/mark.js33
-rw-r--r--src/background/usecases/mark.js39
6 files changed, 133 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..f0586f1
--- /dev/null
+++ b/src/background/domains/global-mark.js
@@ -0,0 +1,24 @@
+export default class GlobalMark {
+ constructor(tabId, url, x, y) {
+ this.tabId0 = tabId;
+ this.url0 = url;
+ this.x0 = x;
+ this.y0 = y;
+ }
+
+ get tabId() {
+ return this.tabId0;
+ }
+
+ get url() {
+ return this.url0;
+ }
+
+ get x() {
+ return this.x0;
+ }
+
+ get y() {
+ return this.y0;
+ }
+}
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/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..339a660
--- /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.url, data.x, data.y);
+ return Promise.resolve(mark);
+ }
+
+ setMark(key, mark) {
+ let marks = this.getOrEmptyMarks();
+ marks[key] = { tabId: mark.tabId, url: mark.url, 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..34b8a74
--- /dev/null
+++ b/src/background/usecases/mark.js
@@ -0,0 +1,39 @@
+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, tab.url, 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');
+ }
+
+ 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);
+ });
+ }
+}