-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.html
197 lines (180 loc) · 7.85 KB
/
index.html
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>yux-storage测试用例</title>
<style>
body {
padding: 0 0.5em 100px;
background: #fff;
}
pre {
padding: 12px 1em;
margin: 1em 0;
background-color: #f5f7f9;
font-family: Consolas, monospace;
font-size: 100%;
white-space: pre-wrap;
word-wrap: break-word;
border-radius: 4px;
box-shadow: inset 1px 0 #e3e5e8, inset 0 1px #e3e5e8, inset -1px 0 #eaf0f5, inset 0 -1px #eaf0f5;
tab-size: 2;
}
blockquote {
font-family: 'kaiti', 'STKaiti';
border-left: solid silver;
background-color: #ffffe0;
padding: 0.5em 1em;
background-color: beige;
margin: 0;
}
button,input {
margin: 5px;
}
p {
font-size: 12px;
}
input[type="file" i]{
outline: 0;
}
input[type="file" i]::-webkit-file-upload-button{
display: none;
}
</style>
<link rel="stylesheet" href="https://qidian.gtimg.com/lulu/pure/css/common/ui/LightTip.css">
<link rel="stylesheet" href="https://qidian.gtimg.com/lulu/pure/css/common/ui/Button.css">
<script src="https://qidian.gtimg.com/lulu/pure/js/common/ui/LightTip.js"></script>
</head>
<body>
<h1>yux-storage</h1>
<blockquote>推荐打开控制台查看</blockquote>
<h2>getItem</h2>
<pre>yuxStorage.getItem('A').then(res => {
console.log(result)
});</pre>
<button class="ui-button" data-type="success" onclick="gets('A')">获取key:A</button>
<button class="ui-button" data-type="success" onclick="gets('B')">获取key:B</button>
<button class="ui-button" data-type="success" onclick="gets(['A','B'])">获取key:['A','B']</button>
<h2>setItem</h2>
<pre>yuxStorage.setItem(key, value)</pre>
<button class="ui-button" data-type="primary" onclick="sets('A', 1)">设置 A = 1</button>
<button class="ui-button" data-type="primary" onclick="sets('B', 2)">设置 B = 2</button>
<p>测试键为数组或者数字</p>
<button class="ui-button" data-type="primary" onclick="sets(['A','B'], 3)">设置 ['A','B'] = 3</button>
<button class="ui-button" data-type="primary" onclick="sets(1, 4)">设置 1 = 4</button>
<p>测试值为非字符串</p>
<button class="ui-button" data-type="primary" onclick="sets('C', [1,2])">设置 C = [1,2]</button>
<button class="ui-button" data-type="primary" onclick="sets('C', {name: 'xboxyan'})">设置 C = {name: 'xboxyan'}</button>
<button class="ui-button" data-type="primary" onclick="sets('C', Date())">设置 C = Date()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Blob())">设置 C = new Blob()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new ArrayBuffer())">设置 C = new ArrayBuffer()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new RegExp())">设置 C = new RegExp()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Float32Array())">设置 C = new Float32Array()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Int16Array())">设置 C = new Int16Array()</button>
<button class="ui-button" data-type="primary" onclick="sets('C', new Uint16Array())">设置 C = new Uint16Array()</button>
<p>测试值为随意上传的文件,不限大小</p>
<label class="ui-button" data-type="primary" id="label" for="file">选择文件</label>
<input id="file" type="file" onchange="sets('C', event.target.files[0])" />
<p>监听上面的值</p>
<pre id="pre_c"></pre>
<p>获取上面的值</p>
<button class="ui-button" data-type="success" onclick="gets('C')">获取key:C</button>
<h2>removeItem</h2>
<pre>yuxStorage.removeItem(key)</pre>
<button class="ui-button" data-type="warning" onclick="removes('A')">删除 A</button>
<button class="ui-button" data-type="warning" onclick="removes('B')">删除 B</button>
<h2>key</h2>
<pre>yuxStorage.key(keyIndex)</pre>
<button class="ui-button" data-type="success" onclick="key(0)">第1个键</button>
<button class="ui-button" data-type="success" onclick="key(1)">第2个键</button>
<button class="ui-button" data-type="success" onclick="key(2)">第3个键</button>
<button class="ui-button" data-type="success" onclick="key(3)">第4个键</button>
<button class="ui-button" data-type="success" onclick="key(4)">第5个键</button>
<h2>keys</h2>
<pre>yuxStorage.keys().then(names => {
console.log(result)
};</pre>
<button class="ui-button" data-type="success" onclick="keys()">所有键</button>
<h2>clear</h2>
<pre>yuxStorage.clear()</pre>
<button class="ui-button" data-type="danger" onclick="clearStorage()">!清空全部数据</button>
<h2>错误捕获</h2>
<pre>// 回调函数
yuxStorage.getItem({},function(err,value){
if(err){
console.log(err)
}else{
console.log(value)
}
});
// promise
yuxStorage.getItem(key).then(value => {
console.log(value)
}).catch(err => console.log(err))</pre>
<p>参数不合法时会报错</p>
<button class="ui-button" data-type="success" onclick="gets({})">获取key:{}</button>
<button class="ui-button" data-type="primary" onclick="sets('M', function(){})">设置 M = function(){}</button>
<script type="module">
import yuxStorage, { YuxDB } from './yux-storage.js'
window.gets = (key) => {
yuxStorage.getItem(key).then(res => {
console.log(key, '=>', res)
new LightTip().success(JSON.stringify(key)+': '+Object.prototype.toString.call(res)+ ' '+JSON.stringify(res));
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
window.sets = (key, value) => {
yuxStorage.setItem(key, value).then(res => {
console.log(key, '设置完成')
new LightTip().success('设置完成');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
window.removes = (key) => {
yuxStorage.removeItem(key).then(res => {
console.log(key, '已被移除')
new LightTip().success(JSON.stringify(key)+'已被移除');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
window.clearStorage = () => {
yuxStorage.clear().then(res => {
console.log('数据已清空')
new LightTip().success('数据已清空');
}).catch(err => {
console.log(err)
new LightTip().error(JSON.stringify(err));
})
}
window.key = (index) => {
yuxStorage.key(index).then(res => {
console.log(index, '=>', res)
new LightTip().success(`第${index}个键: `+JSON.stringify(res));
})
}
window.keys = () => {
yuxStorage.keys().then(res => {
console.log('全部键', res)
new LightTip().success('全部键: '+JSON.stringify(res));
}).catch(err => console.log(err))
}
yuxStorage.addEventListener((type, data, projectName) => {
console.log(type, data, projectName)
pre_c.innerHTML = JSON.stringify(data)
})
document.addEventListener('yuxStorage', ev => {
const [type, data, projectName] = ev.detail;
console.log(type, data, projectName)
})
const MyStorage = new YuxDB('自定义')
MyStorage.setItem('xxx', '我是test2022')
</script>
</body>
</html>