Scratchpads V1
This commit is contained in:
137
script.js
137
script.js
@@ -284,6 +284,142 @@
|
||||
return path;
|
||||
}
|
||||
|
||||
function initScratchpads() {
|
||||
var pads = document.querySelectorAll('div.luxtools-scratchpad[data-luxtools-scratchpad="1"]');
|
||||
if (!pads || !pads.length) return;
|
||||
|
||||
function setStatus(root, msg) {
|
||||
var el = root.querySelector('.luxtools-scratchpad-status');
|
||||
if (!el) return;
|
||||
el.textContent = msg || '';
|
||||
}
|
||||
|
||||
function getSectok() {
|
||||
try {
|
||||
if (window.JSINFO && window.JSINFO.sectok) return String(window.JSINFO.sectok);
|
||||
} catch (e) {}
|
||||
return '';
|
||||
}
|
||||
|
||||
function loadPad(root) {
|
||||
var endpoint = (root.getAttribute('data-endpoint') || '').trim();
|
||||
var pad = (root.getAttribute('data-pad') || '').trim();
|
||||
var pageId = (root.getAttribute('data-pageid') || '').trim();
|
||||
if (!endpoint || !pad || !pageId) return Promise.reject(new Error('missing params'));
|
||||
|
||||
var url = endpoint + '?cmd=load&pad=' + encodeURIComponent(pad) + '&id=' + encodeURIComponent(pageId);
|
||||
return window.fetch(url, {
|
||||
method: 'GET',
|
||||
credentials: 'same-origin'
|
||||
}).then(function (res) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
if (!res.ok || !body || body.ok !== true) {
|
||||
var msg = (body && body.error) ? body.error : ('HTTP ' + res.status);
|
||||
throw new Error(msg);
|
||||
}
|
||||
return body.text || '';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function savePad(root, text) {
|
||||
var endpoint = (root.getAttribute('data-endpoint') || '').trim();
|
||||
var pad = (root.getAttribute('data-pad') || '').trim();
|
||||
var pageId = (root.getAttribute('data-pageid') || '').trim();
|
||||
if (!endpoint || !pad || !pageId) return Promise.reject(new Error('missing params'));
|
||||
|
||||
var params = new window.URLSearchParams();
|
||||
params.set('cmd', 'save');
|
||||
params.set('pad', pad);
|
||||
params.set('id', pageId);
|
||||
params.set('text', text || '');
|
||||
params.set('sectok', getSectok());
|
||||
|
||||
return window.fetch(endpoint, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
|
||||
body: params.toString()
|
||||
}).then(function (res) {
|
||||
return res.json().catch(function () { return null; }).then(function (body) {
|
||||
if (!res.ok || !body || body.ok !== true) {
|
||||
var msg = (body && body.error) ? body.error : ('HTTP ' + res.status);
|
||||
throw new Error(msg);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function openEditor(root) {
|
||||
var editor = root.querySelector('.luxtools-scratchpad-editor');
|
||||
var textarea = root.querySelector('textarea.luxtools-scratchpad-text');
|
||||
if (!editor || !textarea) return;
|
||||
|
||||
editor.hidden = false;
|
||||
setStatus(root, 'Loading…');
|
||||
textarea.disabled = true;
|
||||
|
||||
loadPad(root).then(function (text) {
|
||||
textarea.value = text;
|
||||
textarea.disabled = false;
|
||||
setStatus(root, '');
|
||||
textarea.focus();
|
||||
}).catch(function (e) {
|
||||
textarea.disabled = false;
|
||||
setStatus(root, 'Load failed: ' + (e && e.message ? e.message : 'error'));
|
||||
});
|
||||
}
|
||||
|
||||
function closeEditor(root) {
|
||||
var editor = root.querySelector('.luxtools-scratchpad-editor');
|
||||
if (!editor) return;
|
||||
editor.hidden = true;
|
||||
setStatus(root, '');
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var t = e.target;
|
||||
if (!t) return;
|
||||
|
||||
var edit = t.closest ? t.closest('a.luxtools-scratchpad-edit') : null;
|
||||
if (edit) {
|
||||
var root = edit.closest('div.luxtools-scratchpad');
|
||||
if (!root) return;
|
||||
e.preventDefault();
|
||||
openEditor(root);
|
||||
return;
|
||||
}
|
||||
|
||||
var save = t.closest ? t.closest('button.luxtools-scratchpad-save') : null;
|
||||
if (save) {
|
||||
var rootS = save.closest('div.luxtools-scratchpad');
|
||||
if (!rootS) return;
|
||||
e.preventDefault();
|
||||
var textareaS = rootS.querySelector('textarea.luxtools-scratchpad-text');
|
||||
if (!textareaS) return;
|
||||
textareaS.disabled = true;
|
||||
setStatus(rootS, 'Saving…');
|
||||
savePad(rootS, textareaS.value).then(function () {
|
||||
setStatus(rootS, 'Saved. Reloading…');
|
||||
try { window.location.reload(); } catch (err) {}
|
||||
}).catch(function (err) {
|
||||
textareaS.disabled = false;
|
||||
setStatus(rootS, 'Save failed: ' + (err && err.message ? err.message : 'error'));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var cancel = t.closest ? t.closest('button.luxtools-scratchpad-cancel') : null;
|
||||
if (cancel) {
|
||||
var rootC = cancel.closest('div.luxtools-scratchpad');
|
||||
if (!rootC) return;
|
||||
e.preventDefault();
|
||||
closeEditor(rootC);
|
||||
}
|
||||
}, true);
|
||||
}
|
||||
|
||||
function findOpenElement(target) {
|
||||
var el = target;
|
||||
while (el && el !== document) {
|
||||
@@ -350,4 +486,5 @@
|
||||
|
||||
document.addEventListener('click', onClick, false);
|
||||
document.addEventListener('DOMContentLoaded', initGalleryThumbs, false);
|
||||
document.addEventListener('DOMContentLoaded', initScratchpads, false);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user