Skip to content

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

feat: add embed extension

thecodrrfa3095e47fb7
diff --git a/packages/editor/dist/extensions/embed/component.d.ts b/packages/editor/dist/extensions/embed/component.d.ts
new file mode 100644
index 000000000..f7ad8b28e
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/component.d.ts
@@ -0,0 +1,3 @@
+/// <reference types="react" />
+import { NodeViewProps } from "@tiptap/react";
+export declare function EmbedComponent(props: NodeViewProps): JSX.Element;
diff --git a/packages/editor/dist/extensions/embed/component.js b/packages/editor/dist/extensions/embed/component.js
new file mode 100644
index 000000000..80a14436b
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/component.js
@@ -0,0 +1,105 @@
+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);
+};
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
+import { Box, Flex } from "rebass";
+import { NodeViewWrapper } from "@tiptap/react";
+import { ThemeProvider } from "emotion-theming";
+import { Resizable } from "re-resizable";
+import { ToolButton } from "../../toolbar/components/tool-button";
+import { useEffect, useRef, useState } from "react";
+import { EmbedPopup } from "../../toolbar/popups/embed-popup";
+export function EmbedComponent(props) {
+ var _a = props.node.attrs, src = _a.src, width = _a.width, height = _a.height, align = _a.align;
+ var editor = props.editor, updateAttributes = props.updateAttributes;
+ var embedRef = useRef();
+ var isActive = editor.isActive("embed", { src: src });
+ var _b = __read(useState(), 2), isToolbarVisible = _b[0], setIsToolbarVisible = _b[1];
+ var theme = editor.storage.theme;
+ useEffect(function () {
+ setIsToolbarVisible(isActive);
+ }, [isActive]);
+ return (_jsx(NodeViewWrapper, { children: _jsx(ThemeProvider, __assign({ theme: theme }, { children: _jsx(Box, __assign({ sx: {
+ display: "flex",
+ justifyContent: align === "center"
+ ? "center"
+ : align === "left"
+ ? "start"
+ : "end",
+ } }, { children: _jsxs(Resizable, __assign({ size: {
+ height: height || "auto",
+ width: width || "auto",
+ }, maxWidth: "100%", onResizeStop: function (e, direction, ref, d) {
+ updateAttributes({
+ width: ref.clientWidth,
+ height: ref.clientHeight,
+ });
+ }, lockAspectRatio: true }, { children: [_jsx(Flex, __assign({ sx: { position: "relative", justifyContent: "end" } }, { children: isToolbarVisible && (_jsx(ImageToolbar, { editor: editor, align: align, height: height || 0, width: width || 0, src: src })) })), _jsx(Box, __assign({ as: "iframe", ref: embedRef, src: src, width: "100%", height: "100%", sx: {
+ border: isActive
+ ? "2px solid var(--primary)"
+ : "2px solid transparent",
+ borderRadius: "default",
+ } }, props))] })) })) })) }));
+}
+function ImageToolbar(props) {
+ var editor = props.editor, height = props.height, width = props.width, src = props.src;
+ var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
+ return (_jsxs(Flex, __assign({ sx: {
+ flexDirection: "column",
+ position: "absolute",
+ top: -40,
+ mb: 2,
+ zIndex: 9999,
+ alignItems: "end",
+ } }, { children: [_jsxs(Flex, __assign({ sx: {
+ bg: "background",
+ boxShadow: "menu",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ mb: 2,
+ } }, { children: [_jsxs(Flex, __assign({ className: "toolbar-group", sx: {
+ pr: 1,
+ mr: 1,
+ borderRight: "1px solid var(--border)",
+ ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
+ } }, { children: [_jsx(ToolButton, { toggled: false, title: "Align left", id: "alignLeft", icon: "alignLeft", onClick: function () {
+ return editor.chain().focus().setEmbedAlignment({ align: "left" }).run();
+ } }), _jsx(ToolButton, { toggled: false, title: "Align center", id: "alignCenter", icon: "alignCenter", onClick: function () {
+ return editor
+ .chain()
+ .focus()
+ .setEmbedAlignment({ align: "center" })
+ .run();
+ } }), _jsx(ToolButton, { toggled: false, title: "Align right", id: "alignRight", icon: "alignRight", onClick: function () {
+ return editor.chain().focus().setEmbedAlignment({ align: "right" }).run();
+ } })] })), _jsx(Flex, __assign({ className: "toolbar-group", sx: {
+ pr: 1,
+ mr: 1,
+ borderRight: "1px solid var(--border)",
+ ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
+ } }, { children: _jsx(ToolButton, { toggled: isOpen, title: "Embed properties", id: "embedProperties", icon: "more", onClick: function () { return setIsOpen(function (s) { return !s; }); } }) }))] })), isOpen && (_jsx(EmbedPopup, { title: "Embed properties", icon: "close", onClose: function () { return setIsOpen(false); }, embed: props, onSourceChanged: function (src) { }, onSizeChanged: function (size) { return editor.commands.setEmbedSize(size); } }))] })));
+}
diff --git a/packages/editor/dist/extensions/embed/embed.d.ts b/packages/editor/dist/extensions/embed/embed.d.ts
new file mode 100644
index 000000000..e03119824
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/embed.d.ts
@@ -0,0 +1,27 @@
+import { Node } from "@tiptap/core";
+export interface EmbedOptions {
+ HTMLAttributes: Record<string, any>;
+}
+export declare type EmbedAttributes = Partial<EmbedSizeOptions> & {
+ src: string;
+};
+export declare type EmbedAlignmentOptions = {
+ align?: "center" | "left" | "right";
+};
+export declare type EmbedSizeOptions = {
+ width: number;
+ height: number;
+};
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ embed: {
+ /**
+ * Add an image
+ */
+ insertEmbed: (options: EmbedAttributes) => ReturnType;
+ setEmbedAlignment: (options: EmbedAlignmentOptions) => ReturnType;
+ setEmbedSize: (options: EmbedSizeOptions) => ReturnType;
+ };
+ }
+}
+export declare const EmbedNode: Node<EmbedOptions, any>;
diff --git a/packages/editor/dist/extensions/embed/embed.js b/packages/editor/dist/extensions/embed/embed.js
new file mode 100644
index 000000000..b02782ff7
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/embed.js
@@ -0,0 +1,81 @@
+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 { Node, mergeAttributes } from "@tiptap/core";
+import { ReactNodeViewRenderer } from "@tiptap/react";
+import { EmbedComponent } from "./component";
+export var EmbedNode = Node.create({
+ name: "embed",
+ content: "",
+ marks: "",
+ draggable: true,
+ addOptions: function () {
+ return {
+ HTMLAttributes: {},
+ };
+ },
+ group: function () {
+ return "block";
+ },
+ addAttributes: function () {
+ return {
+ src: {
+ default: null,
+ },
+ width: { default: null },
+ height: { default: null },
+ align: { default: "left" },
+ };
+ },
+ parseHTML: function () {
+ return [
+ {
+ tag: "iframe[src]",
+ },
+ ];
+ },
+ renderHTML: function (_a) {
+ var HTMLAttributes = _a.HTMLAttributes;
+ return [
+ "iframe",
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
+ ];
+ },
+ addNodeView: function () {
+ return ReactNodeViewRenderer(EmbedComponent);
+ },
+ addCommands: function () {
+ var _this = this;
+ return {
+ insertEmbed: function (options) {
+ return function (_a) {
+ var commands = _a.commands;
+ return commands.insertContent({
+ type: _this.name,
+ attrs: options,
+ });
+ };
+ },
+ setEmbedAlignment: function (options) {
+ return function (_a) {
+ var commands = _a.commands;
+ return commands.updateAttributes(_this.name, __assign({}, options));
+ };
+ },
+ setEmbedSize: function (options) {
+ return function (_a) {
+ var commands = _a.commands;
+ return commands.updateAttributes(_this.name, __assign({}, options));
+ };
+ },
+ };
+ },
+});
diff --git a/packages/editor/dist/extensions/embed/index.d.ts b/packages/editor/dist/extensions/embed/index.d.ts
new file mode 100644
index 000000000..12b3c5295
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/index.d.ts
@@ -0,0 +1 @@
+export * from "./embed";
diff --git a/packages/editor/dist/extensions/embed/index.js b/packages/editor/dist/extensions/embed/index.js
new file mode 100644
index 000000000..12b3c5295
--- /dev/null
+++ b/packages/editor/dist/extensions/embed/index.js
@@ -0,0 +1 @@
+export * from "./embed";
diff --git a/packages/editor/dist/index.js b/packages/editor/dist/index.js
index c16bf0e48..94adba884 100644
--- a/packages/editor/dist/index.js
+++ b/packages/editor/dist/index.js
@@ -51,6 +51,7 @@ import { TaskListNode } from "./extensions/task-list";
import { TaskItemNode } from "./extensions/task-item";
import { Dropcursor } from "./extensions/drop-cursor";
import { SearchReplace } from "./extensions/search-replace";
+import { EmbedNode } from "./extensions/embed";
EditorView.prototype.updateState = function updateState(state) {
if (!this.docView)
return; // This prevents the matchesNode error on hot reloads
@@ -101,6 +102,7 @@ var useTiptap = function (options, deps) {
placeholder: "Start writing your note...",
}),
ImageNode,
+ EmbedNode,
AttachmentNode.configure({
onDownloadAttachment: onDownloadAttachment,
}),
diff --git a/packages/editor/dist/toolbar/components/dropdown.d.ts b/packages/editor/dist/toolbar/components/dropdown.d.ts
index 74f67791b..b51bc5ede 100644
--- a/packages/editor/dist/toolbar/components/dropdown.d.ts
+++ b/packages/editor/dist/toolbar/components/dropdown.d.ts
@@ -3,6 +3,7 @@ import { MenuItem } from "../../components/menu/types";
declare type DropdownProps = {
selectedItem: string | JSX.Element;
items: MenuItem[];
+ buttonRef?: React.MutableRefObject<HTMLButtonElement | undefined>;
};
export declare function Dropdown(props: DropdownProps): JSX.Element;
export {};
diff --git a/packages/editor/dist/toolbar/components/dropdown.js b/packages/editor/dist/toolbar/components/dropdown.js
index 1274e1f63..b39d24059 100644
--- a/packages/editor/dist/toolbar/components/dropdown.js
+++ b/packages/editor/dist/toolbar/components/dropdown.js
@@ -32,10 +32,14 @@ import { Icon } from "./icon";
import { Icons } from "../icons";
import { MenuPresenter } from "../../components/menu/menu";
export function Dropdown(props) {
- var items = props.items, selectedItem = props.selectedItem;
- var buttonRef = useRef(null);
+ var items = props.items, selectedItem = props.selectedItem, buttonRef = props.buttonRef;
+ var internalRef = useRef();
var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
- return (_jsxs(_Fragment, { children: [_jsxs(Button, __assign({ ref: buttonRef, sx: {
+ return (_jsxs(_Fragment, { children: [_jsxs(Button, __assign({ ref: function (ref) {
+ internalRef.current = ref;
+ if (buttonRef)
+ buttonRef.current = ref;
+ }, sx: {
p: 1,
m: 0,
bg: isOpen ? "hover" : "transparent",
@@ -49,7 +53,7 @@ export function Dropdown(props) {
}, onClick: function () { return setIsOpen(function (s) { return !s; }); } }, { children: [typeof selectedItem === "string" ? (_jsx(Text, __assign({ sx: { fontSize: 12, mr: 1, color: "text" } }, { children: selectedItem }))) : (selectedItem), _jsx(Icon, { path: Icons.chevronDown, size: 16, color: "text" })] })), _jsx(MenuPresenter, { options: {
type: "menu",
position: {
- target: buttonRef.current || undefined,
+ target: internalRef.current || undefined,
isTargetAbsolute: true,
location: "below",
yOffset: 5,
diff --git a/packages/editor/dist/toolbar/components/popup.d.ts b/packages/editor/dist/toolbar/components/popup.d.ts
index a5be83a3e..2f5082aec 100644
--- a/packages/editor/dist/toolbar/components/popup.d.ts
+++ b/packages/editor/dist/toolbar/components/popup.d.ts
@@ -2,7 +2,7 @@ import { ButtonProps } from "rebass";
import { IconNames } from "../icons";
import { PropsWithChildren } from "react";
import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";
-declare type PopupProps = {
+export declare type PopupProps = {
title?: string;
action?: PopupButtonProps;
};
diff --git a/packages/editor/dist/toolbar/icons.d.ts b/packages/editor/dist/toolbar/icons.d.ts
index c7d18eb41..d03aff89d 100644
--- a/packages/editor/dist/toolbar/icons.d.ts
+++ b/packages/editor/dist/toolbar/icons.d.ts
@@ -21,6 +21,7 @@ export declare const Icons: {
highlight: string;
textColor: string;
link: string;
+ url: string;
image: string;
colorClear: string;
check: string;
@@ -57,6 +58,7 @@ export declare const Icons: {
caseSensitive: string;
wholeWord: string;
regex: string;
+ embed: string;
plus: string;
close: string;
delete: string;
diff --git a/packages/editor/dist/toolbar/icons.js b/packages/editor/dist/toolbar/icons.js
index fffbc2aae..88c952cff 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, mdiDeleteOutline, mdiDownloadOutline, mdiFormatListCheckbox, mdiDrag, mdiCheckboxMarkedOutline, mdiChevronUp, mdiArrowUp, mdiArrowDown, mdiRegex, mdiFormatLetterCase, mdiFormatLetterMatches, } 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, mdiFormatListCheckbox, mdiDrag, mdiCheckboxMarkedOutline, mdiChevronUp, mdiArrowUp, mdiArrowDown, mdiRegex, mdiFormatLetterCase, mdiFormatLetterMatches, mdiMoviePlusOutline, mdiLink, } from "@mdi/js";
export var Icons = {
bold: mdiFormatBold,
italic: mdiFormatItalic,
@@ -22,6 +22,7 @@ export var Icons = {
highlight: mdiFormatColorHighlight,
textColor: mdiFormatColorText,
link: mdiLinkPlus,
+ url: mdiLink,
image: mdiImage,
colorClear: mdiInvertColorsOff,
check: mdiCheck,
@@ -58,6 +59,7 @@ export var Icons = {
caseSensitive: mdiFormatLetterCase,
wholeWord: mdiFormatLetterMatches,
regex: mdiRegex,
+ embed: mdiMoviePlusOutline,
plus: mdiPlus,
close: mdiClose,
delete: mdiDeleteOutline,
diff --git a/packages/editor/dist/toolbar/popups/embed.d.ts b/packages/editor/dist/toolbar/popups/embed.d.ts
new file mode 100644
index 000000000..8543bfa48
--- /dev/null
+++ b/packages/editor/dist/toolbar/popups/embed.d.ts
@@ -0,0 +1,15 @@
+/// <reference types="react" />
+import { EmbedAlignmentOptions, EmbedAttributes, EmbedSizeOptions } from "../../extensions/embed";
+import { IconNames } from "../icons";
+declare type Embed = Required<EmbedAttributes> & EmbedAlignmentOptions;
+export declare type EmbedPopupProps = {
+ onClose: (embed: Embed) => void;
+ title: string;
+ icon: IconNames;
+ embed?: Embed;
+ onSizeChanged?: (size: EmbedSizeOptions) => void;
+ onSourceChanged?: (src: string) => void;
+ onFloatingChanged?: (float: boolean) => void;
+};
+export declare function EmbedPopup(props: EmbedPopupProps): JSX.Element;
+export {};
diff --git a/packages/editor/dist/toolbar/popups/embed.js b/packages/editor/dist/toolbar/popups/embed.js
new file mode 100644
index 000000000..4a0d9381c
--- /dev/null
+++ b/packages/editor/dist/toolbar/popups/embed.js
@@ -0,0 +1,74 @@
+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);
+};
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
+import { Flex, Text } from "rebass";
+import { useCallback, useEffect, useState } from "react";
+import { Popup } from "../../toolbar/components/popup";
+import { Toggle } from "../../components/toggle";
+import { Input } from "@rebass/forms";
+export function EmbedPopup(props) {
+ var onClose = props.onClose, onSizeChanged = props.onSizeChanged, onFloatingChanged = props.onFloatingChanged, onSourceChanged = props.onSourceChanged, title = props.title, icon = props.icon, embed = props.embed;
+ var _a = __read(useState((embed === null || embed === void 0 ? void 0 : embed.width) || 300), 2), width = _a[0], setWidth = _a[1];
+ var _b = __read(useState((embed === null || embed === void 0 ? void 0 : embed.height) || 150), 2), height = _b[0], setHeight = _b[1];
+ var _c = __read(useState((embed === null || embed === void 0 ? void 0 : embed.src) || ""), 2), src = _c[0], setSrc = _c[1];
+ var _d = __read(useState((embed === null || embed === void 0 ? void 0 : embed.float) || false), 2), isFloating = _d[0], setIsFloating = _d[1];
+ var onSizeChange = useCallback(function (newWidth, newHeight) {
+ var size = newWidth
+ ? {
+ width: newWidth,
+ height: newWidth * (height / width),
+ }
+ : newHeight
+ ? {
+ width: newHeight * (width / height),
+ height: newHeight,
+ }
+ : {
+ width: 0,
+ height: 0,
+ };
+ setWidth(size.width);
+ setHeight(size.height);
+ if (onSizeChanged)
+ onSizeChanged(size);
+ }, [width, height]);
+ useEffect(function () {
+ onFloatingChanged && onFloatingChanged(isFloating);
+ }, [onFloatingChanged, isFloating]);
+ useEffect(function () {
+ onSourceChanged && onSourceChanged(src);
+ }, [onSourceChanged, src]);
+ return (_jsx(Popup, __assign({ title: title, action: {
+ icon: icon,
+ onClick: function () { return onClose({ height: height, width: width, src: src, float: isFloating }); },
+ } }, { children: _jsxs(Flex, __assign({ sx: { width: 200, flexDirection: "column", p: 1 } }, { children: [_jsx(Input, { value: src, onChange: function (e) { return setSrc(e.target.value); } }), _jsxs(Flex, __assign({ sx: { justifyContent: "space-between", alignItems: "center" } }, { children: [_jsx(Text, __assign({ variant: "body" }, { children: "Floating?" })), _jsx(Toggle, { checked: isFloating, onClick: function () { return setIsFloating(function (s) { return !s; }); } })] })), _jsxs(Flex, __assign({ sx: { alignItems: "center", mt: 2 } }, { children: [_jsx(Input, { type: "number", placeholder: "Width", value: width, sx: {
+ mr: 2,
+ p: 1,
+ fontSize: "body",
+ }, onChange: function (e) { return onSizeChange(e.target.valueAsNumber); } }), _jsx(Input, { type: "number", placeholder: "Height", value: height, sx: { p: 1, fontSize: "body" }, onChange: function (e) { return onSizeChange(undefined, e.target.valueAsNumber); } })] }))] })) })));
+}
diff --git a/packages/editor/dist/toolbar/popups/embedpopup.d.ts b/packages/editor/dist/toolbar/popups/embedpopup.d.ts
new file mode 100644
index 000000000..15a854407
--- /dev/null
+++ b/packages/editor/dist/toolbar/popups/embedpopup.d.ts
@@ -0,0 +1,14 @@
+/// <reference types="react" />
+import { EmbedAlignmentOptions, EmbedAttributes, EmbedSizeOptions } from "../../extensions/embed";
+import { IconNames } from "../icons";
+declare type Embed = Required<EmbedAttributes> & EmbedAlignmentOptions;
+export declare type EmbedPopupProps = {
+ onClose: (embed: Embed) => void;
+ title: string;
+ icon: IconNames;
+ embed?: Embed;
+ onSizeChanged?: (size: EmbedSizeOptions) => void;
+ onSourceChanged?: (src: string) => void;
+};
+export declare function EmbedPopup(props: EmbedPopupProps): JSX.Element;
+export {};
diff --git a/packages/editor/dist/toolbar/popups/embedpopup.js b/packages/editor/dist/toolbar/popups/embedpopup.js
new file mode 100644
index 000000000..4da217713
--- /dev/null
+++ b/packages/editor/dist/toolbar/popups/embedpopup.js
@@ -0,0 +1,119 @@
+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);
+};
+var __read = (this && this.__read) || function (o, n) {
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
+ if (!m) return o;
+ var i = m.call(o), r, ar = [], e;
+ try {
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
+ }
+ catch (error) { e = { error: error }; }
+ finally {
+ try {
+ if (r && !r.done && (m = i["return"])) m.call(i);
+ }
+ finally { if (e) throw e.error; }
+ }
+ return ar;
+};
+import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
+import { Button, Flex, Text } from "rebass";
+import { useCallback, useEffect, useState } from "react";
+import { Popup } from "../components/popup";
+import { Input, Textarea } from "@rebass/forms";
+import { convertUrlToEmbedUrl } from "@social-embed/lib";
+export function EmbedPopup(props) {
+ var onClose = props.onClose, onSizeChanged = props.onSizeChanged, onSourceChanged = props.onSourceChanged, title = props.title, icon = props.icon, embed = props.embed;
+ var _a = __read(useState((embed === null || embed === void 0 ? void 0 : embed.width) || 300), 2), width = _a[0], setWidth = _a[1];
+ var _b = __read(useState((embed === null || embed === void 0 ? void 0 : embed.height) || 150), 2), height = _b[0], setHeight = _b[1];
+ var _c = __read(useState((embed === null || embed === void 0 ? void 0 : embed.src) || ""), 2), src = _c[0], setSrc = _c[1];
+ var _d = __read(useState("url"), 2), embedSource = _d[0], setEmbedSource = _d[1];
+ var _e = __read(useState(null), 2), error = _e[0], setError = _e[1];
+ var onSizeChange = useCallback(function (newWidth, newHeight) {
+ var size = newWidth
+ ? {
+ width: newWidth,
+ height: newWidth * (height / width),
+ }
+ : newHeight
+ ? {
+ width: newHeight * (width / height),
+ height: newHeight,
+ }
+ : {
+ width: 0,
+ height: 0,
+ };
+ setWidth(size.width);
+ setHeight(size.height);
+ if (onSizeChanged)
+ onSizeChanged(size);
+ }, [width, height]);
+ useEffect(function () {
+ onSourceChanged && onSourceChanged(src);
+ }, [onSourceChanged, src]);
+ return (_jsx(Popup, __assign({ title: title, action: {
+ icon: icon,
+ onClick: function () {
+ setError(null);
+ var _src = src;
+ var _width = width;
+ var _height = height;
+ if (embedSource === "code") {
+ var document_1 = new DOMParser().parseFromString(src, "text/html");
+ if (document_1.getElementsByTagName("iframe").length <= 0)
+ return setError("Embed code must include an iframe.");
+ var srcValue = getAttribute(document_1, "src");
+ if (!srcValue)
+ return setError("Embed code must include an iframe with an src attribute.");
+ _src = srcValue;
+ var widthValue = getAttribute(document_1, "width");
+ if (widthValue && !isNaN(parseInt(widthValue)))
+ _width = parseInt(widthValue);
+ var heightValue = getAttribute(document_1, "height");
+ if (heightValue && !isNaN(parseInt(heightValue)))
+ _height = parseInt(heightValue);
+ }
+ var convertedUrl = convertUrlToEmbedUrl(_src);
+ if (!!convertedUrl)
+ _src = convertedUrl;
+ onClose({
+ height: _height,
+ width: _width,
+ src: _src,
+ });
+ },
+ } }, { children: _jsxs(Flex, __assign({ sx: { width: 300, flexDirection: "column", p: 1 } }, { children: [error && (_jsxs(Text, __assign({ variant: "error", sx: {
+ bg: "errorBg",
+ color: "error",
+ p: 1,
+ borderRadius: "default",
+ } }, { children: ["Error: ", error] }))), _jsxs(Flex, { children: [_jsx(Button, __assign({ variant: "dialog", sx: {
+ p: 1,
+ mr: 1,
+ color: embedSource === "url" ? "primary" : "text",
+ }, onClick: function () { return setEmbedSource("url"); } }, { children: "From link" })), _jsx(Button, __assign({ variant: "dialog", sx: {
+ p: 1,
+ color: embedSource === "code" ? "primary" : "text",
+ }, onClick: function () { return setEmbedSource("code"); } }, { children: "From code" }))] }), embedSource === "url" ? (_jsx(Input, { placeholder: "Embed source URL", value: src, autoFocus: true, onChange: function (e) { return setSrc(e.target.value); }, sx: { p: 1, mt: 1, fontSize: "body" } })) : (_jsx(Textarea, { autoFocus: true, variant: "forms.input", sx: { fontSize: "subBody", fontFamily: "monospace", mt: 1 }, minHeight: 100, onChange: function (e) { return setSrc(e.target.value); }, placeholder: "Paste embed code here. Only iframes are supported." })), embedSource === "url" ? (_jsxs(Flex, __assign({ sx: { alignItems: "center", mt: 2 } }, { children: [_jsx(Input, { type: "number", placeholder: "Width", value: width, sx: {
+ mr: 2,
+ p: 1,
+ fontSize: "body",
+ }, onChange: function (e) { return onSizeChange(e.target.valueAsNumber); } }), _jsx(Input, { type: "number", placeholder: "Height", value: height, sx: { p: 1, fontSize: "body" }, onChange: function (e) { return onSizeChange(undefined, e.target.valueAsNumber); } })] }))) : null] })) })));
+}
+function getAttribute(document, id) {
+ var element = document.querySelector("[".concat(id, "]"));
+ if (!element)
+ return null;
+ var attribute = element.getAttribute(id);
+ return attribute;
+}
diff --git a/packages/editor/dist/toolbar/popups/index.d.ts b/packages/editor/dist/toolbar/popups/index.d.ts
new file mode 100644
index 000000000..e69de29bb
diff --git a/packages/editor/dist/toolbar/popups/index.js b/packages/editor/dist/toolbar/popups/index.js
new file mode 100644
index 000000000..3918c74e4
--- /dev/null
+++ b/packages/editor/dist/toolbar/popups/index.js
@@ -0,0 +1 @@
+"use strict";
diff --git a/packages/editor/dist/toolbar/toolbar.js b/packages/editor/dist/toolbar/toolbar.js
index 5157d2412..f8a88c10f 100644
--- a/packages/editor/dist/toolbar/toolbar.js
+++ b/packages/editor/dist/toolbar/toolbar.js
@@ -26,7 +26,7 @@ export function Toolbar(props) {
["codeblock", "blockquote"],
["formatClear", "ltr", "rtl"],
["numberedList", "bulletList", "checklist"],
- ["link", "image", "attachment", "table"],
+ ["link", "image", "attachment", "table", "embed"],
["textColor", "highlight"],
];
if (!editor)
diff --git a/packages/editor/dist/toolbar/tools/block.d.ts b/packages/editor/dist/toolbar/tools/block.d.ts
index 0ed3d42bb..c9c743822 100644
--- a/packages/editor/dist/toolbar/tools/block.d.ts
+++ b/packages/editor/dist/toolbar/tools/block.d.ts
@@ -25,6 +25,11 @@ export declare class Image implements ITool {
title: string;
render: (props: ToolProps) => JSX.Element;
}
+export declare class Embed implements ITool {
+ id: ToolId;
+ title: string;
+ render: (props: ToolProps) => JSX.Element;
+}
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 9739f6505..90518f945 100644
--- a/packages/editor/dist/toolbar/tools/block.js
+++ b/packages/editor/dist/toolbar/tools/block.js
@@ -49,6 +49,7 @@ import { Dropdown } from "../components/dropdown";
import { Icon } from "../components/icon";
import { Box, Button, Flex, Text } from "rebass";
import { Popup } from "../components/popup";
+import { EmbedPopup } from "../popups/embed-popup";
var BlockTool = /** @class */ (function () {
function BlockTool(id, title, icon, command) {
var _this = this;
@@ -120,6 +121,31 @@ var Image = /** @class */ (function () {
return Image;
}());
export { Image };
+var Embed = /** @class */ (function () {
+ function Embed() {
+ var _this = this;
+ this.id = "embed";
+ this.title = "Embed";
+ this.render = function (props) {
+ var editor = props.editor;
+ var _a = __read(useState(false), 2), isOpen = _a[0], setIsOpen = _a[1];
+ var buttonRef = useRef(null);
+ return (_jsxs(_Fragment, { children: [_jsx(ToolButton, { buttonRef: buttonRef, id: _this.id, title: _this.title, icon: "embed", onClick: function () { return setIsOpen(function (s) { return !s; }); }, toggled: isOpen }), _jsx(MenuPresenter, __assign({ isOpen: isOpen, items: [], onClose: function () { return setIsOpen(false); }, options: {
+ type: "menu",
+ position: {
+ target: buttonRef.current || undefined,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 10,
+ },
+ } }, { children: _jsx(EmbedPopup, { title: "Insert embed", icon: "check", onClose: function (embed) {
+ editor.chain().focus().insertEmbed(embed).run();
+ } }) }))] }));
+ };
+ }
+ return Embed;
+}());
+export { Embed };
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 9f6b7983b..e8c050ef8 100644
--- a/packages/editor/dist/toolbar/tools/index.d.ts
+++ b/packages/editor/dist/toolbar/tools/index.d.ts
@@ -2,7 +2,7 @@ import { ITool } from "../types";
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, Table } from "./block";
+import { Blockquote, CodeBlock, HorizontalRule, Image, Table, Embed } from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList, Checklist } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
@@ -37,6 +37,7 @@ declare const tools: {
image: Image;
attachment: Attachment;
table: Table;
+ embed: Embed;
};
export declare type ToolId = keyof typeof tools;
export declare function findToolById(id: ToolId): ITool;
diff --git a/packages/editor/dist/toolbar/tools/index.js b/packages/editor/dist/toolbar/tools/index.js
index 399523476..4fbe4578f 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, Attachment, } from "./inline";
import { FontSize, FontFamily } from "./font";
import { AlignCenter, AlignLeft, AlignRight, AlignJustify } from "./alignment";
-import { Blockquote, CodeBlock, HorizontalRule, Image, Table } from "./block";
+import { Blockquote, CodeBlock, HorizontalRule, Image, Table, Embed, } from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList, Checklist } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
@@ -36,6 +36,7 @@ var tools = {
image: new Image(),
attachment: new Attachment(),
table: new Table(),
+ embed: new Embed(),
};
export function findToolById(id) {
return tools[id];
diff --git a/packages/editor/package-lock.json b/packages/editor/package-lock.json
index 3fc2cbb4c..f3a61a68c 100644
--- a/packages/editor/package-lock.json
+++ b/packages/editor/package-lock.json
@@ -12,6 +12,7 @@
"@mdi/react": "^1.5.0",
"@notesnook/theme": "file:../themeprovider",
"@rebass/forms": "^4.0.6",
+ "@social-embed/lib": "^0.0.1-next.12",
"@tiptap/extension-character-count": "^2.0.0-beta.24",
"@tiptap/extension-color": "^2.0.0-beta.9",
"@tiptap/extension-font-family": "^2.0.0-beta.21",
@@ -3354,6 +3355,11 @@
"@sinonjs/commons": "^1.7.0"
}
},
+ "node_modules/@social-embed/lib": {
+ "version": "0.0.1-next.12",
+ "resolved": "https://registry.npmjs.org/@social-embed/lib/-/lib-0.0.1-next.12.tgz",
+ "integrity": "sha512-CUqgq2PTmdmWhNPzth9JN1cFoaFsIWhgiuDVD23hjD+9HJVD+xs3GlbZF+YWYMKVhRa3hib4A7Ev8rOjz2QBwQ=="
+ },
"node_modules/@styled-system/background": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/@styled-system/background/-/background-5.1.2.tgz",
@@ -27247,6 +27253,11 @@
"@sinonjs/commons": "^1.7.0"
}
},
+ "@social-embed/lib": {
+ "version": "0.0.1-next.12",
+ "resolved": "https://registry.npmjs.org/@social-embed/lib/-/lib-0.0.1-next.12.tgz",
+ "integrity": "sha512-CUqgq2PTmdmWhNPzth9JN1cFoaFsIWhgiuDVD23hjD+9HJVD+xs3GlbZF+YWYMKVhRa3hib4A7Ev8rOjz2QBwQ=="
+ },
"@styled-system/background": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/@styled-system/background/-/background-5.1.2.tgz",
diff --git a/packages/editor/package.json b/packages/editor/package.json
index 19d7eeb44..eb82283d9 100644
--- a/packages/editor/package.json
+++ b/packages/editor/package.json
@@ -8,6 +8,7 @@
"@mdi/react": "^1.5.0",
"@notesnook/theme": "file:../themeprovider",
"@rebass/forms": "^4.0.6",
+ "@social-embed/lib": "^0.0.1-next.12",
"@tiptap/extension-character-count": "^2.0.0-beta.24",
"@tiptap/extension-color": "^2.0.0-beta.9",
"@tiptap/extension-font-family": "^2.0.0-beta.21",
diff --git a/packages/editor/src/extensions/embed/component.tsx b/packages/editor/src/extensions/embed/component.tsx
new file mode 100644
index 000000000..105291f7c
--- /dev/null
+++ b/packages/editor/src/extensions/embed/component.tsx
@@ -0,0 +1,199 @@
+import { Box, Flex, Image, ImageProps, Text } from "rebass";
+import { NodeViewWrapper, NodeViewProps, FloatingMenu } from "@tiptap/react";
+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 {
+ EmbedAlignmentOptions,
+ EmbedAttributes,
+ EmbedSizeOptions,
+} from "./embed";
+import { EmbedPopup } from "../../toolbar/popups/embed-popup";
+
+export function EmbedComponent(props: NodeViewProps) {
+ const { src, width, height, align } = props.node.attrs as EmbedAttributes &
+ EmbedAlignmentOptions;
+
+ const { editor, updateAttributes } = props;
+ const embedRef = useRef<HTMLIFrameElement>();
+ const isActive = editor.isActive("embed", { src });
+ const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
+ const theme = editor.storage.theme as Theme;
+
+ useEffect(() => {
+ setIsToolbarVisible(isActive);
+ }, [isActive]);
+
+ return (
+ <NodeViewWrapper>
+ <ThemeProvider theme={theme}>
+ <Box
+ sx={{
+ display: "flex",
+ justifyContent:
+ align === "center"
+ ? "center"
+ : align === "left"
+ ? "start"
+ : "end",
+ }}
+ >
+ <Resizable
+ size={{
+ height: height || "auto",
+ width: width || "auto",
+ }}
+ maxWidth="100%"
+ onResizeStop={(e, direction, ref, d) => {
+ updateAttributes({
+ width: ref.clientWidth,
+ height: ref.clientHeight,
+ });
+ }}
+ lockAspectRatio={true}
+ >
+ <Flex sx={{ position: "relative", justifyContent: "end" }}>
+ {isToolbarVisible && (
+ <ImageToolbar
+ editor={editor}
+ align={align}
+ height={height || 0}
+ width={width || 0}
+ src={src}
+ />
+ )}
+ </Flex>
+ <Box
+ as="iframe"
+ ref={embedRef}
+ src={src}
+ width={"100%"}
+ height={"100%"}
+ sx={{
+ border: isActive
+ ? "2px solid var(--primary)"
+ : "2px solid transparent",
+ borderRadius: "default",
+ }}
+ {...props}
+ />
+ </Resizable>
+ </Box>
+ </ThemeProvider>
+ </NodeViewWrapper>
+ );
+}
+
+type ImageToolbarProps = Required<EmbedAttributes> &
+ EmbedAlignmentOptions & {
+ editor: Editor;
+ };
+
+function ImageToolbar(props: ImageToolbarProps) {
+ const { editor, height, width, src } = props;
+ const [isOpen, setIsOpen] = useState(false);
+
+ return (
+ <Flex
+ sx={{
+ flexDirection: "column",
+ position: "absolute",
+ top: -40,
+ mb: 2,
+ zIndex: 9999,
+ alignItems: "end",
+ }}
+ >
+ <Flex
+ sx={{
+ bg: "background",
+ boxShadow: "menu",
+ flexWrap: "nowrap",
+ borderRadius: "default",
+ mb: 2,
+ }}
+ >
+ <Flex
+ className="toolbar-group"
+ sx={{
+ pr: 1,
+ mr: 1,
+ borderRight: "1px solid var(--border)",
+ ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
+ }}
+ >
+ <ToolButton
+ toggled={false}
+ title="Align left"
+ id="alignLeft"
+ icon="alignLeft"
+ onClick={() =>
+ editor.chain().focus().setEmbedAlignment({ align: "left" }).run()
+ }
+ />
+
+ <ToolButton
+ toggled={false}
+ title="Align center"
+ id="alignCenter"
+ icon="alignCenter"
+ onClick={() =>
+ editor
+ .chain()
+ .focus()
+ .setEmbedAlignment({ align: "center" })
+ .run()
+ }
+ />
+
+ <ToolButton
+ toggled={false}
+ title="Align right"
+ id="alignRight"
+ icon="alignRight"
+ onClick={() =>
+ editor.chain().focus().setEmbedAlignment({ align: "right" }).run()
+ }
+ />
+ </Flex>
+ <Flex
+ className="toolbar-group"
+ sx={{
+ pr: 1,
+ mr: 1,
+ borderRight: "1px solid var(--border)",
+ ":last-of-type": { mr: 0, pr: 0, borderRight: "none" },
+ }}
+ >
+ <ToolButton
+ toggled={isOpen}
+ title="Embed properties"
+ id="embedProperties"
+ icon="more"
+ onClick={() => setIsOpen((s) => !s)}
+ />
+ </Flex>
+ </Flex>
+
+ {isOpen && (
+ <EmbedPopup
+ title="Embed properties"
+ icon="close"
+ onClose={() => setIsOpen(false)}
+ embed={props}
+ onSourceChanged={(src) => {}}
+ onSizeChanged={(size) => editor.commands.setEmbedSize(size)}
+ />
+ )}
+ </Flex>
+ );
+}
diff --git a/packages/editor/src/extensions/embed/embed.ts b/packages/editor/src/extensions/embed/embed.ts
new file mode 100644
index 000000000..b6af9d93e
--- /dev/null
+++ b/packages/editor/src/extensions/embed/embed.ts
@@ -0,0 +1,104 @@
+import { Node, nodeInputRule, mergeAttributes } from "@tiptap/core";
+import { findChildren, ReactNodeViewRenderer } from "@tiptap/react";
+import { Attribute } from "@tiptap/core";
+import { EmbedComponent } from "./component";
+
+export interface EmbedOptions {
+ HTMLAttributes: Record<string, any>;
+}
+
+export type EmbedAttributes = Partial<EmbedSizeOptions> & {
+ src: string;
+};
+
+export type EmbedAlignmentOptions = {
+ align?: "center" | "left" | "right";
+};
+
+export type EmbedSizeOptions = {
+ width: number;
+ height: number;
+};
+
+declare module "@tiptap/core" {
+ interface Commands<ReturnType> {
+ embed: {
+ /**
+ * Add an image
+ */
+ insertEmbed: (options: EmbedAttributes) => ReturnType;
+ setEmbedAlignment: (options: EmbedAlignmentOptions) => ReturnType;
+ setEmbedSize: (options: EmbedSizeOptions) => ReturnType;
+ };
+ }
+}
+
+export const EmbedNode = Node.create<EmbedOptions>({
+ name: "embed",
+ content: "",
+ marks: "",
+ draggable: true,
+
+ addOptions() {
+ return {
+ HTMLAttributes: {},
+ };
+ },
+
+ group() {
+ return "block";
+ },
+
+ addAttributes() {
+ return {
+ src: {
+ default: null,
+ },
+ width: { default: null },
+ height: { default: null },
+ align: { default: "left" },
+ };
+ },
+
+ parseHTML() {
+ return [
+ {
+ tag: "iframe[src]",
+ },
+ ];
+ },
+
+ renderHTML({ HTMLAttributes }) {
+ return [
+ "iframe",
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),
+ ];
+ },
+
+ addNodeView() {
+ return ReactNodeViewRenderer(EmbedComponent);
+ },
+
+ addCommands() {
+ return {
+ insertEmbed:
+ (options) =>
+ ({ commands }) => {
+ return commands.insertContent({
+ type: this.name,
+ attrs: options,
+ });
+ },
+ setEmbedAlignment:
+ (options) =>
+ ({ commands }) => {
+ return commands.updateAttributes(this.name, { ...options });
+ },
+ setEmbedSize:
+ (options) =>
+ ({ commands }) => {
+ return commands.updateAttributes(this.name, { ...options });
+ },
+ };
+ },
+});
diff --git a/packages/editor/src/extensions/embed/index.ts b/packages/editor/src/extensions/embed/index.ts
new file mode 100644
index 000000000..12b3c5295
--- /dev/null
+++ b/packages/editor/src/extensions/embed/index.ts
@@ -0,0 +1 @@
+export * from "./embed";
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 4779a115d..7fccfea7e 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -30,6 +30,7 @@ import { TaskListNode } from "./extensions/task-list";
import { TaskItemNode } from "./extensions/task-item";
import { Dropcursor } from "./extensions/drop-cursor";
import { SearchReplace } from "./extensions/search-replace";
+import { EmbedNode } from "./extensions/embed";
EditorView.prototype.updateState = function updateState(state) {
if (!(this as any).docView) return; // This prevents the matchesNode error on hot reloads
@@ -93,6 +94,7 @@ const useTiptap = (
}),
ImageNode,
+ EmbedNode,
AttachmentNode.configure({
onDownloadAttachment,
}),
diff --git a/packages/editor/src/toolbar/components/dropdown.tsx b/packages/editor/src/toolbar/components/dropdown.tsx
index 2add61e7f..69eb9351d 100644
--- a/packages/editor/src/toolbar/components/dropdown.tsx
+++ b/packages/editor/src/toolbar/components/dropdown.tsx
@@ -8,16 +8,20 @@ import { MenuItem } from "../../components/menu/types";
type DropdownProps = {
selectedItem: string | JSX.Element;
items: MenuItem[];
+ buttonRef?: React.MutableRefObject<HTMLButtonElement | undefined>;
};
export function Dropdown(props: DropdownProps) {
- const { items, selectedItem } = props;
- const buttonRef = useRef<HTMLButtonElement>(null);
+ const { items, selectedItem, buttonRef } = props;
+ const internalRef = useRef<any>();
const [isOpen, setIsOpen] = useState(false);
return (
<>
<Button
- ref={buttonRef}
+ ref={(ref) => {
+ internalRef.current = ref;
+ if (buttonRef) buttonRef.current = ref;
+ }}
sx={{
p: 1,
m: 0,
@@ -45,7 +49,7 @@ export function Dropdown(props: DropdownProps) {
options={{
type: "menu",
position: {
- target: buttonRef.current || undefined,
+ target: internalRef.current || undefined,
isTargetAbsolute: true,
location: "below",
yOffset: 5,
diff --git a/packages/editor/src/toolbar/components/popup.tsx b/packages/editor/src/toolbar/components/popup.tsx
index a307724d1..02e7e00f2 100644
--- a/packages/editor/src/toolbar/components/popup.tsx
+++ b/packages/editor/src/toolbar/components/popup.tsx
@@ -4,7 +4,7 @@ import { IconNames, Icons } from "../icons";
import { PropsWithChildren } from "react";
import { SchemeColors } from "@notesnook/theme/dist/theme/colorscheme";
-type PopupProps = {
+export type PopupProps = {
title?: string;
action?: PopupButtonProps;
};
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index 2143fddbb..4607864fc 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -75,6 +75,8 @@ import {
mdiFormatFont,
mdiFormatLetterCase,
mdiFormatLetterMatches,
+ mdiMoviePlusOutline,
+ mdiLink,
} from "@mdi/js";
export const Icons = {
@@ -100,6 +102,7 @@ export const Icons = {
highlight: mdiFormatColorHighlight,
textColor: mdiFormatColorText,
link: mdiLinkPlus,
+ url: mdiLink,
image: mdiImage,
colorClear: mdiInvertColorsOff,
check: mdiCheck,
@@ -138,6 +141,7 @@ export const Icons = {
caseSensitive: mdiFormatLetterCase,
wholeWord: mdiFormatLetterMatches,
regex: mdiRegex,
+ embed: mdiMoviePlusOutline,
plus: mdiPlus,
close: mdiClose,
diff --git a/packages/editor/src/toolbar/popups/embed-popup.tsx b/packages/editor/src/toolbar/popups/embed-popup.tsx
new file mode 100644
index 000000000..19a29b3b6
--- /dev/null
+++ b/packages/editor/src/toolbar/popups/embed-popup.tsx
@@ -0,0 +1,191 @@
+import { Button, Flex, Text } from "rebass";
+import { useCallback, useEffect, useState } from "react";
+import { Popup } from "../components/popup";
+import { Toggle } from "../../components/toggle";
+import { Input, Textarea } from "@rebass/forms";
+import {
+ EmbedAlignmentOptions,
+ EmbedAttributes,
+ EmbedSizeOptions,
+} from "../../extensions/embed";
+import { IconNames } from "../icons";
+import { ToolButton } from "../components/tool-button";
+import { convertUrlToEmbedUrl } from "@social-embed/lib";
+
+type Embed = Required<EmbedAttributes> & EmbedAlignmentOptions;
+type EmbedSource = "url" | "code";
+export type EmbedPopupProps = {
+ onClose: (embed: Embed) => void;
+ title: string;
+ icon: IconNames;
+
+ embed?: Embed;
+ onSizeChanged?: (size: EmbedSizeOptions) => void;
+ onSourceChanged?: (src: string) => void;
+};
+
+export function EmbedPopup(props: EmbedPopupProps) {
+ const { onClose, onSizeChanged, onSourceChanged, title, icon, embed } = props;
+ const [width, setWidth] = useState(embed?.width || 300);
+ const [height, setHeight] = useState(embed?.height || 150);
+ const [src, setSrc] = useState(embed?.src || "");
+ const [embedSource, setEmbedSource] = useState<EmbedSource>("url");
+ const [error, setError] = useState<string | null>(null);
+
+ const onSizeChange = useCallback(
+ (newWidth?: number, newHeight?: number) => {
+ const size: EmbedSizeOptions = newWidth
+ ? {
+ width: newWidth,
+ height: newWidth * (height / width),
+ }
+ : newHeight
+ ? {
+ width: newHeight * (width / height),
+ height: newHeight,
+ }
+ : {
+ width: 0,
+ height: 0,
+ };
+ setWidth(size.width);
+ setHeight(size.height);
+ if (onSizeChanged) onSizeChanged(size);
+ },
+ [width, height]
+ );
+
+ useEffect(() => {
+ onSourceChanged && onSourceChanged(src);
+ }, [onSourceChanged, src]);
+
+ return (
+ <Popup
+ title={title}
+ action={{
+ icon,
+ onClick: () => {
+ setError(null);
+ let _src = src;
+ let _width = width;
+ let _height = height;
+ if (embedSource === "code") {
+ const document = new DOMParser().parseFromString(src, "text/html");
+ if (document.getElementsByTagName("iframe").length <= 0)
+ return setError("Embed code must include an iframe.");
+
+ const srcValue = getAttribute(document, "src");
+ if (!srcValue)
+ return setError(
+ "Embed code must include an iframe with an src attribute."
+ );
+
+ _src = srcValue;
+
+ const widthValue = getAttribute(document, "width");
+ if (widthValue && !isNaN(parseInt(widthValue)))
+ _width = parseInt(widthValue);
+
+ const heightValue = getAttribute(document, "height");
+ if (heightValue && !isNaN(parseInt(heightValue)))
+ _height = parseInt(heightValue);
+ }
+ const convertedUrl = convertUrlToEmbedUrl(_src);
+ if (!!convertedUrl) _src = convertedUrl;
+ onClose({
+ height: _height,
+ width: _width,
+ src: _src,
+ });
+ },
+ }}
+ >
+ <Flex sx={{ width: 300, flexDirection: "column", p: 1 }}>
+ {error && (
+ <Text
+ variant={"error"}
+ sx={{
+ bg: "errorBg",
+ color: "error",
+ p: 1,
+ borderRadius: "default",
+ }}
+ >
+ Error: {error}
+ </Text>
+ )}
+ {/* <Flex sx={{ position: "relative", alignItems: "center" }}> */}
+ <Flex>
+ <Button
+ variant={"dialog"}
+ sx={{
+ p: 1,
+ mr: 1,
+ color: embedSource === "url" ? "primary" : "text",
+ }}
+ onClick={() => setEmbedSource("url")}
+ >
+ From link
+ </Button>
+ <Button
+ variant={"dialog"}
+ sx={{
+ p: 1,
+ color: embedSource === "code" ? "primary" : "text",
+ }}
+ onClick={() => setEmbedSource("code")}
+ >
+ From code
+ </Button>
+ </Flex>
+ {embedSource === "url" ? (
+ <Input
+ placeholder="Embed source URL"
+ value={src}
+ autoFocus
+ onChange={(e) => setSrc(e.target.value)}
+ sx={{ p: 1, mt: 1, fontSize: "body" }}
+ />
+ ) : (
+ <Textarea
+ autoFocus
+ variant={"forms.input"}
+ sx={{ fontSize: "subBody", fontFamily: "monospace", mt: 1 }}
+ minHeight={100}
+ onChange={(e) => setSrc(e.target.value)}
+ placeholder="Paste embed code here. Only iframes are supported."
+ />
+ )}
+ {embedSource === "url" ? (
+ <Flex sx={{ alignItems: "center", mt: 2 }}>
+ <Input
+ type="number"
+ placeholder="Width"
+ value={width}
+ sx={{
+ mr: 2,
+ p: 1,
+ fontSize: "body",
+ }}
+ onChange={(e) => onSizeChange(e.target.valueAsNumber)}
+ />
+ <Input
+ type="number"
+ placeholder="Height"
+ value={height}
+ sx={{ p: 1, fontSize: "body" }}
+ onChange={(e) => onSizeChange(undefined, e.target.valueAsNumber)}
+ />
+ </Flex>
+ ) : null}
+ </Flex>
+ </Popup>
+ );
+}
+
+function getAttribute(document: Document, id: string) {
+ const element = document.querySelector(`[${id}]`);
+ if (!element) return null;
+ const attribute = element.getAttribute(id);
+ return attribute;
+}
diff --git a/packages/editor/src/toolbar/toolbar.tsx b/packages/editor/src/toolbar/toolbar.tsx
index 06e7c7d1a..5560e562f 100644
--- a/packages/editor/src/toolbar/toolbar.tsx
+++ b/packages/editor/src/toolbar/toolbar.tsx
@@ -34,7 +34,7 @@ export function Toolbar(props: ToolbarProps) {
["codeblock", "blockquote"],
["formatClear", "ltr", "rtl"],
["numberedList", "bulletList", "checklist"],
- ["link", "image", "attachment", "table"],
+ ["link", "image", "attachment", "table", "embed"],
["textColor", "highlight"],
];
diff --git a/packages/editor/src/toolbar/tools/block.tsx b/packages/editor/src/toolbar/tools/block.tsx
index 33f3d1dac..ab92c855e 100644
--- a/packages/editor/src/toolbar/tools/block.tsx
+++ b/packages/editor/src/toolbar/tools/block.tsx
@@ -9,6 +9,7 @@ import { Dropdown } from "../components/dropdown";
import { Icon } from "../components/icon";
import { Box, Button, Flex, Text } from "rebass";
import { Popup } from "../components/popup";
+import { EmbedPopup } from "../popups/embed-popup";
class BlockTool<TId extends ToolId> implements ITool {
constructor(
@@ -89,6 +90,51 @@ export class Image implements ITool {
};
}
+export class Embed implements ITool {
+ id: ToolId = "embed";
+ title: string = "Embed";
+
+ render = (props: ToolProps) => {
+ const { editor } = props;
+ const [isOpen, setIsOpen] = useState(false);
+ const buttonRef = useRef<HTMLButtonElement>(null);
+ return (
+ <>
+ <ToolButton
+ buttonRef={buttonRef}
+ id={this.id}
+ title={this.title}
+ icon={"embed"}
+ onClick={() => setIsOpen((s) => !s)}
+ toggled={isOpen}
+ />
+ <MenuPresenter
+ isOpen={isOpen}
+ items={[]}
+ onClose={() => setIsOpen(false)}
+ options={{
+ type: "menu",
+ position: {
+ target: buttonRef.current || undefined,
+ isTargetAbsolute: true,
+ location: "below",
+ yOffset: 10,
+ },
+ }}
+ >
+ <EmbedPopup
+ title="Insert embed"
+ icon="check"
+ onClose={(embed) => {
+ editor.chain().focus().insertEmbed(embed).run();
+ }}
+ />
+ </MenuPresenter>
+ </>
+ );
+ };
+}
+
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 4739ad1e5..a735eaae5 100644
--- a/packages/editor/src/toolbar/tools/index.ts
+++ b/packages/editor/src/toolbar/tools/index.ts
@@ -13,7 +13,14 @@ import {
} from "./inline";
import { FontSize, FontFamily } from "./font";
import { AlignCenter, AlignLeft, AlignRight, AlignJustify } from "./alignment";
-import { Blockquote, CodeBlock, HorizontalRule, Image, Table } from "./block";
+import {
+ Blockquote,
+ CodeBlock,
+ HorizontalRule,
+ Image,
+ Table,
+ Embed,
+} from "./block";
import { Headings } from "./headings";
import { NumberedList, BulletList, Checklist } from "./lists";
import { LeftToRight, RightToLeft } from "./text-direction";
@@ -49,6 +56,7 @@ const tools = {
image: new Image(),
attachment: new Attachment(),
table: new Table(),
+ embed: new Embed(),
};
export type ToolId = keyof typeof tools;
export function findToolById(id: ToolId): ITool {
diff --git a/packages/editor/toolbar.md b/packages/editor/toolbar.md
index ae34223b4..3e0eea3c3 100644
--- a/packages/editor/toolbar.md
+++ b/packages/editor/toolbar.md
@@ -66,7 +66,7 @@ Total Tools:
- [x] link
- [x] image
- [x] attachment
-- [] media
+- [x] media
- [x] table
- [x] ltr rtl
- [x] searchreplace