TextEllipsis.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { ref, watch, computed, onActivated, onMounted, defineComponent, nextTick, createVNode as _createVNode } from "vue";
  2. import { makeNumericProp, makeStringProp, createNamespace, windowWidth } from "../utils/index.mjs";
  3. import { useExpose } from "../composables/use-expose.mjs";
  4. const [name, bem] = createNamespace("text-ellipsis");
  5. const textEllipsisProps = {
  6. rows: makeNumericProp(1),
  7. dots: makeStringProp("..."),
  8. content: makeStringProp(""),
  9. expandText: makeStringProp(""),
  10. collapseText: makeStringProp(""),
  11. position: makeStringProp("end")
  12. };
  13. var stdin_default = defineComponent({
  14. name,
  15. props: textEllipsisProps,
  16. emits: ["clickAction"],
  17. setup(props, {
  18. emit,
  19. slots
  20. }) {
  21. const text = ref(props.content);
  22. const expanded = ref(false);
  23. const hasAction = ref(false);
  24. const root = ref();
  25. const actionRef = ref();
  26. let needRecalculate = false;
  27. const actionText = computed(() => expanded.value ? props.collapseText : props.expandText);
  28. const pxToNum = (value) => {
  29. if (!value) return 0;
  30. const match = value.match(/^\d*(\.\d*)?/);
  31. return match ? Number(match[0]) : 0;
  32. };
  33. const cloneContainer = () => {
  34. if (!root.value || !root.value.isConnected) return;
  35. const originStyle = window.getComputedStyle(root.value);
  36. const container = document.createElement("div");
  37. const styleNames = Array.prototype.slice.apply(originStyle);
  38. styleNames.forEach((name2) => {
  39. container.style.setProperty(name2, originStyle.getPropertyValue(name2));
  40. });
  41. container.style.position = "fixed";
  42. container.style.zIndex = "-9999";
  43. container.style.top = "-9999px";
  44. container.style.height = "auto";
  45. container.style.minHeight = "auto";
  46. container.style.maxHeight = "auto";
  47. container.innerText = props.content;
  48. document.body.appendChild(container);
  49. return container;
  50. };
  51. const calcEllipsisText = (container, maxHeight) => {
  52. var _a, _b;
  53. const {
  54. content,
  55. position,
  56. dots
  57. } = props;
  58. const end = content.length;
  59. const middle = 0 + end >> 1;
  60. const actionHTML = slots.action ? (_b = (_a = actionRef.value) == null ? void 0 : _a.outerHTML) != null ? _b : "" : props.expandText;
  61. const calcEllipse = () => {
  62. const tail = (left, right) => {
  63. if (right - left <= 1) {
  64. if (position === "end") {
  65. return content.slice(0, left) + dots;
  66. }
  67. return dots + content.slice(right, end);
  68. }
  69. const middle2 = Math.round((left + right) / 2);
  70. if (position === "end") {
  71. container.innerText = content.slice(0, middle2) + dots;
  72. } else {
  73. container.innerText = dots + content.slice(middle2, end);
  74. }
  75. container.innerHTML += actionHTML;
  76. if (container.offsetHeight > maxHeight) {
  77. if (position === "end") {
  78. return tail(left, middle2);
  79. }
  80. return tail(middle2, right);
  81. }
  82. if (position === "end") {
  83. return tail(middle2, right);
  84. }
  85. return tail(left, middle2);
  86. };
  87. return tail(0, end);
  88. };
  89. const middleTail = (leftPart, rightPart) => {
  90. if (leftPart[1] - leftPart[0] <= 1 && rightPart[1] - rightPart[0] <= 1) {
  91. return content.slice(0, leftPart[0]) + dots + content.slice(rightPart[1], end);
  92. }
  93. const leftMiddle = Math.floor((leftPart[0] + leftPart[1]) / 2);
  94. const rightMiddle = Math.ceil((rightPart[0] + rightPart[1]) / 2);
  95. container.innerText = props.content.slice(0, leftMiddle) + props.dots + props.content.slice(rightMiddle, end);
  96. container.innerHTML += actionHTML;
  97. if (container.offsetHeight >= maxHeight) {
  98. return middleTail([leftPart[0], leftMiddle], [rightMiddle, rightPart[1]]);
  99. }
  100. return middleTail([leftMiddle, leftPart[1]], [rightPart[0], rightMiddle]);
  101. };
  102. return props.position === "middle" ? middleTail([0, middle], [middle, end]) : calcEllipse();
  103. };
  104. const calcEllipsised = () => {
  105. const container = cloneContainer();
  106. if (!container) {
  107. needRecalculate = true;
  108. return;
  109. }
  110. const {
  111. paddingBottom,
  112. paddingTop,
  113. lineHeight
  114. } = container.style;
  115. const maxHeight = Math.ceil((Number(props.rows) + 0.5) * pxToNum(lineHeight) + pxToNum(paddingTop) + pxToNum(paddingBottom));
  116. if (maxHeight < container.offsetHeight) {
  117. hasAction.value = true;
  118. text.value = calcEllipsisText(container, maxHeight);
  119. } else {
  120. hasAction.value = false;
  121. text.value = props.content;
  122. }
  123. document.body.removeChild(container);
  124. };
  125. const toggle = (isExpanded = !expanded.value) => {
  126. expanded.value = isExpanded;
  127. };
  128. const onClickAction = (event) => {
  129. toggle();
  130. emit("clickAction", event);
  131. };
  132. const renderAction = () => {
  133. const action = slots.action ? slots.action({
  134. expanded: expanded.value
  135. }) : actionText.value;
  136. return _createVNode("span", {
  137. "ref": actionRef,
  138. "class": bem("action"),
  139. "onClick": onClickAction
  140. }, [action]);
  141. };
  142. onMounted(() => {
  143. calcEllipsised();
  144. if (slots.action) {
  145. nextTick(calcEllipsised);
  146. }
  147. });
  148. onActivated(() => {
  149. if (needRecalculate) {
  150. needRecalculate = false;
  151. calcEllipsised();
  152. }
  153. });
  154. watch([windowWidth, () => [props.content, props.rows, props.position]], calcEllipsised);
  155. useExpose({
  156. toggle
  157. });
  158. return () => _createVNode("div", {
  159. "ref": root,
  160. "class": bem()
  161. }, [expanded.value ? props.content : text.value, hasAction.value ? renderAction() : null]);
  162. }
  163. });
  164. export {
  165. stdin_default as default,
  166. textEllipsisProps
  167. };