@mantou/gem 1.7.12 → 2.0.0-alpha.0
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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/gem.es.js +2010 -1765
- package/dist/gem.umd.js +52 -53
- package/elements/base/dialog.d.ts +0 -11
- package/elements/base/dialog.d.ts.map +1 -1
- package/elements/base/dialog.js +93 -66
- package/elements/base/dialog.js.map +1 -1
- package/elements/base/gesture.d.ts +1 -2
- package/elements/base/gesture.d.ts.map +1 -1
- package/elements/base/gesture.js +157 -96
- package/elements/base/gesture.js.map +1 -1
- package/elements/base/link.d.ts +5 -5
- package/elements/base/link.d.ts.map +1 -1
- package/elements/base/link.js +249 -145
- package/elements/base/link.js.map +1 -1
- package/elements/base/modal-factory.d.ts +145 -139
- package/elements/base/modal-factory.d.ts.map +1 -1
- package/elements/base/modal-factory.js +84 -25
- package/elements/base/modal-factory.js.map +1 -1
- package/elements/base/reflect.d.ts +0 -1
- package/elements/base/reflect.d.ts.map +1 -1
- package/elements/base/reflect.js +83 -38
- package/elements/base/reflect.js.map +1 -1
- package/elements/base/route.d.ts +12 -24
- package/elements/base/route.d.ts.map +1 -1
- package/elements/base/route.js +263 -164
- package/elements/base/route.js.map +1 -1
- package/elements/base/title.d.ts +3 -11
- package/elements/base/title.d.ts.map +1 -1
- package/elements/base/title.js +110 -59
- package/elements/base/title.js.map +1 -1
- package/elements/base/unsafe.d.ts +0 -1
- package/elements/base/unsafe.d.ts.map +1 -1
- package/elements/base/unsafe.js +82 -33
- package/elements/base/unsafe.js.map +1 -1
- package/elements/base/use.d.ts.map +1 -1
- package/elements/base/use.js +95 -47
- package/elements/base/use.js.map +1 -1
- package/helper/i18n.d.ts +4 -9
- package/helper/i18n.d.ts.map +1 -1
- package/helper/i18n.js +69 -65
- package/helper/i18n.js.map +1 -1
- package/helper/logger.js +10 -9
- package/helper/logger.js.map +1 -1
- package/helper/react-shim.d.ts +15 -0
- package/helper/react-shim.d.ts.map +1 -0
- package/helper/react-shim.js +56 -0
- package/helper/react-shim.js.map +1 -0
- package/helper/react-utils.d.ts +15 -0
- package/helper/react-utils.d.ts.map +1 -0
- package/helper/react-utils.js +50 -0
- package/helper/react-utils.js.map +1 -0
- package/helper/request.d.ts +4 -4
- package/helper/request.d.ts.map +1 -1
- package/helper/request.js +1 -1
- package/helper/request.js.map +1 -1
- package/helper/ssr-shim.d.ts.map +1 -1
- package/helper/ssr-shim.js +8 -1
- package/helper/ssr-shim.js.map +1 -1
- package/helper/theme.d.ts +14 -21
- package/helper/theme.d.ts.map +1 -1
- package/helper/theme.js +55 -38
- package/helper/theme.js.map +1 -1
- package/index.d.ts +4 -3
- package/index.d.ts.map +1 -1
- package/index.js +5 -3
- package/index.js.map +1 -1
- package/lib/decorators.d.ts +74 -42
- package/lib/decorators.d.ts.map +1 -1
- package/lib/decorators.js +324 -84
- package/lib/decorators.js.map +1 -1
- package/lib/directives.d.ts +4 -0
- package/lib/directives.d.ts.map +1 -0
- package/lib/directives.js +5 -0
- package/lib/directives.js.map +1 -0
- package/lib/element.d.ts +112 -110
- package/lib/element.d.ts.map +1 -1
- package/lib/element.js +431 -468
- package/lib/element.js.map +1 -1
- package/lib/history.d.ts +4 -0
- package/lib/history.d.ts.map +1 -1
- package/lib/history.js +12 -13
- package/lib/history.js.map +1 -1
- package/lib/store.d.ts +1 -1
- package/lib/store.d.ts.map +1 -1
- package/lib/store.js +4 -5
- package/lib/store.js.map +1 -1
- package/lib/utils.d.ts +3 -43
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +61 -235
- package/lib/utils.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.d.ts.map +1 -1
- package/lib/version.js +2 -1
- package/lib/version.js.map +1 -1
- package/package.json +12 -11
- package/src/dist.ts +3 -2
- package/src/elements/base/dialog.ts +10 -20
- package/src/elements/base/gesture.ts +16 -14
- package/src/elements/base/link.ts +65 -44
- package/src/elements/base/modal-factory.ts +14 -14
- package/src/elements/base/reflect.ts +24 -32
- package/src/elements/base/route.ts +55 -68
- package/src/elements/base/title.ts +25 -42
- package/src/elements/base/unsafe.ts +12 -21
- package/src/elements/base/use.ts +17 -13
- package/src/helper/i18n.ts +20 -14
- package/src/helper/react-utils.ts +66 -0
- package/src/helper/ssr-shim.ts +8 -1
- package/src/helper/theme.ts +72 -45
- package/src/index.ts +6 -4
- package/src/lib/decorators.ts +394 -93
- package/src/lib/directives.ts +6 -0
- package/src/lib/element.ts +473 -500
- package/src/lib/history.ts +4 -3
- package/src/lib/store.ts +2 -1
- package/src/lib/utils.ts +12 -191
- package/src/lib/version.ts +2 -1
- package/src/test/elements/link.test.ts +1 -1
- package/src/test/elements/title.test.ts +1 -0
- package/src/test/gem-element/advance.test.ts +72 -49
- package/src/test/gem-element/basic.test.ts +37 -50
- package/src/test/gem-element/decorators.test.ts +22 -21
- package/src/test/gem-element/multi.test.ts +5 -5
- package/src/test/utils.test.ts +15 -45
package/src/lib/element.ts
CHANGED
|
@@ -1,37 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TemplateResult } from 'lit-html';
|
|
2
|
+
import { html, render } from 'lit-html';
|
|
2
3
|
|
|
3
4
|
import type { GemReflectElement } from '../elements/reflect';
|
|
4
5
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
addMicrotask,
|
|
9
|
-
Sheet,
|
|
10
|
-
SheetToken,
|
|
11
|
-
isArrayChange,
|
|
12
|
-
GemError,
|
|
13
|
-
kebabToCamelCase,
|
|
14
|
-
PropProxyMap,
|
|
15
|
-
removeItems,
|
|
16
|
-
addListener,
|
|
17
|
-
} from './utils';
|
|
18
|
-
import * as GemExports from './element';
|
|
19
|
-
import * as VersionExports from './version';
|
|
6
|
+
import type { Store } from './store';
|
|
7
|
+
import { connect } from './store';
|
|
8
|
+
import { LinkedList, addMicrotask, isArrayChange, addListener, randomStr } from './utils';
|
|
20
9
|
|
|
21
10
|
export { html, svg, render, directive, TemplateResult, SVGTemplateResult } from 'lit-html';
|
|
22
|
-
export { repeat } from 'lit-html/directives/repeat';
|
|
23
|
-
|
|
24
|
-
// https://github.com/Polymer/lit-html/issues/1048
|
|
25
|
-
export { guard } from 'lit-html/directives/guard';
|
|
26
|
-
|
|
27
|
-
export { ifDefined } from 'lit-html/directives/if-defined';
|
|
28
11
|
|
|
29
12
|
declare global {
|
|
30
|
-
|
|
31
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/CustomStateSet
|
|
32
|
-
states: Set<string>;
|
|
33
|
-
}
|
|
34
|
-
// 用于 css 选择器选择元素,使用 @refobject 自动选择获取
|
|
13
|
+
// 用于 css 选择器选择元素,使用 RefObject 自动选择获取
|
|
35
14
|
// 必须使用 attr 赋值
|
|
36
15
|
/**
|
|
37
16
|
* @attr ref
|
|
@@ -39,264 +18,447 @@ declare global {
|
|
|
39
18
|
interface HTMLElement {
|
|
40
19
|
ref: string;
|
|
41
20
|
}
|
|
21
|
+
interface DOMStringMap {
|
|
22
|
+
gemReflect?: '';
|
|
23
|
+
gemStyle?: '';
|
|
24
|
+
[name: string]: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
// https://github.com/tc39/proposal-decorator-metadata
|
|
27
|
+
interface SymbolConstructor {
|
|
28
|
+
readonly metadata: symbol;
|
|
29
|
+
}
|
|
42
30
|
}
|
|
43
31
|
|
|
44
|
-
|
|
45
|
-
|
|
32
|
+
const { assign, defineProperty, setPrototypeOf } = Object;
|
|
33
|
+
|
|
34
|
+
defineProperty(Symbol, 'metadata', { value: Symbol.for('Symbol.metadata') });
|
|
35
|
+
|
|
36
|
+
const execCallback = (fun: any) => typeof fun === 'function' && fun();
|
|
37
|
+
|
|
38
|
+
// 跨多个 gem 工作
|
|
39
|
+
export const SheetToken = Symbol.for('gem@sheetToken');
|
|
40
|
+
// proto prop
|
|
41
|
+
export const UpdateToken = Symbol.for('gem@update');
|
|
42
|
+
|
|
43
|
+
export const BoundaryCSSState = 'gem-style-boundary';
|
|
44
|
+
export const RenderErrorEvent = 'gem@render-error';
|
|
45
|
+
|
|
46
|
+
// fix modal-factory type error
|
|
47
|
+
const updateTokenAlias = UpdateToken;
|
|
48
|
+
|
|
49
|
+
const rootStyleSheetInfo = new WeakMap<Document | ShadowRoot, Map<CSSStyleSheet, number>>();
|
|
50
|
+
const rootUpdateFnMap = new WeakMap<Map<CSSStyleSheet, number>, () => void>();
|
|
51
|
+
|
|
52
|
+
const getReflectTargets = (ele: ShadowRoot | GemElement) =>
|
|
53
|
+
[...ele.querySelectorAll<GemReflectElement>('[data-gem-reflect]')].map((e) => e.target);
|
|
54
|
+
|
|
55
|
+
class RefObject<T = HTMLElement> {
|
|
56
|
+
refSelector: string;
|
|
57
|
+
ele: GemElement | ShadowRoot;
|
|
58
|
+
ref: string;
|
|
59
|
+
|
|
60
|
+
constructor(current: GemElement) {
|
|
61
|
+
const ref = `ref-${randomStr()}`;
|
|
62
|
+
this.refSelector = `[ref=${ref}]`;
|
|
63
|
+
this.ele = current.internals.shadowRoot || current;
|
|
64
|
+
this.ref = ref;
|
|
65
|
+
}
|
|
66
|
+
get element() {
|
|
67
|
+
for (const e of [this.ele, ...getReflectTargets(this.ele)]) {
|
|
68
|
+
// 在 LightDOM 中可能工作很慢?
|
|
69
|
+
const result = e.querySelector(this.refSelector);
|
|
70
|
+
if (result) return result as T;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
get elements() {
|
|
74
|
+
return [this.ele, ...getReflectTargets(this.ele)]
|
|
75
|
+
.map((e) => [...e.querySelectorAll(this.refSelector)] as T[])
|
|
76
|
+
.flat();
|
|
77
|
+
}
|
|
78
|
+
toString() {
|
|
79
|
+
return this.ref;
|
|
80
|
+
}
|
|
46
81
|
}
|
|
47
82
|
|
|
48
|
-
|
|
49
|
-
|
|
83
|
+
/**必须使用在字段中,否则会读取到错误的实例 */
|
|
84
|
+
export let createRef: <T>() => RefObject<T>;
|
|
85
|
+
|
|
86
|
+
class GemCSSStyleSheet extends CSSStyleSheet {
|
|
87
|
+
#ele?: HTMLStyleElement;
|
|
88
|
+
|
|
89
|
+
cssText = '';
|
|
90
|
+
get element() {
|
|
91
|
+
if (!this.#ele) {
|
|
92
|
+
this.#ele = document.createElement('style');
|
|
93
|
+
this.#ele.textContent = this.cssText;
|
|
94
|
+
this.#ele.dataset.gemStyle = '';
|
|
95
|
+
}
|
|
96
|
+
return this.#ele;
|
|
97
|
+
}
|
|
98
|
+
replaceSync(text: string) {
|
|
99
|
+
this.cssText = text;
|
|
100
|
+
if (this.#ele) this.#ele.textContent = text;
|
|
101
|
+
super.replaceSync(text);
|
|
102
|
+
}
|
|
50
103
|
}
|
|
51
104
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
105
|
+
class GemCSSSheet {
|
|
106
|
+
#content = '';
|
|
107
|
+
#media = '';
|
|
108
|
+
constructor(media = '') {
|
|
109
|
+
this.#media = media;
|
|
110
|
+
}
|
|
111
|
+
setContent(v: string) {
|
|
112
|
+
this.#content = v;
|
|
60
113
|
}
|
|
114
|
+
|
|
115
|
+
// 不需要 GC
|
|
116
|
+
#record = new Map<any, GemCSSStyleSheet>();
|
|
117
|
+
#used = new Map<GemCSSStyleSheet, string>();
|
|
118
|
+
getStyle(host?: HTMLElement) {
|
|
119
|
+
const isLight = host && !(host as GemElement).internals?.shadowRoot;
|
|
120
|
+
|
|
121
|
+
// 对同一类 dom 只使用同一个样式表
|
|
122
|
+
const key = isLight ? host.constructor : this;
|
|
123
|
+
if (!this.#record.has(key)) {
|
|
124
|
+
const sheet = new GemCSSStyleSheet({ media: this.#media });
|
|
125
|
+
this.#record.set(key, sheet);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const sheet = this.#record.get(key)!;
|
|
129
|
+
|
|
130
|
+
// 只执行一次
|
|
131
|
+
if (!this.#used.has(sheet)) {
|
|
132
|
+
let style = this.#content;
|
|
133
|
+
let scope = '';
|
|
134
|
+
if (isLight) {
|
|
135
|
+
// light dom 嵌套时需要选择子元素
|
|
136
|
+
// `> *` 实际上是多范围?是否存在性能问题
|
|
137
|
+
scope = `@scope (${host.tagName}) to (:state(${BoundaryCSSState}) > *)`;
|
|
138
|
+
// 不能使用 @layer,两个 @layer 不能覆盖,只有顺序起作用
|
|
139
|
+
// 所以外部不能通过元素名称选择器来覆盖样式,除非样式在之前插入(会自动反转应用到尾部, see `appleCSSStyleSheet`)
|
|
140
|
+
style = `${scope}{ ${style} }`;
|
|
141
|
+
}
|
|
142
|
+
sheet.replaceSync(style);
|
|
143
|
+
this.#used.set(sheet, scope);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return sheet;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 一般用于主题更新,不支持 layer
|
|
150
|
+
updateStyle() {
|
|
151
|
+
this.#used.forEach((scope, sheet) => {
|
|
152
|
+
sheet.replaceSync(scope ? `${scope}{${this.#content}}` : this.#content);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export type Sheet<T> = { [P in keyof T]: P } & { [SheetToken]: GemCSSSheet };
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
*
|
|
161
|
+
* 创建 style sheet 用于 `@adoptedStyle`,不支持样式更新,只支持自定义 CSS 属性
|
|
162
|
+
*/
|
|
163
|
+
export function createCSSSheet<T extends Record<string, string>>(media: string, rules: T | string): Sheet<T>;
|
|
164
|
+
export function createCSSSheet<T extends Record<string, string>>(rules: T | string): Sheet<T>;
|
|
165
|
+
export function createCSSSheet<T extends Record<string, string>>(
|
|
166
|
+
mediaOrRules: T | string,
|
|
167
|
+
rulesValue?: T | string,
|
|
168
|
+
): Sheet<T> {
|
|
169
|
+
const media = rulesValue ? (mediaOrRules as string) : '';
|
|
170
|
+
const rules = rulesValue || mediaOrRules;
|
|
171
|
+
const styleSheet = new GemCSSSheet(media);
|
|
172
|
+
const sheet: any = { [SheetToken]: styleSheet };
|
|
173
|
+
let style = '';
|
|
174
|
+
if (typeof rules === 'string') {
|
|
175
|
+
style = rules;
|
|
176
|
+
} else {
|
|
177
|
+
Object.keys(rules).forEach((key) => {
|
|
178
|
+
const isScope = key === '$';
|
|
179
|
+
// 对于已经有 `-` 的保留原始 key,支持覆盖修改
|
|
180
|
+
sheet[key] = isScope || key.includes('-') ? key : `${key}-${randomStr()}`;
|
|
181
|
+
style += `${isScope ? ':scope,:host' : `.${sheet[key]}`} {${rules[key]}}`;
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
styleSheet.setContent(style);
|
|
185
|
+
return sheet as Sheet<T>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const updateStyleSheets = (map: Map<CSSStyleSheet, number>, sheets: CSSStyleSheet[], value: number) => {
|
|
189
|
+
let needUpdate = false;
|
|
190
|
+
sheets.forEach((e) => {
|
|
191
|
+
const count = map.get(e) || 0;
|
|
192
|
+
needUpdate ||= (value === 1 && count === 0) || (value === -1 && count === 1);
|
|
193
|
+
// count 为 0 时不立即删除,以便更新时识别是否外部样式
|
|
194
|
+
// 但是只要挂载 document 的样式表就不会被 GC 了, 是否要在闲时 GC?
|
|
195
|
+
map.set(e, count + value);
|
|
196
|
+
});
|
|
197
|
+
if (needUpdate) {
|
|
198
|
+
// 避免重复更新,例如 list 元素增删,全 light dom 时 document 更新
|
|
199
|
+
addMicrotask(rootUpdateFnMap.get(map)!);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const appleCSSStyleSheet = (ele: HTMLElement, sheets: CSSStyleSheet[]) => {
|
|
204
|
+
const root = ele.getRootNode() as ShadowRoot | Document;
|
|
205
|
+
if (!rootStyleSheetInfo.has(root)) {
|
|
206
|
+
const map = new Map<CSSStyleSheet, number>();
|
|
207
|
+
rootStyleSheetInfo.set(root, map);
|
|
208
|
+
rootUpdateFnMap.set(map, () => {
|
|
209
|
+
// 先找到外部样式表
|
|
210
|
+
const newSheets = root.adoptedStyleSheets.filter((e) => !map.has(e));
|
|
211
|
+
map.forEach((count, sheet) => count && newSheets.push(sheet));
|
|
212
|
+
// 外层元素的样式要放到最后,以提升优先级,但是只考虑第一次出现样式表的位置
|
|
213
|
+
root.adoptedStyleSheets = newSheets.reverse();
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const map = rootStyleSheetInfo.get(root)!;
|
|
218
|
+
|
|
219
|
+
updateStyleSheets(map, sheets, 1);
|
|
220
|
+
return () => updateStyleSheets(map, sheets, -1);
|
|
61
221
|
};
|
|
62
|
-
|
|
222
|
+
|
|
223
|
+
/**必须使用在字段中,否则会读取到错误的实例 */
|
|
224
|
+
export let createState: <T>(initState: T) => T & ((state: Partial<T>) => void);
|
|
63
225
|
|
|
64
226
|
type GetDepFun<T> = () => T;
|
|
65
227
|
type EffectCallback<T> = (depValues: T, oldDepValues?: T) => any;
|
|
66
228
|
type EffectItem<T> = {
|
|
67
229
|
callback: EffectCallback<T>;
|
|
68
230
|
initialized?: boolean;
|
|
69
|
-
|
|
231
|
+
fixed?: boolean; // 不需要清理,只会添加一次
|
|
70
232
|
values?: T;
|
|
71
233
|
getDep?: GetDepFun<T>;
|
|
72
234
|
preCallback?: () => void;
|
|
73
235
|
};
|
|
74
236
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
237
|
+
function execEffectList(list?: EffectItem<any>[]) {
|
|
238
|
+
list?.forEach((effectItem) => {
|
|
239
|
+
const { callback, getDep, values, preCallback } = effectItem;
|
|
240
|
+
const newValues = getDep?.();
|
|
241
|
+
if (!getDep || !values || isArrayChange(values, newValues)) {
|
|
242
|
+
execCallback(preCallback);
|
|
243
|
+
effectItem.preCallback = callback(newValues, values);
|
|
244
|
+
effectItem.values = newValues;
|
|
245
|
+
// 更新时也要设置,避免异步的 `#initEffect` 重复执行
|
|
246
|
+
effectItem.initialized = true;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
78
250
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
251
|
+
function clearEffect(list: EffectItem<any>[]) {
|
|
252
|
+
return list.filter((e) => {
|
|
253
|
+
execCallback(e.preCallback);
|
|
254
|
+
e.initialized = false;
|
|
255
|
+
e.values = undefined;
|
|
256
|
+
return e.fixed;
|
|
257
|
+
});
|
|
83
258
|
}
|
|
84
259
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
260
|
+
type Render = () => TemplateResult | null | undefined;
|
|
261
|
+
type RenderItem = { render: Render; condition?: () => boolean };
|
|
262
|
+
|
|
263
|
+
export let createTemplate: (ele: GemElement, item: RenderItem) => void;
|
|
89
264
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
265
|
+
export type Metadata = Partial<ShadowRootInit> & {
|
|
266
|
+
noBlocking?: boolean;
|
|
267
|
+
aria?: Partial<
|
|
268
|
+
ARIAMixin & {
|
|
269
|
+
/**自动添加 tabIndex;支持 `disabled` 属性 */
|
|
270
|
+
focusable: boolean;
|
|
271
|
+
}
|
|
272
|
+
>;
|
|
273
|
+
// 实例化时使用到,DevTools 需要读取
|
|
274
|
+
observedStores: Store<unknown>[];
|
|
275
|
+
adoptedStyleSheets?: Sheet<unknown>[];
|
|
100
276
|
// 以下静态字段仅供外部读取,没有实际作用
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
277
|
+
observedProperties?: string[];
|
|
278
|
+
observedAttributes?: string[];
|
|
279
|
+
definedEvents?: string[];
|
|
280
|
+
definedCSSStates?: string[];
|
|
281
|
+
definedParts?: string[];
|
|
282
|
+
definedSlots?: string[];
|
|
283
|
+
/**@deprecated GemDevtools 在使用*/
|
|
284
|
+
definedRefs?: string[];
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// global render task pool
|
|
288
|
+
const noBlockingTaskList = new LinkedList<() => void>();
|
|
289
|
+
const tick = (timeStamp = performance.now()) => {
|
|
290
|
+
if (performance.now() > timeStamp + 16) return requestAnimationFrame(tick);
|
|
291
|
+
const task = noBlockingTaskList.get();
|
|
292
|
+
if (task) {
|
|
293
|
+
task();
|
|
294
|
+
tick(timeStamp);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
noBlockingTaskList.addEventListener('start', () => addMicrotask(tick));
|
|
298
|
+
|
|
299
|
+
let currentConstructGemElement: GemElement;
|
|
105
300
|
|
|
106
|
-
|
|
107
|
-
|
|
301
|
+
export abstract class GemElement extends HTMLElement {
|
|
302
|
+
// 禁止覆盖自定义元素原生生命周期方法
|
|
303
|
+
// https://github.com/microsoft/TypeScript/issues/21388#issuecomment-934345226
|
|
304
|
+
static #final = Symbol();
|
|
108
305
|
|
|
109
306
|
#renderRoot: HTMLElement | ShadowRoot;
|
|
110
|
-
#internals
|
|
307
|
+
#internals: ElementInternals & {
|
|
308
|
+
// 有别于 adoptedStyleSheets,用来定义单个实例样式
|
|
309
|
+
sheets: GemCSSStyleSheet[];
|
|
310
|
+
stateList: ReturnType<typeof createState>[];
|
|
311
|
+
};
|
|
312
|
+
#effectList: EffectItem<any>[] = [];
|
|
313
|
+
#memoList: EffectItem<any>[] = [];
|
|
314
|
+
#renderList: RenderItem[] = [];
|
|
111
315
|
#isAppendReason?: boolean;
|
|
112
|
-
// 和 isConnected 有区别
|
|
113
316
|
#isMounted?: boolean;
|
|
114
|
-
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
317
|
+
// not in constructor 的近似值
|
|
318
|
+
#isConnected?: boolean;
|
|
319
|
+
#rendering?: boolean;
|
|
320
|
+
#clearStyle?: any;
|
|
118
321
|
|
|
119
|
-
|
|
322
|
+
[updateTokenAlias]() {
|
|
323
|
+
// 避免 `connectedCallback` 中的 property 赋值造成多余更新
|
|
324
|
+
if (this.#isMounted) {
|
|
325
|
+
addMicrotask(this.#update);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
static {
|
|
330
|
+
createState = <T>(initState: T) => {
|
|
331
|
+
const ele = currentConstructGemElement;
|
|
332
|
+
const state: any = (payload: Partial<T>) => {
|
|
333
|
+
assign(state, payload);
|
|
334
|
+
// 避免无限刷新
|
|
335
|
+
if (!ele.#rendering) addMicrotask(ele.#update);
|
|
336
|
+
};
|
|
337
|
+
setPrototypeOf(state, null);
|
|
338
|
+
delete state.name;
|
|
339
|
+
delete state.length;
|
|
340
|
+
ele.#internals.stateList.push(state);
|
|
341
|
+
assign(state, initState);
|
|
342
|
+
return state as T & ((this: GemElement, state: Partial<T>) => void);
|
|
343
|
+
};
|
|
344
|
+
createRef = () => new RefObject(currentConstructGemElement);
|
|
345
|
+
createTemplate = (ele, item) => ele.#renderList.push(item);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
constructor() {
|
|
120
349
|
super();
|
|
121
350
|
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
351
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
352
|
+
currentConstructGemElement = this;
|
|
353
|
+
|
|
354
|
+
const { mode, serializable, delegatesFocus, slotAssignment, aria } = this.#metadata;
|
|
355
|
+
const { focusable, ...internalsAria } = aria || {};
|
|
356
|
+
|
|
357
|
+
this.#renderRoot = !mode ? this : this.attachShadow({ mode, serializable, delegatesFocus, slotAssignment });
|
|
358
|
+
this.#internals = this.attachInternals() as GemElement['internals'];
|
|
359
|
+
this.#internals.stateList = [];
|
|
360
|
+
this.#internals.sheets = [];
|
|
131
361
|
|
|
132
|
-
this.#
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
delegatesFocus: options.delegatesFocus,
|
|
138
|
-
slotAssignment: options.slotAssignment,
|
|
139
|
-
});
|
|
362
|
+
assign(this.#internals, internalsAria);
|
|
363
|
+
|
|
364
|
+
// light dom 添加边界,防止内容被外部样式化
|
|
365
|
+
// 特殊情况下需要手动 `delete` 边界,以应该外部样式
|
|
366
|
+
if (!mode) this.#internals.states.add(BoundaryCSSState);
|
|
140
367
|
|
|
141
368
|
// https://stackoverflow.com/questions/43836886/failed-to-construct-customelement-error-when-javascript-file-is-placed-in-head
|
|
142
369
|
// focusable 元素一般同时具备 disabled 属性
|
|
143
370
|
// 和原生元素行为保持一致,disabled 时不触发 click 事件
|
|
144
371
|
let hasInitTabIndex: boolean | undefined;
|
|
145
|
-
this.
|
|
146
|
-
|
|
372
|
+
this.#effectList.push({
|
|
373
|
+
fixed: true,
|
|
374
|
+
getDep: () => [(this as any).disabled],
|
|
375
|
+
callback: ([disabled = false]) => {
|
|
147
376
|
if (hasInitTabIndex === undefined) hasInitTabIndex = this.hasAttribute('tabindex');
|
|
148
|
-
|
|
149
|
-
this.
|
|
150
|
-
|
|
151
|
-
if (options.focusable && !hasInitTabIndex) {
|
|
152
|
-
this.tabIndex = -Number(disabled);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if ((options.focusable || options.delegatesFocus) && disabled) {
|
|
377
|
+
this.#internals.ariaDisabled = String(disabled);
|
|
378
|
+
if (focusable && !hasInitTabIndex) this.tabIndex = -Number(disabled);
|
|
379
|
+
if ((focusable || delegatesFocus) && disabled) {
|
|
156
380
|
return addListener(this, 'click', (e: Event) => e.isTrusted && e.stopImmediatePropagation(), {
|
|
157
381
|
capture: true,
|
|
158
382
|
});
|
|
159
383
|
}
|
|
160
384
|
},
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.effect(
|
|
171
|
-
() => {
|
|
172
|
-
const root = this.getRootNode() as ShadowRoot | Document;
|
|
173
|
-
root.adoptedStyleSheets = [...root.adoptedStyleSheets, ...sheets];
|
|
174
|
-
return () => {
|
|
175
|
-
root.adoptedStyleSheets = removeItems(root.adoptedStyleSheets, sheets);
|
|
176
|
-
};
|
|
177
|
-
},
|
|
178
|
-
() => [],
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
get #metadata(): Metadata {
|
|
389
|
+
return (this.constructor as any)[Symbol.metadata];
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
get #renderItem() {
|
|
393
|
+
return this.#renderList.find((e) => !e.condition || e.condition());
|
|
182
394
|
}
|
|
183
395
|
|
|
184
396
|
get internals() {
|
|
185
|
-
if (!this.#internals) {
|
|
186
|
-
this.#internals = this.attachInternals();
|
|
187
|
-
// https://groups.google.com/a/chromium.org/g/blink-dev/c/JvpHoUfhJYE?pli=1
|
|
188
|
-
// https://bugs.webkit.org/show_bug.cgi?id=215911
|
|
189
|
-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1588763
|
|
190
|
-
try {
|
|
191
|
-
this.#internals.states.add('foo');
|
|
192
|
-
this.#internals.states.delete('foo');
|
|
193
|
-
} catch {
|
|
194
|
-
Reflect.defineProperty(this.#internals, 'states', {
|
|
195
|
-
value: {
|
|
196
|
-
has: (v: string) => kebabToCamelCase(v) in this.dataset,
|
|
197
|
-
add: (v: string) => (this.dataset[kebabToCamelCase(v)] = ''),
|
|
198
|
-
delete: (v: string) => delete this.dataset[kebabToCamelCase(v)],
|
|
199
|
-
},
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
397
|
return this.#internals;
|
|
204
398
|
}
|
|
205
399
|
|
|
206
400
|
/**
|
|
207
|
-
*
|
|
208
|
-
* 设置元素 state,会触发更新
|
|
209
|
-
*
|
|
210
|
-
* ```js
|
|
211
|
-
* class App extends GemElement {
|
|
212
|
-
* click() {
|
|
213
|
-
* this.setState({});
|
|
214
|
-
* }
|
|
215
|
-
* }
|
|
216
|
-
* ```
|
|
401
|
+
* 每次更新完检查依赖,执行对应的副作用回调
|
|
217
402
|
* */
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
Object.assign(this.state, payload);
|
|
221
|
-
addMicrotask(this.#update);
|
|
403
|
+
#execEffect = () => {
|
|
404
|
+
execEffectList(this.#effectList);
|
|
222
405
|
};
|
|
223
406
|
|
|
224
|
-
#
|
|
225
|
-
|
|
226
|
-
const { callback, getDep, values, preCallback } = effectItem;
|
|
227
|
-
const newValues = getDep?.();
|
|
228
|
-
if (!getDep || !values || isArrayChange(values, newValues)) {
|
|
229
|
-
execCallback(preCallback);
|
|
230
|
-
effectItem.preCallback = callback(newValues, values);
|
|
231
|
-
effectItem.values = newValues;
|
|
232
|
-
}
|
|
233
|
-
});
|
|
407
|
+
#execMemo = () => {
|
|
408
|
+
execEffectList(this.#memoList);
|
|
234
409
|
};
|
|
235
410
|
|
|
236
411
|
/**
|
|
237
|
-
*
|
|
412
|
+
* - 条件渲染
|
|
413
|
+
* - 不提供 `render` 时显示子内容
|
|
414
|
+
* - 返回 `null` 时渲染空的子内容
|
|
415
|
+
* - 返回 `undefined` 时不会更新现有内容
|
|
238
416
|
* */
|
|
239
|
-
#
|
|
240
|
-
|
|
417
|
+
#render = (item?: RenderItem) => {
|
|
418
|
+
try {
|
|
419
|
+
this.#rendering = true;
|
|
420
|
+
this.#execMemo();
|
|
421
|
+
const isLight = this.#renderRoot === this;
|
|
422
|
+
const temp = item ? item.render() : isLight ? undefined : html`<slot></slot>`;
|
|
423
|
+
this.#rendering = false;
|
|
424
|
+
if (temp === undefined) return;
|
|
425
|
+
render(temp, this.#renderRoot);
|
|
426
|
+
} catch (err) {
|
|
427
|
+
this.dispatchEvent(new CustomEvent(RenderErrorEvent, { bubbles: true, composed: true, detail: err }));
|
|
428
|
+
}
|
|
241
429
|
};
|
|
242
430
|
|
|
243
|
-
#
|
|
244
|
-
this.#
|
|
431
|
+
#updateCallback = () => {
|
|
432
|
+
const item = this.#renderItem;
|
|
433
|
+
if (
|
|
434
|
+
this.#isMounted &&
|
|
435
|
+
// 当有渲染函数,但是没一个符合条件时跳过
|
|
436
|
+
(item || !this.#renderList.length)
|
|
437
|
+
) {
|
|
438
|
+
this.#render(item);
|
|
439
|
+
addMicrotask(this.#execEffect);
|
|
440
|
+
}
|
|
245
441
|
};
|
|
246
442
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
*
|
|
253
|
-
* ```js
|
|
254
|
-
* class App extends GemElement {
|
|
255
|
-
* mounted() {
|
|
256
|
-
* this.effect(callback, () => [this.attrName]);
|
|
257
|
-
* }
|
|
258
|
-
* }
|
|
259
|
-
* ```
|
|
260
|
-
* */
|
|
261
|
-
effect = <T = any[] | undefined>(callback: EffectCallback<T>, getDep?: T extends any[] ? () => T : undefined) => {
|
|
262
|
-
if (!this.#effectList) this.#effectList = [];
|
|
263
|
-
const effectItem: EffectItem<T> = {
|
|
264
|
-
callback,
|
|
265
|
-
getDep,
|
|
266
|
-
initialized: this.#isMounted,
|
|
267
|
-
inConstructor: (this as any)[constructorSymbol],
|
|
268
|
-
};
|
|
269
|
-
// 以挂载时立即执行副作用,未挂载时等挂载后执行
|
|
270
|
-
if (this.#isMounted) {
|
|
271
|
-
effectItem.values = getDep?.() as T;
|
|
272
|
-
effectItem.preCallback = callback(effectItem.values);
|
|
443
|
+
#update = () => {
|
|
444
|
+
if (this.#metadata.noBlocking) {
|
|
445
|
+
noBlockingTaskList.add(this.#updateCallback);
|
|
446
|
+
} else {
|
|
447
|
+
this.#updateCallback();
|
|
273
448
|
}
|
|
274
|
-
this.#effectList.push(effectItem);
|
|
275
449
|
};
|
|
276
450
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
* }), () => [this.attrName]);
|
|
289
|
-
* }
|
|
290
|
-
* }
|
|
291
|
-
* ```
|
|
292
|
-
* */
|
|
293
|
-
memo = <T = any[] | undefined>(callback: EffectCallback<T>, getDep?: T extends any[] ? () => T : undefined) => {
|
|
294
|
-
if (!this.#memoList) this.#memoList = [];
|
|
295
|
-
this.#memoList.push({
|
|
296
|
-
callback,
|
|
297
|
-
getDep,
|
|
298
|
-
inConstructor: (this as any)[constructorSymbol],
|
|
299
|
-
});
|
|
451
|
+
#prepareStyle = () => {
|
|
452
|
+
const { adoptedStyleSheets = [] } = this.#metadata;
|
|
453
|
+
const { shadowRoot, sheets } = this.#internals;
|
|
454
|
+
|
|
455
|
+
const clsSheets = adoptedStyleSheets.map((item) => item[SheetToken].getStyle(this));
|
|
456
|
+
if (shadowRoot) {
|
|
457
|
+
shadowRoot.adoptedStyleSheets = clsSheets.concat(sheets);
|
|
458
|
+
} else {
|
|
459
|
+
this.prepend(...sheets.map((e) => e.element));
|
|
460
|
+
return appleCSSStyleSheet(this, clsSheets);
|
|
461
|
+
}
|
|
300
462
|
};
|
|
301
463
|
|
|
302
464
|
/**
|
|
@@ -313,133 +475,54 @@ export abstract class GemElement<T = Record<string, unknown>> extends HTMLElemen
|
|
|
313
475
|
});
|
|
314
476
|
};
|
|
315
477
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
render(temp, this.#renderRoot);
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
/**@lifecycle */
|
|
337
|
-
mounted?(): void | (() => void) | Promise<void>;
|
|
338
|
-
|
|
339
|
-
/**@lifecycle */
|
|
340
|
-
shouldUpdate?(): boolean;
|
|
341
|
-
#shouldUpdate = () => {
|
|
342
|
-
return this.shouldUpdate ? this.shouldUpdate() : true;
|
|
478
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ willMount?(): void | Promise<void>;
|
|
479
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ mounted?(): void | (() => void) | Promise<void>;
|
|
480
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ updated?(): void | Promise<void>;
|
|
481
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ unmounted?(): void | Promise<void>;
|
|
482
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ shouldUpdate?(): boolean;
|
|
483
|
+
/** @lifecycle 将来会彻底移除,现在做兼容实现*/ render?(): TemplateResult | null | undefined;
|
|
484
|
+
#compat = () => {
|
|
485
|
+
if (this.willMount) this.memo(this.willMount.bind(this), () => []);
|
|
486
|
+
if (this.mounted) this.effect(this.mounted.bind(this), () => []);
|
|
487
|
+
if (this.updated) this.effect((_, old) => old && this.updated!.apply(this));
|
|
488
|
+
if (this.unmounted)
|
|
489
|
+
this.effect(
|
|
490
|
+
() => this.unmounted!.bind(this),
|
|
491
|
+
() => [],
|
|
492
|
+
);
|
|
493
|
+
if (!this.#renderList.length && this.render)
|
|
494
|
+
this.#renderList.push({ render: this.render.bind(this), condition: this.shouldUpdate?.bind(this) });
|
|
343
495
|
};
|
|
344
496
|
|
|
345
|
-
#updateCallback = () => {
|
|
346
|
-
if (this.#isMounted && this.#shouldUpdate()) {
|
|
347
|
-
this.#render();
|
|
348
|
-
addMicrotask(this.#updated);
|
|
349
|
-
addMicrotask(this.#execEffect);
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
#update = () => {
|
|
354
|
-
if (this.#isAsync) {
|
|
355
|
-
asyncRenderTaskList.add(this.#updateCallback);
|
|
356
|
-
} else {
|
|
357
|
-
this.#updateCallback();
|
|
358
|
-
}
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* @helper
|
|
363
|
-
* async
|
|
364
|
-
*/
|
|
365
|
-
update = () => {
|
|
366
|
-
addMicrotask(this.#update);
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
/**@lifecycle */
|
|
370
|
-
updated?(): void | Promise<void>;
|
|
371
|
-
#updated = () => {
|
|
372
|
-
this.updated?.();
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @lifecycle
|
|
377
|
-
*
|
|
378
|
-
* 卸载元素,但不一定会被垃圾回收,所以事件监听器需要手动清除,以免重复注册
|
|
379
|
-
*/
|
|
380
|
-
unmounted?(): void | Promise<void>;
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* @private
|
|
384
|
-
* @final
|
|
385
|
-
* use `effect`
|
|
386
|
-
*/
|
|
387
|
-
attributeChangedCallback() {
|
|
388
|
-
if (this.#isMounted) {
|
|
389
|
-
addMicrotask(this.#update);
|
|
390
|
-
}
|
|
391
|
-
return GemElement.#final;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
497
|
#disconnectStore?: (() => void)[];
|
|
395
|
-
#connectedCallback = () => {
|
|
498
|
+
#connectedCallback = async () => {
|
|
396
499
|
if (this.#isAppendReason) {
|
|
397
500
|
this.#isAppendReason = false;
|
|
398
501
|
return;
|
|
399
502
|
}
|
|
503
|
+
this.#compat();
|
|
400
504
|
|
|
401
|
-
|
|
402
|
-
Reflect.set(this, constructorSymbol, false);
|
|
505
|
+
const { observedStores } = this.#metadata;
|
|
403
506
|
|
|
404
|
-
this
|
|
405
|
-
const { observedStores, rootElement } = this.constructor as typeof GemElement;
|
|
507
|
+
this.#isConnected = true;
|
|
406
508
|
this.#disconnectStore = observedStores?.map((store) => connect(store, this.#update));
|
|
407
|
-
this.#render();
|
|
509
|
+
this.#render(this.#renderItem);
|
|
408
510
|
this.#isMounted = true;
|
|
409
|
-
this.#
|
|
511
|
+
this.#clearStyle = this.#prepareStyle();
|
|
512
|
+
// 等待所有元素的样式被应用,再执行回调
|
|
513
|
+
// 这让 mounted 和 effect 回调和其他回调一样保持一样的异步行为
|
|
514
|
+
await Promise.resolve();
|
|
410
515
|
this.#initEffect();
|
|
411
|
-
if (
|
|
412
|
-
rootElement &&
|
|
413
|
-
this.isConnected &&
|
|
414
|
-
(this.getRootNode() as ShadowRoot).host?.tagName !== rootElement.toUpperCase()
|
|
415
|
-
) {
|
|
416
|
-
throw new GemError(`not allow current root type`);
|
|
417
|
-
}
|
|
418
516
|
};
|
|
419
517
|
|
|
420
|
-
closestElement<K extends keyof HTMLElementTagNameMap>(tag: K): HTMLElementTagNameMap[K] | null;
|
|
421
|
-
closestElement<K extends abstract new (...args: any) => any>(constructor: K): InstanceType<K> | null;
|
|
422
|
-
closestElement<K extends abstract new (...args: any) => any>(constructorOrTag: K | string) {
|
|
423
|
-
const isConstructor = typeof constructorOrTag === 'function';
|
|
424
|
-
const tagName = typeof constructorOrTag === 'string' && constructorOrTag.toUpperCase();
|
|
425
|
-
const is = (ele: Element) => (isConstructor ? ele.constructor === constructorOrTag : ele.tagName === tagName);
|
|
426
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
427
|
-
let node: Element | null = this;
|
|
428
|
-
while (node) {
|
|
429
|
-
if (is(node)) break;
|
|
430
|
-
node = node.parentElement || (node.getRootNode() as ShadowRoot).host;
|
|
431
|
-
}
|
|
432
|
-
return node;
|
|
433
|
-
}
|
|
434
|
-
|
|
435
518
|
/**
|
|
436
519
|
* @private
|
|
437
520
|
* @final
|
|
438
|
-
* use `mounted
|
|
521
|
+
* use `mounted`
|
|
439
522
|
*/
|
|
440
523
|
connectedCallback() {
|
|
441
|
-
if (this.
|
|
442
|
-
|
|
524
|
+
if (this.#metadata.noBlocking) {
|
|
525
|
+
noBlockingTaskList.add(this.#connectedCallback);
|
|
443
526
|
} else {
|
|
444
527
|
this.#connectedCallback();
|
|
445
528
|
}
|
|
@@ -464,184 +547,74 @@ export abstract class GemElement<T = Record<string, unknown>> extends HTMLElemen
|
|
|
464
547
|
this.#isAppendReason = true;
|
|
465
548
|
return;
|
|
466
549
|
}
|
|
550
|
+
noBlockingTaskList.delete(this.#connectedCallback);
|
|
551
|
+
noBlockingTaskList.delete(this.#updateCallback);
|
|
467
552
|
this.#isMounted = false;
|
|
468
553
|
this.#disconnectStore?.forEach((disconnect) => disconnect());
|
|
469
|
-
|
|
470
|
-
this
|
|
471
|
-
this.#
|
|
472
|
-
|
|
554
|
+
// 是否要异步执行回调?
|
|
555
|
+
this.#effectList = clearEffect(this.#effectList);
|
|
556
|
+
this.#memoList = clearEffect(this.#memoList);
|
|
557
|
+
execCallback(this.#clearStyle);
|
|
473
558
|
return GemElement.#final;
|
|
474
559
|
}
|
|
475
560
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
561
|
+
/**
|
|
562
|
+
* @helper
|
|
563
|
+
* 记录副作用回调和值;
|
|
564
|
+
* 回调到返回值如果是函数将再卸载时执行;
|
|
565
|
+
* 第一次执行时 `oldDeps` 为空;
|
|
566
|
+
*
|
|
567
|
+
* ```js
|
|
568
|
+
* class App extends GemElement {
|
|
569
|
+
* mounted() {
|
|
570
|
+
* this.effect(callback, () => [this.attrName]);
|
|
571
|
+
* }
|
|
572
|
+
* }
|
|
573
|
+
* ```
|
|
574
|
+
* */
|
|
575
|
+
effect = <K = any[] | undefined>(callback: EffectCallback<K>, getDep?: K extends any[] ? () => K : undefined) => {
|
|
576
|
+
const effectItem: EffectItem<K> = {
|
|
577
|
+
callback,
|
|
578
|
+
getDep,
|
|
579
|
+
initialized: this.#isMounted,
|
|
580
|
+
fixed: !this.#isConnected,
|
|
581
|
+
};
|
|
582
|
+
// 已挂载时立即执行副作用,未挂载时等挂载后执行
|
|
583
|
+
if (this.#isMounted) {
|
|
584
|
+
effectItem.values = getDep?.() as K;
|
|
585
|
+
effectItem.preCallback = callback(effectItem.values);
|
|
586
|
+
}
|
|
587
|
+
this.#effectList.push(effectItem);
|
|
482
588
|
};
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
const gemElementProxyMap = new PropProxyMap<GemElement>();
|
|
486
|
-
|
|
487
|
-
export function defineAttribute(target: GemElement, prop: string, attr: string) {
|
|
488
|
-
const { booleanAttributes, numberAttributes } = target.constructor as typeof GemElement;
|
|
489
|
-
Object.defineProperty(target, prop, {
|
|
490
|
-
configurable: true,
|
|
491
|
-
get() {
|
|
492
|
-
// fix karma test
|
|
493
|
-
// 判断是否是自定义元素实例
|
|
494
|
-
if (!(initSymbol in this)) return;
|
|
495
|
-
const that = this as GemElement;
|
|
496
|
-
// 不能从 proxy 对象中读取值
|
|
497
|
-
const value = that.getAttribute(attr);
|
|
498
|
-
if (booleanAttributes?.has(attr)) {
|
|
499
|
-
return value === null ? false : true;
|
|
500
|
-
}
|
|
501
|
-
if (numberAttributes?.has(attr)) {
|
|
502
|
-
return Number(value);
|
|
503
|
-
}
|
|
504
|
-
return value || '';
|
|
505
|
-
},
|
|
506
|
-
set(v: string | null | undefined | number | boolean) {
|
|
507
|
-
const that = this as GemElement;
|
|
508
|
-
const proxy = gemElementProxyMap.get(this);
|
|
509
|
-
const hasSet = proxy[prop];
|
|
510
|
-
const value = that.getAttribute(attr);
|
|
511
|
-
// https://github.com/whatwg/dom/issues/922
|
|
512
|
-
if (this[initSymbol] && value !== null && !hasSet) return;
|
|
513
|
-
// 字段和构造函数中都有对 attr 设置时会执行多次
|
|
514
|
-
// Firefox WebConsole 中不知道为什么在构造函数中 this[initSymbol] 已经为 false
|
|
515
|
-
proxy[prop] = true;
|
|
516
|
-
const isBool = booleanAttributes?.has(attr);
|
|
517
|
-
if (v === null || v === undefined) {
|
|
518
|
-
that.removeAttribute(attr);
|
|
519
|
-
} else if (isBool) {
|
|
520
|
-
// 当 attr 存在且 !!v 为 true 时,toggleAttribute 不会造成 Attribute 改变
|
|
521
|
-
that.toggleAttribute(attr, !!v);
|
|
522
|
-
} else {
|
|
523
|
-
if (value !== String(v)) that.setAttribute(attr, String(v));
|
|
524
|
-
}
|
|
525
|
-
},
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
const isEventHandleSymbol = Symbol('event handle');
|
|
530
|
-
export function defineProperty(
|
|
531
|
-
target: GemElement,
|
|
532
|
-
prop: string,
|
|
533
|
-
event?: string,
|
|
534
|
-
eventOptions?: Omit<CustomEventInit<unknown>, 'detail'>,
|
|
535
|
-
) {
|
|
536
|
-
Object.defineProperty(target, prop, {
|
|
537
|
-
configurable: true,
|
|
538
|
-
get() {
|
|
539
|
-
const value = gemElementProxyMap.get(this)[prop];
|
|
540
|
-
if (value || !event) {
|
|
541
|
-
return value;
|
|
542
|
-
} else {
|
|
543
|
-
this[prop] = emptyFunction;
|
|
544
|
-
return this[prop];
|
|
545
|
-
}
|
|
546
|
-
},
|
|
547
|
-
set(v) {
|
|
548
|
-
const that = this as GemElement;
|
|
549
|
-
const proxy = gemElementProxyMap.get(that);
|
|
550
|
-
if (v !== proxy[prop]) {
|
|
551
|
-
if (event) {
|
|
552
|
-
proxy[prop] = v?.[isEventHandleSymbol]
|
|
553
|
-
? v
|
|
554
|
-
: (detail: any, options: any) => {
|
|
555
|
-
const evt = new CustomEvent(event, { ...options, ...eventOptions, detail });
|
|
556
|
-
that.dispatchEvent(evt);
|
|
557
|
-
v(detail, options);
|
|
558
|
-
};
|
|
559
|
-
Reflect.set(proxy[prop]!, isEventHandleSymbol, true);
|
|
560
|
-
// emitter 不触发元素更新
|
|
561
|
-
} else {
|
|
562
|
-
proxy[prop] = v;
|
|
563
|
-
this[updateSymbol]();
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
},
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
const getReflectTargets = (ele: ShadowRoot | GemElement) =>
|
|
571
|
-
[...ele.querySelectorAll<GemReflectElement>('[data-gem-reflect]')].map((e) => e.target);
|
|
572
589
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
proxy[prop] = refobject;
|
|
598
|
-
}
|
|
599
|
-
return refobject;
|
|
600
|
-
},
|
|
601
|
-
});
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
export function defineCSSState(target: GemElement, prop: string, state: string) {
|
|
605
|
-
Object.defineProperty(target, prop, {
|
|
606
|
-
configurable: true,
|
|
607
|
-
get() {
|
|
608
|
-
const that = this as GemElement;
|
|
609
|
-
const { states } = that.internals;
|
|
610
|
-
return states?.has(state);
|
|
611
|
-
},
|
|
612
|
-
set(v: boolean) {
|
|
613
|
-
const that = this as GemElement;
|
|
614
|
-
const { states } = that.internals;
|
|
615
|
-
if (v) {
|
|
616
|
-
states?.add(state);
|
|
617
|
-
} else {
|
|
618
|
-
states?.delete(state);
|
|
619
|
-
}
|
|
620
|
-
},
|
|
621
|
-
});
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
export const nativeDefineElement = customElements.define.bind(customElements);
|
|
625
|
-
customElements.define = (name: string, cls: CustomElementConstructor, options?: ElementDefinitionOptions) => {
|
|
626
|
-
if (cls.prototype instanceof GemElement) {
|
|
627
|
-
const { observedAttributes, observedProperties, defineEvents, defineCSSStates, defineRefs } =
|
|
628
|
-
cls as unknown as typeof GemElement;
|
|
629
|
-
observedAttributes?.forEach((attr) => defineAttribute(cls.prototype, kebabToCamelCase(attr), attr));
|
|
630
|
-
observedProperties?.forEach((prop) => defineProperty(cls.prototype, prop));
|
|
631
|
-
defineEvents?.forEach((event) => defineProperty(cls.prototype, kebabToCamelCase(event), event));
|
|
632
|
-
defineCSSStates?.forEach((state) => defineCSSState(cls.prototype, kebabToCamelCase(state), state));
|
|
633
|
-
defineRefs?.forEach((ref) => defineRef(cls.prototype, kebabToCamelCase(ref), ref));
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
nativeDefineElement(name, cls, options);
|
|
637
|
-
};
|
|
638
|
-
|
|
639
|
-
declare global {
|
|
640
|
-
interface Window {
|
|
641
|
-
__GEM_DEVTOOLS__HOOK__?: (typeof GemExports & typeof VersionExports) | Record<string, never>;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
590
|
+
/**
|
|
591
|
+
* @helper
|
|
592
|
+
* 在 `render` 前执行回调;
|
|
593
|
+
* 和 `effect` 一样接受依赖数组参数,在 `constructor` 中使用;
|
|
594
|
+
* 第一次执行时 `oldDeps` 为空;
|
|
595
|
+
*
|
|
596
|
+
* ```js
|
|
597
|
+
* class App extends GemElement {
|
|
598
|
+
* constructor() {
|
|
599
|
+
* super();
|
|
600
|
+
* this.memo(() => {
|
|
601
|
+
* this.a = exec(this.attrName);
|
|
602
|
+
* }), () => [this.attrName]);
|
|
603
|
+
* }
|
|
604
|
+
* }
|
|
605
|
+
* ```
|
|
606
|
+
* */
|
|
607
|
+
memo = <K = any[] | undefined>(callback: EffectCallback<K>, getDep?: K extends any[] ? () => K : undefined) => {
|
|
608
|
+
this.#memoList.push({
|
|
609
|
+
callback,
|
|
610
|
+
getDep,
|
|
611
|
+
fixed: !this.#isConnected,
|
|
612
|
+
});
|
|
613
|
+
};
|
|
644
614
|
|
|
645
|
-
|
|
646
|
-
|
|
615
|
+
/**
|
|
616
|
+
* @helper
|
|
617
|
+
* async
|
|
618
|
+
*/
|
|
619
|
+
update = () => addMicrotask(this.#update);
|
|
647
620
|
}
|