MCPcopy Create free account
hub / github.com/027xiguapi/code-box / Print

Class Print

utils/print.ts:16–217  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14}
15
16export class Print {
17 private static defaultOptions: PrintOptions = {
18 globalStyles: true,
19 mediaPrint: false,
20 stylesheet: null,
21 noPrintSelector: ".no-print",
22 iframe: true,
23 append: null,
24 prepend: null,
25 manuallyCopyFormValues: true,
26 deferred: Promise.resolve(),
27 timeout: 750,
28 title: null,
29 doctype: "<!doctype html>"
30 }
31
32 static print(
33 element: HTMLElement,
34 options?: Partial<PrintOptions>
35 ): Promise<void> {
36 const mergedOptions = { ...Print.defaultOptions, ...options }
37 const $element = element
38 const content = Print.getContentToPrint($element, mergedOptions)
39
40 return mergedOptions.iframe
41 ? Print.printContentInIFrame(content, mergedOptions)
42 : Print.printContentInNewWindow(content, mergedOptions)
43 }
44
45 private static getContentToPrint(
46 element: HTMLElement,
47 options: PrintOptions
48 ): string {
49 const clone = element.cloneNode(true) as HTMLElement
50 Print.handleStyles(clone, options)
51 Print.removeUnwantedElements(clone, options.noPrintSelector)
52 Print.handleFormValues(clone, options.manuallyCopyFormValues)
53 Print.addExtraContent(clone, options)
54
55 return clone.outerHTML
56 }
57
58 private static handleStyles(clone: HTMLElement, options: PrintOptions): void {
59 if (options.globalStyles) {
60 const styles = document.querySelectorAll("style, link, meta, base, title")
61 styles.forEach((style) => clone.appendChild(style.cloneNode(true)))
62 } else if (options.mediaPrint) {
63 const printStyles = document.querySelectorAll("link[media=print]")
64 printStyles.forEach((style) => clone.appendChild(style.cloneNode(true)))
65 }
66
67 if (options.stylesheet) {
68 const stylesheets = Array.isArray(options.stylesheet)
69 ? options.stylesheet
70 : [options.stylesheet]
71 stylesheets.forEach((sheet) => {
72 const link = document.createElement("link")
73 link.rel = "stylesheet"

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected