-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Get the new clone with canvas content cloned for a given node. | ||
* | ||
* @param {Node} node The node to clone | ||
* @param {Boolean} deep equal to the deep parameter of Node.cloneNode(), default false | ||
* @return {Node} new clone with canvas content cloned | ||
*/ | ||
export default function cloneNode(node, deep = false) { | ||
const newClone = node.cloneNode(deep); | ||
let canvases; | ||
let newCanvases; | ||
if (node.tagName === 'CANVAS') { | ||
canvases = [node]; | ||
newCanvases = [newClone]; | ||
} else { | ||
canvases = node.querySelectorAll('canvas'); | ||
newCanvases = newClone.querySelectorAll('canvas'); | ||
} | ||
[...newCanvases].forEach((newCanvas, i) => { | ||
newCanvas.getContext('2d').drawImage(canvases[i], 0, 0); | ||
}); | ||
return newClone; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import cloneNode from './cloneNode'; | ||
|
||
export default cloneNode; |
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {createSandbox} from 'helper'; | ||
import cloneNode from '../cloneNode'; | ||
|
||
const sampleMarkup = ` | ||
<div class="CanvasContainer"> | ||
<div class="FirstDiv">First</div> | ||
<canvas class="FirstCanvas" width="100" height="100"></canvas> | ||
<div class="SecondDiv">Second</div> | ||
<canvas class="SecondCanvas" widht="100" height="100"></canvas> | ||
</div> | ||
`; | ||
|
||
describe('utils', () => { | ||
let sandbox; | ||
let canvasContainer; | ||
let firstCanvas; | ||
let secondCanvas; | ||
let firstDiv; | ||
let secondDiv; | ||
|
||
beforeEach(() => { | ||
sandbox = createSandbox(sampleMarkup); | ||
canvasContainer = sandbox.querySelector('.CanvasContainer'); | ||
firstCanvas = canvasContainer.querySelector('.FirstCanvas'); | ||
secondCanvas = canvasContainer.querySelector('.SecondCanvas'); | ||
firstDiv = canvasContainer.querySelector('.FirstDiv'); | ||
secondDiv = canvasContainer.querySelector('.SecondDiv'); | ||
|
||
const firstCtx = firstCanvas.getContext('2d'); | ||
const secondCtx = secondCanvas.getContext('2d'); | ||
|
||
firstCtx.fillRect(0, 0, 10, 10); | ||
secondCtx.fillRect(10, 10, 20, 20); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.parentNode.removeChild(sandbox); | ||
}); | ||
|
||
it('clone canvas content when cloned node has canvas elements', () => { | ||
const newClone = cloneNode(canvasContainer, true); | ||
const newFirstCanvas = newClone.querySelector('.FirstCanvas'); | ||
const newSecondCanvas = newClone.querySelector('.SecondCanvas'); | ||
expect(newFirstCanvas.toDataURL()).toEqual(firstCanvas.toDataURL()); | ||
expect(newSecondCanvas.toDataURL()).toEqual(secondCanvas.toDataURL()); | ||
}); | ||
|
||
it('clone nodes when cloned node has canvas elements', () => { | ||
const newClone = cloneNode(canvasContainer, true); | ||
const newFirstDiv = newClone.querySelector('.FirstDiv'); | ||
const newSecondDiv = newClone.querySelector('.SecondDiv'); | ||
expect(newFirstDiv.innerHTML).toEqual(firstDiv.innerHTML); | ||
expect(newSecondDiv.innerHTML).toEqual(secondDiv.innerHTML); | ||
}); | ||
|
||
it('clone canvas content when cloned node is canvas', () => { | ||
const newCanvas = cloneNode(firstCanvas, true); | ||
expect(newCanvas.toDataURL()).toEqual(firstCanvas.toDataURL()); | ||
}); | ||
|
||
it('not clone child nodes when deep is false', () => { | ||
const newClone = cloneNode(canvasContainer); | ||
expect(newClone.childNodes).toHaveLength(0); | ||
}); | ||
}); |
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,3 +1,4 @@ | ||
export {default as closest} from './closest'; | ||
export {default as requestNextAnimationFrame} from './requestNextAnimationFrame'; | ||
export {default as distance} from './distance'; | ||
export {default as cloneNode} from './cloneNode'; |