fix iframe scrolling in edit

This commit is contained in:
Tomas Mirchev 2025-06-30 17:21:50 +00:00
parent 2e4549ebd7
commit 0bf9588acb

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect, useMemo } from "react"; import React, { useRef, useState, useEffect, useMemo } from "react";
import { marked } from "marked"; import { marked } from "marked";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
import { useStore } from "./store.js"; import { useStore } from "./store.js";
@ -111,6 +111,9 @@ function useFileContent(resource) {
} }
const ResourcePage = ({ topics }) => { const ResourcePage = ({ topics }) => {
// Iframe
const iframeRef = useRef(null);
// Config + Token // Config + Token
const authToken = useStore((state) => state.config.token); const authToken = useStore((state) => state.config.token);
const changeConfig = useStore((state) => state.changeConfig); const changeConfig = useStore((state) => state.changeConfig);
@ -154,7 +157,8 @@ const ResourcePage = ({ topics }) => {
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
margin: 0; margin: 0;
padding: 0 12px 40px; padding: 0;
overflow: hidden;
} }
</style> </style>
</head> </head>
@ -166,6 +170,19 @@ const ResourcePage = ({ topics }) => {
return fileContent; return fileContent;
}, [content]); }, [content]);
const handleLoad = () => {
const iframe = iframeRef.current;
if (iframe) {
try {
// Only works for same-origin content
const contentHeight = iframe.contentDocument.body.scrollHeight;
iframe.style.height = contentHeight + "px";
} catch (error) {
console.log("Cross-origin iframe - cannot access content height");
}
}
};
const handleTokenSubmit = () => { const handleTokenSubmit = () => {
if (tokenInput.trim()) { if (tokenInput.trim()) {
setAuthToken(tokenInput); setAuthToken(tokenInput);
@ -295,9 +312,11 @@ const ResourcePage = ({ topics }) => {
}} }}
/> />
<iframe <iframe
ref={iframeRef}
onLoad={handleLoad}
srcDoc={htmlContent} srcDoc={htmlContent}
title="MD Content" title="MD Content"
className="flex-1 min-h-full" className="flex-1 min-h-full border-none p-4"
allow="fullscreen" allow="fullscreen"
/> />
</div> </div>