Files
Cloud-Blog/app/composables/useCopy.ts
2026-01-01 00:13:50 +08:00

19 lines
753 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 点击触发元素时,将文本复制到剪贴板,并在触发元素上显示提示信息。
* @param target - 提供复制文本、目标元素或组件实例。可为字符串、输入框(复制其 `value`)或其他 HTMLElement复制其文本内容
*/
export default function useCopy(
target: MaybeRefOrGetter<{ $el: Element } | HTMLInputElement | Element | null> | string,
) {
const getEl = (element: any) => element?.$el ?? element;
const getText = () => {
const el = getEl(toValue(target));
if (typeof target === "string") return target;
if (el instanceof HTMLInputElement) return el.value;
return (el?.textContent as string) || "";
};
return useClipboard({ source: getText, legacy: true });
}