2026-05-24 18:13:33 +08:00
|
|
|
import { defineComponent, getCurrentInstance, h, type PropType } from "vue";
|
|
|
|
|
|
|
|
|
|
type Color = string | [number, number, number];
|
2026-05-25 20:45:21 +08:00
|
|
|
type WrapMode =
|
|
|
|
|
| "wrap"
|
|
|
|
|
| "hard"
|
|
|
|
|
| "truncate"
|
|
|
|
|
| "truncate-end"
|
|
|
|
|
| "truncate-middle"
|
|
|
|
|
| "truncate-start";
|
2026-05-24 18:13:33 +08:00
|
|
|
|
|
|
|
|
export const Text = defineComponent({
|
|
|
|
|
name: "Text",
|
|
|
|
|
props: {
|
|
|
|
|
color: [String, Array] as PropType<Color>,
|
|
|
|
|
backgroundColor: [String, Array] as PropType<Color>,
|
|
|
|
|
dimColor: Boolean,
|
|
|
|
|
bold: Boolean,
|
|
|
|
|
italic: Boolean,
|
|
|
|
|
underline: Boolean,
|
|
|
|
|
strikethrough: Boolean,
|
|
|
|
|
inverse: Boolean,
|
|
|
|
|
wrap: { type: String as PropType<WrapMode>, default: "wrap" },
|
|
|
|
|
},
|
|
|
|
|
setup(props, { slots }) {
|
|
|
|
|
return () => {
|
|
|
|
|
const insideText = isInsideText();
|
|
|
|
|
const elementType = insideText ? "virtual-text" : "text";
|
|
|
|
|
return h(elementType, props as never, slots.default?.());
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function isInsideText(): boolean {
|
|
|
|
|
let parent = getCurrentInstance()?.parent;
|
|
|
|
|
while (parent) {
|
|
|
|
|
const name = parent.type && (parent.type as { name?: string }).name;
|
|
|
|
|
if (name === "Text") return true;
|
|
|
|
|
parent = parent.parent;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|