-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.html
271 lines (259 loc) · 9.92 KB
/
clipboard.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html>
<html lang="en">
<head>
<title>Clipboard</title>
<link rel="icon" href="/favicon-32x32.png" type="image/gif" sizes="32x32">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<!-- // App -->
<div id="app">
<v-app v-cloak>
<!-- // App Bar -->
<v-app-bar app>
<v-toolbar-title class="my-2 ml-md-8">
<v-img width="80px" src="/logo.png" />
</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn @click='toggleTheme' class="mr-md-4" icon>
<v-icon>mdi-lightbulb-on</v-icon>
</v-btn>
</v-app-bar>
<!-- // Main -->
<v-main>
<v-breadcrumbs class="ml-md-8" :items="[{text: 'Home', href: '/'}, {text: 'Clipboard', disabled: true}]">
</v-breadcrumbs>
<v-container>
<!-- // List of key value -->
<v-card v-if='labelContentArr.length > 0' class="mx-auto" max-width='1000'>
<v-card-text>
<template v-for='(item, index) in labelContentArr'>
<v-row draggable="true" @drag='drag(index)' @dragover='dragover(index)' @dragend='dragend'
class="px-4 py-3" :key="`item-${item.ts}`" no-gutters>
<!-- // Label -->
<v-col cols='12' md='2' class="font-weight-bold">
<label role="button" @click='copyToClipboard(item.label)'>{{ item.label }}</label>
</v-col>
<!-- // Content -->
<v-col offset-md='1' cols='10' md='7'>
<label role="button" @click='copyToClipboard(item.content)'>{{ item.content }}</label>
</v-col>
<!-- // Actions -->
<v-col offset-md='1' cols='2' md='1'>
<v-sheet class="transparent d-flex justify-end">
<v-icon small @click="deleteFromList(index)" class="mr-1" title='Delete'>mdi-delete</v-icon>
</v-sheet>
</v-col>
</v-row>
<v-divider />
</template>
<!-- // Instructions -->
<v-row class="pt-8 ml-4 font-italic info--text" no-gutters>
Click any label / value to copy to clipboard... Drag row to move up / down.
</v-row>
</v-card-text>
</v-card>
<!-- // New key value -->
<v-card :class="{'px-4 mx-auto': true, 'mt-4': labelContentArr.length > 0}" max-width='1000'>
<v-card-text>
<v-row no-gutters :align="'baseline'">
<v-col cols='12' md='3'>
<v-text-field autofocus ref='tempLabel' label="Label" v-model.trim='tempLabel' />
</v-col>
<v-col cols='12' md='6' offset-md='1'>
<v-text-field label="Value" v-model.trim='tempContent' @keyup.enter="addToList" />
</v-col>
<v-col cols='12' md='2' class="text-right">
<v-btn color='primary' @click='addToList' small>Add To List</v-btn>
</v-col>
</v-row>
</v-card-text>
</v-card>
<!-- // Temp textbox to copy text to clipboard -->
<input type="text" ref='clipBoardInput' v-show='showTempInput' v-model.trim='textToCopy' />
<!-- // Snackbar -->
<v-snackbar v-model="snackbar" :timeout="1500">{{ message }}</v-snackbar>
</v-container>
</v-main>
</v-app>
</div>
<!-- // JS Code -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.js"></script>
<script>
// Get array of label-content from storage
function getLabelContentArrayFromStorage() {
// To be returned
let labelContentArr = []
// Iterate on each key
Object.keys(localStorage).forEach(key => {
try {
let tempObj = { label: key }
Object.assign(tempObj, JSON.parse(localStorage.getItem(key)))
// Push to array
if (tempObj.ts) labelContentArr.push(tempObj)
} catch (e) { }
})
// Sort by ts
return labelContentArr.sort((x, y) => x.ts - y.ts)
}
// Mount App
new Vue({
el: '#app',
vuetify: new Vuetify({
theme: {
dark: localStorage.getItem('lightTheme') != 'Yes'
}
}),
data() {
return {
tempLabel: '',
tempContent: '',
// {label, content, ts}
labelContentArr: getLabelContentArrayFromStorage(),
// For copy to clipboard
textToCopy: '',
showTempInput: false,
snackbar: false,
message: '',
// Drag and drop
dragIndex: null,
dragoverIndex: null
}
},
methods: {
// Add key-value to list
addToList() {
// Do not allow empty label
if (this.tempLabel == '') return
// If label already exists
const lcIndex = this.labelContentArr.findIndex(({ label }) => this.tempLabel == label)
if (lcIndex > -1) {
const confirmVal = confirm(`Are you sure you want to replace value of '${this.tempLabel}'?`)
// Cancel next steps
if (!confirmVal) return
// Replace existing item
this.$set(this.labelContentArr[lcIndex], 'label', this.tempLabel)
this.$set(this.labelContentArr[lcIndex], 'content', this.tempContent)
} else {
// Save new item to list
this.labelContentArr.push({
label: this.tempLabel,
content: this.tempContent,
ts: Date.now()
})
}
// Reset values and focus to key input
this.tempLabel = ''
this.tempContent = ''
this.$refs.tempLabel.focus()
},
// Delete pair
deleteFromList(index) {
// Find item
const label = this.labelContentArr[index].label
const confirmVal = confirm(`Are you sure you want to delete '${label}'?`)
if (confirmVal) this.$delete(this.labelContentArr, index)
},
// On Drag
drag(index) {
this.dragIndex = index
},
// On Dragover
dragover(index) {
this.dragoverIndex = index
if (this.dragIndex != this.dragoverIndex)
this.swap(this.dragIndex, this.dragoverIndex)
},
// On Dragend
dragend() {
// Sorted timestamps
let tsArr = this.labelContentArr.map(x => x.ts).sort((x, y) => x - y)
// Assign timestamp to each element
this.labelContentArr.forEach((item, i) => {
this.$set(this.labelContentArr[i], 'ts', tsArr[i])
})
},
// Swap elements
swap(index1, index2) {
let tempObj = this.labelContentArr[index1]
this.$set(this.labelContentArr, index1, this.labelContentArr[index2])
this.$set(this.labelContentArr, index2, tempObj)
},
// Copy to clipboard
copyToClipboard(text) {
// Shoe temp input
this.showTempInput = true
this.textToCopy = text
// On next tick
this.$nextTick(() => {
// Copy value of textbox to clipboard
this.$refs.clipBoardInput.select()
document.execCommand("copy")
// Hide temp input
this.showTempInput = false
})
// Show message
this.showMessage(`Copied to clipboard "${text}"`)
},
// Show message
showMessage(message) {
this.message = message
this.snackbar = true
},
// Toggle theme
toggleTheme() {
this.$vuetify.theme.dark = !this.$vuetify.theme.dark
localStorage.setItem('lightTheme', this.$vuetify.theme.dark ? 'No' : 'Yes')
}
},
computed: {
// To watch element added or removed
labelArr() { return this.labelContentArr.map(lc => lc.label) },
// To watch value updated or not
contentArr() { return this.labelContentArr.map(lc => lc.content).sort() },
// To watch whether elements swapped or not
tsArr() { return this.labelContentArr.map(lc => lc.ts) },
},
watch: {
// Add / Remove item from local storage
labelArr(to, from) {
if (to.length > from.length) {
// Get newly added label-content and save to localstorage
let { label, content, ts } = this.labelContentArr.filter(lc => !from.includes(lc.label))[0]
localStorage.setItem(label, JSON.stringify({ content, ts }))
} else if (to.length < from.length) {
// Get removed label and remove from localstorage
let removedLabel = from.filter(label => !to.includes(label))[0]
localStorage.removeItem(removedLabel)
}
},
// Update items in localstorage
tsArr(to, from) {
if (to.length == from.length) {
this.labelContentArr.forEach(({ label, content, ts }) => {
localStorage.setItem(label, JSON.stringify({ content, ts }))
})
}
},
// Update items in localstorage
contentArr(to, from) {
if (to.length == from.length && to.join('-') != from.join('-')) {
this.labelContentArr.forEach(({ label, content, ts }) => {
localStorage.setItem(label, JSON.stringify({ content, ts }))
})
}
}
}
})
</script>
</body>
</html>