import{_ as V}from"./chunks/AdminExample.vue_vue_type_script_setup_true_lang.wPcM-fsm.js";import{u as P}from"./chunks/index.DNzt2uky.js";import{l as B,r as G}from"./chunks/light.BQgF5P5u.js";import{i as L}from"./chunks/isSymbol.rCNNreax.js";import{B as M,_ as O}from"./chunks/DemoFrame.vue_vue_type_script_setup_true_lang.BibqOSJQ.js";import{d as W,o as m,b as f,w as N,j as _,a5 as X,k as x,a as j,t as H,p as U,c as K,a3 as C,E as q}from"./chunks/framework.D8G7yXPy.js";var J=/\s/;function z(s){for(var i=s.length;i--&&J.test(s.charAt(i)););return i}var Y=/^\s+/;function Q(s){return s&&s.slice(0,z(s)+1).replace(Y,"")}var T=NaN,Z=/^[-+]0x[0-9a-f]+$/i,ss=/^0b[01]+$/i,is=/^0o[0-7]+$/i,as=parseInt;function w(s){if(typeof s=="number")return s;if(L(s))return T;if(B(s)){var i=typeof s.valueOf=="function"?s.valueOf():s;s=B(i)?i+"":i}if(typeof s!="string")return s===0?s:+s;s=Q(s);var a=ss.test(s);return a||is.test(s)?as(s.slice(2),a?2:8):Z.test(s)?T:+s}var u=function(){return G.Date.now()},ns="Expected a function",hs=Math.max,ts=Math.min;function ls(s,i,a){var t,p,l,e,h,k,r=0,D=!1,d=!1,F=!0;if(typeof s!="function")throw new TypeError(ns);i=w(i)||0,B(a)&&(D=!!a.leading,d="maxWait"in a,l=d?hs(w(a.maxWait)||0,i):l,F="trailing"in a?!!a.trailing:F);function c(n){var E=t,g=p;return t=p=void 0,r=n,e=s.apply(g,E),e}function I(n){return r=n,h=setTimeout(y,i),D?c(n):e}function S(n){var E=n-k,g=n-r,A=i-E;return d?ts(A,l-g):A}function v(n){var E=n-k,g=n-r;return k===void 0||E>=i||E<0||d&&g>=l}function y(){var n=u();if(v(n))return b(n);h=setTimeout(y,S(n))}function b(n){return h=void 0,F&&t?c(n):(t=p=void 0,e)}function $(){h!==void 0&&clearTimeout(h),r=0,t=k=p=h=void 0}function R(){return h===void 0?e:b(u())}function o(){var n=u(),E=v(n);if(t=arguments,p=this,k=n,E){if(h===void 0)return I(k);if(d)return clearTimeout(h),h=setTimeout(y,i),c(k)}return h===void 0&&(h=setTimeout(y,i)),e}return o.cancel=$,o.flush=R,o}const ks=()=>{const s=new WeakMap;return{beforeMount:(i,{value:a})=>{const{func:t,trigger:p="click",wait:l=500,options:e}=a;if(typeof t!="function")throw new TypeError("debounce directive value must be a function");const h=ls(t,l,e),k=P(i,p,h);s.set(i,()=>{h.cancel(),k()})},beforeUnmount(i){var a;(a=s.get(i))==null||a(),s.delete(i)}}},ps={class:"ray-demo"},es={class:"ray-demo-result"},Es=W({__name:"DebounceDemo",setup(s){const i=ks(),a=U(0),t=()=>{a.value+=1};return(p,l)=>(m(),f(O,null,{default:N(()=>[_("div",ps,[X((m(),f(x(M),null,{default:N(()=>[...l[0]||(l[0]=[j(" 连续点击 / Click repeatedly ",-1)])]),_:1})),[[x(i),{wait:500,func:t}]]),_("output",es,"执行次数 / Calls: "+H(a.value),1)])]),_:1}))}}),Cs=JSON.parse('{"title":"指令","description":"","frontmatter":{},"headers":[],"relativePath":"reference/directives.md","filePath":"reference/directives.md"}'),rs={name:"reference/directives.md"},us=Object.assign(rs,{setup(s){return(i,a)=>(m(),K("div",null,[a[0]||(a[0]=C('
应用在 main.ts 调用 setupDirectives(app)。注册器查找 packages/directives/src/modules/<name>/index.ts,调用默认导出的工厂,将返回的 Vue Directive 以目录名注册。SFC 中可直接用 v-<name>;从文件名和源码进入可查各指令的绑定类型。
在同一示例操作复制、节流、防抖、文本省略、波纹与禁用。下方防抖小例可以快速对照计数。
',4)),q(V,{path:"/directive",title:"六种指令的交互示例",height:550}),a[1]||(a[1]=C(`import type { ConditionalPick } from '@/types'
import {
NAlert,
NButton,
NCard,
NFlex,
NForm,
NFormItem,
NInput,
NInputGroup,
NInputNumber,
NSwitch,
} from 'naive-ui'
import DemoPage from '../components/DemoPage'
import DemoResult from '../components/DemoResult'
const RDirective = defineComponent({
name: 'RDirective',
setup() {
const state = reactive({
copyValueOne: '我是待复制内容区域一',
copyValueTwo: '我是待复制内容区域二',
throttleBtnClickCount: 0,
debounceBtnClickCount: 0,
disabledValue: false,
formCount: 0,
})
const ellipsisLineClamp = ref(2)
const updateDemoValue = (
key: keyof ConditionalPick<typeof state, number>,
) => {
state[key]++
}
return {
...toRefs(state),
updateDemoValue,
ellipsisLineClamp,
}
},
render() {
return (
<DemoPage
title={this.$t('menu.Directive')}
description={this.$t('showcase.directiveDescription')}
eyebrow="RAY TEMPLATE / EXAMPLES"
>
<div class="r-demo-grid">
<NCard title={this.$t('demoDetails.directive.ripple')}>
<NFlex vertical>
<NFlex>
<NButton v-ripple type="info">
{this.$t('demoDetails.directive.ripplePointer')}
</NButton>
<NButton v-ripple={[true, ['center']]}>
{this.$t('demoDetails.directive.rippleCenter')}
</NButton>
</NFlex>
</NFlex>
</NCard>
<NCard title={this.$t('demoDetails.directive.ellipsis')}>
<NFlex vertical>
<NCard title={this.$t('demoDetails.directive.single')}>
<div
v-ellipsis={{
type: 'block',
width: '100px',
}}
>
我是一段很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本。
</div>
</NCard>
<NCard title={this.$t('demoDetails.directive.multi')}>
<NFlex vertical size="large">
<NAlert title="注意" type="warning">
该方法基于非标准属性实现(-webkit-line-clamp),可能会有兼容性问题,所以请谨慎使用。
详情参考:
<a href="https://caniuse.com/?search=line-clamp">
can i use
</a>
</NAlert>
<div
v-ellipsis={{
type: 'line',
width: '100px',
line: this.ellipsisLineClamp,
}}
>
我是一段很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的文本。
</div>
<NInputNumber
v-model:value={this.ellipsisLineClamp}
min={1}
/>
</NFlex>
</NCard>
</NFlex>
</NCard>
<NCard title={this.$t('demoDetails.directive.copy1')}>
<NInputGroup>
<NInput v-model:value={this.copyValueOne} />
<NButton v-copy={this.copyValueOne}>
{this.$t('demoDetails.directive.copy')}
</NButton>
</NInputGroup>
</NCard>
<NCard title={this.$t('demoDetails.directive.copy2')}>
<NInputGroup>
<NInput v-model:value={this.copyValueTwo} />
<NButton v-copy={this.copyValueTwo}>
{this.$t('demoDetails.directive.copy')}
</NButton>
</NInputGroup>
</NCard>
<NCard title={this.$t('demoDetails.directive.throttle')}>
<NFlex vertical>
<NButton
v-throttle={{
func: this.updateDemoValue.bind(
null,
'throttleBtnClickCount',
),
trigger: 'click',
wait: 3000,
options: {},
}}
>
{this.$t('demoDetails.directive.run')}
</NButton>
<DemoResult label={this.$t('demoDetails.directive.calls')}>
{this.throttleBtnClickCount}
</DemoResult>
</NFlex>
</NCard>
<NCard title={this.$t('demoDetails.directive.debounce')}>
<NFlex vertical>
<NButton
v-debounce={{
func: this.updateDemoValue.bind(
null,
'debounceBtnClickCount',
),
trigger: 'click',
wait: 3000,
options: {},
}}
>
{this.$t('demoDetails.directive.run')}
</NButton>
<DemoResult label={this.$t('demoDetails.directive.calls')}>
{this.debounceBtnClickCount}
</DemoResult>
</NFlex>
</NCard>
<NCard title={this.$t('demoDetails.directive.disabled')}>
<NFlex vertical>
<NSwitch v-model:value={this.disabledValue}>
{{
checked: () => '取消',
unchecked: () => '禁用',
}}
</NSwitch>
<NCard title={this.$t('demoDetails.directive.nativeForm')}>
<form
v-disabled={this.disabledValue}
onSubmit={(event) => {
event.preventDefault()
this.formCount += 1
}}
>
<NInputGroup>
<NInput
inputProps={{
'aria-label': this.$t(
'demoDetails.directive.formInput',
),
}}
/>
<NButton attrType="submit">
{this.$t('demoDetails.directive.submit')}
</NButton>
</NInputGroup>
</form>
<DemoResult label={this.$t('demoDetails.directive.calls')}>
{this.formCount}
</DemoResult>
</NCard>
<NCard title={this.$t('demoDetails.directive.text')}>
<NFlex vertical>
<p v-disabled={this.disabledValue}>
我是可以被禁用的文本内容
</p>
</NFlex>
</NCard>
<NCard title={this.$t('demoDetails.directive.naive')}>
<NFlex vertical justify="start">
<NForm v-disabled={this.disabledValue}>
<NFormItem>
<NInput />
</NFormItem>
</NForm>
<NButton v-disabled={this.disabledValue}>按钮</NButton>
<NSwitch v-disabled={this.disabledValue}></NSwitch>
</NFlex>
</NCard>
</NFlex>
</NCard>
</div>
</DemoPage>
)
},
})
export default RDirective绑定要复制的字符串;绑定值改变时销毁并重建 ClipboardJS 实例,卸载时销毁。Ray Admin 注册时接入消息提示。独立使用 createCopyDirective 时可提供自己的结果回调。复制应由用户点击触发。数字等值请先显式转为字符串。
<template>
<button type="button" v-copy="'ORD-001'">复制订单号</button>
</template>连续点击按钮,停止 500ms 后,防抖只执行一次。
`,7)),q(Es),a[2]||(a[2]=C(`<template>
<DemoFrame>
<div class="ray-demo">
<NButton v-debounce="{ wait: 500, func: increment }">
连续点击 / Click repeatedly
</NButton>
<output class="ray-demo-result">执行次数 / Calls: {{ calls }}</output>
</div>
</DemoFrame>
</template>
<script setup lang="ts">
import debounce from '@ray-template/directives/debounce'
import { NButton } from 'naive-ui'
import { ref } from 'vue'
import DemoFrame from './DemoFrame.vue'
const vDebounce = debounce()
const calls = ref(0)
const increment = () => {
calls.value += 1
}
</script>两者都接收 { func, trigger?, wait?, options? }。func 必须是函数并可接收原生事件;trigger 默认 'click',wait 默认 500 毫秒,options 转给 Lodash 的对应方法。防抖适合在连续输入停止后处理,节流适合限制持续触发的频率。
<script setup lang="ts">
import { ref } from 'vue'
const keyword = ref('')
const requestedKeyword = ref('')
const clicks = ref(0)
const applySearch = () => {
requestedKeyword.value = keyword.value
}
const countClick = () => {
clicks.value += 1
}
</script>
<template>
<input
v-model="keyword"
v-debounce="{ func: applySearch, trigger: 'input', wait: 300 }"
/>
<p>已提交的查询词:{{ requestedKeyword }}</p>
<button type="button" v-throttle="{ func: countClick, wait: 1000 }">
点击次数 {{ clicks }}
</button>
</template>options.leading / trailing 决定在等待周期首尾是否执行;防抖还可设 maxWait。挂载时读取绑定值,更新不会重绑。若回调依赖变动数据,让原函数在执行时读取响应式值。width 必填,数字按像素;字符串可传 CSS 尺寸。type: 'block' 做单行省略,type: 'line' 使用 line 行截断,默认按单行处理。popoverText: true 把当前文本写入原生 title,不创建 Popover。挂载和更新时写入样式;切换模式或关掉悬浮文本不会自动删除上一次写入的样式或 title。经常切换的场景可以改用组件库的省略组件,或切换节点 key。
<template>
<p v-ellipsis="{ type: 'line', line: 2, width: 240, popoverText: true }">
这里放较长的说明文字,超出两行后会被省略。
</p>
</template>省略绑定值或传 true 启用,false 关闭。绑定对象 { class: '...' } 可指定波纹样式。.center 从中心扩散,.circle 使用圆形计算。通过 pointer 事件渲染,属于视觉反馈,交互语义仍由外层按钮负责。
<template>
<button type="button" v-ripple.center.circle>查看详情</button>
</template>接收布尔值并修改原生 disabled 属性和模板禁用样式。当前实现关闭禁用时仍会写入空的 disabled 属性,HTML 原生按钮将继续视为禁用。因此原生控件使用 :disabled="busy",Naive UI 使用对应 disabled prop。对自定义控件,要同时处理指针、键盘与可访问状态;样式不能取代业务权限检查。
每个指令放在单独目录,工厂默认导出。可用 CustomDirectiveFC<HTMLElement, T> 标注绑定值。在 mounted 取得元素,在 updated 处理值更新,在 beforeUnmount 释放事件、计时器和第三方实例。多个元素需要独立数据时,把状态放在元素关联的 WeakMap,避免用单个变量保存全部实例的资源。
import type { CustomDirectiveFC } from '@/directives/types'
const focusWhen: CustomDirectiveFC<HTMLElement, boolean> = () => ({
mounted(el, { value }) {
if (value) el.focus()
},
updated(el, { value, oldValue }) {
if (value && !oldValue) el.focus()
},
})
export default focusWhen将示例保存为 packages/directives/src/modules/focus-when/index.ts,在组件中用 v-focus-when。目录名按现有约定用单段小写 kebab-case。注册器要求默认导出函数,缺失会抛错。