import { Utility } from "@hpcc-js/common"; import * as React from "@hpcc-js/preact-shim"; import { Icon } from "./icon"; import { Rectangle } from "./shape"; interface TextLine { text: string; height?: number; anchor?: string; baseline?: string; fontFamily?: string; fill?: string; } export const TextLine: React.FunctionComponent = ({ text, height = 12, anchor = "middle", baseline = "middle", fontFamily = "Verdana", fill = "black" }) => { return {text}; }; interface Text { text: string; height?: number; fontFamily?: string; fill?: string; onSizeUpdate?: (size: { width: number, height: number }) => void; } export const Text: React.FunctionComponent = ({ text, height = 12, fontFamily = "Verdana", fill = "black", onSizeUpdate }) => { const [totalWidth, setTotalWidthUpdate] = React.useState(0); const [totalHeight, setTotalHeightUpdate] = React.useState(0); React.useEffect(() => { onSizeUpdate && onSizeUpdate({ width: totalWidth, height: totalHeight }); }, [totalWidth, totalHeight, onSizeUpdate]); const parts = React.useMemo(() => { return text.split("\n"); }, [text]); const ts = React.useMemo(() => { return Utility.textSize(parts, fontFamily, height); }, [fontFamily, height, parts]); setTotalWidthUpdate(ts.width); setTotalHeightUpdate(parts.length * (height + 2) - 2); const yOffset = -(totalHeight / 2) + (height / 2); const TextLines = React.useMemo(() => { return parts.map((p, i) => { return ; }); }, [fill, fontFamily, height, parts, yOffset]); return <>{TextLines}; }; export interface TextBox { text: string; height?: number; fontFamily?: string; padding?: number; fill?: string; stroke?: string; textFill?: string; strokeWidth?: number; cornerRadius?: number; textOffsetY?: number; onSizeUpdate?: (size: { width: number, height: number }) => void; } export const TextBox: React.FunctionComponent = ({ text, height = 12, fontFamily = "Verdana", padding = 4, fill = "whitesmoke", stroke = "lightgray", textFill = "black", strokeWidth = 1, cornerRadius = 0, onSizeUpdate }) => { const [textWidth, setTextWidthUpdate] = React.useState(0); const [textHeight, setTextHeightUpdate] = React.useState(0); React.useEffect(() => { onSizeUpdate && onSizeUpdate({ width: textWidth, height: textHeight }); }, [textWidth, textHeight, onSizeUpdate]); const onTextSizeUpdate = React.useCallback(size => { setTextWidthUpdate(size.width); setTextHeightUpdate(size.height); }, []); const w = textWidth + padding * 2 + strokeWidth; const h = textHeight + padding * 2 + strokeWidth; const textOffsetY = Math.floor(height / 10); return <> ; }; export interface LabelledRect extends TextBox { width?: number; fontSize?: number; } export interface IconLabelledRect extends LabelledRect { icon: string; iconFontFamily: string; iconFontSize: number; } export const LabelledRect: React.FunctionComponent = ({ text, height = 12, width = 12, fontFamily = "Verdana", fontSize = 10, padding = 3, fill = "whitesmoke", stroke = "lightgray", textFill = "black", strokeWidth = 1, cornerRadius = 0, onSizeUpdate = (size: { width: number, height: number }) => { } }) => { return <> ; }; export const IconLabelledRect: React.FunctionComponent = ({ icon, iconFontFamily, text, height = 12, width = 12, fontFamily = "Verdana", fontSize = 10, padding = 3, fill = "whitesmoke", stroke = "lightgray", textFill = "black", strokeWidth = 1, cornerRadius = 0, onSizeUpdate = (size: { width: number, height: number }) => { } }) => { return <> ; };