Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client web/7416 disable image pasting when hide image options flag is true #7428

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
80 changes: 47 additions & 33 deletions src/core/ui/forms/MarkdownInput/MarkdownInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,64 +119,78 @@ export const MarkdownInput = memo(
const items = clipboardData?.items;

if (!items) {
return false; // Allow default behavior if no items are found.
return false;
}

const storageBucketId = storageConfig?.storageBucketId;

if (!storageBucketId) {
return false; // Stop custom handling if storageBucketId is missing.
return false;
}

let imageProcessed = false;
// General function to check for images and HTML content with images
const isImageOrHtmlWithImage = (item: DataTransferItem) => {
if (item.kind === 'file' && item.type.startsWith('image/')) {
return true; // Image
}

for (const item of items) {
// Handle images first
if (!imageProcessed && item.kind === 'file' && item.type.startsWith('image/')) {
const file = item.getAsFile();

if (file) {
const reader = new FileReader();

reader.onload = () => {
uploadFile({
variables: {
file,
uploadData: {
storageBucketId,
temporaryLocation,
},
},
});
};
if (item.kind === 'string' && item.type === 'text/html') {
const htmlContent = clipboardData.getData('text/html');
return htmlContent.includes('<img'); // HTML tag with image
}

reader.readAsDataURL(file);
imageProcessed = true;
return false; // Not an image or HTML with images
};

// If hideImageOptions is enabled, block images
if (hideImageOptions) {
for (const item of items) {
if (isImageOrHtmlWithImage(item)) {
event.preventDefault();
return true; // Block paste of images or HTML with images
}
}

// Check for HTML content containing <img> tags
if (!imageProcessed && item.kind === 'string' && item.type === 'text/html') {
const htmlContent = clipboardData.getData('text/html');
return false; // Allow paste of text
}

if (htmlContent.includes('<img')) {
imageProcessed = true; // Mark as processed to avoid duplicates.
// Original logic for handling images
let imageProcessed = false;

for (const item of items) {
if (!imageProcessed && isImageOrHtmlWithImage(item)) {
if (item.kind === 'file' && item.type.startsWith('image/')) {
const file = item.getAsFile();
if (file) {
const reader = new FileReader();
reader.onload = () => {
uploadFile({
variables: {
file,
uploadData: { storageBucketId, temporaryLocation },
},
});
};
reader.readAsDataURL(file);
imageProcessed = true;
}
} else if (item.kind === 'string' && item.type === 'text/html') {
imageProcessed = true; // HTML with images
}
}

// Break the loop once an image or relevant HTML is handled.
if (imageProcessed) {
// Stop if we have already processed an image
break;
}
}

// Prevent default behavior only if we processed an image or relevant HTML.
if (imageProcessed) {
event.preventDefault();
return true; // Block default behavior in the editor.
return true; // Block default behavior for images
}

return false; // Allow default behavior for non-image content.
return false; // Allow default behavior for text
reactoholic marked this conversation as resolved.
Show resolved Hide resolved
},
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const OrganizationVerifiedStatus = ({ verified, helpText }: VerifiedStatu
const { t } = useTranslation();

return (
<BlockTitle sx={{ display: 'flex', alignItems: 'center', columnGap: 0.5 }}>
<BlockTitle variant="caption" sx={{ display: 'flex', alignItems: 'center', columnGap: 0.5 }}>
{helpText && verified && (
<Tooltip title={helpText} arrow placement="right">
<BeenhereOutlined fontSize="small" color="primary" />
Expand Down