hyper-html-api 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ // GENERATED from clayjs/src/lib/region-capabilities.js. Edit ClayJS and run `npm run propagate:foundation`.
2
+
3
+ export const CAPABILITIES = Object.freeze({
4
+ data: Object.freeze({ tokens: ['no-data'], bundles: ['editor-ui'] }),
5
+ save: Object.freeze({ tokens: ['no-save'], bundles: ['editor-ui'] }),
6
+ snapshot: Object.freeze({ tokens: ['no-snapshot'], bundles: ['editor-ui'] }),
7
+ watch: Object.freeze({ tokens: ['no-watch'], bundles: ['editor-ui'] }),
8
+ undo: Object.freeze({ tokens: ['no-undo'], bundles: ['editor-ui'] }),
9
+ history: Object.freeze({ tokens: [], bundles: ['editor-ui'] }),
10
+ })
11
+
12
+ export const BUNDLES = Object.freeze({
13
+ 'editor-ui': Object.freeze(['no-data', 'no-save', 'no-snapshot', 'no-watch', 'no-undo']),
14
+ })
15
+
16
+ export const POLICY_TOKENS = Object.freeze([
17
+ 'no-save',
18
+ 'no-snapshot',
19
+ 'no-trigger-autosave',
20
+ 'no-dirty',
21
+ 'no-watch',
22
+ 'no-undo',
23
+ 'no-data',
24
+ 'freeze',
25
+ 'editor-ui',
26
+ ])
27
+
28
+ export function hasPolicyToken(element, token) {
29
+ if (!element || element.nodeType !== 1) return false
30
+ const clay = element.getAttribute?.('clay')
31
+ return !!(
32
+ (clay && clay.split(/\s+/).includes(token)) ||
33
+ element.hasAttribute?.(token)
34
+ )
35
+ }
36
+
37
+ export function expandsTo(element, token) {
38
+ if (hasPolicyToken(element, token)) return true
39
+ for (const [bundle, members] of Object.entries(BUNDLES)) {
40
+ if (members.includes(token) && hasPolicyToken(element, bundle)) return true
41
+ }
42
+ return false
43
+ }
44
+
45
+ export function capabilitySelector(capability) {
46
+ const definition = CAPABILITIES[capability]
47
+ if (!definition) throw new Error(`Unknown region capability: ${capability}`)
48
+ return [...definition.tokens, ...definition.bundles]
49
+ .flatMap(token => [`[clay~="${token}"]`, `[${token}]`])
50
+ .join(', ')
51
+ }
52
+
53
+ export function closestWithCapability(node, capability) {
54
+ let element = node && node.nodeType === 1 ? node : node?.parentElement
55
+ while (element && element.nodeType === 1) {
56
+ const definition = CAPABILITIES[capability]
57
+ if (!definition) throw new Error(`Unknown region capability: ${capability}`)
58
+ if (definition.tokens.some(token => expandsTo(element, token)) ||
59
+ definition.bundles.some(bundle => hasPolicyToken(element, bundle))) return element
60
+ element = element.parentElement
61
+ }
62
+ return null
63
+ }
64
+
65
+ export function hasCapability(node, capability) {
66
+ return !!closestWithCapability(node, capability)
67
+ }
68
+
69
+ export function isPolicyAttribute(name) {
70
+ return name === 'clay' || POLICY_TOKENS.includes(name)
71
+ }