Skip to content

Commit b5544b1

Browse files
LightLight
authored andcommitted
feat(functions): 将附件上传后提示框调整为填写 Logo 地址
1 parent d86ae5a commit b5544b1

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/functions.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ function logoPreview() {
248248
static $a = 0;
249249
if ($a == 0) {
250250
$defaultTip = _t('请输入 Logo 的 URL');
251+
$logoTitle = _t('填写 Logo');
252+
$logoDes = _t('请在下方的输入框内输入要填写的 Logo 地址');
253+
$okText = _t('确定');
254+
$cancelText = _t('取消');
251255
echo <<<EOD
252256
<style>
253257
.webstack-theme-preview-span {
@@ -259,6 +263,15 @@ function logoPreview() {
259263
max-height: 54px;
260264
border-radius: 50%;
261265
}
266+
.webstack-wmd-prompt {
267+
position: absolute;
268+
top: 0px;
269+
z-index: 1000;
270+
opacity: 0.5;
271+
height: 100%;
272+
left: 0px;
273+
width: 100%;
274+
}
262275
</style>
263276
<script type="text/javascript">
264277
function webstackImageHandler(imgDom, loadCallback, errorCallback) {
@@ -279,6 +292,51 @@ function webstackImageHandler(imgDom, loadCallback, errorCallback) {
279292
};
280293
}
281294
}
295+
function webstackCreateDialog(filename, url, okCallback, cancelCallback) {
296+
const dialogDiv = document.createElement('div');
297+
dialogDiv.className = 'wmd-prompt-dialog';
298+
dialogDiv.setAttribute('role', 'dialog');
299+
const textDiv = document.createElement('div');
300+
textDiv.innerHTML = '<p><b>$logoTitle (' + filename + ')</b></p><p>$logoDes</p>';
301+
const logoForm = document.createElement('form');
302+
const textInput = document.createElement('input');
303+
textInput.type = 'text';
304+
textInput.value = url;
305+
const okBtn = document.createElement('button');
306+
okBtn.type = 'button';
307+
okBtn.className = 'btn btn-s primary';
308+
okBtn.textContent = '$okText';
309+
okBtn.addEventListener('click', e => {
310+
e.stopPropagation();
311+
if (typeof okCallback === 'function') {
312+
okCallback(textInput);
313+
}
314+
dialogDiv && dialogDiv.remove();
315+
});
316+
const cancelBtn = document.createElement('button');
317+
cancelBtn.type = 'button';
318+
cancelBtn.className = 'btn btn-s';
319+
cancelBtn.textContent = '$cancelText';
320+
cancelBtn.addEventListener('click', e => {
321+
e.stopPropagation();
322+
if (typeof cancelCallback === 'function') {
323+
cancelCallback();
324+
}
325+
dialogDiv && dialogDiv.remove();
326+
})
327+
logoForm.appendChild(textInput);
328+
logoForm.appendChild(okBtn);
329+
logoForm.appendChild(cancelBtn);
330+
dialogDiv.appendChild(textDiv);
331+
dialogDiv.appendChild(logoForm);
332+
return dialogDiv;
333+
}
334+
function webstackCreatePrompt() {
335+
const prompt = document.createElement('div');
336+
prompt.className = 'wmd-prompt-background webstack-wmd-prompt';
337+
prompt.style.height = document.body.clientHeight + 'px';
338+
return prompt;
339+
}
282340
window.onload = function() {
283341
const logoInput = document.querySelector('input[name="fields[logo]"]');
284342
if (logoInput) {
@@ -325,6 +383,39 @@ function webstackImageHandler(imgDom, loadCallback, errorCallback) {
325383
p.textContent = defaultTip;
326384
}
327385
});
386+
387+
if (window.Typecho && window.Typecho.insertFileToEditor) {
388+
window.Typecho.insertFileToEditor = function(file, url, _isImage) {
389+
const prompt = webstackCreatePrompt();
390+
const dialog = webstackCreateDialog(file, url, textInput => {
391+
logoInput.value = textInput.value;
392+
img.src = logoInput.value;
393+
if (logoInput.value.length === 0) {
394+
img.style.display = 'none';
395+
p.textContent = defaultTip;
396+
} else {
397+
img.style.display = 'block';
398+
webstackImageHandler(
399+
img,
400+
imgDom => {
401+
p.textContent = '( ' + imgDom.naturalWidth + ' x ' + imgDom.naturalHeight + ' )';
402+
},
403+
() => {
404+
img.style.display = 'none';
405+
p.textContent = defaultTip;
406+
}
407+
);
408+
}
409+
prompt && prompt.remove();
410+
}, () => {
411+
prompt && prompt.remove();
412+
});
413+
setTimeout(() => {
414+
document.body.appendChild(prompt);
415+
document.body.appendChild(dialog);
416+
}, 0);
417+
}
418+
}
328419
}
329420
};
330421
</script>

0 commit comments

Comments
 (0)