forked from github/plane
impoved complexity to O(1) from O(n) for large docs
This commit is contained in:
parent
814af00402
commit
0e5f82beba
@ -3,7 +3,6 @@ import { Node as ProseMirrorNode } from "@tiptap/pm/model";
|
|||||||
import fileService from "services/file.service";
|
import fileService from "services/file.service";
|
||||||
|
|
||||||
const deleteKey = new PluginKey("delete-image");
|
const deleteKey = new PluginKey("delete-image");
|
||||||
|
|
||||||
const IMAGE_NODE_TYPE = "image";
|
const IMAGE_NODE_TYPE = "image";
|
||||||
|
|
||||||
interface ImageNode extends ProseMirrorNode {
|
interface ImageNode extends ProseMirrorNode {
|
||||||
@ -17,6 +16,13 @@ const TrackImageDeletionPlugin = (): Plugin =>
|
|||||||
new Plugin({
|
new Plugin({
|
||||||
key: deleteKey,
|
key: deleteKey,
|
||||||
appendTransaction: (transactions: readonly Transaction[], oldState: EditorState, newState: EditorState) => {
|
appendTransaction: (transactions: readonly Transaction[], oldState: EditorState, newState: EditorState) => {
|
||||||
|
const newImageSources = new Set();
|
||||||
|
newState.doc.descendants((node) => {
|
||||||
|
if (node.type.name === IMAGE_NODE_TYPE) {
|
||||||
|
newImageSources.add(node.attrs.src);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
transactions.forEach((transaction) => {
|
transactions.forEach((transaction) => {
|
||||||
if (!transaction.docChanged) return;
|
if (!transaction.docChanged) return;
|
||||||
|
|
||||||
@ -31,14 +37,7 @@ const TrackImageDeletionPlugin = (): Plugin =>
|
|||||||
|
|
||||||
// Check if the node has been deleted or replaced
|
// Check if the node has been deleted or replaced
|
||||||
if (!newNode || newNode.type.name !== IMAGE_NODE_TYPE) {
|
if (!newNode || newNode.type.name !== IMAGE_NODE_TYPE) {
|
||||||
// Check if the node still exists elsewhere in the document
|
if (!newImageSources.has(oldNode.attrs.src)) {
|
||||||
let nodeExists = false;
|
|
||||||
newState.doc.descendants((node) => {
|
|
||||||
if (node.attrs.src === oldNode.attrs.src) {
|
|
||||||
nodeExists = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!nodeExists) {
|
|
||||||
removedImages.push(oldNode as ImageNode);
|
removedImages.push(oldNode as ImageNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user