fix copy
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import copyText from "copy-text-to-clipboard"
|
||||
import { copyToClipboard } from "~/utils/functions"
|
||||
import { code, input, output } from "oj/composables/code"
|
||||
import { problem } from "oj/composables/problem"
|
||||
import { LANGUAGE_SHOW_VALUE, SOURCES, STORAGE_KEY } from "utils/constants"
|
||||
@@ -57,9 +57,13 @@ const languageOptions: DropdownOption[] = problem.value!.languages.map((it) => (
|
||||
value: it,
|
||||
}))
|
||||
|
||||
const copy = () => {
|
||||
copyText(code.value)
|
||||
const copy = async () => {
|
||||
const success = await copyToClipboard(code.value)
|
||||
if (success) {
|
||||
message.success("代码复制成功")
|
||||
} else {
|
||||
message.error("代码复制失败")
|
||||
}
|
||||
}
|
||||
|
||||
const reset = () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { Icon } from "@iconify/vue"
|
||||
import copy from "copy-text-to-clipboard"
|
||||
import { copyToClipboard } from "~/utils/functions"
|
||||
|
||||
defineProps<{ value: string }>()
|
||||
const [copied, toggle] = useToggle()
|
||||
@@ -9,10 +9,12 @@ const { start } = useTimeoutFn(() => toggle(false), 1000, { immediate: false })
|
||||
const COPY = h(Icon, { icon: "twemoji:clipboard" })
|
||||
const OK = h(Icon, { icon: "twemoji:check-mark-button" })
|
||||
|
||||
function handleClick(value: string) {
|
||||
copy(value)
|
||||
async function handleClick(value: string) {
|
||||
const success = await copyToClipboard(value)
|
||||
if (success) {
|
||||
toggle(true)
|
||||
start()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
|
||||
@@ -9,16 +9,20 @@
|
||||
</n-tooltip>
|
||||
</template>
|
||||
<script lang="ts" setup>
|
||||
import copyText from "copy-text-to-clipboard"
|
||||
import { copyToClipboard } from "~/utils/functions"
|
||||
|
||||
const message = useMessage()
|
||||
|
||||
const slots = useSlots()
|
||||
|
||||
function handleClick() {
|
||||
async function handleClick() {
|
||||
const textToCopy = getTextFromSlot()
|
||||
copyText(textToCopy)
|
||||
const success = await copyToClipboard(textToCopy)
|
||||
if (success) {
|
||||
message.success("已复制")
|
||||
} else {
|
||||
message.error("复制失败")
|
||||
}
|
||||
}
|
||||
|
||||
function getTextFromSlot() {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { getTime, intervalToDuration, parseISO, type Duration } from "date-fns"
|
||||
import { User } from "./types"
|
||||
import { USER_TYPE } from "./constants"
|
||||
import { strFromU8, strToU8, unzlibSync, zlibSync } from "fflate"
|
||||
import copyTextFallback from "copy-text-to-clipboard"
|
||||
|
||||
function calculateACRate(acCount: number, totalCount: number): string {
|
||||
if (totalCount === 0) return "0.00"
|
||||
@@ -199,6 +200,33 @@ export function atou(base64: string): string {
|
||||
return strFromU8(unzipped)
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文本到剪贴板
|
||||
* 优先使用 Clipboard API(支持在 modal 中使用),失败时回退到 copy-text-to-clipboard
|
||||
* @param text 要复制的文本
|
||||
* @returns Promise<boolean> 复制是否成功
|
||||
*/
|
||||
export async function copyToClipboard(text: string): Promise<boolean> {
|
||||
// 优先使用现代 Clipboard API
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(text)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.warn("Clipboard API 复制失败,尝试使用回退方法:", error)
|
||||
}
|
||||
}
|
||||
|
||||
// 回退到 copy-text-to-clipboard
|
||||
try {
|
||||
const success = copyTextFallback(text)
|
||||
return success
|
||||
} catch (error) {
|
||||
console.error("复制失败:", error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// function getChromeVersion() {
|
||||
// var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)
|
||||
// return raw ? parseInt(raw[2], 10) : 0
|
||||
|
||||
Reference in New Issue
Block a user