-
-
Notifications
You must be signed in to change notification settings - Fork 685
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): add resize layout & function page prompt (#856)
- Loading branch information
Showing
21 changed files
with
395 additions
and
55 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,4 @@ | |
"prettier --write" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useEffect } from "react"; | ||
import { ChevronLeftIcon, ChevronRightIcon } from "@chakra-ui/icons"; | ||
import { Center } from "@chakra-ui/react"; | ||
import clsx from "clsx"; | ||
|
||
import useResizable from "@/hooks/useResizable"; | ||
import { page, panel } from "@/pages/customSetting"; | ||
import useCustomSettingStore from "@/pages/customSetting"; | ||
export default function Resize(props: { | ||
type: "x" | "y"; | ||
reverse?: boolean; | ||
pageId: page; | ||
panelId: panel; | ||
containerRef: any; | ||
}) { | ||
const { type, pageId, panelId, reverse, containerRef } = props; | ||
const store = useCustomSettingStore(); | ||
const { width, height, minWidth, maxWidth, minHeight, maxHeight, display } = store.getLayoutInfo( | ||
pageId, | ||
panelId, | ||
); | ||
const { isDragging, position, separatorProps } = useResizable({ | ||
axis: type, | ||
initial: type === "x" ? width : height, | ||
min: type === "x" ? minWidth : minHeight, | ||
max: type === "x" ? maxWidth : maxHeight, | ||
reverse, | ||
containerRef, | ||
}); | ||
useEffect(() => { | ||
const newPosition = { | ||
width: position, | ||
height: position, | ||
}; | ||
store.setLayoutInfo(pageId, panelId, newPosition); | ||
}, [position, pageId, panelId, store]); | ||
|
||
return ( | ||
<> | ||
{display === "none" ? null : ( | ||
<div | ||
className={ | ||
type === "x" ? "h-full w-2 cursor-col-resize group" : "h-2 w-full cursor-row-resize" | ||
} | ||
{...separatorProps} | ||
> | ||
<Center className="w-full h-full"> | ||
{type === "x" && width <= 20 ? ( | ||
<div | ||
className={clsx( | ||
reverse ? "rounded-l-lg" : "rounded-r-lg", | ||
"w-2 h-[30px] bg-grayIron-300 group-hover:bg-grayIron-400 transition-colors leading-loose text-lafWhite-600", | ||
)} | ||
> | ||
{reverse ? <ChevronLeftIcon fontSize={10} /> : <ChevronRightIcon fontSize={10} />} | ||
</div> | ||
) : ( | ||
<div | ||
className={clsx( | ||
type === "x" && isDragging ? "h-full " : "h-0", | ||
type === "y" && isDragging ? "w-full " : "w-0", | ||
isDragging ? " border-primary-400 " : "", | ||
"border-l-2 border-t-2 transition-all", | ||
)} | ||
></div> | ||
)} | ||
</Center> | ||
</div> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import type React from "react"; | ||
import { useCallback, useMemo, useRef, useState } from "react"; | ||
|
||
type SeparatorProps = React.ComponentPropsWithoutRef<"hr">; | ||
type Resizable = { | ||
position: number; | ||
isDragging: boolean; | ||
separatorProps: SeparatorProps; | ||
}; | ||
type UseResizableProps = { | ||
axis: "x" | "y"; | ||
containerRef?: React.RefObject<HTMLElement>; | ||
disabled?: boolean; | ||
initial?: number; | ||
min?: number; | ||
max?: number; | ||
reverse?: boolean; | ||
}; | ||
|
||
export type ResizableProps = UseResizableProps & { | ||
children: (props: Resizable) => JSX.Element; | ||
}; | ||
|
||
const useResizable = ({ | ||
axis, | ||
disabled = false, | ||
initial = 0, | ||
min = 0, | ||
max = Infinity, | ||
reverse, | ||
containerRef, | ||
}: UseResizableProps): Resizable => { | ||
const isResizing = useRef(false); | ||
const [isDragging, setIsDragging] = useState(false); | ||
const [position, setPosition] = useState(Math.min(Math.max(initial, min), max)); | ||
|
||
const ariaProps = useMemo<SeparatorProps>( | ||
() => ({ | ||
role: "separator", | ||
"aria-valuenow": position, | ||
"aria-valuemin": min, | ||
"aria-valuemax": max, | ||
"aria-orientation": axis === "x" ? "vertical" : "horizontal", | ||
"aria-disabled": disabled, | ||
}), | ||
[axis, disabled, max, min, position], | ||
); | ||
|
||
const handleMove = useCallback( | ||
(e: MouseEvent | TouchEvent) => { | ||
if (!isResizing.current) return; | ||
if (disabled) return; | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
const currentPosition = (() => { | ||
if (axis === "x") { | ||
const x = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX; | ||
if (containerRef?.current) { | ||
const containerNode = containerRef.current; | ||
const { left, width } = containerNode.getBoundingClientRect(); | ||
return reverse ? left + width - x : x - left; | ||
} | ||
return reverse ? document.body.offsetWidth - x : x; | ||
} | ||
const y = e instanceof MouseEvent ? e.clientY : e.touches[0].clientY; | ||
if (containerRef?.current) { | ||
const containerNode = containerRef.current; | ||
const { top, height } = containerNode.getBoundingClientRect(); | ||
return reverse ? top + height - y : y - top; | ||
} | ||
return reverse ? document.body.offsetHeight - y : y; | ||
})(); | ||
if (min < currentPosition && currentPosition < max) { | ||
setPosition(currentPosition); | ||
} | ||
}, | ||
[axis, disabled, max, min, reverse, containerRef], | ||
); | ||
|
||
const handleUp = useCallback( | ||
(e: MouseEvent | TouchEvent) => { | ||
if (disabled) return; | ||
e.stopPropagation(); | ||
isResizing.current = false; | ||
setIsDragging(false); | ||
document.removeEventListener("mousemove", handleMove); | ||
document.removeEventListener("touchmove", handleMove); | ||
document.removeEventListener("mouseup", handleUp); | ||
document.removeEventListener("touchend", handleUp); | ||
}, | ||
[disabled, handleMove], | ||
); | ||
|
||
const handleDown = useCallback<any>( | ||
(e: React.MouseEvent | React.TouchEvent) => { | ||
if (disabled) return; | ||
e.stopPropagation(); | ||
isResizing.current = true; | ||
setIsDragging(true); | ||
document.addEventListener("mousemove", handleMove); | ||
document.addEventListener("touchmove", handleMove); | ||
document.addEventListener("mouseup", handleUp); | ||
document.addEventListener("touchend", handleUp); | ||
}, | ||
[disabled, handleMove, handleUp], | ||
); | ||
return { | ||
position, | ||
isDragging, | ||
separatorProps: { | ||
...ariaProps, | ||
onMouseDown: handleDown, | ||
onTouchStart: handleDown, | ||
}, | ||
}; | ||
}; | ||
|
||
export default useResizable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.