fix: clipboard copy fallback via execCommand (iframe blocks navigator.clipboard)

This commit is contained in:
Hermes Agent committed 2026-09-09 15:43:11 +02:00
1 parent 831a5b3615
commit 13f3129c78
1 file changed
+41 -4
+41 -4
View File
@@ -89,6 +89,8 @@ function Plugin () {
}
// Копирует все debug-значения (RAW + расчёт) в буфер обмена как текст.
// Важно: в iframe плагина Figma navigator.clipboard часто недоступен (или его
// промис тихо reject'ится), поэтому обязателен fallback через document.execCommand('copy').
function copyInfo () {
if (windowInfo == null) return
const i = windowInfo
@@ -110,11 +112,46 @@ function Plugin () {
`center.y = ${i.center.y} (y + h / 2)`
]
const text = lines.join('\n')
function markCopied () {
setCopied(true)
setTimeout(function () { setCopied(false) }, 1200)
}
// Путь 1: современный async Clipboard API (если доступен и сработал)
if (navigator.clipboard != null && navigator.clipboard.writeText != null) {
navigator.clipboard.writeText(text).then(function () {
setCopied(true)
setTimeout(function () { setCopied(false) }, 1200)
}).catch(function () { /* тихо */ })
const apiResult = navigator.clipboard.writeText(text)
if (apiResult != null && typeof apiResult.then === 'function') {
apiResult.then(function () { markCopied() }).catch(function () {
// тихо — переходим к fallback
fallbackCopy(text, markCopied)
})
return
}
}
// Путь 2: синхронный fallback для iframe/небезопасных контекстов
fallbackCopy(text, markCopied)
}
// Fallback-копирование через временный <textarea> + document.execCommand('copy').
// Работает там, где navigator.clipboard недоступен (iframe Figma, file://, не HTTPS).
function fallbackCopy (text: string, onDone: () => void) {
try {
const ta = document.createElement('textarea')
ta.value = text
ta.style.position = 'fixed'
ta.style.top = '-9999px'
ta.style.opacity = '0'
document.body.appendChild(ta)
ta.focus()
ta.select()
const ok = document.execCommand('copy')
document.body.removeChild(ta)
if (ok) {
onDone()
}
} catch (e) {
// оба пути не сработали — ничего не делаем (индикации нет)
}
}