Skip to content

Commit 45e7430

Browse files
committed
feat: bind exportImage to ME instance
1 parent 4935321 commit 45e7430

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ Mind elixir is a free open source mind map core.
3131
- High performance
3232
- Framework agnostic
3333
- Pluginable
34+
- Export as SVG or PNG
3435
- Build-in drag and drop / node edit plugin
3536
- Summarize nodes
3637
- Undo / Redo
3738
- Styling your node with CSS
39+
- Efficient shortcuts
3840

3941
<details>
4042
<summary>Table of Contents</summary>
@@ -50,6 +52,7 @@ Mind elixir is a free open source mind map core.
5052
- [Event Handling](#event-handling)
5153
- [Data Export And Import](#data-export-and-import)
5254
- [Operation Guards](#operation-guards)
55+
- [Export as a Image](#export-as-a-image)
5356
- [Methods](#methods)
5457
- [Theme](#theme)
5558
- [Shortcuts](#shortcuts)
@@ -253,6 +256,24 @@ let mind = new MindElixir({
253256
})
254257
```
255258

259+
## Export as a Image
260+
261+
```typescript
262+
import { exportPng } from './plugin/exportImage'
263+
264+
const mind = {/** mind elixir instance */}
265+
const downloadPng = async () => {
266+
const blob = await exportPng(mind) // Get a Blob!
267+
if (!blob) return
268+
const url = URL.createObjectURL(blob)
269+
const a = document.createElement('a')
270+
a.href = url
271+
a.download = 'filename.png'
272+
a.click()
273+
URL.revokeObjectURL(url)
274+
}
275+
```
276+
256277
## Methods
257278

258279
https://github.com/ssshooter/mind-elixir-core/blob/master/api/mind-elixir.api.md

src/dev.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ import example2 from './exampleData/2'
55
import example3 from './exampleData/3'
66
import type { Options, MindElixirData, MindElixirInstance } from './types/index'
77
import type { Operation } from './utils/pubsub'
8-
import { exportSvg, exportPng } from './plugin/exportImage'
8+
import { exportPng } from './plugin/exportImage'
99

10-
window.exportSvg = exportSvg
11-
window.exportPng = exportPng
1210
interface Window {
1311
m: MindElixirInstance
1412
M: MindElixirCtor
1513
E: typeof MindElixir.E
16-
exportSvg: typeof exportSvg
17-
exportPng: typeof exportPng
14+
downloadPng: typeof downloadPng
1815
}
1916

2017
declare let window: Window
@@ -104,6 +101,19 @@ mind.bus.addListener('selectNode', node => {
104101
mind.bus.addListener('expandNode', node => {
105102
console.log('expandNode: ', node)
106103
})
104+
105+
const downloadPng = async () => {
106+
const blob = await mind.exportPng()
107+
if (!blob) return
108+
const url = URL.createObjectURL(blob)
109+
const a = document.createElement('a')
110+
a.href = url
111+
a.download = 'filename.png'
112+
a.click()
113+
URL.revokeObjectURL(url)
114+
}
115+
116+
window.downloadPng = downloadPng
107117
window.m = mind
108118
// window.m2 = mind2
109119
window.M = MindElixir

src/methods.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import * as interact from './interact'
1515
import * as nodeOperation from './nodeOperation'
1616
import * as customLink from './customLink'
1717
import * as summaryOperation from './summary'
18+
import * as exportImage from './plugin/exportImage'
1819

1920
type Operations = keyof typeof nodeOperation
2021
type NodeOperation = Record<Operations, ReturnType<typeof beforeHook>>
@@ -62,6 +63,7 @@ const methods = {
6263
...(nodeOperationHooked as NodeOperation),
6364
...customLink,
6465
...summaryOperation,
66+
...exportImage,
6567
init(this: MindElixirInstance, data: MindElixirData) {
6668
if (!data || !data.nodeData) return new Error('MindElixir: `data` is required')
6769
if (data.direction !== undefined) {

src/plugin/exportImage.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,14 @@ function blobToUrl(blob: Blob): Promise<string> {
177177
})
178178
}
179179

180-
export const exportSvg = (mei: MindElixirInstance) => {
181-
const svgString = generateSvg(mei)
180+
export const exportSvg = function (this: MindElixirInstance) {
181+
const svgString = generateSvg(this)
182182
const blob = new Blob([svgString], { type: 'image/svg+xml' })
183183
return blob
184184
}
185185

186-
export const exportPng = async (mei: MindElixirInstance) => {
187-
const svgString = generateSvg(mei)
186+
export const exportPng = async function (this: MindElixirInstance): Promise<Blob | null> {
187+
const svgString = generateSvg(this)
188188
const blob = new Blob([svgString], { type: 'image/svg+xml' })
189189
// use base64 to bypass canvas taint
190190
const url = await blobToUrl(blob)

0 commit comments

Comments
 (0)