Skip to content

Commit

Permalink
Merge pull request #2349 from epam/fix-uploading-files
Browse files Browse the repository at this point in the history
[SlateEditor] Fix files upload
  • Loading branch information
AlekseyManetov authored Jun 12, 2024
2 parents 53940b0 + 6320c75 commit 7596a1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
23 changes: 17 additions & 6 deletions uui-editor/src/plugins/uploadFilePlugin/file_uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { FileUploadResponse } from '@epam/uui-core';
import {
PlateEditor,
getPlugin,
KEY_INSERT_DATA,
deleteBackward,
insertEmptyElement,
insertNodes,
Expand All @@ -15,18 +14,29 @@ import { IFRAME_TYPE } from '../iframePlugin/constants';
import { TImageElement } from '../imagePlugin/types';
import { TIframeElement } from '../iframePlugin/types';
import { TAttachmentElement } from '../attachmentPlugin/types';
import { UPLOAD_PLUGIN_KEY } from './constants';

export type UploadType = keyof typeof UPLOAD_BLOCKS;

export interface UploadFileOptions {
uploadFile?: UploadFile;
}

type UploadFile = (
file: File,
onProgress?: (progress: number) => any
) => Promise<FileUploadResponse>;

export interface UploadFileOptions {
uploadFile?: UploadFile;
}

type FileUploader = (
editor: PlateEditor,
files: File[],
overriddenAction?: UploadType,
) => Promise<void>;

export interface FileUploaderOptions {
uploadFiles?: FileUploader;
}

const UPLOAD_BLOCKS = {
attachment: (file: FileUploadResponse): TAttachmentElement => ({
type: ATTACHMENT_TYPE,
Expand Down Expand Up @@ -120,7 +130,8 @@ export const createFileUploader = (options?: UploadFileOptions) =>
export const useFilesUploader = (editor: PlateEditor) => {
return useCallback(
(files: File[], overriddenAction?: UploadType): Promise<void> => {
const callback = getPlugin(editor, KEY_INSERT_DATA)?.options?.uploadFiles;
const callback = getPlugin<FileUploaderOptions>(editor, UPLOAD_PLUGIN_KEY)?.options.uploadFiles;

if (callback) {
return callback(
editor,
Expand Down
7 changes: 4 additions & 3 deletions uui-editor/src/plugins/uploadFilePlugin/uploadFilePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@udecode/plate-common';
import { UPLOAD_PLUGIN_KEY } from './constants';
import {
FileUploaderOptions,
UploadFileOptions,
createFileUploader,
} from './file_uploader';
Expand All @@ -20,7 +21,7 @@ const isFilesUploadEvent = (dataTransfer: DataTransfer) => {
};

export const uploadFilePlugin = (uploadOptions?: UploadFileOptions): PlatePlugin => {
const createUploadPlugin = createPluginFactory({
const createUploadPlugin = createPluginFactory<FileUploaderOptions>({
key: UPLOAD_PLUGIN_KEY,
options: { uploadFiles: createFileUploader(uploadOptions) },
handlers: {
Expand All @@ -37,7 +38,7 @@ export const uploadFilePlugin = (uploadOptions?: UploadFileOptions): PlatePlugin
select(editor, at);

const { files } = event.dataTransfer;
plugin.options.uploadFiles(editor, Array.from(files));
plugin.options.uploadFiles?.(editor, Array.from(files));
return true;
};
},
Expand All @@ -49,7 +50,7 @@ export const uploadFilePlugin = (uploadOptions?: UploadFileOptions): PlatePlugin
event.stopPropagation();

const { files } = event.clipboardData;
plugin.options.uploadFiles(editor, Array.from(files));
plugin.options.uploadFiles?.(editor, Array.from(files));
return true;
};
},
Expand Down

0 comments on commit 7596a1b

Please sign in to comment.