Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

cleanup & fix types

Roman Pominovfc8228afa3a5
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.stories.tsx
deleted file mode 100644
index 826e73848c..0000000000
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.stories.tsx
+++ /dev/null
@@ -1,108 +0,0 @@
-import { ComponentStory, ComponentMeta } from "@storybook/react";
-import { Canvas, Item } from "./dnd";
-
-export default {
- component: Canvas,
-} as ComponentMeta<typeof Canvas>;
-
-const tree = {
- id: "root",
- type: "div",
- style: {},
- content: [
- {
- id: "1",
- type: "ul",
- style: {},
- content: [
- { id: "1.1", type: "li", content: "Foo" },
- { id: "1.2", type: "li", content: "Bar" },
- {
- id: "1.3",
- type: "li",
- content: [
- {
- id: "1.3.1",
- type: "div",
- style: { background: "red" },
- content: "Baz",
- },
- ],
- },
- ],
- },
- {
- id: "2",
- type: "div",
- style: { display: "flex" },
- content: [
- {
- id: "2.1",
- type: "div",
- style: { width: "100px", height: "100px", background: "red" },
- content: [],
- },
- {
- id: "2.2",
- type: "div",
- style: { width: "100px", height: "100px", background: "green" },
- content: [],
- },
- {
- id: "2.3",
- type: "div",
- style: { width: "100px", height: "100px", background: "blue" },
- content: [],
- },
- ],
- },
- {
- id: "3",
- type: "div",
- style: {},
- content: [
- {
- id: "3.1",
- type: "div",
- style: { height: "100px", background: "red" },
- content: [],
- },
- {
- id: "3.2",
- type: "div",
- style: { display: "flex", padding: "20px", background: "green" },
- content: [
- {
- id: "3.2.1",
- type: "div",
- style: { width: "100px", height: "100px", background: "red" },
- content: [],
- },
- {
- id: "3.2.2",
- type: "div",
- style: { width: "100px", height: "100px", background: "yellow" },
- content: [],
- },
- {
- id: "3.2.3",
- type: "div",
- style: { width: "100px", height: "100px", background: "blue" },
- content: [],
- },
- ],
- },
- {
- id: "3.3",
- type: "div",
- style: { height: "100px", background: "blue" },
- content: [],
- },
- ],
- },
- ],
-} as Item;
-
-export const Main: ComponentStory<typeof Canvas> = () => {
- return <Canvas data={tree} />;
-};
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.tsx
deleted file mode 100644
index cd20000b86..0000000000
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/dnd.tsx
+++ /dev/null
@@ -1,201 +0,0 @@
-import { CSSProperties, useRef, useState } from "react";
-
-type componentType = "div" | "p" | "ul" | "li" | "span";
-
-export type Item = {
- id: string;
- type: componentType;
- style: CSSProperties;
- content: Item[] | string;
-};
-
-type CanvasProps = {
- data: Item;
-};
-
-const CanvasNode = ({ item }: { item: Item }) => {
- const children =
- typeof item.content === "string"
- ? item.content
- : item.content.map((child) => <CanvasNode item={child} key={child.id} />);
-
- switch (item.type) {
- case "div":
- return (
- <div style={item.style} data-id={item.id}>
- {children}
- </div>
- );
- case "p":
- return (
- <p style={item.style} data-id={item.id}>
- {children}
- </p>
- );
- case "ul":
- return (
- <ul style={item.style} data-id={item.id}>
- {children}
- </ul>
- );
- case "li":
- return (
- <li style={item.style} data-id={item.id}>
- <div>{children}</div>
- </li>
- );
- case "span":
- return (
- <span style={item.style} data-id={item.id}>
- {children}
- </span>
- );
- }
-};
-
-const canInsert = (item: Item, destanation: Item) => {
- if (destanation.type === "ul") {
- return item.type === "li";
- }
-
- if (destanation.type === "p") {
- return false;
- }
-
- return typeof destanation.content !== "string";
-};
-
-const findId = (
- domNode: EventTarget,
- root: HTMLElement | null
-): string | null => {
- if (!(domNode instanceof HTMLElement)) {
- return null;
- }
-
- if (domNode.dataset.id != null) {
- return domNode.dataset.id;
- }
-
- let paretn = domNode.parentElement;
-
- if (paretn === root || paretn === null) {
- return null;
- }
-
- return findId(paretn, root);
-};
-
-const findElement = (
- root: HTMLElement | null,
- id: string
-): HTMLElement | null => {
- if (root === null) {
- return null;
- }
-
- if (root.dataset.id === id) {
- return root;
- }
-
- const children = [...root.children];
-
- for (const child of children) {
- const result = findElement(child as HTMLElement, id);
-
- if (result != null) {
- return result;
- }
- }
-
- return null;
-};
-
-type InsertLocation =
- | { type: "vertical"; minX: number; maxX: number; y: number }
- | { type: "horizontal"; minY: number; maxY: number; x: number };
-
-export const Canvas = ({ data }: CanvasProps) => {
- let rootRef = useRef(null);
-
- let [moveState, setMoveState] = useState<{
- initialPointerPosition: { x: number; y: number };
- initialRect: DOMRect;
- id: string;
- } | null>(null);
-
- let insertLocation: InsertLocation | null = {
- type: "vertical",
- minX: 0,
- maxX: 100,
- y: 0,
- } as InsertLocation;
-
- return (
- <div
- ref={rootRef}
- style={{ position: "relative" }}
- onPointerDown={(ev) => {
- const id = findId(ev.target, rootRef.current);
-
- if (id == null) {
- return;
- }
-
- const element = findElement(rootRef.current, id);
-
- if (element == null) {
- return;
- }
-
- setMoveState({
- id,
- initialRect: element.getBoundingClientRect(),
- initialPointerPosition: { x: ev.clientX, y: ev.clientY },
- });
- }}
- onPointerMove={(ev) => {
- if (moveState != null) {
- console.log(findId(ev.target, rootRef.current));
- }
- }}
- onPointerUp={(ev) => setMoveState(null)}
- >
- <CanvasNode item={data} />
- {insertLocation !== null && (
- <div
- style={{
- position: "absolute",
- left: "0",
- top: "0",
- width: "100%",
- height: "100%",
- pointerEvents: "none",
- }}
- >
- <div
- style={
- insertLocation.type === "vertical"
- ? {
- position: "absolute",
- top: insertLocation.y,
- left: insertLocation.minX,
- width: insertLocation.maxX - insertLocation.minX,
- height: "1px",
- backgroundColor: "lightblue",
- }
- : {
- position: "absolute",
- left: insertLocation.x,
- top: insertLocation.minY,
- height: insertLocation.maxY - insertLocation.minY,
- width: "1px",
- backgroundColor: "lightblue",
- }
- }
- />
- </div>
- )}
- </div>
- );
-};
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
index 4f8eb252d5..3c82ad1ef5 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
@@ -1,5 +1,5 @@
-import { ComponentStory, ComponentMeta } from "@storybook/react";
-import { useEffect, useRef, useState } from "react";
+import { ComponentMeta } from "@storybook/react";
+import { useRef, useState } from "react";
import { Box } from "../../box";
import { useDropTarget, type Area } from "./use-drop-target";
import { useDrag } from "./use-drag";
@@ -42,6 +42,7 @@ export const Playground = () => {
const dragProps = useDrag({
onStart(event: any) {
+ // FIXME: can't have target here, won't work with iframe
if (event.target.dataset.draggable === "false") {
event.cancel();
return;
@@ -55,7 +56,7 @@ export const Playground = () => {
setPlacementIndicatorRect(undefined);
handleHoldEnd();
},
- onShiftChange({ shifts, target }: any) {
+ onShiftChange({ shifts }: any) {
console.log("shifts", shifts);
if (dragItemRef.current) {
dragItemRef.current.textContent = `shifted ${shifts}`;
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts
index fbd00b36a4..655b52e981 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drag.ts
@@ -3,26 +3,15 @@ import { useRef } from "react";
type State =
| {
- status: "idle";
+ status: "idle" | "canceled";
}
| {
- status: "pending";
+ status: "pending" | "dragging";
x: number;
y: number;
initialX: number;
initialY: number;
shifts: number;
- }
- | {
- status: "dragging";
- x: number;
- y: number;
- initialX: number;
- initialY: number;
- shifts: number;
- }
- | {
- status: "canceled";
};
const initialState = {
@@ -79,15 +68,7 @@ export const useDrag = ({
};
onStart({ target, cancel });
},
- onMove({
- pageX: x,
- pageY: y,
- target,
- }: {
- pageX: number;
- pageY: number;
- target: HTMLElement;
- }) {
+ onMove({ pageX: x, pageY: y }: { pageX: number; pageY: number }) {
if (state.current.status === "canceled") {
return;
}
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts
index e173ecd146..66d9e83bcb 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-move.ts
@@ -14,7 +14,6 @@
* We had to copy this file from react-aria because we need pageX and pageY.
*/
-import type { MoveEvents, PointerType } from "@react-types/shared";
import React, { HTMLAttributes, useMemo, useRef } from "react";
import { useGlobalListeners } from "@react-aria/utils";
@@ -30,12 +29,60 @@ interface EventBase {
altKey: boolean;
}
+export type PointerType = "mouse" | "pen" | "touch" | "keyboard" | "virtual";
+
+interface BaseMoveEvent {
+ /** The pointer type that triggered the move event. */
+ pointerType: PointerType;
+ /** Whether the shift keyboard modifier was held during the move event. */
+ shiftKey: boolean;
+ /** Whether the ctrl keyboard modifier was held during the move event. */
+ ctrlKey: boolean;
+ /** Whether the meta keyboard modifier was held during the move event. */
+ metaKey: boolean;
+ /** Whether the alt keyboard modifier was held during the move event. */
+ altKey: boolean;
+}
+
+interface MoveStartEvent extends BaseMoveEvent {
+ /** The type of move event being fired. */
+ type: "movestart";
+ target: HTMLElement;
+ pageX: number;
+ pageY: number;
+}
+
+interface MoveMoveEvent extends BaseMoveEvent {
+ /** The type of move event being fired. */
+ type: "move";
+ /** The amount moved in the X direction since the last event. */
+ deltaX: number;
+ /** The amount moved in the Y direction since the last event. */
+ deltaY: number;
+ pageX: number;
+ pageY: number;
+}
+
+interface MoveEndEvent extends BaseMoveEvent {
+ /** The type of move event being fired. */
+ type: "moveend";
+}
+
+interface MoveEvents {
+ /** Handler that is called when a move interaction starts. */
+ onMoveStart?: (e: MoveStartEvent) => void;
+ /** Handler that is called when the element is moved. */
+ onMove?: (e: MoveMoveEvent) => void;
+ /** Handler that is called when a move interaction ends. */
+ onMoveEnd?: (e: MoveEndEvent) => void;
+}
+
/**
* Handles move interactions across mouse, touch, and keyboard, including dragging with
* the mouse or touch, and using the arrow keys. Normalizes behavior across browsers and
* platforms, and ignores emulated mouse events on touch devices.
*/
-export function useMove(props: /*MoveEvents*/ any): MoveResult {
+export function useMove(props: MoveEvents): MoveResult {
let { onMoveStart, onMove, onMoveEnd } = props;
let state = useRef<{
@@ -54,7 +101,7 @@ export function useMove(props: /*MoveEvents*/ any): MoveResult {
state.current.didMove = false;
};
let move = (
- originalEvent: any /*EventBase*/,
+ originalEvent: MouseEvent | TouchEvent | React.KeyboardEvent,
pointerType: PointerType,
deltaX: number,
deltaY: number
@@ -63,6 +110,15 @@ export function useMove(props: /*MoveEvents*/ any): MoveResult {
return;
}
+ // FIXME: this is a temporary fix
+ // we need to figure out how to properly support keyboard and touch
+ if (
+ !(originalEvent.target instanceof HTMLElement) ||
+ !("pageX" in originalEvent)
+ ) {
+ return;
+ }
+
if (!state.current.didMove) {
state.current.didMove = true;
onMoveStart?.({
@@ -77,6 +133,7 @@ export function useMove(props: /*MoveEvents*/ any): MoveResult {
pageY: originalEvent.pageY,
});
}
+
onMove?.({
type: "move",
pointerType,
@@ -86,7 +143,6 @@ export function useMove(props: /*MoveEvents*/ any): MoveResult {
metaKey: originalEvent.metaKey,
ctrlKey: originalEvent.ctrlKey,
altKey: originalEvent.altKey,
- target: originalEvent.target,
pageX: originalEvent.pageX,
pageY: originalEvent.pageY,
});
@@ -224,7 +280,7 @@ export function useMove(props: /*MoveEvents*/ any): MoveResult {
}
let triggerKeyboardMove = (
- e: EventBase,
+ e: React.KeyboardEvent,
deltaX: number,
deltaY: number
) => {
diff --git a/apps/designer/package.json b/apps/designer/package.json
index 98dfd85189..6e9ff0cf55 100644
--- a/apps/designer/package.json
+++ b/apps/designer/package.json
@@ -94,7 +94,6 @@
},
"devDependencies": {
"@babel/core": "^7.18.5",
- "@react-types/shared": "^3.13.1",
"@remix-run/dev": "^1.6.4",
"@storybook/addon-actions": "^6.5.9",
"@storybook/addon-essentials": "^6.5.9",