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

[ Fix : URL Copied from Image Drawer And Pasted To Quill Editor Shows Image In Preview Or View Mode ] #1165

Open
wants to merge 2 commits into
base: master
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
32 changes: 29 additions & 3 deletions src/components/Editor/QuillEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const QuillEditor = ({ id, data, tutorial_id }) => {
// This path in cloud firestore contains yjs documents storing content of a step
// (actual data used to render is present in "steps" collection in the same doc)
const basePath = ["tutorials", tutorial_id, "yjsStepDocs", id];
let provider, binding, ydoc;
let provider, binding, ydoc, quill;

const currentUserHandle = useSelector(
({
Expand Down Expand Up @@ -69,7 +69,7 @@ const QuillEditor = ({ id, data, tutorial_id }) => {
container.removeChild(container.firstChild);
}

const editor = new Quill(editorRef.current, {
quill = new Quill(editorRef.current, {
modules: {
cursors: true,
toolbar: [
Expand All @@ -93,6 +93,32 @@ const QuillEditor = ({ id, data, tutorial_id }) => {
],
history: {
userOnly: true
},
clipboard: {
matchers: [
// Custom matcher to handle pasted image URLs
[
Node.TEXT_NODE,
(node, delta) => {
const regex = /\!\[.*\]\((https?:\/\/[^\s]+)\)/;
const match = node.data.match(regex);

if (match) {
const imageUrl = match[1];
console.log("Image URL", imageUrl);
const Delta= Quill.import("delta")
const imageDelta = new Delta()
.retain(delta.length())
.delete(node.data.length)
.insert({ image: imageUrl }, { alt: "Image" });

quill.updateContents(imageDelta, "silent");
return true;
}
return false;
}
]
]
}
},
placeholder: "Start collaborating...",
Expand All @@ -104,7 +130,7 @@ const QuillEditor = ({ id, data, tutorial_id }) => {
// color: getColor(currentUserHandle)
// });

binding = new QuillBinding(ytext, editor, provider.awareness);
binding = new QuillBinding(ytext, quill, provider.awareness);
} catch (err) {
console.log(err);
}
Expand Down
282 changes: 157 additions & 125 deletions src/components/Tutorials/subComps/ImageDrawer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,72 +50,60 @@ const ImageDrawer = ({ onClose, visible, owner, tutorial_id, imageURLs }) => {
}) => deleting_error
);

const [uploadSnackbarOpen, setUploadSnackbarOpen] = React.useState(false);
const [uploadErrorSnackbarOpen, setUploadErrorSnackbarOpen] =
React.useState(false);
const [deleteSnackbarOpen, setDeleteSnackbarOpen] = React.useState(false);
const [deleteErrorSnackbarOpen, setDeleteErrorSnackbarOpen] =
React.useState(false);

useEffect(() => {
if (uploading === false && uploading_error === false) {
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message="Image Uploaded successfully...."
/>;
setUploadSnackbarOpen(true);
} else if (uploading === false && uploading_error) {
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message={uploading_error}
/>;
setUploadErrorSnackbarOpen(true);
}
}, [uploading, uploading_error]);

useEffect(() => {
if (deleting === false && deleting_error === false) {
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message="Deleted Succefully...."
/>;
setDeleteSnackbarOpen(true);
} else if (deleting === false && deleting_error) {
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message={deleting_error}
/>;
setDeleteErrorSnackbarOpen(true);
}
}, [deleting, deleting_error]);

const handleSnackbarClose = type => {
switch (type) {
case "upload":
setUploadSnackbarOpen(false);
setUploadErrorSnackbarOpen(false);
break;
case "delete":
setDeleteSnackbarOpen(false);
setDeleteErrorSnackbarOpen(false);
break;
default:
break;
}
};

useEffect(() => {
clearTutorialImagesReducer()(dispatch);
return () => {
clearTutorialImagesReducer()(dispatch);
};
}, [dispatch]);

const props = {
name: "file",
multiple: true,
beforeUpload(file, files) {
uploadTutorialImages(owner, tutorial_id, files)(
firebase,
firestore,
dispatch
);
return false;
}
const beforeUpload = async files => {
console.log("Image Upload Started!!!")
await uploadTutorialImages(owner, tutorial_id, files)(
firebase,
firestore,
dispatch
);
console.log("Uploaded the Images");
return false;
};

const deleteFile = (name, url) =>
Expand All @@ -127,85 +115,129 @@ const ImageDrawer = ({ onClose, visible, owner, tutorial_id, imageURLs }) => {
)(firebase, firestore, dispatch);

return (
<Drawer
title="Images"
data-testid="imageDrawer"
anchor="right"
closable={true}
onClose={onClose}
open={visible}
getContainer={true}
style={{ position: "absolute" }}
width="400px"
className="image-drawer"
destroyOnClose={true}
maskClosable={false}
>
<div className="col-pad-24" data-testId="tutorialImgUpload">
<Grid>
<input
id="file-upload"
fullWidth
accept="image/*"
type="file"
{...props}
/>
{uploading ? (
<>
<LoadingOutlined /> Please wait...
<p className="ant-upload-hint mt-8">Uploading image(s)...</p>
</>
) : (
<>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">
Click or drag images to here to upload
</p>
</>
)}
</Grid>
{imageURLs &&
imageURLs.length > 0 &&
imageURLs.map((image, i) => (
<Grid className="mb-24" key={i}>
<Grid xs={24} md={8}>
<img src={image.url} alt="" />
</Grid>
<Grid xs={24} md={16} className="pl-8" style={{}}>
<h4 className="pb-8">{image.name}</h4>

<CopyToClipboard
text={`![alt=image; scale=1.0](${image.url})`}
onCopy={() => (
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message="Copied...."
/>
)}
>
<Button type="primary">Copy URL</Button>
</CopyToClipboard>

<Button
loading={deleting}
onClick={() => deleteFile(image.name, image.url)}
type="ghost"
danger
>
Delete
</Button>
<>
<Drawer
title="Images"
data-testid="imageDrawer"
anchor="right"
closable={true}
onClose={onClose}
open={visible}
getContainer={true}
style={{ position: "absolute" }}
width="400px"
className="image-drawer"
destroyOnClose={true}
maskClosable={false}
>
<div className="col-pad-24" data-testId="tutorialImgUpload">
<Grid>
<input
id="file-upload"
fullWidth
accept="image/*"
type="file"
name="file"
multiple
onChange={event => beforeUpload(event.target.files)}
/>
{uploading ? (
<>
<LoadingOutlined /> Please wait...
<p className="ant-upload-hint mt-8">Uploading image(s)...</p>
</>
) : (
<>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">
Click or drag images to here to upload
</p>
</>
)}
</Grid>
{imageURLs &&
imageURLs.length > 0 &&
imageURLs.map((image, i) => (
<Grid className="mb-24" key={i}>
<Grid xs={24} md={8}>
<img src={image.url} alt="" />
</Grid>
<Grid xs={24} md={16} className="pl-8" style={{}}>
<h4 className="pb-8">{image.name}</h4>

<CopyToClipboard
text={`![alt=image; scale=1.0](${image.url})`}
onCopy={() => (
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={true}
autoHideDuration={6000}
message="Copied...."
/>
)}
>
<Button type="primary">Copy URL</Button>
</CopyToClipboard>

<Button
loading={deleting}
onClick={() => deleteFile(image.name, image.url)}
type="ghost"
danger
>
Delete
</Button>
</Grid>
</Grid>
</Grid>
))}
</div>
</Drawer>
))}
</div>
</Drawer>
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={uploadSnackbarOpen}
autoHideDuration={6000}
onClose={() => handleSnackbarClose("upload")}
message="Image Uploaded successfully...."
/>
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={uploadErrorSnackbarOpen}
autoHideDuration={6000}
onClose={() => handleSnackbarClose("upload")}
message={uploading_error}
/>
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={deleteSnackbarOpen}
autoHideDuration={6000}
onClose={() => handleSnackbarClose("delete")}
message="Deleted Successfully...."
/>
<Snackbar
anchorOrigin={{
vertical: "bottom",
horizontal: "left"
}}
open={deleteErrorSnackbarOpen}
autoHideDuration={6000}
onClose={() => handleSnackbarClose("delete")}
message={deleting_error}
/>
</>
);
};

Expand Down
Loading