docbrown/app/javascript/article-form/actions.js
Nick Taylor f0963c1bfa
Drag and drop an image in the editor (#10145)
* Refactored drag n drop functionality to a hook and component.

* Add the snackbar to the article editor.

* Now you can drag and drop images in the post body content.

* Cleaning up a few things.

* Now post cover images can be dragged and dropped.

* Removed unused empty function.

* Added @testing-library/preact-hooks so that we can test hooks.

* Some renaming, small refactor.

* Added tests

* Added more tests.

* Added more tests.

* Added message about dropping only one image in post body.

* Added message about dropping only one image for cover image.

* put onDrop into it's own meethod.

* More tests.

* Added some API documentation.

* Updated editor help with drag and drop image help.

* Changed wording.

* Now the alt text for markdown removes the file extension.

* Updated help wording. Thanks @rhymes

* Now the image markdown adds a new line at the end as we currently do not support inline images.
2020-09-04 14:12:59 -04:00

121 lines
3.1 KiB
JavaScript

import { validateFileInputs } from '../packs/validateFileInputs';
export function previewArticle(payload, successCb, failureCb) {
fetch('/articles/preview', {
method: 'POST',
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
article_body: payload,
}),
credentials: 'same-origin',
})
.then((response) => response.json())
.then(successCb)
.catch(failureCb);
}
export function getArticle() {}
function processPayload(payload) {
const {
/* eslint-disable no-unused-vars */
previewShowing,
helpShowing,
previewResponse,
helpHTML,
imageManagementShowing,
moreConfigShowing,
errors,
/* eslint-enable no-unused-vars */
...neededPayload
} = payload;
return neededPayload;
}
export function submitArticle(payload, clearStorage, errorCb, failureCb) {
const method = payload.id ? 'PUT' : 'POST';
const url = payload.id ? `/articles/${payload.id}` : '/articles';
fetch(url, {
method,
headers: {
Accept: 'application/json',
'X-CSRF-Token': window.csrfToken,
'Content-Type': 'application/json',
},
body: JSON.stringify({
article: processPayload(payload),
}),
credentials: 'same-origin',
})
.then((response) => response.json())
.then((response) => {
if (response.current_state_path) {
clearStorage();
window.location.replace(response.current_state_path);
} else {
// If there is an error and the method is POST, we know they are trying to publish.
errorCb(response, method === 'POST');
}
})
.catch(failureCb);
}
function generateUploadFormdata(payload) {
const token = window.csrfToken;
const formData = new FormData();
formData.append('authenticity_token', token);
Object.entries(payload.image).forEach(([_, value]) =>
formData.append('image[]', value),
);
if (payload.wrap_cloudinary) {
formData.append('wrap_cloudinary', 'true');
}
return formData;
}
export function generateMainImage(payload, successCb, failureCb) {
fetch('/image_uploads', {
method: 'POST',
headers: {
'X-CSRF-Token': window.csrfToken,
},
body: generateUploadFormdata(payload),
credentials: 'same-origin',
})
.then((response) => response.json())
.then((json) => {
if (json.error) {
throw new Error(json.error);
}
const { links } = json;
const { image } = payload;
return successCb({ links, image });
})
.catch(failureCb);
}
/**
* Processes images for upload.
*
* @param {FileList} images Images to be uploaded.
* @param {Function} handleImageSuccess The handler that runs when the image is uploaded successfully.
* @param {Function} handleImageFailure The handler that runs when the image upload fails.
*/
export function processImageUpload(
images,
handleImageSuccess,
handleImageFailure,
) {
// Currently only one image is supported for upload.
if (images.length > 0 && validateFileInputs()) {
const payload = { image: images };
generateMainImage(payload, handleImageSuccess, handleImageFailure);
}
}