Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: improve attaching image from url

thecodrre2e04ec27574
diff --git a/packages/editor/dist/extensions/attachment/attachment.js b/packages/editor/dist/extensions/attachment/attachment.js
index 8cb258c18..6b2e8a9fe 100644
--- a/packages/editor/dist/extensions/attachment/attachment.js
+++ b/packages/editor/dist/extensions/attachment/attachment.js
@@ -48,7 +48,7 @@ export var AttachmentNode = Node.create({
},
hash: getDataAttribute("hash"),
filename: getDataAttribute("filename"),
- type: getDataAttribute("type"),
+ type: getDataAttribute("mime"),
size: getDataAttribute("size"),
};
},
@@ -117,8 +117,7 @@ export var AttachmentNode = Node.create({
try {
for (var attachments_1 = __values(attachments), attachments_1_1 = attachments_1.next(); !attachments_1_1.done; attachments_1_1 = attachments_1.next()) {
var attachment = attachments_1_1.value;
- console.log(attachment.node.attrs);
- tr.setNodeMarkup(attachment.pos, attachment.node.type, __assign(__assign({}, attachment.node.attrs), { progress: progress }));
+ tr.setNodeMarkup(attachment.pos, attachment.node.type, __assign(__assign({}, attachment.node.attrs), { progress: progress === 100 ? null : progress }));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
diff --git a/packages/editor/dist/extensions/attachment/component.js b/packages/editor/dist/extensions/attachment/component.js
index 0d02da5c2..4bba46e18 100644
--- a/packages/editor/dist/extensions/attachment/component.js
+++ b/packages/editor/dist/extensions/attachment/component.js
@@ -10,7 +10,7 @@ var __assign = (this && this.__assign) || function () {
return __assign.apply(this, arguments);
};
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
-import { Box, Flex, Text } from "rebass";
+import { Box, Text } from "rebass";
import { useRef } from "react";
// import { MenuPresenter } from "../../components/menu/menu";
import { Icon } from "../../toolbar/components/icon";
@@ -48,7 +48,13 @@ export function AttachmentComponent(props) {
fontSize: "0.65rem",
color: "fontTertiary",
flexShrink: 0,
- } }, { children: progress ? "".concat(progress, "%") : formatBytes(size) })), _jsx(DesktopOnly, { children: selected && (_jsx(Flex, __assign({ sx: { position: "absolute", top: -35 } }, { children: _jsx(ToolbarGroup, { editor: editor, tools: ["removeAttachment", "downloadAttachment"] }) }))) })] })) }));
+ } }, { children: progress ? "".concat(progress, "%") : formatBytes(size) })), _jsx(DesktopOnly, { children: selected && (_jsx(ToolbarGroup, { editor: editor, tools: ["removeAttachment", "downloadAttachment"], sx: {
+ boxShadow: "menu",
+ borderRadius: "default",
+ bg: "background",
+ position: "absolute",
+ top: -35,
+ } })) })] })) }));
}
function formatBytes(bytes, decimals) {
if (decimals === void 0) { decimals = 1; }
diff --git a/packages/editor/dist/extensions/image/image.js b/packages/editor/dist/extensions/image/image.js
index a610afec9..b5c940ca9 100644
--- a/packages/editor/dist/extensions/image/image.js
+++ b/packages/editor/dist/extensions/image/image.js
@@ -74,7 +74,7 @@ export var ImageNode = Node.create({
align: getDataAttribute("align", "left"),
hash: getDataAttribute("hash"),
filename: getDataAttribute("filename"),
- type: getDataAttribute("type"),
+ type: getDataAttribute("mime"),
size: getDataAttribute("size"),
};
},
diff --git a/packages/editor/dist/toolbar/popups/imageupload.js b/packages/editor/dist/toolbar/popups/imageupload.js
index ae29c649a..17144cf3c 100644
--- a/packages/editor/dist/toolbar/popups/imageupload.js
+++ b/packages/editor/dist/toolbar/popups/imageupload.js
@@ -77,9 +77,8 @@ export function ImageUploadPopup(props) {
alignSelf: ["stretch", "end", "end"],
my: 1,
mr: [0, 1],
- py: 2,
}, onClick: function () { return __awaiter(_this, void 0, void 0, function () {
- var response, dataurl, _a;
+ var response, contentType, contentLength, size, dataurl, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, fetch(url)];
@@ -87,12 +86,20 @@ export function ImageUploadPopup(props) {
response = _b.sent();
if (!response.ok)
return [2 /*return*/]; // TODO show error
+ contentType = response.headers.get("Content-Type");
+ contentLength = response.headers.get("Content-Length");
+ if (!contentType ||
+ !contentLength ||
+ contentLength === "0" ||
+ !contentType.startsWith("image/"))
+ return [2 /*return*/];
+ size = parseInt(contentLength);
_a = toDataURL;
return [4 /*yield*/, response.blob()];
case 2: return [4 /*yield*/, _a.apply(void 0, [_b.sent()])];
case 3:
dataurl = _b.sent();
- onInsert({ src: dataurl });
+ onInsert({ src: dataurl, type: contentType, size: size });
return [2 /*return*/];
}
});
diff --git a/packages/editor/dist/toolbar/tools/block.js b/packages/editor/dist/toolbar/tools/block.js
index 81230ebb4..e1919b2c0 100644
--- a/packages/editor/dist/toolbar/tools/block.js
+++ b/packages/editor/dist/toolbar/tools/block.js
@@ -52,7 +52,7 @@ export function InsertBlock(props) {
isMobile ? embedMobile(editor) : embedDesktop(editor),
table(editor),
];
- }, [isMobile]);
+ }, [editor, isMobile]);
return (_jsxs(_Fragment, { children: [_jsx(Button, __assign({ ref: buttonRef, sx: {
p: 1,
m: 0,
@@ -233,7 +233,7 @@ var uploadImageFromURL = function (editor) { return ({
showPopup({
theme: editor.storage.theme,
popup: function (hide) { return (_jsx(ImageUploadPopup, { onInsert: function (image) {
- editor === null || editor === void 0 ? void 0 : editor.commands.insertImage(image);
+ editor === null || editor === void 0 ? void 0 : editor.chain().focus().insertImage(image).run();
hide();
}, onClose: hide })); },
});
diff --git a/packages/editor/src/extensions/attachment/attachment.ts b/packages/editor/src/extensions/attachment/attachment.ts
index 4c1ab06a5..6e65d5bd9 100644
--- a/packages/editor/src/extensions/attachment/attachment.ts
+++ b/packages/editor/src/extensions/attachment/attachment.ts
@@ -66,7 +66,7 @@ export const AttachmentNode = Node.create<AttachmentOptions>({
},
hash: getDataAttribute("hash"),
filename: getDataAttribute("filename"),
- type: getDataAttribute("type"),
+ type: getDataAttribute("mime"),
size: getDataAttribute("size"),
};
},
@@ -129,10 +129,9 @@ export const AttachmentNode = Node.create<AttachmentOptions>({
node.attrs.hash === hash
);
for (const attachment of attachments) {
- console.log(attachment.node.attrs);
tr.setNodeMarkup(attachment.pos, attachment.node.type, {
...attachment.node.attrs,
- progress,
+ progress: progress === 100 ? null : progress,
});
}
tr.setMeta("preventUpdate", true);
diff --git a/packages/editor/src/extensions/attachment/component.tsx b/packages/editor/src/extensions/attachment/component.tsx
index 2385b5c77..0dc8a0454 100644
--- a/packages/editor/src/extensions/attachment/component.tsx
+++ b/packages/editor/src/extensions/attachment/component.tsx
@@ -70,12 +70,17 @@ export function AttachmentComponent(
</Text>
<DesktopOnly>
{selected && (
- <Flex sx={{ position: "absolute", top: -35 }}>
- <ToolbarGroup
- editor={editor}
- tools={["removeAttachment", "downloadAttachment"]}
- />
- </Flex>
+ <ToolbarGroup
+ editor={editor}
+ tools={["removeAttachment", "downloadAttachment"]}
+ sx={{
+ boxShadow: "menu",
+ borderRadius: "default",
+ bg: "background",
+ position: "absolute",
+ top: -35,
+ }}
+ />
)}
</DesktopOnly>
</Box>
diff --git a/packages/editor/src/extensions/image/image.ts b/packages/editor/src/extensions/image/image.ts
index 7996126d0..147d0ea51 100644
--- a/packages/editor/src/extensions/image/image.ts
+++ b/packages/editor/src/extensions/image/image.ts
@@ -90,7 +90,7 @@ export const ImageNode = Node.create<ImageOptions>({
align: getDataAttribute("align", "left"),
hash: getDataAttribute("hash"),
filename: getDataAttribute("filename"),
- type: getDataAttribute("type"),
+ type: getDataAttribute("mime"),
size: getDataAttribute("size"),
};
},
diff --git a/packages/editor/src/toolbar/popups/image-upload.tsx b/packages/editor/src/toolbar/popups/image-upload.tsx
index 0ed2a2773..7ba828498 100644
--- a/packages/editor/src/toolbar/popups/image-upload.tsx
+++ b/packages/editor/src/toolbar/popups/image-upload.tsx
@@ -32,13 +32,25 @@ export function ImageUploadPopup(props: ImageUploadPopupProps) {
alignSelf: ["stretch", "end", "end"],
my: 1,
mr: [0, 1],
- py: 2,
}}
onClick={async () => {
const response = await fetch(url);
if (!response.ok) return; // TODO show error
+
+ const contentType = response.headers.get("Content-Type");
+ const contentLength = response.headers.get("Content-Length");
+
+ if (
+ !contentType ||
+ !contentLength ||
+ contentLength === "0" ||
+ !contentType.startsWith("image/")
+ )
+ return;
+
+ const size = parseInt(contentLength);
const dataurl = await toDataURL(await response.blob());
- onInsert({ src: dataurl });
+ onInsert({ src: dataurl, type: contentType, size });
}}
disabled={!url.trim()}
>
diff --git a/packages/editor/src/toolbar/tools/block.tsx b/packages/editor/src/toolbar/tools/block.tsx
index f904d525a..8841c1bad 100644
--- a/packages/editor/src/toolbar/tools/block.tsx
+++ b/packages/editor/src/toolbar/tools/block.tsx
@@ -40,7 +40,7 @@ export function InsertBlock(props: ToolProps) {
isMobile ? embedMobile(editor) : embedDesktop(editor),
table(editor),
];
- }, [isMobile]);
+ }, [editor, isMobile]);
return (
<>
@@ -269,7 +269,7 @@ const uploadImageFromURL = (editor: Editor | null): MenuItem => ({
popup: (hide) => (
<ImageUploadPopup
onInsert={(image) => {
- editor?.commands.insertImage(image);
+ editor?.chain().focus().insertImage(image).run();
hide();
}}
onClose={hide}