import { measureElement, updateElementSizeCache } from "@plait/common"; import type { PlaitBoard } from "@plait/core"; import { getDefaultFontSizeForMindElement, NodeSpace, type MindElement, type PlaitMindBoard } from "@plait/mind"; const measured = new WeakMap(); /** Plait measures with a system font by default. Seed its cache with the canvas font. */ export function measureXmindTopics(board: PlaitBoard, element: MindElement) { const topic = element.data?.topic; const maxWidth = NodeSpace.getTopicMaxDynamicWidth(board as PlaitMindBoard, element); if (topic && measured.get(topic) !== maxWidth) { const size = measureElement(board, topic, { fontSize: getDefaultFontSizeForMindElement(element), fontFamily: 'NeverMind, Arial, "PingFang SC", sans-serif', }, maxWidth); updateElementSizeCache(board, topic, { width: Math.ceil(size.width) + 2, height: Math.ceil(size.height), }); measured.set(topic, maxWidth); } for (const child of element.children || []) measureXmindTopics(board, child); }