@molecule/app-angular 3.12.1 → 3.14.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/README.md +476 -18
- package/dist/dom-placement.d.ts +226 -10
- package/dist/dom-placement.d.ts.map +1 -1
- package/dist/dom-placement.js +944 -55
- package/dist/dom-placement.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/lazy-slot.d.ts +100 -0
- package/dist/lazy-slot.d.ts.map +1 -0
- package/dist/lazy-slot.js +152 -0
- package/dist/lazy-slot.js.map +1 -0
- package/dist/node.d.ts +25 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +31 -0
- package/dist/node.js.map +1 -0
- package/dist/providers.d.ts +2 -1
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +3 -1
- package/dist/providers.js.map +1 -1
- package/dist/routed-view.d.ts +6 -2
- package/dist/routed-view.d.ts.map +1 -1
- package/dist/routed-view.js +15 -9
- package/dist/routed-view.js.map +1 -1
- package/dist/slot-copy.d.ts +59 -0
- package/dist/slot-copy.d.ts.map +1 -0
- package/dist/slot-copy.js +126 -0
- package/dist/slot-copy.js.map +1 -0
- package/package.json +1 -1
package/dist/dom-placement.js
CHANGED
|
@@ -8,11 +8,19 @@
|
|
|
8
8
|
* ({@link setElementPresent}, {@link useHostPresence});
|
|
9
9
|
* - an element rendered in another parent, React's `createPortal`
|
|
10
10
|
* ({@link placeElement});
|
|
11
|
+
* - an element or a slot's content rendered beside the component's host,
|
|
12
|
+
* React's fragment root ({@link placeElementBefore},
|
|
13
|
+
* {@link placeContentBefore});
|
|
11
14
|
* - a slot's content rendered in another parent ({@link placeContent});
|
|
12
15
|
* - one wrapper element per projected child, React's
|
|
13
16
|
* `children.map((c) => <div>{c}</div>)` ({@link WrappedChildren},
|
|
14
17
|
* {@link MolWrapEachChild}).
|
|
15
18
|
*
|
|
19
|
+
* A node that was placed still has one parent in the rendered tree, the one
|
|
20
|
+
* React context follows ({@link renderedParent}), and what a consumer binds on
|
|
21
|
+
* the tag of a component whose host left the page lands on the component's
|
|
22
|
+
* root ({@link setHostRoot}).
|
|
23
|
+
*
|
|
16
24
|
* Angular keeps inserting, moving and removing the nodes it created, and it
|
|
17
25
|
* finds their place through the nodes themselves: it inserts a view before the
|
|
18
26
|
* first node of the next view, under the parent of the container's anchor
|
|
@@ -20,9 +28,16 @@
|
|
|
20
28
|
* placement rules are applied where Angular places nodes, in its renderer
|
|
21
29
|
* ({@link provideDomPlacement}); components never move a node themselves.
|
|
22
30
|
*
|
|
31
|
+
* A host or a capture that is out of the page has no `parentNode`, so a
|
|
32
|
+
* component never reads one to decide where something goes: it names the
|
|
33
|
+
* element to stand in front of ({@link placeElementBefore},
|
|
34
|
+
* {@link placeContentBefore}), which follows that element wherever it is, and
|
|
35
|
+
* finds its rendered ancestors with {@link renderedParent}.
|
|
36
|
+
*
|
|
23
37
|
* @module
|
|
24
38
|
*/
|
|
25
|
-
import { afterEveryRender, ChangeDetectorRef, DestroyRef, Directive, effect, ElementRef, inject, Injectable, InjectionToken, makeEnvironmentProviders, provideEnvironmentInitializer, RendererFactory2, signal, } from '@angular/core';
|
|
39
|
+
import { afterEveryRender, ChangeDetectorRef, DestroyRef, Directive, effect, ElementRef, inject, Injectable, InjectionToken, isDevMode, makeEnvironmentProviders, provideEnvironmentInitializer, reflectComponentType, RendererFactory2, signal, untracked, } from '@angular/core';
|
|
40
|
+
import { isLazySlotAnchor, isLazySlotContent } from './lazy-slot.js';
|
|
26
41
|
import * as i0 from "@angular/core";
|
|
27
42
|
/** Set once {@link provideDomPlacement} decorated the application's renderer factory. */
|
|
28
43
|
const DOM_PLACEMENT = new InjectionToken('@molecule/app-angular DOM placement');
|
|
@@ -41,18 +56,303 @@ const wrapperOwners = new WeakMap();
|
|
|
41
56
|
* a view's root nodes, so an element moved out of a component goes when its
|
|
42
57
|
* placeholder's view goes (React unmounts a portal with its owner).
|
|
43
58
|
*/
|
|
44
|
-
const elsewhere = new
|
|
59
|
+
const elsewhere = new WeakSet();
|
|
60
|
+
/**
|
|
61
|
+
* A node → what {@link placeElementBefore} and {@link placeContentBefore}
|
|
62
|
+
* rendered in front of it, in render order. It stands in front of the node
|
|
63
|
+
* wherever Angular puts the node.
|
|
64
|
+
*/
|
|
65
|
+
const leaders = new WeakMap();
|
|
66
|
+
/** An element rendered in front of another node → that node. */
|
|
67
|
+
const led = new WeakMap();
|
|
68
|
+
/** A capture whose content is rendered in front of another node → that node. */
|
|
69
|
+
const ledContent = new WeakMap();
|
|
70
|
+
/** A placeholder → the absent element whose place it holds. */
|
|
71
|
+
const heldPlaces = new WeakMap();
|
|
72
|
+
/**
|
|
73
|
+
* Set once anything was rendered outside its place ({@link placeElement},
|
|
74
|
+
* {@link placeContent}); until then a removal has nothing to take along.
|
|
75
|
+
*/
|
|
76
|
+
let rendersElsewhere = false;
|
|
77
|
+
/** A host whose component calls {@link usePlacement} → what the consumer bound on its tag. */
|
|
78
|
+
const tagBindings = new WeakMap();
|
|
79
|
+
/** Attributes that are Angular's own, never the consumer's. */
|
|
80
|
+
const isFrameworkAttribute = (name) => name.startsWith('_ng') || name.startsWith('ng-') || name === 'ngskiphydration';
|
|
81
|
+
/**
|
|
82
|
+
* The input and output names of the component a view belongs to; the input
|
|
83
|
+
* names lower-cased, as the DOM writes attribute names.
|
|
84
|
+
*
|
|
85
|
+
* @param view - The component's own change detector, whose context is the
|
|
86
|
+
* component instance once Angular created it.
|
|
87
|
+
* @returns The names.
|
|
88
|
+
*/
|
|
89
|
+
function memberNames(view) {
|
|
90
|
+
const component = view?.context;
|
|
91
|
+
const mirror = component ? reflectComponentType(component.constructor) : null;
|
|
92
|
+
return {
|
|
93
|
+
inputs: new Set((mirror?.inputs ?? []).map((input) => input.templateName.toLowerCase())),
|
|
94
|
+
outputs: new Set((mirror?.outputs ?? []).map((output) => output.templateName)),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Whether an element is out of the page with its place held: a host whose
|
|
99
|
+
* component rendered its DOM in the host's place.
|
|
100
|
+
*
|
|
101
|
+
* @param element - The host.
|
|
102
|
+
* @returns Whether it is absent.
|
|
103
|
+
*/
|
|
104
|
+
const isAbsent = (element) => placeholders.has(element) && !elsewhere.has(element);
|
|
105
|
+
/**
|
|
106
|
+
* The elements a host renders in its own place: the elements placed in front
|
|
107
|
+
* of it and the top-level elements of the content placed in front of it.
|
|
108
|
+
*
|
|
109
|
+
* @param host - The host.
|
|
110
|
+
* @returns The elements, in order.
|
|
111
|
+
*/
|
|
112
|
+
function ledElements(host) {
|
|
113
|
+
const elements = [];
|
|
114
|
+
for (const leader of leaders.get(host) ?? []) {
|
|
115
|
+
if (!leader.content) {
|
|
116
|
+
if (leader.node.nodeType === 1)
|
|
117
|
+
elements.push(leader.node);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const range = contentRanges.get(leader.node);
|
|
121
|
+
if (range === undefined)
|
|
122
|
+
continue;
|
|
123
|
+
for (let node = range.start.nextSibling; node !== null && node !== range.end; node = node.nextSibling) {
|
|
124
|
+
if (node.nodeType === 1)
|
|
125
|
+
elements.push(node);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return elements;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Takes what was written for a tag off the root that carried it and puts it
|
|
132
|
+
* back on the host, where the next root finds it.
|
|
133
|
+
*
|
|
134
|
+
* @param host - The host.
|
|
135
|
+
* @param bindings - Its bindings.
|
|
136
|
+
*/
|
|
137
|
+
function returnTagAttributes(host, bindings) {
|
|
138
|
+
const target = bindings.target;
|
|
139
|
+
if (target === null)
|
|
140
|
+
return;
|
|
141
|
+
for (const [name, value] of bindings.attributes) {
|
|
142
|
+
target.removeAttribute(name);
|
|
143
|
+
host.setAttribute(name, value);
|
|
144
|
+
}
|
|
145
|
+
for (const name of bindings.classes) {
|
|
146
|
+
target.classList.remove(name);
|
|
147
|
+
host.classList.add(name);
|
|
148
|
+
}
|
|
149
|
+
for (const [name, value] of bindings.styles) {
|
|
150
|
+
;
|
|
151
|
+
target.style.removeProperty(name);
|
|
152
|
+
host.style.setProperty(name, value);
|
|
153
|
+
}
|
|
154
|
+
bindings.attributes.clear();
|
|
155
|
+
bindings.classes.clear();
|
|
156
|
+
bindings.styles.clear();
|
|
157
|
+
bindings.target = null;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Moves everything the consumer wrote on the host onto the root that stands
|
|
161
|
+
* for it.
|
|
162
|
+
*
|
|
163
|
+
* @param host - The host.
|
|
164
|
+
* @param bindings - Its bindings, with `target` set.
|
|
165
|
+
*/
|
|
166
|
+
function moveTagAttributes(host, bindings) {
|
|
167
|
+
const target = bindings.target;
|
|
168
|
+
for (const attribute of Array.from(host.attributes)) {
|
|
169
|
+
const name = attribute.name;
|
|
170
|
+
if (isFrameworkAttribute(name) || bindings.except.has(name) || bindings.inputs().has(name))
|
|
171
|
+
continue;
|
|
172
|
+
if (name === 'class') {
|
|
173
|
+
for (const token of Array.from(host.classList)) {
|
|
174
|
+
target.classList.add(token);
|
|
175
|
+
bindings.classes.add(token);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else if (name === 'style') {
|
|
179
|
+
const style = host.style;
|
|
180
|
+
for (const property of Array.from(style)) {
|
|
181
|
+
const value = style.getPropertyValue(property);
|
|
182
|
+
target.style.setProperty(property, value, style.getPropertyPriority(property));
|
|
183
|
+
bindings.styles.set(property, value);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
target.setAttribute(name, attribute.value);
|
|
188
|
+
bindings.attributes.set(name, attribute.value);
|
|
189
|
+
}
|
|
190
|
+
host.removeAttribute(name);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Applies what the consumer bound on a component's tag to the DOM the
|
|
195
|
+
* component rendered in its host's place; see {@link setHostRoot}.
|
|
196
|
+
*
|
|
197
|
+
* @param host - The host.
|
|
198
|
+
* @param settled - Whether the render is complete (the moment to refuse a
|
|
199
|
+
* binding that has nowhere to land).
|
|
200
|
+
*/
|
|
201
|
+
function syncTagBindings(host, settled) {
|
|
202
|
+
const bindings = tagBindings.get(host);
|
|
203
|
+
if (bindings === undefined)
|
|
204
|
+
return;
|
|
205
|
+
// Nothing bound on the tag (the usual case): nothing to read.
|
|
206
|
+
if (bindings.listeners.size === 0 && bindings.target === null && !host.hasAttributes())
|
|
207
|
+
return;
|
|
208
|
+
const absent = isAbsent(host);
|
|
209
|
+
const named = bindings.named;
|
|
210
|
+
const first = leaders.get(host);
|
|
211
|
+
// Listeners: the named root, else every element in the host's place (the
|
|
212
|
+
// events that bubbled to the host while it enclosed them).
|
|
213
|
+
const roots = !absent
|
|
214
|
+
? []
|
|
215
|
+
: named !== undefined
|
|
216
|
+
? named === null
|
|
217
|
+
? []
|
|
218
|
+
: [named]
|
|
219
|
+
: bindings.listeners.size > 0
|
|
220
|
+
? ledElements(host)
|
|
221
|
+
: [];
|
|
222
|
+
for (const listener of bindings.listeners) {
|
|
223
|
+
// Angular also listens natively for a name that is one of the component's outputs; the consumer means the output.
|
|
224
|
+
if (bindings.outputs().has(listener.name))
|
|
225
|
+
continue;
|
|
226
|
+
for (const [root, detach] of listener.attached) {
|
|
227
|
+
if (roots.includes(root))
|
|
228
|
+
continue;
|
|
229
|
+
detach();
|
|
230
|
+
listener.attached.delete(root);
|
|
231
|
+
}
|
|
232
|
+
if (bindings.renderer === null)
|
|
233
|
+
continue;
|
|
234
|
+
for (const root of roots) {
|
|
235
|
+
if (listener.attached.has(root))
|
|
236
|
+
continue;
|
|
237
|
+
const listen = bindings.renderer.listen;
|
|
238
|
+
listener.attached.set(root, listen.call(bindings.renderer, root, listener.name, listener.callback, listener.options));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// Attributes, classes and styles: the named root, else the component's one element.
|
|
242
|
+
const single = first?.length === 1 && !first[0].content && first[0].node.nodeType === 1
|
|
243
|
+
? first[0].node
|
|
244
|
+
: null;
|
|
245
|
+
const target = !absent ? null : named !== undefined ? named : single;
|
|
246
|
+
if (target !== bindings.target) {
|
|
247
|
+
returnTagAttributes(host, bindings);
|
|
248
|
+
bindings.target = target;
|
|
249
|
+
}
|
|
250
|
+
if (target !== null)
|
|
251
|
+
moveTagAttributes(host, bindings);
|
|
252
|
+
if (!settled || !absent || !isDevMode())
|
|
253
|
+
return;
|
|
254
|
+
const nowhere = named === undefined && (first?.length ?? 0) === 0;
|
|
255
|
+
const unheard = Array.from(bindings.listeners).filter((listener) => !bindings.outputs().has(listener.name));
|
|
256
|
+
if (nowhere && unheard.length > 0) {
|
|
257
|
+
const names = unheard.map((listener) => `(${listener.name})`).join(', ');
|
|
258
|
+
throw new Error(`${names} is bound on <${bindings.tag}>, which renders no element in its place, so it can never fire. ` +
|
|
259
|
+
`Bind it on an element inside, or use the component's own outputs.`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Names the element that stands for the calling component's host: React's
|
|
264
|
+
* `{...rest}` target. Call it in the component's {@link usePlacement}
|
|
265
|
+
* function, for a component whose host is out of the page.
|
|
266
|
+
*
|
|
267
|
+
* An Angular consumer binds on the component's tag: `<mol-switch
|
|
268
|
+
* (keydown)="…" class="…" data-testid="…" [attr.aria-label]="…">`. With the
|
|
269
|
+
* host out of the page those would do nothing. Instead, while the host is
|
|
270
|
+
* absent:
|
|
271
|
+
*
|
|
272
|
+
* - native listeners are attached to the root (`$event.currentTarget` is the
|
|
273
|
+
* root, as it is in React);
|
|
274
|
+
* - attributes, class names and styles the consumer writes on the tag are
|
|
275
|
+
* written on the root, in the same instruction, and the ones already on the
|
|
276
|
+
* host are moved there.
|
|
277
|
+
*
|
|
278
|
+
* A component that calls nothing gets the common case for free: with one
|
|
279
|
+
* element in its place (`placeElementBefore(root, host)`) that element is the
|
|
280
|
+
* root; with several elements or projected content in its place, listeners go
|
|
281
|
+
* to each of them (the events that used to bubble to the host) and attributes
|
|
282
|
+
* stay on the host. A component with nothing in its place (a portal) names its
|
|
283
|
+
* root, or a listener on its tag throws in dev mode: it could never fire.
|
|
284
|
+
*
|
|
285
|
+
* @param host - The component's host element.
|
|
286
|
+
* @param root - The element that stands for it, or `null` while React renders
|
|
287
|
+
* none.
|
|
288
|
+
* @param options - `except`: attribute names the component reads off its host
|
|
289
|
+
* itself (it merges them with its own), which stay on the host.
|
|
290
|
+
* @param options.except - Those attribute names.
|
|
291
|
+
*/
|
|
292
|
+
export function setHostRoot(host, root, options = {}) {
|
|
293
|
+
const bindings = tagBindings.get(host);
|
|
294
|
+
if (bindings === undefined) {
|
|
295
|
+
throw new Error('setHostRoot() is called in the usePlacement() function of the component whose host it is.');
|
|
296
|
+
}
|
|
297
|
+
bindings.named = root ?? null;
|
|
298
|
+
if (options.except !== undefined)
|
|
299
|
+
bindings.except = new Set(options.except);
|
|
300
|
+
}
|
|
301
|
+
/** The placement function that is running now. */
|
|
302
|
+
let running = null;
|
|
303
|
+
/** A parent whose child list a placement function read → the functions to run again when Angular changes it. */
|
|
304
|
+
const contentReaders = new WeakMap();
|
|
305
|
+
/**
|
|
306
|
+
* Records that the running placement function depends on `parent`'s children.
|
|
307
|
+
*
|
|
308
|
+
* @param parent - The capture, or the parent its content was moved to.
|
|
309
|
+
*/
|
|
310
|
+
function readBy(parent) {
|
|
311
|
+
if (parent === null || running === null)
|
|
312
|
+
return;
|
|
313
|
+
let readers = contentReaders.get(parent);
|
|
314
|
+
if (readers === undefined)
|
|
315
|
+
contentReaders.set(parent, (readers = new Set()));
|
|
316
|
+
readers.add(running);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Runs again every placement function that read `parent`'s children: Angular
|
|
320
|
+
* just added, removed or changed one of them.
|
|
321
|
+
*
|
|
322
|
+
* @param parent - The parent Angular changed.
|
|
323
|
+
*/
|
|
324
|
+
function contentChanged(parent) {
|
|
325
|
+
if (parent === null)
|
|
326
|
+
return;
|
|
327
|
+
const readers = contentReaders.get(parent);
|
|
328
|
+
if (readers === undefined)
|
|
329
|
+
return;
|
|
330
|
+
for (const placement of Array.from(readers)) {
|
|
331
|
+
if (placement.destroyed)
|
|
332
|
+
readers.delete(placement);
|
|
333
|
+
else
|
|
334
|
+
placement.run();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
45
337
|
/**
|
|
46
338
|
* Whether Angular's node is one React child: an element, or a text node that
|
|
47
339
|
* is not template indentation. Angular creates an interpolated text node empty
|
|
48
340
|
* and fills it in the same pass, so an empty text node is a child; comments are
|
|
49
341
|
* Angular's own block anchors.
|
|
50
342
|
*
|
|
343
|
+
* Whitespace-only text is indentation, on one line too: Angular collapses a
|
|
344
|
+
* template's line breaks around an interpolation into spaces in the same text
|
|
345
|
+
* node (`\n {{ x }}\n` renders `' '` while `x` is empty), the
|
|
346
|
+
* formatting JSX drops when it compiles. A consumer's deliberate `{{ ' ' }}`
|
|
347
|
+
* renders the same DOM, so it reads as no content here, where React's
|
|
348
|
+
* `slot(' ')` counts it; nothing in the page tells the two apart.
|
|
349
|
+
*
|
|
51
350
|
* @param node - The node Angular is placing.
|
|
52
351
|
* @returns Whether the node gets its own wrapper.
|
|
53
352
|
*/
|
|
54
353
|
function isChild(node) {
|
|
55
|
-
|
|
354
|
+
// A lazy slot is one child, stamped or not; its content is stamped inside the anchor's wrapper.
|
|
355
|
+
if (node.nodeType === 1 || isLazySlotAnchor(node))
|
|
56
356
|
return true;
|
|
57
357
|
if (node.nodeType !== 3)
|
|
58
358
|
return false;
|
|
@@ -69,6 +369,13 @@ function isChild(node) {
|
|
|
69
369
|
*/
|
|
70
370
|
function placeOf(ref, parent) {
|
|
71
371
|
let node = placeholders.get(ref) ?? ref;
|
|
372
|
+
// What stands in front of `ref` is part of it: a node goes before all of it.
|
|
373
|
+
const leader = leaders.get(ref)?.[0];
|
|
374
|
+
if (leader !== undefined) {
|
|
375
|
+
const first = leader.content ? contentRanges.get(leader.node)?.start : leader.node;
|
|
376
|
+
if (first !== undefined && first.parentNode === node.parentNode)
|
|
377
|
+
node = first;
|
|
378
|
+
}
|
|
72
379
|
while (node.parentNode !== null && node.parentNode !== parent) {
|
|
73
380
|
if (!wrapperOwners.has(node.parentNode))
|
|
74
381
|
break;
|
|
@@ -76,6 +383,59 @@ function placeOf(ref, parent) {
|
|
|
76
383
|
}
|
|
77
384
|
return node;
|
|
78
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Forgets that an element, or a capture's content, stands in front of another
|
|
388
|
+
* node: it is back in its own place, or Angular is placing it itself.
|
|
389
|
+
*
|
|
390
|
+
* @param node - The element, or the capture.
|
|
391
|
+
* @param content - Whether it is the capture's content.
|
|
392
|
+
*/
|
|
393
|
+
function stopLeading(node, content = false) {
|
|
394
|
+
const map = content ? ledContent : led;
|
|
395
|
+
const sibling = map.get(node);
|
|
396
|
+
if (sibling === undefined)
|
|
397
|
+
return;
|
|
398
|
+
map.delete(node);
|
|
399
|
+
const rest = (leaders.get(sibling) ?? []).filter((leader) => leader.node !== node || leader.content !== content);
|
|
400
|
+
if (rest.length > 0)
|
|
401
|
+
leaders.set(sibling, rest);
|
|
402
|
+
else
|
|
403
|
+
leaders.delete(sibling);
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Puts what stands in front of `node` in front of it again: Angular just
|
|
407
|
+
* inserted or moved `node` alone.
|
|
408
|
+
*
|
|
409
|
+
* @param node - The node Angular placed.
|
|
410
|
+
*/
|
|
411
|
+
function leadAgain(node) {
|
|
412
|
+
const list = leaders.get(node);
|
|
413
|
+
if (list === undefined)
|
|
414
|
+
return;
|
|
415
|
+
// An absent node, or one rendered elsewhere, holds its place with its placeholder.
|
|
416
|
+
const place = placeholders.get(node) ?? node;
|
|
417
|
+
const parent = place.parentNode;
|
|
418
|
+
if (parent === null)
|
|
419
|
+
return;
|
|
420
|
+
let next = place;
|
|
421
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
422
|
+
const leader = list[i];
|
|
423
|
+
if (!leader.content) {
|
|
424
|
+
if (leader.node.nextSibling !== next)
|
|
425
|
+
parent.insertBefore(leader.node, next);
|
|
426
|
+
next = leader.node;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
const range = contentRanges.get(leader.node);
|
|
430
|
+
if (range === undefined)
|
|
431
|
+
continue;
|
|
432
|
+
if (range.end.nextSibling !== next)
|
|
433
|
+
for (const part of runOf(range))
|
|
434
|
+
parent.insertBefore(part, next);
|
|
435
|
+
runParents.add(parent);
|
|
436
|
+
next = range.start;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
79
439
|
/**
|
|
80
440
|
* Forgets an absent element's placeholder: Angular is removing or re-placing
|
|
81
441
|
* the element, so the old place is no longer its place.
|
|
@@ -88,6 +448,7 @@ function dropPlaceholder(node) {
|
|
|
88
448
|
return;
|
|
89
449
|
placeholders.delete(node);
|
|
90
450
|
elsewhere.delete(node);
|
|
451
|
+
stopLeading(node);
|
|
91
452
|
const wrapper = placeholder.parentNode;
|
|
92
453
|
placeholder.remove();
|
|
93
454
|
dropEmptyWrapper(wrapper);
|
|
@@ -108,24 +469,37 @@ function dropEmptyWrapper(node) {
|
|
|
108
469
|
owner.refresh();
|
|
109
470
|
}
|
|
110
471
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
472
|
+
* Puts everything whose place is inside a subtree Angular is removing back in
|
|
473
|
+
* that place, so it leaves the page with the subtree (React unmounts a portal
|
|
474
|
+
* and a slot with the element that rendered them) and is there again if
|
|
475
|
+
* Angular re-inserts the subtree: an element {@link placeElement} rendered in
|
|
476
|
+
* another parent goes back to its placeholder, a capture's content
|
|
477
|
+
* {@link placeContent} moved goes back inside the capture. It walks the removed
|
|
478
|
+
* nodes once and follows what it puts back, so nesting needs no order.
|
|
113
479
|
*
|
|
114
|
-
* @param
|
|
480
|
+
* @param node - The node Angular is removing, then each node below it.
|
|
115
481
|
*/
|
|
116
|
-
function
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
482
|
+
function takeAlong(node) {
|
|
483
|
+
const range = contentRanges.get(node);
|
|
484
|
+
if (range !== undefined && range.start.parentNode !== node)
|
|
485
|
+
returnContent(node, range);
|
|
486
|
+
const held = heldPlaces.get(node);
|
|
487
|
+
if (held !== undefined) {
|
|
488
|
+
if (elsewhere.has(held)) {
|
|
489
|
+
elsewhere.delete(held);
|
|
490
|
+
placeholders.delete(held);
|
|
491
|
+
stopLeading(held);
|
|
492
|
+
node.replaceWith(held);
|
|
122
493
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
placeholders.delete(element);
|
|
127
|
-
element.remove();
|
|
494
|
+
// An absent element is not below its placeholder; what it holds is.
|
|
495
|
+
takeAlong(held);
|
|
496
|
+
return;
|
|
128
497
|
}
|
|
498
|
+
if (node.firstChild === null)
|
|
499
|
+
return;
|
|
500
|
+
// A copy: putting something back moves nodes of this list.
|
|
501
|
+
for (const child of Array.from(node.childNodes))
|
|
502
|
+
takeAlong(child);
|
|
129
503
|
}
|
|
130
504
|
/**
|
|
131
505
|
* Restores an absent host as soon as Angular renders a node into it, so the
|
|
@@ -160,7 +534,12 @@ function withPlacement(renderer) {
|
|
|
160
534
|
}
|
|
161
535
|
dropPlaceholder(child);
|
|
162
536
|
const from = child.parentNode;
|
|
163
|
-
|
|
537
|
+
// A lazy slot's content goes right before its anchor, wherever a component placed the anchor
|
|
538
|
+
// (placeElement): Angular names the slot's own parent, where the anchor no longer is.
|
|
539
|
+
const lazyAnchor = ref !== null && isLazySlotAnchor(ref) && ref.parentNode !== null;
|
|
540
|
+
if (lazyAnchor)
|
|
541
|
+
parent = ref.parentNode;
|
|
542
|
+
const before = ref === null ? null : lazyAnchor ? ref : placeOf(ref, parent);
|
|
164
543
|
const owner = wrappingParents.get(parent);
|
|
165
544
|
let node = child;
|
|
166
545
|
if (owner !== undefined && isChild(child)) {
|
|
@@ -171,21 +550,119 @@ function withPlacement(renderer) {
|
|
|
171
550
|
renderer.appendChild(parent, node);
|
|
172
551
|
else
|
|
173
552
|
renderer.insertBefore(parent, node, before, isMove);
|
|
553
|
+
leadAgain(child);
|
|
174
554
|
if (from !== parent)
|
|
175
555
|
dropEmptyWrapper(from);
|
|
176
556
|
owner?.refresh();
|
|
177
557
|
restoreAbsentHostOf(parent);
|
|
558
|
+
contentChanged(parent);
|
|
178
559
|
};
|
|
179
560
|
placed.appendChild = (parent, child) => insert(parent, child, null);
|
|
180
561
|
placed.insertBefore = (parent, child, ref, isMove) => insert(parent, child, ref ?? null, isMove);
|
|
181
562
|
placed.removeChild = (parent, child, ...rest) => {
|
|
563
|
+
if (rendersElsewhere)
|
|
564
|
+
takeAlong(child);
|
|
182
565
|
dropPlaceholder(child);
|
|
183
566
|
const from = child.parentNode;
|
|
184
567
|
renderer.removeChild(parent, child, ...rest);
|
|
185
568
|
dropEmptyWrapper(from);
|
|
186
|
-
|
|
187
|
-
|
|
569
|
+
contentChanged(from);
|
|
570
|
+
};
|
|
571
|
+
placed.setValue = (node, value) => {
|
|
572
|
+
renderer.setValue(node, value);
|
|
573
|
+
contentChanged(node.parentNode);
|
|
574
|
+
};
|
|
575
|
+
// What the consumer binds on the tag of a component that places its own DOM
|
|
576
|
+
// lands on the root that stands for the host (setHostRoot).
|
|
577
|
+
placed.listen = ((target, name, callback, options) => {
|
|
578
|
+
const listen = renderer.listen;
|
|
579
|
+
const off = listen.call(renderer, target, name, callback, options);
|
|
580
|
+
const bindings = typeof target === 'object' && target !== null ? tagBindings.get(target) : undefined;
|
|
581
|
+
if (bindings === undefined)
|
|
582
|
+
return off;
|
|
583
|
+
const listener = { name, callback, options, attached: new Map() };
|
|
584
|
+
// The decorated renderer: a root that is itself such a host passes the listener on.
|
|
585
|
+
bindings.renderer = placed;
|
|
586
|
+
bindings.listeners.add(listener);
|
|
587
|
+
syncTagBindings(target, false);
|
|
588
|
+
return () => {
|
|
589
|
+
off();
|
|
590
|
+
bindings.listeners.delete(listener);
|
|
591
|
+
for (const detach of listener.attached.values())
|
|
592
|
+
detach();
|
|
593
|
+
listener.attached.clear();
|
|
594
|
+
};
|
|
595
|
+
});
|
|
596
|
+
const forwards = (el, name) => {
|
|
597
|
+
const bindings = tagBindings.get(el);
|
|
598
|
+
if (bindings === undefined || bindings.target === null)
|
|
599
|
+
return undefined;
|
|
600
|
+
return isFrameworkAttribute(name) || bindings.except.has(name) ? undefined : bindings;
|
|
601
|
+
};
|
|
602
|
+
placed.setAttribute = (el, name, value, namespace) => {
|
|
603
|
+
const bindings = namespace ? undefined : forwards(el, name);
|
|
604
|
+
if (bindings === undefined || name === 'class' || name === 'style') {
|
|
605
|
+
renderer.setAttribute(el, name, value, namespace);
|
|
606
|
+
if (bindings !== undefined)
|
|
607
|
+
moveTagAttributes(el, bindings);
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
;
|
|
611
|
+
bindings.target.setAttribute(name, value);
|
|
612
|
+
bindings.attributes.set(name, value);
|
|
613
|
+
};
|
|
614
|
+
placed.removeAttribute = (el, name, namespace) => {
|
|
615
|
+
const bindings = namespace ? undefined : forwards(el, name);
|
|
616
|
+
if (bindings === undefined || !bindings.attributes.has(name)) {
|
|
617
|
+
renderer.removeAttribute(el, name, namespace);
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
;
|
|
621
|
+
bindings.target.removeAttribute(name);
|
|
622
|
+
bindings.attributes.delete(name);
|
|
623
|
+
};
|
|
624
|
+
placed.addClass = (el, name) => {
|
|
625
|
+
const bindings = forwards(el, 'class');
|
|
626
|
+
if (bindings === undefined) {
|
|
627
|
+
renderer.addClass(el, name);
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
;
|
|
631
|
+
bindings.target.classList.add(name);
|
|
632
|
+
bindings.classes.add(name);
|
|
188
633
|
};
|
|
634
|
+
placed.removeClass = (el, name) => {
|
|
635
|
+
const bindings = forwards(el, 'class');
|
|
636
|
+
if (bindings === undefined || !bindings.classes.has(name)) {
|
|
637
|
+
renderer.removeClass(el, name);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
;
|
|
641
|
+
bindings.target.classList.remove(name);
|
|
642
|
+
bindings.classes.delete(name);
|
|
643
|
+
};
|
|
644
|
+
placed.setStyle = ((el, style, value, flags) => {
|
|
645
|
+
const bindings = forwards(el, 'style');
|
|
646
|
+
if (bindings === undefined) {
|
|
647
|
+
;
|
|
648
|
+
renderer.setStyle(el, style, value, flags);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
;
|
|
652
|
+
renderer.setStyle(bindings.target, style, value, flags);
|
|
653
|
+
bindings.styles.set(style, value);
|
|
654
|
+
});
|
|
655
|
+
placed.removeStyle = ((el, style, flags) => {
|
|
656
|
+
const bindings = forwards(el, 'style');
|
|
657
|
+
if (bindings === undefined || !bindings.styles.has(style)) {
|
|
658
|
+
;
|
|
659
|
+
renderer.removeStyle(el, style, flags);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
;
|
|
663
|
+
renderer.removeStyle(bindings.target, style, flags);
|
|
664
|
+
bindings.styles.delete(style);
|
|
665
|
+
});
|
|
189
666
|
return placed;
|
|
190
667
|
}
|
|
191
668
|
/**
|
|
@@ -237,6 +714,8 @@ function assertDomPlacement(user) {
|
|
|
237
714
|
* holds an `<ng-content>`). "Attached" means the element has a parent, not
|
|
238
715
|
* that it is in the document, so it also works inside a host that is itself
|
|
239
716
|
* absent. An element that was never taken out is left where Angular put it.
|
|
717
|
+
* An element rendered elsewhere ({@link placeElement}, {@link placeElementBefore})
|
|
718
|
+
* leaves that parent too, and `true` puts it back in its own place.
|
|
240
719
|
* Angular may remove or move the element while it is absent; its placeholder
|
|
241
720
|
* goes then.
|
|
242
721
|
*
|
|
@@ -252,16 +731,42 @@ export function setElementPresent(element, present) {
|
|
|
252
731
|
return;
|
|
253
732
|
placeholders.delete(element);
|
|
254
733
|
elsewhere.delete(element);
|
|
734
|
+
stopLeading(element);
|
|
255
735
|
// A placeholder whose parent was removed meanwhile has nowhere to restore to.
|
|
256
736
|
if (placeholder.parentNode !== null)
|
|
257
737
|
placeholder.replaceWith(element);
|
|
258
738
|
}
|
|
739
|
+
else if (placeholder !== undefined && elsewhere.has(element)) {
|
|
740
|
+
// Rendered elsewhere (placeElement, placeElementBefore): it leaves that
|
|
741
|
+
// parent, and the placeholder it already has keeps its own place.
|
|
742
|
+
elsewhere.delete(element);
|
|
743
|
+
stopLeading(element);
|
|
744
|
+
element.remove();
|
|
745
|
+
}
|
|
259
746
|
else if (placeholder === undefined && element.parentNode !== null) {
|
|
260
747
|
const comment = element.ownerDocument.createComment('');
|
|
261
748
|
element.replaceWith(comment);
|
|
262
749
|
placeholders.set(element, comment);
|
|
750
|
+
heldPlaces.set(comment, element);
|
|
263
751
|
}
|
|
264
752
|
}
|
|
753
|
+
/**
|
|
754
|
+
* The element a placeholder stands in for while the element is out of its own
|
|
755
|
+
* place ({@link setElementPresent}, {@link placeElement},
|
|
756
|
+
* {@link placeElementBefore}). A slot's children ({@link SlotReader.nodes})
|
|
757
|
+
* list such an element as its placeholder; a component that took one of its
|
|
758
|
+
* slot's children out of the page finds that child again with this.
|
|
759
|
+
*
|
|
760
|
+
* @param node - A node of the rendered tree.
|
|
761
|
+
* @returns The element whose place the node holds, or `undefined` when the
|
|
762
|
+
* node is not a current placeholder.
|
|
763
|
+
*/
|
|
764
|
+
export function heldElement(node) {
|
|
765
|
+
if (!node)
|
|
766
|
+
return undefined;
|
|
767
|
+
const held = heldPlaces.get(node);
|
|
768
|
+
return held !== undefined && placeholders.get(held) === node ? held : undefined;
|
|
769
|
+
}
|
|
265
770
|
/**
|
|
266
771
|
* Renders an element in another parent while it keeps its place in Angular's
|
|
267
772
|
* view: Angular's equivalent of React's `createPortal(child, document.body)`.
|
|
@@ -271,9 +776,6 @@ export function setElementPresent(element, present) {
|
|
|
271
776
|
* it is elsewhere, the element follows Angular and the next call moves it
|
|
272
777
|
* again. When Angular removes the view that holds its place, the element is
|
|
273
778
|
* removed from the other parent too, as React unmounts a portal.
|
|
274
|
-
*
|
|
275
|
-
* @param element - The element (nothing happens without one).
|
|
276
|
-
* @param target - The parent to render it in, or `null` for its own place.
|
|
277
779
|
*/
|
|
278
780
|
export function placeElement(element, target) {
|
|
279
781
|
if (!element)
|
|
@@ -286,13 +788,169 @@ export function placeElement(element, target) {
|
|
|
286
788
|
const comment = element.ownerDocument.createComment('');
|
|
287
789
|
element.replaceWith(comment);
|
|
288
790
|
placeholders.set(element, comment);
|
|
791
|
+
heldPlaces.set(comment, element);
|
|
289
792
|
}
|
|
793
|
+
stopLeading(element);
|
|
290
794
|
elsewhere.add(element);
|
|
795
|
+
rendersElsewhere = true;
|
|
291
796
|
if (element.parentNode !== target)
|
|
292
797
|
target.appendChild(element);
|
|
293
798
|
}
|
|
799
|
+
/**
|
|
800
|
+
* Renders an element of the component's template immediately in front of
|
|
801
|
+
* another node, as its sibling: Angular's equivalent of a React component whose
|
|
802
|
+
* root is a fragment, `<><div>{children}</div>{portal}</>`, where the node is
|
|
803
|
+
* the component's host (kept in the DOM with `display: contents`).
|
|
804
|
+
*
|
|
805
|
+
* The element then belongs to the node's place. Angular inserts a new sibling
|
|
806
|
+
* before the element, not between the two; when Angular moves the node (a
|
|
807
|
+
* consumer `@for` reorders), the element moves with it; when Angular removes
|
|
808
|
+
* the node, the element goes back to its own place inside it and leaves the
|
|
809
|
+
* page with it. Several elements placed before the same node stand in the
|
|
810
|
+
* order they were placed. Calling it again moves nothing while the element
|
|
811
|
+
* already stands there.
|
|
812
|
+
*
|
|
813
|
+
* "In front of the node" means in front of the node's place. A host that is
|
|
814
|
+
* absent (`setElementPresent(host, false)`) holds its place with a comment, and
|
|
815
|
+
* the element stands in front of that: this is how a fragment root renders
|
|
816
|
+
* React's exact DOM, with no host element among the siblings at all. A node
|
|
817
|
+
* another component rendered elsewhere ({@link placeElement}) is likewise
|
|
818
|
+
* led at its place, not where it was rendered.
|
|
819
|
+
*
|
|
820
|
+
* Nothing happens while the node has no parent; call it
|
|
821
|
+
* from the component's {@link usePlacement} function, which runs again.
|
|
822
|
+
* `placeElement(element, null)` puts the element back in its own place.
|
|
823
|
+
*
|
|
824
|
+
* @param element - The element (nothing happens without one).
|
|
825
|
+
* @param sibling - The node it stands in front of, usually the host.
|
|
826
|
+
*/
|
|
827
|
+
export function placeElementBefore(element, sibling) {
|
|
828
|
+
if (!element || !sibling || (placeholders.get(sibling) ?? sibling).parentNode === null)
|
|
829
|
+
return;
|
|
830
|
+
if (led.get(element) !== sibling) {
|
|
831
|
+
stopLeading(element);
|
|
832
|
+
if (!placeholders.has(element) && element.parentNode !== null) {
|
|
833
|
+
const comment = element.ownerDocument.createComment('');
|
|
834
|
+
element.replaceWith(comment);
|
|
835
|
+
placeholders.set(element, comment);
|
|
836
|
+
heldPlaces.set(comment, element);
|
|
837
|
+
}
|
|
838
|
+
elsewhere.add(element);
|
|
839
|
+
rendersElsewhere = true;
|
|
840
|
+
led.set(element, sibling);
|
|
841
|
+
leaders.set(sibling, [...(leaders.get(sibling) ?? []), { node: element, content: false }]);
|
|
842
|
+
}
|
|
843
|
+
leadAgain(sibling);
|
|
844
|
+
}
|
|
294
845
|
/** A capture element → the comments that bracket its content wherever it is. */
|
|
295
846
|
const contentRanges = new WeakMap();
|
|
847
|
+
/** One of those comments → its capture. */
|
|
848
|
+
const rangeCaptures = new WeakMap();
|
|
849
|
+
/**
|
|
850
|
+
* The parents a capture's content was rendered in, outside the capture. Only
|
|
851
|
+
* their children can belong to a run, so only they cost {@link renderedParent}
|
|
852
|
+
* a look at the siblings. A parent the content left stays listed; it costs the
|
|
853
|
+
* look and nothing else.
|
|
854
|
+
*/
|
|
855
|
+
const runParents = new WeakSet();
|
|
856
|
+
/**
|
|
857
|
+
* A node's parent in the rendered tree: the parent React context follows.
|
|
858
|
+
* It is the DOM parent, except where this module placed the node:
|
|
859
|
+
*
|
|
860
|
+
* - an element that is absent, or rendered in another parent
|
|
861
|
+
* ({@link setElementPresent}, {@link placeElement},
|
|
862
|
+
* {@link placeElementBefore}), belongs where its placeholder is, the place
|
|
863
|
+
* the template gave it. A portal's content therefore has the component that
|
|
864
|
+
* declared it as an ancestor, not `document.body`, as in React.
|
|
865
|
+
* - a top-level node of a capture's content that renders outside the capture
|
|
866
|
+
* ({@link placeContent}, {@link placeContentBefore}) belongs to the capture.
|
|
867
|
+
* With `placeContentBefore(host, host)` that is the host: what a provider
|
|
868
|
+
* renders in its own place is the provider's child, and a sibling that merely
|
|
869
|
+
* shares the DOM parent is not.
|
|
870
|
+
*
|
|
871
|
+
* So a component looks its context up from its own host and registers one on
|
|
872
|
+
* its own host, whether or not the host is in the page.
|
|
873
|
+
*
|
|
874
|
+
* @param node - The node.
|
|
875
|
+
* @returns Its parent in the rendered tree, or `null` at the top (or for a
|
|
876
|
+
* node that has no place: never inserted, or removed).
|
|
877
|
+
*/
|
|
878
|
+
export function renderedParent(node) {
|
|
879
|
+
if (!node)
|
|
880
|
+
return null;
|
|
881
|
+
const place = placeholders.get(node) ?? node;
|
|
882
|
+
const parent = place.parentNode;
|
|
883
|
+
if (parent === null)
|
|
884
|
+
return null;
|
|
885
|
+
if (runParents.has(parent)) {
|
|
886
|
+
for (let prev = place.previousSibling; prev !== null; prev = prev.previousSibling) {
|
|
887
|
+
if (prev.nodeType !== 8)
|
|
888
|
+
continue;
|
|
889
|
+
const capture = rangeCaptures.get(prev);
|
|
890
|
+
if (capture === undefined)
|
|
891
|
+
continue;
|
|
892
|
+
const range = contentRanges.get(capture);
|
|
893
|
+
// Inside this run: its capture is the parent.
|
|
894
|
+
if (prev === range.start)
|
|
895
|
+
return capture;
|
|
896
|
+
// An earlier run ended here: none of it encloses the node.
|
|
897
|
+
prev = range.start;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return parent.nodeType === 1 ? parent : null;
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Moves a capture's content from wherever it was rendered back inside the
|
|
904
|
+
* capture: the place Angular put it, and where Angular takes it from when it
|
|
905
|
+
* projects it again.
|
|
906
|
+
*
|
|
907
|
+
* @param capture - The element Angular projected the content into.
|
|
908
|
+
* @param range - The comments that bracket the content.
|
|
909
|
+
* @param range.start - The comment before the content.
|
|
910
|
+
* @param range.end - The comment after the content.
|
|
911
|
+
*/
|
|
912
|
+
function returnContent(capture, range) {
|
|
913
|
+
stopLeading(capture, true);
|
|
914
|
+
for (const node of runOf(range))
|
|
915
|
+
capture.appendChild(node);
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* A capture's content with the two comments that bracket it.
|
|
919
|
+
*
|
|
920
|
+
* @param range - The comments that bracket the content.
|
|
921
|
+
* @param range.start - The comment before the content.
|
|
922
|
+
* @param range.end - The comment after the content.
|
|
923
|
+
* @returns The nodes, in order.
|
|
924
|
+
*/
|
|
925
|
+
function runOf(range) {
|
|
926
|
+
const run = [];
|
|
927
|
+
for (let node = range.start; node !== null; node = node.nextSibling) {
|
|
928
|
+
run.push(node);
|
|
929
|
+
if (node === range.end)
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
return run;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* The comments that bracket a capture's content, created around the content
|
|
936
|
+
* the first time.
|
|
937
|
+
*
|
|
938
|
+
* @param capture - The element Angular projected the content into.
|
|
939
|
+
* @returns The two comments.
|
|
940
|
+
*/
|
|
941
|
+
function rangeOf(capture) {
|
|
942
|
+
let range = contentRanges.get(capture);
|
|
943
|
+
if (range === undefined) {
|
|
944
|
+
const document = capture.ownerDocument;
|
|
945
|
+
range = { start: document.createComment(''), end: document.createComment('') };
|
|
946
|
+
capture.prepend(range.start);
|
|
947
|
+
capture.append(range.end);
|
|
948
|
+
contentRanges.set(capture, range);
|
|
949
|
+
rangeCaptures.set(range.start, capture);
|
|
950
|
+
rangeCaptures.set(range.end, capture);
|
|
951
|
+
}
|
|
952
|
+
return range;
|
|
953
|
+
}
|
|
296
954
|
/**
|
|
297
955
|
* Renders a capture element's content somewhere else: Angular's equivalent of
|
|
298
956
|
* React placing the same `{slot}` in a different parent depending on props
|
|
@@ -304,7 +962,8 @@ const contentRanges = new WeakMap();
|
|
|
304
962
|
* view under the parent of its anchor, before a node of the same run. Never
|
|
305
963
|
* move a filtered or remembered list of nodes instead; the anchors or the
|
|
306
964
|
* nodes Angular adds later stay behind (NG05106). Calling it again with the
|
|
307
|
-
* same target moves nothing.
|
|
965
|
+
* same target moves nothing. When Angular removes the capture (its view went),
|
|
966
|
+
* the content goes back inside it and leaves the page with it.
|
|
308
967
|
*
|
|
309
968
|
* @param capture - The element Angular projected the content into.
|
|
310
969
|
* @param target - The parent to render the content in, or `null` to put it
|
|
@@ -315,54 +974,284 @@ const contentRanges = new WeakMap();
|
|
|
315
974
|
export function placeContent(capture, target, before = null) {
|
|
316
975
|
if (!capture)
|
|
317
976
|
return;
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
capture.append(range.end);
|
|
326
|
-
contentRanges.set(capture, range);
|
|
977
|
+
if (target === null && !contentRanges.has(capture))
|
|
978
|
+
return;
|
|
979
|
+
const range = rangeOf(capture);
|
|
980
|
+
if (target === null) {
|
|
981
|
+
if (range.start.parentNode !== capture)
|
|
982
|
+
returnContent(capture, range);
|
|
983
|
+
return;
|
|
327
984
|
}
|
|
328
|
-
|
|
329
|
-
|
|
985
|
+
// Rendered here from now on, wherever placeContentBefore had it follow a node.
|
|
986
|
+
const followed = ledContent.has(capture);
|
|
987
|
+
stopLeading(capture, true);
|
|
988
|
+
if (range.start.parentNode === target && !followed)
|
|
330
989
|
return;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
990
|
+
// An absent element holds its place with its placeholder.
|
|
991
|
+
const place = before === null ? null : (placeholders.get(before) ?? before);
|
|
992
|
+
for (const node of runOf(range))
|
|
993
|
+
target.insertBefore(node, place);
|
|
994
|
+
runParents.add(target);
|
|
995
|
+
rendersElsewhere = true;
|
|
996
|
+
readBy(target);
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Renders a capture's content immediately in front of another node, as its
|
|
1000
|
+
* siblings, with no element around it: Angular's equivalent of a React
|
|
1001
|
+
* component whose root is a fragment of what it was given, `<>{children}</>`
|
|
1002
|
+
* (a provider or a gate that renders no DOM of its own). The node is usually
|
|
1003
|
+
* the component's host, and the capture may be the host itself:
|
|
1004
|
+
* `placeContentBefore(host, host)` renders everything the host holds in front
|
|
1005
|
+
* of it and leaves the host empty.
|
|
1006
|
+
*
|
|
1007
|
+
* The content then belongs to the node's place, as an element does with
|
|
1008
|
+
* {@link placeElementBefore}: Angular inserts a new sibling before the content,
|
|
1009
|
+
* the content moves with the node, and it goes back inside the capture when
|
|
1010
|
+
* Angular removes the node. Angular's block anchors move with the content, so
|
|
1011
|
+
* an `@if` in the component's own template around `<ng-content>` and the
|
|
1012
|
+
* consumer's blocks render at the new place from the first node on. Nothing
|
|
1013
|
+
* happens while the node has no parent; call it from the component's
|
|
1014
|
+
* {@link usePlacement} function. `placeContent(capture, null)` puts the
|
|
1015
|
+
* content back.
|
|
1016
|
+
*
|
|
1017
|
+
* @param capture - The element that holds the content (nothing happens
|
|
1018
|
+
* without one).
|
|
1019
|
+
* @param sibling - The node the content stands in front of, usually the host.
|
|
1020
|
+
*/
|
|
1021
|
+
export function placeContentBefore(capture, sibling) {
|
|
1022
|
+
if (!capture || !sibling || (placeholders.get(sibling) ?? sibling).parentNode === null)
|
|
1023
|
+
return;
|
|
1024
|
+
rangeOf(capture);
|
|
1025
|
+
if (ledContent.get(capture) !== sibling) {
|
|
1026
|
+
stopLeading(capture, true);
|
|
1027
|
+
ledContent.set(capture, sibling);
|
|
1028
|
+
leaders.set(sibling, [...(leaders.get(sibling) ?? []), { node: capture, content: true }]);
|
|
1029
|
+
rendersElsewhere = true;
|
|
336
1030
|
}
|
|
337
|
-
|
|
338
|
-
for (const node of run)
|
|
339
|
-
parent.insertBefore(node, ref);
|
|
1031
|
+
leadAgain(sibling);
|
|
340
1032
|
}
|
|
341
1033
|
/**
|
|
342
1034
|
* Whether a slot renders anything, React's `{slot && …}` for projected
|
|
343
1035
|
* content: an element, or text that is not template indentation. It reads the
|
|
344
1036
|
* content wherever {@link placeContent} rendered it, so a component that moved
|
|
345
1037
|
* its slot elsewhere still sees it. Comments (Angular's anchors) are not
|
|
346
|
-
* content
|
|
1038
|
+
* content, except the anchor of a `*molLazy` slot ({@link isLazySlotAnchor}),
|
|
1039
|
+
* stamped or not: its wrapper must be in the page for it to be stamped.
|
|
347
1040
|
*
|
|
348
1041
|
* @param capture - The element Angular projected the content into.
|
|
1042
|
+
* @param ownLeadingNodes - How many of the capture's first child nodes the
|
|
1043
|
+
* port's own template renders before the slot (its own text), which are not
|
|
1044
|
+
* content.
|
|
349
1045
|
* @returns Whether the slot has content.
|
|
350
1046
|
*/
|
|
351
|
-
|
|
1047
|
+
function contentOf(capture, ownLeadingNodes = 0) {
|
|
1048
|
+
return childrenOf(capture, true, ownLeadingNodes).length > 0;
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* A slot's React children, wherever {@link placeContent} rendered them: its
|
|
1052
|
+
* elements and its text that is not template indentation, in order. A
|
|
1053
|
+
* projected component that renders nothing counts as the placeholder that
|
|
1054
|
+
* holds its place, and a `*molLazy` slot as its anchor comment, stamped or
|
|
1055
|
+
* not ({@link isLazySlotAnchor}).
|
|
1056
|
+
*
|
|
1057
|
+
* @param capture - The element Angular projected the content into.
|
|
1058
|
+
* @param firstOnly - Stop at the first child.
|
|
1059
|
+
* @param ownLeadingNodes - How many of the capture's first child nodes the
|
|
1060
|
+
* port's own template renders before the slot, which are not children.
|
|
1061
|
+
* @returns The children.
|
|
1062
|
+
*/
|
|
1063
|
+
function childrenOf(capture, firstOnly, ownLeadingNodes = 0) {
|
|
1064
|
+
const children = [];
|
|
352
1065
|
if (!capture)
|
|
353
|
-
return
|
|
1066
|
+
return children;
|
|
354
1067
|
const range = contentRanges.get(capture);
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
1068
|
+
const moved = range !== undefined && range.start.parentNode !== capture;
|
|
1069
|
+
readBy(capture);
|
|
1070
|
+
if (moved)
|
|
1071
|
+
readBy(range.start.parentNode);
|
|
1072
|
+
const end = moved ? range.end : null;
|
|
1073
|
+
let node = moved ? range.start.nextSibling : capture.firstChild;
|
|
1074
|
+
for (let skipped = 0; skipped < ownLeadingNodes && node !== null && node !== end; skipped += 1) {
|
|
1075
|
+
node = node.nextSibling;
|
|
1076
|
+
}
|
|
1077
|
+
for (; node !== null && node !== end; node = node.nextSibling) {
|
|
1078
|
+
const isContent = node.nodeType === 1 ||
|
|
1079
|
+
(node.nodeType === 3 && node.data.trim() !== '') ||
|
|
1080
|
+
// A projected component that renders nothing is still a child React was given.
|
|
1081
|
+
heldPlaces.has(node) ||
|
|
1082
|
+
// A lazy slot is one child, its anchor, whether or not its content is stamped (React's
|
|
1083
|
+
// `children` holds the element either way); its stamped nodes are that child, not others.
|
|
1084
|
+
isLazySlotAnchor(node);
|
|
1085
|
+
if (!isContent || isLazySlotContent(node))
|
|
1086
|
+
continue;
|
|
1087
|
+
children.push(node);
|
|
1088
|
+
if (firstOnly)
|
|
1089
|
+
break;
|
|
1090
|
+
}
|
|
1091
|
+
return children;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Reads a slot from a lifecycle hook, which can lag the content: a hook runs
|
|
1095
|
+
* before the consumer's `@if` adds its node, and no hook runs at all when the
|
|
1096
|
+
* content comes through an `OnPush` component in between. Read slots in the
|
|
1097
|
+
* function passed to {@link usePlacement} instead; this export goes in the
|
|
1098
|
+
* next major version.
|
|
1099
|
+
*
|
|
1100
|
+
* @param capture - The capture.
|
|
1101
|
+
* @returns Whether the slot has content.
|
|
1102
|
+
* @deprecated Use the reader {@link usePlacement} passes.
|
|
1103
|
+
*/
|
|
1104
|
+
export function hasContent(capture) {
|
|
1105
|
+
return contentOf(capture);
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* Gives {@link usePlacement} the component's own moments. A component that
|
|
1109
|
+
* calls `usePlacement` lists it in `hostDirectives`; `usePlacement` throws
|
|
1110
|
+
* without it. Its hooks fire exactly when the component's own would: after the
|
|
1111
|
+
* component's view was first rendered (its view queries are set), and each
|
|
1112
|
+
* time the parent checks the component (its inputs are set).
|
|
1113
|
+
*/
|
|
1114
|
+
export class MolPlacement {
|
|
1115
|
+
constructor() {
|
|
1116
|
+
this.runs = [];
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Adds a placement function to run at the component's moments.
|
|
1120
|
+
*
|
|
1121
|
+
* @param run - Runs it.
|
|
1122
|
+
* @internal
|
|
1123
|
+
*/
|
|
1124
|
+
add(run) {
|
|
1125
|
+
this.runs.push(run);
|
|
1126
|
+
}
|
|
1127
|
+
/** The component's view exists and its view queries are set. */
|
|
1128
|
+
ngAfterViewInit() {
|
|
1129
|
+
for (const run of this.runs)
|
|
1130
|
+
run();
|
|
1131
|
+
}
|
|
1132
|
+
/** The parent set the component's inputs. */
|
|
1133
|
+
ngDoCheck() {
|
|
1134
|
+
for (const run of this.runs)
|
|
1135
|
+
run();
|
|
1136
|
+
}
|
|
1137
|
+
static { this.ɵfac = function MolPlacement_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MolPlacement)(); }; }
|
|
1138
|
+
static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: MolPlacement }); }
|
|
1139
|
+
}
|
|
1140
|
+
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MolPlacement, [{
|
|
1141
|
+
type: Directive,
|
|
1142
|
+
args: [{ standalone: true }]
|
|
1143
|
+
}], null, null); })();
|
|
1144
|
+
/**
|
|
1145
|
+
* Registers the calling component's placement function: the one place where
|
|
1146
|
+
* it decides, from its inputs and its projected content, which of its template
|
|
1147
|
+
* elements are present ({@link setElementPresent}) and where its slots render
|
|
1148
|
+
* ({@link placeContent}, {@link placeElement}). Call it in the constructor.
|
|
1149
|
+
*
|
|
1150
|
+
* The component writes no lifecycle hook for this. The function runs:
|
|
1151
|
+
*
|
|
1152
|
+
* - right after Angular adds, removes or changes a node of a slot the function
|
|
1153
|
+
* read, in the same instruction — in whatever pass and from whatever
|
|
1154
|
+
* template the node came, so the decision never lags the content and the new
|
|
1155
|
+
* content is in the page before its own `ngAfterViewInit`;
|
|
1156
|
+
* - when a signal it read changed (an effect);
|
|
1157
|
+
* - when the component's view was first rendered and each time its parent
|
|
1158
|
+
* checks it ({@link MolPlacement}, which the component lists in
|
|
1159
|
+
* `hostDirectives`), so a plain input is followed in any kind of change
|
|
1160
|
+
* detection, a bare `detectChanges()` included;
|
|
1161
|
+
* - after every render.
|
|
1162
|
+
*
|
|
1163
|
+
* `hasContent` is the only way to ask whether a slot has content, and it works
|
|
1164
|
+
* only while the function runs: a lifecycle hook cannot read a slot, because
|
|
1165
|
+
* no hook runs at a moment when the slot is known to be current
|
|
1166
|
+
* (`ngDoCheck` runs before the consumer's `@if` adds its node; no hook at all
|
|
1167
|
+
* runs when the content comes through an `OnPush` component in between).
|
|
1168
|
+
*
|
|
1169
|
+
* @param place - The placement function. It gets `hasContent`.
|
|
1170
|
+
*/
|
|
1171
|
+
export function usePlacement(place) {
|
|
1172
|
+
assertDomPlacement('usePlacement');
|
|
1173
|
+
const moments = inject(MolPlacement, { self: true, optional: true });
|
|
1174
|
+
if (moments === null) {
|
|
1175
|
+
throw new Error('usePlacement() needs `hostDirectives: [MolPlacement]` in the decorator of the component that calls it ' +
|
|
1176
|
+
"(from @molecule/app-angular): it runs the placement function at the component's own moments.");
|
|
1177
|
+
}
|
|
1178
|
+
const inRun = () => {
|
|
1179
|
+
if (running !== placement) {
|
|
1180
|
+
throw new Error('hasContent() was called outside its usePlacement() function. A lifecycle hook cannot read a ' +
|
|
1181
|
+
'slot: decide presence and placement inside the function passed to usePlacement().');
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
const hasContent = ((capture, ownLeadingNodes) => {
|
|
1185
|
+
inRun();
|
|
1186
|
+
return contentOf(capture, ownLeadingNodes);
|
|
1187
|
+
});
|
|
1188
|
+
/** The last children read from each capture, returned again while they are the same nodes. */
|
|
1189
|
+
const lastNodes = new WeakMap();
|
|
1190
|
+
hasContent.nodes = (capture) => {
|
|
1191
|
+
inRun();
|
|
1192
|
+
const nodes = childrenOf(capture, false);
|
|
1193
|
+
if (!capture)
|
|
1194
|
+
return nodes;
|
|
1195
|
+
const last = lastNodes.get(capture);
|
|
1196
|
+
if (last !== undefined &&
|
|
1197
|
+
last.length === nodes.length &&
|
|
1198
|
+
last.every((n, i) => n === nodes[i])) {
|
|
1199
|
+
return last;
|
|
363
1200
|
}
|
|
1201
|
+
lastNodes.set(capture, nodes);
|
|
1202
|
+
return nodes;
|
|
1203
|
+
};
|
|
1204
|
+
const host = inject(ElementRef).nativeElement;
|
|
1205
|
+
// Absent when a test calls usePlacement outside a component.
|
|
1206
|
+
const view = inject(ChangeDetectorRef, { optional: true });
|
|
1207
|
+
let names;
|
|
1208
|
+
if (host.nodeType === 1 && !tagBindings.has(host)) {
|
|
1209
|
+
tagBindings.set(host, {
|
|
1210
|
+
renderer: null,
|
|
1211
|
+
listeners: new Set(),
|
|
1212
|
+
named: undefined,
|
|
1213
|
+
except: new Set(),
|
|
1214
|
+
target: null,
|
|
1215
|
+
attributes: new Map(),
|
|
1216
|
+
classes: new Set(),
|
|
1217
|
+
styles: new Map(),
|
|
1218
|
+
tag: host.tagName.toLowerCase(),
|
|
1219
|
+
inputs: () => (names ??= memberNames(view)).inputs,
|
|
1220
|
+
outputs: () => (names ??= memberNames(view)).outputs,
|
|
1221
|
+
});
|
|
364
1222
|
}
|
|
365
|
-
|
|
1223
|
+
let settled = false;
|
|
1224
|
+
const call = () => {
|
|
1225
|
+
if (placement.destroyed || running === placement)
|
|
1226
|
+
return;
|
|
1227
|
+
const outer = running;
|
|
1228
|
+
running = placement;
|
|
1229
|
+
try {
|
|
1230
|
+
place(hasContent);
|
|
1231
|
+
}
|
|
1232
|
+
finally {
|
|
1233
|
+
running = outer;
|
|
1234
|
+
}
|
|
1235
|
+
if (host.nodeType === 1)
|
|
1236
|
+
syncTagBindings(host, settled);
|
|
1237
|
+
};
|
|
1238
|
+
const placement = { run: () => untracked(call), destroyed: false };
|
|
1239
|
+
effect(call);
|
|
1240
|
+
moments.add(placement.run);
|
|
1241
|
+
afterEveryRender({
|
|
1242
|
+
write: () => {
|
|
1243
|
+
settled = true;
|
|
1244
|
+
try {
|
|
1245
|
+
placement.run();
|
|
1246
|
+
}
|
|
1247
|
+
finally {
|
|
1248
|
+
settled = false;
|
|
1249
|
+
}
|
|
1250
|
+
},
|
|
1251
|
+
});
|
|
1252
|
+
inject(DestroyRef).onDestroy(() => {
|
|
1253
|
+
placement.destroyed = true;
|
|
1254
|
+
});
|
|
366
1255
|
}
|
|
367
1256
|
/**
|
|
368
1257
|
* Keeps the calling component's host element out of the DOM while
|