diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-11 17:45:58 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-11 17:45:58 +0900 |
commit | 8cef5981b808bc1713170627c88dc26ca81063c1 (patch) | |
tree | 6e1af4a4888bfcd9dcb64df00438ad97c5e5c392 /src/content/repositories | |
parent | c6288f19d93a05f96274dd172450b8350389c39f (diff) |
Clipbaord as a clean architecture
Diffstat (limited to 'src/content/repositories')
-rw-r--r-- | src/content/repositories/ClipboardRepository.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/content/repositories/ClipboardRepository.ts b/src/content/repositories/ClipboardRepository.ts new file mode 100644 index 0000000..747ae6a --- /dev/null +++ b/src/content/repositories/ClipboardRepository.ts @@ -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'); + } + } +} |