Merge pull request #40226 from cjc7373/fix-copy-to-clipboard
fix: eliminate extra lines when copying to clipboard
This commit is contained in:
		
						commit
						a72cd0cbd2
					
				|  | @ -10,36 +10,17 @@ | |||
| <script type="text/javascript"> | ||||
| function copyCode(elem){ | ||||
|   if (document.getElementById(elem)) { | ||||
|     // create hidden text element, if it doesn't already exist | ||||
|     var targetId = "_hiddenCopyText_"; | ||||
|     // must use a temporary form element for the selection and copy | ||||
|     target = document.getElementById(targetId); | ||||
|     if (!target) { | ||||
|       var target = document.createElement("textarea"); | ||||
|       target.style.position = "absolute"; | ||||
|       target.style.left = "-9999px"; | ||||
|       target.style.top = "0"; | ||||
|       target.id = targetId; | ||||
|       document.body.appendChild(target); | ||||
|     } | ||||
|     target.value = document.getElementById(elem).innerText; | ||||
|     // select the content | ||||
|     target.select(); | ||||
| 
 | ||||
|     // copy the selection | ||||
|     var succeed; | ||||
|     try { | ||||
|         succeed = document.execCommand("copy"); | ||||
|     } catch(e) { | ||||
|         swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard."); | ||||
|         succeed = false; | ||||
|     } | ||||
|     if (succeed) { | ||||
|       swal("Copied to clipboard: ",elem); | ||||
|       return succeed; | ||||
|     if (navigator.clipboard) { | ||||
|       navigator.clipboard.writeText(document.getElementById(elem).textContent).then( | ||||
|         function () { | ||||
|           swal("Copied to clipboard: ",elem); | ||||
|         }, | ||||
|         function () { | ||||
|           swal("Oh, no…","Failed to copy to clipboard: ",elem); | ||||
|         }, | ||||
|       ); | ||||
|     } else { | ||||
|       swal("Oops!", elem + " not found when trying to copy code"); | ||||
|       return false; | ||||
|       swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard."); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ | |||
|     </img> | ||||
|     </div> | ||||
|     <div class="includecode" id="{{ $file | anchorize }}"> | ||||
|     {{ highlight . $codelang "" }} | ||||
|     {{- highlight . $codelang "" -}} | ||||
|     </div> | ||||
| </div> | ||||
| {{ end }} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue