ngx-print 20.1.0-beta.3 → 20.1.0-beta.4
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/.editorconfig +13 -0
- package/.eslintrc.json +50 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +31 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- package/.github/dependabot.yml +17 -0
- package/.github/workflows/PullRequest.yml +32 -0
- package/.github/workflows/stale.yml +26 -0
- package/.prettierignore +42 -0
- package/.prettierrc.json +14 -0
- package/CHANGELOG.md +17 -0
- package/_config.yml +1 -0
- package/angular.json +64 -0
- package/karma.conf.js +43 -0
- package/ng-package.json +7 -0
- package/package.json +68 -43
- package/src/lib/ngx-print.base.ts +320 -0
- package/src/lib/ngx-print.directive.spec.ts +139 -0
- package/src/lib/ngx-print.directive.ts +113 -0
- package/src/lib/ngx-print.module.ts +8 -0
- package/src/lib/ngx-print.service.spec.ts +201 -0
- package/src/lib/ngx-print.service.ts +51 -0
- package/src/lib/print-helper.ts +24 -0
- package/src/lib/print-options.ts +16 -0
- package/src/public_api.ts +18 -0
- package/tsconfig.json +35 -0
- package/tsconfig.lib.json +14 -0
- package/tsconfig.spec.json +17 -0
- package/tslint.json +17 -0
- package/fesm2022/ngx-print.mjs +0 -465
- package/fesm2022/ngx-print.mjs.map +0 -1
- package/index.d.ts +0 -241
- package/index.d.ts.map +0 -1
- package/src/assets/print-helper.js +0 -42
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { NgxPrintService } from './ngx-print.service';
|
|
4
|
+
import { Component, CSP_NONCE, inject, provideZonelessChangeDetection } from '@angular/core';
|
|
5
|
+
import { PrintOptions } from './print-options';
|
|
6
|
+
|
|
7
|
+
const testNonce = 'dummy-nonce-value';
|
|
8
|
+
|
|
9
|
+
@Component({
|
|
10
|
+
template: `
|
|
11
|
+
<div id="print-section">
|
|
12
|
+
<h1>Welcome to ngx-print</h1>
|
|
13
|
+
<img
|
|
14
|
+
width="300"
|
|
15
|
+
alt="Angular Logo"
|
|
16
|
+
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==" />
|
|
17
|
+
<h2>Here are some links to help you start:</h2>
|
|
18
|
+
<ul>
|
|
19
|
+
<li>
|
|
20
|
+
<h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
|
|
21
|
+
</li>
|
|
22
|
+
<li>
|
|
23
|
+
<h2><a target="_blank" rel="noopener" href="https://github.com/angular/angular-cli/wiki">CLI Documentation</a></h2>
|
|
24
|
+
</li>
|
|
25
|
+
<li>
|
|
26
|
+
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
|
|
27
|
+
</li>
|
|
28
|
+
</ul>
|
|
29
|
+
<table border="1">
|
|
30
|
+
<tr>
|
|
31
|
+
<td>Row 1, Column 1</td>
|
|
32
|
+
<td>Row 1, Column 2</td>
|
|
33
|
+
</tr>
|
|
34
|
+
<tr>
|
|
35
|
+
<td>Row 2, Column 1</td>
|
|
36
|
+
<td>Row 2, Column 2</td>
|
|
37
|
+
</tr>
|
|
38
|
+
</table>
|
|
39
|
+
</div>
|
|
40
|
+
`,
|
|
41
|
+
})
|
|
42
|
+
class TestNgxPrintServiceComponent {
|
|
43
|
+
private printService = inject(NgxPrintService);
|
|
44
|
+
|
|
45
|
+
printMe(printOptions: PrintOptions) {
|
|
46
|
+
this.printService.print(printOptions);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('NgxPrintService', () => {
|
|
51
|
+
let service: NgxPrintService;
|
|
52
|
+
let component: TestNgxPrintServiceComponent;
|
|
53
|
+
let fixture: ComponentFixture<TestNgxPrintServiceComponent>;
|
|
54
|
+
|
|
55
|
+
const styleSheet: { [key: string]: { [key: string]: string } } = {
|
|
56
|
+
'h2': { 'border': 'solid 1px' },
|
|
57
|
+
'h1': { 'color': 'red', 'border': '1px solid' },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
TestBed.configureTestingModule({
|
|
62
|
+
imports: [TestNgxPrintServiceComponent],
|
|
63
|
+
providers: [{ provide: CSP_NONCE, useValue: testNonce }, NgxPrintService, provideZonelessChangeDetection()],
|
|
64
|
+
});
|
|
65
|
+
service = TestBed.inject(NgxPrintService);
|
|
66
|
+
// Create a fixture object (that is going to allows us to create an instance of that component)
|
|
67
|
+
fixture = TestBed.createComponent(TestNgxPrintServiceComponent);
|
|
68
|
+
|
|
69
|
+
// Create a component instance ( ~ new Component)
|
|
70
|
+
component = fixture.componentInstance;
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should be created', () => {
|
|
74
|
+
expect(service).toBeTruthy();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should print', () => {
|
|
78
|
+
spyOn(service, 'print');
|
|
79
|
+
|
|
80
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
81
|
+
printSectionId: 'print-section',
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
component.printMe(customPrintOptions);
|
|
85
|
+
|
|
86
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should print with title', () => {
|
|
90
|
+
spyOn(service, 'print');
|
|
91
|
+
|
|
92
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
93
|
+
printSectionId: 'print-section',
|
|
94
|
+
printTitle: 'Test Title',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
component.printMe(customPrintOptions);
|
|
98
|
+
|
|
99
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should print with existing css', () => {
|
|
103
|
+
spyOn(service, 'print');
|
|
104
|
+
|
|
105
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
106
|
+
printSectionId: 'print-section',
|
|
107
|
+
useExistingCss: true,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
component.printMe(customPrintOptions);
|
|
111
|
+
|
|
112
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should print with delay', () => {
|
|
116
|
+
spyOn(service, 'print');
|
|
117
|
+
|
|
118
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
119
|
+
printSectionId: 'print-section',
|
|
120
|
+
printDelay: 2000,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
component.printMe(customPrintOptions);
|
|
124
|
+
|
|
125
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should print with previewOnly', () => {
|
|
129
|
+
spyOn(service, 'print');
|
|
130
|
+
|
|
131
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
132
|
+
printSectionId: 'print-section',
|
|
133
|
+
previewOnly: true,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
component.printMe(customPrintOptions);
|
|
137
|
+
|
|
138
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should not close', () => {
|
|
142
|
+
spyOn(service, 'print');
|
|
143
|
+
|
|
144
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
145
|
+
printSectionId: 'print-section',
|
|
146
|
+
closeWindow: false,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
component.printMe(customPrintOptions);
|
|
150
|
+
|
|
151
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should open new tab', () => {
|
|
155
|
+
spyOn(service, 'print');
|
|
156
|
+
|
|
157
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
158
|
+
printSectionId: 'print-section',
|
|
159
|
+
openNewTab: true,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
component.printMe(customPrintOptions);
|
|
163
|
+
|
|
164
|
+
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should test the printStyle', () => {
|
|
168
|
+
// Create a spy on the instance's method
|
|
169
|
+
spyOn(service, 'returnStyleValues').and.callThrough();
|
|
170
|
+
|
|
171
|
+
// Call the function before checking if it has been called
|
|
172
|
+
service.returnStyleValues();
|
|
173
|
+
|
|
174
|
+
// Check if returnStyleValues has been called
|
|
175
|
+
expect(service.returnStyleValues).toHaveBeenCalled();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should return a string from array of objects', () => {
|
|
179
|
+
service.printStyle = styleSheet;
|
|
180
|
+
|
|
181
|
+
// Ensure the print styles are correctly formatted in the document
|
|
182
|
+
expect(service.returnStyleValues()).toEqual('<style nonce="' + testNonce + '"> h2{border:solid 1px} h1{color:red;border:1px solid} </style>');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should emit on print completion (void)', done => {
|
|
186
|
+
// Subscribe to the observable
|
|
187
|
+
service.printComplete$.subscribe(() => {
|
|
188
|
+
expect(true).toBeTrue(); // Just ensure it emits
|
|
189
|
+
done();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const customPrintOptions: PrintOptions = new PrintOptions({
|
|
193
|
+
printSectionId: 'print-section',
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
component.printMe(customPrintOptions);
|
|
197
|
+
|
|
198
|
+
// Simulate the popup sending the print complete message
|
|
199
|
+
window.dispatchEvent(new MessageEvent('message', { data: { type: 'print-complete' } }));
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { PrintBase } from './ngx-print.base';
|
|
3
|
+
import { PrintOptions } from './print-options';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Service for handling printing functionality in Angular applications.
|
|
7
|
+
* Extends the base printing class (PrintBase).
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @class NgxPrintService
|
|
11
|
+
* @extends {PrintBase}
|
|
12
|
+
*/
|
|
13
|
+
@Injectable({
|
|
14
|
+
providedIn: 'root',
|
|
15
|
+
})
|
|
16
|
+
export class NgxPrintService extends PrintBase {
|
|
17
|
+
printComplete$ = this.printComplete.asObservable();
|
|
18
|
+
/**
|
|
19
|
+
* Initiates the printing process using the provided print options.
|
|
20
|
+
*
|
|
21
|
+
* @param {PrintOptions} printOptions - Options for configuring the printing process.
|
|
22
|
+
* @memberof NgxPrintService
|
|
23
|
+
* @returns {void}
|
|
24
|
+
*/
|
|
25
|
+
public print(printOptions: PrintOptions): void {
|
|
26
|
+
// Call the print method in the parent class
|
|
27
|
+
super.print(printOptions);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sets the print style for the printing process.
|
|
32
|
+
*
|
|
33
|
+
* @param {{ [key: string]: { [key: string]: string } }} values - A dictionary representing the print styles.
|
|
34
|
+
* @memberof NgxPrintService
|
|
35
|
+
* @setter
|
|
36
|
+
*/
|
|
37
|
+
set printStyle(values: { [key: string]: { [key: string]: string } }) {
|
|
38
|
+
super.setPrintStyle(values);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Sets the stylesheet file for the printing process.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} cssList - A string representing the path to the stylesheet file.
|
|
45
|
+
* @memberof NgxPrintService
|
|
46
|
+
* @setter
|
|
47
|
+
*/
|
|
48
|
+
set styleSheetFile(cssList: string) {
|
|
49
|
+
super.setStyleSheetFile(cssList);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PrintOptions } from './print-options';
|
|
2
|
+
|
|
3
|
+
// src/lib/print-helper.ts
|
|
4
|
+
export function initPrintWindow(windowRef: Window, printOptions: PrintOptions) {
|
|
5
|
+
function triggerPrint() {
|
|
6
|
+
windowRef.removeEventListener('load', triggerPrint, false);
|
|
7
|
+
if (!printOptions.previewOnly) {
|
|
8
|
+
setTimeout(() => {
|
|
9
|
+
windowRef.print();
|
|
10
|
+
if (printOptions.closeWindow) windowRef.close();
|
|
11
|
+
}, printOptions.printDelay || 0);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function afterPrint() {
|
|
16
|
+
if (windowRef.opener) {
|
|
17
|
+
windowRef.opener.postMessage({ type: 'print-complete' }, '*');
|
|
18
|
+
}
|
|
19
|
+
if (printOptions.closeWindow) windowRef.close();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
windowRef.addEventListener('load', triggerPrint, false);
|
|
23
|
+
windowRef.addEventListener('afterprint', afterPrint, { once: true });
|
|
24
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class PrintOptions {
|
|
2
|
+
printSectionId: string = '';
|
|
3
|
+
printTitle: string = '';
|
|
4
|
+
useExistingCss: boolean = false;
|
|
5
|
+
bodyClass: string = '';
|
|
6
|
+
openNewTab: boolean = false;
|
|
7
|
+
previewOnly: boolean = false;
|
|
8
|
+
closeWindow: boolean = true;
|
|
9
|
+
printDelay: number = 0;
|
|
10
|
+
|
|
11
|
+
constructor(options?: Partial<PrintOptions>) {
|
|
12
|
+
if (options) {
|
|
13
|
+
Object.assign(this, options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Public API Surface of ngx-print
|
|
3
|
+
*/
|
|
4
|
+
export { NgxPrintDirective } from './lib/ngx-print.directive';
|
|
5
|
+
export { NgxPrintModule } from './lib/ngx-print.module';
|
|
6
|
+
export { NgxPrintService } from './lib/ngx-print.service';
|
|
7
|
+
export { PrintOptions } from './lib/print-options';
|
|
8
|
+
import { initPrintWindow } from './lib/print-helper';
|
|
9
|
+
|
|
10
|
+
// Expose globally for popup windows
|
|
11
|
+
(window as any).initPrintWindow = initPrintWindow;
|
|
12
|
+
|
|
13
|
+
// Optional: listen for postMessage from child window
|
|
14
|
+
window.addEventListener('message', (event: MessageEvent) => {
|
|
15
|
+
if (event.data?.type === 'init-print') {
|
|
16
|
+
initPrintWindow(window, event.data.options);
|
|
17
|
+
}
|
|
18
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compileOnSave": false,
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "./",
|
|
5
|
+
"outDir": "./dist/out-tsc",
|
|
6
|
+
"sourceMap": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"declaration": false,
|
|
9
|
+
"module": "ES2022",
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"emitDecoratorMetadata": true,
|
|
12
|
+
"experimentalDecorators": true,
|
|
13
|
+
"target": "ES2022",
|
|
14
|
+
"strict": true,
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
"typeRoots": [
|
|
17
|
+
"node_modules/@types"
|
|
18
|
+
],
|
|
19
|
+
"lib": [
|
|
20
|
+
"ES2022",
|
|
21
|
+
"dom"
|
|
22
|
+
],
|
|
23
|
+
"paths": {
|
|
24
|
+
"ngx-print": [
|
|
25
|
+
"dist/ngx-print"
|
|
26
|
+
],
|
|
27
|
+
"ngx-print/*": [
|
|
28
|
+
"dist/ngx-print/*"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"angularCompilerOptions": {
|
|
33
|
+
"compilationMode": "partial"
|
|
34
|
+
}
|
|
35
|
+
}
|