| 1 |
import { registry } from "@web/core/registry"; |
| 2 |
import { Component, onMounted, reactive, useRef, xml } from "@odoo/owl"; |
| 3 |
import { toCanvas } from "@point_of_sale/app/utils/html-to-image"; |
| 4 |
import { waitImages } from "@point_of_sale/utils"; |
| 5 |
|
| 6 |
class ComponentRenderer extends Component { |
| 7 |
static props = ["comp", "onMounted"]; |
| 8 |
static template = xml` |
| 9 |
<div t-ref="ref"> |
| 10 |
<t t-component="props.comp.component" t-props="props.comp.props"/> |
| 11 |
</div> |
| 12 |
`; |
| 13 |
setup() { |
| 14 |
this.ref = useRef("ref"); |
| 15 |
onMounted(() => { |
| 16 |
this.props.onMounted(this.ref?.el?.firstElementChild); |
| 17 |
}); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
export class RenderContainer extends Component { |
| 22 |
static props = ["comp", "onRendered"]; |
| 23 |
static components = { ComponentRenderer }; |
| 24 |
// the `.render-container` is used by other functions that need a |
| 25 |
// place where to momentarily render some html code |
| 26 |
// we should only intact with that div through the `whenMounted` function |
| 27 |
static template = xml` |
| 28 |
<div class="render-container-parent" style="left: -1000px; position: fixed;"> |
| 29 |
<t t-if="props.comp.component"> |
| 30 |
<ComponentRenderer comp="props.comp" onMounted="props.onRendered" /> |
| 31 |
</t> |
| 32 |
<div class="render-container" /> |
| 33 |
</div>`; |
| 34 |
} |
| 35 |
/** |
| 36 |
* This service does for components what renderToElement does for templates. |
| 37 |
* In order to obtain the html code that represents a component, we need to |
| 38 |
* actually mount the respective component in the dom. |
| 39 |
*/ |
| 40 |
export const renderService = { |
| 41 |
dependencies: [], |
| 42 |
start() { |
| 43 |
const toBeRenderedComponentData = reactive({}); |
| 44 |
let elem, resolver; |
| 45 |
registry.category("main_components").add("RenderContainer", { |
| 46 |
Component: RenderContainer, |
| 47 |
props: { |
| 48 |
comp: toBeRenderedComponentData, |
| 49 |
onRendered: (el) => { |
| 50 |
elem = el; |
| 51 |
resolver?.(); |
| 52 |
// after obtaining the html code, we need to flush the |
| 53 |
// contents of the RenderContainer component |
| 54 |
toBeRenderedComponentData.component = null; |
| 55 |
}, |
| 56 |
}, |
| 57 |
}); |
| 58 |
const toHtml = async (component, props) => { |
| 59 |
Object.assign(toBeRenderedComponentData, { component, props }); |
| 60 |
// we wait for the RenderContainer component to actually |
| 61 |
// render our component |
| 62 |
await new Promise((r) => (resolver = r)); |
| 63 |
return elem; |
| 64 |
}; |
| 65 |
const toCanvas = async (component, props, options) => |
| 66 |
htmlToCanvas(await toHtml(component, props), options); |
| 67 |
const toJpeg = async (component, props, options) => { |
| 68 |
const canvas = await toCanvas(component, props, options); |
| 69 |
return canvas.toDataURL("image/jpeg").replace("data:image/jpeg;base64,", ""); |
| 70 |
}; |
| 71 |
const whenMounted = async ({ el, container, callback }) => { |
| 72 |
container ||= document.querySelector(".render-container"); |
| 73 |
container.textContent = ""; |
| 74 |
return await applyWhenMounted({ el, container, callback }); |
| 75 |
}; |
| 76 |
return { toHtml, toCanvas, toJpeg, whenMounted }; |
| 77 |
}, |
| 78 |
}; |
| 79 |
registry.category("services").add("renderer", renderService); |
| 80 |
|
| 81 |
/** |
| 82 |
* This function is meant to be used for the cases where an |
| 83 |
* action needs to be performed based on some html code, but |
| 84 |
* that html code has to be in the dom for the action to be |
| 85 |
* performed. ( for example calling html-to-image ) |
| 86 |
*/ |
| 87 |
const applyWhenMounted = async ({ el, container, callback }) => { |
| 88 |
const elClone = el.cloneNode(true); |
| 89 |
const sameClassElements = container.querySelectorAll(`.${[...el.classList].join(".")}`); |
| 90 |
// Remove all elements with the same class as the one we are about to add |
| 91 |
sameClassElements.forEach((element) => { |
| 92 |
element.remove(); |
| 93 |
}); |
| 94 |
container.appendChild(elClone); |
| 95 |
const res = await callback(elClone); |
| 96 |
return res; |
| 97 |
}; |
| 98 |
|
| 99 |
const sanitizeNodeText = (element) => { |
| 100 |
if (element.nodeType === Node.TEXT_NODE) { |
| 101 |
element.textContent = element.textContent.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, ""); // eslint-disable-line no-control-regex |
| 102 |
return; |
| 103 |
} |
| 104 |
for (const child of element.childNodes) { |
| 105 |
sanitizeNodeText(child); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
/** |
| 110 |
* This function assumes that the `renderer` service is available. |
| 111 |
*/ |
| 112 |
export const htmlToCanvas = async (el, options) => { |
| 113 |
if (options.addClass) { |
| 114 |
el.classList.add(...options.addClass.split(" ")); |
| 115 |
} |
| 116 |
sanitizeNodeText(el); |
| 117 |
return await applyWhenMounted({ |
| 118 |
el, |
| 119 |
container: document.querySelector(".render-container"), |
| 120 |
callback: async (el) => { |
| 121 |
await waitImages(el); // Ensure all images in the cloned element are fully loaded to be rendered correctly |
| 122 |
return toCanvas(el, { |
| 123 |
backgroundColor: "#ffffff", |
| 124 |
height: Math.ceil(el.clientHeight), |
| 125 |
width: Math.ceil(el.clientWidth), |
| 126 |
pixelRatio: 1, |
| 127 |
}); |
| 128 |
}, |
| 129 |
}); |
| 130 |
}; |