+ {content}
+ {renderControls()}
+ {target === "panel" && }
+
+ )*/
+}
+
+export { ExtraContentItem }
\ No newline at end of file
diff --git a/src/components/ExtraContent/index.js b/src/components/ExtraContent/index.js
index 3ea62fb77..b9a4cfc24 100644
--- a/src/components/ExtraContent/index.js
+++ b/src/components/ExtraContent/index.js
@@ -17,12 +17,10 @@
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-import { Fragment, h } from "preact"
-import { useRef, useEffect, useState } from "preact/hooks"
-import { espHttpURL } from "../Helpers"
-import { useUiContextFn } from "../../contexts"
-import { useHttpFn } from "../../hooks"
-import { Play, Pause, Aperture, RefreshCcw } from "preact-feather"
+import { h, Fragment } from "preact"
+import { useRef, useEffect, useCallback, useState } from "preact/hooks"
+import { elementsCache } from "../../areas/elementsCache"
+import { ExtraContentItem } from "./extraContentItem"
import {
ButtonImg,
FullScreenButton,
@@ -30,361 +28,145 @@ import {
ContainerHelper,
} from "../Controls"
import { T } from "../Translations"
+import { RefreshCcw } from "preact-feather"
import { iconsFeather } from "../Images"
import { iconsTarget } from "../../targets"
+import { useUiContextFn } from "../../contexts"
-const contentCache = {}
-const refreshPausedList = {}
-const timerIDs = {}
-
-const ExtraContent = ({
- id,
- source,
- refreshtime,
- label,
- type,
- target,
- icon,
-}) => {
- const { createNewRequest } = useHttpFn
- const [refreshPaused, setRefreshPaused] = useState(refreshPausedList[id])
+const ExtraContent = ({ id, source, refreshtime, label, type, target, icon }) => {
+ const containerRef = useRef(null)
const [isFullScreen, setIsFullScreen] = useState(false)
- const element = useRef(null)
- const imageCache = useRef(null)
- const panelRef = useRef(null)
- const pageSource = type == "camera" ? "/snap" : source
+ const extra_content_id = `extra_content_${id}`
+ const target_id = `target_${id}`
const iconsList = { ...iconsTarget, ...iconsFeather }
+ console.log(id)
+ const updateContentPosition = () => {
+ if (containerRef.current) {
+ const { top, left, width, height } = containerRef.current.getBoundingClientRect()
+ //console.log("New Position for element " + extra_content_id + ":", top, left, width, height)
+ elementsCache.updatePosition(extra_content_id, { top, left, width, height })
+ } else {
+ //console.log("Element " + extra_content_id + " doesn't exist")
+ }
+ }
+ updateContentPosition()
useEffect(() => {
- const handleFullScreenChange = () => {
- setIsFullScreen(document.fullscreenElement !== null)
+ if (!elementsCache.has(extra_content_id)) {
+ console.log("Creating element " + extra_content_id," because it doesn't exist")
+ elementsCache.create(extra_content_id, {
+ id,
+ source,
+ type,
+ label,
+ target,
+ refreshtime,
+ icon,
+ isVisible: true
+ })
+ } else {
+ console.log("Updating element " + extra_content_id + " because it already exists")
+ elementsCache.updateState(extra_content_id, { isVisible: true})
}
- document.addEventListener("fullscreenchange", handleFullScreenChange)
+ updateContentPosition()
+
+ const handleScrollAndResize = () => requestAnimationFrame(updateContentPosition)
+
+ const main = document.getElementById("main")
+ if (main) {
+ main.addEventListener('scroll', handleScrollAndResize)
+ main.addEventListener('resize', handleScrollAndResize)
+ }
+ window.addEventListener('resize', handleScrollAndResize)
+
return () => {
- document.removeEventListener(
- "fullscreenchange",
- handleFullScreenChange
- )
+ const main = document.getElementById("main")
+ if (main) {
+ main.removeEventListener('scroll', handleScrollAndResize)
+ main.removeEventListener('resize', handleScrollAndResize)
+ }
+ window.removeEventListener('resize', handleScrollAndResize)
+ console.log("Hiding element " + extra_content_id)
+ //elementsCache.updateState(extra_content_id, { isVisible: false })
}
}, [])
- const loadContent = (init = false) => {
- if (!init && refreshPausedList[id]) {
- return
- }
- if (pageSource.startsWith("http")) {
- switch (type) {
- case "image":
- if (element.current) element.current.src = pageSource
- break
- default:
- if (element.current) element.current.src = pageSource
- }
- } else {
- if (
- (type == "image" || type == "extension") &&
- contentCache[id] != undefined
- ) {
- element.current.classList.remove("d-block")
- element.current.classList.add("d-none")
- element.current.onload = () => {
- URL.revokeObjectURL(element.current.src)
- if (type == "extension") {
- const doc = element.current.contentWindow.document
- const body = doc.querySelector("body")
- body.classList.add("body-extension")
- const css = document.querySelectorAll("style")
- //inject css
- css.forEach((csstag) => {
- doc.head.appendChild(csstag.cloneNode(true))
- }) //to avoid the flickering when apply css
- }
- element.current.classList.remove("d-none")
- element.current.classList.add("d-block")
- }
- element.current.src = URL.createObjectURL(contentCache[id])
- } else {
- const idquery = type == "content" ? type + id : "download" + id
- createNewRequest(
- espHttpURL(pageSource),
- { method: "GET", id: idquery, max: 2 },
- {
- onSuccess: (result) => {
- switch (type) {
- case "camera":
- case "image":
- if (element.current) {
- imageCache.current = result
- element.current.onload = () => {
- URL.revokeObjectURL(
- element.current.src
- )
- }
- contentCache[id] = result
- element.current.src =
- URL.createObjectURL(
- contentCache[id]
- )
- }
- break
- //cannot be used because this way disable javascript in iframe
- case "extension":
- element.current.onload = () => {
- URL.revokeObjectURL(element.current.src)
- const doc =
- element.current.contentWindow
- .document
- const body = doc.querySelector("body")
- body.classList.add("body-extension")
- const css =
- document.querySelectorAll("style")
- //inject css
- css.forEach((csstag) => {
- doc.head.appendChild(
- csstag.cloneNode(true)
- )
- })
- //to avoid the flickering when apply css
- element.current.classList.remove(
- "d-none"
- )
- element.current.classList.add("d-block")
- }
- contentCache[id] = result
- element.current.src = URL.createObjectURL(
- contentCache[id]
- )
- //todo inject css
- break
- default:
- if (
- element.current &&
- element.current.contentWindow
- )
- element.current.contentWindow.document.body.innerHTML =
- result
- }
- },
- onFail: (error) => {
- const errorContent =
- "
-
-
-
+
+
+ {renderControls()}
)
- if (target == "panel") {
- const displayIcon = iconsList[icon] ? iconsList[icon] : ""
- //console.log("Panel :", id, "Ref :", panelRef.current)
+ }
+
+ if (target === "panel") {
+ // console.log("Rendering panel element " + extra_content_id)
+ const displayIcon = iconsList[icon] || ""
return (
-
-
-
-
- {displayIcon}
- {T(label)}
-
-
- {refreshtime == 0 && (
- }
- onclick={() => {
- useUiContextFn.haptic()
- if (contentCache[id])
- contentCache[id] = undefined
- loadContent()
- }}
- />
- )}
-
-
- {
- clearInterval(timerIDs[id])
- }}
- />
+
+
+
+
+
+ {displayIcon}
+ {T(label)}
+
+
+ {renderControls()}
-
-
-
-
-
- {parseInt(refreshtime) > 0 && type != "extension" && (
-
- )}
-
+
+ {/* Host content */}
+
+ {parseInt(refreshtime) > 0 && type !== "extension" && (
+
+ )}
+
+
+
)
}
}
-export default ExtraContent
+export { ExtraContent, ExtraContentItem }
\ No newline at end of file
diff --git a/src/components/Panels/ExtraPanel.js b/src/components/Panels/ExtraPanel.js
index 6449679bd..d07a71254 100644
--- a/src/components/Panels/ExtraPanel.js
+++ b/src/components/Panels/ExtraPanel.js
@@ -17,7 +17,7 @@ Files.js - ESP3D WebUI component file
*/
import { h } from "preact"
-import ExtraContent from "../ExtraContent"
+import {ExtraContent} from "../ExtraContent"
/*
* Local const
diff --git a/src/pages/extrapages/index.js b/src/pages/extrapages/index.js
index 06973735a..f6b5c3ffc 100644
--- a/src/pages/extrapages/index.js
+++ b/src/pages/extrapages/index.js
@@ -18,7 +18,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import { h } from "preact"
-import ExtraContent from "../../components/ExtraContent"
+import {ExtraContent} from "../../components/ExtraContent"
const ExtraPage = ({ id, source, refreshtime, label, type }) => {
return (
diff --git a/src/style/components/_app.scss b/src/style/components/_app.scss
index a94a0115c..bc4de2b22 100644
--- a/src/style/components/_app.scss
+++ b/src/style/components/_app.scss
@@ -109,3 +109,19 @@ body {
.modal-container {
max-height: 95vh;
}
+
+.page_extra {
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+}
+.extra-content-container {
+ height: 100%;
+ width: 100%;
+ position:absolute;
+ background-color: red!important;
+ color: white!important;
+ border: 1px solid red!important;
+ z-index: 99999;
+ opacity: 1;
+}
\ No newline at end of file