-
Notifications
You must be signed in to change notification settings - Fork 3
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
3 changed files
with
99 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect } from "react"; | ||
import useStudioStore from "../store/useStudioStore"; | ||
|
||
export default function useInjectUserStyles(iframeDocument: Document | undefined) { | ||
const [ | ||
componentTree, | ||
getComponentMetadata, | ||
activePage, | ||
UUIDToFileMetadata, | ||
pageCss, | ||
] = useStudioStore((store) => [ | ||
store.actions.getComponentTree(), | ||
store.actions.getComponentMetadata, | ||
store.pages.activePageName, | ||
store.fileMetadatas.UUIDToFileMetadata, | ||
store.pages.getActivePageState()?.cssImports, | ||
]); | ||
useEffect(() => { | ||
if (!iframeDocument) { | ||
return; | ||
} | ||
componentTree?.forEach((component) => { | ||
const cssImports = getComponentMetadata(component)?.cssImports; | ||
cssImports?.forEach((cssFilepath) => { | ||
injectStyleIntoIframe(iframeDocument, cssFilepath); | ||
}); | ||
}); | ||
pageCss?.forEach((cssFilepath) => { | ||
injectStyleIntoIframe(iframeDocument, cssFilepath); | ||
}); | ||
|
||
return () => { | ||
clearStylingFromIframe(iframeDocument); | ||
}; | ||
}, [ | ||
componentTree, | ||
getComponentMetadata, | ||
iframeDocument, | ||
pageCss, | ||
activePage, | ||
UUIDToFileMetadata, | ||
]); | ||
} | ||
|
||
function clearStylingFromIframe(iframeDocument: Document) { | ||
const styleElements = [...iframeDocument.head.getElementsByTagName("style")]; | ||
styleElements.forEach((element) => { | ||
element.remove(); | ||
}); | ||
} | ||
|
||
function injectStyleIntoIframe( | ||
iframeDocument: Document, | ||
filepath: string | ||
) { | ||
const originalStyletag = document.querySelector( | ||
`[studio-style-filepath='${filepath}']` | ||
); | ||
if (originalStyletag) { | ||
const iframeStyletag = originalStyletag.cloneNode(true); | ||
iframeDocument.head.appendChild(iframeStyletag); | ||
} | ||
} |
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,34 @@ | ||
import { RefObject, useEffect, useState } from "react"; | ||
import { Viewport } from "../components/Viewport/defaults"; | ||
|
||
export default function useViewportOption ( | ||
viewport: Viewport, | ||
previewRef: RefObject<HTMLDivElement> | ||
) { | ||
const [css, setCss] = useState( | ||
(viewport.styles?.width ?? 0) * (previewRef.current?.clientHeight ?? 0) > | ||
(viewport.styles?.height ?? 0) * (previewRef.current?.clientWidth ?? 0) | ||
? " w-full " | ||
: " h-full " | ||
); | ||
const handleResize = () => { | ||
const tempCss = | ||
(viewport.styles?.width ?? 0) * (previewRef.current?.clientHeight ?? 0) > | ||
(viewport.styles?.height ?? 0) * (previewRef.current?.clientWidth ?? 0) | ||
? " w-full " | ||
: " h-full "; | ||
if (tempCss !== css) { | ||
setCss(tempCss); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const resizeObserver = new ResizeObserver(handleResize); | ||
if (previewRef.current) { | ||
resizeObserver.observe(previewRef.current); | ||
} | ||
return () => resizeObserver.disconnect(); | ||
}); | ||
|
||
return css; | ||
}; |