diff --git a/package-lock.json b/package-lock.json index dce8356..6ede1bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "background", - "version": "2.0.7", + "version": "2.0.8-alpha.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "background", - "version": "2.0.7", + "version": "2.0.8-alpha.5", "license": "MIT", "dependencies": { "@vscode/sudo-prompt": "^9.3.1", diff --git a/package.json b/package.json index f11747e..0ed04e4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "background", "displayName": "background", "description": "Bring background images to your vscode", - "version": "2.0.7", + "version": "2.0.8-alpha.5", "scripts": { "vscode:prepublish": "npm run compile", "vscode:uninstall": "node ./out/uninstall", diff --git a/src/background/Background.ts b/src/background/Background.ts index ffcd80a..38f286b 100644 --- a/src/background/Background.ts +++ b/src/background/Background.ts @@ -88,7 +88,7 @@ export class Background implements Disposable { let content = await fs.promises.readFile(path.join(docDir, docName), ENCODING); // 替换图片内联为base64 content = content.replace(/\.\.\/images[^\")]+/g, (relativePath: string) => { - const imgPath = path.join(vscodePath.extensionRoot, 'images', relativePath); + const imgPath = path.join(vscodePath.extRoot, 'images', relativePath); return ( `data:image/${path.extname(imgPath).slice(1) || 'png'};base64,` + diff --git a/src/background/PatchGenerator/PatchGenerator.base.ts b/src/background/PatchGenerator/PatchGenerator.base.ts index 6bba415..2718d69 100644 --- a/src/background/PatchGenerator/PatchGenerator.base.ts +++ b/src/background/PatchGenerator/PatchGenerator.base.ts @@ -1,4 +1,3 @@ -import path from 'path'; import { pathToFileURL } from 'url'; import fg from 'fast-glob'; @@ -47,6 +46,9 @@ export function css(template: TemplateStringsArray, ...args: any[]) { export class AbsPatchGenerator { protected config: T; + /** 必须有图片 */ + protected imageRequired = true; + constructor(config: T) { const images = config?.images.filter(n => n.length) || []; this.config = { @@ -168,16 +170,24 @@ container.appendChild(div); } public create() { + if (this.imageRequired && !this.config.images.length) { + return ''; + } + const style = this.compileCSS(this.getStyle()); const script = this.getScript().trim(); return [ this.getPreload(), - ` - var style = document.createElement("style"); + (() => { + if (!style.length) { + return ''; + } + return ` + const style = document.createElement("style"); style.textContent = ${JSON.stringify(style)}; - document.head.appendChild(style); - `, + document.head.appendChild(style);`; + })(), script ] .filter(n => !!n.length) @@ -185,3 +195,11 @@ container.appendChild(div); .join(';'); } } + +export class WithoutImagesPatchGenerator extends AbsPatchGenerator { + constructor() { + super({ images: [] }); + } + + protected readonly imageRequired = false; +} diff --git a/src/background/PatchGenerator/PatchGenerator.checksums.ts b/src/background/PatchGenerator/PatchGenerator.checksums.ts index 03ef791..e92933f 100644 --- a/src/background/PatchGenerator/PatchGenerator.checksums.ts +++ b/src/background/PatchGenerator/PatchGenerator.checksums.ts @@ -1,4 +1,4 @@ -import { AbsPatchGenerator, css } from './PatchGenerator.base'; +import { css, WithoutImagesPatchGenerator } from './PatchGenerator.base'; // https://github.com/Microsoft/vscode/blob/main/src/vs/workbench/services/integrity/electron-sandbox/integrityService.ts#L147 // https://github.com/microsoft/vscode-loc/blob/main/i18n/vscode-language-pack-zh-hans/translations/main.i18n.json#L14343 @@ -38,10 +38,7 @@ const Translations = [ '安裝似乎已損毀。請重新安裝。' ]; -export class ChecksumsPatchGenerator extends AbsPatchGenerator { - constructor() { - super({ images: [] }); - } +export class ChecksumsPatchGenerator extends WithoutImagesPatchGenerator { /** * fix checksums with css. LOL * diff --git a/src/background/PatchGenerator/PatchGenerator.fullscreen.ts b/src/background/PatchGenerator/PatchGenerator.fullscreen.ts index 43ec6fd..2e7b113 100644 --- a/src/background/PatchGenerator/PatchGenerator.fullscreen.ts +++ b/src/background/PatchGenerator/PatchGenerator.fullscreen.ts @@ -73,7 +73,7 @@ function getNextImg() { } function setNextImg() { - document.documentElement.style.setProperty(cssvariable, 'url(' + getNextImg() + ')'); + document.body.style.setProperty(cssvariable, 'url(' + getNextImg() + ')'); } if (interval > 0) { diff --git a/src/background/PatchGenerator/PatchGenerator.theme.ts b/src/background/PatchGenerator/PatchGenerator.theme.ts index ff096a6..18e3070 100644 --- a/src/background/PatchGenerator/PatchGenerator.theme.ts +++ b/src/background/PatchGenerator/PatchGenerator.theme.ts @@ -1,10 +1,6 @@ -import { AbsPatchGenerator, css } from './PatchGenerator.base'; - -export class ThemePatchGenerator extends AbsPatchGenerator { - constructor() { - super({ images: [] }); - } +import { css, WithoutImagesPatchGenerator } from './PatchGenerator.base'; +export class ThemePatchGenerator extends WithoutImagesPatchGenerator { /** * 混合模式使用 css variable * @@ -16,13 +12,13 @@ export class ThemePatchGenerator extends AbsPatchGenerator { protected getStyle(): string { return css` // 浅色主题(默认) - :root { + body { // 不使用混合模式 - ${ThemePatchGenerator.cssMixBlendMode}: normal; + ${ThemePatchGenerator.cssMixBlendMode}: unset; } - // 深色主题 (覆盖) - :root:has(body > .monaco-workbench.vs-dark) { + // 深色主题 (覆盖),避免使用 :root + body:has(> .monaco-workbench.vs-dark) { // 使用混合模式 ${ThemePatchGenerator.cssMixBlendMode}: screen; } diff --git a/src/background/PatchGenerator/index.ts b/src/background/PatchGenerator/index.ts index 058cb35..63ac4a8 100644 --- a/src/background/PatchGenerator/index.ts +++ b/src/background/PatchGenerator/index.ts @@ -35,6 +35,7 @@ export class PatchGenerator { new PanelPatchGenerator(options.panel).create(), // panel new FullscreenPatchGenerator(options.fullscreen).create() // fullscreen ] + .filter(n => !!n.length) .map(n => _.withIIFE(n)) .join(';'); diff --git a/src/utils/vscodePath.ts b/src/utils/vscodePath.ts index 8dd7ab0..7fa684c 100644 --- a/src/utils/vscodePath.ts +++ b/src/utils/vscodePath.ts @@ -43,7 +43,10 @@ export const vscodePath = { * 基础目录 */ base, - extensionRoot: path.join(__dirname, '../../'), + /** + * 扩展根目录 + */ + extRoot: path.join(__dirname, '../../'), /** * css文件路径 */