-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WEB-1244] fix: add better image insertion and replacement logic in t…
…he editor (#4508) * fix: add better image insertion and replacement logic * refactor: image handling in editor * chore: remove passing uploadKey around * refactor: remove unused code * fix: redundant files removed * fix: add is editor ready to discard api to control behvaiours from our app * fix: focus issues and image insertion position when not using slash command * fix: import order fixed
- Loading branch information
1 parent
061a447
commit ade6ede
Showing
22 changed files
with
483 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { PluginKey } from "@tiptap/pm/state"; | ||
|
||
export const uploadKey = new PluginKey("upload-image"); | ||
export const deleteKey = new PluginKey("delete-image"); | ||
export const restoreKey = new PluginKey("restore-image"); | ||
|
||
export const IMAGE_NODE_TYPE = "image"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { EditorState, Plugin, Transaction } from "@tiptap/pm/state"; | ||
import { DeleteImage } from "src/types/delete-image"; | ||
import { Editor } from "@tiptap/core"; | ||
|
||
import { type ImageNode } from "src/ui/plugins/image/types/image-node"; | ||
import { deleteKey, IMAGE_NODE_TYPE } from "src/ui/plugins/image/constants"; | ||
|
||
export const TrackImageDeletionPlugin = (editor: Editor, deleteImage: DeleteImage): Plugin => | ||
new Plugin({ | ||
key: deleteKey, | ||
appendTransaction: (transactions: readonly Transaction[], oldState: EditorState, newState: EditorState) => { | ||
const newImageSources = new Set<string>(); | ||
newState.doc.descendants((node) => { | ||
if (node.type.name === IMAGE_NODE_TYPE) { | ||
newImageSources.add(node.attrs.src); | ||
} | ||
}); | ||
|
||
transactions.forEach((transaction) => { | ||
// transaction could be a selection | ||
if (!transaction.docChanged) return; | ||
|
||
const removedImages: ImageNode[] = []; | ||
|
||
// iterate through all the nodes in the old state | ||
oldState.doc.descendants((oldNode) => { | ||
// if the node is not an image, then return as no point in checking | ||
if (oldNode.type.name !== IMAGE_NODE_TYPE) return; | ||
|
||
// Check if the node has been deleted or replaced | ||
if (!newImageSources.has(oldNode.attrs.src)) { | ||
removedImages.push(oldNode as ImageNode); | ||
} | ||
}); | ||
|
||
removedImages.forEach(async (node) => { | ||
const src = node.attrs.src; | ||
editor.storage.image.deletedImageSet.set(src, true); | ||
await onNodeDeleted(src, deleteImage); | ||
}); | ||
}); | ||
|
||
return null; | ||
}, | ||
}); | ||
|
||
async function onNodeDeleted(src: string, deleteImage: DeleteImage): Promise<void> { | ||
try { | ||
const assetUrlWithWorkspaceId = new URL(src).pathname.substring(1); | ||
await deleteImage(assetUrlWithWorkspaceId); | ||
} catch (error) { | ||
console.error("Error deleting image: ", error); | ||
} | ||
} |
Oops, something went wrong.