88 lines
4.5 KiB
JavaScript
88 lines
4.5 KiB
JavaScript
/* 复制文本到系统剪贴板 + 据实反馈(v3.24.67)
|
||
*
|
||
* 全站有多处「复制到剪贴板」,此前各自写法不一:有的不 await,有的 catch 里只写注释,
|
||
* 但**提示语都无条件说成功**。权限被拒(非 https / 用户拒绝 / 浏览器策略)时用户拿不到东西却以为拿到了。
|
||
* 这里收口成一个返回 Promise<boolean> 的入口;需要就地反馈且拿不到 setNotice 的地方
|
||
* (如崩溃兜底屏)用 copyWithButton 改按钮文案。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
|
||
const g = (global && typeof global === 'object') ? global : {}
|
||
|
||
function copyText(text) {
|
||
return new Promise((resolve) => {
|
||
const t = text == null ? '' : String(text)
|
||
let p = null
|
||
try {
|
||
p = (typeof navigator !== 'undefined' && navigator.clipboard && navigator.clipboard.writeText) ? navigator.clipboard.writeText(t) : null
|
||
} catch (e) { p = null }
|
||
if (p && typeof p.then === 'function') {
|
||
p.then(() => resolve(true)).catch(() => resolve(false))
|
||
return
|
||
}
|
||
// 老浏览器 / 非安全上下文兜底:临时 textarea + execCommand
|
||
try {
|
||
const doc = g.document
|
||
const ta = doc.createElement('textarea')
|
||
ta.value = t
|
||
ta.style.cssText = 'position:fixed;left:-9999px;top:0;opacity:0'
|
||
doc.body.appendChild(ta)
|
||
ta.select()
|
||
const ok = doc.execCommand('copy')
|
||
doc.body.removeChild(ta)
|
||
resolve(!!ok)
|
||
} catch (e) { resolve(false) }
|
||
})
|
||
}
|
||
|
||
/* 画布上有没有「用户选中的文字」(区分节点选择与应用内文本选区)。
|
||
* 用途:Ctrl+C 原本只要选中节点就 preventDefault 去复制节点。当用户选中画布上的文字
|
||
* (提示词预览、输出文本)想复制文字时,会被改成复制节点;v3.24.68 起复制还会写系统剪贴板,
|
||
* 等于把用户真正想复制的内容覆盖掉。所以有文本选区时让浏览器原生复制走。 */
|
||
function hasTextSelection(el) {
|
||
try {
|
||
// input / textarea 的选区不进 window.getSelection(),必须看 selectionStart/End
|
||
if (el && typeof el.selectionStart === 'number') return el.selectionEnd > el.selectionStart
|
||
const s = g.getSelection ? g.getSelection() : (g.document && g.document.getSelection ? g.document.getSelection() : null)
|
||
return !!(s && !s.isCollapsed && String(s).trim())
|
||
} catch (e) { return false }
|
||
}
|
||
|
||
/* 「焦点在输入框里、且没选中任何文字时按了 ⌘C / ⌘X」—— 这种情况该由画布接管。
|
||
*
|
||
* 修的是什么(v3.24.70):onKey 原本只要焦点在 INPUT/TEXTAREA 就无条件 return,
|
||
* 于是「粘完图片 → 顺手点了一下新节点(其实点在图片链接框上)→ 按 ⌘C 复制节点」完全没反应:
|
||
* 原生复制在无选区时本来就复制不到任何东西,用户只会觉得"复制粘贴坏了"。
|
||
* 实测:焦点在 .iwb-ref-url-input 上按 ⌘C,系统剪贴板仍是空的、画布无任何提示。
|
||
*
|
||
* 边界:有选区 → 一律让原生走(用户要复制那段文字);放大编辑浮层 / 全屏遮罩里也不抢
|
||
* (那是"在浮层里编辑文本"的场景);只认 C/X 两键,不与可配置快捷键冲突 ——
|
||
* 因为这两键在输入框里的语义由浏览器固定。 */
|
||
function fieldCopyIntent(e, el) {
|
||
if (!el || !e) return false
|
||
try { if (el.closest && el.closest('.iwb-md-expand, .iwb-fullmask')) return false } catch (err) { return false }
|
||
if (hasTextSelection(el)) return false
|
||
const k = String(e.key || '').toLowerCase()
|
||
if (k !== 'c' && k !== 'x') return false
|
||
return !!(e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey
|
||
}
|
||
|
||
// 就地反馈:改按钮文案并在 2 秒后还原。崩溃兜底屏没有 setNotice 可用,只能这样。
|
||
function copyWithButton(text, btnEl, okText, failText) {
|
||
return copyText(text).then((ok) => {
|
||
try {
|
||
if (btnEl) {
|
||
const old = btnEl.getAttribute('data-iwb-label') || btnEl.textContent
|
||
btnEl.setAttribute('data-iwb-label', old)
|
||
btnEl.textContent = ok ? (okText || '已复制 ✓') : (failText || '复制失败,请改用「导出 JSON」')
|
||
setTimeout(() => { try { btnEl.textContent = old } catch (e) { /* ignore */ } }, 2000)
|
||
}
|
||
} catch (e) { /* 反馈失败不影响结果判定 */ }
|
||
return ok
|
||
})
|
||
}
|
||
|
||
g.IWBClipboardCopy = Object.freeze({ copyText, copyWithButton, hasTextSelection, fieldCopyIntent })
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|