forgot one

This commit is contained in:
James Shiffer 2020-07-06 07:57:55 -07:00
parent a9d2b49456
commit 8ba12e5eca
No known key found for this signature in database
GPG Key ID: C0DB8774A1B3BA45

View File

@ -18,7 +18,7 @@
const { session } = stores(); const { session } = stores();
let editor, form, uploadForm; let editor, form, uploadForm;
let loading = false; let loading = false, loadingAttach = false;
let title = '', category = ''; let title = '', category = '';
export let categories; export let categories;
@ -112,28 +112,35 @@
async function upload() async function upload()
{ {
let fd = new FormData(uploadForm); let fd = new FormData(uploadForm);
const res = await fetch(`/cms/upload`, { loadingAttach = true;
method: 'POST', try {
headers: { const res = await fetch(`/cms/upload`, {
'Accept': 'application/json' method: 'POST',
}, headers: {
body: fd 'Accept': 'application/json'
}); },
const json = await res.json(); body: fd
if (res.status === 200) { });
const ans = prompt('(Optional) Enter the dimensions to resize this image to (e.g. "350x150")'); const json = await res.json();
if (ans) { if (res.status === 200) {
const dim = ans.split('x'); const ans = prompt('(Optional) Enter the dimensions to resize this image to (e.g. "350x150")');
if (Number.isInteger(+dim[0]) && Number.isInteger(+dim[1])) { if (ans) {
editor.setHtml(editor.getHtml(true) + `<img src="${json.url}" width="${dim[0]}" height="${dim[1]}">`, false); const dim = ans.split('x');
if (Number.isInteger(+dim[0]) && Number.isInteger(+dim[1])) {
editor.setHtml(editor.getHtml(true) + `<img src="${json.url}" width="${dim[0]}" height="${dim[1]}">`, false);
} else {
editor.setHtml(editor.getHtml(true) + `<img src="${json.url}">`, false);
}
} else { } else {
editor.setHtml(editor.getHtml(true) + `<img src="${json.url}">`, false); editor.setHtml(editor.getHtml(true) + `<img src="${json.url}">`, false);
} }
} else { } else {
editor.setHtml(editor.getHtml(true) + `<img src="${json.url}">`, false); alert(`Error ${res.status}: ${json.message}`);
} }
} else { } catch (err) {
alert(`Error ${res.status}: ${json.message}`); console.error(err);
} finally {
loadingAttach = false;
} }
} }
</script> </script>
@ -169,6 +176,9 @@
<form enctype="multipart/form-data" action="/cms/upload" method="POST" bind:this={uploadForm}> <form enctype="multipart/form-data" action="/cms/upload" method="POST" bind:this={uploadForm}>
<p>Add Image to Article: <p>Add Image to Article:
<input type="file" name="upload" accept="image/*"> <button on:click|preventDefault={upload}>Upload</button> <input type="file" name="upload" accept="image/*"> <button on:click|preventDefault={upload}>Upload</button>
{#if loadingAttach}
<progress>Uploading...</progress>
{/if}
</p> </p>
</form> </form>
<button on:click={submit}>Submit</button> <button on:click={submit}>Submit</button>