Merge pull request 'Fix #163: /new Create button broken (stale iscan/createCan path)' (#164) from fix-163 into dev
This commit was merged in pull request #164.
This commit is contained in:
@@ -151,8 +151,6 @@ $('reguess').addEventListener('click', guessLang);
|
||||
content.addEventListener('paste', () => setTimeout(guessLang, 0));
|
||||
|
||||
async function create() {
|
||||
// #4: can mode — bundle the editor + extra items into a can via multipart
|
||||
if ($('iscan').checked) return createCan();
|
||||
// #38: file attached -> file paste (1 file = 1 paste; text is ignored)
|
||||
if (attachedFile) return createFilePaste();
|
||||
|
||||
@@ -330,61 +328,6 @@ async function createFilePaste() {
|
||||
finishCreate(data);
|
||||
}
|
||||
|
||||
// #4: can creation — POST multipart to /api/pastes/can. The main editor is
|
||||
// the first item; each extra can-item row is another text item.
|
||||
async function createCan() {
|
||||
const items = [];
|
||||
if (content.value.trim()) {
|
||||
items.push({title: $('title').value || 'main', content: content.value, language: $('language').value || ''});
|
||||
}
|
||||
document.querySelectorAll('#can-items .can-item-row').forEach(row => {
|
||||
const t = row.querySelector('.can-item-title').value.trim();
|
||||
const c = row.querySelector('.can-item-content').value;
|
||||
if (c.trim()) items.push({title: t || ('item-' + (items.length + 1)), content: c});
|
||||
});
|
||||
if (!items.length) { toast('Nothing to put in the can', 'error'); return; }
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('title', $('title').value || 'Untitled can');
|
||||
fd.append('json_items', JSON.stringify(items));
|
||||
if ($('haspw').checked) fd.append('password', $('password').value);
|
||||
if ($('unlisted').checked) fd.append('visibility', 'unlisted');
|
||||
const exp = document.querySelector('input[name="exp"]:checked').value;
|
||||
if (exp === 'custom') {
|
||||
const dur = composeCustomExpiry();
|
||||
if (dur === null) { toast('Check the custom expiry', 'error'); return; }
|
||||
if (dur) fd.append('expires_in', dur);
|
||||
} else if (exp) {
|
||||
fd.append('expires_in', exp);
|
||||
}
|
||||
if ($('custom').value.trim()) fd.append('custom_slug', $('custom').value.trim());
|
||||
|
||||
const res = await fetch('/api/pastes/can', {method: 'POST', body: fd});
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
showResult(friendlyError(data), 'err');
|
||||
toast('Can create failed', 'error');
|
||||
return;
|
||||
}
|
||||
const url = location.origin + data.url;
|
||||
showResult('<a href="' + url + '">' + url + '</a> <button class="btn btn-icon" id="result-copy" title="Copy URL" type="button">⧉</button>', 'ok');
|
||||
const copyBtn = document.getElementById('result-copy');
|
||||
copyBtn.addEventListener('click', () => {
|
||||
try {
|
||||
navigator.clipboard.writeText(url);
|
||||
copyBtn.classList.add('ok');
|
||||
copyBtn.textContent = 'Success!';
|
||||
setTimeout(() => { copyBtn.classList.remove('ok'); copyBtn.textContent = '⧉'; }, 2000);
|
||||
} catch(e) { toast('Copy failed', 'error'); }
|
||||
});
|
||||
// password-protected can: unlock now with the password we already have (#26 parity)
|
||||
if ($('haspw').checked && data.id) {
|
||||
const pd = new FormData();
|
||||
pd.append('password', $('password').value);
|
||||
try { await fetch('/can/' + data.id, {method: 'POST', body: pd}); } catch(e) {}
|
||||
}
|
||||
location.href = data.url;
|
||||
}
|
||||
// reset stale result state when returning via Back (bfcache) (#28)
|
||||
window.addEventListener('pageshow', e => {
|
||||
if (!e.persisted) return;
|
||||
|
||||
Reference in New Issue
Block a user