Skip to content

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

refactor: attachments

thecodrr83221ad91d35
diff --git a/apps/web/src/app-effects.js b/apps/web/src/app-effects.js
index f604f5ba1..7bce3883a 100644
--- a/apps/web/src/app-effects.js
+++ b/apps/web/src/app-effects.js
@@ -65,20 +65,18 @@ function AppEffects({ isMobile, isTablet, setShow, slideToIndex }) {
const attachmentsLoadingEvent = EV.subscribe(
EVENTS.attachmentsLoading,
({ type, total, current }) => {
- const key =
+ const [key, status] =
type === "download"
- ? "downloadingAttachments"
- : "uploadingAttachments";
+ ? ["downloadingAttachments", `Downloading`]
+ : ["uploadingAttachments", `Uploading`];
- if (current === total) {
- setProcessingStatus(key);
- return;
- }
- const status =
- type === "download"
- ? `Downloading attachments (${current}/${total})`
- : `Uploading attachments (${current}/${total})`;
- setProcessingStatus(key, status, 0);
+ if (current === total) setProcessingStatus(key);
+ else
+ setProcessingStatus(
+ key,
+ `${status} attachments (${current}/${total})`,
+ 0
+ );
}
);
@@ -91,11 +89,9 @@ function AppEffects({ isMobile, isTablet, setShow, slideToIndex }) {
: "uploadingAttachments";
const { status } = store.get().processingStatuses[key];
const percent = Math.round((loaded / total) * 100);
- if (loaded === total) {
- setProcessingStatus(key, status, 100);
- return;
- }
- setProcessingStatus(key, status, percent);
+
+ if (loaded === total) setProcessingStatus(key, status, 100);
+ else setProcessingStatus(key, status, percent);
}
);
diff --git a/apps/web/src/components/editor/editor.css b/apps/web/src/components/editor/editor.css
index 3a81a2cb2..dcbe3785d 100644
--- a/apps/web/src/components/editor/editor.css
+++ b/apps/web/src/components/editor/editor.css
@@ -93,69 +93,6 @@
border-color: #b1b1b1 !important;
}
-span.attachment {
- overflow: hidden;
- position: relative;
- z-index: 1;
- user-select: none;
- display: inline-flex;
- align-items: center;
- background-color: var(--bgSecondary);
- padding: 0px 5px 0px 22px;
- border-radius: 5px;
- border: 1px solid var(--border);
- font-size: 0.85rem;
- cursor: pointer !important;
-}
-
-span.attachment::before {
- display: inline-block;
- z-index: -1;
- position: absolute;
- top: 0;
- left: 0;
- content: "";
- width: var(--progress, 0px);
- height: 100%;
-
- transition: width 100ms ease-in;
-
- background-size: 15px 15px;
- background-repeat: no-repeat;
- background-position: 5px 1.5px;
- background-color: var(--border);
-}
-
-span.attachment:hover {
- background-color: var(--hover);
-}
-
-span.attachment:not([data-progress])::after {
- content: attr(data-size);
- color: var(--fontTertiary);
- margin-left: 5px;
- font-size: 0.7rem;
-}
-
-span.attachment[data-progress]::after {
- content: attr(data-progress);
- color: var(--fontTertiary);
- margin-left: 5px;
- font-size: 0.7rem;
-}
-
-span.attachment em::before {
- content: "";
- position: absolute;
- width: 15px;
- height: 15px;
- left: 5px;
- top: 1.5px;
- background-color: var(--text);
- mask-size: 15px 15px;
- mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24' width='24'%3E%3Cpath d='M7.5,18A5.5,5.5 0 0,1 2,12.5A5.5,5.5 0 0,1 7.5,7H18A4,4 0 0,1 22,11A4,4 0 0,1 18,15H9.5A2.5,2.5 0 0,1 7,12.5A2.5,2.5 0 0,1 9.5,10H17V11.5H9.5A1,1 0 0,0 8.5,12.5A1,1 0 0,0 9.5,13.5H18A2.5,2.5 0 0,0 20.5,11A2.5,2.5 0 0,0 18,8.5H7.5A4,4 0 0,0 3.5,12.5A4,4 0 0,0 7.5,16.5H17V18H7.5Z'%3E%3C/path%3E%3C/svg%3E");
-}
-
img {
border-radius: 5px;
max-width: 100% !important;
diff --git a/apps/web/src/components/editor/plugins/attachments.js b/apps/web/src/components/editor/plugins/attachments.js
deleted file mode 100644
index d0fb6cdce..000000000
--- a/apps/web/src/components/editor/plugins/attachments.js
+++ /dev/null
@@ -1,78 +0,0 @@
-function register(editor) {
- editor.addCommand("mceAttachImage", function (image) {
- insertImage(editor, image);
- });
-
- editor.addCommand("mceAttachFile", function (file) {
- insertFile(editor, file);
- });
-
- editor.addCommand("mceReplaceImage", function (image) {
- const { hash, src } = image;
- console.log("mceReplaceImage", hash, src);
- const element = editor.dom.$(`img[data-hash="${hash}"]`);
- if (!element || !src || !hash) return;
- element.attr("src", src);
- });
-
- editor.addCommand("mceUpdateAttachmentProgress", function (progressState) {
- const { hash, total, loaded } = progressState;
- const element = document.querySelector(
- `span.attachment[data-hash="${hash}"]`
- );
- if (!element || !total || !loaded) return;
- if (total === loaded) {
- element.removeAttribute("data-progress");
- element.style.removeProperty("--progress");
- } else {
- const percent = Math.round((loaded / total) * 100);
- element.setAttribute("data-progress", `${percent}%`);
- element.style.setProperty("--progress", `${percent}%`);
- }
- });
-}
-
-async function insertImage(editor, image) {
- var content = `
- <img class="attachment"
- alt="${image.filename || image.hash}"
- data-mime="${image.type}"
- data-hash="${image.hash}"
- data-filename="${image.filename}"
- src="${image.dataurl}"
- data-size="${image.size}"
- style="float: left;"/>`;
- editor.insertContent(content);
-}
-
-async function insertFile(editor, file) {
- var content = `
- <span
- class="attachment"
- contenteditable="false"
- title="${file.filename}"
- data-mime="${file.type}"
- data-filename="${file.filename}"
- data-hash="${file.hash}"
- data-size="${formatBytes(file.size, 1)}">
- <em>&nbsp;</em>
- ${file.filename}
- </span>`;
- editor.insertContent(content);
-}
-
-function formatBytes(bytes, decimals = 2) {
- if (bytes === 0) return "0B";
-
- const k = 1024;
- const dm = decimals < 0 ? 0 : decimals;
- const sizes = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
-
- const i = Math.floor(Math.log(bytes) / Math.log(k));
-
- return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i];
-}
-
-(function init() {
- global.tinymce.PluginManager.add("attachments", register);
-})();
diff --git a/apps/web/src/components/editor/plugins/attachmentshandler.css b/apps/web/src/components/editor/plugins/attachmentshandler.css
new file mode 100644
index 000000000..a27394386
--- /dev/null
+++ b/apps/web/src/components/editor/plugins/attachmentshandler.css
@@ -0,0 +1,62 @@
+span.attachment {
+ overflow: hidden;
+ position: relative;
+ z-index: 1;
+ user-select: none;
+ display: inline-flex;
+ align-items: center;
+ background-color: var(--bgSecondary);
+ padding: 0px 5px 0px 22px;
+ border-radius: 5px;
+ border: 1px solid var(--border);
+ font-size: 0.85rem;
+ cursor: pointer !important;
+}
+
+span.attachment::before {
+ display: inline-block;
+ z-index: -1;
+ position: absolute;
+ top: 0;
+ left: 0;
+ content: "";
+ width: var(--progress, 0px);
+ height: 100%;
+
+ transition: width 100ms ease-in;
+
+ background-size: 15px 15px;
+ background-repeat: no-repeat;
+ background-position: 5px 1.5px;
+ background-color: var(--border);
+}
+
+span.attachment:hover {
+ background-color: var(--hover);
+}
+
+span.attachment:not([data-progress])::after {
+ content: attr(data-size);
+ color: var(--fontTertiary);
+ margin-left: 5px;
+ font-size: 0.7rem;
+}
+
+span.attachment[data-progress]::after {
+ content: attr(data-progress);
+ color: var(--fontTertiary);
+ margin-left: 5px;
+ font-size: 0.7rem;
+}
+
+span.attachment em::before {
+ content: "";
+ position: absolute;
+ width: 15px;
+ height: 15px;
+ left: 5px;
+ top: 1.5px;
+ background-color: var(--text);
+ mask-size: 15px 15px;
+ mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24' width='24'%3E%3Cpath d='M7.5,18A5.5,5.5 0 0,1 2,12.5A5.5,5.5 0 0,1 7.5,7H18A4,4 0 0,1 22,11A4,4 0 0,1 18,15H9.5A2.5,2.5 0 0,1 7,12.5A2.5,2.5 0 0,1 9.5,10H17V11.5H9.5A1,1 0 0,0 8.5,12.5A1,1 0 0,0 9.5,13.5H18A2.5,2.5 0 0,0 20.5,11A2.5,2.5 0 0,0 18,8.5H7.5A4,4 0 0,0 3.5,12.5A4,4 0 0,0 7.5,16.5H17V18H7.5Z'%3E%3C/path%3E%3C/svg%3E");
+}
diff --git a/apps/web/src/components/editor/tinymce.js b/apps/web/src/components/editor/tinymce.js
index 3a05a34fb..be798a37b 100644
--- a/apps/web/src/components/editor/tinymce.js
+++ b/apps/web/src/components/editor/tinymce.js
@@ -29,9 +29,10 @@ import "@streetwritersco/tinymce-plugins/collapsibleheaders";
import "@streetwritersco/tinymce-plugins/paste";
import "@streetwritersco/tinymce-plugins/shortcuts";
import "@streetwritersco/tinymce-plugins/keyboardquirks";
+import "@streetwritersco/tinymce-plugins/attachmentshandler";
import "./plugins/picker";
-import "./plugins/attachments";
import "./plugins/icons";
+import "./plugins/attachmentshandler.css";
import { Editor } from "@tinymce/tinymce-react";
import { showBuyDialog } from "../../common/dialog-controller";
import { useStore as useThemeStore } from "../../stores/theme-store";
@@ -112,7 +113,7 @@ const plugins = {
default:
"importcss searchreplace autolink directionality media table hr advlist lists imagetools noneditable autoresize",
custom:
- "icons keyboardquirks collapsibleheaders shortlink attachments picker paste codeblock inlinecode shortcuts checklist",
+ "icons keyboardquirks collapsibleheaders shortlink attachmentshandler picker paste codeblock inlinecode shortcuts checklist",
pro: "textpattern",
};
@@ -162,9 +163,9 @@ function TinyMCE(props) {
const mediaAttachmentDownloadedEvent = EV.subscribe(
EVENTS.mediaAttachmentDownloaded,
- (arg) => {
- console.log("downloaded attachment", arg);
- tinymceRef.current.editor.execCommand("mceReplaceImage", arg);
+ (image) => {
+ const { hash, src } = image;
+ tinymceRef.current.editor.execCommand("mceReplaceImage", { hash, src });
}
);
return () => {
@@ -236,6 +237,9 @@ function TinyMCE(props) {
span: "--progress",
},
extended_valid_elements: `img[*|src=placeholder.svg]`,
+ attachmenthandler_download_attachment: (hash) => {
+ console.log(hash);
+ },
}}
onBeforeExecCommand={async (command) => {
const isPremiumCommand = premiumCommands.some((cmd) => {