-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
307 additions
and
68 deletions.
There are no files selected for viewing
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
141 changes: 141 additions & 0 deletions
141
packages/core/components/Puck/components/Canvas/index.tsx
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,141 @@ | ||
import { getBox } from "css-box-model"; | ||
import { | ||
ReactNode, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { useAppContext } from "../../context"; | ||
import { ViewportControls } from "../../../ViewportControls"; | ||
import styles from "../../styles.module.css"; | ||
import { getClassNameFactory } from "../../../../lib"; | ||
import { Preview } from "../Preview"; | ||
|
||
const getClassName = getClassNameFactory("Puck", styles); | ||
|
||
export const Canvas = () => { | ||
const { dispatch, state, overrides } = useAppContext(); | ||
const { ui } = state; | ||
const frameRef = useRef<HTMLDivElement>(null); | ||
|
||
const [rootHeight, setRootHeight] = useState(0); | ||
const [autoZoom, setAutoZoom] = useState(1); | ||
|
||
const defaultRender = useMemo< | ||
React.FunctionComponent<{ children?: ReactNode }> | ||
>(() => { | ||
const PuckDefault = ({ children }: { children?: ReactNode }) => ( | ||
<>{children}</> | ||
); | ||
|
||
return PuckDefault; | ||
}, []); | ||
|
||
const CustomPreview = useMemo( | ||
() => overrides.preview || defaultRender, | ||
[overrides] | ||
); | ||
|
||
// TODO deal with window resize | ||
const resetAutoZoom = useCallback(() => { | ||
if (frameRef.current) { | ||
const frame = frameRef.current; | ||
|
||
const box = getBox(frame); | ||
const frameWidth = box.contentBox.width; | ||
const frameHeight = box.contentBox.height; | ||
|
||
const viewportHeight = | ||
ui.viewport.height === "auto" ? frameHeight : ui.viewport.height; | ||
|
||
if (ui.viewport.width > frameWidth || viewportHeight > frameHeight) { | ||
const widthZoom = Math.min(frameWidth / ui.viewport.width, 1); | ||
const heightZoom = Math.min(frameHeight / viewportHeight, 1); | ||
|
||
let zoom = widthZoom; | ||
|
||
if (widthZoom < heightZoom) { | ||
setRootHeight(viewportHeight / zoom); | ||
} else { | ||
setRootHeight(viewportHeight); | ||
zoom = heightZoom; | ||
} | ||
|
||
setAutoZoom(zoom); | ||
|
||
dispatch({ | ||
type: "setUi", | ||
ui: { | ||
...ui, | ||
viewport: { | ||
...ui.viewport, | ||
zoom, | ||
}, | ||
}, | ||
}); | ||
} else { | ||
setAutoZoom(1); | ||
|
||
if (ui.viewport.zoom === autoZoom) { | ||
setRootHeight(viewportHeight); | ||
|
||
dispatch({ | ||
type: "setUi", | ||
ui: { | ||
...ui, | ||
viewport: { | ||
...ui.viewport, | ||
zoom: 1, | ||
}, | ||
}, | ||
}); | ||
} else { | ||
setRootHeight(viewportHeight / ui.viewport.zoom); | ||
} | ||
} | ||
} | ||
}, [frameRef, ui.leftSideBarVisible, ui, autoZoom]); | ||
|
||
// Auto zoom | ||
useEffect(() => { | ||
resetAutoZoom(); | ||
}, [ | ||
frameRef, | ||
ui.leftSideBarVisible, | ||
ui.rightSideBarVisible, | ||
ui.viewport.width, | ||
]); | ||
|
||
return ( | ||
<div | ||
className={getClassName("canvas")} | ||
onClick={() => | ||
dispatch({ | ||
type: "setUi", | ||
ui: { itemSelector: null }, | ||
recordHistory: true, | ||
}) | ||
} | ||
> | ||
<div className={getClassName("canvasControls")}> | ||
<ViewportControls autoZoom={autoZoom} /> | ||
</div> | ||
<div className={getClassName("frame")} ref={frameRef}> | ||
<div | ||
className={getClassName("root")} | ||
style={{ | ||
width: ui.viewport.width, | ||
height: rootHeight, | ||
transform: `scale(${ui.viewport.zoom})`, | ||
}} | ||
> | ||
<CustomPreview> | ||
<Preview /> | ||
</CustomPreview> | ||
</div> | ||
</div> | ||
</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
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.