editor: use dataurl instead of src attr for loading images

This commit is contained in:
Abdullah Atta
2023-03-22 09:34:06 +05:00
committed by Abdullah Atta
parent eaaecbd5e5
commit 08106c20e8
3 changed files with 33 additions and 6 deletions

View File

@@ -398,7 +398,7 @@ function toIEditor(editor: Editor): IEditor {
loadImage: (hash, src) => loadImage: (hash, src) =>
editor.current?.commands.updateImage( editor.current?.commands.updateImage(
{ hash }, { hash },
{ hash, src, preventUpdate: true } { hash, dataurl: src, preventUpdate: true }
), ),
sendAttachmentProgress: (hash, type, progress) => sendAttachmentProgress: (hash, type, progress) =>
editor.current?.commands.setAttachmentProgress({ editor.current?.commands.setAttachmentProgress({

View File

@@ -44,7 +44,8 @@ export function ImageComponent(
) { ) {
const { editor, node, selected } = props; const { editor, node, selected } = props;
const isMobile = useIsMobile(); const isMobile = useIsMobile();
const { src, alt, title, width, height, textDirection, hash } = node.attrs; const { dataurl, src, alt, title, width, height, textDirection, hash } =
node.attrs;
const float = isMobile ? false : node.attrs.float; const float = isMobile ? false : node.attrs.float;
let align = node.attrs.align; let align = node.attrs.align;
@@ -58,9 +59,10 @@ export function ImageComponent(
useEffect( useEffect(
() => { () => {
(async () => { (async () => {
if (!src) return; if (!src && !dataurl) return;
try { try {
if (isDataUrl(src)) setSource(await toBlobURL(src)); if (dataurl) setSource(await toBlobURL(dataurl));
else if (isDataUrl(src)) setSource(await toBlobURL(src));
else { else {
const { url, size, blob, type } = await downloadImage( const { url, size, blob, type } = await downloadImage(
src, src,
@@ -79,7 +81,7 @@ export function ImageComponent(
})(); })();
}, },
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[src, imageRef, downloadOptions] [src, dataurl, imageRef, downloadOptions]
); );
return ( return (

View File

@@ -35,9 +35,29 @@ export interface ImageOptions {
HTMLAttributes: Record<string, unknown>; HTMLAttributes: Record<string, unknown>;
} }
/**
* We have two attributes that store the source of an image:
*
* 1. `src`
* 2. `dataurl`
*
* `src` is the image's inherent source. This can contain a URL, a base64-dataurl,
* a blob...etc. We should never touch this attribute. This is also where we store
* the data that we want to upload so after we download a pasted image, its base64
* dataurl goes in this attribute.
*
* `dataurl` should never get added to the final HTML. This attribute is where we
* restore an image's data after loading a note.
*
* The reason we have 2 instead of a single attribute is to avoid unnecessary processing.
* Keeping everything in the `src` attribute requires us to always send the rendered image
* along with everything else. This is pointless because we already have the image's rendered
* data.
*/
export type ImageAttributes = Partial<ImageSizeOptions> & export type ImageAttributes = Partial<ImageSizeOptions> &
Partial<Attachment> & { Partial<Attachment> & {
src: string; src: string;
dataurl?: string;
alt?: string; alt?: string;
title?: string; title?: string;
textDirection?: TextDirections; textDirection?: TextDirections;
@@ -114,7 +134,12 @@ export const ImageNode = Node.create<ImageOptions>({
hash: getDataAttribute("hash"), hash: getDataAttribute("hash"),
filename: getDataAttribute("filename"), filename: getDataAttribute("filename"),
type: getDataAttribute("mime"), type: getDataAttribute("mime"),
size: getDataAttribute("size") size: getDataAttribute("size"),
dataurl: {
...getDataAttribute("dataurl"),
rendered: false
}
}; };
}, },