Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export const Editor = forwardRef((props: EditorProps, ref) => {
dataTestId,
onKeyDown,
hideBorder,
uploadFile,
onFileUploadAttempt,
onFileUploadFailed,
onFileUploadSucceeded,
onFileDownloadView,
uploadFileProps,
} = props;
const { manager, state, getContext } = useRemirror({
extensions: () => [
Expand All @@ -77,11 +73,7 @@ export const Editor = forwardRef((props: EditorProps, ref) => {
new HistoryExtension({}),
new HorizontalRuleExtension({}),
new FileDragDropExtension({
onFileUpload: uploadFile,
onFileUploadAttempt,
onFileUploadFailed,
onFileUploadSucceeded,
onFileDownloadView,
uploadFileProps,
}),
new GapCursorExtension(), // required to allow cursor placement next to non-editable inline elements
new ImageExtension({ enableResizing: !readOnly }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import {
import { FileUploadFailureType, FileUploadSource } from '@components/components/Editor/types';
import { notification } from '@components/components/Notification/notification';

interface FileDragDropOptions {
interface FileUploadProps {
onFileUpload?: (file: File) => Promise<string>;
supportedTypes?: string[];
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
onFileUploadFailed?: (
fileType: string,
Expand All @@ -46,6 +45,11 @@ interface FileDragDropOptions {
onFileDownloadView?: (fileType: string, fileSize: number) => void;
}

interface FileDragDropOptions {
uploadFileProps?: FileUploadProps;
supportedTypes?: string[];
}

/**
* The FileDragDrop extension allows users to drag and drop files into the editor.
* It creates file nodes that render differently based on file type (images, PDFs, etc.)
Expand Down Expand Up @@ -144,12 +148,12 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
// Process each file
const fileArray = Array.from(files);
const processPromises = fileArray.map(async (file) => {
this.options.onFileUploadAttempt?.(file.type, file.size, 'drag-and-drop');
this.options.uploadFileProps?.onFileUploadAttempt?.(file.type, file.size, 'drag-and-drop');

const validation = validateFile(file, { allowedTypes: supportedTypes });
if (!validation.isValid) {
console.error(validation.error);
this.options.onFileUploadFailed?.(
this.options.uploadFileProps?.onFileUploadFailed?.(
file.type,
file.size,
'drag-and-drop',
Expand Down Expand Up @@ -184,14 +188,14 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
view.dispatch(transaction);

// Upload file if handler is provided
if (this.options.onFileUpload) {
if (this.options.uploadFileProps?.onFileUpload) {
try {
const finalUrl = await this.options.onFileUpload(file);
const finalUrl = await this.options.uploadFileProps.onFileUpload(file);
this.updateNodeWithUrl(view, placeholderAttrs.id, finalUrl);
this.options.onFileUploadSucceeded?.(file.type, file.size, 'drag-and-drop');
this.options.uploadFileProps.onFileUploadSucceeded?.(file.type, file.size, 'drag-and-drop');
} catch (uploadError) {
console.error(uploadError);
this.options.onFileUploadFailed?.(
this.options.uploadFileProps.onFileUploadFailed?.(
file.type,
file.size,
'drag-and-drop',
Expand All @@ -205,7 +209,7 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
});
}
} else {
this.options.onFileUploadFailed?.(
this.options.uploadFileProps?.onFileUploadFailed?.(
file.type,
file.size,
'drag-and-drop',
Expand All @@ -218,7 +222,7 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
}
} catch (error) {
console.error(error);
this.options.onFileUploadFailed?.(
this.options.uploadFileProps?.onFileUploadFailed?.(
file.type,
file.size,
'drag-and-drop',
Expand Down Expand Up @@ -385,7 +389,9 @@ class FileDragDropExtension extends NodeExtension<FileDragDropOptions> {
ReactComponent: ComponentType<NodeViewComponentProps> = (props) => (
<FileNodeView
{...props}
onFileDownloadView={(fileType, fileSize) => this.options.onFileDownloadView?.(fileType, fileSize)}
onFileDownloadView={(fileType, fileSize) =>
this.options.uploadFileProps?.onFileDownloadView?.(fileType, fileSize)
}
/>
);

Expand All @@ -408,12 +414,14 @@ const decoratedExt = extension<FileDragDropOptions>({
handlerKeys: [],
customHandlerKeys: [],
defaultOptions: {
onFileUpload: async (_file: File) => '',
uploadFileProps: {
onFileUpload: async (_file: File) => '',
onFileUploadAttempt: () => {},
onFileUploadFailed: () => {},
onFileUploadSucceeded: () => {},
onFileDownloadView: () => {},
},
supportedTypes: SUPPORTED_FILE_TYPES,
onFileUploadAttempt: () => {},
onFileUploadFailed: () => {},
onFileUploadSucceeded: () => {},
onFileDownloadView: () => {},
},
})(FileDragDropExtension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const FileUploadButton = () => {
const [showDropdown, setShowDropdown] = useState(false);

// Hide the button when uploading of files is disabled
if (!fileExtension.options.onFileUpload) return null;
if (!fileExtension.options.uploadFileProps?.onFileUpload) return null;

return (
<Dropdown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const FileUploadContent = ({ hideDropdown }: Props) => {
if (files.length === 0) return;

const supportedTypes = SUPPORTED_FILE_TYPES;
const { onFileUpload, onFileUploadAttempt, onFileUploadFailed, onFileUploadSucceeded } = fileExtension.options;
const { onFileUpload, onFileUploadAttempt, onFileUploadFailed, onFileUploadSucceeded } =
fileExtension.options.uploadFileProps || {};

try {
// Process files concurrently
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const Toolbar = ({ styles }: Props) => {
const remirrorContext = useRemirrorContext();
const fileExtension = remirrorContext.getExtension(FileDragDropExtension);

const shouldShowImageButtonV2 = documentationFileUploadV1 && fileExtension.options.onFileUpload;
const shouldShowImageButtonV2 = documentationFileUploadV1 && fileExtension.options.uploadFileProps?.onFileUpload;

return (
<Container style={styles}>
Expand Down
26 changes: 15 additions & 11 deletions datahub-web-react/src/alchemy-components/components/Editor/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ export enum FileUploadFailureType {
UNKNOWN = 'unknown',
}

export interface FileUploadProps {
onFileUpload?: (file: File) => Promise<string>;
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
onFileUploadFailed?: (
fileType: string,
fileSize: number,
source: FileUploadSource,
failureType: FileUploadFailureType,
comment?: string,
) => void;
onFileUploadSucceeded?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
onFileDownloadView?: (fileType: string, fileSize: number) => void;
}

export type EditorProps = {
readOnly?: boolean;
content?: string;
Expand All @@ -19,15 +33,5 @@ export type EditorProps = {
dataTestId?: string;
onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
hideBorder?: boolean;
uploadFile?: (file: File) => Promise<string>;
onFileUploadAttempt?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
onFileUploadFailed?: (
fileType: string,
fileSize: number,
source: FileUploadSource,
failureType: FileUploadFailureType,
comment?: string,
) => void;
onFileUploadSucceeded?: (fileType: string, fileSize: number, source: FileUploadSource) => void;
onFileDownloadView?: (fileType: string, fileSize: number) => void;
uploadFileProps?: FileUploadProps;
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ export function AboutFieldTab({ properties }: AboutFieldTabProps) {
<FieldDescription
expandedField={expandedField}
editableFieldInfo={editableFieldInfo}
editorProps={{ uploadFile, ...uploadFileAnalyticsCallbacks }}
editorProps={{
uploadFileProps: {
onFileUpload: uploadFile,
...uploadFileAnalyticsCallbacks,
},
}}
/>
<FieldTags
expandedField={expandedField}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ export const DescriptionEditor = ({ onComplete }: DescriptionEditorProps) => {
content={updatedDescription}
onChange={handleEditorChange}
placeholder="Describe this asset to make it more discoverable. Tag @user or reference @asset to make your docs come to life!"
uploadFile={uploadFile}
{...uploadFileAnalyticsCallbacks}
uploadFileProps={{
onFileUpload: uploadFile,
...uploadFileAnalyticsCallbacks,
}}
hideBorder
/>
</EditorContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export default function EditDescriptionModal({
onChange={(description) => setUpdatedDescription(description)}
toolbarStyles={toolbarStyles}
dataTestId="description-editor"
uploadFile={uploadFile}
{...uploadFileAnalyticsCallbacks}
uploadFileProps={{
onFileUpload: uploadFile,
...uploadFileAnalyticsCallbacks,
}}
/>
</Modal>
);
Expand Down
Loading