@@ -56,7 +56,7 @@ export class PrintHook implements OnDestroy {
56
56
57
57
/** What is the desired mqAlias to use while printing? */
58
58
get printAlias ( ) : string [ ] {
59
- return this . layoutConfig . printWithBreakpoints ?? [ ] ;
59
+ return [ ... ( this . layoutConfig . printWithBreakpoints ?? [ ] ) ] ;
60
60
}
61
61
62
62
/** Lookup breakpoints associated with print aliases. */
@@ -77,11 +77,13 @@ export class PrintHook implements OnDestroy {
77
77
/** Update event with printAlias mediaQuery information */
78
78
updateEvent ( event : MediaChange ) : MediaChange {
79
79
let bp : OptionalBreakPoint = this . breakpoints . findByQuery ( event . mediaQuery ) ;
80
+
80
81
if ( this . isPrintEvent ( event ) ) {
81
82
// Reset from 'print' to first (highest priority) print breakpoint
82
83
bp = this . getEventBreakpoints ( event ) [ 0 ] ;
83
84
event . mediaQuery = bp ?. mediaQuery ?? '' ;
84
85
}
86
+
85
87
return mergeAlias ( event , bp ) ;
86
88
}
87
89
@@ -100,11 +102,13 @@ export class PrintHook implements OnDestroy {
100
102
private beforePrintEventListeners : Function [ ] = [ ] ;
101
103
private afterPrintEventListeners : Function [ ] = [ ] ;
102
104
105
+ private formerActivations : Array < BreakPoint > | null = null ;
106
+
103
107
// registerBeforeAfterPrintHooks registers a `beforeprint` event hook so we can
104
108
// trigger print styles synchronously and apply proper layout styles.
105
109
// It is a noop if the hooks have already been registered or if the document's
106
110
// `defaultView` is not available.
107
- private registerBeforeAfterPrintHooks ( target : HookTarget ) {
111
+ registerBeforeAfterPrintHooks ( target : HookTarget ) {
108
112
// `defaultView` may be null when rendering on the server or in other contexts.
109
113
if ( ! this . _document . defaultView || this . registeredBeforeAfterPrintHooks ) {
110
114
return ;
@@ -145,8 +149,6 @@ export class PrintHook implements OnDestroy {
145
149
* @return pipeable tap predicate
146
150
*/
147
151
interceptEvents ( target : HookTarget ) {
148
- this . registerBeforeAfterPrintHooks ( target ) ;
149
-
150
152
return ( event : MediaChange ) => {
151
153
if ( this . isPrintEvent ( event ) ) {
152
154
if ( event . matches && ! this . isPrinting ) {
@@ -156,9 +158,11 @@ export class PrintHook implements OnDestroy {
156
158
this . stopPrinting ( target ) ;
157
159
target . updateStyles ( ) ;
158
160
}
159
- } else {
160
- this . collectActivations ( event ) ;
161
+
162
+ return ;
161
163
}
164
+
165
+ this . collectActivations ( target , event ) ;
162
166
} ;
163
167
}
164
168
@@ -175,13 +179,15 @@ export class PrintHook implements OnDestroy {
175
179
*/
176
180
protected startPrinting ( target : HookTarget , bpList : OptionalBreakPoint [ ] ) {
177
181
this . isPrinting = true ;
182
+ this . formerActivations = target . activatedBreakpoints ;
178
183
target . activatedBreakpoints = this . queue . addPrintBreakpoints ( bpList ) ;
179
184
}
180
185
181
186
/** For any print de-activations, reset the entire print queue */
182
187
protected stopPrinting ( target : HookTarget ) {
183
188
target . activatedBreakpoints = this . deactivations ;
184
189
this . deactivations = [ ] ;
190
+ this . formerActivations = null ;
185
191
this . queue . clear ( ) ;
186
192
this . isPrinting = false ;
187
193
}
@@ -204,20 +210,29 @@ export class PrintHook implements OnDestroy {
204
210
* - sort and save when starting print
205
211
* - restore as activatedTargets and clear when stop printing
206
212
*/
207
- collectActivations ( event : MediaChange ) {
213
+ collectActivations ( target : HookTarget , event : MediaChange ) {
208
214
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
+
209
224
if ( ! event . matches ) {
210
225
const bp = this . breakpoints . findByQuery ( event . mediaQuery ) ;
211
226
// Deactivating a breakpoint
212
227
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
+ }
215
235
}
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 = [ ] ;
221
236
}
222
237
}
223
238
}
0 commit comments