text2.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { defineComponent, ref, computed, useAttrs, onMounted, onUpdated, openBlock, createBlock, resolveDynamicComponent, normalizeClass, unref, normalizeStyle, withCtx, renderSlot } from 'vue';
  2. import { textProps } from './text.mjs';
  3. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  4. import { useFormSize } from '../../form/src/hooks/use-form-common-props.mjs';
  5. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  6. import { isUndefined } from '../../../utils/types.mjs';
  7. const __default__ = defineComponent({
  8. name: "ElText"
  9. });
  10. const _sfc_main = /* @__PURE__ */ defineComponent({
  11. ...__default__,
  12. props: textProps,
  13. setup(__props) {
  14. const props = __props;
  15. const textRef = ref();
  16. const textSize = useFormSize();
  17. const ns = useNamespace("text");
  18. const textKls = computed(() => [
  19. ns.b(),
  20. ns.m(props.type),
  21. ns.m(textSize.value),
  22. ns.is("truncated", props.truncated),
  23. ns.is("line-clamp", !isUndefined(props.lineClamp))
  24. ]);
  25. const inheritTitle = useAttrs().title;
  26. const bindTitle = () => {
  27. var _a, _b, _c, _d, _e;
  28. if (inheritTitle)
  29. return;
  30. let shouldAddTitle = false;
  31. const text = ((_a = textRef.value) == null ? void 0 : _a.textContent) || "";
  32. if (props.truncated) {
  33. const width = (_b = textRef.value) == null ? void 0 : _b.offsetWidth;
  34. const scrollWidth = (_c = textRef.value) == null ? void 0 : _c.scrollWidth;
  35. if (width && scrollWidth && scrollWidth > width) {
  36. shouldAddTitle = true;
  37. }
  38. } else if (!isUndefined(props.lineClamp)) {
  39. const height = (_d = textRef.value) == null ? void 0 : _d.offsetHeight;
  40. const scrollHeight = (_e = textRef.value) == null ? void 0 : _e.scrollHeight;
  41. if (height && scrollHeight && scrollHeight > height) {
  42. shouldAddTitle = true;
  43. }
  44. }
  45. if (shouldAddTitle) {
  46. textRef.value.setAttribute("title", text);
  47. } else {
  48. textRef.value.removeAttribute("title");
  49. }
  50. };
  51. onMounted(bindTitle);
  52. onUpdated(bindTitle);
  53. return (_ctx, _cache) => {
  54. return openBlock(), createBlock(resolveDynamicComponent(_ctx.tag), {
  55. ref_key: "textRef",
  56. ref: textRef,
  57. class: normalizeClass(unref(textKls)),
  58. style: normalizeStyle({ "-webkit-line-clamp": _ctx.lineClamp })
  59. }, {
  60. default: withCtx(() => [
  61. renderSlot(_ctx.$slots, "default")
  62. ]),
  63. _: 3
  64. }, 8, ["class", "style"]);
  65. };
  66. }
  67. });
  68. var Text = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "text.vue"]]);
  69. export { Text as default };
  70. //# sourceMappingURL=text2.mjs.map