Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit d73a3e4

Browse files
authored
fix(core): properly collect deactivated breakpoints before print (#1380)
1 parent edca0d4 commit d73a3e4

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

projects/libs/flex-layout/core/media-marshaller/media-marshaller.ts

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ export class MediaMarshaller {
355355
private observeActivations() {
356356
const queries = this.breakpoints.items.map(bp => bp.mediaQuery);
357357

358+
this.hook.registerBeforeAfterPrintHooks(this);
358359
this.matchMedia
359360
.observe(this.hook.withPrintQuery(queries))
360361
.pipe(

projects/libs/flex-layout/core/media-marshaller/print-hook.ts

+29-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class PrintHook implements OnDestroy {
5656

5757
/** What is the desired mqAlias to use while printing? */
5858
get printAlias(): string[] {
59-
return this.layoutConfig.printWithBreakpoints ?? [];
59+
return [...(this.layoutConfig.printWithBreakpoints ?? [])];
6060
}
6161

6262
/** Lookup breakpoints associated with print aliases. */
@@ -77,11 +77,13 @@ export class PrintHook implements OnDestroy {
7777
/** Update event with printAlias mediaQuery information */
7878
updateEvent(event: MediaChange): MediaChange {
7979
let bp: OptionalBreakPoint = this.breakpoints.findByQuery(event.mediaQuery);
80+
8081
if (this.isPrintEvent(event)) {
8182
// Reset from 'print' to first (highest priority) print breakpoint
8283
bp = this.getEventBreakpoints(event)[0];
8384
event.mediaQuery = bp?.mediaQuery ?? '';
8485
}
86+
8587
return mergeAlias(event, bp);
8688
}
8789

@@ -100,11 +102,13 @@ export class PrintHook implements OnDestroy {
100102
private beforePrintEventListeners: Function[] = [];
101103
private afterPrintEventListeners: Function[] = [];
102104

105+
private formerActivations: Array<BreakPoint> | null = null;
106+
103107
// registerBeforeAfterPrintHooks registers a `beforeprint` event hook so we can
104108
// trigger print styles synchronously and apply proper layout styles.
105109
// It is a noop if the hooks have already been registered or if the document's
106110
// `defaultView` is not available.
107-
private registerBeforeAfterPrintHooks(target: HookTarget) {
111+
registerBeforeAfterPrintHooks(target: HookTarget) {
108112
// `defaultView` may be null when rendering on the server or in other contexts.
109113
if (!this._document.defaultView || this.registeredBeforeAfterPrintHooks) {
110114
return;
@@ -145,8 +149,6 @@ export class PrintHook implements OnDestroy {
145149
* @return pipeable tap predicate
146150
*/
147151
interceptEvents(target: HookTarget) {
148-
this.registerBeforeAfterPrintHooks(target);
149-
150152
return (event: MediaChange) => {
151153
if (this.isPrintEvent(event)) {
152154
if (event.matches && !this.isPrinting) {
@@ -156,9 +158,11 @@ export class PrintHook implements OnDestroy {
156158
this.stopPrinting(target);
157159
target.updateStyles();
158160
}
159-
} else {
160-
this.collectActivations(event);
161+
162+
return;
161163
}
164+
165+
this.collectActivations(target, event);
162166
};
163167
}
164168

@@ -175,13 +179,15 @@ export class PrintHook implements OnDestroy {
175179
*/
176180
protected startPrinting(target: HookTarget, bpList: OptionalBreakPoint[]) {
177181
this.isPrinting = true;
182+
this.formerActivations = target.activatedBreakpoints;
178183
target.activatedBreakpoints = this.queue.addPrintBreakpoints(bpList);
179184
}
180185

181186
/** For any print de-activations, reset the entire print queue */
182187
protected stopPrinting(target: HookTarget) {
183188
target.activatedBreakpoints = this.deactivations;
184189
this.deactivations = [];
190+
this.formerActivations = null;
185191
this.queue.clear();
186192
this.isPrinting = false;
187193
}
@@ -204,20 +210,29 @@ export class PrintHook implements OnDestroy {
204210
* - sort and save when starting print
205211
* - restore as activatedTargets and clear when stop printing
206212
*/
207-
collectActivations(event: MediaChange) {
213+
collectActivations(target: HookTarget, event: MediaChange) {
208214
if (!this.isPrinting || this.isPrintingBeforeAfterEvent) {
215+
if (!this.isPrintingBeforeAfterEvent) {
216+
// Only clear deactivations if we aren't printing from a `beforeprint` event.
217+
// Otherwise, this will clear before `stopPrinting()` is called to restore
218+
// the pre-Print Activations.
219+
this.deactivations = [];
220+
221+
return;
222+
}
223+
209224
if (!event.matches) {
210225
const bp = this.breakpoints.findByQuery(event.mediaQuery);
211226
// Deactivating a breakpoint
212227
if (bp) {
213-
this.deactivations.push(bp);
214-
this.deactivations.sort(sortDescendingPriority);
228+
const hasFormerBp = this.formerActivations && this.formerActivations.includes(bp);
229+
const wasActivated = !this.formerActivations && target.activatedBreakpoints.includes(bp);
230+
const shouldDeactivate = hasFormerBp || wasActivated;
231+
if (shouldDeactivate) {
232+
this.deactivations.push(bp);
233+
this.deactivations.sort(sortDescendingPriority);
234+
}
215235
}
216-
} else if (!this.isPrintingBeforeAfterEvent) {
217-
// Only clear deactivations if we aren't printing from a `beforeprint` event.
218-
// Otherwise, this will clear before `stopPrinting()` is called to restore
219-
// the pre-Print Activations.
220-
this.deactivations = [];
221236
}
222237
}
223238
}

0 commit comments

Comments
 (0)