|
9 | 9 | * https://v3.vuejs.org/guide/migration/render-function-api.html#render-function-argument |
10 | 10 | */ |
11 | 11 |
|
12 | | - import { defineComponent, h } from "vue" |
13 | | - import { useQuasar } from "quasar" |
| 12 | +import { defineComponent, h } from "vue" |
14 | 13 |
|
15 | | - export default defineComponent({ |
16 | | - name: "QPdfviewer", |
| 14 | +export default defineComponent({ |
| 15 | + name: "QPdfviewer", |
17 | 16 |
|
18 | | - props: { |
19 | | - src: String, |
20 | | - type: { |
21 | | - type: String, |
22 | | - default: 'html5', |
23 | | - validator: (v) => ['html5', 'pdfjs'].indexOf(v) !== -1 |
24 | | - }, |
25 | | - errorString: { |
26 | | - type: String, |
27 | | - default: |
28 | | - "This browser does not support PDFs. Download the PDF to view it:" |
29 | | - }, |
30 | | - contentStyle: [String, Object, Array], |
31 | | - contentClass: [String, Object, Array], |
32 | | - innerContentStyle: [String, Object, Array], |
33 | | - innerContentClass: [String, Object, Array] |
34 | | - }, |
| 17 | + props: { |
| 18 | + src: String, |
| 19 | + type: { |
| 20 | + type: String, |
| 21 | + default: 'html5', |
| 22 | + validator: (v) => [ 'html5', 'pdfjs' ].indexOf(v) !== -1 |
| 23 | + }, |
| 24 | + errorString: { |
| 25 | + type: String, |
| 26 | + default: |
| 27 | + "This browser does not support PDFs. Download the PDF to view it:" |
| 28 | + }, |
| 29 | + contentStyle: [ String, Object, Array ], |
| 30 | + contentClass: [ String, Object, Array ], |
| 31 | + innerContentStyle: [ String, Object, Array ], |
| 32 | + innerContentClass: [ String, Object, Array ] |
| 33 | + }, |
35 | 34 |
|
36 | | - emits: [ |
37 | | - ], |
| 35 | + emits: [ |
| 36 | + ], |
38 | 37 |
|
39 | | - data() { |
40 | | - return { |
41 | | - hashId: 'q-pdfviewer-' + Math.random().toString(36).substr(2, 9) |
42 | | - } |
43 | | - }, |
| 38 | + // Use discrete render fnction |
| 39 | + setup (props) { |
| 40 | + const hashId = 'q-pdfviewer-' + Math.random().toString(36).substr(2, 9) |
44 | 41 |
|
45 | | - // Use discrete render fnction |
46 | | - render(prop) { |
47 | | - const $q = useQuasar() |
| 42 | + function __renderObject () { |
| 43 | + return h('object', { |
| 44 | + // Change to new format style for Vue3 |
| 45 | + class: [props.innerContentClass], |
| 46 | + style: [props.innerContentStyle], |
| 47 | + id: hashId, |
| 48 | + data: props.src, |
| 49 | + type: 'application/pdf', |
| 50 | + width: '100%', |
| 51 | + height: '100%' |
| 52 | + }, [ |
| 53 | + // browser object not supported, try iframe |
| 54 | + __renderIFrame() |
| 55 | + ]) |
| 56 | + } |
48 | 57 |
|
49 | | - function __renderObject () { |
50 | | - return h('object', { |
51 | | - // Change to new format style for Vue3 |
52 | | - class: [prop.innerContentClass], |
53 | | - style: [prop.innerContentStyle], |
54 | | - id: prop.hashId, |
55 | | - data: prop.src, |
56 | | - type: 'application/pdf', |
57 | | - width: '100%', |
58 | | - height: '100%' |
59 | | - }, [ |
60 | | - // browser object not supported, try iframe |
61 | | - __renderIFrame() |
62 | | - ]) |
63 | | - } |
| 58 | + function __renderIFrame () { |
| 59 | + return h('iframe', { |
| 60 | + // Change to new format style for Vue3 |
| 61 | + class: ['q-pdfviewer__iframe'], |
| 62 | + src: props.src, |
| 63 | + width: '100%', |
| 64 | + height: '100%' |
| 65 | + }, [ |
| 66 | + // iframe not supported either, give user a link to download |
| 67 | + __renderText() |
| 68 | + ]) |
| 69 | + } |
64 | 70 |
|
65 | | - function __renderIFrame () { |
66 | | - return h('iframe', { |
67 | | - // Change to new format style for Vue3 |
68 | | - class: ['q-pdfviewer__iframe'], |
69 | | - src: prop.src, |
70 | | - width: '100%', |
71 | | - height: '100%' |
72 | | - }, [ |
73 | | - // iframe not supported either, give user a link to download |
74 | | - __renderText() |
75 | | - ]) |
76 | | - } |
| 71 | + function __renderIFramePDFJS () { |
| 72 | + return h('iframe', { |
| 73 | + // Change to new format style for Vue3 |
| 74 | + class: ['q-pdfviewer__iframe'], |
| 75 | + src: '/pdfjs/web/viewer.html?file=' + encodeURIComponent(props.src), |
| 76 | + width: '100%', |
| 77 | + height: '100%' |
| 78 | + }, [ |
| 79 | + // iframe not supported either, give user a link to download |
| 80 | + __renderText() |
| 81 | + ]) |
| 82 | + } |
77 | 83 |
|
78 | | - function __renderIFramePDFJS () { |
79 | | - return h('iframe', { |
80 | | - // Change to new format style for Vue3 |
81 | | - class: ['q-pdfviewer__iframe'], |
82 | | - src: '/pdfjs/web/viewer.html?file=' + encodeURIComponent(prop.src), |
83 | | - width: '100%', |
84 | | - height: '100%' |
85 | | - }, [ |
86 | | - // iframe not supported either, give user a link to download |
87 | | - __renderText() |
88 | | - ]) |
89 | | - } |
| 84 | + function __renderText () { |
| 85 | + // Render a link to the PDF |
| 86 | + return h('a', { |
| 87 | + // Change to new format style for Vue3 |
| 88 | + href: props.src, |
| 89 | + target: '_blank' |
| 90 | + }) |
| 91 | + } |
90 | 92 |
|
91 | | - function __renderText () { |
92 | | - // Render a link to the PDF |
93 | | - return h('a', { |
94 | | - // Change to new format style for Vue3 |
95 | | - href: prop.src, |
96 | | - target: '_blank' |
97 | | - }) |
98 | | - } |
| 93 | + function __renderPdf () { |
| 94 | + if (props.src !== void 0 && props.src.length > 0) { |
| 95 | + return h( |
| 96 | + "div", |
| 97 | + { |
| 98 | + // Change to new format style for Vue3 |
| 99 | + class: [ "q-pdfviewer", props.contentClass ], |
| 100 | + style: [props.contentStyle], |
| 101 | + }, |
| 102 | + [ |
| 103 | + props.type === "pdfjs" |
| 104 | + ? __renderIFramePDFJS(h) |
| 105 | + : __renderObject(h), |
| 106 | + ] |
| 107 | + ) |
| 108 | + } |
| 109 | + } |
99 | 110 |
|
100 | | - if (prop.src !== void 0 && prop.src.length > 0) { |
101 | | - return h( |
102 | | - "div", |
103 | | - { |
104 | | - // Change to new format style for Vue3 |
105 | | - class: ["q-pdfviewer", prop.contentClass], |
106 | | - style: [prop.contentStyle], |
107 | | - }, |
108 | | - [ |
109 | | - $q.platform.is.electron || prop.type === "pdfjs" |
110 | | - ? __renderIFramePDFJS(h) |
111 | | - : __renderObject(h), |
112 | | - ] |
113 | | - ); |
114 | | - } |
115 | | - return ''; |
116 | | - }, |
117 | | - |
118 | | - setup() { |
119 | | - return { |
120 | | - } |
121 | | - } |
122 | | - }) |
| 111 | + return () => __renderPdf() |
| 112 | + } |
| 113 | +}) |
0 commit comments