-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vscode): add rem to px preview (#2808)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
6 changed files
with
200 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { toArray } from '@unocss/core' | ||
import type { ExtensionContext } from 'vscode' | ||
import { workspace } from 'vscode' | ||
import type { AutoCompleteMatchType } from '@unocss/autocomplete' | ||
import { createNanoEvents } from '../../core/src/utils/events' | ||
|
||
export interface UseConfigurationOptions<Init> { | ||
ext?: ExtensionContext | ||
scope?: string | ||
initialValue: Init | ||
alias?: Partial<Record<keyof Init, string>> | ||
} | ||
|
||
export type ConfigurationListenerMap<Init> = Map<keyof Init, WatchConfigurationHandler<Init, keyof Init>> | ||
|
||
export type WatchConfigurationHandler<Init, K extends keyof Init> = (value: Init[K]) => void | ||
|
||
export function getConfigurations<Init extends Record<string, unknown>>(options: UseConfigurationOptions<Init>) { | ||
const { initialValue, alias, scope, ext } = options | ||
const configuration = {} as Init | ||
|
||
const getConfigurationKey = (key: keyof Init) => { | ||
key = alias?.[key] ?? key | ||
return [scope, key].filter(Boolean).join('.') | ||
} | ||
|
||
const reload = () => { | ||
const _config = workspace.getConfiguration() | ||
for (const key in initialValue) { | ||
const configurationKey = getConfigurationKey(key) | ||
configuration[key] = _config.get(configurationKey, initialValue[key]) | ||
} | ||
} | ||
|
||
const reset = () => { | ||
const _config = workspace.getConfiguration() | ||
for (const key in initialValue) { | ||
configuration[key] = initialValue[key] | ||
const configurationKey = getConfigurationKey(key) | ||
_config.update(configurationKey, initialValue[key], true) | ||
} | ||
} | ||
|
||
reload() | ||
|
||
const emitter = createNanoEvents() | ||
|
||
const watchChanged = <K extends keyof Init>(key: K | K[], fn: WatchConfigurationHandler<Init, K>) => { | ||
const keys = toArray(key) | ||
const unsubscribes = keys.map(key => emitter.on(`update:${String(key)}`, fn)) | ||
return () => unsubscribes.forEach(fn => fn()) | ||
} | ||
|
||
const disposable = workspace.onDidChangeConfiguration((e) => { | ||
const _config = workspace.getConfiguration() | ||
const changedKeys = new Set<keyof Init>() | ||
|
||
for (const key in initialValue) { | ||
const configurationKey = getConfigurationKey(key) | ||
if (e.affectsConfiguration(configurationKey)) { | ||
const value = _config.get(configurationKey, initialValue[key]) | ||
configuration[key as keyof Init] = value as Init[keyof Init] | ||
changedKeys.add(key) | ||
} | ||
} | ||
for (const key of changedKeys) | ||
emitter.emit(`update:${String(key)}`, configuration[key]) | ||
}) | ||
|
||
if (ext) | ||
ext.subscriptions.push(disposable) | ||
|
||
return { | ||
configuration, | ||
watchChanged, | ||
disposable, | ||
reload, | ||
reset, | ||
} | ||
} | ||
|
||
export function useConfigurations(ext: ExtensionContext) { | ||
return getConfigurations({ | ||
ext, | ||
scope: 'unocss', | ||
initialValue: { | ||
colorPreview: true, | ||
languagesIds: <string[]>[], | ||
matchType: <AutoCompleteMatchType>'prefix', | ||
maxItems: 1000, | ||
remToPxPreview: false, | ||
remToPxRatio: 16, | ||
underline: true, | ||
}, | ||
alias: { | ||
matchType: 'autocomplete.matchType', | ||
maxItems: 'autocomplete.maxItems', | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters