This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add print support with mediaQuery override
Implement PrintHook service to intercept print mediaQuery activation events. * suspend activation changes in MediaMarshaller while print-mode is active * trigger MediaObserver to notify subscribers with print mqAlias
- Loading branch information
1 parent
350e753
commit 735f6e1
Showing
9 changed files
with
180 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {Inject, Injectable} from '@angular/core'; | ||
|
||
import {MediaChange} from '../media-change'; | ||
import {BreakPoint} from '../breakpoints/break-point'; | ||
import {LAYOUT_CONFIG, LayoutConfigOptions} from '../tokens/library-config'; | ||
import {BreakPointRegistry, OptionalBreakPoint} from '../breakpoints/break-point-registry'; | ||
|
||
/** | ||
* Interface to apply PrintHook to call anonymous `target.updateStyles()` | ||
*/ | ||
export interface HookTarget { | ||
activatedBreakpoints: BreakPoint[]; | ||
|
||
updateStyles(): void; | ||
} | ||
|
||
/** | ||
* PrintHook - Use to intercept print MediaQuery activations and force | ||
* layouts to render with the specified print alias/breakpoint | ||
* | ||
* Used in MediaMarshaller and MediaObserver | ||
*/ | ||
@Injectable({providedIn: 'root'}) | ||
export class PrintHook { | ||
protected offlineActivations: BreakPoint[] | null = null; | ||
|
||
constructor( | ||
protected breakpoints: BreakPointRegistry, | ||
@Inject(LAYOUT_CONFIG) protected layoutConfig: LayoutConfigOptions) { | ||
} | ||
|
||
/** | ||
* Add 'print' mediaQuery: to listen for matchMedia activations | ||
*/ | ||
withPrintListener(queries: string[]): string[] { | ||
if (!!this.printAlias) { | ||
queries.push('print'); | ||
} | ||
return queries; | ||
} | ||
|
||
/** | ||
* Is this service currently in Print-mode ? | ||
*/ | ||
get isPrinting(): boolean { | ||
return !!this.offlineActivations; | ||
} | ||
|
||
/** | ||
* What is the desired mqAlias to use while printing? | ||
*/ | ||
get printAlias(): string { | ||
return this.layoutConfig.printWithBreakpoint || ''; | ||
} | ||
|
||
/** | ||
* Lookup breakpoint associated with print alias. | ||
*/ | ||
get printBreakPoint(): OptionalBreakPoint { | ||
return this.breakpoints.findByAlias(this.printAlias!); | ||
} | ||
|
||
/** | ||
* Prepare RxJs filter operator with partial application | ||
* @return pipeable filter predicate | ||
*/ | ||
interceptEvents(target: HookTarget) { | ||
return (change: MediaChange): boolean => { | ||
if (change.mediaQuery == 'print') { | ||
|
||
if (change.matches && !this.isPrinting) { | ||
this.enablePrintMode(target, this.printBreakPoint); | ||
target.updateStyles(); | ||
} else if (!change.matches && this.isPrinting) { | ||
this.disablePrintMode(target); | ||
target.updateStyles(); | ||
} | ||
} | ||
|
||
return !this.isPrinting; | ||
}; | ||
} | ||
|
||
/** | ||
* Save current activateBreakpoints (for later restore) | ||
* and substitute only the printAlias breakpoint | ||
*/ | ||
protected enablePrintMode(target: HookTarget, bp: OptionalBreakPoint) { | ||
if (!!bp) { | ||
this.offlineActivations = target.activatedBreakpoints; | ||
target.activatedBreakpoints = [bp]; | ||
} | ||
} | ||
|
||
/** | ||
* Restore cached activatedBreakpoints and clear isPrinting | ||
* state | ||
*/ | ||
protected disablePrintMode(target: HookTarget) { | ||
target.activatedBreakpoints = this.offlineActivations!; | ||
this.offlineActivations = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters