import { mdiXml } from "@mdi/js"; import Icon from "@mdi/react"; import PictureAsPdfIcon from "@mui/icons-material/PictureAsPdf"; import { IconButton, Tooltip } from "@mui/material"; import jsPDF from "jspdf"; import React from "react"; import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch"; import { Couple } from "../../api/CoupleApi"; import { Member } from "../../api/MemberApi"; import { useDarkTheme } from "../../hooks/context_providers/DarkThemeProvider"; import { FamilyTreeNode } from "../../utils/family_tree"; import { downloadBlob } from "../../utils/files_utils"; import { getTextWidth } from "../../utils/render_utils"; import "./simpletree.css"; import "./Roboto-normal"; const FACE_WIDTH = 60; const FACE_HEIGHT = 70; /** * Vertical space between faces and text */ const FACE_TEXT_SPACING = 2; /** * Cards height */ const CARD_HEIGHT = 114; /** * Space between spouse cards */ const SPOUSE_SPACING = 10; /** * Space between two siblings hierachy */ const SIBLINGS_SPACING = 15; /** * Vertical space between two generations */ const LEVEL_SPACING = 25; const NAME_FONT = "13px Roboto"; const BIRTH_FONT = "10px Roboto"; interface SimpleTreeSpouseInfo { member: Member; couple: Couple; } interface SimpleTreeNode { member: Member; spouse?: SimpleTreeSpouseInfo; down: SimpleTreeNode[]; /** * The width of the parent and its children */ width: number; /** * The width of the parents */ parentWidth: number; /** * The sum of the width of the children */ childrenWidth: number; } /** * Get the width of a member card */ function memberCardWidth(m?: Member): number { if (!m) return 0; const firstNameWidth = getTextWidth(m.first_name ?? "", NAME_FONT); const lastNameWidth = getTextWidth(m.lastNameUpperCase ?? "", NAME_FONT); const birthDeathWidth = getTextWidth(m.displayBirthDeath, BIRTH_FONT); return Math.max(FACE_WIDTH, firstNameWidth, lastNameWidth, birthDeathWidth); } function center(container_width: number, el_width: number): number { return Math.floor((container_width - el_width) / 2); } function buildSimpleTreeNode( tree: FamilyTreeNode, depth: number ): SimpleTreeNode { if (depth === 0) throw new Error("Too much recursion reached!"); const lastCouple = tree.couples?.[tree.couples?.length - 1 ?? 0]; // Preprocess children let childrenToProcess = tree.down; if (depth > 1) tree.couples?.forEach( (c) => (childrenToProcess = childrenToProcess?.concat(c.down)) ); else childrenToProcess = []; const node: SimpleTreeNode = { down: childrenToProcess?.map((c) => buildSimpleTreeNode(c, depth - 1)) ?? [], member: tree.member, spouse: lastCouple ? { couple: lastCouple.couple, member: lastCouple.member, } : undefined, parentWidth: -1, childrenWidth: -1, width: -1, }; // Compute current level width let levelWidth: number; if (node.spouse) { levelWidth = SPOUSE_SPACING + memberCardWidth(node.member) + memberCardWidth(node.spouse.member); } else { levelWidth = memberCardWidth(node.member); } // Compute down level width const downWidth = SIBLINGS_SPACING * node.down.length - SIBLINGS_SPACING + node.down.reduce((prev, n) => prev + n.width, 0); node.parentWidth = levelWidth; node.childrenWidth = downWidth; node.width = Math.max(levelWidth, downWidth); return node; } /** * Simple family tree * * Only one couple can be shown in this version */ export function SimpleFamilyTree(p: { tree: FamilyTreeNode; depth: number; }): React.ReactElement { const darkTheme = useDarkTheme(); const tree = React.useMemo( () => buildSimpleTreeNode(p.tree, p.depth), [p.tree, p.depth] ); const height = p.depth * (CARD_HEIGHT + LEVEL_SPACING) - LEVEL_SPACING; console.info(`tree width=${tree.width} height=${height}`); const doExport = async (onlySVG: boolean) => { const el: HTMLElement = document.querySelector(".simpletree")!; const svg = el.outerHTML; // Download in SVG format if (onlySVG) { const blob = new Blob([svg], { type: "image/svg+xml", }); downloadBlob(blob, "ArbreGenealogique.svg"); return; } const PDF_MARGIN = 10; const PDF_MAX_SIZE = 14400 - PDF_MARGIN * 2; const tree_width = Math.min(tree.width, PDF_MAX_SIZE); const tree_height = Math.min(height, PDF_MAX_SIZE); console.info(`pdf tree w=${tree_width} h=${tree_height}`); // Download in PDF format const doc = new jsPDF({ orientation: "l", unit: "px", format: [tree_height + PDF_MARGIN * 2, tree_width + PDF_MARGIN * 2], }); doc.setFont("Roboto", "normal"); await doc.svg(el, { x: PDF_MARGIN, y: PDF_MARGIN, height: tree_height, width: tree_width, }); // Save the created pdf doc.save("ArbreGenealogique.pdf"); }; const exportPDF = () => doExport(false); const exportSVG = () => doExport(true); return (