Skip to content

Commit

Permalink
feat!: change config key
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Oct 18, 2024
1 parent fab7e17 commit b53d827
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 47 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ VSCode extension that custom ui css style in both editor and webview

| Key | Description | Type | Default |
| --------------------------------------------- | ---------------------------------------------------------------------------------------------------- | --------- | ---------- |
| `custom-ui-style.monospace` | Global monospace font family that apply in both editor and webview, fallback to editor's font family | `string` | `` |
| `custom-ui-style.sansSerif` | Global sans-serif font family that apply in both editor and webview | `string` | `` |
| `custom-ui-style.backgroundUrl` | Full-screen background image url, support protocol: 'https://', 'file://', 'data:' | `string` | `` |
| `custom-ui-style.backgroundOpacity` | Background image opacity | `number` | `0.9` |
| `custom-ui-style.backgroundSize` | Background image size | `string` | `"cover"` |
| `custom-ui-style.backgroundPosition` | Background image size | `string` | `"center"` |
| `custom-ui-style.font.monospace` | Global monospace font family that apply in both editor and webview, fallback to editor's font family | `string` | `` |
| `custom-ui-style.font.sansSerif` | Global sans-serif font family that apply in both editor and webview | `string` | `` |
| `custom-ui-style.background.url` | Full-screen background image url, support protocol: 'https://', 'file://', 'data:' | `string` | `` |
| `custom-ui-style.background.opacity` | Background image opacity | `number` | `0.9` |
| `custom-ui-style.background.size` | Background image size | `string` | `"cover"` |
| `custom-ui-style.background.position` | Background image size | `string` | `"center"` |
| `custom-ui-style.stylesheet` | Custom css for editor, support nest selectors | `object` | `{}` |
| `custom-ui-style.webviewMonospaceSelector` | Custom monospace selector in webview | `array` | `` |
| `custom-ui-style.webviewSansSerifSelector` | Custom sans-serif selector in webview | `array` | `` |
| `custom-ui-style.webviewStylesheet` | Custom css for webview, support nest selectors | `object` | `{}` |
| `custom-ui-style.applyOnConfigurationChanged` | Whether to apply styles when configuration changed | `boolean` | `false` |
| `custom-ui-style.webview.monospaceSelector` | Custom monospace selector in webview | `array` | `` |
| `custom-ui-style.webview.sansSerifSelector` | Custom sans-serif selector in webview | `array` | `` |
| `custom-ui-style.webview.stylesheet` | Custom css for webview, support nest selectors | `object` | `{}` |
| `custom-ui-style.applyOnConfigurationChanged` | Whether to apply styles when configuration changed, 1500ms debounce | `boolean` | `false` |

<!-- configs -->

Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@
"type": "object",
"title": "Custom UI Style",
"properties": {
"custom-ui-style.monospace": {
"custom-ui-style.font.monospace": {
"scope": "application",
"type": "string",
"description": "Global monospace font family that apply in both editor and webview, fallback to editor's font family"
},
"custom-ui-style.sansSerif": {
"custom-ui-style.font.sansSerif": {
"scope": "application",
"type": "string",
"description": "Global sans-serif font family that apply in both editor and webview"
},
"custom-ui-style.backgroundUrl": {
"custom-ui-style.background.url": {
"scope": "machine",
"type": "string",
"description": "Full-screen background image url, support protocol: 'https://', 'file://', 'data:'"
},
"custom-ui-style.backgroundOpacity": {
"custom-ui-style.background.opacity": {
"scope": "application",
"type": "number",
"description": "Background image opacity",
"default": 0.9
},
"custom-ui-style.backgroundSize": {
"custom-ui-style.background.size": {
"scope": "application",
"type": "string",
"enum": [
Expand All @@ -84,7 +84,7 @@
"description": "Background image size",
"default": "cover"
},
"custom-ui-style.backgroundPosition": {
"custom-ui-style.background.position": {
"scope": "application",
"type": "string",
"description": "Background image size",
Expand All @@ -95,23 +95,23 @@
"type": "object",
"description": "Custom css for editor, support nest selectors"
},
"custom-ui-style.webviewMonospaceSelector": {
"custom-ui-style.webview.monospaceSelector": {
"scope": "application",
"type": "array",
"items": {
"type": "string"
},
"description": "Custom monospace selector in webview"
},
"custom-ui-style.webviewSansSerifSelector": {
"custom-ui-style.webview.sansSerifSelector": {
"scope": "application",
"type": "array",
"items": {
"type": "string"
},
"description": "Custom sans-serif selector in webview"
},
"custom-ui-style.webviewStylesheet": {
"custom-ui-style.webview.stylesheet": {
"scope": "application",
"type": "object",
"description": "Custom css for webview, support nest selectors"
Expand All @@ -120,7 +120,7 @@
"scope": "resource",
"type": "boolean",
"default": false,
"description": "Whether to apply styles when configuration changed"
"description": "Whether to apply styles when configuration changed, 1500ms debounce"
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const editorConfig = defineConfigObject('editor', {
})

export function getFamilies() {
let { monospace, sansSerif } = config
monospace ||= editorConfig.fontFamily
return { monospace, sansSerif }
return {
monospace: config['font.monospace'] || editorConfig.fontFamily,
sansSerif: config['font.sansSerif'],
}
}
10 changes: 5 additions & 5 deletions src/manager/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { config, getFamilies } from '../config'
import { normalizeUrl } from '../path'
import { generateStyleFromObject } from '../utils'
import { BaseFileManager } from './base'
import { VSC_DFAULT_SANS_FONT, VSC_NOTEBOOK_MONO_FONT } from './js'
import { VSC_DFAULT_SANS_FONT, VSC_NOTEBOOK_MONO_FONT } from './renderer'

const banner = '/* Custom UI Style Start */'
const footer = '/* Custom UI Style End */'

function generateBackgroundCSS() {
const url = config.backgroundUrl
const url = config['background.url']
if (!url) {
return ''
}
return `
body:has([id='workbench.parts.editor']) {
background-size: ${config.backgroundSize};
background-size: ${config['background.size']};
background-repeat: no-repeat;
background-attachment: fixed; // for code-server
background-position: ${config.backgroundPosition};
opacity: ${config.backgroundOpacity};
background-position: ${config['background.position']};
opacity: ${config['background.opacity']};
background-image: url('${normalizeUrl(url)}');
}`
}
Expand Down
2 changes: 1 addition & 1 deletion src/manager/js.ts → src/manager/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const VSC_DFAULT_SANS_FONT = {

export const VSC_NOTEBOOK_MONO_FONT = `"SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace`

export class JsFileManager extends BaseFileManager {
export class RendererFileManager extends BaseFileManager {
patch(fontChanged: boolean, content: () => string): Promisable<string | undefined> {
if (!fontChanged) {
return undefined
Expand Down
13 changes: 4 additions & 9 deletions src/manager/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ const defaultSansSerifSelector: string[] = ['.font-sans', '.github-markdown-body

function getCSS() {
const { monospace, sansSerif } = getFamilies()
const {
webviewSansSerifSelector = [],
webviewMonospaceSelector = [],
webviewStylesheet,
} = config
let result = ''
if (monospace) {
const monoSelectors = [...defaultMonospaceSelector, ...webviewMonospaceSelector]
const monoSelectors = [...defaultMonospaceSelector, ...config['webview.monospaceSelector'] || []]
result += `${monoSelectors}{font-family:${escapeQuote(monospace)}!important}`
}
if (sansSerif) {
const sansSelectors = [...defaultSansSerifSelector, ...webviewSansSerifSelector]
const sansSelectors = [...defaultSansSerifSelector, ...config['webview.sansSerifSelector'] || []]
result += `${sansSelectors}{font-family:${escapeQuote(sansSerif)}!important}`
}
if (webviewStylesheet) {
result += generateStyleFromObject(webviewStylesheet)
if (config['webview.stylesheet']) {
result += generateStyleFromObject(config['webview.stylesheet'])
}
return result
}
Expand Down
32 changes: 23 additions & 9 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const webviewHTMLBakPath = getWebviewHTML(`.${bakExt}.html`)
/**
* See https://code.visualstudio.com/api/references/vscode-api#env
*/
function getMainPath(baseExt: string, backupExt?: string) {
function getRendererPath(baseExt: string, backupExt?: string) {
const ext = backupExt ? `${backupExt}.${baseExt}` : baseExt
return path.join(
baseDir,
Expand All @@ -42,21 +42,35 @@ function getMainPath(baseExt: string, backupExt?: string) {
)
}
/**
* css file path
* CSS file path
*/
export const cssPath = getMainPath('css')
export const cssPath = getRendererPath('css')
/**
* css file path
* CSS file backup path
*/
export const cssBakPath = getMainPath('css', bakExt)
export const cssBakPath = getRendererPath('css', bakExt)
/**
* js file path
* Main js file path
*/
export const jsPath = getMainPath('js')
export const rendererPath = getRendererPath('js')
/**
* js file path
* Main js file backup path
*/
export const jsBakPath = getMainPath('js', bakExt)
export const rendererBakPath = getRendererPath('js', bakExt)

function getMainPath(baseExt: string, backupExt?: string) {
const ext = backupExt ? `${backupExt}.${baseExt}` : baseExt
return path.join(
baseDir,
'vs',
'code',
'electron-main',
`main.${ext}`,
)
}

export const mainPath = getMainPath('js')
export const mainBakPath = getMainPath('js', bakExt)

export function normalizeUrl(url: string) {
if (!url.startsWith('file://')) {
Expand Down

0 comments on commit b53d827

Please sign in to comment.