|
| 1 | +import type { Topic } from '../types/dom' |
| 2 | +import type { MindElixirInstance } from '../types' |
| 3 | +import { setAttributes } from '../utils' |
| 4 | +import { getOffsetLT } from '../utils' |
| 5 | + |
| 6 | +function createSvgDom(height: string, width: string) { |
| 7 | + const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') |
| 8 | + setAttributes(svg, { |
| 9 | + version: '1.1', |
| 10 | + xmlns: 'http://www.w3.org/2000/svg', |
| 11 | + height, |
| 12 | + width, |
| 13 | + }) |
| 14 | + return svg |
| 15 | +} |
| 16 | + |
| 17 | +function lineHightToPadding(lineHeight: string, fontSize: string) { |
| 18 | + return (parseInt(lineHeight) - parseInt(fontSize)) / 2 |
| 19 | +} |
| 20 | + |
| 21 | +function generateSvgText(tpc: HTMLElement, tpcStyle: CSSStyleDeclaration, x: number, y: number) { |
| 22 | + const g = document.createElementNS('http://www.w3.org/2000/svg', 'g') |
| 23 | + const content = tpc.childNodes[0].textContent |
| 24 | + const lines = content!.split('\n') |
| 25 | + lines.forEach((line, index) => { |
| 26 | + const text = document.createElementNS('http://www.w3.org/2000/svg', 'text') |
| 27 | + setAttributes(text, { |
| 28 | + x: x + parseInt(tpcStyle.paddingLeft) + '', |
| 29 | + y: |
| 30 | + y + |
| 31 | + parseInt(tpcStyle.paddingTop) + |
| 32 | + lineHightToPadding(tpcStyle.lineHeight, tpcStyle.fontSize) * (index + 1) + |
| 33 | + parseFloat(tpcStyle.fontSize) * (index + 1) + |
| 34 | + '', |
| 35 | + 'text-anchor': 'start', |
| 36 | + 'font-family': tpcStyle.fontFamily, |
| 37 | + 'font-size': `${tpcStyle.fontSize}`, |
| 38 | + 'font-weight': `${tpcStyle.fontWeight}`, |
| 39 | + fill: `${tpcStyle.color}`, |
| 40 | + }) |
| 41 | + text.innerHTML = line |
| 42 | + g.appendChild(text) |
| 43 | + }) |
| 44 | + return g |
| 45 | +} |
| 46 | + |
| 47 | +function generateSvgTextUsingForeignObject(tpc: HTMLElement, tpcStyle: CSSStyleDeclaration, x: number, y: number) { |
| 48 | + const content = tpc.childNodes[0].textContent! |
| 49 | + const foreignObject = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject') |
| 50 | + setAttributes(foreignObject, { |
| 51 | + x: x + parseInt(tpcStyle.paddingLeft) + '', |
| 52 | + y: y + parseInt(tpcStyle.paddingTop) + '', |
| 53 | + width: tpcStyle.width, |
| 54 | + height: tpcStyle.height, |
| 55 | + }) |
| 56 | + const div = document.createElement('div') |
| 57 | + setAttributes(div, { |
| 58 | + xmlns: 'http://www.w3.org/1999/xhtml', |
| 59 | + style: `font-family: ${tpcStyle.fontFamily}; font-size: ${tpcStyle.fontSize}; font-weight: ${tpcStyle.fontWeight}; color: ${tpcStyle.color}; white-space: pre-wrap;`, |
| 60 | + }) |
| 61 | + div.innerHTML = content |
| 62 | + foreignObject.appendChild(div) |
| 63 | + return foreignObject |
| 64 | +} |
| 65 | + |
| 66 | +function convertDivToSvg(mei: MindElixirInstance, tpc: HTMLElement, useForeignObject = false) { |
| 67 | + const tpcStyle = getComputedStyle(tpc) |
| 68 | + const { offsetLeft: x, offsetTop: y } = getOffsetLT(mei.nodes, tpc) |
| 69 | + |
| 70 | + const bg = document.createElementNS('http://www.w3.org/2000/svg', 'rect') |
| 71 | + setAttributes(bg, { |
| 72 | + x: x + '', |
| 73 | + y: y + '', |
| 74 | + rx: tpcStyle.borderRadius, |
| 75 | + ry: tpcStyle.borderRadius, |
| 76 | + width: tpcStyle.width, |
| 77 | + height: tpcStyle.height, |
| 78 | + fill: tpcStyle.backgroundColor, |
| 79 | + stroke: tpcStyle.borderColor, |
| 80 | + 'stroke-width': tpcStyle.borderWidth, |
| 81 | + }) |
| 82 | + const g = document.createElementNS('http://www.w3.org/2000/svg', 'g') |
| 83 | + g.appendChild(bg) |
| 84 | + let text: SVGGElement | null |
| 85 | + if (useForeignObject) { |
| 86 | + text = generateSvgTextUsingForeignObject(tpc, tpcStyle, x, y) |
| 87 | + } else text = generateSvgText(tpc, tpcStyle, x, y) |
| 88 | + g.appendChild(text) |
| 89 | + return g |
| 90 | +} |
| 91 | + |
| 92 | +function convertAToSvg(mei: MindElixirInstance, a: HTMLAnchorElement) { |
| 93 | + const aStyle = getComputedStyle(a) |
| 94 | + const { offsetLeft: x, offsetTop: y } = getOffsetLT(mei.nodes, a) |
| 95 | + const svgA = document.createElementNS('http://www.w3.org/2000/svg', 'a') |
| 96 | + const text = document.createElementNS('http://www.w3.org/2000/svg', 'text') |
| 97 | + setAttributes(text, { |
| 98 | + x: x + '', |
| 99 | + y: y + parseInt(aStyle.fontSize) + '', |
| 100 | + 'text-anchor': 'start', |
| 101 | + 'font-family': aStyle.fontFamily, |
| 102 | + 'font-size': `${aStyle.fontSize}`, |
| 103 | + 'font-weight': `${aStyle.fontWeight}`, |
| 104 | + fill: `${aStyle.color}`, |
| 105 | + }) |
| 106 | + text.innerHTML = a.textContent! |
| 107 | + svgA.appendChild(text) |
| 108 | + svgA.setAttribute('href', a.href) |
| 109 | + return svgA |
| 110 | +} |
| 111 | + |
| 112 | +const padding = 100 |
| 113 | + |
| 114 | +const head = `<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">` |
| 115 | + |
| 116 | +const generateSvg = (mei: MindElixirInstance) => { |
| 117 | + const mapDiv = mei.nodes |
| 118 | + const height = mapDiv.offsetHeight + padding * 2 |
| 119 | + const width = mapDiv.offsetWidth + padding * 2 |
| 120 | + const svg = createSvgDom(height + 'px', width + 'px') |
| 121 | + const g = document.createElementNS('http://www.w3.org/2000/svg', 'svg') |
| 122 | + const bgColor = document.createElementNS('http://www.w3.org/2000/svg', 'rect') |
| 123 | + setAttributes(bgColor, { |
| 124 | + x: '0', |
| 125 | + y: '0', |
| 126 | + width: `${width}`, |
| 127 | + height: `${height}`, |
| 128 | + fill: mei.theme.cssVar['--bgcolor'] as string, |
| 129 | + }) |
| 130 | + svg.appendChild(bgColor) |
| 131 | + mapDiv.querySelectorAll('.subLines').forEach(item => { |
| 132 | + const clone = item.cloneNode(true) as SVGSVGElement |
| 133 | + const { offsetLeft, offsetTop } = getOffsetLT(mapDiv, item.parentElement as HTMLElement) |
| 134 | + clone.setAttribute('x', `${offsetLeft}`) |
| 135 | + clone.setAttribute('y', `${offsetTop}`) |
| 136 | + g.appendChild(clone) |
| 137 | + }) |
| 138 | + |
| 139 | + const mainLine = mapDiv.querySelector('.lines')?.cloneNode(true) |
| 140 | + mainLine && g.appendChild(mainLine) |
| 141 | + const topiclinks = mapDiv.querySelector('.topiclinks')?.cloneNode(true) |
| 142 | + topiclinks && g.appendChild(topiclinks) |
| 143 | + const summaries = mapDiv.querySelector('.summary')?.cloneNode(true) |
| 144 | + summaries && g.appendChild(summaries) |
| 145 | + |
| 146 | + mapDiv.querySelectorAll('me-tpc').forEach(tpc => { |
| 147 | + g.appendChild(convertDivToSvg(mei, tpc as Topic, true)) |
| 148 | + }) |
| 149 | + mapDiv.querySelectorAll('.tags > span').forEach(tag => { |
| 150 | + g.appendChild(convertDivToSvg(mei, tag as HTMLElement)) |
| 151 | + }) |
| 152 | + mapDiv.querySelectorAll('.icons > span').forEach(icon => { |
| 153 | + g.appendChild(convertDivToSvg(mei, icon as HTMLElement)) |
| 154 | + }) |
| 155 | + mapDiv.querySelectorAll('.hyper-link').forEach(hl => { |
| 156 | + g.appendChild(convertAToSvg(mei, hl as HTMLAnchorElement)) |
| 157 | + }) |
| 158 | + setAttributes(g, { |
| 159 | + x: padding + '', |
| 160 | + y: padding + '', |
| 161 | + overflow: 'visible', |
| 162 | + }) |
| 163 | + svg.appendChild(g) |
| 164 | + return head + svg.outerHTML |
| 165 | +} |
| 166 | + |
| 167 | +function blobToUrl(blob: Blob): Promise<string> { |
| 168 | + return new Promise((resolve, reject) => { |
| 169 | + const reader = new FileReader() |
| 170 | + reader.onload = evt => { |
| 171 | + resolve(evt.target!.result as string) |
| 172 | + } |
| 173 | + reader.onerror = err => { |
| 174 | + reject(err) |
| 175 | + } |
| 176 | + reader.readAsDataURL(blob) |
| 177 | + }) |
| 178 | +} |
| 179 | + |
| 180 | +export const exportSvg = function (this: MindElixirInstance) { |
| 181 | + const svgString = generateSvg(this) |
| 182 | + const blob = new Blob([svgString], { type: 'image/svg+xml' }) |
| 183 | + return blob |
| 184 | +} |
| 185 | + |
| 186 | +export const exportPng = async function (this: MindElixirInstance): Promise<Blob | null> { |
| 187 | + const svgString = generateSvg(this) |
| 188 | + const blob = new Blob([svgString], { type: 'image/svg+xml' }) |
| 189 | + // use base64 to bypass canvas taint |
| 190 | + const url = await blobToUrl(blob) |
| 191 | + return new Promise((resolve, reject) => { |
| 192 | + const img = new Image() |
| 193 | + img.setAttribute('crossOrigin', 'anonymous') |
| 194 | + img.onload = () => { |
| 195 | + const canvas = document.createElement('canvas') |
| 196 | + canvas.width = img.width |
| 197 | + canvas.height = img.height |
| 198 | + const ctx = canvas.getContext('2d')! |
| 199 | + ctx.drawImage(img, 0, 0) |
| 200 | + canvas.toBlob(resolve, 'image/png', 1) |
| 201 | + } |
| 202 | + img.src = url |
| 203 | + img.onerror = reject |
| 204 | + }) |
| 205 | +} |
0 commit comments