1
1
import * as React from 'react' ;
2
- import { useRef } from 'react' ;
2
+ import { useRef , useState } from 'react' ;
3
3
import { useIntl } from '@edx/frontend-platform/i18n' ;
4
4
import { Button } from '@openedx/paragon' ;
5
5
import messages from '../messages' ;
6
6
import { type BlockResponse } from '../types' ;
7
7
8
- export const PrintSyllabus = ( { blockData } : { blockData : BlockResponse } ) => {
9
- const intl = useIntl ( ) ;
10
- const iframeRef = useRef < HTMLIFrameElement | null > ( null ) ;
11
- const blocks = blockData ?. blocks ;
12
- const rootBlock = blockData ?. blocks [ blockData . root ] ;
8
+ const buildSyllabusForPrint = ( intl , blocks , rootBlock ) => {
13
9
const makeList = ( items : string [ ] | null ) => {
14
10
if ( ! items ) {
15
11
return '' ;
@@ -27,56 +23,71 @@ export const PrintSyllabus = ({ blockData }: { blockData: BlockResponse }) => {
27
23
) ) ) } `) ) } `) ) } `) ) ;
28
24
29
25
const srcdoc = `<html>
30
- <head>
31
- <title>${ intl . formatMessage ( messages . syllabusTitle ) } </title>
32
- <style>
33
- body > ul {
34
- padding: 0.125rem;
35
- margin: 0;
36
- list-style: none;
37
- }
26
+ <head>
27
+ <title>${ intl . formatMessage ( messages . syllabusTitle ) } </title>
28
+ <style>
29
+ body > ul {
30
+ padding: 0.125rem;
31
+ margin: 0;
32
+ list-style: none;
33
+ }
34
+
35
+ body > ul > li {
36
+ border: 1px solid black;
37
+ margin: 0.5rem 0;
38
+ padding: 0 0.5rem;
39
+ }
40
+
41
+ body > ul > li > ul {
42
+ break-after: auto;
43
+ list-style: none;
44
+ }
45
+
46
+ body ul ul ul ul li:before {
47
+ content: '\\01F517';
48
+ margin-right: 0.5rem;
49
+ }
38
50
39
- body > ul > li {
40
- border: 1px solid black;
41
- margin: 0.5rem 0;
42
- padding: 0 0.5rem;
43
- }
51
+ body ul ul ul ul {
52
+ list-style: none;
53
+ padding-left: 0;
54
+ }
44
55
45
- body > ul > li > ul {
46
- break-after: auto;
47
- list-style: none;
48
- }
56
+ body ul > li ul {
57
+ break-after: auto;
58
+ }
49
59
50
- body ul ul ul ul li:before {
51
- content: '\\01F517';
52
- margin-right: 0.5rem;
53
- }
54
-
55
- body ul ul ul ul {
56
- list-style: none;
57
- padding-left: 0;
58
- }
60
+ li {
61
+ break-inside: avoid;
62
+ }
59
63
60
- body ul > li ul {
61
- break-after: auto;
62
- }
64
+ ul {
65
+ margin: 0;
66
+ padding-left: 1rem;
67
+ break-after: auto;
68
+ }
69
+ </style>
70
+ </head>
71
+ <body>
72
+ ${ syllabusList }
73
+ </body>
74
+ </html>
75
+ ` ;
76
+ return srcdoc ;
77
+ } ;
78
+
79
+ export const PrintSyllabus = ( { blockData } : { blockData : BlockResponse } ) => {
80
+ const intl = useIntl ( ) ;
81
+ const iframeRef = useRef < HTMLIFrameElement | null > ( null ) ;
82
+ const [ srcdoc , setSrcdoc ] = useState ( '' ) ;
63
83
64
- li {
65
- break-inside: avoid;
66
- }
84
+ const printSyllabus = ( ) => {
85
+ const blocks = blockData ?. blocks ;
86
+ const rootBlock = blockData ?. blocks [ blockData . root ] ;
87
+ setSrcdoc ( buildSyllabusForPrint ( intl , blocks , rootBlock ) ) ;
88
+ iframeRef . current . contentWindow . print ( ) ;
89
+ } ;
67
90
68
- ul {
69
- margin: 0;
70
- padding-left: 1rem;
71
- break-after: auto;
72
- }
73
- </style>
74
- </head>
75
- <body>
76
- ${ syllabusList }
77
- </body>
78
- </html>
79
- ` ;
80
91
return (
81
92
< div className = "d-flex justify-content-end my-2" >
82
93
< iframe
@@ -88,7 +99,7 @@ ${syllabusList}
88
99
{ iframeRef . current ?. contentWindow && (
89
100
< Button
90
101
variant = "outline-primary"
91
- onClick = { ( ) => iframeRef . current . contentWindow . print ( ) }
102
+ onClick = { ( ) => printSyllabus ( ) }
92
103
>
93
104
{ intl . formatMessage ( messages . print ) }
94
105
</ Button >
0 commit comments