From ca36b2163f076fa3b69144da7d2dafcd8531458c Mon Sep 17 00:00:00 2001 From: ssshooter Date: Tue, 22 Nov 2022 15:46:26 +0800 Subject: [PATCH] feat: add data2Xmind function, code optimization --- index.js | 43 ++++++++++++++++++++++++------------------- main.js | 3 ++- package.json | 2 +- readme.md | 12 +++++++++++- xmind/metadata.js | 4 ++-- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index e9cefc8..303a889 100644 --- a/index.js +++ b/index.js @@ -42,30 +42,35 @@ function convert(data) { } } +export function data2Xmind(data, version) { + const zip = new JSZip() + const id = generateUUID() + + if (version) { + metadata.creator.version = version + } + metadata.activeSheetId = id + const c0 = content[0] + c0.id = id + c0.title = data.nodeData.topic + convert(data.nodeData) + c0.rootTopic = data.nodeData + zip.file('manifest.json', JSON.stringify(manifest)) + zip.file('metadata.json', JSON.stringify(metadata)) + zip.file('content.json', JSON.stringify(content)) + + return zip.generateAsync({ type: 'blob' }) +} + export default function (me) { me.exportXmind = function () { - const zip = new JSZip() const data = me.getAllData() - const id = generateUUID() - - metadata.creator.version = me.version - metadata.activeSheetId = id - const c0 = content[0] - c0.id = id - c0.title = data.nodeData.topic - const clone = JSON.parse(JSON.stringify(data.nodeData)) - convert(clone) - c0.rootTopic = clone - zip.file('manifest.json', JSON.stringify(manifest)) - zip.file('metadata.json', JSON.stringify(metadata)) - zip.file('content.json', JSON.stringify(content)) - - return zip.generateAsync({ type: 'blob' }) + return data2Xmind(data, me.version) } me.exportXmindFile = async function (fileName) { const data = me.getAllData() - this.exportXmind().then(function (content) { - saveAs(content, (fileName || data.nodeData.topic) + '.xmind') - }) + const file = await data2Xmind(data, me.version) + // `topic` had renamed to `title` + saveAs(file, (fileName || data.nodeData.title) + '.xmind') } } diff --git a/main.js b/main.js index 3cd73f8..c12a516 100644 --- a/main.js +++ b/main.js @@ -2,7 +2,7 @@ import MindElixir, { E } from 'mind-elixir' import { exportSvg, exportPng } from 'mind-elixir/dist/painter' import example from 'mind-elixir/dist/example1' -import exportXmind from './index' +import exportXmind, { data2Xmind } from './index' // import exportXmind from '@mind-elixir/export-xmind' const app = document.querySelector('#app') @@ -90,3 +90,4 @@ window.M = MindElixir window.E = MindElixir.E window.exportSvg = exportSvg window.exportPng = exportPng +window.data2Xmind = data2Xmind diff --git a/package.json b/package.json index d2846f8..7507c2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mind-elixir/export-xmind", - "version": "1.0.0", + "version": "1.1.0", "type": "module", "files": [ "dist" diff --git a/readme.md b/readme.md index f01ba10..4a58942 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,8 @@ npm i @mind-elixir/export-xmind jszip file-saver ## How To Use +### Use As A Plugin + ```javascript import MindElixir from 'mind-elixir' import exportXmind from '@mind-elixir/export-xmind' @@ -20,5 +22,13 @@ mind.init(data) const blob = await mind.exportXmind() // get blob -mind.exportXmindFile() // download file +mind.exportXmindFile(fileName) // download file +``` + +### MindElixir Data To Xmind + +```javascript +import { data2Xmind } from '@mind-elixir/export-xmind' +// use this way to avoid creating instance +const blob = await data2Xmind(data) // data returned by getAllData() ``` diff --git a/xmind/metadata.js b/xmind/metadata.js index 402b9cc..4c88a49 100644 --- a/xmind/metadata.js +++ b/xmind/metadata.js @@ -1,7 +1,7 @@ export default { creator: { name: 'Mind-Elixir', - version: '12.0.3.202206241736', + version: '1.0.0', }, - activeSheetId: '17b972f40eea83577fe8ab16e5', + activeSheetId: '', }