-
Notifications
You must be signed in to change notification settings - Fork 675
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from crabbly/custom_clone
Custom html deep clone
- Loading branch information
Showing
8 changed files
with
114 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,59 @@ | ||
import { collectStyles, loopNodesCollectStyles, addWrapper, addHeader } from './functions' | ||
import { addHeader } from './functions' | ||
import Print from './print' | ||
|
||
export default { | ||
print: (params, printFrame) => { | ||
// Get HTML printable element | ||
// Get the DOM printable element | ||
let printElement = document.getElementById(params.printable) | ||
|
||
// Check if element exists | ||
// Check if the element exists | ||
if (!printElement) { | ||
window.console.error('Invalid HTML element id: ' + params.printable) | ||
|
||
return false | ||
return | ||
} | ||
|
||
// Make a copy of the printElement to prevent DOM changes | ||
let printableElement = document.createElement('div') | ||
printableElement.appendChild(printElement.cloneNode(true)) | ||
|
||
// Add cloned element to DOM, to have DOM element methods available. It will also be easier to colect styles | ||
printableElement.setAttribute('style', 'height:0; overflow:hidden;') | ||
printableElement.setAttribute('id', 'printJS-html') | ||
printElement.parentNode.appendChild(printableElement) | ||
|
||
// Update printableElement variable with newly created DOM element | ||
printableElement = document.getElementById('printJS-html') | ||
|
||
// Process html styles | ||
if (params.scanStyles === true) { | ||
// Optional - include margin and padding | ||
if (params.honorMarginPadding) params.targetStyles.push('margin', 'padding') | ||
|
||
// Optional - include color | ||
if (params.honorColor) params.targetStyles.push('color') | ||
// Clone the target element including its children (if available) | ||
params.printableElement = cloneElement(printElement, params) | ||
|
||
// Get main element styling | ||
printableElement.setAttribute('style', collectStyles(printableElement, params) + 'margin:0 !important;') | ||
// Add header | ||
if (params.header) { | ||
addHeader(params.printableElement, params) | ||
} | ||
|
||
// Get all children elements | ||
let elements = printableElement.children | ||
// Print html element contents | ||
Print.send(params, printFrame) | ||
} | ||
} | ||
|
||
// Get styles for all children elements | ||
loopNodesCollectStyles(elements, params) | ||
} | ||
function cloneElement (element, params) { | ||
// Clone the main node (if not already inside the recursion process) | ||
const clone = element.cloneNode() | ||
|
||
// Add header | ||
if (params.header) { | ||
addHeader(printableElement, params.header, params.headerStyle) | ||
// Loop over and process the children elements / nodes (including text nodes) | ||
for (let child of element.childNodes) { | ||
// Check if we are skiping the current element | ||
if (params.ignoreElements.indexOf(child.id) !== -1) { | ||
continue | ||
} | ||
|
||
// Remove DOM printableElement | ||
printableElement.parentNode.removeChild(printableElement) | ||
// Clone the child element | ||
const clonedChild = cloneElement(child, params) | ||
|
||
// Store html data | ||
params.htmlData = addWrapper(printableElement.innerHTML, params) | ||
// Attach the cloned child to the cloned parent node | ||
clone.appendChild(clonedChild) | ||
} | ||
|
||
// Print html element contents | ||
Print.send(params, printFrame) | ||
// Check if the element needs any state processing (copy user input data) | ||
switch (element.tagName) { | ||
case 'SELECT': | ||
// Copy the current selection value to its clone | ||
clone.value = element.value | ||
break | ||
case 'CANVAS': | ||
// Copy the canvas content to its clone | ||
clone.getContext('2d').drawImage(element, 0, 0) | ||
break | ||
} | ||
|
||
return clone | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters