From d35dbdcbc30f150dc56f82794ae6dec5acad8e6d Mon Sep 17 00:00:00 2001 From: fengchenchen Date: Sat, 12 Oct 2024 11:38:42 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20pdf=E9=A2=84=E8=A7=88=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E3=80=81=E6=A0=87=E5=87=86=E8=A7=A3=E8=AF=BB=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E9=A1=B5=E9=9D=A2=20=E5=A2=9E=E5=8A=A0=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E5=AD=97=E7=AC=A6=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6?= =?UTF-8?q?1000=EF=BC=8C=E8=B6=85=E5=87=BA=E5=90=8E=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=88=AA=E5=8F=96=E5=89=8D1000=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/pdfjs/web/viewer.html | 47 +++++++++++++++-- src/components/StandardResolutionSheet.vue | 2 +- src/main.js | 2 + src/utils/copyLimit.js | 51 +++++++++++++++++++ .../detail/ForeignStandardDetailPage.vue | 2 +- 5 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/utils/copyLimit.js diff --git a/public/pdfjs/web/viewer.html b/public/pdfjs/web/viewer.html index ad4f85d..55d1537 100644 --- a/public/pdfjs/web/viewer.html +++ b/public/pdfjs/web/viewer.html @@ -623,11 +623,50 @@ See https://github.com/adobe-type-tools/cmap-resources const handleSelectionChange = (event) => { const windowSelection = window.getSelection() const selectionText = windowSelection ? window.getSelection().toString() : document.selection.createRange().text - if (selectionText.length > 500) { - alert('超出可复制长度') - windowSelection ? window.getSelection().removeAllRanges() : document.selection.empty() + // if (selectionText.length > 50) { + // alert('超出可复制长度') + // windowSelection ? window.getSelection().removeAllRanges() : document.selection.empty() + // } + } + document.addEventListener('selectionchange', handleSelectionChange); + + const handleCopy = (e) => { + const COPY_MAX_LENGTH = 1000 + const windowSelection = window.getSelection() + let selectionText = windowSelection ? window.getSelection().toString() : document.selection.createRange().text + if (selectionText.length > COPY_MAX_LENGTH) { + // 单次复制超过x个字,提示并截取前x字 + alert('超出可复制长度' + COPY_MAX_LENGTH) + selectionText = selectionText.substring(0, COPY_MAX_LENGTH) + // 清楚页面选中的效果 + const wSelections = window.getSelection() + if (wSelections) { + wSelections.removeAllRanges() + } else { + document.selection.empty() + } + const tempTextarea = document.createElement('textarea') + tempTextarea.value = selectionText + tempTextarea.style.height = '0px' + tempTextarea.style.width = '0px' + document.body.appendChild(tempTextarea) + tempTextarea.select()// Safari兼容 + tempTextarea.setSelectionRange(0, 99999999) + let timer = setTimeout(() => { + document.execCommand('copy') + document.body.removeChild(tempTextarea) + // 清除 textarea 选中的效果 + if (window.getSelection()) { + window.getSelection()?.removeAllRanges && window.getSelection()?.removeAllRanges() + } else { + document.selection.empty() + } + clearTimeout(timer) + timer = null + }, 100) } } - document.addEventListener('selectionchange', handleSelectionChange) + // 监听复制 + document.addEventListener('copy', handleCopy); diff --git a/src/components/StandardResolutionSheet.vue b/src/components/StandardResolutionSheet.vue index b19e649..e96fa4b 100644 --- a/src/components/StandardResolutionSheet.vue +++ b/src/components/StandardResolutionSheet.vue @@ -1,5 +1,5 @@