Enhance/Settings UI of plugins (#4035)

more settings types & polish releated ui
This commit is contained in:
Charlie
2022-02-17 10:00:53 +08:00
committed by GitHub
parent 736a4610e6
commit 24b0236b7d
24 changed files with 1028 additions and 472 deletions

View File

@@ -1,8 +1,9 @@
import { StyleString, UIOptions } from './LSPlugin'
import { SettingSchemaDesc, StyleString, UIOptions } from './LSPlugin'
import { PluginLocal } from './LSPlugin.core'
import { snakeCase } from 'snake-case'
import * as nodePath from 'path'
import DOMPurify from 'dompurify'
import { merge } from 'lodash-es'
interface IObject {
[key: string]: any;
@@ -48,47 +49,7 @@ export function isObject (item: any) {
return (item === Object(item) && !Array.isArray(item))
}
export function deepMerge (
target: IObject,
...sources: Array<IObject>
) {
// return the target if no sources passed
if (!sources.length) {
return target
}
const result: IObject = target
if (isObject(result)) {
const len: number = sources.length
for (let i = 0; i < len; i += 1) {
const elm: any = sources[i]
if (isObject(elm)) {
for (const key in elm) {
if (elm.hasOwnProperty(key)) {
if (isObject(elm[key])) {
if (!result[key] || !isObject(result[key])) {
result[key] = {}
}
deepMerge(result[key], elm[key])
} else {
if (Array.isArray(result[key]) && Array.isArray(elm[key])) {
// concatenate the two arrays and remove any duplicate primitive values
result[key] = Array.from(new Set(result[key].concat(elm[key])))
} else {
result[key] = elm[key]
}
}
}
}
}
}
}
return result
}
export const deepMerge = merge
export function genID () {
// Math.random should be unique because of its seeding algorithm.
@@ -244,7 +205,6 @@ export function setupInjectedUI (
const pl = this
if ('slot' in ui) {
slot = ui.slot
selector = `#${slot}`
@@ -426,3 +386,17 @@ export function setupInjectedTheme (url?: string) {
injectedThemeEffect = null
})
}
export function mergeSettingsWithSchema (
settings: Record<string, any>,
schema: Array<SettingSchemaDesc>) {
const defaults = (schema || []).reduce((a, b) => {
if ('default' in b) {
a[b.key] = b.default
}
return a
}, {})
// shadow copy
return Object.assign(defaults, settings)
}