@master/css-runtime 2.0.0-rc.54 → 2.0.0-rc.56
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.
- package/dist/core.d.ts +2 -4
- package/dist/debug.d.ts +1 -0
- package/dist/global.min.js +1 -1
- package/dist/index.cjs +55 -83
- package/dist/index.d.ts +2 -2
- package/dist/index.development.cjs +443 -0
- package/dist/index.development.d.ts +1 -0
- package/dist/index.development.mjs +440 -0
- package/dist/index.mjs +54 -82
- package/dist/init.d.ts +10 -0
- package/dist/register-global.d.ts +9 -0
- package/package.json +1 -1
- package/dist/init-css-runtime.d.ts +0 -9
- package/dist/types/global.d.ts +0 -9
package/dist/index.cjs
CHANGED
|
@@ -3,45 +3,40 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var css = require('@master/css');
|
|
6
|
+
var cssDevtoolsHook = require('@master/css-devtools-hook');
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
function registerGlobal(CSSRuntime) {
|
|
9
|
+
if (!globalThis.CSSRuntime) globalThis.CSSRuntime = CSSRuntime;
|
|
10
|
+
cssDevtoolsHook.installHook();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class CSSRuntime extends css.MasterCSS {
|
|
8
14
|
root;
|
|
9
15
|
customConfig;
|
|
10
|
-
|
|
16
|
+
static instances = new WeakMap();
|
|
11
17
|
host;
|
|
12
18
|
observing;
|
|
13
19
|
progressive;
|
|
14
|
-
// @ts-expect-error
|
|
15
20
|
container;
|
|
16
21
|
observer;
|
|
17
22
|
constructor(root = document, customConfig = css.config){
|
|
18
23
|
super(customConfig), this.root = root, this.customConfig = customConfig, this.observing = false, this.progressive = false;
|
|
19
|
-
|
|
20
|
-
this.supportVendors = new Set();
|
|
21
|
-
const styleDeclaration = document.documentElement.style;
|
|
22
|
-
if ('webkitTransform' in styleDeclaration) this.supportVendors.add('webkit');
|
|
23
|
-
if ('MozTransform' in styleDeclaration) this.supportVendors.add('moz');
|
|
24
|
-
if ('msTransform' in styleDeclaration) this.supportVendors.add('ms');
|
|
25
|
-
if ('OTransform' in styleDeclaration) this.supportVendors.add('o');
|
|
26
|
-
}
|
|
27
|
-
init() {
|
|
28
|
-
const existingRuntimeCSS = globalThis.runtimeCSSs.find((eachCSS)=>eachCSS.root === this.root);
|
|
29
|
-
if (existingRuntimeCSS) throw new Error('Cannot create multiple RuntimeCSS instances for the same root element.');
|
|
30
|
-
const rootConstructorName = this.root?.constructor.name;
|
|
24
|
+
const rootConstructorName = root?.constructor.name;
|
|
31
25
|
if (rootConstructorName === 'HTMLDocument' || rootConstructorName === 'Document') {
|
|
32
|
-
|
|
33
|
-
this.root.defaultView.globalThis.runtimeCSS = this;
|
|
34
|
-
// @ts-ignore readonly
|
|
26
|
+
this.root.defaultView.globalThis.cssRuntime = this;
|
|
35
27
|
this.container = this.root.head;
|
|
36
|
-
// @ts-ignore readonly
|
|
37
28
|
this.host = this.root.documentElement;
|
|
38
29
|
} else {
|
|
39
|
-
// @ts-ignore readonly
|
|
40
30
|
this.container = this.root;
|
|
41
|
-
// @ts-ignore readonly
|
|
42
31
|
this.host = this.root.host;
|
|
43
32
|
}
|
|
44
|
-
|
|
33
|
+
this.supportVendors = new Set();
|
|
34
|
+
const styleDeclaration = document.documentElement.style;
|
|
35
|
+
if ('webkitTransform' in styleDeclaration) this.supportVendors.add('webkit');
|
|
36
|
+
if ('MozTransform' in styleDeclaration) this.supportVendors.add('moz');
|
|
37
|
+
if ('msTransform' in styleDeclaration) this.supportVendors.add('ms');
|
|
38
|
+
if ('OTransform' in styleDeclaration) this.supportVendors.add('o');
|
|
39
|
+
globalThis.CSSRuntime.instances.set(this.root, this);
|
|
45
40
|
}
|
|
46
41
|
/**
|
|
47
42
|
* Observe the DOM for changes and update the running stylesheet. (browser only)
|
|
@@ -70,10 +65,10 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
70
65
|
const firstConnectedElementsClasses = new Map();
|
|
71
66
|
const addClasses = (element)=>{
|
|
72
67
|
element.classList.forEach((className)=>{
|
|
73
|
-
if (this.
|
|
74
|
-
this.
|
|
68
|
+
if (this.classCounts.has(className)) {
|
|
69
|
+
this.classCounts.set(className, this.classCounts.get(className) + 1);
|
|
75
70
|
} else {
|
|
76
|
-
this.
|
|
71
|
+
this.classCounts.set(className, 1);
|
|
77
72
|
this.add(className);
|
|
78
73
|
}
|
|
79
74
|
const classes = firstConnectedElementsClasses.get(element);
|
|
@@ -98,14 +93,14 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
98
93
|
// const test = ''
|
|
99
94
|
// if (test) {
|
|
100
95
|
// console.log('')
|
|
101
|
-
// console.log(`${test}: ${this.
|
|
96
|
+
// console.log(`${test}: ${this.classCounts.get(test)}`)
|
|
102
97
|
// }
|
|
103
|
-
const
|
|
98
|
+
const eachClassCounts = new Map();
|
|
104
99
|
const targetFirstAttrMutationRecord = new Map();
|
|
105
|
-
const
|
|
100
|
+
const updateClassCount = (classes, isAdding = false)=>{
|
|
106
101
|
const usage = isAdding ? 1 : -1;
|
|
107
102
|
classes.forEach((className)=>{
|
|
108
|
-
|
|
103
|
+
eachClassCounts.set(className, (eachClassCounts.get(className) || 0) + usage);
|
|
109
104
|
});
|
|
110
105
|
};
|
|
111
106
|
const connectedStatusMap = new Map();
|
|
@@ -148,12 +143,12 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
148
143
|
targetFirstAttrMutationRecord.delete(target);
|
|
149
144
|
}
|
|
150
145
|
if (adding) {
|
|
151
|
-
|
|
146
|
+
updateClassCount(target.classList, adding);
|
|
152
147
|
} else {
|
|
153
148
|
if (firstAttrMutationRecord) {
|
|
154
|
-
|
|
149
|
+
updateClassCount(firstAttrMutationRecord.oldValue ? firstAttrMutationRecord.oldValue.split(/\s+/) : [], adding);
|
|
155
150
|
} else {
|
|
156
|
-
|
|
151
|
+
updateClassCount(target.classList, adding);
|
|
157
152
|
}
|
|
158
153
|
disconnectedStatusMap.forEach((disconnectedTargetStatus, disconnectedTarget)=>{
|
|
159
154
|
if (disconnectedTargetStatus.mutationRecord.target === target && disconnectedTargetStatus.change !== 0) {
|
|
@@ -178,59 +173,33 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
178
173
|
const removedClasses = oldClassList.filter((c)=>!newClassList.contains(c));
|
|
179
174
|
if (addedClasses.length) {
|
|
180
175
|
// console.log('[attribute]', '[add] ', addedClasses, target)
|
|
181
|
-
|
|
176
|
+
updateClassCount(addedClasses, true);
|
|
182
177
|
}
|
|
183
178
|
if (removedClasses.length) {
|
|
184
179
|
// console.log('[attribute]', '[remove]', removedClasses, target)
|
|
185
|
-
|
|
180
|
+
updateClassCount(removedClasses, false);
|
|
186
181
|
}
|
|
187
182
|
});
|
|
188
183
|
/**
|
|
189
184
|
* Merge the class usage changes into the current class usage map.
|
|
190
|
-
*/
|
|
191
|
-
let currentCount = this.
|
|
185
|
+
*/ eachClassCounts.forEach((countChange, className)=>{
|
|
186
|
+
let currentCount = this.classCounts.get(className) || 0;
|
|
192
187
|
let newCount = currentCount + countChange;
|
|
193
188
|
if (newCount > 0) {
|
|
194
|
-
this.
|
|
189
|
+
this.classCounts.set(className, newCount);
|
|
195
190
|
if (currentCount === 0) {
|
|
196
191
|
this.add(className);
|
|
197
192
|
}
|
|
198
193
|
} else {
|
|
199
|
-
this.
|
|
194
|
+
this.classCounts.delete(className);
|
|
200
195
|
this.remove(className);
|
|
201
196
|
}
|
|
202
197
|
});
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
// element.classList.forEach((className) => {
|
|
209
|
-
// if (Object.prototype.hasOwnProperty.call(safeClassUsages, className)) {
|
|
210
|
-
// safeClassUsages[className]++
|
|
211
|
-
// } else {
|
|
212
|
-
// safeClassUsages[className] = 1
|
|
213
|
-
// }
|
|
214
|
-
// })
|
|
215
|
-
// })
|
|
216
|
-
// this.host.classList.forEach((className) => {
|
|
217
|
-
// if (Object.prototype.hasOwnProperty.call(safeClassUsages, className)) {
|
|
218
|
-
// safeClassUsages[className]++
|
|
219
|
-
// } else {
|
|
220
|
-
// safeClassUsages[className] = 1
|
|
221
|
-
// }
|
|
222
|
-
// })
|
|
223
|
-
// for (const className in safeClassUsages) {
|
|
224
|
-
// if (this.classUsages.get(className) !== safeClassUsages[className]) {
|
|
225
|
-
// throw new Error(`[css] ${className} ${this.classUsages.get(className)} (correct: ${safeClassUsages[className]})`)
|
|
226
|
-
// }
|
|
227
|
-
// }
|
|
228
|
-
// this.classUsages.forEach((count, className) => {
|
|
229
|
-
// if (!Object.prototype.hasOwnProperty.call(safeClassUsages, className)) {
|
|
230
|
-
// throw new Error(`[css] ${className} ${count} (correct: 0)`)
|
|
231
|
-
// }
|
|
232
|
-
// })
|
|
233
|
-
// end: debug
|
|
198
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('mutated', {
|
|
199
|
+
records: mutationRecords,
|
|
200
|
+
classCounts: eachClassCounts,
|
|
201
|
+
cssRuntime: this
|
|
202
|
+
});
|
|
234
203
|
});
|
|
235
204
|
this.observer.observe(this.root, {
|
|
236
205
|
attributes: true,
|
|
@@ -242,7 +211,7 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
242
211
|
subtree: true
|
|
243
212
|
});
|
|
244
213
|
if (!this.progressive) {
|
|
245
|
-
this.host.
|
|
214
|
+
this.host.removeAttribute('hidden');
|
|
246
215
|
}
|
|
247
216
|
// @ts-ignore
|
|
248
217
|
this.observing = true;
|
|
@@ -361,7 +330,7 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
361
330
|
// @ts-ignore
|
|
362
331
|
this.observing = false;
|
|
363
332
|
this.reset();
|
|
364
|
-
this.
|
|
333
|
+
this.classCounts.clear();
|
|
365
334
|
if (!this.progressive) {
|
|
366
335
|
this.style?.remove();
|
|
367
336
|
this.style = null;
|
|
@@ -378,25 +347,28 @@ class RuntimeCSS extends css.MasterCSS {
|
|
|
378
347
|
}
|
|
379
348
|
destroy() {
|
|
380
349
|
this.disconnect();
|
|
381
|
-
|
|
350
|
+
globalThis.CSSRuntime.instances.delete(this.root);
|
|
382
351
|
return this;
|
|
383
352
|
}
|
|
384
353
|
}
|
|
385
|
-
|
|
386
|
-
(
|
|
387
|
-
|
|
388
|
-
globalThis.runtimeCSSs = runtimeCSSs;
|
|
389
|
-
})();
|
|
354
|
+
(function(CSSRuntime) {
|
|
355
|
+
registerGlobal(CSSRuntime);
|
|
356
|
+
})(CSSRuntime);
|
|
390
357
|
|
|
391
358
|
/**
|
|
392
|
-
* Initialize a new
|
|
359
|
+
* Initialize a new CSSRuntime instance and observe the target root
|
|
393
360
|
* @param config master css config
|
|
394
|
-
* @param
|
|
361
|
+
* @param root target root to observe
|
|
362
|
+
* @param autoObserve auto observe the target root
|
|
395
363
|
* @returns master css instance
|
|
396
|
-
*/ function initCSSRuntime(config) {
|
|
397
|
-
|
|
364
|
+
*/ function initCSSRuntime(config, root = document, autoObserve = true) {
|
|
365
|
+
let cssRuntime = globalThis.CSSRuntime.instances.get(root);
|
|
366
|
+
if (cssRuntime) return cssRuntime;
|
|
367
|
+
cssRuntime = new CSSRuntime(document, config);
|
|
368
|
+
if (autoObserve) cssRuntime.observe();
|
|
369
|
+
return cssRuntime;
|
|
398
370
|
}
|
|
399
371
|
|
|
400
|
-
exports.
|
|
401
|
-
exports.default =
|
|
372
|
+
exports.CSSRuntime = CSSRuntime;
|
|
373
|
+
exports.default = CSSRuntime;
|
|
402
374
|
exports.initCSSRuntime = initCSSRuntime;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { default as initCSSRuntime } from './init
|
|
1
|
+
export { default as CSSRuntime, default } from './core';
|
|
2
|
+
export { default as initCSSRuntime } from './init';
|