Set base inside of iframes (#1766)
* Set base inside of iframes * Add `system.getURLPrefix` syscall
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from "preact/hooks";
|
||||
import { useEffect, useMemo, useRef } from "preact/hooks";
|
||||
import type { Client } from "../client.ts";
|
||||
import type { PanelConfig } from "../types/ui.ts";
|
||||
import { panelHtml } from "./panel_html.ts";
|
||||
@@ -12,6 +12,10 @@ export function Panel({
|
||||
}) {
|
||||
const iFrameRef = useRef<HTMLIFrameElement>(null);
|
||||
|
||||
const html = useMemo(() => {
|
||||
return panelHtml.replace("{{.HostPrefix}}", document.baseURI);
|
||||
}, []);
|
||||
|
||||
function updateContent() {
|
||||
if (!iFrameRef.current?.contentWindow) {
|
||||
return;
|
||||
@@ -87,7 +91,7 @@ export function Panel({
|
||||
return (
|
||||
<div className="sb-panel" style={{ flex: config.mode }}>
|
||||
<iframe
|
||||
srcDoc={panelHtml}
|
||||
srcDoc={html}
|
||||
ref={iFrameRef}
|
||||
style={{ visibility: "hidden" }}
|
||||
onLoad={() => iFrameRef.current!.style.visibility = "visible"}
|
||||
|
||||
@@ -2,7 +2,7 @@ export const panelHtml = `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<base target="_top">
|
||||
<base target="_top" href="{{.HostPrefix}}">
|
||||
<meta name='color-scheme' content='dark light'>
|
||||
<script>
|
||||
const pendingRequests = new Map();
|
||||
|
||||
@@ -218,6 +218,10 @@ export class DocumentEditor {
|
||||
script.text = documentEditorJS;
|
||||
doc.head.prepend(script);
|
||||
|
||||
const base = doc.createElement("base");
|
||||
base.href = document.baseURI;
|
||||
doc.head.prepend(base);
|
||||
|
||||
// This isn't great, but the only way to get the up-to-date theme without
|
||||
// using media queries
|
||||
const theme = document.querySelector("html")?.getAttribute("data-theme");
|
||||
|
||||
@@ -141,6 +141,11 @@ export function systemSyscalls(
|
||||
"system.getMode": () => {
|
||||
return readOnlyMode ? "ro" : "rw";
|
||||
},
|
||||
"system.getURLPrefix": () => {
|
||||
const url = new URL(document.baseURI);
|
||||
|
||||
return url.pathname;
|
||||
},
|
||||
"system.getVersion": () => {
|
||||
return publicVersion;
|
||||
},
|
||||
|
||||
@@ -61,6 +61,13 @@ export function getMode(): Promise<"ro" | "rw"> {
|
||||
return syscall("system.getMode");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the prefix set by SB_URL_PREFIX or "/" if the variable isn't set
|
||||
*/
|
||||
export function getURLPrefix(): Promise<string> {
|
||||
return syscall("system.getURLPrefix");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SilverBullet version
|
||||
*/
|
||||
|
||||
@@ -55,6 +55,15 @@ local mode = system.getMode()
|
||||
print("System mode: " .. mode)
|
||||
```
|
||||
|
||||
### system.getURLPrefix()
|
||||
Returns the prefix set by [[Install/Configuration|SB_URL_PREFIX]] or "/" if the variable isn't set
|
||||
|
||||
Example:
|
||||
```lua
|
||||
local prefix = system.getURLPrefix()
|
||||
print("Prefix: " .. prefix)
|
||||
```
|
||||
|
||||
### system.getVersion()
|
||||
Returns the SilverBullet version.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user