Skip to content

Commit 3f678c0

Browse files
LightLight
authored andcommitted
feat(functions): 在编辑页面添加显示 Logo 预览
1 parent 6ae7ea1 commit 3f678c0

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

src/functions.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,94 @@ function themeFields($layout)
240240
$layout->addItem($url);
241241
$layout->addItem($text);
242242
$layout->addItem($logo);
243+
logoPreview();
244+
}
245+
246+
function logoPreview() {
247+
if (!isset($a)) {
248+
static $a = 0;
249+
if ($a == 0) {
250+
$defaultTip = _t('请输入 Logo 的 URL');
251+
echo <<<EOD
252+
<style>
253+
.webstack-theme-preview-span {
254+
float: right;
255+
margin-right: 1%;
256+
}
257+
.webstack-theme-preview-image {
258+
max-width: 54px;
259+
max-height: 54px;
260+
}
261+
</style>
262+
<script type="text/javascript">
263+
function webstackImageHandler(imgDom, loadCallback, errorCallback) {
264+
if (imgDom.complete) {
265+
if (typeof loadCallback === 'function') {
266+
loadCallback(imgDom);
267+
}
268+
} else {
269+
imgDom.onload = function () {
270+
if (typeof loadCallback === 'function') {
271+
loadCallback(imgDom);
272+
}
273+
};
274+
imgDom.onerror = function () {
275+
if (typeof errorCallback === 'function') {
276+
errorCallback();
277+
}
278+
};
279+
}
280+
}
281+
window.onload = function() {
282+
const logoInput = document.querySelector('input[name="fields[logo]"]');
283+
if (logoInput) {
284+
const defaultTip = "$defaultTip";
285+
const previewSpan = document.createElement('span');
286+
previewSpan.className = 'webstack-theme-preview-span';
287+
const img = document.createElement('img');
288+
img.className = 'webstack-theme-preview-image';
289+
img.src = logoInput.value;
290+
const p = logoInput.parentNode.querySelector('.description');
291+
if (logoInput.value.length === 0) {
292+
img.style.display = 'none';
293+
} else {
294+
webstackImageHandler(
295+
img,
296+
imgDom => {
297+
p.textContent = '( ' + imgDom.naturalWidth + ' x ' + imgDom.naturalHeight + ' )';
298+
},
299+
() => {
300+
img.style.display = 'none';
301+
p.textContent = defaultTip;
302+
}
303+
);
304+
}
305+
previewSpan.appendChild(img);
306+
logoInput.after(previewSpan);
307+
logoInput.addEventListener('change', e => {
308+
img.src = e.target.value;
309+
if (e.target.value) {
310+
img.style.display = 'block';
311+
webstackImageHandler(
312+
img,
313+
imgDom => {
314+
p.textContent = '( ' + imgDom.naturalWidth + ' x ' + imgDom.naturalHeight + ' )';
315+
},
316+
() => {
317+
img.style.display = 'none';
318+
p.textContent = defaultTip;
319+
}
320+
);
321+
} else {
322+
img.style.display = 'none';
323+
p.textContent = defaultTip;
324+
}
325+
});
326+
}
327+
};
328+
</script>
329+
EOD;
330+
}
331+
$a++;
332+
}
243333
}

0 commit comments

Comments
 (0)