mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-10-04 13:26:30 +00:00
Fix: Resolve multiple image insertion issues and ensure proper inline error messaging (#21193)
* Fix: Resolve multiple image insertion issues and ensure proper inline error messaging * fixed failing unit test (cherry picked from commit 9b96bf89d7de2b1af21b1acbf75ddd00308b05a6)
This commit is contained in:
parent
5b170accdb
commit
09107478cd
@ -84,10 +84,27 @@ const BarMenu: FC<BarMenuProps> = ({ editor, onLinkToggle }) => {
|
|||||||
{
|
{
|
||||||
name: 'image',
|
name: 'image',
|
||||||
icon: ImageIcon,
|
icon: ImageIcon,
|
||||||
command: () =>
|
command: () => {
|
||||||
|
const { state } = editor.view;
|
||||||
|
const { selection } = state;
|
||||||
|
|
||||||
|
// Get the current position
|
||||||
|
const pos = selection.$anchor.pos;
|
||||||
|
|
||||||
|
// Create a new selection at the current position
|
||||||
|
editor.commands.setTextSelection(pos);
|
||||||
|
|
||||||
|
// Insert a new line if we're at the end of a block
|
||||||
|
if (
|
||||||
|
selection.$anchor.parentOffset ===
|
||||||
|
selection.$anchor.parent.content.size
|
||||||
|
) {
|
||||||
|
editor.commands.insertContent('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now add the image
|
||||||
editor
|
editor
|
||||||
.chain()
|
.chain()
|
||||||
.focus()
|
|
||||||
.setFile({
|
.setFile({
|
||||||
url: '',
|
url: '',
|
||||||
fileName: '',
|
fileName: '',
|
||||||
@ -96,7 +113,11 @@ const BarMenu: FC<BarMenuProps> = ({ editor, onLinkToggle }) => {
|
|||||||
type: FileType.IMAGE,
|
type: FileType.IMAGE,
|
||||||
isImage: true,
|
isImage: true,
|
||||||
})
|
})
|
||||||
.run(),
|
.run();
|
||||||
|
|
||||||
|
// Move cursor after the image
|
||||||
|
editor.commands.setTextSelection(pos + 1);
|
||||||
|
},
|
||||||
isActive: () => editor.isActive('image'),
|
isActive: () => editor.isActive('image'),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -287,7 +287,9 @@ describe('EntityAttachmentProvider', () => {
|
|||||||
it('handles upload error with string error', async () => {
|
it('handles upload error with string error', async () => {
|
||||||
const mockFile = new File([''], 'test.jpg', { type: 'image/jpeg' });
|
const mockFile = new File([''], 'test.jpg', { type: 'image/jpeg' });
|
||||||
const errorMessage = 'Upload failed';
|
const errorMessage = 'Upload failed';
|
||||||
mockOnImageUpload.mockRejectedValue(errorMessage);
|
mockOnImageUpload.mockRejectedValue({
|
||||||
|
response: { data: { message: errorMessage } },
|
||||||
|
});
|
||||||
|
|
||||||
render(
|
render(
|
||||||
<EntityAttachmentProvider entityType={EntityType.TABLE}>
|
<EntityAttachmentProvider entityType={EntityType.TABLE}>
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
*/
|
*/
|
||||||
import { EditorView } from '@tiptap/pm/view';
|
import { EditorView } from '@tiptap/pm/view';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import { isString, noop } from 'lodash';
|
import { isString, isUndefined, noop } from 'lodash';
|
||||||
import React, { createContext, ReactNode, useContext, useState } from 'react';
|
import React, { createContext, ReactNode, useContext, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { EntityType } from '../../../../enums/entity.enum';
|
import { EntityType } from '../../../../enums/entity.enum';
|
||||||
@ -162,6 +162,9 @@ export const EntityAttachmentProvider = ({
|
|||||||
currentTr.replaceWith(tempNodePos, tempNodePos + 1, finalNode);
|
currentTr.replaceWith(tempNodePos, tempNodePos + 1, finalNode);
|
||||||
view.dispatch(currentTr);
|
view.dispatch(currentTr);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
const errorMessage = (error as AxiosError<{ message: string }>).response
|
||||||
|
?.data?.message;
|
||||||
|
|
||||||
// Get the current state for error handling
|
// Get the current state for error handling
|
||||||
const currentState = view.state;
|
const currentState = view.state;
|
||||||
const currentTr = currentState.tr;
|
const currentTr = currentState.tr;
|
||||||
@ -182,7 +185,9 @@ export const EntityAttachmentProvider = ({
|
|||||||
|
|
||||||
showInlineAlert
|
showInlineAlert
|
||||||
? setErrorMessage(
|
? setErrorMessage(
|
||||||
isString(error) ? error : t('label.failed-to-upload-file')
|
!isUndefined(errorMessage) && isString(errorMessage)
|
||||||
|
? errorMessage
|
||||||
|
: t('label.failed-to-upload-file')
|
||||||
)
|
)
|
||||||
: showErrorToast(error as AxiosError, t('label.failed-to-upload-file'));
|
: showErrorToast(error as AxiosError, t('label.failed-to-upload-file'));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user