|
1 | 1 | <template> |
2 | | - <div class="flex h-[430px] flex-col bg-white rounded-lg overflow-hidden"> |
| 2 | + <div class="flex h-full min-h-[430px] flex-col bg-white rounded-lg overflow-hidden"> |
3 | 3 | <div |
4 | 4 | class="flex-shrink-0 flex justify-center items-center w-full h-[178px] bg-white" |
5 | 5 | > |
|
58 | 58 |
|
59 | 59 | <div |
60 | 60 | v-if="tool.description" |
61 | | - class="text-slate-500 text-[16px] leading-[22px] mb-2 overflow-hidden" |
62 | | - style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 5" |
| 61 | + ref="descriptionContainerRef" |
| 62 | + class="flex-grow min-h-0" |
| 63 | + :class="{ 'overflow-hidden': needShowMore && !showMore }" |
63 | 64 | > |
64 | | - {{ tool.description }} |
| 65 | + <div |
| 66 | + ref="descriptionRef" |
| 67 | + class="relative flex-grow text-slate-500 text-[16px] leading-[22px] mb-2 overflow-hidden" |
| 68 | + style="height: auto" |
| 69 | + > |
| 70 | + <div v-html="formatMultiline(tool.description)" /> |
| 71 | + |
| 72 | + <div |
| 73 | + v-if="needShowMore" |
| 74 | + class="flex justify-end bottom-0 right-0 bg-white pl-0.5 text-dark-blue" |
| 75 | + :class="{ absolute: !showMore, 'w-full': showMore }" |
| 76 | + > |
| 77 | + <button @click="onToggleShowMore"> |
| 78 | + {{ showMore ? 'Show less' : '... Show more' }} |
| 79 | + </button> |
| 80 | + </div> |
| 81 | + </div> |
65 | 82 | </div> |
66 | 83 |
|
67 | 84 | <div class="mt-auto flex-shrink-0 h-[56px]"> |
@@ -92,5 +109,68 @@ export default { |
92 | 109 | props: { |
93 | 110 | tool: Object, |
94 | 111 | }, |
| 112 | + data() { |
| 113 | + return { |
| 114 | + descriptionHeight: 'auto', |
| 115 | + needShowMore: true, |
| 116 | + showMore: false, |
| 117 | + }; |
| 118 | + }, |
| 119 | + methods: { |
| 120 | + escapeHtml(value = '') { |
| 121 | + return String(value) |
| 122 | + .replace(/&/g, '&') |
| 123 | + .replace(/</g, '<') |
| 124 | + .replace(/>/g, '>') |
| 125 | + .replace(/"/g, '"') |
| 126 | + .replace(/'/g, '''); |
| 127 | + }, |
| 128 | + formatMultiline(value) { |
| 129 | + if (!value) return ''; |
| 130 | + const normalized = String(value).replace(/\r\n?/g, '\n'); |
| 131 | + return this.escapeHtml(normalized).replace(/\n/g, '<br>'); |
| 132 | + }, |
| 133 | + computeDescriptionHeight() { |
| 134 | + const containerEl = this.$refs.descriptionContainerRef; |
| 135 | + const descriptionEl = this.$refs.descriptionRef; |
| 136 | + if (!containerEl || !descriptionEl) { |
| 137 | + return; |
| 138 | + } |
| 139 | +
|
| 140 | + const maxHeight = containerEl.clientHeight; |
| 141 | + const rows = Math.floor(maxHeight / 22); |
| 142 | + descriptionEl.style.height = 'auto'; |
| 143 | + this.descriptionHeight = 'auto'; |
| 144 | +
|
| 145 | + this.needShowMore = descriptionEl.offsetHeight > maxHeight; |
| 146 | + if (descriptionEl.offsetHeight > maxHeight) { |
| 147 | + descriptionEl.style.height = `${rows * 22}px`; |
| 148 | + this.descriptionHeight = `${rows * 22}px`; |
| 149 | + } else { |
| 150 | + this.showMore = false; |
| 151 | + } |
| 152 | + }, |
| 153 | + onToggleShowMore() { |
| 154 | + const descriptionEl = this.$refs.descriptionRef; |
| 155 | + if (!descriptionEl) { |
| 156 | + return; |
| 157 | + } |
| 158 | +
|
| 159 | + this.showMore = !this.showMore; |
| 160 | + if (!this.showMore) { |
| 161 | + descriptionEl.style.height = this.descriptionHeight; |
| 162 | + } else { |
| 163 | + descriptionEl.style.height = 'auto'; |
| 164 | + } |
| 165 | + }, |
| 166 | + }, |
| 167 | + mounted: function () { |
| 168 | + if (this.tool.description) { |
| 169 | + this.computeDescriptionHeight(); |
| 170 | + } else { |
| 171 | + this.needShowMore = false; |
| 172 | + this.showMore = false; |
| 173 | + } |
| 174 | + }, |
95 | 175 | }; |
96 | 176 | </script> |
0 commit comments