From 51601100fd4334a81bc4792e47949ef33fbcc952 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 29 May 2022 17:31:36 +0800 Subject: [PATCH] fix: make sure all gitako elements within variable scope --- src/components/SideBar.tsx | 3 ++- src/content.tsx | 8 +++++--- src/styles/index.scss | 5 ++++- src/utils/DOMHelper.ts | 20 ++++++++++++-------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 00c193c..7c5918c 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -41,7 +41,8 @@ export function SideBar() { if (hasMetaData) { DOMHelper.markGitakoReadyState(true) setShowSettings(false) - $logoContainerElement.onChange(DOMHelper.insertLogoMountPoint()) + const mountPointElement = DOMHelper.insertLogoMountPoint() + if (mountPointElement) $logoContainerElement.onChange(mountPointElement) } else { DOMHelper.markGitakoReadyState(false) } diff --git a/src/content.tsx b/src/content.tsx index f4200f9..44d0ba8 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -2,6 +2,7 @@ import { Gitako } from 'components/Gitako' import { platform } from 'platforms' import * as React from 'react' import { createRoot } from 'react-dom/client' +import { rootElementID } from 'utils/DOMHelper' import './content.scss' if (platform.resolvePartialMetaData()) { @@ -14,9 +15,10 @@ if (platform.resolvePartialMetaData()) { async function init() { await injectStyles(browser.runtime.getURL('content.css')) - const SideBarElement = document.createElement('div') - document.body.appendChild(SideBarElement) - createRoot(SideBarElement).render() + const sideBarElement = document.createElement('div') + sideBarElement.setAttribute('id', rootElementID) + document.body.appendChild(sideBarElement) + createRoot(sideBarElement).render() } // injects a copy of stylesheets so that other extensions(e.g. dark reader) could read diff --git a/src/styles/index.scss b/src/styles/index.scss index 14b1f1e..7d0c128 100644 --- a/src/styles/index.scss +++ b/src/styles/index.scss @@ -301,6 +301,10 @@ $minimal-z-index: max( @include interactive-background($default, $hover, $active, $focus); } +##{$name}-root { + @include enableThemes; +} + .#{$name}-toggle-show-button-wrapper { @include hide-for-print(); @@ -365,7 +369,6 @@ $minimal-z-index: max( .#{$name}-side-bar { @include hide-for-print(); - @include enableThemes; $resizeHandlerWidth: 1px; .#{$name}-side-bar-body-wrapper { diff --git a/src/utils/DOMHelper.ts b/src/utils/DOMHelper.ts index c27734c..301560a 100644 --- a/src/utils/DOMHelper.ts +++ b/src/utils/DOMHelper.ts @@ -2,6 +2,8 @@ * this helper helps manipulating DOM */ +export const rootElementID = 'gitako-root' + export function setGitakoBodyClass(className: string, enable: boolean) { const classList = document.body.classList if (enable) classList.add(className) @@ -47,7 +49,8 @@ export function $( existCallback: undefined | null, otherwise: () => T2, ): HTMLElement | null | T2 -export function $(selector: string, existCallback?: any, otherwise?: any) { // eslint-disable-line @typescript-eslint/no-explicit-any +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function $(selector: string, existCallback?: any, otherwise?: any) { const element = document.querySelector(selector) if (element) { return existCallback ? existCallback(element) : element @@ -59,13 +62,14 @@ export function $(selector: string, existCallback?: any, otherwise?: any) { // e * add the logo element into DOM */ export function insertLogoMountPoint() { - const logoID = 'gitako-logo-mount-point' - const logoSelector = formatID(logoID) - return $(logoSelector, undefined, function createLogoMountPoint() { - const logoMountElement = document.createElement('div') - logoMountElement.setAttribute('id', logoID) - document.body.appendChild(logoMountElement) - return logoMountElement + return $(formatID(rootElementID), container => { + const logoID = 'gitako-logo-mount-point' + return $(formatID(logoID), undefined, function createLogoMountPoint() { + const logoMountElement = document.createElement('div') + logoMountElement.setAttribute('id', logoID) + container.appendChild(logoMountElement) + return logoMountElement + }) }) }