1 | import { exportaAHtml } from "./exportaAHtml.js" |
2 | import { getImgParaElementoHtml } from "./muestraObjeto.js" |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | export function muestraImagenSeleccionada(raizHtml, input) { |
9 | return new Promise((resolve, reject) => { |
10 | setTimeout(() => { |
11 | |
12 | const img = getImgParaElementoHtml(raizHtml, input) |
13 | if (img !== null) { |
14 | try { |
15 | |
16 | const dataUrl = getDataUrlDeSeleccion(input) |
17 | if (dataUrl === "") { |
18 | |
19 | const file = input.dataset.file |
20 | if (file === undefined || file === "") { |
21 | img.hidden = true |
22 | img.src = "" |
23 | } else { |
24 | img.hidden = false |
25 | img.src = file |
26 | } |
27 | |
28 | } else { |
29 | |
30 | img.hidden = false |
31 | img.src = dataUrl |
32 | |
33 | } |
34 | |
35 | resolve(true) |
36 | |
37 | } catch (error) { |
38 | |
39 | img.hidden = true |
40 | |
41 | reject(error) |
42 | |
43 | } |
44 | } |
45 | |
46 | }, |
47 | 500) |
48 | }) |
49 | } |
50 | exportaAHtml(muestraImagenSeleccionada) |
51 | |
52 | |
53 | |
54 | |
55 | export function getDataUrlDeSeleccion(input) { |
56 | const seleccion = getArchivoSeleccionado(input) |
57 | if (seleccion === null) { |
58 | return "" |
59 | } else { |
60 | return URL.createObjectURL(seleccion) |
61 | } |
62 | } |
63 | exportaAHtml(getDataUrlDeSeleccion) |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | export function getArchivoSeleccionado(input) { |
70 | const seleccion = input.files |
71 | if (seleccion === null || seleccion.length === 0) { |
72 | return null |
73 | } else { |
74 | return seleccion.item(0) |
75 | } |
76 | } |
77 | exportaAHtml(getArchivoSeleccionado) |