|
1 | 1 | import { TreeNode } from "./newick-loader"; |
2 | 2 |
|
3 | 3 | export function DownloadNewick(data: TreeNode, fileName: string) { |
4 | | - const toNewick = (node: TreeNode): string => { |
5 | | - if (!node.branchset || node.branchset.length === 0) { |
6 | | - return node.name ? `${node.name}:${node.length || 0}` : ''; |
7 | | - } |
8 | | - |
9 | | - const branches = node.branchset.map(child => toNewick(child)); |
10 | | - return `(${branches.join(',')})${node.name ? `:${node.length || 0}` : ''}`; |
11 | | - }; |
12 | | - |
13 | | - const newickContent = toNewick(data); |
14 | | - const blob = new Blob([newickContent], { type: "text/plain;charset=utf-8;" }); |
15 | | - const url = URL.createObjectURL(blob); |
16 | | - const link = document.createElement("a"); |
17 | | - link.setAttribute("href", url); |
18 | | - link.setAttribute("download", `${fileName}.nwk`); |
19 | | - link.click(); |
20 | | - URL.revokeObjectURL(url); |
| 4 | + const toNewick = (node: TreeNode): string => { |
| 5 | + if (!node.branchset || node.branchset.length === 0) { |
| 6 | + return node.name ? `${node.name}:${node.length || 0}` : ""; |
| 7 | + } |
| 8 | + |
| 9 | + const branches = node.branchset.map((child) => toNewick(child)); |
| 10 | + return `(${branches.join(",")})${node.name ? `:${node.length || 0}` : ""}`; |
| 11 | + }; |
| 12 | + |
| 13 | + const newickContentData = toNewick(data); |
| 14 | + const blob = new Blob([newickContentData], { |
| 15 | + type: "text/plain;charset=utf-8;", |
| 16 | + }); |
| 17 | + const url = URL.createObjectURL(blob); |
| 18 | + const link = document.createElement("a"); |
| 19 | + link.setAttribute("href", url); |
| 20 | + link.setAttribute("download", `${fileName}.nwk`); |
| 21 | + link.click(); |
| 22 | + URL.revokeObjectURL(url); |
21 | 23 | } |
22 | 24 |
|
23 | 25 | export function serializeSVG(svg: SVGElement): Blob { |
24 | | - const xmlns = "http://www.w3.org/2000/xmlns/"; |
25 | | - const xlinkns = "http://www.w3.org/1999/xlink"; |
26 | | - const svgns = "http://www.w3.org/2000/svg"; |
27 | | - |
28 | | - const clonedSvg = svg.cloneNode(true) as SVGElement; |
29 | | - const fragment = window.location.href + "#"; |
30 | | - |
31 | | - const walker = document.createTreeWalker(clonedSvg, NodeFilter.SHOW_ELEMENT); |
32 | | - while (walker.nextNode()) { |
33 | | - const currentNode = walker.currentNode as Element; |
34 | | - for (const attr of Array.from(currentNode.attributes)) { |
35 | | - if (attr.value.includes(fragment)) { |
36 | | - attr.value = attr.value.replace(fragment, "#"); |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - clonedSvg.setAttributeNS(xmlns, "xmlns", svgns); |
42 | | - clonedSvg.setAttributeNS(xmlns, "xmlns:xlink", xlinkns); |
43 | | - |
44 | | - const serializer = new XMLSerializer(); |
45 | | - const svgString = serializer.serializeToString(clonedSvg); |
46 | | - |
47 | | - return new Blob([svgString], { type: "image/svg+xml" }); |
| 26 | + const xmlns = "http://www.w3.org/2000/xmlns/"; |
| 27 | + const xlinkns = "http://www.w3.org/1999/xlink"; |
| 28 | + const svgns = "http://www.w3.org/2000/svg"; |
| 29 | + |
| 30 | + const clonedSvg = svg.cloneNode(true) as SVGElement; |
| 31 | + const fragment = window.location.href + "#"; |
| 32 | + |
| 33 | + const walker = document.createTreeWalker(clonedSvg, NodeFilter.SHOW_ELEMENT); |
| 34 | + while (walker.nextNode()) { |
| 35 | + const currentNode = walker.currentNode as Element; |
| 36 | + for (const attr of Array.from(currentNode.attributes)) { |
| 37 | + if (attr.value.includes(fragment)) { |
| 38 | + attr.value = attr.value.replace(fragment, "#"); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + clonedSvg.setAttributeNS(xmlns, "xmlns", svgns); |
| 44 | + clonedSvg.setAttributeNS(xmlns, "xmlns:xlink", xlinkns); |
| 45 | + |
| 46 | + const serializer = new XMLSerializer(); |
| 47 | + const svgString = serializer.serializeToString(clonedSvg); |
| 48 | + |
| 49 | + return new Blob([svgString], { type: "image/svg+xml" }); |
48 | 50 | } |
0 commit comments