2023-03-06 12:25:54 +08:00
|
|
|
/**
|
|
|
|
* {}
|
|
|
|
* @author yutent<yutent.io@gmail.com>
|
|
|
|
* @date 2023/03/06 12:08:35
|
|
|
|
*/
|
|
|
|
|
2023-03-12 23:04:50 +08:00
|
|
|
const boolMap = Object.create(null)
|
2023-03-10 18:38:52 +08:00
|
|
|
|
|
|
|
;[
|
|
|
|
'autofocus',
|
|
|
|
'autoplay',
|
|
|
|
'async',
|
|
|
|
'allowTransparency',
|
|
|
|
'checked',
|
|
|
|
'controls',
|
|
|
|
'declare',
|
|
|
|
'disabled',
|
|
|
|
'defer',
|
|
|
|
'defaultChecked',
|
|
|
|
'defaultSelected',
|
|
|
|
'contentEditable',
|
|
|
|
'isMap',
|
|
|
|
'loop',
|
|
|
|
'multiple',
|
|
|
|
'noHref',
|
|
|
|
'noResize',
|
|
|
|
'noShade',
|
|
|
|
'open',
|
|
|
|
'readOnly',
|
2023-03-16 13:40:25 +08:00
|
|
|
'selected',
|
|
|
|
'loading'
|
2023-03-10 18:38:52 +08:00
|
|
|
].forEach(function (name) {
|
|
|
|
boolMap[name.toLowerCase()] = name
|
|
|
|
})
|
|
|
|
|
|
|
|
export { boolMap }
|
|
|
|
|
2023-03-08 11:50:38 +08:00
|
|
|
export const WC_PART = Symbol('wc_path')
|
2023-03-08 15:14:02 +08:00
|
|
|
export const NOTHING = Symbol('wc-nothing')
|
2023-03-10 18:38:52 +08:00
|
|
|
export const __finalized__ = Symbol('finalized')
|
|
|
|
export const __props__ = Symbol('props')
|
|
|
|
export const __changed_props__ = Symbol('changed_props')
|
|
|
|
export const __mounted__ = Symbol('mounted')
|
2023-03-13 18:28:03 +08:00
|
|
|
export const __pending__ = Symbol('pending')
|
|
|
|
export const __children__ = Symbol('children')
|
2023-03-07 19:15:23 +08:00
|
|
|
|
2023-03-12 23:04:50 +08:00
|
|
|
export const RESET_CSS_STYLE = `* {box-sizing: border-box;margin: 0;padding: 0;}::before,::after {box-sizing: border-box;}`
|
2023-03-08 11:11:05 +08:00
|
|
|
|
2023-03-13 18:28:03 +08:00
|
|
|
function getDefaultValue(type) {
|
|
|
|
switch (type) {
|
|
|
|
case Number:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
case Boolean:
|
|
|
|
return false
|
|
|
|
|
|
|
|
case Object:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
case Array:
|
|
|
|
return []
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ''
|
2023-03-08 11:11:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 18:28:03 +08:00
|
|
|
function getType(v) {
|
|
|
|
switch (typeof v) {
|
|
|
|
case 'number':
|
|
|
|
return { type: Number, default: v }
|
|
|
|
|
|
|
|
case 'boolean':
|
|
|
|
return { type: Boolean, default: v }
|
|
|
|
|
|
|
|
case 'object':
|
|
|
|
return Array.isArray(v)
|
|
|
|
? { type: Array, default: v }
|
|
|
|
: { type: Object, default: v }
|
|
|
|
|
|
|
|
default:
|
|
|
|
return { type: String, default: v + '' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parsePropsDeclaration(options) {
|
|
|
|
if (options && typeof options === 'object') {
|
|
|
|
if (options.hasOwnProperty('type')) {
|
|
|
|
return Object.assign(
|
|
|
|
{ attribute: true, default: getDefaultValue(options.type) },
|
|
|
|
options
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (options) {
|
|
|
|
case Number:
|
|
|
|
options = { type: Number }
|
|
|
|
break
|
|
|
|
|
|
|
|
case Boolean:
|
|
|
|
options = { type: Boolean }
|
|
|
|
break
|
|
|
|
|
|
|
|
case Object:
|
|
|
|
options = { type: Object }
|
|
|
|
break
|
|
|
|
|
|
|
|
case Array:
|
|
|
|
options = { type: Array }
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
options = getType(options)
|
|
|
|
break
|
|
|
|
}
|
2023-03-15 19:14:22 +08:00
|
|
|
options.default = options.hasOwnProperty('default')
|
|
|
|
? options.default
|
|
|
|
: getDefaultValue(options.type)
|
2023-03-13 18:28:03 +08:00
|
|
|
options.attribute = true
|
|
|
|
return options
|
2023-03-08 11:11:05 +08:00
|
|
|
}
|
|
|
|
|
2023-03-13 18:28:03 +08:00
|
|
|
export function fixedValue(value, options) {
|
|
|
|
switch (options.type) {
|
|
|
|
case Number:
|
2023-03-15 19:14:22 +08:00
|
|
|
return value === null ? null : +value || 0
|
2023-03-13 18:28:03 +08:00
|
|
|
|
|
|
|
case Boolean:
|
|
|
|
return value === false ? false : value !== null
|
|
|
|
|
|
|
|
case Object:
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
return JSON.parse(value)
|
|
|
|
} catch (err) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case Array:
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
return value
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
return JSON.parse(value)
|
|
|
|
} catch (err) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2023-03-16 16:31:16 +08:00
|
|
|
return value === null || value === void 0 ? null : value + ''
|
2023-03-13 18:28:03 +08:00
|
|
|
}
|
2023-03-08 11:11:05 +08:00
|
|
|
}
|