|
| 1 | +<template> |
| 2 | + <div class="mindmap-fullscreen"> |
| 3 | + <div id="mindMapContainerFull" ref="containerRef" class="mind-map-container-full"></div> |
| 4 | + |
| 5 | + <div class="fullscreen-toolbar"> |
| 6 | + <el-button type="primary" :icon="Close" circle size="small" title="退出全屏" @click="handleClose" /> |
| 7 | + <div class="toolbar-divider"></div> |
| 8 | + <el-button type="default" :icon="ZoomIn" circle size="small" title="放大" @click="handleZoomIn" /> |
| 9 | + <el-button type="default" :icon="ZoomOut" circle size="small" title="缩小" @click="handleZoomOut" /> |
| 10 | + <el-button type="default" :icon="Aim" circle size="small" title="适应画布" @click="handleFit" /> |
| 11 | + </div> |
| 12 | + </div> |
| 13 | +</template> |
| 14 | + |
| 15 | +<script setup> |
| 16 | +import { ref, onMounted, onBeforeUnmount, nextTick } from 'vue' |
| 17 | +import { ElMessage, ElButton } from 'element-plus' |
| 18 | +import { Close, ZoomIn, ZoomOut, Aim } from '@element-plus/icons-vue' |
| 19 | +import MindMap from 'simple-mind-map' |
| 20 | +
|
| 21 | +const props = defineProps({ |
| 22 | + content: { type: String, required: true } |
| 23 | +}) |
| 24 | +
|
| 25 | +const emit = defineEmits(['close']) |
| 26 | +
|
| 27 | +const containerRef = ref(null) |
| 28 | +const mindMapInstance = ref(null) |
| 29 | +
|
| 30 | +const convertToMindMapFormat = (jsonData) => { |
| 31 | + try { |
| 32 | + const data = typeof jsonData === 'object' ? jsonData : JSON.parse(jsonData) |
| 33 | + return data.data && (data.data.text || data.data.title) |
| 34 | + ? data |
| 35 | + : { data: { text: data.text || data.title || "思维导图" }, children: data.children || [] } |
| 36 | + } catch { |
| 37 | + return { data: { text: "解析失败的思维导图" }, children: [] } |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +const adjustView = () => { |
| 42 | + const mm = mindMapInstance.value |
| 43 | + if (!mm) return |
| 44 | + mm.command?.executeCommand('fit') |
| 45 | + requestAnimationFrame(() => { |
| 46 | + if (mm.view?.translateToCenter) { |
| 47 | + mm.view.translateToCenter() |
| 48 | + } else { |
| 49 | + mm.command?.executeCommand('TO_CENTER') |
| 50 | + } |
| 51 | + }) |
| 52 | +} |
| 53 | +
|
| 54 | +const initMindMap = async () => { |
| 55 | + try { |
| 56 | + if (!containerRef.value) return |
| 57 | + if (mindMapInstance.value) mindMapInstance.value.destroy() |
| 58 | + await nextTick() |
| 59 | +
|
| 60 | + const container = containerRef.value |
| 61 | + const mindMapData = convertToMindMapFormat(props.content) |
| 62 | +
|
| 63 | + mindMapInstance.value = new MindMap({ |
| 64 | + el: container, |
| 65 | + data: mindMapData, |
| 66 | + theme: 'primary', |
| 67 | + layout: 'logicalStructure', |
| 68 | + enableNodeDragging: true, |
| 69 | + height: window.innerHeight, |
| 70 | + width: window.innerWidth, |
| 71 | + keypress: true, |
| 72 | + contextMenu: false, |
| 73 | + fit: true, |
| 74 | + scale: 0.9, |
| 75 | + textAutoWrap: true, |
| 76 | + nodeTextEdit: false, |
| 77 | + mousewheelAction: 'zoom', |
| 78 | + enableMouseWheel: true |
| 79 | + }) |
| 80 | +
|
| 81 | + mindMapInstance.value.render() |
| 82 | + setTimeout(() => adjustView(), 120) |
| 83 | + } catch { |
| 84 | + ElMessage.error('思维导图初始化失败') |
| 85 | + } |
| 86 | +} |
| 87 | +
|
| 88 | +const handleZoomIn = () => { |
| 89 | + mindMapInstance.value?.view?.enlarge() |
| 90 | +} |
| 91 | +
|
| 92 | +const handleZoomOut = () => { |
| 93 | + mindMapInstance.value?.view?.narrow() |
| 94 | +} |
| 95 | +
|
| 96 | +const handleFit = () => { |
| 97 | + adjustView() |
| 98 | +} |
| 99 | +
|
| 100 | +const handleClose = () => { |
| 101 | + emit('close') |
| 102 | +} |
| 103 | +
|
| 104 | +let resizeRaf = 0 |
| 105 | +const handleResize = () => { |
| 106 | + cancelAnimationFrame(resizeRaf) |
| 107 | + resizeRaf = requestAnimationFrame(() => { |
| 108 | + if (mindMapInstance.value) { |
| 109 | + mindMapInstance.value.resize() |
| 110 | + adjustView() |
| 111 | + } |
| 112 | + }) |
| 113 | +} |
| 114 | +
|
| 115 | +const handleKeydown = (e) => { |
| 116 | + if (e.key === 'Escape') { |
| 117 | + handleClose() |
| 118 | + } |
| 119 | +} |
| 120 | +
|
| 121 | +onMounted(() => { |
| 122 | + initMindMap() |
| 123 | + window.addEventListener('keydown', handleKeydown) |
| 124 | + window.addEventListener('resize', handleResize) |
| 125 | +}) |
| 126 | +
|
| 127 | +onBeforeUnmount(() => { |
| 128 | + mindMapInstance.value?.destroy() |
| 129 | + window.removeEventListener('keydown', handleKeydown) |
| 130 | + window.removeEventListener('resize', handleResize) |
| 131 | + cancelAnimationFrame(resizeRaf) |
| 132 | +}) |
| 133 | +</script> |
| 134 | +
|
| 135 | +<style scoped> |
| 136 | +.mindmap-fullscreen { |
| 137 | + position: fixed; |
| 138 | + top: 0; |
| 139 | + left: 0; |
| 140 | + right: 0; |
| 141 | + bottom: 0; |
| 142 | + width: 100vw; |
| 143 | + height: 100vh; |
| 144 | + background: #fff; |
| 145 | + z-index: 9999; |
| 146 | +} |
| 147 | +
|
| 148 | +#mindMapContainerFull * { |
| 149 | + margin: 0; |
| 150 | + padding: 0; |
| 151 | +} |
| 152 | +
|
| 153 | +.mind-map-container-full { |
| 154 | + width: 100%; |
| 155 | + height: 100%; |
| 156 | + background: #fff; |
| 157 | +} |
| 158 | +
|
| 159 | +.fullscreen-toolbar { |
| 160 | + position: fixed; |
| 161 | + top: 20px; |
| 162 | + right: 20px; |
| 163 | + z-index: 10000; |
| 164 | + display: flex; |
| 165 | + align-items: center; |
| 166 | + gap: 8px; |
| 167 | + background: rgba(255, 255, 255, 0.95); |
| 168 | + padding: 8px 12px; |
| 169 | + border-radius: 8px; |
| 170 | + box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); |
| 171 | +} |
| 172 | +
|
| 173 | +.toolbar-divider { |
| 174 | + width: 1px; |
| 175 | + height: 24px; |
| 176 | + background: #e5e7eb; |
| 177 | + margin: 0 4px; |
| 178 | +} |
| 179 | +</style> |
0 commit comments