mount-observer 0.0.31 → 0.0.32

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 (3) hide show
  1. package/compose.js +26 -14
  2. package/package.json +2 -2
  3. package/types.d.ts +0 -201
package/compose.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { inclTemplQry } from './MountObserver.js';
2
- export const guid = Symbol.for('Wr0WPVh84k+O93miuENdMA');
2
+ export const childRefsKey = Symbol.for('Wr0WPVh84k+O93miuENdMA');
3
+ export const cloneKey = Symbol.for('LD97VKZYc02CQv23DT/6fQ');
3
4
  export async function compose(self, el, level) {
4
5
  const src = el.getAttribute('src');
5
6
  el.removeAttribute('src');
@@ -69,25 +70,36 @@ export async function compose(self, el, level) {
69
70
  el.dispatchEvent(new LoadEvent(clone));
70
71
  }
71
72
  if (level === 0) {
72
- const childRefs = [];
73
+ const refs = [];
73
74
  for (const child of clone.children) {
74
- childRefs.push(new WeakRef(child));
75
+ refs.push(new WeakRef(child));
75
76
  }
76
- el[guid] = childRefs;
77
+ el[childRefsKey] = refs;
77
78
  }
78
- if (shadowRootModeOnLoad !== null) {
79
- const parent = el.parentElement;
80
- if (parent === null)
81
- throw 404;
82
- if (parent.shadowRoot === null)
83
- parent.attachShadow({ mode: shadowRootModeOnLoad });
84
- parent.shadowRoot?.append(clone);
79
+ //if template has itemscope attribute, assume want to do some data binding before instantiating into
80
+ //DOM fragment.
81
+ let cloneStashed = false;
82
+ if (el.hasAttribute('itemscope')) {
83
+ el[cloneKey] = clone;
84
+ cloneStashed = true;
85
85
  }
86
86
  else {
87
- el.after(clone);
87
+ if (shadowRootModeOnLoad !== null) {
88
+ const parent = el.parentElement;
89
+ if (parent === null)
90
+ throw 404;
91
+ if (parent.shadowRoot === null)
92
+ parent.attachShadow({ mode: shadowRootModeOnLoad });
93
+ parent.shadowRoot?.append(clone);
94
+ }
95
+ else {
96
+ el.after(clone);
97
+ }
98
+ }
99
+ if (!cloneStashed) {
100
+ if (level !== 0 || (slots.length === 0 && el.attributes.length === 0))
101
+ el.remove();
88
102
  }
89
- if (level !== 0 || (slots.length === 0 && !el.hasAttribute('itemscope')))
90
- el.remove();
91
103
  }
92
104
  export class LoadEvent extends Event {
93
105
  clone;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mount-observer",
3
- "version": "0.0.31",
3
+ "version": "0.0.32",
4
4
  "description": "Observe and act on css matches.",
5
5
  "main": "MountObserver.js",
6
6
  "module": "MountObserver.js",
@@ -30,7 +30,7 @@
30
30
  "*.js",
31
31
  "types.d.ts"
32
32
  ],
33
- "types": "types.d.ts",
33
+ "types": "./ts-refs/mount-observer/types.d.ts",
34
34
  "scripts": {
35
35
  "serve": "node node_modules/may-it-serve",
36
36
  "test": "playwright test",
package/types.d.ts DELETED
@@ -1,201 +0,0 @@
1
- //import { MountObserver } from "./MountObserver";
2
-
3
- export interface JSONSerializableMountInit{
4
- readonly on?: CSSMatch,
5
- readonly observedAttrsWhenMounted?: (string | ObservedSourceOfTruthAttribute)[],
6
- readonly whereAttr?: WhereAttr,
7
- readonly whereElementIntersectsWith?: IntersectionObserverInit,
8
- readonly whereMediaMatches?: MediaQuery,
9
- readonly import?: ImportString | [ImportString, ImportAssertions] | PipelineProcessor,
10
-
11
- }
12
-
13
- export interface ObservedSourceOfTruthAttribute<TProps = any> {
14
- name: string,
15
- mapsTo?: keyof TProps & string,
16
- valIfNull?: any,
17
- valIfFalsy?: any,
18
- instanceOf?: any,
19
- customParser?: (newValue: string | null, oldValue: string | null, instance: Element) => any
20
- }
21
-
22
- export interface MountInit extends JSONSerializableMountInit{
23
-
24
- readonly withTargetShadowRoot?: ShadowRoot,
25
- readonly whereInstanceOf?: Array<{new(): Element}>,
26
- readonly whereSatisfies?: PipelineProcessor<boolean>,
27
- readonly do?: MountObserverCallbacks
28
- // /**
29
- // * Purpose -- there are scenarios where we may only want to affect changes that occur after the initial
30
- // * server rendering, so we only want to mount elements that appear
31
- // */
32
- // readonly ignoreInitialMatches?: boolean,
33
- }
34
-
35
- export interface MountObserverCallbacks{
36
- readonly mount?: PipelineProcessor,
37
- readonly dismount?: PipelineProcessor,
38
- readonly disconnect?: PipelineProcessor,
39
- readonly reconfirm?: PipelineProcessor,
40
- readonly exit?: PipelineProcessor,
41
- }
42
-
43
- export interface RootCnfg{
44
- start: string,
45
- context: 'BuiltIn' | 'CustomElement' | 'Both'
46
- }
47
-
48
- //export type RootAttrOptions = Array<string | RootCnfg>;
49
- export type delimiter = string;
50
- export interface WhereAttr{
51
-
52
- hasBase?: string | [delimiter, string],
53
- hasBranchIn?: Array<string> | [delimiter, Array<string>],
54
- hasRootIn?: Array<RootCnfg>,
55
- /**
56
- * Used by consumers to track the universal meaning of this combination
57
- * regardless of how the actual name values may be changed.
58
- */
59
- metadata?: any,
60
- }
61
- type CSSMatch = string;
62
- type ImportString = string;
63
- type MediaQuery = string;
64
-
65
- export interface AttribMatch{
66
- names: string[],
67
- //for boolean, support true/false/mixed
68
- // type?: 'number' | 'string' | 'date' | 'json-object' | 'boolean',
69
- // valConverter?: (s: string) => any,
70
- // validator?: (v: any) => boolean;
71
- }
72
-
73
- export interface IMountObserver {
74
- // readonly mountInit: MountInit,
75
- // readonly mountedRefs: WeakRef<Element>[],
76
- // readonly dismountedRefs: WeakRef<Element>[],
77
- observe(within: Node): void;
78
- disconnect(within: Node): void;
79
- module?: any;
80
- mountedElements: WeakSet<Element>;
81
- readAttrs(match: Element, branchIndexes?: Set<number>) : AttrChangeInfo[]
82
- }
83
-
84
- export interface MountContext{
85
- stage?: PipelineStage,
86
- initializing?: boolean,
87
- }
88
-
89
- type PipelineStage = 'Inspecting' | 'PreImport' | 'PostImport' | 'Import'
90
- export type PipelineProcessor<ReturnType = void> = (matchingElement: Element, observer: IMountObserver, ctx: MountContext) => Promise<ReturnType> | ReturnType;
91
-
92
- //#region mutation event
93
- export type mutationEventName = 'mutation-event';
94
- export interface MutationEvent{
95
- mutationRecords: Array<MutationRecord>
96
- }
97
- export type mutationEventHandler = (e: MutationEvent) => void;
98
-
99
- export interface AddMutationEventListener {
100
- addEventListener(eventName: mutationEventName, handler: mutationEventHandler, options?: AddEventListenerOptions): void;
101
- }
102
- //#endregion
103
-
104
- interface AttrParts{
105
- name: string,
106
- root?: string,
107
- base?: string,
108
- branch?: string,
109
- branchIdx: number,
110
- leaf?: string, //TODO
111
- leafIdx?: number, //TODO
112
- rootCnfg?: RootCnfg,
113
- metadata?: any,
114
- }
115
-
116
- interface AttrChangeInfo{
117
- oldValue: string | null,
118
- newValue: string | null,
119
- isSOfTAttr: boolean,
120
- idx?: number,
121
- name: string,
122
- parts?: AttrParts,
123
- mapsTo?: string,
124
- }
125
-
126
- //#region mount event
127
- export type mountEventName = 'mount';
128
- export interface IMountEvent{
129
- mountedElement: Element,
130
- }
131
- export type mountEventHandler = (e: IMountEvent) => void;
132
-
133
- export interface AddMountEventListener {
134
- addEventListener(eventName: mountEventName, handler: mountEventHandler, options?: AddEventListenerOptions): void;
135
- }
136
- //#endregion
137
-
138
- //#region dismount event
139
- export type dismountEventName = 'dismount';
140
- export interface IDismountEvent {
141
- dismountedElement: Element
142
- }
143
-
144
- export type dismountEventHandler = (e: IDismountEvent) => void;
145
-
146
- export interface AddDismountEventListener {
147
- addEventListener(eventName: dismountEventName, handler: dismountEventHandler, options?: AddEventListenerOptions): void;
148
- }
149
- //#endregion
150
-
151
- //#region disconnected event
152
- export type disconnectedEventName = 'disconnect';
153
- export interface IDisconnectEvent {
154
- disconnectedElement: Element
155
- }
156
-
157
- export type disconnectedEventHandler = (e: IDisconnectEvent) => void;
158
-
159
- export interface AddDisconnectEventListener {
160
- addEventListener(eventName: disconnectedEventName, handler: disconnectedEventHandler, options?: AddEventListenerOptions): void;
161
- }
162
- //endregion
163
-
164
- //#region attribute change event
165
- export type attrChangeEventName = 'attrChange';
166
- export interface IAttrChangeEvent extends IMountEvent {
167
- attrChangeInfos: Array<AttrChangeInfo>,
168
- }
169
- export type attrChangeEventHandler = (e: IAttrChangeEvent) => void;
170
- export interface AddAttrChangeEventListener{
171
- addEventListener(eventName: attrChangeEventName, handler: attrChangeEventHandler, options?: AddEventListenerOptions): void;
172
- }
173
- //#endregion
174
-
175
- //#region load event
176
- export type loadEventName = 'load';
177
- export interface ILoadEvent {
178
- clone: DocumentFragment
179
- }
180
- export type loadEventHandler = (e: ILoadEvent) => void;
181
- export interface AddLoadEventListener{
182
- addEventListener(eventName: loadEventName, handler: loadEventHandler, options?: AddEventListenerOptions): void
183
- }
184
- //#endregion
185
-
186
- //#region MountObserver Script Element
187
- export interface MOSEAddedProps<TSynConfig=any>{
188
- init: JSONSerializableMountInit;
189
- observer: IMountObserver;
190
- do: MountObserverCallbacks;
191
- synConfig: TSynConfig;
192
- }
193
- export interface MOSE<TSynConfig=any>
194
- extends HTMLScriptElement, MOSEAddedProps<TSynConfig>{
195
-
196
- }
197
-
198
- //#endregion
199
-
200
-
201
-