Skip to content

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

refactor: use same resizer component for image & embed

thecodrr5c68fe8cc764
diff --git a/packages/editor/src/components/resizer/index.tsx b/packages/editor/src/components/resizer/index.tsx
new file mode 100644
index 000000000..7e605c842
--- /dev/null
+++ b/packages/editor/src/components/resizer/index.tsx
@@ -0,0 +1,64 @@
+import { Resizable } from "re-resizable";
+import { PropsWithChildren } from "react";
+import { Icon, Icons } from "../../toolbar";
+import { Editor } from "../../types";
+
+type ResizerProps = {
+ editor: Editor;
+ selected: boolean;
+ width?: number;
+ height?: number;
+ onResize: (width: number, height: number) => void;
+};
+export function Resizer(props: PropsWithChildren<ResizerProps>) {
+ const { editor, selected, onResize, width, height, children } = props;
+
+ return (
+ <Resizable
+ enable={{
+ bottom: false,
+ left: false,
+ right: false,
+ top: false,
+ bottomLeft: false,
+ bottomRight: editor.isEditable && selected,
+ topLeft: false,
+ topRight: false,
+ }}
+ size={{
+ height: height || "auto",
+ width: width || "auto",
+ }}
+ maxWidth="100%"
+ minWidth={150}
+ minHeight={150}
+ handleComponent={{
+ bottomRight: (
+ <Icon
+ sx={{
+ width: 25,
+ height: 25,
+ marginLeft: -17,
+ marginTop: "3px",
+ borderTopLeftRadius: "default",
+ borderBottomRightRadius: "default",
+ }}
+ path={Icons.resize}
+ size={25}
+ color="primary"
+ />
+ ),
+ }}
+ onResizeStop={(e, direction, ref, d) => {
+ try {
+ onResize(ref.clientWidth, ref.clientHeight);
+ } catch {
+ // ignore
+ }
+ }}
+ lockAspectRatio={true}
+ >
+ {children}
+ </Resizable>
+ );
+}
diff --git a/packages/editor/src/extensions/embed/component.tsx b/packages/editor/src/extensions/embed/component.tsx
index 592d5b7ef..757e50840 100644
--- a/packages/editor/src/extensions/embed/component.tsx
+++ b/packages/editor/src/extensions/embed/component.tsx
@@ -1,11 +1,12 @@
import { Box, Flex } from "rebass";
import { Resizable } from "re-resizable";
-import { useRef } from "react";
+import { useRef, useState } from "react";
import { EmbedAlignmentOptions, EmbedAttributes } from "./embed";
import { SelectionBasedReactNodeViewProps } from "../react";
import { DesktopOnly } from "../../components/responsive";
import { ToolbarGroup } from "../../toolbar/components/toolbar-group";
import { Icon, Icons } from "../../toolbar";
+import { Resizer } from "../../components/resizer";
export function EmbedComponent(
props: SelectionBasedReactNodeViewProps<
@@ -13,6 +14,7 @@ export function EmbedComponent(
>
) {
const { editor, updateAttributes, selected, node } = props;
+ const [isLoading, setIsLoading] = useState(true);
const embedRef = useRef<HTMLIFrameElement>();
const { src, width, height, align } = node.attrs;
@@ -22,82 +24,51 @@ export function EmbedComponent(
sx={{
display: "flex",
justifyContent:
- align === "center" ? "center" : align === "left" ? "start" : "end"
+ align === "center" ? "center" : align === "left" ? "start" : "end",
+ position: "relative",
}}
>
- <Resizable
- enable={{
- bottom: false,
- left: false,
- right: false,
- top: false,
- bottomLeft: false,
- bottomRight: editor.isEditable && selected,
- topLeft: false,
- topRight: false
- }}
- size={{
- height: height || "auto",
- width: width || "auto"
- }}
- maxWidth="100%"
- handleComponent={{
- bottomRight: (
- <Icon
- sx={{
- width: 25,
- height: 25,
- marginLeft: -17,
- marginTop: "3px",
- borderTopLeftRadius: "default",
- borderBottomRightRadius: "default"
- }}
- path={Icons.resize}
- size={25}
- color="primary"
- />
- )
- }}
- onResizeStop={(e, direction, ref, d) => {
+ <Resizer
+ editor={editor}
+ selected={selected}
+ width={width}
+ height={height}
+ onResize={(width, height) => {
updateAttributes(
{
- width: ref.clientWidth,
- height: ref.clientHeight
+ width,
+ height,
},
{ addToHistory: true, preventUpdate: false }
);
}}
- lockAspectRatio={true}
>
- {/* <Flex sx={{ position: "relative", justifyContent: "end" }}>
-
- </Flex> */}
<Flex
width={"100%"}
sx={{
position: "relative",
justifyContent: "end",
- borderTop: editor.isEditable
- ? "20px solid var(--bgSecondary)"
- : "none",
+ p: "small",
+ bg: editor.isEditable ? "bgSecondary" : "transparent",
borderTopLeftRadius: "default",
borderTopRightRadius: "default",
borderColor: selected ? "border" : "bgSecondary",
cursor: "pointer",
":hover": {
- borderColor: "border"
- }
+ borderColor: "border",
+ },
}}
>
+ <Icon path={Icons.dragHandle} size={"big"} />
<DesktopOnly>
{selected && (
<Flex sx={{ position: "relative", justifyContent: "end" }}>
<Flex
sx={{
position: "absolute",
- top: -40,
+ top: -10,
mb: 2,
- alignItems: "end"
+ alignItems: "end",
}}
>
<ToolbarGroup
@@ -106,12 +77,12 @@ export function EmbedComponent(
"embedAlignLeft",
"embedAlignCenter",
"embedAlignRight",
- "embedProperties"
+ "embedProperties",
]}
sx={{
boxShadow: "menu",
borderRadius: "default",
- bg: "background"
+ bg: "background",
}}
/>
</Flex>
@@ -126,14 +97,30 @@ export function EmbedComponent(
width={"100%"}
height={"100%"}
sx={{
+ bg: "bgSecondary",
border: selected
? "2px solid var(--primary)"
: "2px solid transparent",
- borderRadius: "default"
+ borderRadius: "default",
}}
+ onLoad={() => setIsLoading(false)}
{...props}
/>
- </Resizable>
+ {isLoading && (
+ <Flex
+ sx={{
+ position: "absolute",
+ bottom: 0,
+ width: "100%",
+ height: "calc(100% - 20px)",
+ alignItems: "center",
+ justifyContent: "center",
+ }}
+ >
+ <Icon path={Icons.loading} rotate size={32} color="disabled" />
+ </Flex>
+ )}
+ </Resizer>
</Box>
</>
);
diff --git a/packages/editor/src/extensions/image/component.tsx b/packages/editor/src/extensions/image/component.tsx
index 7d925324d..e2e1a5c7a 100644
--- a/packages/editor/src/extensions/image/component.tsx
+++ b/packages/editor/src/extensions/image/component.tsx
@@ -8,6 +8,7 @@ import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
import { ToolbarGroup } from "../../toolbar/components/toolbar-group";
import { useIsMobile } from "../../toolbar/stores/toolbar-store";
+import { Resizer } from "../../components/resizer";
export function ImageComponent(
props: SelectionBasedReactNodeViewProps<
@@ -41,57 +42,24 @@ export function ImageComponent(
? "start"
: "end",
":hover .drag-handle, :active .drag-handle": {
- opacity: 1
- }
+ opacity: 1,
+ },
}}
>
- <Resizable
- enable={{
- bottom: false,
- left: false,
- right: false,
- top: false,
- bottomLeft: false,
- bottomRight: editor.isEditable && selected,
- topLeft: false,
- topRight: false
- }}
- handleComponent={{
- bottomRight: (
- <Icon
- sx={{
- width: 25,
- height: 25,
- marginLeft: -17,
- marginTop: -17,
- borderTopLeftRadius: "default",
- borderBottomRightRadius: "default"
- }}
- path={Icons.resize}
- size={25}
- color="primary"
- />
- )
- }}
- style={{
- position: "relative",
- float: float ? (align === "left" ? "left" : "right") : "none"
- }}
- size={{
- height: height || "auto",
- width: width || "auto"
- }}
- maxWidth="100%"
- onResizeStop={(e, direction, ref, d) => {
+ <Resizer
+ editor={editor}
+ selected={selected}
+ width={width}
+ height={height}
+ onResize={(width, height) => {
updateAttributes(
{
- width: ref.clientWidth,
- height: ref.clientHeight
+ width,
+ height,
},
{ addToHistory: true, preventUpdate: false }
);
}}
- lockAspectRatio={true}
>
<DesktopOnly>
{selected && (
@@ -101,7 +69,7 @@ export function ImageComponent(
position: "absolute",
top: -40,
mb: 2,
- alignItems: "end"
+ alignItems: "end",
}}
>
<ToolbarGroup
@@ -110,12 +78,12 @@ export function ImageComponent(
"imageAlignLeft",
"imageAlignCenter",
"imageAlignRight",
- "imageProperties"
+ "imageProperties",
]}
sx={{
boxShadow: "menu",
borderRadius: "default",
- bg: "background"
+ bg: "background",
}}
/>
</Flex>
@@ -133,7 +101,7 @@ export function ImageComponent(
position: "absolute",
top: 2,
left: 2,
- zIndex: 999
+ zIndex: 999,
}}
/>
)}
@@ -150,11 +118,11 @@ export function ImageComponent(
border: selected
? "2px solid var(--primary)"
: "2px solid transparent",
- borderRadius: "default"
+ borderRadius: "default",
}}
{...props}
/>
- </Resizable>
+ </Resizer>
</Box>
</>
);