FEATURE: New API hook to allow for client side upload transforms (#12990)

This commit is contained in:
Rafael dos Santos Silva 2021-05-11 12:20:28 -03:00 committed by GitHub
parent 033a1fb2af
commit 2f60309b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 1 deletions

View File

@ -53,6 +53,13 @@ export function addComposerUploadHandler(extensions, method) {
}); });
} }
const uploadProcessorQueue = [];
const uploadProcessorActions = {};
export function addComposerUploadProcessor(queueItem, actionItem) {
uploadProcessorQueue.push(queueItem);
Object.assign(uploadProcessorActions, actionItem);
}
const uploadMarkdownResolvers = []; const uploadMarkdownResolvers = [];
export function addComposerUploadMarkdownResolver(resolver) { export function addComposerUploadMarkdownResolver(resolver) {
uploadMarkdownResolvers.push(resolver); uploadMarkdownResolvers.push(resolver);
@ -634,12 +641,34 @@ export default Component.extend({
const $element = $(this.element); const $element = $(this.element);
$.blueimp.fileupload.prototype.processActions = uploadProcessorActions;
$element.fileupload({ $element.fileupload({
url: getURL(`/uploads.json?client_id=${this.messageBus.clientId}`), url: getURL(`/uploads.json?client_id=${this.messageBus.clientId}`),
dataType: "json", dataType: "json",
pasteZone: $element, pasteZone: $element,
processQueue: uploadProcessorQueue,
}); });
$element
.on("fileuploadprocess", (e, data) => {
this.appEvents.trigger(
"composer:insert-text",
`[${I18n.t("processing_filename", {
filename: data.files[data.index].name,
})}]()\n`
);
})
.on("fileuploadprocessalways", (e, data) => {
this.appEvents.trigger(
"composer:replace-text",
`[${I18n.t("processing_filename", {
filename: data.files[data.index].name,
})}]()\n`,
""
);
});
$element.on("fileuploadpaste", (e) => { $element.on("fileuploadpaste", (e) => {
this._pasted = true; this._pasted = true;

View File

@ -1,6 +1,7 @@
import ComposerEditor, { import ComposerEditor, {
addComposerUploadHandler, addComposerUploadHandler,
addComposerUploadMarkdownResolver, addComposerUploadMarkdownResolver,
addComposerUploadProcessor,
} from "discourse/components/composer-editor"; } from "discourse/components/composer-editor";
import { addButton, removeButton } from "discourse/widgets/post-menu"; import { addButton, removeButton } from "discourse/widgets/post-menu";
import { import {
@ -72,7 +73,7 @@ import { replaceTagRenderer } from "discourse/lib/render-tag";
import { setNewCategoryDefaultColors } from "discourse/routes/new-category"; import { setNewCategoryDefaultColors } from "discourse/routes/new-category";
// If you add any methods to the API ensure you bump up this number // If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = "0.11.2"; const PLUGIN_API_VERSION = "0.11.3";
class PluginApi { class PluginApi {
constructor(version, container) { constructor(version, container) {
@ -933,6 +934,31 @@ class PluginApi {
addComposerUploadHandler(extensions, method); addComposerUploadHandler(extensions, method);
} }
/**
* Registers a pre-processor for file uploads
* See https://github.com/blueimp/jQuery-File-Upload/wiki/Options#file-processing-options
* Your theme/plugin will also need to load https://github.com/blueimp/jQuery-File-Upload/blob/v10.13.0/js/jquery.fileupload-process.js
* for this hook to work.
*
* Useful for transforming to-be uploaded files client-side
*
* Example:
*
* api.addComposerUploadProcessor({action: 'myFileTransformation'}, {
* myFileTransformation: function (data, options) {
* let p = new Promise((resolve, reject) => {
* let file = data.files[data.index];
* console.log(`Transforming ${file.name}`);
* // do work...
* resolve(data);
* });
* return p;
* });
*/
addComposerUploadProcessor(queueItem, actionItem) {
addComposerUploadProcessor(queueItem, actionItem);
}
/** /**
* Registers a function to generate Markdown after a file has been uploaded. * Registers a function to generate Markdown after a file has been uploaded.
* *

View File

@ -368,6 +368,7 @@ en:
upload: "Upload" upload: "Upload"
uploading: "Uploading..." uploading: "Uploading..."
uploading_filename: "Uploading: %{filename}..." uploading_filename: "Uploading: %{filename}..."
processing_filename: "Processing: %{filename}..."
clipboard: "clipboard" clipboard: "clipboard"
uploaded: "Uploaded!" uploaded: "Uploaded!"