forked from junohamburg/kirby-visual-block-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (166 loc) · 4.84 KB
/
Copy pathindex.js
File metadata and controls
182 lines (166 loc) · 4.84 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
180
181
182
const visualBlockStore = Vue.observable({
blockTypes: [],
images: {},
isLoading: false,
hasFetched: false,
error: null,
});
function loadImage(url) {
return new Promise((resolve) => {
const image = new Image();
image.onload = () => resolve(image);
image.onerror = () => resolve(null); // Resolve with null on error
image.src = url;
});
}
async function fetchAndLoadVisualBlockData(api) {
const store = visualBlockStore;
if (!api) {
store.error = "Internal error: API not available.";
return;
}
if (store.hasFetched || store.isLoading) {
return;
}
if (!window.panel || !window.panel.user || window.panel.user.id === null) {
return;
}
store.isLoading = true;
store.error = null;
try {
const apiResponse = await api.get("visual-block-selector");
const blockTypes = Object.keys(apiResponse);
const images = {};
store.blockTypes = blockTypes;
const loadImagePromises = Object.entries(apiResponse).map(
async ([name, url]) => {
const img = await loadImage(url);
images[name] = img; // Store image object or null
},
);
await Promise.all(loadImagePromises);
store.images = images;
store.hasFetched = true;
} catch (error) {
store.error = "Failed to load visual block data.";
} finally {
store.isLoading = false;
}
}
panel.plugin("junohamburg/visual-block-selector", {
created(VueInstance) {
fetchAndLoadVisualBlockData(VueInstance.$api);
},
components: {
"k-block-selector": {
extends: "k-block-selector",
template: `
<k-dialog
:cancel-button="false"
:size="showVisualBlockSelector ? 'visual' : 'medium'"
:submit-button="false"
:visible="true"
class="k-block-selector"
@cancel="$emit('cancel')"
@submit="$emit('submit', value)"
>
<k-headline v-if="headline">
{{ headline }}
</k-headline>
<details
v-for="(group, groupName) in groups"
:key="groupName"
:open="group.open"
>
<summary>{{ group.label }}</summary>
<k-navigate
v-if="showVisualBlockSelector"
class="k-block-types"
>
<button
class="k-block-selector-button"
v-for="fieldset in group.fieldsets"
:key="fieldset.name"
:disabled="disabledFieldsets.includes(fieldset.type)"
:aria-disabled="disabledFieldsets.includes(fieldset.type)"
@click="$emit('submit', fieldset.type)"
@focus.native="$emit('input', fieldset.type)"
>
<span class="k-block-selector-button-preview">
<img v-if="previewImages[fieldset.type]" :src="previewImages[fieldset.type].src" alt="" />
<k-icon v-else :type="fieldset.icon || 'box'" />
</span>
<span class="k-block-selector-button-text">{{ fieldset.name }}</span>
</button>
</k-navigate>
<k-navigate
v-else
class="k-block-types"
>
<k-button
v-for="fieldset in group.fieldsets"
:key="fieldset.name"
:disabled="disabledFieldsets.includes(fieldset.type)"
:icon="fieldset.icon ?? 'box'"
:text="fieldset.name"
size="lg"
@click="$emit('submit', fieldset.type)"
@focus.native="$emit('input', fieldset.type)"
/>
</k-navigate>
</details>
<!-- eslint-disable vue/no-v-html -->
<p
class="k-clipboard-hint"
v-html="$t('field.blocks.fieldsets.paste', { shortcut })"
/>
<!-- eslint-enable -->
</k-dialog>
`,
created() {
fetchAndLoadVisualBlockData(this.$api);
},
computed: {
isLoading() {
return visualBlockStore.isLoading;
},
fetchError() {
return visualBlockStore.error;
},
previewImages() {
return visualBlockStore.images;
},
showVisualBlockSelector() {
const store = visualBlockStore;
if (store.isLoading || !store.hasFetched || store.error) {
return false;
}
const availableVisualTypes = store.blockTypes;
if (!availableVisualTypes || availableVisualTypes.length === 0) {
return false;
}
for (const group of Object.values(this.groups)) {
for (const fieldset of group.fieldsets) {
if (availableVisualTypes.includes(fieldset.type)) {
return true;
}
}
}
return false;
},
},
methods: {
isDisabled(type) {
return this.disabledFieldsets.includes(type);
},
submit(type) {
this.$emit("submit", type);
},
getImageSrc(type) {
const image = visualBlockStore.images[type];
return image?.src ?? null;
},
},
},
},
});