-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathpadding.js
More file actions
121 lines (100 loc) · 3.34 KB
/
padding.js
File metadata and controls
121 lines (100 loc) · 3.34 KB
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
import hotkeys from 'hotkeys-js'
import { metaKey, getStyle, getSide, showHideSelected } from '../utilities/'
const key_events = 'up,down,left,right'
.split(',')
.reduce((events, event) =>
`${events},${event},alt+${event},shift+${event},shift+alt+${event}`
, '')
.substring(1)
const command_events = `${metaKey}+up,${metaKey}+shift+up,${metaKey}+down,${metaKey}+shift+down`
export function Padding(visbug) {
hotkeys(key_events, (e, handler) => {
if (e.cancelBubble) return
e.preventDefault()
padElement(visbug.selection(), handler.key)
})
hotkeys(command_events, (e, handler) => {
e.preventDefault()
padAllElementSides(visbug.selection(), handler.key)
})
visbug.onSelectedUpdate(paintBackgrounds)
return () => {
hotkeys.unbind(key_events)
hotkeys.unbind(command_events)
hotkeys.unbind('up,down,left,right') // bug in lib?
visbug.removeSelectedCallback(paintBackgrounds)
removeBackgrounds(visbug.selection())
}
}
export function padElement(els, direction) {
els
.map(el => showHideSelected(el))
.map(el => ({
el,
style: 'padding' + getSide(direction),
current: parseInt(getStyle(el, 'padding' + getSide(direction)), 10),
amount: direction.split('+').includes('shift') ? 10 : 1,
negative: direction.split('+').includes('alt'),
}))
.map(payload =>
Object.assign(payload, {
padding: payload.negative
? payload.current - payload.amount
: payload.current + payload.amount
}))
.forEach(({el, style, padding}) =>
el.style[style] = `${padding < 0 ? 0 : padding}px`)
}
export function padAllElementSides(els, keycommand) {
const combo = keycommand.split('+')
let spoof = ''
if (combo.includes('shift')) spoof = 'shift+' + spoof
if (combo.includes('down')) spoof = 'alt+' + spoof
'up,down,left,right'.split(',')
.forEach(side => padElement(els, spoof + side))
}
function paintBackgrounds(els) {
els.forEach(el => {
const label_id = el.getAttribute('data-label-id')
document
.querySelector(`visbug-handles[data-label-id="${label_id}"]`)
.backdrop = {
element: createPaddingVisual(el),
update: createPaddingVisual,
}
})
}
function removeBackgrounds(els) {
els.forEach(el => {
const label_id = el.getAttribute('data-label-id')
const boxmodel = document.querySelector(`visbug-handles[data-label-id="${label_id}"]`)
.$shadow.querySelector('visbug-boxmodel')
if (boxmodel) boxmodel.remove()
})
}
export function createPaddingVisual(el, hover = false) {
const bounds = el.getBoundingClientRect()
const calculatedStyle = getStyle(el, 'padding')
const boxdisplay = document.createElement('visbug-boxmodel')
if (calculatedStyle !== '0px') {
const sides = {
top: getStyle(el, 'paddingTop'),
right: getStyle(el, 'paddingRight'),
bottom: getStyle(el, 'paddingBottom'),
left: getStyle(el, 'paddingLeft'),
}
Object.entries(sides).forEach(([side, val]) => {
if (typeof val !== 'number')
val = parseInt(getStyle(el, 'padding'+'-'+side).slice(0, -2))
sides[side] = Math.round(val.toFixed(1) * 100) / 100
})
boxdisplay.position = {
mode: 'padding',
color: hover ? 'purple' : 'pink',
bounds,
sides,
element: el
}
}
return boxdisplay
}