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
|
/* eslint-disable no-undef */
;
(function (window, document) {
let pane
let main
let paneLeft
let cloak
// entry
onEvent('DOMContentLoaded', e => {
pane = qs('.sidebar')
main = qs('.main')
cloak = qs('#cloak')
paneLeft = pane.getBoundingClientRect().left
tryLoadMode()
tryRestoreSidebar()
activateSidebarToggle()
activateModeSwitcher()
activateSidebarDrag()
activateDialog()
}, 0, document)
// sidebar function dragging wrapper
function activateSidebarDrag () {
const state = {
last: false,
resizing: false,
resizable: false
}
function switchCursor () {
if (state.resizable) {
setStyle(document.body, { cursor: 'ew-resize' })
} else {
setStyle(document.body, { cursor: 'auto' })
}
}
function canResize (x, threshold = 4) {
return Math.abs(x - pane.getBoundingClientRect().right) <= threshold
}
function resize (x) {
let newWidth = x - paneLeft
if (newWidth <= 10) {
addClass(pane, 'hide')
} else {
adjustWidth(newWidth)
}
}
function cancelResize (e) {
state.resizing = false
let r = pane.getBoundingClientRect()
saveWidth(r.right - r.left)
}
function saveWidth (x) {
if (x <= 10) {
return
}
ls.set('sidebar_width', x)
}
onEvent('mousedown', e => {
if (state.resizable) {
state.resizing = true
}
}, 0)
onEvent('mouseup', cancelResize, 0)
onEvent('keydown', e => {
if (e.ctrlKey && e.keyCode === 66) {
toggleSidebar()
}
}, 0)
onEvent('touchstart', e => {
state.resizing = canResize(e.touches[0].clientX, 10)
})
onEvent('touchend', cancelResize)
onPointerMove(pointerMoveHandler, 50, qs('.middle'))
function pointerMoveHandler (e) {
state.resizable = canResize(e.clientX)
if (state.resizing) {
resize(e.clientX)
} else if (e.type === 'mousemove' && e.clientX > 10 && state.last !== state.resizable) {
switchCursor()
state.last = state.resizable
}
}
}
// sidebar dragging function end
// toggle and load sidebar width
function adjustWidth (x, unit = 'px') {
setStyle(pane, { width: x + 'px' })
setStyle(main, { marginLeft: x + 'px' })
}
function tryRestoreSidebar () {
let x = ls.get('sidebar_width')
adjustWidth(x)
let flag = ls.get('is_sidebar_hide')
flag === '1' ? addClass(pane, 'hide') : removeClass(pane, 'hide')
}
function activateSidebarToggle () {
onEvent('click', toggleSidebar, 0, '.toggle-sidebar')
}
function toggleSidebar (e) {
if (hasClass(pane, 'hide')) {
removeClass(pane, 'hide')
ls.set('is_sidebar_hide', '0')
} else {
addClass(pane, 'hide')
ls.set('is_sidebar_hide', '1')
}
e.preventDefault()
e.stopPropagation()
return false
}
// style mode functions
const toMode = name => {
if (!hasClass(document.body, name)) {
addClass(clearClass(document.body), name)
}
ls.set('mode', name)
}
function tryLoadMode () {
let mode = ls.get('mode')
mode && toMode(mode)
if (mode === 'wild-mode') {
let wildEle = elt(
'style',
{ class: 'wild-ele' },
ls.get('wild_style'))
document.body.appendChild(wildEle)
}
hideCloak()
}
function activateModeSwitcher () {
onEvent('click', e => {
e.preventDefault()
e.stopPropagation()
if (e.target.id === 'theme-switcher-button') {
return
}
if (hasClass(e.target, 'export-wild')) {
addClass('.dialog', 'show')
qs('#theme-name').focus()
return
}
let theme = e.target.dataset.theme
if (!theme) {
console.error('Theme name not found, probably missing an "Identifier" field in Config file.')
return
}
toMode(theme + '-mode')
if (theme === 'wild') {
const wildEle = qs('.wild-ele')
const style = wildStyle()
if (wildEle) {
wildEle.innerText = style
} else {
document.body.appendChild(elt('style', { class: 'wild-ele' }, style))
}
ls.set('wild_style', style)
}
}, 0, '#theme-switcher')
}
// function showCloak () {
// removeClass(cloak, 'hide')
// }
function hideCloak () {
addClass(cloak, 'hide')
}
function activateDialog () {
onEvent('click', e => {
removeClass('.dialog', 'show')
}, 0, '.close-dialog')
onEvent('click', e => {
const themeName = qs('#theme-name').value.trim()
if (!themeName) {
alert('Name is required.')
return
}
if (!/^[a-zA-Z\s-]+$/.test(themeName)) {
alert('Only English letters, space, hyphens are allowed.')
return
}
if (themeName === 'light' || themeName === 'dark' || themeName === 'wild') {
alert('Mess up builtin themes.')
return
}
exportWild(themeName)
removeClass('.dialog', 'show')
}, 0, '.export')
}
function exportWild (themeName) {
let content = generateText(themeName)
download(themeName + '.css', content)
}
function generateText (themeName) {
let style = ls.get('wild_style')
let identifier = themeName.replace(/\s/g, '-')
style = style.replace(/wild-mode/g, `${identifier}-mode`)
const content = `/*
Hugo Edidor theme wild mode exported.
homepage: https://github.com/jacobsun/edidor
Usage:
1. Throw me to your site root folder/static/css directory, you can rename me to whatever you like.
2. Copy the code below to your config file.
--------
[[menu.main]]
parent = "Theme"
identifier = "${identifier}"
name = "${themeName}"
url = "#"
weight = 30
--------
3. Change the field 'name' and 'weight' to whatever you like. Leave others alone, and don't add your custom field, hugo doesn't support that, though I really hope so :/.
*/
${style}`
return content
}
function download (filename, text) {
const a = elt('a', {
href: 'data:text/plain;charset=utf-8,' + encodeURIComponent(text),
download: filename,
style: {
display: 'none'
}
})
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
// Start file download.
})(window, document)
|