Skip to content

Commit

Permalink
fixup! feat(text-editor): add support for pasting inline images
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikWallstrom committed Mar 5, 2025
1 parent 5aae2b8 commit 7503410
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ImageState,
} from '../../../text-editor.types';
import { Node } from 'prosemirror-model';
import { imageCache } from './node';

export const pluginKey = new PluginKey('imageInserterPlugin');

Expand Down Expand Up @@ -83,6 +84,8 @@ const findAndHandleRemovedImages = (
state: removedImage.attrs.state,
};
imageRemovedCallback(imageInfo);

imageCache.delete(removedImage.attrs.fileInfoId);

Check failure on line 88 in src/components/text-editor/prosemirror-adapter/plugins/image/inserter.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `········`
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NodeSpec, Node, Attrs, DOMOutputSpec } from 'prosemirror-model';
import { ImageState } from '../../../text-editor.types';
import { MarkdownSerializerState } from 'prosemirror-markdown';

const imageCache = new Map<string, HTMLImageElement>();
export const imageCache = new Map<string, HTMLImageElement>();

type MarkdownSerializerFunction = (
state: MarkdownSerializerState,
Expand Down Expand Up @@ -96,16 +96,15 @@ function createImageNodeSpec(): NodeSpec {
return document.createElement('span');
}

if (imageCache.has(node.attrs.fileInfoId)) {
const img = imageCache.get(node.attrs.fileInfoId);

return updateImageElement(img, node);
let img = imageCache.get(node.attrs.fileInfoId);
if (img) {
updateImageElement(img, node);
} else {
const img = createImageElement(node);
img = createImageElement(node);
imageCache.set(node.attrs.fileInfoId, img);

return img;
}

return img;
},
parseDOM: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
ImageInfo,
} from '../text-editor.types';
import { getTableNodes, getTableEditingPlugins } from './plugins/table-plugin';
import { getImageNode } from './plugins/image/node';
import { getImageNode, imageCache } from './plugins/image/node';

const DEBOUNCE_TIMEOUT = 300;

Expand Down Expand Up @@ -230,6 +230,8 @@ export class ProsemirrorAdapter {
}

public disconnectedCallback() {
imageCache.clear();

this.host.removeEventListener(
'open-editor-link-menu',
this.handleOpenLinkMenu,
Expand Down

0 comments on commit 7503410

Please sign in to comment.