-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview-background.js
More file actions
126 lines (103 loc) · 3.67 KB
/
preview-background.js
File metadata and controls
126 lines (103 loc) · 3.67 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
122
123
124
125
126
const defaultLightPreviewBackgroundColor = '#ffffff'
const defaultDarkPreviewBackgroundColor = '#12141c'
const toHexChannel = value => value.toString(16).padStart(2, '0')
const normalizeColorToHex = colorValue => {
if (typeof colorValue !== 'string' || colorValue.length === 0) {
return ''
}
if (/^#[\da-f]{6}$/i.test(colorValue)) {
return colorValue.toLowerCase()
}
if (/^#[\da-f]{3}$/i.test(colorValue)) {
return colorValue
.slice(1)
.split('')
.map(channel => channel + channel)
.join('')
.replace(/^/, '#')
.toLowerCase()
}
const channels = colorValue.match(/\d+/g)
if (!channels || channels.length < 3) {
return ''
}
const [red, green, blue] = channels.slice(0, 3).map(value => Number.parseInt(value, 10))
if ([red, green, blue].some(value => Number.isNaN(value))) {
return ''
}
return `#${toHexChannel(red)}${toHexChannel(green)}${toHexChannel(blue)}`
}
export const createPreviewBackgroundController = ({
previewBgColorInput,
getPreviewHost,
getDefaultPreviewBackgroundColor,
onBackgroundColorChange,
}) => {
let previewBackgroundColor = null
let previewBackgroundCustomized = false
const resolveDefaultPreviewBackgroundColor = () => {
const themeDefaultPreviewBackgroundColor =
document.documentElement.dataset.theme === 'light'
? defaultLightPreviewBackgroundColor
: defaultDarkPreviewBackgroundColor
if (typeof getDefaultPreviewBackgroundColor === 'function') {
const configuredColor = getDefaultPreviewBackgroundColor()
const normalizedConfiguredColor = normalizeColorToHex(configuredColor)
if (normalizedConfiguredColor) {
return normalizedConfiguredColor
}
}
return themeDefaultPreviewBackgroundColor
}
const applyPreviewBackgroundColor = color => {
const previewHost = getPreviewHost()
if (!previewHost) {
return
}
if (typeof color === 'string' && color.length > 0) {
previewHost.style.backgroundColor = color
previewHost.style.setProperty('--preview-iframe-background-color', color)
if (typeof onBackgroundColorChange === 'function') {
onBackgroundColorChange(color)
}
return
}
previewHost.style.removeProperty('background-color')
previewHost.style.removeProperty('--preview-iframe-background-color')
if (typeof onBackgroundColorChange === 'function') {
onBackgroundColorChange('')
}
}
const syncPreviewBackgroundPickerFromTheme = () => {
const previewHost = getPreviewHost()
if (!previewBgColorInput || !previewHost || previewBackgroundCustomized) {
return
}
const defaultPreviewBackgroundColor = resolveDefaultPreviewBackgroundColor()
previewBackgroundColor = defaultPreviewBackgroundColor
previewBgColorInput.value = defaultPreviewBackgroundColor
applyPreviewBackgroundColor(defaultPreviewBackgroundColor)
}
const initializePreviewBackgroundPicker = () => {
const previewHost = getPreviewHost()
if (!previewBgColorInput || !previewHost) {
return
}
const initialColor = resolveDefaultPreviewBackgroundColor()
previewBackgroundColor = initialColor
previewBackgroundCustomized = false
previewBgColorInput.value = initialColor
applyPreviewBackgroundColor(initialColor)
previewBgColorInput.addEventListener('input', () => {
previewBackgroundColor = previewBgColorInput.value
previewBackgroundCustomized = true
applyPreviewBackgroundColor(previewBackgroundColor)
})
}
return {
applyPreviewBackgroundColor,
getPreviewBackgroundColor: () => previewBackgroundColor,
initializePreviewBackgroundPicker,
syncPreviewBackgroundPickerFromTheme,
}
}