Files
claude-code-mirror/claude-code源码-中文注释/src/ink/get-max-width.ts
2026-04-03 13:01:19 +08:00

27 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { LayoutEdge, type LayoutNode } from './layout/node.js'
/**
* 返回 yoga 节点的内容宽度(计算宽度减去内边距和边框)。
*
* 警告:可能返回比父容器更宽的值。
* 在列方向 flex 父级中width 是横轴 —— align-items:
* stretch 从不将子元素收缩到其内在大小以下,所以文本
* 节点溢出(标准 CSS 行为。Yoga 以两次 pass 测量叶节点:
* AtMost pass 确定宽度Exactly pass 确定高度。
* getComputedWidth() 反映更宽的 AtMost 结果,而
* getComputedHeight() 反映更窄的 Exactly 结果。
* 使用此函数进行换行的调用者应限制为实际可用的屏幕空间,
* 以便渲染的行数与布局高度保持一致。
*/
const getMaxWidth = (yogaNode: LayoutNode): number => {
return (
yogaNode.getComputedWidth() -
yogaNode.getComputedPadding(LayoutEdge.Left) -
yogaNode.getComputedPadding(LayoutEdge.Right) -
yogaNode.getComputedBorder(LayoutEdge.Left) -
yogaNode.getComputedBorder(LayoutEdge.Right)
)
}
export default getMaxWidth