|
| 1 | +import type {CustomElementClass} from './custom-element.js' |
| 2 | +import type {ControllableClass} from './controllable.js' |
| 3 | +import {controllable} from './controllable.js' |
| 4 | +import {dasherize, mustDasherize} from './dasherize.js' |
| 5 | +import {createMark} from './mark.js' |
| 6 | +import {createAbility} from './ability.js' |
| 7 | + |
| 8 | +const attrChangedCallback = Symbol() |
| 9 | + |
| 10 | +export interface Attrable { |
| 11 | + [key: PropertyKey]: unknown |
| 12 | + [attrChangedCallback](changed: Map<PropertyKey, unknown>): void |
| 13 | +} |
| 14 | + |
| 15 | +export interface AttrableClass { |
| 16 | + new (): Attrable |
| 17 | +} |
| 18 | + |
| 19 | +const Identity = (v: unknown) => v |
| 20 | +let setFromMutation = false |
| 21 | +const attrs = new WeakMap<Element, Map<string, PropertyKey>>() |
| 22 | + |
| 23 | +const handleMutations = (mutations: MutationRecord[]) => { |
| 24 | + for (const mutation of mutations) { |
| 25 | + if (mutation.type === 'attributes') { |
| 26 | + const name = mutation.attributeName! |
| 27 | + const el = mutation.target as Element & {[key: PropertyKey]: unknown} |
| 28 | + const key = attrs.get(el)?.get(name) |
| 29 | + if (key) { |
| 30 | + setFromMutation = true |
| 31 | + el[key] = el.getAttribute(name) |
| 32 | + setFromMutation = false |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +const observer = new MutationObserver(handleMutations) |
| 38 | + |
| 39 | +const [attr, getAttr, initializeAttrs] = createMark<Element & Attrable>( |
| 40 | + ({name}) => mustDasherize(name, '@attr'), |
| 41 | + (instance: Element & Attrable, {name, kind, access}) => { |
| 42 | + let cast: typeof Identity | typeof Boolean | typeof Number | typeof String = Identity |
| 43 | + let initialValue: unknown |
| 44 | + if (access.get) { |
| 45 | + initialValue = access.get.call(instance) |
| 46 | + } else if ('value' in access && kind !== 'method') { |
| 47 | + initialValue = access.value |
| 48 | + } |
| 49 | + let value = initialValue |
| 50 | + const attributeName = dasherize(name) |
| 51 | + const setCallback = (kind === 'method' ? access.value : access.set) || Identity |
| 52 | + const getCallback = access.get || (() => value) |
| 53 | + if (!attrs.get(instance)) attrs.set(instance, new Map()) |
| 54 | + attrs.get(instance)!.set(attributeName, name) |
| 55 | + if (typeof value === 'number') { |
| 56 | + cast = Number |
| 57 | + } else if (typeof value === 'boolean') { |
| 58 | + cast = Boolean |
| 59 | + } else if (typeof value === 'string') { |
| 60 | + cast = String |
| 61 | + } |
| 62 | + const queue = new Map() |
| 63 | + const requestAttrChanged = async (newValue: unknown) => { |
| 64 | + queue.set(name, newValue) |
| 65 | + if (queue.size > 1) return |
| 66 | + await Promise.resolve() |
| 67 | + const changed = new Map(queue) |
| 68 | + queue.clear() |
| 69 | + instance[attrChangedCallback](changed) |
| 70 | + } |
| 71 | + return { |
| 72 | + get() { |
| 73 | + const has = instance.hasAttribute(attributeName) |
| 74 | + if (has) { |
| 75 | + return cast === Boolean ? has : cast(instance.getAttribute(attributeName)) |
| 76 | + } |
| 77 | + return cast(getCallback.call(instance)) |
| 78 | + }, |
| 79 | + set(newValue: unknown) { |
| 80 | + const isInitial = newValue === null |
| 81 | + if (isInitial) newValue = initialValue |
| 82 | + const same = Object.is(value, newValue) |
| 83 | + value = newValue |
| 84 | + setCallback.call(instance, value) |
| 85 | + if (setFromMutation || same || isInitial) return |
| 86 | + requestAttrChanged(newValue) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +) |
| 91 | + |
| 92 | +export {attr, getAttr, attrChangedCallback} |
| 93 | +export const attrable = createAbility( |
| 94 | + <T extends CustomElementClass>(Class: T): T & ControllableClass & AttrableClass => |
| 95 | + class extends controllable(Class) { |
| 96 | + [key: PropertyKey]: unknown |
| 97 | + constructor() { |
| 98 | + super() |
| 99 | + initializeAttrs(this) |
| 100 | + observer.observe(this, {attributeFilter: Array.from(getAttr(this)).map(dasherize)}) |
| 101 | + } |
| 102 | + |
| 103 | + [attrChangedCallback](changed: Map<PropertyKey, unknown>) { |
| 104 | + if (!this.isConnected) return |
| 105 | + for (const [name, value] of changed) { |
| 106 | + if (typeof value === 'boolean') { |
| 107 | + this.toggleAttribute(dasherize(name), value) |
| 108 | + } else { |
| 109 | + this.setAttribute(dasherize(name), String(value)) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +) |
0 commit comments