yuang-framework-ui-pc
Version:
yuang-framework-ui-pc Library
114 lines (113 loc) • 3.77 kB
JavaScript
import { defineComponent, ref, computed, resolveComponent, createBlock, openBlock, mergeProps, withCtx, renderSlot, createCommentVNode, normalizeProps } from "vue";
import { omit, pick, contentIsEllipsis } from "../utils/core";
import EleTooltip from "../ele-tooltip/index";
import EleText from "../ele-text/index";
import { textPropKeys } from "../ele-text/props";
import { ellipsisProps } from "./props";
const _sfc_main = defineComponent({
name: "EleEllipsis",
components: { EleText, EleTooltip },
props: ellipsisProps,
setup(props) {
const textRef = ref(null);
const virtualRef = ref();
const text = ref("");
const title = computed(() => {
if (!props.tooltip || typeof props.tooltip !== "object" || props.tooltip.original !== true) {
return;
}
return text.value;
});
const tooltipProps = computed(() => {
const isObj = props.tooltip && props.tooltip !== true;
const opt = isObj ? { ...props.tooltip } : {};
opt.content = text.value;
opt.virtualRef = virtualRef.value;
opt.virtualTriggering = true;
return omit(opt, ["original"]);
});
const multiLine = computed(() => {
return !!(props.maxLine && props.maxLine > 1);
});
const textProps = computed(() => {
return Object.assign({ title: title.value }, pick(props, textPropKeys));
});
const rootStyle = computed(() => {
const style = {};
const { lineHeight, maxLine } = props;
if (lineHeight != null) {
const h = typeof lineHeight === "number" ? `${lineHeight}px` : lineHeight;
style.lineHeight = h;
if (multiLine.value) {
style.height = `calc(${h} * ${maxLine})`;
}
}
if (multiLine.value) {
style["-webkit-line-clamp"] = maxLine;
}
return style;
});
const handleHover = (e) => {
if (!props.tooltip) {
virtualRef.value = void 0;
return;
}
const target = e.currentTarget;
if (typeof props.tooltip === "object" && props.tooltip.content) {
if (text.value !== props.tooltip.content) {
text.value = props.tooltip.content;
}
} else {
if (target) {
const temp = contentIsEllipsis(target) ? target.innerText : "";
if (text.value !== temp) {
text.value = temp;
}
}
}
if (text.value && (props.tooltip === true || !props.tooltip.original)) {
if (virtualRef.value !== target) {
virtualRef.value = target;
}
} else {
virtualRef.value = void 0;
}
};
return {
textRef,
virtualRef,
tooltipProps,
multiLine,
textProps,
rootStyle,
handleHover
};
}
});
const _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_EleTooltip = resolveComponent("EleTooltip");
const _component_EleText = resolveComponent("EleText");
return openBlock(), createBlock(_component_EleText, mergeProps(_ctx.textProps, {
ref: "textRef",
style: _ctx.rootStyle,
class: ["ele-ellipsis", { "is-multi-line": _ctx.multiLine }],
onMouseover: _ctx.handleHover
}), {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default"),
_ctx.virtualRef ? (openBlock(), createBlock(_component_EleTooltip, normalizeProps(mergeProps({ key: 0 }, _ctx.tooltipProps)), null, 16)) : createCommentVNode("", true)
]),
_: 3
}, 16, ["style", "class", "onMouseover"]);
}
const index = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
index as default
};