Clipbaord as a clean architecture
This commit is contained in:
parent
c6288f19d9
commit
8cef5981b8
7 changed files with 195 additions and 52 deletions
|
@ -3,15 +3,15 @@ import * as actions from './index';
|
|||
import * as messages from '../../shared/messages';
|
||||
import * as navigates from '../navigates';
|
||||
import * as focuses from '../focuses';
|
||||
import * as urls from '../urls';
|
||||
import * as consoleFrames from '../console-frames';
|
||||
import * as markActions from './mark';
|
||||
|
||||
import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase';
|
||||
import ClipboardUseCase from '../usecases/ClipboardUseCase';
|
||||
import { SettingRepositoryImpl } from '../repositories/SettingRepository';
|
||||
import { ScrollPresenterImpl } from '../presenters/ScrollPresenter';
|
||||
|
||||
let addonEnabledUseCase = new AddonEnabledUseCase();
|
||||
let clipbaordUseCase = new ClipboardUseCase();
|
||||
let settingRepository = new SettingRepositoryImpl();
|
||||
let scrollPresenter = new ScrollPresenterImpl();
|
||||
|
||||
|
@ -95,13 +95,11 @@ const exec = async(
|
|||
focuses.focusInput();
|
||||
break;
|
||||
case operations.URLS_YANK:
|
||||
urls.yank(window);
|
||||
consoleFrames.postInfo('Yanked ' + window.location.href);
|
||||
await clipbaordUseCase.yankCurrentURL();
|
||||
break;
|
||||
case operations.URLS_PASTE:
|
||||
urls.paste(
|
||||
window, operation.newTab ? operation.newTab : false,
|
||||
settings.search,
|
||||
await clipbaordUseCase.openOrSearch(
|
||||
operation.newTab ? operation.newTab : false,
|
||||
);
|
||||
break;
|
||||
default:
|
||||
|
|
18
src/content/client/TabsClient.ts
Normal file
18
src/content/client/TabsClient.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import * as messages from '../../shared/messages';
|
||||
|
||||
export default interface TabsClient {
|
||||
openUrl(url: string, newTab: boolean): Promise<void>;
|
||||
|
||||
// eslint-disable-next-line semi
|
||||
}
|
||||
|
||||
export class TabsClientImpl {
|
||||
async openUrl(url: string, newTab: boolean): Promise<void> {
|
||||
await browser.runtime.sendMessage({
|
||||
type: messages.OPEN_URL,
|
||||
url,
|
||||
newTab,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
46
src/content/repositories/ClipboardRepository.ts
Normal file
46
src/content/repositories/ClipboardRepository.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
export default interface ClipboardRepository {
|
||||
read(): string;
|
||||
|
||||
write(text: string): void;
|
||||
|
||||
// eslint-disable-next-line semi
|
||||
}
|
||||
|
||||
export class ClipboardRepositoryImpl {
|
||||
read(): string {
|
||||
let textarea = window.document.createElement('textarea');
|
||||
window.document.body.append(textarea);
|
||||
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.top = '-100px';
|
||||
textarea.contentEditable = 'true';
|
||||
textarea.focus();
|
||||
|
||||
let ok = window.document.execCommand('paste');
|
||||
let value = textarea.textContent!!;
|
||||
textarea.remove();
|
||||
|
||||
if (!ok) {
|
||||
throw new Error('failed to access clipbaord');
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
write(text: string): void {
|
||||
let input = window.document.createElement('input');
|
||||
window.document.body.append(input);
|
||||
|
||||
input.style.position = 'fixed';
|
||||
input.style.top = '-100px';
|
||||
input.value = text;
|
||||
input.select();
|
||||
|
||||
let ok = window.document.execCommand('copy');
|
||||
input.remove();
|
||||
|
||||
if (!ok) {
|
||||
throw new Error('failed to access clipbaord');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
import * as messages from '../shared/messages';
|
||||
import * as urls from '../shared/urls';
|
||||
import { Search } from '../shared/Settings';
|
||||
|
||||
const yank = (win: Window) => {
|
||||
let input = win.document.createElement('input');
|
||||
win.document.body.append(input);
|
||||
|
||||
input.style.position = 'fixed';
|
||||
input.style.top = '-100px';
|
||||
input.value = win.location.href;
|
||||
input.select();
|
||||
|
||||
win.document.execCommand('copy');
|
||||
|
||||
input.remove();
|
||||
};
|
||||
|
||||
const paste = (win: Window, newTab: boolean, search: Search) => {
|
||||
let textarea = win.document.createElement('textarea');
|
||||
win.document.body.append(textarea);
|
||||
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.top = '-100px';
|
||||
textarea.contentEditable = 'true';
|
||||
textarea.focus();
|
||||
|
||||
if (win.document.execCommand('paste')) {
|
||||
let value = textarea.textContent as string;
|
||||
let url = urls.searchUrl(value, search);
|
||||
browser.runtime.sendMessage({
|
||||
type: messages.OPEN_URL,
|
||||
url,
|
||||
newTab,
|
||||
});
|
||||
}
|
||||
|
||||
textarea.remove();
|
||||
};
|
||||
|
||||
export { yank, paste };
|
44
src/content/usecases/ClipboardUseCase.ts
Normal file
44
src/content/usecases/ClipboardUseCase.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import * as urls from '../../shared/urls';
|
||||
import ClipboardRepository, { ClipboardRepositoryImpl }
|
||||
from '../repositories/ClipboardRepository';
|
||||
import SettingRepository, { SettingRepositoryImpl }
|
||||
from '../repositories/SettingRepository';
|
||||
import TabsClient, { TabsClientImpl }
|
||||
from '../client/TabsClient';
|
||||
import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient';
|
||||
|
||||
export default class ClipboardUseCase {
|
||||
private repository: ClipboardRepository;
|
||||
|
||||
private settingRepository: SettingRepository;
|
||||
|
||||
private client: TabsClient;
|
||||
|
||||
private consoleClient: ConsoleClient;
|
||||
|
||||
constructor({
|
||||
repository = new ClipboardRepositoryImpl(),
|
||||
settingRepository = new SettingRepositoryImpl(),
|
||||
client = new TabsClientImpl(),
|
||||
consoleClient = new ConsoleClientImpl(),
|
||||
} = {}) {
|
||||
this.repository = repository;
|
||||
this.settingRepository = settingRepository;
|
||||
this.client = client;
|
||||
this.consoleClient = consoleClient;
|
||||
}
|
||||
|
||||
async yankCurrentURL(): Promise<string> {
|
||||
let url = window.location.href;
|
||||
this.repository.write(url);
|
||||
await this.consoleClient.info('Yanked ' + url);
|
||||
return Promise.resolve(url);
|
||||
}
|
||||
|
||||
async openOrSearch(newTab: boolean): Promise<void> {
|
||||
let search = this.settingRepository.get().search;
|
||||
let text = this.repository.read();
|
||||
let url = urls.searchUrl(text, search);
|
||||
await this.client.openUrl(url, newTab);
|
||||
}
|
||||
}
|
Reference in a new issue