Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions packages/plugin/webview/src/lib/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,24 @@ function applyTheme(theme: Theme) {

/** Start listening on theme changed */
async function listenOnThemeChanged(client: PluginClient) {
let cssLink: HTMLLinkElement;
let cssHttpLink: HTMLLinkElement;
let cssHttpsLink: HTMLLinkElement;
// Memorized the css link but only create it when needed
const getLink = () => {
if (!cssLink) {
cssLink = document.createElement('link')
cssLink.setAttribute('rel', 'stylesheet')
document.head.prepend(cssLink)
const getLink = (element) => {
if (!element) {
element = document.createElement('link')
element.setAttribute('rel', 'stylesheet')
document.head.prepend(element)
}
return cssLink;
return element;
}

// If there is a url in the theme, use it
const setLink = (theme: Theme) => {
if (theme.url) {
getLink().setAttribute('href', theme.url.replace(/^http:/,"").replace(/^https:/,""))
const url = theme.url.replace(/^http:/, "protocol:").replace(/^https:/, "protocol:");
getLink(cssHttpLink).setAttribute('href', url.replace(/^protocol:/, "http:"));
getLink(cssHttpsLink).setAttribute('href', url.replace(/^protocol:/, "https:"));
document.documentElement.style.setProperty('--theme', theme.quality)
}
}
Expand All @@ -186,6 +189,6 @@ async function listenOnThemeChanged(client: PluginClient) {
setLink(theme);
applyTheme(theme);
})
return cssLink
return cssHttpsLink
}