Skip to content

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

editor: eagerly load image/webclip instead of waiting on client

Abdullah Attaf53fb58024ba
diff --git a/packages/editor/src/extensions/attachment/attachment.ts b/packages/editor/src/extensions/attachment/attachment.ts
index d5feed476..124e00fe0 100644
--- a/packages/editor/src/extensions/attachment/attachment.ts
+++ b/packages/editor/src/extensions/attachment/attachment.ts
@@ -21,30 +21,17 @@ import { Node, mergeAttributes, findChildren, Editor } from "@tiptap/core";
import { Attribute } from "@tiptap/core";
import { createSelectionBasedNodeView } from "../react";
import { AttachmentComponent } from "./component";
+import { Attachment } from "./types";
export type AttachmentType = "image" | "file" | "camera";
export interface AttachmentOptions {
+ types: string[];
HTMLAttributes: Record<string, unknown>;
onDownloadAttachment: (editor: Editor, attachment: Attachment) => boolean;
onOpenAttachmentPicker: (editor: Editor, type: AttachmentType) => boolean;
onPreviewAttachment: (editor: Editor, attachment: Attachment) => boolean;
}
-export type AttachmentWithProgress = AttachmentProgress & Attachment;
-
-export type Attachment = {
- hash: string;
- filename: string;
- mime: string;
- size: number;
-};
-
-export type AttachmentProgress = {
- progress: number;
- type: "upload" | "download" | "encrypt";
- hash: string;
-};
-
declare module "@tiptap/core" {
interface Commands<ReturnType> {
attachment: {
@@ -52,8 +39,14 @@ declare module "@tiptap/core" {
insertAttachment: (attachment: Attachment) => ReturnType;
removeAttachment: () => ReturnType;
downloadAttachment: (attachment: Attachment) => ReturnType;
- setAttachmentProgress: (progress: AttachmentProgress) => ReturnType;
previewAttachment: (options: Attachment) => ReturnType;
+ updateAttachment: (
+ attachment: Partial<Attachment>,
+ options: {
+ preventUpdate?: boolean;
+ query: (attachment: Attachment) => boolean;
+ }
+ ) => ReturnType;
};
}
}
@@ -67,6 +60,7 @@ export const AttachmentNode = Node.create<AttachmentOptions>({
addOptions() {
return {
+ types: [this.name],
HTMLAttributes: {},
onDownloadAttachment: () => false,
onOpenAttachmentPicker: () => false,
@@ -82,6 +76,7 @@ export const AttachmentNode = Node.create<AttachmentOptions>({
addAttributes() {
return {
+ type: { default: "file", rendered: false },
progress: {
default: 0,
rendered: false
@@ -153,23 +148,24 @@ export const AttachmentNode = Node.create<AttachmentOptions>({
({ editor }) => {
return this.options.onOpenAttachmentPicker(editor, type);
},
- setAttachmentProgress:
- (options) =>
+ updateAttachment:
+ (attachment, options) =>
({ state, tr, dispatch }) => {
- const { hash, progress } = options;
const attachments = findChildren(
state.doc,
(node) =>
- (node.type.name === this.name || node.type.name === "image") &&
- node.attrs.hash === hash
+ this.options.types.includes(node.type.name) &&
+ options.query(node.attrs as Attachment)
);
- for (const attachment of attachments) {
- tr.setNodeMarkup(attachment.pos, attachment.node.type, {
- ...attachment.node.attrs,
- progress: progress === 100 ? null : progress
+ if (!attachments.length) return false;
+
+ for (const { node, pos } of attachments) {
+ tr.setNodeMarkup(pos, node.type, {
+ ...node.attrs,
+ ...attachment
});
}
- tr.setMeta("preventUpdate", true);
+ tr.setMeta("preventUpdate", options.preventUpdate || false);
tr.setMeta("addToHistory", false);
if (dispatch) dispatch(tr);
return true;
diff --git a/packages/editor/src/extensions/attachment/component.tsx b/packages/editor/src/extensions/attachment/component.tsx
index 95b9cb045..d021e2ec5 100644
--- a/packages/editor/src/extensions/attachment/component.tsx
+++ b/packages/editor/src/extensions/attachment/component.tsx
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Box, Text } from "@theme-ui/components";
-import { AttachmentWithProgress } from "./attachment";
+import { FileAttachment } from "./types";
import { useRef, useState } from "react";
import { Icon } from "@notesnook/ui";
import { Icons } from "../../toolbar/icons";
@@ -27,7 +27,7 @@ import { ToolbarGroup } from "../../toolbar/components/toolbar-group";
import { DesktopOnly } from "../../components/responsive";
export function AttachmentComponent(
- props: SelectionBasedReactNodeViewProps<AttachmentWithProgress>
+ props: SelectionBasedReactNodeViewProps<FileAttachment>
) {
const { editor, node, selected } = props;
const { filename, size, progress } = node.attrs;
diff --git a/packages/editor/src/extensions/attachment/index.ts b/packages/editor/src/extensions/attachment/index.ts
index 424e1c65a..b1a5168f0 100644
--- a/packages/editor/src/extensions/attachment/index.ts
+++ b/packages/editor/src/extensions/attachment/index.ts
@@ -18,3 +18,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
export * from "./attachment";
+export * from "./types";
diff --git a/packages/editor/src/extensions/attachment/types.ts b/packages/editor/src/extensions/attachment/types.ts
new file mode 100644
index 000000000..8d8429b52
--- /dev/null
+++ b/packages/editor/src/extensions/attachment/types.ts
@@ -0,0 +1,53 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+export type BaseAttachment = {
+ hash: string;
+ filename: string;
+ mime: string;
+ size: number;
+ progress?: number;
+};
+
+export type FileAttachment = BaseAttachment & {
+ type: "file";
+};
+
+export type WebClipAttachment = BaseAttachment & {
+ type: "web-clip";
+ src: string;
+ title: string;
+ width?: string;
+ height?: string;
+};
+
+export type ImageAttachment = BaseAttachment & {
+ type: "image";
+ width?: number;
+ height?: number;
+ src?: string;
+ aspectRatio?: number;
+} & ImageAlignmentOptions;
+
+export type ImageAlignmentOptions = {
+ float?: boolean;
+ align?: "center" | "left" | "right";
+};
+
+export type Attachment = FileAttachment | WebClipAttachment | ImageAttachment;
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 034896f5e..fb193d7be 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -17,9 +17,10 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { ThemeUIStyleObject } from "@theme-ui/core";
import { Box, Flex, Image } from "@theme-ui/components";
-import { ImageAlignmentOptions, ImageAttributes } from "./image";
-import { useRef } from "react";
+import { ImageAttributes } from "./image";
+import { useEffect, useState } from "react";
import { SelectionBasedReactNodeViewProps } from "../react";
import { DesktopOnly } from "../../components/responsive";
import { Icon } from "@notesnook/ui";
@@ -38,56 +39,59 @@ import {
toDataURL
} from "../../utils/downloader";
import { motion } from "framer-motion";
+import { useObserver } from "../../hooks/use-observer";
+import { Attachment, ImageAlignmentOptions } from "../attachment";
export const AnimatedImage = motion(Image);
export function ImageComponent(
- props: SelectionBasedReactNodeViewProps<
- Partial<ImageAttributes & ImageAlignmentOptions>
- >
+ props: SelectionBasedReactNodeViewProps<Partial<ImageAttributes>>
) {
const { editor, node, selected } = props;
+ const { src, alt, title, textDirection, hash, aspectRatio, mime } =
+ node.attrs;
+ const [bloburl, setBloburl] = useState<string | undefined>(
+ toBlobURL("", hash)
+ );
+
const isMobile = useIsMobile();
- const {
- bloburl,
- src,
- alt,
- title,
- width,
- height,
- textDirection,
- hash,
- aspectRatio,
- mime
- } = node.attrs;
+ const { inView, ref: imageRef } = useObserver<HTMLImageElement>({
+ threshold: 0.2
+ });
const float = isMobile ? false : node.attrs.float;
let align = node.attrs.align;
if (!align) align = textDirection ? "right" : "left";
- const imageRef = useRef<HTMLImageElement>(null);
const downloadOptions = useToolbarStore((store) => store.downloadOptions);
const isReadonly = !editor.current?.isEditable;
const isSVG = !!mime && mime.includes("/svg");
- const relativeHeight = aspectRatio
- ? editor.view.dom.clientWidth / aspectRatio
- : undefined;
+ const { height, width } = clampSize(
+ node.attrs,
+ editor.view.dom.clientWidth,
+ aspectRatio
+ );
+
+ useEffect(() => {
+ if (!inView) return;
+ if (src || !hash || bloburl) return;
+ (async function () {
+ const data = await editor.current?.storage
+ .getAttachmentData?.(node.attrs)
+ .catch(() => null);
+ if (typeof data !== "string" || !data) return; // TODO: show error
+
+ setBloburl(toBlobURL(data, hash));
+ })();
+ }, [inView]);
+
+ console.log({ width, height, aspectRatio });
return (
<>
<Box
sx={{
- display: float ? "inline" : "flex",
- ml: float ? (align === "right" ? 2 : 0) : 0,
- mr: float ? (align === "left" ? 2 : 0) : 0,
- float: float ? (align as "left" | "right") : "none",
- justifyContent: float
- ? "stretch"
- : align === "center"
- ? "center"
- : align === "left"
- ? "start"
- : "end",
+ ...getAlignmentStyles(node.attrs),
position: "relative",
mt: isSVG ? `24px` : 0,
":hover .drag-handle, :active .drag-handle": {
@@ -95,41 +99,12 @@ export function ImageComponent(
}
}}
>
- {!src && !bloburl && hash && (
- <Flex
- sx={{
- position: "absolute",
- top: 0,
- left: 0,
- width: width || "100%",
- height: height || relativeHeight || "100%",
- maxWidth: "100%",
- minWidth: 135,
- bg: "background-secondary",
- border: selected
- ? "2px solid var(--accent)"
- : "2px solid transparent",
- borderRadius: "default",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- mt: 1,
- py: 50
- }}
- >
- <Icon
- path={Icons.image}
- size={width ? width * 0.2 : 72}
- color="gray"
- />
- </Flex>
- )}
<Resizer
style={{ marginTop: 5 }}
enabled={editor.isEditable && !float}
selected={selected}
width={width}
- height={height || relativeHeight}
+ height={height}
onResize={(width, height) => {
editor.commands.setImageSize({ width, height });
}}
@@ -158,8 +133,9 @@ export function ImageComponent(
hash ? "previewAttachment" : "none",
hash ? "downloadAttachment" : "none",
"imageAlignLeft",
- float ? "none" : "imageAlignCenter",
+ "imageAlignCenter",
"imageAlignRight",
+ "imageFloat",
"imageProperties"
]
}
@@ -211,28 +187,50 @@ export function ImageComponent(
}}
></Box>
) : null}
+
+ {!src && !bloburl && hash && (
+ <Flex
+ sx={{
+ position: "absolute",
+ top: 0,
+ left: 0,
+ width: editor.isEditable ? "100%" : width,
+ height: editor.isEditable ? "100%" : height,
+ bg: "background-secondary",
+ border: selected
+ ? "2px solid var(--accent)"
+ : "2px solid transparent",
+ borderRadius: "default",
+ flexDirection: "column",
+ alignItems: "center",
+ justifyContent: "center",
+ // mt: 1,
+ py: 50
+ }}
+ >
+ <Icon
+ path={Icons.image}
+ size={width ? width * 0.2 : 72}
+ color="gray"
+ />
+ </Flex>
+ )}
<AnimatedImage
as={isSVG ? "object" : "img"}
initial={{ opacity: 0 }}
animate={{ opacity: bloburl || src ? 1 : 0 }}
- transition={{ duration: 0.5, ease: "easeIn" }}
+ transition={{ duration: 0.2, ease: "easeIn" }}
data-drag-image
ref={imageRef}
alt={alt}
crossOrigin="anonymous"
{...(isSVG
? {
- data:
- toBlobURL("", hash) ||
- bloburl ||
- corsify(src, downloadOptions?.corsHost),
+ data: bloburl || corsify(src, downloadOptions?.corsHost),
type: mime
}
: {
- src:
- toBlobURL("", hash) ||
- bloburl ||
- corsify(src, downloadOptions?.corsHost)
+ src: bloburl || corsify(src, downloadOptions?.corsHost)
})}
title={title}
sx={{
@@ -248,6 +246,7 @@ export function ImageComponent(
const { hash, filename, mime, size } = node.attrs;
if (!!hash && !!filename && !!mime && !!size)
editor.current?.commands.previewAttachment({
+ type: "image",
hash,
filename,
mime,
@@ -256,36 +255,49 @@ export function ImageComponent(
}}
onLoad={async function onLoad() {
if (!imageRef.current) return;
- const { clientHeight, clientWidth } = imageRef.current;
+
+ const { naturalWidth, naturalHeight, clientHeight, clientWidth } =
+ imageRef.current;
+ const orignalWidth = naturalWidth || clientWidth;
+ const orignalHeight = naturalHeight || clientHeight;
+ const naturalAspectRatio = orignalWidth / orignalHeight;
+ const fixedDimensions = fixAspectRatio(width, naturalAspectRatio);
+
if (src && !isDataUrl(src) && canParse(src)) {
const image = await downloadImage(src, downloadOptions);
if (!image) return;
const { url, size, blob, mimeType } = image;
+ imageRef.current.src = url;
const dataurl = await toDataURL(blob);
await editor.threadsafe((editor) =>
- editor.commands.updateImage(
- { src, hash },
+ editor.commands.updateAttachment(
{
+ ...fixedDimensions,
src: dataurl,
- bloburl: url,
size: size,
mime: mimeType,
- aspectRatio:
- !height && !width && !aspectRatio
- ? clientWidth / clientHeight
- : undefined
- }
+ aspectRatio: naturalAspectRatio
+ },
+ { query: makeImageQuery(src, hash) }
)
);
- } else if (!height && !width && !aspectRatio) {
+ } else if (!aspectRatio || aspectRatio != naturalAspectRatio) {
await editor.threadsafe((editor) =>
- editor.commands.updateImage(
- { src, hash },
+ editor.commands.updateAttachment(
{
- aspectRatio: clientWidth / clientHeight
- }
+ ...fixedDimensions,
+ aspectRatio: naturalAspectRatio
+ },
+ { query: makeImageQuery(src, hash) }
)
);
+ } else if (height !== fixedDimensions.height) {
+ console.log("RESETTING HEIGHT");
+ await editor.threadsafe((editor) =>
+ editor.commands.updateAttachment(fixedDimensions, {
+ query: makeImageQuery(src, hash)
+ })
+ );
}
}}
/>
@@ -296,6 +308,10 @@ export function ImageComponent(
);
}
+function makeImageQuery(src?: string, hash?: string) {
+ return (a: Attachment) =>
+ (a.type === "image" && a.src === src) || a.hash === hash;
+}
function canParse(src: string) {
if (!src) return false;
try {
@@ -304,3 +320,52 @@ function canParse(src: string) {
return false;
}
}
+
+function clampSize(
+ size: { width?: number; height?: number },
+ maxWidth: number,
+ aspectRatio?: number
+): { width: number; height: number } {
+ if (typeof aspectRatio === "string" && isNaN(aspectRatio)) aspectRatio = 1;
+
+ // if no size we set the image to maximum size.
+ if (!size.width || !size.height) return { width: maxWidth, height: maxWidth };
+
+ if (!aspectRatio) aspectRatio = size.width / size.height;
+
+ if (size.width > maxWidth)
+ return { width: maxWidth, height: maxWidth / aspectRatio };
+
+ return {
+ height: size.height,
+ width: size.width
+ };
+}
+
+function fixAspectRatio(width: number, aspectRatio: number) {
+ return {
+ width,
+ height: width / aspectRatio
+ };
+}
+
+function getAlignmentStyles(
+ options: ImageAlignmentOptions
+): ThemeUIStyleObject {
+ const { align, float } = options;
+ if (float && align !== "center") {
+ return {
+ display: "inline",
+ ml: align === "right" ? 2 : 0,
+ mr: align === "left" ? 2 : 0,
+ float: align,
+ justifyContent: "stretch"
+ };
+ }
+
+ return {
+ display: "flex",
+ justifyContent:
+ align === "center" ? "center" : align === "right" ? "end" : "start"
+ };
+}
diff --git a/packages/editor/src/extensions/image/image.ts b/packages/editor/src/extensions/image/image.ts
index 65605a4fe..a565939e2 100644
--- a/packages/editor/src/extensions/image/image.ts
+++ b/packages/editor/src/extensions/image/image.ts
@@ -17,63 +17,28 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import {
- Node,
- nodeInputRule,
- mergeAttributes,
- findChildren
-} from "@tiptap/core";
-import { Attrs } from "@tiptap/pm/model";
-import { Plugin } from "@tiptap/pm/state";
+import { Node, nodeInputRule, mergeAttributes } from "@tiptap/core";
import { hasSameAttributes } from "../../utils/prosemirror";
-import { Attachment, getDataAttribute } from "../attachment";
+import {
+ ImageAlignmentOptions,
+ ImageAttachment,
+ getDataAttribute
+} from "../attachment";
import { createSelectionBasedNodeView } from "../react";
import { TextDirections } from "../text-direction";
import { ImageComponent } from "./component";
-type Writeable<T> = { -readonly [P in keyof T]: T[P] };
-
export interface ImageOptions {
inline: boolean;
allowBase64: boolean;
HTMLAttributes: Record<string, unknown>;
}
-/**
- * We have two attributes that store the source of an image:
- *
- * 1. `src`
- * 2. `dataurl`
- *
- * `src` is the image's inherent source. This can contain a URL, a base64-dataurl,
- * a blob...etc. We should never touch this attribute. This is also where we store
- * the data that we want to upload so after we download a pasted image, its base64
- * dataurl goes in this attribute.
- *
- * `dataurl` should never get added to the final HTML. This attribute is where we
- * restore an image's data after loading a note.
- *
- * The reason we have 2 instead of a single attribute is to avoid unnecessary processing.
- * Keeping everything in the `src` attribute requires us to always send the rendered image
- * along with everything else. This is pointless because we already have the image's rendered
- * data.
- */
-export type ImageAttributes = Partial<ImageSizeOptions> &
- Attachment & {
- src: string;
- bloburl?: string;
- alt?: string;
- title?: string;
- textDirection?: TextDirections;
- aspectRatio?: number;
- };
-
-export type ImageAlignmentOptions = {
- float?: boolean;
- align?: "center" | "left" | "right";
+export type ImageAttributes = ImageAttachment & {
+ textDirection?: TextDirections;
};
-export type ImageSizeOptions = {
+export type ImageSize = {
width: number;
height: number;
};
@@ -85,12 +50,8 @@ declare module "@tiptap/core" {
* Add an image
*/
insertImage: (options: Partial<ImageAttributes>) => ReturnType;
- updateImage: (
- query: { src?: string; hash?: string },
- options: Partial<ImageAttributes> & { preventUpdate?: boolean }
- ) => ReturnType;
setImageAlignment: (options: ImageAlignmentOptions) => ReturnType;
- setImageSize: (options: ImageSizeOptions) => ReturnType;
+ setImageSize: (size: ImageSize) => ReturnType;
};
}
}
@@ -120,13 +81,13 @@ export const ImageNode = Node.create<ImageOptions>({
addAttributes() {
return {
- src: {
- default: null
- },
- alt: {
- default: null
+ type: { default: "image", rendered: false },
+ progress: {
+ default: 0,
+ rendered: false
},
- title: {
+
+ src: {
default: null
},
width: { default: null },
@@ -142,7 +103,10 @@ export const ImageNode = Node.create<ImageOptions>({
size: getDataAttribute("size"),
aspectRatio: {
default: undefined,
- parseHTML: (element) => element.dataset.aspectRatio,
+ parseHTML: (element) =>
+ element.dataset.aspectRatio
+ ? parseFloat(element.dataset.aspectRatio)
+ : 1,
renderHTML: (attributes) => {
if (!attributes.aspectRatio) {
return {};
@@ -152,11 +116,6 @@ export const ImageNode = Node.create<ImageOptions>({
[`data-aspect-ratio`]: attributes.aspectRatio
};
}
- },
-
- bloburl: {
- ...getDataAttribute("bloburl"),
- rendered: false
}
};
},
@@ -184,28 +143,6 @@ export const ImageNode = Node.create<ImageOptions>({
});
},
- addProseMirrorPlugins() {
- return [
- new Plugin({
- props: {
- transformCopied: (content) => {
- content.content.descendants((node) => {
- if (
- node.type.name === this.name &&
- typeof node.attrs.bloburl === "string"
- ) {
- const attrs = node.attrs as Writeable<Attrs>;
- attrs.src = attrs.bloburl;
- delete attrs.bloburl;
- }
- });
- return content;
- }
- }
- })
- ];
- },
-
addCommands() {
return {
insertImage:
@@ -242,32 +179,6 @@ export const ImageNode = Node.create<ImageOptions>({
.updateAttributes(this.name, { ...options })
.setNodeSelection(from)
.run();
- },
- updateImage:
- (query, options) =>
- ({ state, tr }) => {
- const keyedQuery = query.hash
- ? { key: "hash", value: query.hash }
- : query.src
- ? { key: "src", value: query.src }
- : null;
- if (!keyedQuery) return false;
-
- const images = findChildren(
- state.doc,
- (node) =>
- node.type.name === this.name &&
- node.attrs[keyedQuery.key] === keyedQuery.value
- );
- for (const image of images) {
- tr.setNodeMarkup(image.pos, image.node.type, {
- ...image.node.attrs,
- ...options
- });
- }
- tr.setMeta("preventUpdate", options.preventUpdate || false);
- tr.setMeta("addToHistory", false);
- return true;
}
};
},
diff --git a/packages/editor/src/extensions/web-clip/component.tsx b/packages/editor/src/extensions/web-clip/component.tsx
index 2e7f34fa6..40325b575 100644
--- a/packages/editor/src/extensions/web-clip/component.tsx
+++ b/packages/editor/src/extensions/web-clip/component.tsx
@@ -41,18 +41,25 @@ export function WebClipComponent(
const [isLoading, setIsLoading] = useState(true);
const embedRef = useRef<HTMLIFrameElement>(null);
const resizeObserverRef = useRef<ResizeObserver>();
- const { src, title, fullscreen, html } = node.attrs;
+ const { src, title, fullscreen, progress } = node.attrs;
useEffect(() => {
- const iframe = embedRef.current;
- if (!iframe || !iframe.contentDocument || !isLoading || !html) return;
- iframe.contentDocument.open();
- iframe.contentDocument.write(html || FAILED_CONTENT);
- iframe.contentDocument.close();
- iframe.contentDocument.head.innerHTML += `<base target="_blank">`;
-
- setIsLoading(false);
- }, [html]);
+ (async function () {
+ const iframe = embedRef.current;
+ if (!iframe || !iframe.contentDocument || !isLoading) return;
+
+ const html = await editor.current?.storage
+ .getAttachmentData?.(node.attrs)
+ .catch(() => null);
+ iframe.contentDocument.open();
+ iframe.contentDocument.write(
+ typeof html !== "string" || !html ? FAILED_CONTENT : html
+ );
+ iframe.contentDocument.close();
+ iframe.contentDocument.head.innerHTML += `<base target="_blank">`;
+ setIsLoading(false);
+ })();
+ }, []);
useEffect(() => {
function fullscreenchanged() {
@@ -193,10 +200,14 @@ export function WebClipComponent(
width: "100%",
height: "calc(100% - 20px)",
alignItems: "center",
- justifyContent: "center"
+ justifyContent: "center",
+ flexDirection: "column"
}}
>
<Icon path={Icons.loading} rotate size={32} />
+ {progress ? (
+ <Text sx={{ mt: 2 }}>Loading web clip ({progress}%)</Text>
+ ) : null}
</Flex>
)}
</Box>
diff --git a/packages/editor/src/extensions/web-clip/web-clip.ts b/packages/editor/src/extensions/web-clip/web-clip.ts
index c17e28cfa..ac5bd4310 100644
--- a/packages/editor/src/extensions/web-clip/web-clip.ts
+++ b/packages/editor/src/extensions/web-clip/web-clip.ts
@@ -17,9 +17,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Node, mergeAttributes, findChildren } from "@tiptap/core";
+import { Node, mergeAttributes } from "@tiptap/core";
import { hasSameAttributes } from "../../utils/prosemirror";
-import { getDataAttribute } from "../attachment";
+import { WebClipAttachment, getDataAttribute } from "../attachment";
import { createSelectionBasedNodeView } from "../react";
import { WebClipComponent } from "./component";
@@ -27,28 +27,10 @@ export interface WebClipOptions {
HTMLAttributes: Record<string, unknown>;
}
-export type WebClipAttributes = {
+export type WebClipAttributes = WebClipAttachment & {
fullscreen: boolean;
- src: string;
- html: string;
- title: string;
- hash: string;
- type: string;
- width?: string;
- height?: string;
};
-declare module "@tiptap/core" {
- interface Commands<ReturnType> {
- webclip: {
- updateWebClip: (
- query: { hash?: string },
- options: { src: string }
- ) => ReturnType;
- };
- }
-}
-
export const WebClipNode = Node.create<WebClipOptions>({
name: "webclip",
content: "",
@@ -68,14 +50,15 @@ export const WebClipNode = Node.create<WebClipOptions>({
addAttributes() {
return {
+ type: { default: "web-clip", rendered: false },
+ progress: {
+ default: 0,
+ rendered: false
+ },
fullscreen: {
rendered: false,
default: false
},
- html: {
- rendered: false,
- default: null
- },
src: {
default: null
},
@@ -89,7 +72,7 @@ export const WebClipNode = Node.create<WebClipOptions>({
default: null
},
hash: getDataAttribute("hash"),
- type: getDataAttribute("mime")
+ mime: getDataAttribute("mime")
};
},
@@ -113,30 +96,5 @@ export const WebClipNode = Node.create<WebClipOptions>({
shouldUpdate: (prev, next) => !hasSameAttributes(prev.attrs, next.attrs),
forceEnableSelection: true
});
- },
-
- addCommands() {
- return {
- updateWebClip:
- (query, options) =>
- ({ state, tr, dispatch }) => {
- const clips = findChildren(
- state.doc,
- (node) =>
- node.type.name === this.name && node.attrs["hash"] === query.hash
- );
-
- for (const clip of clips) {
- tr.setNodeMarkup(clip.pos, clip.node.type, {
- ...clip.node.attrs,
- html: options.src
- });
- }
- tr.setMeta("preventUpdate", true);
- tr.setMeta("addToHistory", false);
- if (dispatch) dispatch(tr);
- return true;
- }
- };
}
});
diff --git a/packages/editor/src/hooks/use-observer.ts b/packages/editor/src/hooks/use-observer.ts
new file mode 100644
index 000000000..7202d141f
--- /dev/null
+++ b/packages/editor/src/hooks/use-observer.ts
@@ -0,0 +1,75 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { useState, useRef, useEffect, useCallback } from "react";
+
+type ObserverType = {
+ threshold: number;
+ rootMargin?: string;
+ once?: boolean;
+};
+
+export function useObserver<T extends Element = Element>({
+ threshold,
+ rootMargin = "0px",
+ once = false
+}: ObserverType) {
+ const [inView, setInView] = useState<boolean>();
+ const ref = useRef<T>(null);
+ const iObserverRef = useRef<IntersectionObserver>();
+
+ const updateInView = useCallback(
+ (val: boolean) => {
+ if (inView && once) {
+ return;
+ }
+ setInView(val);
+ },
+ [inView, once]
+ );
+
+ useEffect(() => {
+ const options = {
+ root: null,
+ rootMargin: rootMargin,
+ threshold: threshold
+ };
+
+ iObserverRef.current = new IntersectionObserver((entries) => {
+ updateInView(entries[0].isIntersecting);
+ }, options);
+ });
+
+ useEffect(() => {
+ if (ref.current) {
+ iObserverRef?.current?.observe(ref.current);
+ }
+
+ const reference = ref.current;
+
+ return () => {
+ if (reference) {
+ iObserverRef.current?.unobserve(reference);
+ iObserverRef.current?.disconnect();
+ }
+ };
+ });
+
+ return { inView, ref };
+}
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index c144c9f3c..3bfb5eee4 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -39,7 +39,11 @@ import StarterKit from "@tiptap/starter-kit";
import ListKeymap from "@tiptap/extension-list-keymap";
import { useEffect, useMemo } from "react";
import "./extensions";
-import { AttachmentNode, AttachmentOptions } from "./extensions/attachment";
+import {
+ Attachment,
+ AttachmentNode,
+ AttachmentOptions
+} from "./extensions/attachment";
import BulletList from "./extensions/bullet-list";
import { CodeBlock } from "./extensions/code-block";
import { Codemark } from "./extensions/code-mark";
@@ -107,6 +111,7 @@ export type TiptapOptions = EditorOptions &
downloadOptions?: DownloadOptions;
isMobile?: boolean;
doubleSpacedLines?: boolean;
+ getAttachmentData: (attachment: Attachment) => Promise<string | undefined>;
};
const useTiptap = (
@@ -120,6 +125,7 @@ const useTiptap = (
onOpenAttachmentPicker,
onPreviewAttachment,
onOpenLink,
+ getAttachmentData,
onBeforeCreate,
downloadOptions,
dateFormat,
@@ -259,7 +265,8 @@ const useTiptap = (
AttachmentNode.configure({
onDownloadAttachment,
onOpenAttachmentPicker,
- onPreviewAttachment
+ onPreviewAttachment,
+ types: [AttachmentNode.name, ImageNode.name, WebClipNode.name]
}),
OutlineListItem,
OutlineList.configure({ keepAttributes: true, keepMarks: true }),
@@ -310,6 +317,7 @@ const useTiptap = (
editor.storage.portalProviderAPI = PortalProviderAPI;
editor.storage.dateFormat = dateFormat;
editor.storage.timeFormat = timeFormat;
+ editor.storage.getAttachmentData = getAttachmentData;
if (onBeforeCreate) onBeforeCreate({ editor });
},
injectCSS: false,
@@ -319,6 +327,7 @@ const useTiptap = (
onPreviewAttachment,
onDownloadAttachment,
onOpenAttachmentPicker,
+ getAttachmentData,
PortalProviderAPI,
onBeforeCreate,
onOpenLink,
@@ -342,6 +351,7 @@ const useTiptap = (
export { type Fragment } from "prosemirror-model";
export { type Attachment, type AttachmentType } from "./extensions/attachment";
+export { type ImageAttributes } from "./extensions/image";
export * from "./extensions/react";
export * from "./toolbar";
export * from "./types";
diff --git a/packages/editor/src/toolbar/icons.ts b/packages/editor/src/toolbar/icons.ts
index efad3c2a0..2bd7e962c 100644
--- a/packages/editor/src/toolbar/icons.ts
+++ b/packages/editor/src/toolbar/icons.ts
@@ -119,7 +119,8 @@ import {
mdiPencilOff,
mdiPencil,
mdiCheckboxMultipleBlankOutline,
- mdiCheckboxMultipleMarked
+ mdiCheckboxMultipleMarked,
+ mdiFormatFloatLeft
} from "@mdi/js";
export const Icons = {
@@ -167,6 +168,7 @@ export const Icons = {
imageDownload: mdiProgressDownload,
imageFailed: mdiProgressAlert,
imageSettings: mdiImageEditOutline,
+ imageFloat: mdiFormatFloatLeft,
colorClear: mdiInvertColorsOff,
save: mdiContentSaveOutline,
copy: mdiClipboardOutline,
diff --git a/packages/editor/src/toolbar/popups/image-properties.tsx b/packages/editor/src/toolbar/popups/image-properties.tsx
index 8f887909f..d13ba363b 100644
--- a/packages/editor/src/toolbar/popups/image-properties.tsx
+++ b/packages/editor/src/toolbar/popups/image-properties.tsx
@@ -18,82 +18,56 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Flex } from "@theme-ui/components";
-import { useCallback } from "react";
import { Popup } from "../components/popup";
-import { Checkbox, Label } from "@theme-ui/components";
-import {
- ImageAlignmentOptions,
- ImageSizeOptions
-} from "../../extensions/image";
+import { ImageAttributes } from "../../extensions/image";
import { Editor } from "../../types";
import { InlineInput } from "../../components/inline-input";
-import { DesktopOnly } from "../../components/responsive";
+import { findSelectedNode } from "../../utils/prosemirror";
-export type ImagePropertiesProps = ImageSizeOptions &
- ImageAlignmentOptions & { editor: Editor; onClose: () => void };
+export type ImagePropertiesProps = {
+ editor: Editor;
+ onClose: () => void;
+};
export function ImageProperties(props: ImagePropertiesProps) {
- const { height, width, float, editor, onClose } = props;
+ const { editor, onClose } = props;
- const onSizeChange = useCallback(
- (newWidth?: number, newHeight?: number) => {
- const size: ImageSizeOptions = newWidth
- ? {
- width: newWidth,
- height: newWidth * (height / width)
- }
- : newHeight
- ? {
- width: newHeight * (width / height),
- height: newHeight
- }
- : {
- width: 0,
- height: 0
- };
+ const image = findSelectedNode(editor, "image");
+ if (!image) return null;
- editor.current?.chain().setImageSize(size).run();
- },
- // eslint-disable-next-line react-hooks/exhaustive-deps
- [width, height]
- );
+ const { width, height, aspectRatio } = image.attrs as ImageAttributes;
return (
<Popup title="Image properties" onClose={onClose}>
- <Flex sx={{ width: ["auto", 300], flexDirection: "column", p: 1 }}>
- <DesktopOnly>
- <Label
- variant="text.body"
- sx={{ justifyContent: "space-between", alignItems: "center" }}
- >
- Float image
- <Checkbox
- checked={float}
- onClick={() =>
- editor
- .chain()
- .setImageAlignment({ float: !float, align: "left" })
- .run()
- }
- />
- </Label>
- </DesktopOnly>
- <Flex sx={{ alignItems: "center", mt: 2 }}>
- <InlineInput
- label="width"
- type="number"
- value={width}
- containerProps={{
- sx: { mr: 1 }
- }}
- onChange={(e) => onSizeChange(e.target.valueAsNumber)}
- />
- <InlineInput
- label="height"
- type="number"
- value={height}
- onChange={(e) => onSizeChange(undefined, e.target.valueAsNumber)}
- />
- </Flex>
+ <Flex sx={{ width: ["auto", 300], alignItems: "center", p: 1 }}>
+ <InlineInput
+ label="width"
+ type="number"
+ value={width || 0}
+ containerProps={{
+ sx: { mr: 1 }
+ }}
+ onChange={(e) => {
+ editor.current?.commands.setImageSize({
+ width: e.target.valueAsNumber,
+ height: aspectRatio
+ ? e.target.valueAsNumber / aspectRatio
+ : e.target.valueAsNumber
+ });
+ }}
+ />
+ <InlineInput
+ label="height"
+ type="number"
+ value={height || 0}
+ onChange={(e) => {
+ editor.current?.commands.setImageSize({
+ width: aspectRatio
+ ? e.target.valueAsNumber * aspectRatio
+ : e.target.valueAsNumber,
+ height: e.target.valueAsNumber
+ });
+ }}
+ />
</Flex>
</Popup>
);
diff --git a/packages/editor/src/toolbar/tool-definitions.ts b/packages/editor/src/toolbar/tool-definitions.ts
index 4b268eacd..c12ff8121 100644
--- a/packages/editor/src/toolbar/tool-definitions.ts
+++ b/packages/editor/src/toolbar/tool-definitions.ts
@@ -262,6 +262,11 @@ const tools: Record<ToolId, ToolDefinition> = {
title: "Align right",
conditional: true
},
+ imageFloat: {
+ icon: "imageFloat",
+ title: "Float image",
+ conditional: true
+ },
imageProperties: {
icon: "more",
title: "Image properties",
diff --git a/packages/editor/src/toolbar/tools/attachment.tsx b/packages/editor/src/toolbar/tools/attachment.tsx
index e37571ef1..420064e9b 100644
--- a/packages/editor/src/toolbar/tools/attachment.tsx
+++ b/packages/editor/src/toolbar/tools/attachment.tsx
@@ -27,11 +27,7 @@ import { Attachment } from "../../extensions/attachment";
export function AttachmentSettings(props: ToolProps) {
const { editor } = props;
const isBottom = useToolbarLocation() === "bottom";
- if (
- (!editor.isActive("attachment") && !editor.isActive("image")) ||
- !isBottom
- )
- return null;
+ if (!editor.isActive("attachment") || !isBottom) return null;
return (
<MoreTools
diff --git a/packages/editor/src/toolbar/tools/image.tsx b/packages/editor/src/toolbar/tools/image.tsx
index 7f1db764d..dcb0bff9d 100644
--- a/packages/editor/src/toolbar/tools/image.tsx
+++ b/packages/editor/src/toolbar/tools/image.tsx
@@ -24,11 +24,8 @@ import { ResponsivePresenter } from "../../components/responsive";
import { MoreTools } from "../components/more-tools";
import { useToolbarLocation } from "../stores/toolbar-store";
import { ImageProperties as ImagePropertiesPopup } from "../popups/image-properties";
-import {
- ImageAlignmentOptions,
- ImageSizeOptions
-} from "../../extensions/image";
import { findSelectedNode } from "../../utils/prosemirror";
+import { ImageAttributes } from "@/src/extensions/image";
export function ImageSettings(props: ToolProps) {
const { editor } = props;
@@ -43,20 +40,14 @@ export function ImageSettings(props: ToolProps) {
popupId="imageSettings"
tools={
editor.isEditable
- ? findSelectedNode(editor, "image")?.attrs?.float
- ? [
- "downloadAttachment",
- "imageAlignLeft",
- "imageAlignRight",
- "imageProperties"
- ]
- : [
- "downloadAttachment",
- "imageAlignLeft",
- "imageAlignCenter",
- "imageAlignRight",
- "imageProperties"
- ]
+ ? [
+ "downloadAttachment",
+ "imageAlignLeft",
+ "imageAlignCenter",
+ "imageAlignRight",
+ "imageFloat",
+ "imageProperties"
+ ]
: ["downloadAttachment"]
}
/>
@@ -65,10 +56,15 @@ export function ImageSettings(props: ToolProps) {
export function ImageAlignLeft(props: ToolProps) {
const { editor } = props;
+ const image = findSelectedNode(editor, "image");
+ if (!image) return null;
+
+ const { align } = image.attrs as ImageAttributes;
+
return (
<ToolButton
{...props}
- toggled={false}
+ toggled={!align || align === "left"}
onClick={() =>
editor.current
?.chain()
@@ -82,10 +78,15 @@ export function ImageAlignLeft(props: ToolProps) {
export function ImageAlignRight(props: ToolProps) {
const { editor } = props;
+ const image = findSelectedNode(editor, "image");
+ if (!image) return null;
+
+ const { align } = image.attrs as ImageAttributes;
+
return (
<ToolButton
{...props}
- toggled={false}
+ toggled={align === "right"}
onClick={() =>
editor.current
?.chain()
@@ -99,10 +100,15 @@ export function ImageAlignRight(props: ToolProps) {
export function ImageAlignCenter(props: ToolProps) {
const { editor } = props;
+ const image = findSelectedNode(editor, "image");
+ if (!image) return null;
+
+ const { align } = image.attrs as ImageAttributes;
+
return (
<ToolButton
{...props}
- toggled={false}
+ toggled={align === "center"}
onClick={() =>
editor.current
?.chain()
@@ -114,16 +120,33 @@ export function ImageAlignCenter(props: ToolProps) {
);
}
+export function ImageFloat(props: ToolProps) {
+ const { editor } = props;
+ const image = findSelectedNode(editor, "image");
+ if (!image) return null;
+
+ const { float } = image.attrs as ImageAttributes;
+
+ return (
+ <ToolButton
+ {...props}
+ toggled={!!float}
+ onClick={() =>
+ editor.current
+ ?.chain()
+ .focus()
+ .setImageAlignment({ float: !float })
+ .run()
+ }
+ />
+ );
+}
+
export function ImageProperties(props: ToolProps) {
const { editor } = props;
const [isOpen, setIsOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
- // TODO: defer until user opens the popup
- const image = findSelectedNode(editor, "image");
- const { float, align, width, height } = (image?.attrs ||
- {}) as ImageAlignmentOptions & ImageSizeOptions;
-
return (
<>
<ToolButton
@@ -150,10 +173,6 @@ export function ImageProperties(props: ToolProps) {
>
<ImagePropertiesPopup
editor={editor}
- height={height}
- width={width}
- align={align}
- float={float}
onClose={() => setIsOpen(false)}
/>
</ResponsivePresenter>
diff --git a/packages/editor/src/toolbar/tools/index.ts b/packages/editor/src/toolbar/tools/index.ts
index a0112c0af..a87fbf771 100644
--- a/packages/editor/src/toolbar/tools/index.ts
+++ b/packages/editor/src/toolbar/tools/index.ts
@@ -66,6 +66,7 @@ import {
ImageAlignCenter,
ImageAlignLeft,
ImageAlignRight,
+ ImageFloat,
ImageProperties
} from "./image";
import {
@@ -130,6 +131,7 @@ const tools = {
imageAlignLeft: ImageAlignLeft,
imageAlignRight: ImageAlignRight,
imageProperties: ImageProperties,
+ imageFloat: ImageFloat,
embedAlignCenter: EmbedAlignCenter,
embedAlignLeft: EmbedAlignLeft,