Browse Source

标题省略

CzRger 2 months ago
parent
commit
b09e8a1121

+ 23 - 56
src/directives/title.ts

@@ -4,14 +4,15 @@ interface AutoTitleOptions {
   always?: boolean;       // 是否总是显示title
   content?: string;      // 自定义title内容
   lines?: number;        // 最大行数(支持多行省略)
+  addClass?: boolean;    // 是否自动添加__text类名,默认为true
 }
 
 const autoTitle: Directive<HTMLElement, boolean | AutoTitleOptions | string> = {
   mounted(el, binding) {
-    observeElement(el, binding);
+    processElement(el, binding, true);
   },
   updated(el, binding) {
-    observeElement(el, binding);
+    processElement(el, binding, false);
   },
   unmounted(el) {
     const observer = (el as any)._autoTitleObserver;
@@ -22,14 +23,31 @@ const autoTitle: Directive<HTMLElement, boolean | AutoTitleOptions | string> = {
   }
 };
 
-function observeElement(el: HTMLElement, binding: DirectiveBinding) {
+function processElement(el: HTMLElement, binding: DirectiveBinding, isMounted: boolean) {
+  const options = parseBinding(binding);
+
+  // 自动添加__text类名(默认添加,可通过addClass: false禁用)
+  if (options.addClass !== false) {
+    el.classList.add(`__text-ellipsis${Number(options.lines) > 1 ? `-${options.lines}` : ''}`);
+  } else {
+    el.classList.remove(`__text-ellipsis${Number(options.lines) > 1 ? `-${options.lines}` : ''}`);
+  }
+
+  // 初始化或更新观察者
+  if (isMounted) {
+    observeElement(el, options);
+  } else {
+    updateTitle(el, options);
+  }
+}
+
+function observeElement(el: HTMLElement, options: AutoTitleOptions) {
   // 先移除旧的观察者
   const oldObserver = (el as any)._autoTitleObserver;
   if (oldObserver) {
     oldObserver.disconnect();
   }
 
-  const options = parseBinding(binding);
   const observer = new ResizeObserver(() => {
     updateTitle(el, options);
   });
@@ -96,55 +114,4 @@ export default autoTitle;
 //   display: -webkit-box;
 //   -webkit-box-orient: vertical;
 //   overflow: hidden;
-// }
-
-
-// import type { Directive, DirectiveBinding } from 'vue';
-//
-// interface TitleOptions {
-//   // 是否总是显示title,即使没有文本溢出
-//   always?: boolean;
-//   // 自定义title内容,如果不提供则使用元素文本
-//   content?: string;
-// }
-//
-// const title: Directive<HTMLElement, boolean | TitleOptions | string> = {
-//   mounted(el, binding) {
-//     updateTitle(el, binding);
-//   },
-//   updated(el, binding) {
-//     updateTitle(el, binding);
-//   }
-// };
-//
-// function updateTitle(el: HTMLElement, binding: DirectiveBinding) {
-//   const options = parseBinding(binding);
-//
-//   if (options.always || el.scrollWidth > el.clientWidth) {
-//     el.setAttribute('title', options.content || el.textContent?.trim() || '');
-//   } else {
-//     el.removeAttribute('title');
-//   }
-// }
-//
-// function parseBinding(binding: DirectiveBinding): TitleOptions {
-//   // 如果绑定值是布尔值
-//   if (typeof binding.value === 'boolean') {
-//     return { always: binding.value };
-//   }
-//
-//   // 如果绑定值是字符串
-//   if (typeof binding.value === 'string') {
-//     return { content: binding.value };
-//   }
-//
-//   // 如果绑定值是对象
-//   if (typeof binding.value === 'object') {
-//     return binding.value;
-//   }
-//
-//   // 默认情况
-//   return { always: false };
-// }
-//
-// export default title;
+// }

+ 2 - 2
src/views/manage/knowledge/index.vue

@@ -67,14 +67,14 @@
               <div class="flex">
                 <img src="@/assets/images/knowledge-item-icon.png" class="mr-[var(--czr-gap)]"/>
                 <div class="flex flex-1 flex-col justify-around overflow-hidden">
-                  <div class="__text-ellipsis text-[1.25rem] text-[#2E3238] font-bold" v-title>{{row.p1}}</div>
+                  <div class="text-[1.25rem] text-[#2E3238] font-bold" v-title>{{row.p1}}</div>
                   <div class="text-[0.75rem] text-[#6F7889]">创建者:{{row.p2}}</div>
                 </div>
                 <div class="ml-auto mt-[0.25rem]">
                   <tagsSelect :value="row.tags" @onChange="tags => onChangeTag(row, tags)"/>
                 </div>
               </div>
-              <div class="__text-ellipsis-3 text-[0.88rem] text-[#606266] mb-auto mt-[var(--czr-gap)]" style="line-height: 1.4rem" v-title="{lines: 3}">{{row.p3}}</div>
+              <div class="text-[0.88rem] text-[#606266] mb-auto mt-[var(--czr-gap)]" style="line-height: 1.4rem" v-title="{lines: 3}">{{row.p3}}</div>
               <div class="flex items-center text-[0.75rem] text-[#6F7889] gap-[var(--czr-gap)]">
                 <div>文档数:{{row.p4}}</div>
                 <div>|</div>

+ 3 - 3
src/views/manage/knowledge/tags-select.vue

@@ -15,7 +15,7 @@
           'bg-[#c8ceda33]': state.show
         }">
           <SvgIcon name="tag" size="14" class="mr-1"/>
-          <span class="__text-ellipsis max-w-[5rem]" v-title>{{value ? value.split(',').map(v => DictionaryStore.knowledgeTags.map.get(v) || v).join(',') : '添加标签'}}</span>
+          <span class="max-w-[5rem]" v-title>{{value ? value.split(',').map(v => DictionaryStore.knowledgeTags.map.get(v) || v).join(',') : '添加标签'}}</span>
         </div>
       </template>
       <div class="w-[15rem] max-h-[20rem] bg-[#c8ceda24]" :select-popover="true">
@@ -42,7 +42,7 @@
                 <template v-for="item in DictionaryStore.knowledgeTags.list">
                   <div class="__hover-bg p-[0.5rem] flex items-center text-[0.88rem] text-[#354052] rounded" @click="state.valueMap.has(item.id) ? state.valueMap.delete(item.id) : state.valueMap.set(item.id, item.id)">
                     <div class="__check scale-[75%] mr-1" :class="{active: state.valueMap.has(item.id)}"/>
-                    <span class="__text-ellipsis flex-1" v-title>{{item.name}}</span>
+                    <span class="flex-1" v-title>{{item.name}}</span>
                   </div>
                 </template>
               </div>
@@ -91,7 +91,7 @@
         </template>
         <template v-else>
           <div class="max-w-[17rem] border border-[var(--czr-border-color)] px-[0.5rem] h-[2.25rem] rounded flex items-center gap-2">
-            <span class="__text-ellipsis" v-title>
+            <span v-title>
               {{item.name}}
             </span>
             <span class="opacity-75">{{item.total}}</span>