-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(drag-drop): not transferring input value when cloning element for…
… preview (#20009) We use `cloneNode` to create a preview for an element which clones all of the attributes of an element, but that may not pick up properties like its `value`. These changes add some logic where we clone the value with a similar approach that we use for cloning `canvas` elements. Also generalizes the approach a bit so we don't have to keep repeating the logic. Fixes #19905. (cherry picked from commit d53d5b6)
- Loading branch information
1 parent
d383bba
commit 9833eea
Showing
3 changed files
with
121 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** Creates a deep clone of an element. */ | ||
export function deepCloneNode(node: HTMLElement): HTMLElement { | ||
const clone = node.cloneNode(true) as HTMLElement; | ||
const descendantsWithId = clone.querySelectorAll('[id]'); | ||
const nodeName = node.nodeName.toLowerCase(); | ||
|
||
// Remove the `id` to avoid having multiple elements with the same id on the page. | ||
clone.removeAttribute('id'); | ||
|
||
for (let i = 0; i < descendantsWithId.length; i++) { | ||
descendantsWithId[i].removeAttribute('id'); | ||
} | ||
|
||
if (nodeName === 'canvas') { | ||
transferCanvasData(node as HTMLCanvasElement, clone as HTMLCanvasElement); | ||
} else if (nodeName === 'input' || nodeName === 'select' || nodeName === 'textarea') { | ||
transferInputData(node as HTMLInputElement, clone as HTMLInputElement); | ||
} | ||
|
||
transferData('canvas', node, clone, transferCanvasData); | ||
transferData('input, textarea, select', node, clone, transferInputData); | ||
return clone; | ||
} | ||
|
||
/** Matches elements between an element and its clone and allows for their data to be cloned. */ | ||
function transferData<T extends Element>(selector: string, node: HTMLElement, clone: HTMLElement, | ||
callback: (source: T, clone: T) => void) { | ||
const descendantElements = node.querySelectorAll<T>(selector); | ||
|
||
if (descendantElements.length) { | ||
const cloneElements = clone.querySelectorAll<T>(selector); | ||
|
||
for (let i = 0; i < descendantElements.length; i++) { | ||
callback(descendantElements[i], cloneElements[i]); | ||
} | ||
} | ||
} | ||
|
||
/** Transfers the data of one input element to another. */ | ||
function transferInputData(source: Element & {value: string}, clone: Element & {value: string}) { | ||
clone.value = source.value; | ||
} | ||
|
||
/** Transfers the data of one canvas element to another. */ | ||
function transferCanvasData(source: HTMLCanvasElement, clone: HTMLCanvasElement) { | ||
const context = clone.getContext('2d'); | ||
|
||
if (context) { | ||
// In some cases `drawImage` can throw (e.g. if the canvas size is 0x0). | ||
// We can't do much about it so just ignore the error. | ||
try { | ||
context.drawImage(source, 0, 0); | ||
} catch {} | ||
} | ||
} |
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