136 lines
4.9 KiB
JavaScript
136 lines
4.9 KiB
JavaScript
/**
|
|
* Comment expand tools — zibll-style shortcode inserts ([g=] [code] [img=] quick).
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function findTextarea(from) {
|
|
var form = from && from.closest ? from.closest('form') : null;
|
|
if (form) {
|
|
return form.querySelector('textarea.grin, textarea#comment, textarea[name="comment"]');
|
|
}
|
|
return document.querySelector('#comment, textarea.grin');
|
|
}
|
|
|
|
function insertAtCursor(textarea, value) {
|
|
if (!textarea) return;
|
|
var start = textarea.selectionStart || 0;
|
|
var end = textarea.selectionEnd || 0;
|
|
var val = textarea.value || '';
|
|
textarea.value = val.slice(0, start) + value + val.slice(end);
|
|
var pos = start + value.length;
|
|
textarea.selectionStart = textarea.selectionEnd = pos;
|
|
textarea.focus();
|
|
textarea.dispatchEvent(new Event('input'));
|
|
}
|
|
|
|
function restUpload(file) {
|
|
var d = window.aksrtData || {};
|
|
var url = d.commentUploadUrl || ((d.restUrl || '/wp-json/aksrt/v1').replace(/\/$/, '') + '/comment/upload-image');
|
|
var fd = new FormData();
|
|
fd.append('image', file);
|
|
fd.append('file', file);
|
|
return fetch(url, {
|
|
method: 'POST',
|
|
headers: { 'X-WP-Nonce': d.restNonce || d.nonce || '' },
|
|
credentials: 'same-origin',
|
|
body: fd
|
|
}).then(function (r) { return r.json(); });
|
|
}
|
|
|
|
document.addEventListener('click', function (e) {
|
|
var sm = e.target && e.target.closest ? e.target.closest('[data-smilie], a.smilie-icon') : null;
|
|
if (sm) {
|
|
e.preventDefault();
|
|
var key = sm.getAttribute('data-smilie');
|
|
if (!key && sm.querySelector) {
|
|
var img = sm.querySelector('[data-smilie]');
|
|
if (img) key = img.getAttribute('data-smilie');
|
|
}
|
|
if (!key) return;
|
|
insertAtCursor(findTextarea(sm), '[g=' + key + ']');
|
|
var wrap = sm.closest('.dropup, .input-expand-wrap');
|
|
if (wrap) wrap.classList.remove('open');
|
|
return;
|
|
}
|
|
|
|
var codeBtn = e.target && e.target.closest ? e.target.closest('.input-code-submit') : null;
|
|
if (codeBtn) {
|
|
e.preventDefault();
|
|
var box = codeBtn.closest('.dropdown-code, .input-expand-code');
|
|
var ta = box ? box.querySelector('textarea') : null;
|
|
var code = ta ? ta.value : '';
|
|
if (!code || code.length < 2) {
|
|
notyf('请输入代码', 'warning');
|
|
return;
|
|
}
|
|
insertAtCursor(findTextarea(codeBtn), '[code]\n' + code + '\n[/code]\n');
|
|
if (ta) ta.value = '';
|
|
var w = codeBtn.closest('.dropup, .input-expand-wrap');
|
|
if (w) w.classList.remove('open');
|
|
return;
|
|
}
|
|
|
|
var imgBtn = e.target && e.target.closest ? e.target.closest('.input-image-submit') : null;
|
|
if (imgBtn) {
|
|
e.preventDefault();
|
|
var ibox = imgBtn.closest('.dropdown-image, .input-expand-image');
|
|
var input = ibox ? ibox.querySelector('.input-image-url') : null;
|
|
var url = input ? (input.value || '').trim() : '';
|
|
if (!/^https?:\/\//i.test(url)) {
|
|
notyf('请输入有效图片地址', 'warning');
|
|
return;
|
|
}
|
|
insertAtCursor(findTextarea(imgBtn), '[img=' + url + ']');
|
|
if (input) input.value = '';
|
|
var iw = imgBtn.closest('.dropup, .input-expand-wrap');
|
|
if (iw) iw.classList.remove('open');
|
|
return;
|
|
}
|
|
|
|
var upBtn = e.target && e.target.closest ? e.target.closest('.input-image-upload') : null;
|
|
if (upBtn) {
|
|
e.preventDefault();
|
|
var fileInput = document.createElement('input');
|
|
fileInput.type = 'file';
|
|
fileInput.accept = 'image/jpeg,image/png,image/gif,image/webp';
|
|
fileInput.addEventListener('change', function () {
|
|
var file = fileInput.files && fileInput.files[0];
|
|
if (!file) return;
|
|
restUpload(file)
|
|
.then(function (json) {
|
|
var u = (json && (json.url || json.source_url || (json.data && (json.data.url || json.data.source_url)))) || '';
|
|
if (!u) {
|
|
notyf((json && json.message) || '上传失败', 'danger');
|
|
return;
|
|
}
|
|
insertAtCursor(findTextarea(upBtn), '[img=' + u + ']');
|
|
})
|
|
.catch(function () { notyf('上传失败', 'danger'); });
|
|
});
|
|
fileInput.click();
|
|
return;
|
|
}
|
|
|
|
var q = e.target && e.target.closest ? e.target.closest('.quick-reply-item') : null;
|
|
if (q) {
|
|
e.preventDefault();
|
|
var phrase = q.getAttribute('data-text') || q.textContent || '';
|
|
insertAtCursor(findTextarea(q), phrase);
|
|
var qw = q.closest('.dropup, .input-expand-wrap');
|
|
if (qw) qw.classList.remove('open');
|
|
}
|
|
});
|
|
|
|
document.addEventListener('keydown', function (e) {
|
|
if (!(e.ctrlKey || e.metaKey) || e.key !== 'Enter') return;
|
|
var ta = e.target;
|
|
if (!ta || ta.id !== 'comment') return;
|
|
var form = ta.closest('#commentform');
|
|
if (!form) return;
|
|
e.preventDefault();
|
|
var submit = form.querySelector('#submit, [type="submit"]');
|
|
if (submit) submit.click();
|
|
});
|
|
})();
|