@94ai/nf-text-overflow 3.1.37 → 3.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import NfTextOverflow from './nf-text-overflow.vue'
2
+ import { Vue2, PluginObject } from 'vue-demi'
3
+
4
+ NfTextOverflow.install = (app: typeof Vue2) => {
5
+ app!.component('nf-text-overflow', NfTextOverflow)
6
+ }
7
+
8
+ export default NfTextOverflow as typeof NfTextOverflow & PluginObject<undefined>
@@ -0,0 +1,151 @@
1
+ <template>
2
+ <div class="nf-text-overflow">
3
+ <div class="nf-text-overflow__content" ref="textbox" :style="{ height: contentHeight, lineHeight: `${rowHeight}px` }">
4
+ <slot>{{ expanded ? content : text }}</slot>
5
+ </div>
6
+ <div class="nf-text-overflow__action" v-if="hasAction" @click="onClickAction">
7
+ {{ expanded ? collapseText : expandText }}
8
+ <i class="nf-text-overflow__icon" :class="[expanded ? 'el-icon-arrow-up' : 'el-icon-arrow-down']"></i>
9
+ </div>
10
+ </div>
11
+ </template>
12
+
13
+ <script lang="ts">
14
+ import { defineComponent } from 'vue-demi'
15
+
16
+ export default defineComponent({
17
+ name: "nf-text-overflow",
18
+ })
19
+ </script>
20
+
21
+ <script lang="ts" setup>
22
+ import { ref, onMounted, onUnmounted, watch, useSlots, computed } from 'vue-demi'
23
+
24
+ const props = withDefaults(defineProps<{
25
+ content?: string
26
+ expandText?: string
27
+ collapseText?: string
28
+ rows?: string | number
29
+ rowHeight?: string | number
30
+ }>(), {
31
+ content: '',
32
+ expandText: '展开',
33
+ collapseText: '收起',
34
+ rows: 1,
35
+ rowHeight: 20
36
+ })
37
+
38
+ const emit = defineEmits<{
39
+ (e: 'clickAction', event: Event): void
40
+ }>()
41
+
42
+
43
+ const text = ref('');
44
+ const expanded = ref(false);
45
+ const hasAction = ref(false);
46
+ const textbox = ref<HTMLElement>();
47
+ const contentHeight = ref()
48
+
49
+ const hasDefaultSlot = computed(() => !!useSlots().default)
50
+ const maxContentHeight = computed(() => Number(props.rows) * Number(props.rowHeight))
51
+
52
+ const pxToNum = (value: string | null) => {
53
+ if (!value) return 0;
54
+ const match = value.match(/^\d*(\.\d*)?/);
55
+ return match ? Number(match[0]) : 0;
56
+ };
57
+
58
+ const calcEllipsised = () => {
59
+ const cloneContainer = () => {
60
+ if (!textbox.value) return;
61
+
62
+ const originStyle = window.getComputedStyle(textbox.value);
63
+ const container = document.createElement('div');
64
+ const styleNames: string[] = Array.prototype.slice.apply(originStyle);
65
+ styleNames.forEach((name) => {
66
+ container.style.setProperty(name, originStyle.getPropertyValue(name));
67
+ });
68
+
69
+ container.style.position = 'fixed';
70
+ container.style.zIndex = '-9999';
71
+ container.style.top = '-9999px';
72
+ container.style.height = 'auto';
73
+ container.style.minHeight = 'auto';
74
+ container.style.maxHeight = 'auto';
75
+
76
+ container.innerText = props.content;
77
+ document.body.appendChild(container);
78
+ return container;
79
+ };
80
+
81
+ const calcEllipsisText = (
82
+ container: HTMLDivElement,
83
+ maxHeight: number
84
+ ) => {
85
+ const { content } = props;
86
+ let left = 0;
87
+ let right = content.length;
88
+ let res = -1;
89
+
90
+ while (left <= right) {
91
+ const mid = Math.floor((left + right) / 2);
92
+ container.innerText = content.slice(0, mid);
93
+ if (container.offsetHeight <= maxHeight) {
94
+ left = mid + 1;
95
+ res = mid;
96
+ } else {
97
+ right = mid - 1;
98
+ }
99
+ }
100
+ return content.slice(0, res);
101
+ };
102
+
103
+ const container = cloneContainer();
104
+ if (!container) return;
105
+
106
+ const { paddingBottom, paddingTop, lineHeight } = container.style;
107
+ const maxHeight =
108
+ (Number(props.rows) + 0.5) * pxToNum(lineHeight) +
109
+ pxToNum(paddingTop) +
110
+ pxToNum(paddingBottom);
111
+ if (maxHeight < container.offsetHeight) {
112
+ hasAction.value = true;
113
+ text.value = calcEllipsisText(container, maxHeight);
114
+ } else {
115
+ hasAction.value = false;
116
+ text.value = props.content;
117
+ }
118
+
119
+ document.body.removeChild(container);
120
+ };
121
+
122
+ const calcContentHeight = () => {
123
+ if (!textbox.value) return;
124
+ const height = textbox.value.offsetHeight
125
+ if (height > maxContentHeight.value) {
126
+ hasAction.value = true;
127
+ contentHeight.value = `${maxContentHeight.value}px`
128
+ }
129
+ }
130
+
131
+ const init = hasDefaultSlot.value ? calcContentHeight : calcEllipsised
132
+
133
+ const onClickAction = (event: MouseEvent) => {
134
+ expanded.value = !expanded.value;
135
+ if (hasDefaultSlot.value) {
136
+ contentHeight.value = expanded.value ? 'auto' : `${maxContentHeight.value}px`
137
+ }
138
+ emit('clickAction', event);
139
+ };
140
+
141
+ onMounted(() => {
142
+ init()
143
+ window.addEventListener('resize', init)
144
+ });
145
+
146
+ onUnmounted(() => {
147
+ window.removeEventListener('resize', init)
148
+ })
149
+
150
+ watch(() => [props.content, props.rows], init);
151
+ </script>
@@ -0,0 +1 @@
1
+ import '@94ai/nf-theme-chalk/lib/css/nf-text-overflow.css'
@@ -0,0 +1 @@
1
+ import '@94ai/nf-theme-chalk/lib/nf-text-overflow.scss'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@94ai/nf-text-overflow",
3
- "version": "3.1.37",
3
+ "version": "3.1.38",
4
4
  "description": "> TODO: description",
5
5
  "keywords": [],
6
6
  "author": "zhongzhenjie <1476686104@qq.com>",
@@ -14,7 +14,7 @@
14
14
  "url": "http://94ai.gitlab.com/zoujiahe/common-ui.git"
15
15
  },
16
16
  "dependencies": {
17
- "@94ai/nf-theme-chalk": "^3.1.37",
17
+ "@94ai/nf-theme-chalk": "^1.0.0",
18
18
  "vue-demi": "^0.14.5"
19
19
  },
20
20
  "peerDependenciesMeta": {
@@ -30,5 +30,5 @@
30
30
  "types": "lib/index.d.ts",
31
31
  "main": "lib/nf-text-overflow.cjs.js",
32
32
  "module": "lib/nf-text-overflow.esm-bundler.js",
33
- "gitHead": "14dfc729b9988003d4686b414e1e9c0aaa7c4dcd"
33
+ "gitHead": "2b60696b405feafbbaeed385e5bcf666ceefbf2c"
34
34
  }