-
Notifications
You must be signed in to change notification settings - Fork 9
/
clipboard.js
53 lines (48 loc) · 1.16 KB
/
clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { LocalStorage } from './localstorage.js';
/**
* Represents a custom clipboard with a specific name
* which stores a value in localStorage under that name
*
* @category Internal
*/
class Clipboard {
/**
* Constructor
*
* @param {string} name - The name of the clipboard.
*/
constructor(name) {
this._name = name;
// we could pass the desired storage as a parameter to change to SessionStorage for example
this._storage = new LocalStorage();
}
/**
* Gets whether the clipboard is empty
*
* @type {boolean}
*/
get empty() {
return !this._storage.has(this._name);
}
/**
* Sets the value to be stored in the clipboard. Pass null to clear the value from storage.
*
* @type {object}
*/
set value(value) {
if (value !== null) {
this._storage.set(this._name, value);
} else {
this._storage.unset(this._name);
}
}
/**
* Gets the value stored in the clipboard.
*
* @type {object}
*/
get value() {
return this._storage.get(this._name);
}
}
export { Clipboard };