提交 6809dbef 编写于 作者: Q qiang

feat: initCostomDataset

上级 73ddbc99
import { camelize } from '@vue/shared'
interface HTMLElementWithDataset extends HTMLElement {
__uniDataset?: Record<string, any>
}
function formatKey(key: string) {
return camelize(key.substring(5))
}
export function initCostomDataset() {
const prototype = HTMLElement.prototype
const setAttribute = prototype.setAttribute
prototype.setAttribute = function (key, value) {
if (key.startsWith('data-') && this.tagName.startsWith('UNI-')) {
const dataset = ((this as HTMLElementWithDataset).__uniDataset =
(this as HTMLElementWithDataset).__uniDataset || {})
dataset[formatKey(key)] = value
}
setAttribute.call(this, key, value)
}
const removeAttribute = prototype.removeAttribute
prototype.removeAttribute = function (key) {
if (
(this as HTMLElementWithDataset).__uniDataset &&
key.startsWith('data-') &&
this.tagName.startsWith('UNI-')
) {
delete (this as HTMLElementWithDataset).__uniDataset![formatKey(key)]
}
removeAttribute.call(this, key)
}
}
export function getCostomDataset(el: HTMLElement | HTMLElementWithDataset) {
return Object.assign(
{},
el.dataset,
(el as HTMLElementWithDataset).__uniDataset
)
}
......@@ -2,11 +2,14 @@ import { App } from 'vue'
import { initLongPress } from './longPress'
import { initAppConfig } from './appConfig'
import { initCostomDataset } from './costomDataset'
export { getCostomDataset } from './costomDataset'
export function initView(app: App) {
if (__NODE_JS__) {
return
}
initCostomDataset()
if (__UNI_FEATURE_LONGPRESS__) {
initLongPress()
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册