forked from frappe/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackgroundHandler.vue
More file actions
179 lines (164 loc) · 5.95 KB
/
BackgroundHandler.vue
File metadata and controls
179 lines (164 loc) · 5.95 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
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
<template>
<Popover placement="left" class="!block w-full" popoverClass="!min-w-fit !mr-[30px]">
<template #target="{ togglePopover }">
<div class="flex w-full items-center justify-between">
<PropertyControl
styleProperty="backgroundImage"
:component="Input"
label="BG Image"
:enableStates="false"
:allowDynamicValue="true"
placeholder="Set Background"
@focus="togglePopover"
:modelValue="backgroundImage"
@update:modelValue="setBGImageURL">
<template #prefix>
<div
class="absolute left-2 top-[6px] z-10 h-4 w-4 cursor-pointer rounded shadow-sm"
@click="togglePopover"
:class="{ 'bg-surface-gray-4': !Boolean(backgroundImage) }"
:style="previewStyle" />
</template>
</PropertyControl>
</div>
</template>
<template #body>
<div class="rounded-lg bg-surface-white p-3 shadow-lg">
<div
class="image-preview group relative h-24 w-48 cursor-pointer overflow-hidden rounded bg-surface-gray-3"
:style="previewStyle">
<FileUploader
@success="setBGImage"
:uploadArgs="{
private: false,
folder: 'Home/Builder Uploads',
optimize: true,
upload_endpoint: '/api/method/builder.api.upload_builder_asset',
}">
<template v-slot="{ openFileSelector }">
<div
class="absolute bottom-0 left-0 right-0 top-0 hidden place-items-center bg-gray-500 bg-opacity-20"
:class="{
'!grid': !backgroundImage,
'group-hover:grid': backgroundImage,
}">
<BuilderButton @click="openFileSelector">Upload</BuilderButton>
</div>
</template>
</FileUploader>
</div>
<div class="mt-4 space-y-2">
<InlineInput
label="Size"
:modelValue="backgroundSize"
type="select"
:options="sizeOptions"
@update:modelValue="setBGSize" />
<InlineInput
label="Position"
:modelValue="backgroundPosition"
type="select"
:options="positionOptions"
@update:modelValue="setBGPosition" />
<InlineInput
label="Repeat"
:modelValue="backgroundRepeat"
type="select"
:options="repeatOptions"
@update:modelValue="setBGRepeat" />
</div>
<BuilderButton v-if="showServeLocallyButton" class="mt-3 w-full" @click="serveBackgroundImageLocally">
{{ serveLocallyButtonText }}
</BuilderButton>
<BuilderButton v-if="backgroundImage" class="mt-3 w-full" variant="subtle" @click="clearBGImage">
Clear Image
</BuilderButton>
</div>
</template>
</Popover>
<PropertyControl label="BG Color" styleProperty="backgroundColor" :component="ColorInput" />
</template>
<script lang="ts" setup>
import ColorInput from "@/components/Controls/ColorInput.vue";
import InlineInput from "@/components/Controls/InlineInput.vue";
import Input from "@/components/Controls/Input.vue";
import PropertyControl from "@/components/Controls/PropertyControl.vue";
import blockController from "@/utils/blockController";
import { getOptimizeButtonText, optimizeImage, shouldShowOptimizeButton } from "@/utils/imageUtils";
import { FileUploader, Popover } from "frappe-ui";
import { computed } from "vue";
const backgroundImage = computed(() => {
const bgImage = blockController.getStyle("backgroundImage") as string;
return bgImage ? bgImage.replace(/^url\(['"]?|['"]?\)$/g, "") : null;
});
const backgroundSize = computed(() => blockController.getStyle("backgroundSize") as string);
const backgroundPosition = computed(() => blockController.getStyle("backgroundPosition") as string);
const backgroundRepeat = computed(() => blockController.getStyle("backgroundRepeat") as string);
const previewStyle = computed(() => ({
backgroundImage: (backgroundImage.value ? `url(${backgroundImage.value})` : "") as string,
backgroundPosition: backgroundPosition.value,
backgroundSize: backgroundSize.value,
backgroundRepeat: backgroundRepeat.value,
}));
const sizeOptions = [
{ label: "Contain", value: "contain" },
{ label: "Cover", value: "cover" },
{ label: "Auto", value: "auto" },
];
const positionOptions = [
{ label: "Center", value: "center" },
{ label: "Top", value: "top" },
{ label: "Bottom", value: "bottom" },
{ label: "Left", value: "left" },
{ label: "Right", value: "right" },
];
const repeatOptions = [
{ label: "No Repeat", value: "no-repeat" },
{ label: "Repeat", value: "repeat" },
{ label: "Repeat X", value: "repeat-x" },
{ label: "Repeat Y", value: "repeat-y" },
];
const setBGImage = (file: { file_url: string }) => {
blockController.setStyle("backgroundImage", `url(${file.file_url})`);
if (!blockController.getStyle("backgroundSize")) {
blockController.setStyle("backgroundSize", "cover");
}
if (!blockController.getStyle("backgroundPosition")) {
blockController.setStyle("backgroundPosition", "center");
}
if (!blockController.getStyle("backgroundRepeat")) {
blockController.setStyle("backgroundRepeat", "no-repeat");
}
};
const setBGImageURL = (url: string) => {
blockController.setStyle("backgroundImage", url ? `url(${url})` : null);
};
const setBGSize = (value: string) => {
blockController.setStyle("backgroundSize", value);
};
const setBGPosition = (value: string) => {
blockController.setStyle("backgroundPosition", value);
};
const setBGRepeat = (value: string) => {
blockController.setStyle("backgroundRepeat", value);
};
const clearBGImage = () => {
blockController.setStyle("backgroundImage", null);
blockController.setStyle("backgroundSize", null);
blockController.setStyle("backgroundPosition", null);
blockController.setStyle("backgroundRepeat", null);
};
const showServeLocallyButton = computed(() => shouldShowOptimizeButton(backgroundImage.value));
const serveLocallyButtonText = computed(() => getOptimizeButtonText(backgroundImage.value));
const serveBackgroundImageLocally = () => {
if (!backgroundImage.value) {
return;
}
return optimizeImage({
imageUrl: backgroundImage.value,
onSuccess: (newUrl: string) => {
blockController.setStyle("backgroundImage", `url(${newUrl})`);
},
});
};
</script>