Skip to content

Commit

Permalink
feat: add image paste plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
muhsin-k committed Sep 4, 2024
1 parent a0d8274 commit 86fb9d4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/plugins/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Plugin } from "prosemirror-state";

/**
* Replaces an image node in the editor with a new image URL.
*
* @param {string} currentUrl - The current URL of the image to be replaced.
* @param {string} newUrl - The new URL to replace the current image with.
* @param {EditorView} view - The ProseMirror editor view.
*/
function replaceImage(currentUrl, newUrl, view) {
view.state.doc.descendants((node, pos) => {
if (node.type.name === "image" && node.attrs.src === currentUrl) {
const tr = view.state.tr.setNodeMarkup(pos, null, {
...node.attrs,
src: newUrl,
});
view.dispatch(tr);
}
});
}

/**
* Creates a ProseMirror plugin that handles image pasting and uploading.
*
* @param {Function} uploadImage - A function that takes an image URL and returns a Promise
* that resolves to the new URL after uploading.
* @returns {Plugin} A ProseMirror plugin that handles image pasting.
*/
const imagePastePlugin = (uploadImage) =>
new Plugin({
props: {
/**
* Handles the paste event in the editor.
*
* @param {EditorView} view - The ProseMirror editor view.
* @param {Event} event - The paste event.
* @param {Slice} slice - The ProseMirror Slice object representing the pasted content.
*/
handlePaste(view, event, slice) {
const imageUrls = [];
slice.content.descendants((node) => {
if (node.type.name === "image") {
imageUrls.push(node.attrs.src);
}
});
Promise.all(imageUrls.map(async (url) => {
try {
const newUrl = await uploadImage(url);
replaceImage(url, newUrl, view);
} catch (error) {
console.error("Error uploading image:", error);
}
}));
},
},
});

export default imagePastePlugin;

0 comments on commit 86fb9d4

Please sign in to comment.