Skip to content

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

feat: add attachments support

thecodrr247befaf63e0
diff --git a/packages/editor/dist/components/menu/useMenu.js b/packages/editor/dist/components/menu/useMenu.js
index e6c5b4cef..761b7bd29 100644
--- a/packages/editor/dist/components/menu/useMenu.js
+++ b/packages/editor/dist/components/menu/useMenu.js
@@ -47,6 +47,9 @@ export function getPosition(element, options) {
if (target !== "mouse" && align === "center" && elementWidth > 0) {
position.left -= elementWidth / 2 - target.clientWidth / 2;
}
+ else if (target !== "mouse" && align === "end" && elementWidth > 0) {
+ position.left -= elementWidth - target.clientWidth;
+ }
// Adjust menu height
if (elementHeight > windowHeight - position.top) {
element.style.maxHeight = "".concat(windowHeight - 20, "px");
diff --git a/packages/editor/dist/extensions/attachment/attachment.d.ts b/packages/editor/dist/extensions/attachment/attachment.d.ts
new file mode 100644
index 000000000..3d3acbe15
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/attachment.d.ts
@@ -0,0 +1,19 @@
+import { Node } from "@tiptap/core";
+export interface AttachmentOptions {
+ onDownloadAttachment: (attachment: Attachment) => boolean;
+}
+export declare type Attachment = {
+ hash: string;
+ filename: string;
+ type: string;
+ size: number;
+};
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ attachment: {
+ insertAttachment: (attachment: Attachment) => ReturnType;
+ downloadAttachment: (attachment: Attachment) => ReturnType;
+ };
+ }
+}
+export declare const AttachmentNode: Node<AttachmentOptions, any>;
diff --git a/packages/editor/dist/extensions/attachment/attachment.js b/packages/editor/dist/extensions/attachment/attachment.js
new file mode 100644
index 000000000..79d300f74
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/attachment.js
@@ -0,0 +1,88 @@
+import { Node, mergeAttributes } from "@tiptap/core";
+import { ReactNodeViewRenderer } from "@tiptap/react";
+import { AttachmentComponent } from "./component";
+export var AttachmentNode = Node.create({
+ name: "attachment",
+ content: "inline*",
+ marks: "",
+ inline: true,
+ atom: true,
+ addOptions: function () {
+ return {
+ // HTMLAttributes: {},
+ onDownloadAttachment: function () { return false; },
+ };
+ },
+ group: function () {
+ return "inline";
+ },
+ draggable: true,
+ addAttributes: function () {
+ return {
+ hash: getDataAttribute("hash"),
+ filename: getDataAttribute("filename"),
+ type: getDataAttribute("type"),
+ size: getDataAttribute("size"),
+ };
+ },
+ parseHTML: function () {
+ return [
+ {
+ tag: "span[data-hash]",
+ },
+ ];
+ },
+ renderHTML: function (_a) {
+ var HTMLAttributes = _a.HTMLAttributes;
+ return ["span", mergeAttributes(HTMLAttributes)];
+ },
+ addNodeView: function () {
+ return ReactNodeViewRenderer(AttachmentComponent);
+ },
+ addCommands: function () {
+ var _this = this;
+ return {
+ insertAttachment: function (attachment) {
+ return function (_a) {
+ var commands = _a.commands;
+ return commands.insertContent({
+ type: _this.name,
+ attrs: attachment,
+ });
+ };
+ },
+ downloadAttachment: function (attachment) {
+ return function (_a) {
+ return _this.options.onDownloadAttachment(attachment);
+ };
+ },
+ };
+ },
+ // addInputRules() {
+ // return [
+ // nodeInputRule({
+ // find: inputRegex,
+ // type: this.type,
+ // getAttributes: (match) => {
+ // const [, , alt, src, title] = match;
+ // return { src, alt, title };
+ // },
+ // }),
+ // ];
+ // },
+});
+function getDataAttribute(name) {
+ return {
+ default: null,
+ parseHTML: function (element) { return element.dataset[name]; },
+ renderHTML: function (attributes) {
+ var _a;
+ if (!attributes[name]) {
+ return {};
+ }
+ return _a = {},
+ _a["data-".concat(name)] = attributes[name],
+ _a;
+ },
+ };
+}
diff --git a/packages/editor/dist/extensions/attachment/component.d.ts b/packages/editor/dist/extensions/attachment/component.d.ts
new file mode 100644
index 000000000..7fb4f0bee
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/component.d.ts
@@ -0,0 +1,4 @@
+/// <reference types="react" />
+import { ImageProps } from "rebass";
+import { NodeViewProps } from "@tiptap/react";
+export declare function AttachmentComponent(props: ImageProps & NodeViewProps): JSX.Element;
diff --git a/packages/editor/dist/extensions/attachment/component.js b/packages/editor/dist/extensions/attachment/component.js
new file mode 100644
index 000000000..d68b48210
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/component.js
@@ -0,0 +1,96 @@
+var __assign = (this && this.__assign) || function () {
+ __assign = Object.assign || function(t) {
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
+ s = arguments[i];
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
+ t[p] = s[p];
+ }
+ return t;
+ };
+ return __assign.apply(this, arguments);
+};
+import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
+import { Box, Flex, Text } from "rebass";
+import { NodeViewWrapper } from "@tiptap/react";
+import { ThemeProvider } from "emotion-theming";
+import { ToolButton } from "../../toolbar/components/tool-button";
+import { useRef } from "react";
+import { MenuPresenter } from "../../components/menu/menu";
+import { Icon } from "../../toolbar/components/icon";
+import { Icons } from "../../toolbar/icons";
+export function AttachmentComponent(props) {
+ var _a = props.node.attrs, hash = _a.hash, filename = _a.filename, size = _a.size;
+ var editor = props.editor, updateAttributes = props.updateAttributes;
+ var elementRef = useRef();
+ var isActive = editor.isActive("attachment", { hash: hash });
+ // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
+ var theme = editor.storage.theme;
+ // useEffect(() => {
+ // setIsToolbarVisible(isActive);
+ // }, [isActive]);
+ return (_jsx(NodeViewWrapper, __assign({ as: "span" }, { children: _jsxs(ThemeProvider, __assign({ theme: theme }, { children: [_jsxs(Box, __assign({ ref: elementRef, as: "span", contentEditable: false, variant: "body", sx: {
+ display: "inline-flex",
+ overflow: "hidden",
+ position: "relative",
+ zIndex: 1,
+ userSelect: "none",
+ alignItems: "center",
+ backgroundColor: "bgSecondary",
+ padding: "0px 5px 0px 5px",
+ borderRadius: "default",
+ border: "1px solid var(--border)",
+ cursor: "pointer",
+ maxWidth: 250,
+ borderColor: isActive ? "primary" : "border",
+ ":hover": {
+ bg: "hover",
+ },
+ }, title: filename }, { children: [_jsx(Icon, { path: Icons.attachment, size: 14 }), _jsx(Text, __assign({ as: "span", sx: {
+ ml: "3px",
+ fontSize: "0.85rem",
+ whiteSpace: "nowrap",
+ textOverflow: "ellipsis",
+ overflow: "hidden",
+ }, className: "filename" }, { children: filename })), _jsx(Text, __assign({ as: "span", className: "size", sx: {
+ ml: "7px",
+ fontSize: "0.7rem",
+ color: "fontTertiary",
+ flexShrink: 0,
+ } }, { children: formatBytes(size) }))] })), _jsx(MenuPresenter, __assign({ isOpen: isActive, onClose: function () { }, items: [], options: {
+ type: "autocomplete",
+ position: {
+ target: elementRef.current || undefined,
+ location: "top",
+ yOffset: -5,
+ isTargetAbsolute: true,
+ align: "end",
+ },
+ } }, { children: _jsx(AttachmentToolbar, { editor: editor }) }))] })) })));
+}
+function formatBytes(bytes, decimals) {
+ if (decimals === void 0) { decimals = 1; }
+ if (bytes === 0)
+ return "0B";
+ var k = 1024;
+ var dm = decimals < 0 ? 0 : decimals;
+ var sizes = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
+ var i = Math.floor(Math.log(bytes) / Math.log(k));
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + sizes[i];
+}
+function AttachmentToolbar(props) {
+ var editor = props.editor;
+ return (_jsx(Flex, __assign({ sx: {
+ flexDirection: "column",
+ // position: "absolute",
+ // top: 0,
+ mb: 2,
+ zIndex: 9999,
+ alignItems: "end",
+ } }, { children: _jsxs(Flex, __assign({ sx: {
+ bg: "background",
+ boxShadow: "menu",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ mb: 2,
+ } }, { children: [_jsx(ToolButton, { toggled: false, title: "Download", id: "download", icon: "download", onClick: function () { }, iconSize: 16, sx: { mr: 1, p: "3px", borderRadius: "small" } }), _jsx(ToolButton, { toggled: false, title: "delete", id: "delete", icon: "delete", onClick: function () { }, iconSize: 16, sx: { mr: 0, p: "3px", borderRadius: "small" } })] })) })));
+}
diff --git a/packages/editor/dist/extensions/attachment/index.d.ts b/packages/editor/dist/extensions/attachment/index.d.ts
new file mode 100644
index 000000000..9f9f38860
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/index.d.ts
@@ -0,0 +1 @@
+export * from "./attachment";
diff --git a/packages/editor/dist/extensions/attachment/index.js b/packages/editor/dist/extensions/attachment/index.js
new file mode 100644
index 000000000..9f9f38860
--- /dev/null
+++ b/packages/editor/dist/extensions/attachment/index.js
@@ -0,0 +1 @@
+export * from "./attachment";
diff --git a/packages/editor/dist/index.d.ts b/packages/editor/dist/index.d.ts
index 626f5581d..e85a96c93 100644
--- a/packages/editor/dist/index.d.ts
+++ b/packages/editor/dist/index.d.ts
@@ -2,5 +2,6 @@
import { EditorOptions } from "@tiptap/react";
import Toolbar from "./toolbar";
import { ThemeConfig } from "@notesnook/theme/dist/theme/types";
-declare const useTiptap: (options?: Partial<EditorOptions & ThemeConfig>, deps?: import("react").DependencyList | undefined) => import("@tiptap/react").Editor | null;
+import { AttachmentOptions } from "./extensions/attachment";
+declare const useTiptap: (options?: Partial<EditorOptions & ThemeConfig & AttachmentOptions>, deps?: import("react").DependencyList | undefined) => import("@tiptap/react").Editor | null;
export { useTiptap, Toolbar };
diff --git a/packages/editor/dist/index.js b/packages/editor/dist/index.js
index f0a8daa6b..cc020f8d3 100644
--- a/packages/editor/dist/index.js
+++ b/packages/editor/dist/index.js
@@ -46,6 +46,7 @@ import TableCell from "./extensions/table-cell";
import TableHeader from "@tiptap/extension-table-header";
import { ImageNode } from "./extensions/image";
import { useTheme } from "@notesnook/theme";
+import { AttachmentNode } from "./extensions/attachment";
EditorView.prototype.updateState = function updateState(state) {
if (!this.docView)
return; // This prevents the matchesNode error on hot reloads
@@ -53,7 +54,7 @@ EditorView.prototype.updateState = function updateState(state) {
};
var useTiptap = function (options, deps) {
if (options === void 0) { options = {}; }
- var theme = options.theme, accent = options.accent, scale = options.scale, onCreate = options.onCreate, restOptions = __rest(options, ["theme", "accent", "scale", "onCreate"]);
+ var theme = options.theme, accent = options.accent, scale = options.scale, onCreate = options.onCreate, onDownloadAttachment = options.onDownloadAttachment, restOptions = __rest(options, ["theme", "accent", "scale", "onCreate", "onDownloadAttachment"]);
var defaultOptions = useMemo(function () { return ({
extensions: [
TextStyle,
@@ -88,6 +89,9 @@ var useTiptap = function (options, deps) {
Placeholder.configure({
placeholder: "Start writing your note...",
}),
+ AttachmentNode.configure({
+ onDownloadAttachment: onDownloadAttachment,
+ }),
],
onCreate: function (_a) {
var editor = _a.editor;
@@ -97,7 +101,7 @@ var useTiptap = function (options, deps) {
if (onCreate)
onCreate({ editor: editor });
},
- }); }, [theme, accent, scale, onCreate]);
+ }); }, [theme, accent, scale, onCreate, onDownloadAttachment]);
var editor = useEditor(__assign(__assign({}, defaultOptions), restOptions), deps);
/**
* Add editor to global for use in React Native.
diff --git a/packages/editor/dist/toolbar/icons.d.ts b/packages/editor/dist/toolbar/icons.d.ts
index 400ef8832..b44dc584b 100644
--- a/packages/editor/dist/toolbar/icons.d.ts
+++ b/packages/editor/dist/toolbar/icons.d.ts
@@ -44,11 +44,13 @@ export declare const Icons: {
cellProperties: string;
backgroundColor: string;
borderColor: string;
- plus: string;
- close: string;
sortDesc: string;
deleteTable: string;
mergeCells: string;
splitCells: string;
+ plus: string;
+ close: string;
+ delete: string;
+ download: string;
};
export declare type IconNames = keyof typeof Icons;
diff --git a/packages/editor/dist/toolbar/icons.js b/packages/editor/dist/toolbar/icons.js
index d5ded62be..e91f836f2 100644
--- a/packages/editor/dist/toolbar/icons.js
+++ b/packages/editor/dist/toolbar/icons.js
@@ -1,4 +1,4 @@
-import { mdiAttachment, mdiBorderHorizontal, mdiCheck, mdiChevronDown, mdiCodeBraces, mdiCodeTags, mdiDotsVertical, mdiFormatAlignCenter, mdiFormatAlignJustify, mdiFormatAlignLeft, mdiFormatAlignRight, mdiFormatBold, mdiFormatClear, mdiFormatColorHighlight, mdiFormatColorText, mdiFormatItalic, mdiFormatListBulleted, mdiFormatListNumbered, mdiFormatQuoteClose, mdiFormatStrikethrough, mdiFormatSubscript, mdiFormatSuperscript, mdiFormatTextdirectionLToR, mdiFormatTextdirectionRToL, mdiFormatUnderline, mdiImage, mdiInvertColorsOff, mdiLinkPlus, mdiLoading, mdiTable, mdiTableBorder, mdiTableRowPlusBefore, mdiTableRowRemove, mdiTableColumnPlusAfter, mdiTableColumnPlusBefore, mdiTableColumnRemove, mdiUploadOutline, mdiPlus, mdiSquareRoundedBadgeOutline, mdiFormatColorFill, mdiBorderAllVariant, mdiClose, mdiSortDescending, mdiArrowExpandRight, mdiArrowExpandLeft, mdiArrowExpandDown, mdiArrowExpandUp, mdiTrashCanOutline, mdiTableMergeCells, mdiTableSplitCell, } from "@mdi/js";
+import { mdiAttachment, mdiBorderHorizontal, mdiCheck, mdiChevronDown, mdiCodeBraces, mdiCodeTags, mdiDotsVertical, mdiFormatAlignCenter, mdiFormatAlignJustify, mdiFormatAlignLeft, mdiFormatAlignRight, mdiFormatBold, mdiFormatClear, mdiFormatColorHighlight, mdiFormatColorText, mdiFormatItalic, mdiFormatListBulleted, mdiFormatListNumbered, mdiFormatQuoteClose, mdiFormatStrikethrough, mdiFormatSubscript, mdiFormatSuperscript, mdiFormatTextdirectionLToR, mdiFormatTextdirectionRToL, mdiFormatUnderline, mdiImage, mdiInvertColorsOff, mdiLinkPlus, mdiLoading, mdiTable, mdiTableBorder, mdiTableRowPlusBefore, mdiTableRowRemove, mdiTableColumnPlusAfter, mdiTableColumnPlusBefore, mdiTableColumnRemove, mdiUploadOutline, mdiPlus, mdiSquareRoundedBadgeOutline, mdiFormatColorFill, mdiBorderAllVariant, mdiClose, mdiSortDescending, mdiArrowExpandRight, mdiArrowExpandLeft, mdiArrowExpandDown, mdiArrowExpandUp, mdiTrashCanOutline, mdiTableMergeCells, mdiTableSplitCell, mdiDeleteOutline, mdiDownloadOutline, } from "@mdi/js";
export var Icons = {
bold: mdiFormatBold,
italic: mdiFormatItalic,
@@ -45,10 +45,12 @@ export var Icons = {
cellProperties: mdiSquareRoundedBadgeOutline,
backgroundColor: mdiFormatColorFill,
borderColor: mdiBorderAllVariant,
- plus: mdiPlus,
- close: mdiClose,
sortDesc: mdiSortDescending,
deleteTable: mdiTrashCanOutline,
mergeCells: mdiTableMergeCells,
splitCells: mdiTableSplitCell,
+ plus: mdiPlus,
+ close: mdiClose,
+ delete: mdiDeleteOutline,
+ download: mdiDownloadOutline,
};
diff --git a/packages/editor/dist/toolbar/tools/block.d.ts b/packages/editor/dist/toolbar/tools/block.d.ts
index 8e859de0a..0ed3d42bb 100644
--- a/packages/editor/dist/toolbar/tools/block.d.ts
+++ b/packages/editor/dist/toolbar/tools/block.d.ts
@@ -25,9 +25,6 @@ export declare class Image implements ITool {
title: string;
render: (props: ToolProps) => JSX.Element;
}
-export declare class Attachment extends BlockTool<ToolId> {
- constructor();
-}
export declare class Table implements ITool {
id: ToolId;
title: string;
diff --git a/packages/editor/dist/toolbar/tools/block.js b/packages/editor/dist/toolbar/tools/block.js
index 18d9b3767..e64a84644 100644
--- a/packages/editor/dist/toolbar/tools/block.js
+++ b/packages/editor/dist/toolbar/tools/block.js
@@ -104,16 +104,6 @@ var Image = /** @class */ (function () {
return Image;
}());
export { Image };
-var Attachment = /** @class */ (function (_super) {
- __extends(Attachment, _super);
- function Attachment() {
- return _super.call(this, "attachment", "Attachment", "attachment", function (editor) {
- return false;
- }) || this;
- }
- return Attachment;
-}(BlockTool));
-export { Attachment };
var Table = /** @class */ (function () {
function Table() {
var _this = this;
diff --git a/packages/editor/dist/toolbar/tools/index.d.ts b/packages/editor/dist/toolbar/tools/index.d.ts
index e6defa4f1..31f49b7e2 100644
--- a/packages/editor/dist/toolbar/tools/index.d.ts
+++ b/packages/editor/dist/toolbar/tools/index.d.ts
@@ -1,8 +1,8 @@
import { ITool } from "../types";
-import { Bold, Italic, Underline, Strikethrough, Code, Subscript, Superscript, ClearFormatting, Link } from "./inline";
+import { Bold, Italic, Underline, Strikethrough, Code, Subscript, Superscript, ClearFormatting, Link, Attachment } from "./inline";
import { FontSize, FontFamily } from "./font";
import { AlignCenter, AlignLeft, AlignRight, AlignJustify } from "./alignment";
-import { Blockquote, CodeBlock, HorizontalRule, Image, Attachment, Table } from "./block";
+import { Blockquote, CodeBlock, HorizontalRule, Image, Table } from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
diff --git a/packages/editor/dist/toolbar/tools/index.js b/packages/editor/dist/toolbar/tools/index.js
index 966bbe561..52ce892bd 100644
--- a/packages/editor/dist/toolbar/tools/index.js
+++ b/packages/editor/dist/toolbar/tools/index.js
@@ -1,7 +1,7 @@
-import { Bold, Italic, Underline, Strikethrough, Code, Subscript, Superscript, ClearFormatting, Link, } from "./inline";
+import { Bold, Italic, Underline, Strikethrough, Code, Subscript, Superscript, ClearFormatting, Link, Attachment, } from "./inline";
import { FontSize, FontFamily } from "./font";
import { AlignCenter, AlignLeft, AlignRight, AlignJustify } from "./alignment";
-import { Blockquote, CodeBlock, HorizontalRule, Image, Attachment, Table, } from "./block";
+import { Blockquote, CodeBlock, HorizontalRule, Image, Table } from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
diff --git a/packages/editor/dist/toolbar/tools/inline.d.ts b/packages/editor/dist/toolbar/tools/inline.d.ts
index 9a24bfb0f..118f86bc1 100644
--- a/packages/editor/dist/toolbar/tools/inline.d.ts
+++ b/packages/editor/dist/toolbar/tools/inline.d.ts
@@ -1,4 +1,3 @@
-/// <reference types="react" />
import { ITool, ToolProps } from "../types";
import { ToolId } from ".";
import { IconNames } from "../icons";
@@ -40,4 +39,9 @@ export declare class Link implements ITool {
title: string;
render: (props: ToolProps) => JSX.Element;
}
+export declare class Attachment implements ITool {
+ id: ToolId;
+ title: string;
+ render: (props: ToolProps) => JSX.Element;
+}
export {};
diff --git a/packages/editor/dist/toolbar/tools/inline.js b/packages/editor/dist/toolbar/tools/inline.js
index c57586ce1..02a92a62f 100644
--- a/packages/editor/dist/toolbar/tools/inline.js
+++ b/packages/editor/dist/toolbar/tools/inline.js
@@ -174,3 +174,16 @@ var Link = /** @class */ (function () {
return Link;
}());
export { Link };
+var Attachment = /** @class */ (function () {
+ function Attachment() {
+ var _this = this;
+ this.id = "attachment";
+ this.title = "Attachment";
+ this.render = function (props) {
+ var editor = props.editor;
+ return (_jsx(ToolButton, { title: _this.title, id: _this.id, icon: "attachment", onClick: function () { }, toggled: false }));
+ };
+ }
+ return Attachment;
+}());
+export { Attachment };
diff --git a/packages/editor/src/components/menu/useMenu.ts b/packages/editor/src/components/menu/useMenu.ts
index f7fd02554..0cc10ec8b 100644
--- a/packages/editor/src/components/menu/useMenu.ts
+++ b/packages/editor/src/components/menu/useMenu.ts
@@ -159,6 +159,8 @@ export function getPosition(
if (target !== "mouse" && align === "center" && elementWidth > 0) {
position.left -= elementWidth / 2 - target.clientWidth / 2;
+ } else if (target !== "mouse" && align === "end" && elementWidth > 0) {
+ position.left -= elementWidth - target.clientWidth;
}
// Adjust menu height
diff --git a/packages/editor/src/extensions/attachment/attachment.ts b/packages/editor/src/extensions/attachment/attachment.ts
new file mode 100644
index 000000000..e7310cf23
--- /dev/null
+++ b/packages/editor/src/extensions/attachment/attachment.ts
@@ -0,0 +1,119 @@
+import { Node, nodeInputRule, mergeAttributes } from "@tiptap/core";
+import { ReactNodeViewRenderer } from "@tiptap/react";
+import { Attribute } from "@tiptap/core";
+import { AttachmentComponent } from "./component";
+
+export interface AttachmentOptions {
+ // HTMLAttributes: Record<string, any>;
+ onDownloadAttachment: (attachment: Attachment) => boolean;
+}
+
+export type Attachment = {
+ hash: string;
+ filename: string;
+ type: string;
+ size: number;
+};
+
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ attachment: {
+ insertAttachment: (attachment: Attachment) => ReturnType;
+ downloadAttachment: (attachment: Attachment) => ReturnType;
+ };
+ }
+}
+
+export const AttachmentNode = Node.create<AttachmentOptions>({
+ name: "attachment",
+ content: "inline*",
+ marks: "",
+ inline: true,
+ atom: true,
+
+ addOptions() {
+ return {
+ // HTMLAttributes: {},
+ onDownloadAttachment: () => false,
+ };
+ },
+
+ group() {
+ return "inline";
+ },
+
+ draggable: true,
+
+ addAttributes() {
+ return {
+ hash: getDataAttribute("hash"),
+ filename: getDataAttribute("filename"),
+ type: getDataAttribute("type"),
+ size: getDataAttribute("size"),
+ };
+ },
+
+ parseHTML() {
+ return [
+ {
+ tag: "span[data-hash]",
+ },
+ ];
+ },
+
+ renderHTML({ HTMLAttributes }) {
+ return ["span", mergeAttributes(HTMLAttributes)];
+ },
+
+ addNodeView() {
+ return ReactNodeViewRenderer(AttachmentComponent);
+ },
+
+ addCommands() {
+ return {
+ insertAttachment:
+ (attachment) =>
+ ({ commands }) => {
+ return commands.insertContent({
+ type: this.name,
+ attrs: attachment,
+ });
+ },
+ downloadAttachment:
+ (attachment) =>
+ ({}) => {
+ return this.options.onDownloadAttachment(attachment);
+ },
+ };
+ },
+
+ // addInputRules() {
+ // return [
+ // nodeInputRule({
+ // find: inputRegex,
+ // type: this.type,
+ // getAttributes: (match) => {
+ // const [, , alt, src, title] = match;
+
+ // return { src, alt, title };
+ // },
+ // }),
+ // ];
+ // },
+});
+
+function getDataAttribute(name: keyof Attachment): Partial<Attribute> {
+ return {
+ default: null,
+ parseHTML: (element) => element.dataset[name],
+ renderHTML: (attributes) => {
+ if (!attributes[name]) {
+ return {};
+ }
+
+ return {
+ [`data-${name}`]: attributes[name],
+ };
+ },
+ };
+}
diff --git a/packages/editor/src/extensions/attachment/component.tsx b/packages/editor/src/extensions/attachment/component.tsx
new file mode 100644
index 000000000..bd51e1b3f
--- /dev/null
+++ b/packages/editor/src/extensions/attachment/component.tsx
@@ -0,0 +1,169 @@
+import { Box, Flex, Image, ImageProps, Text } from "rebass";
+import { NodeViewWrapper, NodeViewProps, FloatingMenu } from "@tiptap/react";
+import { Attachment } from "./attachment";
+import { ThemeConfig } from "@notesnook/theme/dist/theme/types";
+import { ThemeProvider } from "emotion-theming";
+import { Theme, useTheme } from "@notesnook/theme";
+import { Resizable } from "re-resizable";
+import { ToolButton } from "../../toolbar/components/tool-button";
+import { findToolById, ToolId } from "../../toolbar/tools";
+import { Editor } from "@tiptap/core";
+import { useCallback, useEffect, useRef, useState } from "react";
+import { MenuPresenter } from "../../components/menu/menu";
+import { Popup } from "../../toolbar/components/popup";
+import { Toggle } from "../../components/toggle";
+import { Input } from "@rebass/forms";
+import { Icon } from "../../toolbar/components/icon";
+import { Icons } from "../../toolbar/icons";
+
+export function AttachmentComponent(props: ImageProps & NodeViewProps) {
+ const { hash, filename, size } = props.node.attrs as Attachment;
+
+ const { editor, updateAttributes } = props;
+ const elementRef = useRef<HTMLSpanElement>();
+ const isActive = editor.isActive("attachment", { hash });
+ // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
+ const theme = editor.storage.theme as Theme;
+
+ // useEffect(() => {
+ // setIsToolbarVisible(isActive);
+ // }, [isActive]);
+
+ return (
+ <NodeViewWrapper as={"span"}>
+ <ThemeProvider theme={theme}>
+ <Box
+ ref={elementRef}
+ as="span"
+ contentEditable={false}
+ variant={"body"}
+ sx={{
+ display: "inline-flex",
+ overflow: "hidden",
+ position: "relative",
+ zIndex: 1,
+ userSelect: "none",
+ alignItems: "center",
+ backgroundColor: "bgSecondary",
+ padding: "0px 5px 0px 5px",
+ borderRadius: "default",
+ border: "1px solid var(--border)",
+ cursor: "pointer",
+ maxWidth: 250,
+ borderColor: isActive ? "primary" : "border",
+ ":hover": {
+ bg: "hover",
+ },
+ }}
+ title={filename}
+ >
+ <Icon path={Icons.attachment} size={14} />
+ <Text
+ as="span"
+ sx={{
+ ml: "3px",
+ fontSize: "0.85rem",
+ whiteSpace: "nowrap",
+ textOverflow: "ellipsis",
+ overflow: "hidden",
+ }}
+ className="filename"
+ >
+ {filename}
+ </Text>
+ <Text
+ as="span"
+ className="size"
+ sx={{
+ ml: "7px",
+ fontSize: "0.7rem",
+ color: "fontTertiary",
+ flexShrink: 0,
+ }}
+ >
+ {formatBytes(size)}
+ </Text>
+ </Box>
+ <MenuPresenter
+ isOpen={isActive}
+ onClose={() => {}}
+ items={[]}
+ options={{
+ type: "autocomplete",
+ position: {
+ target: elementRef.current || undefined,
+ location: "top",
+ yOffset: -5,
+ isTargetAbsolute: true,
+ align: "end",
+ },
+ }}
+ >
+ <AttachmentToolbar editor={editor} />
+ </MenuPresenter>
+ </ThemeProvider>
+ </NodeViewWrapper>
+ );
+}
+
+function formatBytes(bytes: number, decimals = 1) {
+ 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];
+}
+
+type AttachmentToolbarProps = {
+ editor: Editor;
+};
+
+function AttachmentToolbar(props: AttachmentToolbarProps) {
+ const { editor } = props;
+
+ return (
+ <Flex
+ sx={{
+ flexDirection: "column",
+ // position: "absolute",
+ // top: 0,
+ mb: 2,
+ zIndex: 9999,
+ alignItems: "end",
+ }}
+ >
+ <Flex
+ sx={{
+ bg: "background",
+ boxShadow: "menu",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ mb: 2,
+ }}
+ >
+ <ToolButton
+ toggled={false}
+ title="Download"
+ id="download"
+ icon="download"
+ onClick={() => {}}
+ iconSize={16}
+ sx={{ mr: 1, p: "3px", borderRadius: "small" }}
+ />
+ <ToolButton
+ toggled={false}
+ title="delete"
+ id="delete"
+ icon="delete"
+ onClick={() => {}}
+ iconSize={16}
+ sx={{ mr: 0, p: "3px", borderRadius: "small" }}
+ />
+ </Flex>
+ </Flex>
+ );
+}
diff --git a/packages/editor/src/extensions/attachment/index.ts b/packages/editor/src/extensions/attachment/index.ts
new file mode 100644
index 000000000..9f9f38860
--- /dev/null
+++ b/packages/editor/src/extensions/attachment/index.ts
@@ -0,0 +1 @@
+export * from "./attachment";
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 3c7b0a07e..842a62737 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -25,6 +25,7 @@ import TableHeader from "@tiptap/extension-table-header";
import { ImageNode } from "./extensions/image";
import { ThemeConfig } from "@notesnook/theme/dist/theme/types";
import { useTheme } from "@notesnook/theme";
+import { AttachmentNode, AttachmentOptions } from "./extensions/attachment";
EditorView.prototype.updateState = function updateState(state) {
if (!(this as any).docView) return; // This prevents the matchesNode error on hot reloads
@@ -32,10 +33,18 @@ EditorView.prototype.updateState = function updateState(state) {
};
const useTiptap = (
- options: Partial<EditorOptions & ThemeConfig> = {},
+ options: Partial<EditorOptions & ThemeConfig & AttachmentOptions> = {},
deps?: React.DependencyList
) => {
- const { theme, accent, scale, onCreate, ...restOptions } = options;
+ const {
+ theme,
+ accent,
+ scale,
+ onCreate,
+ onDownloadAttachment,
+ ...restOptions
+ } = options;
+
const defaultOptions = useMemo<Partial<EditorOptions>>(
() => ({
extensions: [
@@ -71,6 +80,9 @@ const useTiptap = (
Placeholder.configure({
placeholder: "Start writing your note...",
}),
+ AttachmentNode.configure({
+ onDownloadAttachment,
+ }),
],
onCreate: ({ editor }) => {
if (theme && accent && scale) {
@@ -79,7 +91,7 @@ const useTiptap = (
if (onCreate) onCreate({ editor });
},
}),
- [theme, accent, scale, onCreate]
+ [theme, accent, scale, onCreate, onDownloadAttachment]
);
const editor = useEditor({ ...defaultOptions, ...restOptions }, deps);
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index cc23727dd..11e18c68f 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -50,6 +50,8 @@ import {
mdiTrashCanOutline,
mdiTableMergeCells,
mdiTableSplitCell,
+ mdiDeleteOutline,
+ mdiDownloadOutline,
} from "@mdi/js";
export const Icons = {
@@ -98,12 +100,15 @@ export const Icons = {
cellProperties: mdiSquareRoundedBadgeOutline,
backgroundColor: mdiFormatColorFill,
borderColor: mdiBorderAllVariant,
- plus: mdiPlus,
- close: mdiClose,
sortDesc: mdiSortDescending,
deleteTable: mdiTrashCanOutline,
mergeCells: mdiTableMergeCells,
splitCells: mdiTableSplitCell,
+
+ plus: mdiPlus,
+ close: mdiClose,
+ delete: mdiDeleteOutline,
+ download: mdiDownloadOutline,
};
export type IconNames = keyof typeof Icons;
diff --git a/packages/editor/src/toolbar/tools/block.tsx b/packages/editor/src/toolbar/tools/block.tsx
index 4e560fc8a..33f3d1dac 100644
--- a/packages/editor/src/toolbar/tools/block.tsx
+++ b/packages/editor/src/toolbar/tools/block.tsx
@@ -89,14 +89,6 @@ export class Image implements ITool {
};
}
-export class Attachment extends BlockTool<ToolId> {
- constructor() {
- super("attachment", "Attachment", "attachment", (editor) => {
- return false;
- });
- }
-}
-
type CellLocation = { column: number; row: number };
type TableSize = { columns: number; rows: number };
export class Table implements ITool {
diff --git a/packages/editor/src/toolbar/tools/index.ts b/packages/editor/src/toolbar/tools/index.ts
index a55fb78a5..5f625bee3 100644
--- a/packages/editor/src/toolbar/tools/index.ts
+++ b/packages/editor/src/toolbar/tools/index.ts
@@ -9,17 +9,11 @@ import {
Superscript,
ClearFormatting,
Link,
+ Attachment,
} from "./inline";
import { FontSize, FontFamily } from "./font";
import { AlignCenter, AlignLeft, AlignRight, AlignJustify } from "./alignment";
-import {
- Blockquote,
- CodeBlock,
- HorizontalRule,
- Image,
- Attachment,
- Table,
-} from "./block";
+import { Blockquote, CodeBlock, HorizontalRule, Image, Table } from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
diff --git a/packages/editor/src/toolbar/tools/inline.tsx b/packages/editor/src/toolbar/tools/inline.tsx
index 5131fd6e9..a78a60903 100644
--- a/packages/editor/src/toolbar/tools/inline.tsx
+++ b/packages/editor/src/toolbar/tools/inline.tsx
@@ -195,3 +195,22 @@ export class Link implements ITool {
);
};
}
+
+export class Attachment implements ITool {
+ id: ToolId = "attachment";
+ title = "Attachment";
+
+ render = (props: ToolProps) => {
+ const { editor } = props;
+
+ return (
+ <ToolButton
+ title={this.title}
+ id={this.id}
+ icon="attachment"
+ onClick={() => {}}
+ toggled={false}
+ />
+ );
+ };
+}