Feat:新增支持txt文件的导出
This commit is contained in:
parent
2baa500c17
commit
660ec00ca7
35
simple-mind-map/src/parse/toTxt.js
Normal file
35
simple-mind-map/src/parse/toTxt.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { walk, nodeRichTextToTextWithWrap } from '../utils'
|
||||||
|
|
||||||
|
const getNodeText = data => {
|
||||||
|
return data.richText ? nodeRichTextToTextWithWrap(data.text) : data.text
|
||||||
|
}
|
||||||
|
|
||||||
|
const getIndent = level => {
|
||||||
|
return new Array(level).fill(' ').join('')
|
||||||
|
}
|
||||||
|
|
||||||
|
// 转换成txt格式
|
||||||
|
export const transformToTxt = root => {
|
||||||
|
let content = ''
|
||||||
|
walk(
|
||||||
|
root,
|
||||||
|
null,
|
||||||
|
(node, parent, isRoot, layerIndex) => {
|
||||||
|
content += getIndent(layerIndex)
|
||||||
|
content += ' ' + getNodeText(node.data)
|
||||||
|
// 概要
|
||||||
|
const generalization = node.data.generalization
|
||||||
|
if (Array.isArray(generalization)) {
|
||||||
|
content += generalization.map(item => {
|
||||||
|
return ` [${getNodeText(item)}]`
|
||||||
|
})
|
||||||
|
} else if (generalization && generalization.text) {
|
||||||
|
content += ` [${getNodeText(generalization)}]`
|
||||||
|
}
|
||||||
|
content += '\n\n'
|
||||||
|
},
|
||||||
|
() => {},
|
||||||
|
true
|
||||||
|
)
|
||||||
|
return content
|
||||||
|
}
|
||||||
@ -10,6 +10,7 @@ import { SVG } from '@svgdotjs/svg.js'
|
|||||||
import drawBackgroundImageToCanvas from '../utils/simulateCSSBackgroundInCanvas'
|
import drawBackgroundImageToCanvas from '../utils/simulateCSSBackgroundInCanvas'
|
||||||
import { transformToMarkdown } from '../parse/toMarkdown'
|
import { transformToMarkdown } from '../parse/toMarkdown'
|
||||||
import { ERROR_TYPES } from '../constants/constant'
|
import { ERROR_TYPES } from '../constants/constant'
|
||||||
|
import { transformToTxt } from '../parse/toTxt'
|
||||||
|
|
||||||
// 导出插件
|
// 导出插件
|
||||||
class Export {
|
class Export {
|
||||||
@ -294,6 +295,15 @@ class Export {
|
|||||||
const res = await readBlob(blob)
|
const res = await readBlob(blob)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// txt文件
|
||||||
|
async txt() {
|
||||||
|
const data = this.mindMap.getData()
|
||||||
|
const content = transformToTxt(data)
|
||||||
|
const blob = new Blob([content])
|
||||||
|
const res = await readBlob(blob)
|
||||||
|
return res
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Export.instanceName = 'doExport'
|
Export.instanceName = 'doExport'
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import {
|
|||||||
selfCloseTagList
|
selfCloseTagList
|
||||||
} from '../constants/constant'
|
} from '../constants/constant'
|
||||||
import MersenneTwister from './mersenneTwister'
|
import MersenneTwister from './mersenneTwister'
|
||||||
|
|
||||||
// 深度优先遍历树
|
// 深度优先遍历树
|
||||||
export const walk = (
|
export const walk = (
|
||||||
root,
|
root,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user