From d348b36159f5d7171bc3ac0903890b9191304da9 Mon Sep 17 00:00:00 2001 From: neverland Date: Tue, 8 Feb 2022 11:41:37 +0800 Subject: [PATCH] chore(cli): extract copyToClipboard to common (#10263) * chore(cli): extract copyToClipboard to common * style: update copied --- packages/vant-cli/site/common/index.js | 27 +++++++++++ .../site/desktop/components/Content.vue | 45 ++++++------------- .../desktop/components/DemoPlayground.vue | 31 ++----------- .../vant/docs/markdown/changelog.en-US.md | 10 ++--- .../vant/docs/markdown/changelog.zh-CN.md | 10 ++--- 5 files changed, 53 insertions(+), 70 deletions(-) diff --git a/packages/vant-cli/site/common/index.js b/packages/vant-cli/site/common/index.js index 0da499ee8..dcfb367e9 100644 --- a/packages/vant-cli/site/common/index.js +++ b/packages/vant-cli/site/common/index.js @@ -8,4 +8,31 @@ export function decamelize(str, sep = '-') { .toLowerCase(); } +// from https://30secondsofcode.org +export function copyToClipboard(str) { + const el = document.createElement('textarea'); + el.value = str; + el.setAttribute('readonly', ''); + el.style.position = 'absolute'; + el.style.left = '-9999px'; + document.body.appendChild(el); + + const selection = document.getSelection(); + + if (!selection) { + return; + } + + const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false; + + el.select(); + document.execCommand('copy'); + document.body.removeChild(el); + + if (selected) { + selection.removeAllRanges(); + selection.addRange(selected); + } +} + export { isMobile }; diff --git a/packages/vant-cli/site/desktop/components/Content.vue b/packages/vant-cli/site/desktop/components/Content.vue index af2b014c2..279e69b14 100644 --- a/packages/vant-cli/site/desktop/components/Content.vue +++ b/packages/vant-cli/site/desktop/components/Content.vue @@ -8,32 +8,7 @@