@dooboostore/simple-web-component 1.0.48 → 1.0.49

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.
Files changed (150) hide show
  1. package/README.md +117 -19
  2. package/dist/cjs/config/config.js +1 -1
  3. package/dist/cjs/config/config.js.map +2 -2
  4. package/dist/cjs/decorators/addEventListener.js +387 -29
  5. package/dist/cjs/decorators/addEventListener.js.map +3 -3
  6. package/dist/cjs/decorators/applyAttribute.js +25 -0
  7. package/dist/cjs/decorators/applyAttribute.js.map +2 -2
  8. package/dist/cjs/decorators/applyClass.js +49 -18
  9. package/dist/cjs/decorators/applyClass.js.map +3 -3
  10. package/dist/cjs/decorators/applyNode.js +67 -1
  11. package/dist/cjs/decorators/applyNode.js.map +3 -3
  12. package/dist/cjs/decorators/applyProperty.js +101 -3
  13. package/dist/cjs/decorators/applyProperty.js.map +2 -2
  14. package/dist/cjs/decorators/applyStyle.js +36 -5
  15. package/dist/cjs/decorators/applyStyle.js.map +2 -2
  16. package/dist/cjs/decorators/changedAttribute.js +39 -0
  17. package/dist/cjs/decorators/changedAttribute.js.map +2 -2
  18. package/dist/cjs/decorators/elementDefine.js +237 -437
  19. package/dist/cjs/decorators/elementDefine.js.map +3 -3
  20. package/dist/cjs/decorators/emitCustomEvent.js +55 -0
  21. package/dist/cjs/decorators/emitCustomEvent.js.map +2 -2
  22. package/dist/cjs/decorators/index.js +3 -1
  23. package/dist/cjs/decorators/index.js.map +2 -2
  24. package/dist/cjs/decorators/intersectionObserver.js +209 -0
  25. package/dist/cjs/decorators/intersectionObserver.js.map +7 -0
  26. package/dist/cjs/decorators/mutationObserver.js +140 -0
  27. package/dist/cjs/decorators/mutationObserver.js.map +3 -3
  28. package/dist/cjs/decorators/query.js +117 -102
  29. package/dist/cjs/decorators/query.js.map +2 -2
  30. package/dist/cjs/decorators/resizeObserver.js +87 -0
  31. package/dist/cjs/decorators/resizeObserver.js.map +2 -2
  32. package/dist/cjs/decorators/setInterval.js +82 -0
  33. package/dist/cjs/decorators/setInterval.js.map +7 -0
  34. package/dist/cjs/decorators/setTimeout.js +82 -0
  35. package/dist/cjs/decorators/setTimeout.js.map +7 -0
  36. package/dist/cjs/decorators/subscribeSwcAppMessageWhileConnected.js +9 -0
  37. package/dist/cjs/decorators/subscribeSwcAppMessageWhileConnected.js.map +2 -2
  38. package/dist/cjs/decorators/subscribeSwcAppRouteChangeWhileConnected.js +9 -0
  39. package/dist/cjs/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +2 -2
  40. package/dist/cjs/types/index.js.map +1 -1
  41. package/dist/esm/config/config.js +1 -1
  42. package/dist/esm/config/config.js.map +2 -2
  43. package/dist/esm/decorators/addEventListener.js +388 -29
  44. package/dist/esm/decorators/addEventListener.js.map +3 -3
  45. package/dist/esm/decorators/applyAttribute.js +25 -0
  46. package/dist/esm/decorators/applyAttribute.js.map +2 -2
  47. package/dist/esm/decorators/applyClass.js +49 -18
  48. package/dist/esm/decorators/applyClass.js.map +3 -3
  49. package/dist/esm/decorators/applyNode.js +67 -1
  50. package/dist/esm/decorators/applyNode.js.map +3 -3
  51. package/dist/esm/decorators/applyProperty.js +101 -3
  52. package/dist/esm/decorators/applyProperty.js.map +2 -2
  53. package/dist/esm/decorators/applyStyle.js +36 -5
  54. package/dist/esm/decorators/applyStyle.js.map +2 -2
  55. package/dist/esm/decorators/changedAttribute.js +40 -0
  56. package/dist/esm/decorators/changedAttribute.js.map +2 -2
  57. package/dist/esm/decorators/elementDefine.js +244 -444
  58. package/dist/esm/decorators/elementDefine.js.map +3 -3
  59. package/dist/esm/decorators/emitCustomEvent.js +55 -0
  60. package/dist/esm/decorators/emitCustomEvent.js.map +2 -2
  61. package/dist/esm/decorators/index.js +3 -1
  62. package/dist/esm/decorators/index.js.map +2 -2
  63. package/dist/esm/decorators/intersectionObserver.js +191 -0
  64. package/dist/esm/decorators/intersectionObserver.js.map +7 -0
  65. package/dist/esm/decorators/mutationObserver.js +141 -0
  66. package/dist/esm/decorators/mutationObserver.js.map +3 -3
  67. package/dist/esm/decorators/query.js +117 -102
  68. package/dist/esm/decorators/query.js.map +2 -2
  69. package/dist/esm/decorators/resizeObserver.js +88 -0
  70. package/dist/esm/decorators/resizeObserver.js.map +2 -2
  71. package/dist/esm/decorators/setInterval.js +64 -0
  72. package/dist/esm/decorators/setInterval.js.map +7 -0
  73. package/dist/esm/decorators/setTimeout.js +64 -0
  74. package/dist/esm/decorators/setTimeout.js.map +7 -0
  75. package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js +9 -0
  76. package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js.map +2 -2
  77. package/dist/esm/decorators/subscribeSwcAppRouteChangeWhileConnected.js +9 -0
  78. package/dist/esm/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +2 -2
  79. package/dist/esm/types/index.js.map +1 -1
  80. package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +2565 -1474
  81. package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
  82. package/dist/types/config/config.d.ts.map +1 -1
  83. package/dist/types/decorators/addEventListener.d.ts +84 -33
  84. package/dist/types/decorators/addEventListener.d.ts.map +1 -1
  85. package/dist/types/decorators/applyAttribute.d.ts +22 -34
  86. package/dist/types/decorators/applyAttribute.d.ts.map +1 -1
  87. package/dist/types/decorators/applyClass.d.ts +34 -34
  88. package/dist/types/decorators/applyClass.d.ts.map +1 -1
  89. package/dist/types/decorators/applyNode.d.ts +30 -20
  90. package/dist/types/decorators/applyNode.d.ts.map +1 -1
  91. package/dist/types/decorators/applyProperty.d.ts +57 -34
  92. package/dist/types/decorators/applyProperty.d.ts.map +1 -1
  93. package/dist/types/decorators/applyStyle.d.ts +25 -12
  94. package/dist/types/decorators/applyStyle.d.ts.map +1 -1
  95. package/dist/types/decorators/changedAttribute.d.ts +11 -0
  96. package/dist/types/decorators/changedAttribute.d.ts.map +1 -1
  97. package/dist/types/decorators/elementDefine.d.ts +0 -14
  98. package/dist/types/decorators/elementDefine.d.ts.map +1 -1
  99. package/dist/types/decorators/emitCustomEvent.d.ts +28 -4
  100. package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
  101. package/dist/types/decorators/index.d.ts +3 -1
  102. package/dist/types/decorators/index.d.ts.map +1 -1
  103. package/dist/types/decorators/intersectionObserver.d.ts +49 -0
  104. package/dist/types/decorators/intersectionObserver.d.ts.map +1 -0
  105. package/dist/types/decorators/mutationObserver.d.ts +35 -25
  106. package/dist/types/decorators/mutationObserver.d.ts.map +1 -1
  107. package/dist/types/decorators/query.d.ts +57 -14
  108. package/dist/types/decorators/query.d.ts.map +1 -1
  109. package/dist/types/decorators/resizeObserver.d.ts +34 -23
  110. package/dist/types/decorators/resizeObserver.d.ts.map +1 -1
  111. package/dist/types/decorators/setInterval.d.ts +29 -0
  112. package/dist/types/decorators/setInterval.d.ts.map +1 -0
  113. package/dist/types/decorators/setTimeout.d.ts +29 -0
  114. package/dist/types/decorators/setTimeout.d.ts.map +1 -0
  115. package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts +7 -1
  116. package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts.map +1 -1
  117. package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts +6 -0
  118. package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts.map +1 -1
  119. package/dist/types/types/index.d.ts +104 -0
  120. package/dist/types/types/index.d.ts.map +1 -1
  121. package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +2565 -1474
  122. package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
  123. package/package.json +7 -5
  124. package/src/config/config.ts +1 -1
  125. package/src/decorators/addEventListener.ts +394 -34
  126. package/src/decorators/applyAttribute.ts +50 -36
  127. package/src/decorators/applyClass.ts +76 -26
  128. package/src/decorators/applyNode.ts +114 -36
  129. package/src/decorators/applyProperty.ts +152 -40
  130. package/src/decorators/applyStyle.ts +64 -17
  131. package/src/decorators/changedAttribute.ts +46 -0
  132. package/src/decorators/elementDefine.ts +281 -580
  133. package/src/decorators/emitCustomEvent.ts +73 -5
  134. package/src/decorators/index.ts +3 -1
  135. package/src/decorators/intersectionObserver.ts +259 -0
  136. package/src/decorators/mutationObserver.ts +215 -39
  137. package/src/decorators/query.ts +168 -126
  138. package/src/decorators/resizeObserver.ts +152 -46
  139. package/src/decorators/setInterval.ts +80 -0
  140. package/src/decorators/setTimeout.ts +80 -0
  141. package/src/decorators/subscribeSwcAppMessageWhileConnected.ts +17 -0
  142. package/src/decorators/subscribeSwcAppRouteChangeWhileConnected.ts +19 -0
  143. package/src/types/index.ts +141 -0
  144. package/dist/cjs/decorators/queryAll.js +0 -181
  145. package/dist/cjs/decorators/queryAll.js.map +0 -7
  146. package/dist/esm/decorators/queryAll.js +0 -162
  147. package/dist/esm/decorators/queryAll.js.map +0 -7
  148. package/dist/types/decorators/queryAll.d.ts +0 -22
  149. package/dist/types/decorators/queryAll.d.ts.map +0 -1
  150. package/src/decorators/queryAll.ts +0 -186
@@ -1,36 +1,110 @@
1
1
  import { ReflectUtils } from '@dooboostore/core';
2
2
  import { getElementConfig, ensureInit } from './elementDefine';
3
3
  import { SwcUtils } from '../utils/Utils';
4
- import { SwcQueryOptions, HelperHostSet } from '../types';
4
+ import { SwcQueryOptions, SwcRootType, SwcFnSelector, SwcSelector, HelperHostSet } from '../types';
5
5
 
6
- export interface QueryOptions extends SwcQueryOptions {
7
- /**
8
- * Filter function to determine whether to include the element.
9
- * If it returns false, the element is skipped.
10
- */
6
+ export type QueryPick = 'first' | 'last' | 'all' | 'even' | 'odd' | number;
7
+
8
+ export interface QueryBaseOptions {
9
+ // 어떤 요소를 고를지 — 기본 'first'(단일). 'all'/'even'/'odd'는 배열, number는 인덱스(단일)
10
+ pick?: QueryPick;
11
11
  filter?: (target: HTMLElement, meta: { currentThis: any, helper: HelperHostSet }) => boolean;
12
12
  }
13
+ // 문자열 셀렉터 전용 — root 허용
14
+ export type QueryOptions = QueryBaseOptions & SwcQueryOptions;
15
+ // 함수 셀렉터 전용 — root 금지
16
+ export type QueryNonQueryOptions = QueryBaseOptions;
17
+
18
+ export type QueryFnSelector = SwcFnSelector;
19
+ export type QuerySelector = SwcSelector;
20
+
21
+ // 셀렉터 종류에 따라 옵션 타입 분기
22
+ export type QueryOptionsOf<S extends QuerySelector> = S extends string ? QueryOptions : QueryNonQueryOptions;
13
23
 
14
24
  export interface QueryMetadata {
15
25
  propertyKey: string | symbol;
16
- selector: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null);
17
- options: QueryOptions ;
26
+ selector: QuerySelector;
27
+ options: QueryOptions;
18
28
  }
19
29
 
20
30
  export const QUERY_METADATA_KEY = Symbol.for('simple-web-component:query');
21
31
 
32
+ /**
33
+ * 셀렉터를 해석해 매칭된 모든 요소를 배열로 반환한다 (query/queryAll 공용).
34
+ * $window/$document는 요소가 아니므로 여기서 처리하지 않는다.
35
+ */
36
+ export const resolveQueryElements = (
37
+ inst: any,
38
+ selector: QuerySelector,
39
+ options: QueryOptions,
40
+ win: Window
41
+ ): HTMLElement[] => {
42
+ const hostSet = SwcUtils.getHelperAndHostSet(win, inst);
43
+ let all: HTMLElement[] = [];
44
+
45
+ if (typeof selector === 'function') {
46
+ const result = (selector as any)(inst, hostSet);
47
+ if (result instanceof win.NodeList) all = Array.from(result as NodeList).filter(e => e instanceof win.HTMLElement) as HTMLElement[];
48
+ else if (Array.isArray(result)) all = (result as any[]).filter(e => e instanceof win.HTMLElement) as HTMLElement[];
49
+ else if (result instanceof win.HTMLElement) all = [result as HTMLElement];
50
+ } else if (typeof selector === 'string') {
51
+ const r = (options as any)?.root || 'auto';
52
+ if (selector === '$this') all = [inst as HTMLElement];
53
+ else if (selector === '$host') all = hostSet.$host ? [hostSet.$host] : [];
54
+ else if (selector === '$parentHost') all = hostSet.$parentHost ? [hostSet.$parentHost] : [];
55
+ else if (selector === '$appHost') all = hostSet.$appHost ? [hostSet.$appHost as any] : [];
56
+ else if (selector === '$firstHost') all = hostSet.$firstHost ? [hostSet.$firstHost] : [];
57
+ else if (selector === '$lastHost') all = hostSet.$lastHost ? [hostSet.$lastHost] : [];
58
+ else if (selector === '$firstAppHost') all = hostSet.$firstAppHost ? [hostSet.$firstAppHost as any] : [];
59
+ else if (selector === '$lastAppHost') all = hostSet.$lastAppHost ? [hostSet.$lastAppHost as any] : [];
60
+ else if (selector === '$hosts') all = hostSet.$hosts.filter(Boolean);
61
+ else if (selector === '$appHosts') all = hostSet.$appHosts.filter(Boolean) as any[];
62
+ else {
63
+ const scopes: any[] = [];
64
+ if (r === 'auto') scopes.push(inst.shadowRoot || inst);
65
+ else if (r === 'light') scopes.push(inst);
66
+ else if (r === 'shadow' && inst.shadowRoot) scopes.push(inst.shadowRoot);
67
+ else if (r === 'all') { scopes.push(inst); if (inst.shadowRoot) scopes.push(inst.shadowRoot); }
68
+ for (const scope of scopes) {
69
+ const found = scope?.querySelectorAll?.(selector) as NodeListOf<HTMLElement> | undefined;
70
+ if (found?.length) all.push(...Array.from(found).filter(e => e instanceof win.HTMLElement));
71
+ }
72
+ }
73
+ }
74
+
75
+ if ((options as any)?.filter) {
76
+ all = all.filter(el => (options as any).filter(el, { currentThis: inst, helper: hostSet }));
77
+ }
78
+ return all;
79
+ };
80
+
81
+ /**
82
+ * 매칭된 요소 배열에서 pick 옵션에 따라 요소를 고른다.
83
+ * - 'first'(기본) / 'last' / number(인덱스) → 단일 요소
84
+ * - 'all' / 'even'(0,2,4...) / 'odd'(1,3,5...) → 배열
85
+ */
86
+ export const pickElements = (
87
+ all: HTMLElement[],
88
+ pick: QueryPick
89
+ ): HTMLElement | HTMLElement[] | null => {
90
+ if (pick === 'all') return all;
91
+ if (pick === 'even') return all.filter((_, i) => i % 2 === 0);
92
+ if (pick === 'odd') return all.filter((_, i) => i % 2 === 1);
93
+ if (typeof pick === 'number') return all[pick] ?? null;
94
+ if (pick === 'last') return all.length ? all[all.length - 1] : null;
95
+ return all.length ? all[0] : null;
96
+ };
97
+
22
98
  export function query(target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor | void;
23
- export function query(selector: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null), options?: QueryOptions): PropertyDecorator;
99
+ export function query(selector: string, options?: QueryOptions): PropertyDecorator;
100
+ export function query(selector: QueryFnSelector, options?: QueryNonQueryOptions): PropertyDecorator;
24
101
  export function query(options?: QueryOptions): PropertyDecorator;
25
102
  /**
26
- * @query decorator to inject a single element into a class property.
27
- *
28
- * Supports both string selectors and function-based selectors:
29
- * - String selector: @query('#my-element')
30
- * - Function selector: @query((currentThis, helper) => currentThis.shadowRoot?.querySelector('.item'))
31
- *
32
- * Supports filtering:
33
- * - @query('#my-element', { filter: (target, meta) => target.hasAttribute('active') })
103
+ * @query decorator — 클래스 필드에 요소를 주입한다.
104
+ *
105
+ * - 문자열 셀렉터: CSS / 특수 셀렉터($this,$host,$window,...), root 옵션으로 탐색 영역 지정
106
+ * - 함수 셀렉터: (currentThis, helper) => Node | NodeList | Element | Element[] | null (root 금지)
107
+ * - options.pick: 'first'(기본) | 'last' | 'all' | 'even' | 'odd' | number
34
108
  */
35
109
  export function query(selectorOrTarget?: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null) | Object, optionsOrPropertyKey?: QueryOptions | string | symbol, descriptor?: PropertyDescriptor): any {
36
110
  // Bare decorator: @query
@@ -50,118 +124,32 @@ export function query(selectorOrTarget?: string | ((currentThis: any, helper: He
50
124
  queries = [];
51
125
  ReflectUtils.defineMetadata(QUERY_METADATA_KEY, queries, constructor);
52
126
  }
53
- queries.push({ propertyKey, selector: selectorOrTarget as any, options: optionsOrPropertyKey as QueryOptions || {} });
127
+ queries.push({ propertyKey, selector: selectorOrTarget as any, options: (optionsOrPropertyKey as QueryOptions) || {} });
54
128
 
55
129
  Object.defineProperty(targetObj, propertyKey, {
56
130
  get(this: HTMLElement) {
57
131
  ensureInit(this);
58
132
  const config = getElementConfig(this);
59
133
  const win = config?.window || window;
60
- const hostSet = SwcUtils.getHelperAndHostSet(win, this);
61
-
62
- // Resolve selector - can be string or function
63
- let resolvedSelector: string;
64
- let resolvedElement: HTMLElement | null = null;
65
-
66
- if (typeof selectorOrTarget === 'function') {
67
- const result = (selectorOrTarget as any)(this, hostSet);
68
- // If function returns a Node/Element, return it directly
69
- if (result instanceof win.Node || result instanceof win.Element) {
70
- resolvedElement = result instanceof win.HTMLElement ? (result as HTMLElement) : null;
71
- }
72
- // If function returns NodeList or Element[], return first element
73
- else if (result instanceof win.NodeList || Array.isArray(result)) {
74
- resolvedElement = result.length > 0 && result[0] instanceof win.HTMLElement ? (result[0] as HTMLElement) : null;
75
- }
76
- } else {
77
- resolvedSelector = selectorOrTarget;
78
-
79
- const r = (optionsOrPropertyKey as QueryOptions)?.root || 'auto';
80
- const applyRoot = (t: any) => {
81
- if (!t || !(t instanceof HTMLElement)) return t;
82
- if (r === 'auto') return t.shadowRoot || t;
83
- if (r === 'shadow') return t.shadowRoot;
84
- if (r === 'light') return t;
85
- if (r === 'all') return t.shadowRoot || t;
86
- return t;
87
- };
88
-
89
- // --- Special Selectors: HostSet ---
90
- if (resolvedSelector === '$this') resolvedElement = applyRoot(this);
91
- else if (resolvedSelector === '$host') resolvedElement = applyRoot(hostSet.$host);
92
- else if (resolvedSelector === '$parentHost') resolvedElement = applyRoot(hostSet.$parentHost);
93
- else if (resolvedSelector === '$appHost') resolvedElement = applyRoot(hostSet.$appHost);
94
- else if (resolvedSelector === '$firstHost') resolvedElement = applyRoot(hostSet.$firstHost);
95
- else if (resolvedSelector === '$lastHost') resolvedElement = applyRoot(hostSet.$lastHost);
96
- else if (resolvedSelector === '$firstAppHost') resolvedElement = applyRoot(hostSet.$firstAppHost);
97
- else if (resolvedSelector === '$lastAppHost') resolvedElement = applyRoot(hostSet.$lastAppHost);
98
- // --- Special Selectors: Env ---
99
- else if (resolvedSelector === '$window') return win;
100
- else if (resolvedSelector === '$document') return win.document;
101
- // --- Standard Selectors ---
102
- else {
103
- if (r === 'shadow') resolvedElement = this.shadowRoot ? (this.shadowRoot.querySelector(resolvedSelector) as HTMLElement | null) : null;
104
- else if (r === 'light') resolvedElement = this.querySelector(resolvedSelector) as HTMLElement | null;
105
- else if (r === 'all') {
106
- const shadowMatch = this.shadowRoot ? (this.shadowRoot.querySelector(resolvedSelector) as HTMLElement | null) : null;
107
- resolvedElement = shadowMatch || (this.querySelector(resolvedSelector) as HTMLElement | null);
108
- }
109
- else resolvedElement = ((this.shadowRoot || this).querySelector(resolvedSelector) as HTMLElement | null);
110
- }
111
- }
134
+ const options = (optionsOrPropertyKey as QueryOptions) || {};
112
135
 
113
- // Apply filter if provided
114
- if (resolvedElement instanceof win.HTMLElement && (optionsOrPropertyKey as QueryOptions)?.filter) {
115
- const shouldInclude = (optionsOrPropertyKey as QueryOptions).filter!(resolvedElement, { currentThis: this, helper: hostSet });
116
- if (!shouldInclude) {
117
- return null;
118
- }
119
- }
136
+ // $window/$document는 요소가 아니므로 항상 단일 반환
137
+ if (typeof selectorOrTarget === 'string' && selectorOrTarget === '$window') return win;
138
+ if (typeof selectorOrTarget === 'string' && selectorOrTarget === '$document') return win.document;
120
139
 
121
- return resolvedElement;
140
+ const all = resolveQueryElements(this, selectorOrTarget as QuerySelector, options, win);
141
+ return pickElements(all, options.pick ?? 'first');
122
142
  },
123
143
  set(this: HTMLElement, nv: any) {
124
144
  ensureInit(this);
125
- if (nv === null || nv === undefined) {
145
+ if (nv === null || nv === undefined || (Array.isArray(nv) && nv.length === 0)) {
126
146
  const config = getElementConfig(this);
127
147
  const win = config?.window || window;
128
- const hostSet = SwcUtils.getHelperAndHostSet(win, this);
129
-
130
- // Resolve selector - can be string or function
131
- if (typeof selectorOrTarget === 'function') {
132
- // Get the element from function and remove it
133
- const result = (selectorOrTarget as any)(this, hostSet);
134
- if (result instanceof win.Element) {
135
- (result as Element).remove();
136
- } else if (result instanceof win.NodeList || Array.isArray(result)) {
137
- const el = result.length > 0 ? result[0] : null;
138
- if (el instanceof win.Element) {
139
- (el as Element).remove();
140
- }
141
- }
142
- return;
143
- }
144
-
145
- const resolvedSelector = selectorOrTarget;
146
-
147
- if (resolvedSelector.startsWith('$') && resolvedSelector !== '$this') return;
148
-
149
- const r = (optionsOrPropertyKey as QueryOptions)?.root || 'auto';
150
- const targets: Element[] = [];
151
-
152
- if (r === 'shadow') {
153
- if (this.shadowRoot?.querySelector(resolvedSelector)) targets.push(this.shadowRoot.querySelector(resolvedSelector)!);
154
- } else if (r === 'light') {
155
- if (this.querySelector(resolvedSelector)) targets.push(this.querySelector(resolvedSelector)!);
156
- } else if (r === 'all') {
157
- if (this.shadowRoot?.querySelector(resolvedSelector)) targets.push(this.shadowRoot.querySelector(resolvedSelector)!);
158
- if (this.querySelector(resolvedSelector)) targets.push(this.querySelector(resolvedSelector)!);
159
- } else {
160
- const el = (this.shadowRoot || this).querySelector(resolvedSelector);
161
- if (el) targets.push(el);
162
- }
163
-
164
- targets.forEach(t => t.remove());
148
+ const options = (optionsOrPropertyKey as QueryOptions) || {};
149
+ const all = resolveQueryElements(this, selectorOrTarget as QuerySelector, options, win);
150
+ const picked = pickElements(all, options.pick ?? 'first');
151
+ if (Array.isArray(picked)) picked.forEach(t => t.remove());
152
+ else if (picked) picked.remove();
165
153
  }
166
154
  },
167
155
  enumerable: true,
@@ -170,7 +158,7 @@ export function query(selectorOrTarget?: string | ((currentThis: any, helper: He
170
158
  };
171
159
  }
172
160
  // Without selector (defaults to $this)
173
- return query('$this', selectorOrTarget as SwcQueryOptions);
161
+ return query('$this', selectorOrTarget as QueryOptions);
174
162
  }
175
163
 
176
164
  export const getQueryMetadata = (target: any): QueryMetadata[] => {
@@ -178,25 +166,79 @@ export const getQueryMetadata = (target: any): QueryMetadata[] => {
178
166
  return ReflectUtils.getMetadata(QUERY_METADATA_KEY, constructor) ?? [];
179
167
  };
180
168
 
169
+ // ─── queryAll (query의 pick:'all' 편의 래퍼 — 호환용) ───
170
+ export const QUERY_ALL_METADATA_KEY = QUERY_METADATA_KEY;
171
+
172
+ export function queryAll(target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor | void;
173
+ export function queryAll(selector: string, options?: Omit<QueryOptions, 'pick'>): PropertyDecorator;
174
+ export function queryAll(selector: QueryFnSelector, options?: Omit<QueryNonQueryOptions, 'pick'>): PropertyDecorator;
175
+ export function queryAll(options?: Omit<QueryOptions, 'pick'>): PropertyDecorator;
176
+ export function queryAll(selectorOrTarget?: any, optionsOrPropertyKey?: any, descriptor?: PropertyDescriptor): any {
177
+ // bare(필드 직접 적용)는 query로 위임
178
+ if (descriptor !== undefined && (typeof optionsOrPropertyKey === 'string' || typeof optionsOrPropertyKey === 'symbol')) {
179
+ return query(selectorOrTarget, optionsOrPropertyKey, descriptor);
180
+ }
181
+ if (typeof selectorOrTarget === 'string' || typeof selectorOrTarget === 'function') {
182
+ return query(selectorOrTarget, {...(optionsOrPropertyKey ?? {}), pick: 'all'});
183
+ }
184
+ return query({...(selectorOrTarget ?? {}), pick: 'all'});
185
+ }
186
+
187
+ export const getQueryAllMetadata = getQueryMetadata;
188
+
181
189
  // ─── root별 편의 데코레이터 ───
182
190
 
183
191
  /**
184
- * @queryShadow(selector, options?) — shadow DOM에서 단일 요소를 쿼리
192
+ * @queryShadow(selector, options?) — shadow DOM에서 요소를 쿼리
193
+ */
194
+ export function queryShadow(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
195
+ return query(selector as any, {...options ?? {}, root: 'shadow'});
196
+ }
197
+
198
+ /**
199
+ * @queryLight(selector, options?) — light DOM에서 요소를 쿼리
200
+ */
201
+ export function queryLight(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
202
+ return query(selector as any, {...options ?? {}, root: 'light'});
203
+ }
204
+
205
+ /**
206
+ * @queryAllRoots(selector, options?) — shadow+light 모두에서 요소를 쿼리 (pick 기본 first)
207
+ */
208
+ export function queryAllRoots(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
209
+ return query(selector as any, {...options ?? {}, root: 'all'});
210
+ }
211
+
212
+ /**
213
+ * @queryAllShadow(selector, options?) — shadow DOM에서 모든 매칭 요소를 배열로 쿼리
185
214
  */
186
- export function queryShadow(selector: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null), options?: Omit<QueryOptions, 'root'>): PropertyDecorator {
187
- return query(selector, {...options ?? {}, root: 'shadow'});
215
+ export function queryAllShadow(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
216
+ return query(selector as any, {...options ?? {}, root: 'shadow', pick: 'all'});
188
217
  }
189
218
 
190
219
  /**
191
- * @queryLight(selector, options?) — light DOM에서 단일 요소를 쿼리
220
+ * @queryAllLight(selector, options?) — light DOM에서 모든 매칭 요소를 배열로 쿼리
192
221
  */
193
- export function queryLight(selector: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null), options?: Omit<QueryOptions, 'root'>): PropertyDecorator {
194
- return query(selector, {...options ?? {}, root: 'light'});
222
+ export function queryAllLight(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
223
+ return query(selector as any, {...options ?? {}, root: 'light', pick: 'all'});
195
224
  }
196
225
 
197
226
  /**
198
- * @queryAll(selector, options?) — shadow+light 모두에서 요소를 쿼리
227
+ * @queryAllAll(selector, options?) — shadow+light 모두에서 모든 매칭 요소를 배열로 쿼리
199
228
  */
200
- export function queryAllRoots(selector: string | ((currentThis: any, helper: HelperHostSet) => Node | NodeList | Element | Element[] | null), options?: Omit<QueryOptions, 'root'>): PropertyDecorator {
201
- return query(selector, {...options ?? {}, root: 'all'});
229
+ export function queryAllAll(selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator {
230
+ return query(selector as any, {...options ?? {}, root: 'all', pick: 'all'});
202
231
  }
232
+
233
+ /**
234
+ * @queryIn(root, pick?) — root + pick 조합 팩토리.
235
+ * first/last/even/odd/all/number를 모두 지원하는 데코레이터를 생성한다.
236
+ *
237
+ * @example
238
+ * @queryIn('shadow', 'even')('.item') // shadow DOM 짝수 인덱스들
239
+ * @queryIn('light', 2)('.item') // light DOM 3번째 요소
240
+ * @queryIn('all')('.item') // root all, pick 기본 first
241
+ */
242
+ export const queryIn = (root: SwcRootType, pick?: QueryPick) =>
243
+ (selector: string | QueryFnSelector, options?: QueryBaseOptions): PropertyDecorator =>
244
+ query(selector as any, {...options ?? {}, root, ...(pick !== undefined ? {pick} : {})});
@@ -1,35 +1,41 @@
1
1
  import { ReflectUtils } from '@dooboostore/core';
2
- import { SpecialSelector, SwcQueryOptions, HelperHostSet } from '../types';
3
- import { EventListenerSelector } from './addEventListener';
4
-
5
- export interface ResizeObserverOptions extends SwcQueryOptions {
6
- // 관찰 대상 셀렉터. 기본값 $this (컴포넌트 자신)
7
- // delegate: true 인 경우 루트에서 매칭 요소들의 크기 변화를 감지 (동적 요소 포함)
8
- delegate?: boolean;
9
- // ResizeObserver box 옵션
2
+ import { SpecialSelector, SwcQueryOptions, SwcFnSelector, SwcSelector, HelperHostSet } from '../types';
3
+
4
+ // 공통 옵션 — root·delegate 없음
5
+ export interface ResizeObserverBaseOptions {
10
6
  box?: 'content-box' | 'border-box' | 'device-pixel-content-box';
11
7
  filter?: (matchedEls: HTMLElement[], meta: { currentThis: any, helper: HelperHostSet }) => boolean;
12
- // observer 해제(disconnected) 시 호출되는 콜백. 첫 번째 인자는 observe된 target element, 두 번째는 사용자 옵션 객체.
13
8
  removeObserver?: (target: Element, optionValue: ResizeObserverOptions) => void;
14
9
  }
15
10
 
11
+ // 문자열 셀렉터 전용 — root·delegate 허용
12
+ export type ResizeObserverQueryOptions = ResizeObserverBaseOptions & SwcQueryOptions & { delegate?: boolean };
13
+ // 함수 셀렉터 전용 — root·delegate 금지
14
+ export type ResizeObserverNonQueryOptions = ResizeObserverBaseOptions;
15
+ // 내부 저장/해석용
16
+ export type ResizeObserverOptions = ResizeObserverQueryOptions;
17
+
18
+ // 셀렉터 종류에 따라 옵션 타입 분기
19
+ export type ResizeObserverOptionsOf<S extends SwcSelector> = S extends string ? ResizeObserverQueryOptions : ResizeObserverNonQueryOptions;
20
+
16
21
  export interface ResizeObserverMetadata {
17
22
  propertyKey: string | symbol;
18
- selector: EventListenerSelector;
23
+ selector: SwcSelector;
19
24
  options: ResizeObserverOptions;
20
25
  }
21
26
 
22
27
  export const RESIZE_OBSERVER_METADATA_KEY = Symbol.for('simple-web-component:resize-observer');
23
28
 
24
- export function resizeObserver(target: SpecialSelector, options?: ResizeObserverOptions): MethodDecorator;
25
- export function resizeObserver(selector: EventListenerSelector, options?: ResizeObserverOptions): MethodDecorator;
26
- export function resizeObserver(options?: ResizeObserverOptions): MethodDecorator;
29
+ export function resizeObserver(target: SpecialSelector, options?: ResizeObserverQueryOptions): MethodDecorator;
30
+ export function resizeObserver(selector: string, options?: ResizeObserverQueryOptions): MethodDecorator;
31
+ export function resizeObserver(selector: SwcFnSelector, options?: ResizeObserverNonQueryOptions): MethodDecorator;
32
+ export function resizeObserver(options?: ResizeObserverQueryOptions): MethodDecorator;
27
33
  /**
28
34
  * @resizeObserver decorator to observe element size changes.
29
35
  */
30
- export function resizeObserver(selectorOrOptions?: EventListenerSelector | ResizeObserverOptions, maybeOptions?: ResizeObserverOptions): MethodDecorator {
36
+ export function resizeObserver(selectorOrOptions?: SwcSelector | ResizeObserverOptions, maybeOptions?: ResizeObserverOptions): MethodDecorator {
31
37
  return (targetObj: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
32
- let selector: EventListenerSelector = '$this';
38
+ let selector: SwcSelector = '$this';
33
39
  let options: ResizeObserverOptions = {};
34
40
 
35
41
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
@@ -52,73 +58,73 @@ export function resizeObserver(selectorOrOptions?: EventListenerSelector | Resiz
52
58
  };
53
59
  }
54
60
 
55
- export function resizeObserverThis(options?: ResizeObserverOptions): MethodDecorator {
61
+ export function resizeObserverThis(options?: ResizeObserverQueryOptions): MethodDecorator {
56
62
  return resizeObserver('$this', options);
57
63
  }
58
64
 
59
- // ─── root별 delegate 헬퍼 ───
65
+ // ─── root별 delegate 헬퍼 (root 주입 → 문자열 셀렉터 전용) ───
60
66
 
61
- export function resizeObserverDelegateLight(selector: EventListenerSelector, options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
62
- export function resizeObserverDelegateLight(options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
63
- export function resizeObserverDelegateLight(selectorOrOptions?: EventListenerSelector | Omit<ResizeObserverOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator {
67
+ export function resizeObserverDelegateLight(selector: string, options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
68
+ export function resizeObserverDelegateLight(options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
69
+ export function resizeObserverDelegateLight(selectorOrOptions?: string | Omit<ResizeObserverQueryOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator {
64
70
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
65
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'light', delegate: true});
71
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'light', delegate: true});
66
72
  }
67
73
  return resizeObserver({...selectorOrOptions ?? {}, root: 'light', delegate: true});
68
74
  }
69
75
 
70
- export function resizeObserverDelegateShadow(selector: EventListenerSelector, options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
71
- export function resizeObserverDelegateShadow(options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
72
- export function resizeObserverDelegateShadow(selectorOrOptions?: EventListenerSelector | Omit<ResizeObserverOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator {
76
+ export function resizeObserverDelegateShadow(selector: string, options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
77
+ export function resizeObserverDelegateShadow(options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
78
+ export function resizeObserverDelegateShadow(selectorOrOptions?: string | Omit<ResizeObserverQueryOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator {
73
79
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
74
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'shadow', delegate: true});
80
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'shadow', delegate: true});
75
81
  }
76
82
  return resizeObserver({...selectorOrOptions ?? {}, root: 'shadow', delegate: true});
77
83
  }
78
84
 
79
- export function resizeObserverDelegateAll(selector: EventListenerSelector, options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
80
- export function resizeObserverDelegateAll(options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
81
- export function resizeObserverDelegateAll(selectorOrOptions?: EventListenerSelector | Omit<ResizeObserverOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator {
85
+ export function resizeObserverDelegateAll(selector: string, options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
86
+ export function resizeObserverDelegateAll(options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
87
+ export function resizeObserverDelegateAll(selectorOrOptions?: string | Omit<ResizeObserverQueryOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator {
82
88
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
83
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'all', delegate: true});
89
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'all', delegate: true});
84
90
  }
85
91
  return resizeObserver({...selectorOrOptions ?? {}, root: 'all', delegate: true});
86
92
  }
87
93
 
88
- export function resizeObserverDelegate(selector: EventListenerSelector, options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
89
- export function resizeObserverDelegate(options?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator;
90
- export function resizeObserverDelegate(selectorOrOptions?: EventListenerSelector | Omit<ResizeObserverOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverOptions, 'delegate'>): MethodDecorator {
94
+ export function resizeObserverDelegate(selector: string, options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
95
+ export function resizeObserverDelegate(options?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator;
96
+ export function resizeObserverDelegate(selectorOrOptions?: string | Omit<ResizeObserverQueryOptions, 'delegate'>, maybeOptions?: Omit<ResizeObserverQueryOptions, 'delegate'>): MethodDecorator {
91
97
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
92
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'auto', delegate: true});
98
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'auto', delegate: true});
93
99
  }
94
100
  return resizeObserver({...selectorOrOptions ?? {}, root: 'auto', delegate: true});
95
101
  }
96
102
 
97
103
  // ─── root별 일반 헬퍼 (delegate 없이, 셀렉터 생략 시 $this) ───
98
104
 
99
- export function resizeObserverLight(selector: EventListenerSelector, options?: ResizeObserverOptions): MethodDecorator;
100
- export function resizeObserverLight(options?: ResizeObserverOptions): MethodDecorator;
101
- export function resizeObserverLight(selectorOrOptions?: EventListenerSelector | ResizeObserverOptions, maybeOptions?: ResizeObserverOptions): MethodDecorator {
105
+ export function resizeObserverLight(selector: string, options?: ResizeObserverQueryOptions): MethodDecorator;
106
+ export function resizeObserverLight(options?: ResizeObserverQueryOptions): MethodDecorator;
107
+ export function resizeObserverLight(selectorOrOptions?: string | ResizeObserverQueryOptions, maybeOptions?: ResizeObserverQueryOptions): MethodDecorator {
102
108
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
103
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'light'});
109
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'light'});
104
110
  }
105
111
  return resizeObserver({...selectorOrOptions ?? {}, root: 'light'});
106
112
  }
107
113
 
108
- export function resizeObserverShadow(selector: EventListenerSelector, options?: ResizeObserverOptions): MethodDecorator;
109
- export function resizeObserverShadow(options?: ResizeObserverOptions): MethodDecorator;
110
- export function resizeObserverShadow(selectorOrOptions?: EventListenerSelector | ResizeObserverOptions, maybeOptions?: ResizeObserverOptions): MethodDecorator {
114
+ export function resizeObserverShadow(selector: string, options?: ResizeObserverQueryOptions): MethodDecorator;
115
+ export function resizeObserverShadow(options?: ResizeObserverQueryOptions): MethodDecorator;
116
+ export function resizeObserverShadow(selectorOrOptions?: string | ResizeObserverQueryOptions, maybeOptions?: ResizeObserverQueryOptions): MethodDecorator {
111
117
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
112
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'shadow'});
118
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'shadow'});
113
119
  }
114
120
  return resizeObserver({...selectorOrOptions ?? {}, root: 'shadow'});
115
121
  }
116
122
 
117
- export function resizeObserverAll(selector: EventListenerSelector, options?: ResizeObserverOptions): MethodDecorator;
118
- export function resizeObserverAll(options?: ResizeObserverOptions): MethodDecorator;
119
- export function resizeObserverAll(selectorOrOptions?: EventListenerSelector | ResizeObserverOptions, maybeOptions?: ResizeObserverOptions): MethodDecorator {
123
+ export function resizeObserverAll(selector: string, options?: ResizeObserverQueryOptions): MethodDecorator;
124
+ export function resizeObserverAll(options?: ResizeObserverQueryOptions): MethodDecorator;
125
+ export function resizeObserverAll(selectorOrOptions?: string | ResizeObserverQueryOptions, maybeOptions?: ResizeObserverQueryOptions): MethodDecorator {
120
126
  if (typeof selectorOrOptions === 'string' || typeof selectorOrOptions === 'function') {
121
- return resizeObserver(selectorOrOptions, {...maybeOptions ?? {}, root: 'all'});
127
+ return resizeObserver(selectorOrOptions as any, {...maybeOptions ?? {}, root: 'all'});
122
128
  }
123
129
  return resizeObserver({...selectorOrOptions ?? {}, root: 'all'});
124
130
  }
@@ -126,4 +132,104 @@ export function resizeObserverAll(selectorOrOptions?: EventListenerSelector | Re
126
132
  export function getResizeObserverMetadata(target: any): ResizeObserverMetadata[] | undefined {
127
133
  const constructor = target instanceof Function ? target : target.constructor;
128
134
  return ReflectUtils.getMetadata(RESIZE_OBSERVER_METADATA_KEY, constructor);
129
- }
135
+ }
136
+
137
+ // ─────────────────────────────────────────────────────────────────────────────
138
+ // ResizeObserverLifeCycler
139
+ // observer 를 직접 생성하지 않고 OnConnectedResult 를 반환한다.
140
+ // elementDefine 이 수집된 ResizeObserverSet 으로 observer 를 생성하고,
141
+ // observe/unobserve 함수를 주입해 MutationObserver delegate 추적이 사용할 수 있게 한다.
142
+ // ─────────────────────────────────────────────────────────────────────────────
143
+ import { ElementDefineLifeCycler, OnConnectedResult, ResizeObserverSet, ResizeObserverSetEntry } from '../types';
144
+ import { SwcUtils } from '../utils/Utils';
145
+
146
+ export class ResizeObserverLifeCycler implements ElementDefineLifeCycler {
147
+ private readonly removeObserverCallbacksMap = new WeakMap<Element, Array<{ fn: (target: Element, opts: unknown) => void; target: any; opts: unknown }>>();
148
+
149
+ onConnected(helperHostSet: HelperHostSet): OnConnectedResult | void {
150
+ const inst = helperHostSet.$this;
151
+ const currentWin = helperHostSet.$w;
152
+
153
+ const removeObserverCallbacks: Array<{ fn: (target: Element, opts: unknown) => void; target: any; opts: unknown }> = [];
154
+ this.removeObserverCallbacksMap.set(inst, removeObserverCallbacks);
155
+
156
+ const allMeta = getResizeObserverMetadata(inst) ?? [];
157
+ if (allMeta.length === 0) return;
158
+
159
+ const root: HTMLElement | ShadowRoot = inst.shadowRoot || inst;
160
+
161
+ // removeObserver 콜백 수집
162
+ for (const m of allMeta) {
163
+ if (m.options.removeObserver) removeObserverCallbacks.push({ fn: m.options.removeObserver, target: root, opts: m.options });
164
+ }
165
+
166
+ // ── ResizeObserver 콜백 ──
167
+ const callback: ResizeObserverSetEntry['callback'] = (entries, obs) => {
168
+ for (const m of allMeta) {
169
+ let matchedEls: HTMLElement[];
170
+ if (m.options.delegate && typeof m.selector === 'string') {
171
+ const isThis = m.selector === '$this' || m.selector === '';
172
+ matchedEls = entries.map(e => e.target as HTMLElement).filter(el => {
173
+ if (!el || el.nodeType !== 1) return false;
174
+ if (isThis) return true;
175
+ return el.matches?.(m.selector as string) || !!el.closest?.(m.selector as string);
176
+ });
177
+ } else {
178
+ matchedEls = entries.map(e => e.target as HTMLElement).filter(t => t && t.nodeType === 1);
179
+ }
180
+ if (matchedEls.length === 0) continue;
181
+ if (m.options.filter) {
182
+ if (!m.options.filter(matchedEls, { currentThis: inst, helper: helperHostSet })) continue;
183
+ }
184
+ const hostSet = SwcUtils.getHostSet(inst);
185
+ inst[m.propertyKey](matchedEls, entries, obs, { ...hostSet, $root: root });
186
+ }
187
+ };
188
+
189
+ // ── observe 대상 목록 구성 ──
190
+ // - non-delegate: 실제 요소를 초기 observe
191
+ // - delegate: 동적 추적용 셀렉터 문자열로 선언 (elementDefine 의 MutationObserver 콜백이 observe/unobserve)
192
+ // 모든 대상은 이 루트의 callback 을 공유한다.
193
+ const resizeObserverSet: ResizeObserverSet = [];
194
+ for (const m of allMeta) {
195
+ if (m.options.delegate && typeof m.selector === 'string') {
196
+ resizeObserverSet.push({ target: m.selector, delegate: true, delegateRoot: m.options.root, options: m.options.box ? { box: m.options.box } : undefined, callback });
197
+ continue;
198
+ }
199
+ let targets: HTMLElement[] = [];
200
+
201
+ if (typeof m.selector === 'function') {
202
+ const res = (m.selector as any)(inst, helperHostSet);
203
+ if (res instanceof currentWin.HTMLElement) targets = [res as HTMLElement];
204
+ else if (res instanceof currentWin.NodeList) targets = Array.from(res as NodeList).filter((e: any) => e instanceof currentWin.HTMLElement) as HTMLElement[];
205
+ else if (Array.isArray(res)) targets = res.filter((e: any) => e instanceof currentWin.HTMLElement) as HTMLElement[];
206
+ } else if (typeof m.selector === 'string') {
207
+ if (m.selector === '$this' || m.selector === '') {
208
+ targets = [inst as HTMLElement];
209
+ } else {
210
+ const r: any = m.options.root || 'auto';
211
+ const scopes: any[] = [];
212
+ if (r === 'auto') scopes.push(inst.shadowRoot || inst);
213
+ else if (r === 'light') scopes.push(inst);
214
+ else if (r === 'shadow' && inst.shadowRoot) scopes.push(inst.shadowRoot);
215
+ else if (r === 'all') { scopes.push(inst); if (inst.shadowRoot) scopes.push(inst.shadowRoot); }
216
+ for (const s of scopes) {
217
+ const found = s?.querySelectorAll?.(m.selector as string) as NodeListOf<HTMLElement> | undefined;
218
+ if (found?.length) targets.push(...Array.from(found));
219
+ }
220
+ }
221
+ }
222
+ for (const el of targets) resizeObserverSet.push({ target: el, options: m.options.box ? { box: m.options.box } : undefined, callback });
223
+ }
224
+
225
+ return { resizeObserverSet };
226
+ }
227
+
228
+ onDisconnected(helperHostSet: HelperHostSet): void {
229
+ const inst = helperHostSet.$this;
230
+ for (const cb of this.removeObserverCallbacksMap.get(inst) ?? []) {
231
+ try { cb.fn(cb.target, cb.opts); } catch (e) { console.error('[SWC] removeObserver error:', e); }
232
+ }
233
+ this.removeObserverCallbacksMap.delete(inst);
234
+ }
235
+ }