mirror of
				https://github.com/pure-admin/vue-pure-admin.git
				synced 2025-11-03 13:44:47 +08:00 
			
		
		
		
	feat: 添加文本复制自定义指令
This commit is contained in:
		
							parent
							
								
									d850496601
								
							
						
					
					
						commit
						3fd9b15698
					
				
							
								
								
									
										33
									
								
								src/directives/copy/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/directives/copy/index.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
import { message } from "@/utils/message";
 | 
			
		||||
import { useEventListener } from "@vueuse/core";
 | 
			
		||||
import { copyTextToClipboard } from "@pureadmin/utils";
 | 
			
		||||
import { Directive, type DirectiveBinding } from "vue";
 | 
			
		||||
 | 
			
		||||
interface CopyEl extends HTMLElement {
 | 
			
		||||
  copyValue: string;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** 文本复制指令(默认双击复制) */
 | 
			
		||||
export const copy: Directive = {
 | 
			
		||||
  mounted(el: CopyEl, binding: DirectiveBinding) {
 | 
			
		||||
    const { value } = binding;
 | 
			
		||||
    if (value) {
 | 
			
		||||
      el.copyValue = value;
 | 
			
		||||
      const arg = binding.arg ?? "dblclick";
 | 
			
		||||
      // Register using addEventListener on mounted, and removeEventListener automatically on unmounted
 | 
			
		||||
      useEventListener(el, arg, () => {
 | 
			
		||||
        const success = copyTextToClipboard(el.copyValue);
 | 
			
		||||
        success
 | 
			
		||||
          ? message("复制成功", { type: "success" })
 | 
			
		||||
          : message("复制失败", { type: "error" });
 | 
			
		||||
      });
 | 
			
		||||
    } else {
 | 
			
		||||
      throw new Error(
 | 
			
		||||
        '[Directive: copy]: need value! Like v-copy="modelValue"'
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  updated(el: CopyEl, binding: DirectiveBinding) {
 | 
			
		||||
    el.copyValue = binding.value;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
@ -1,2 +1,3 @@
 | 
			
		||||
export * from "./auth";
 | 
			
		||||
export * from "./copy";
 | 
			
		||||
export * from "./optimize";
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user