Trae Assistant commited on
Commit
5b437e6
·
0 Parent(s):

Enhance robustness and functionality

Browse files
Files changed (5) hide show
  1. Dockerfile +13 -0
  2. README.md +42 -0
  3. app.py +16 -0
  4. requirements.txt +2 -0
  5. templates/index.html +602 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ # Hugging Face Spaces 默认端口 7860
11
+ EXPOSE 7860
12
+
13
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "app:app"]
README.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: OKR Map Studio
3
+ emoji: 🎯
4
+ colorFrom: indigo
5
+ colorTo: blue
6
+ sdk: docker
7
+ pinned: false
8
+ short_description: 可视化 OKR 目标地图生成器
9
+ ---
10
+
11
+ # OKR 目标地图工作室 (OKR Map Studio)
12
+
13
+ 一个用于可视化管理 OKR (Objectives and Key Results) 的工具。
14
+
15
+ ## 功能特点
16
+
17
+ - 🌲 **可视化树状图**: 清晰展示公司、部门、个人的目标层级关系。
18
+ - 📊 **进度追踪**: 实时调整和查看每个节点的进度百分比。
19
+ - 🎨 **导出图片**: 一键生成高质量 PNG 图片,方便分享和汇报。
20
+ - 💾 **本地存储**: 数据保存在浏览器本地,隐私安全,随时继续工作。
21
+ - 🔄 **JSON 导入导出**: 方便备份和迁移数据。
22
+
23
+ ## 使用方法
24
+
25
+ 1. **编辑节点**: 点击任意卡片,修改标题、负责人、类型和进度。
26
+ 2. **添加子节点**: 鼠标悬停在卡片上,点击右上角的 "+" 按钮。
27
+ 3. **删除节点**: 鼠标悬停在卡片上,点击右上角的垃圾桶按钮(根节点不可删除)。
28
+ 4. **缩放移动**: 按住鼠标左键拖拽画布,使用右下角按钮缩放。
29
+
30
+ ## 技术栈
31
+
32
+ - Python / Flask
33
+ - Vue.js 3
34
+ - Tailwind CSS
35
+ - html2canvas
36
+
37
+ ## 运行
38
+
39
+ ```bash
40
+ pip install -r requirements.txt
41
+ python app.py
42
+ ```
app.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, send_from_directory
2
+ import os
3
+
4
+ app = Flask(__name__)
5
+
6
+ @app.route('/')
7
+ def index():
8
+ return render_template('index.html')
9
+
10
+ @app.route('/static/<path:path>')
11
+ def send_static(path):
12
+ return send_from_directory('static', path)
13
+
14
+ if __name__ == '__main__':
15
+ port = int(os.environ.get('PORT', 7860))
16
+ app.run(host='0.0.0.0', port=port, debug=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Flask
2
+ gunicorn
templates/index.html ADDED
@@ -0,0 +1,602 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>OKR 目标地图工作室</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
10
+ <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
11
+ <style>
12
+ /* Custom Tree CSS */
13
+ .tree ul {
14
+ padding-top: 20px;
15
+ position: relative;
16
+ transition: all 0.5s;
17
+ -webkit-transition: all 0.5s;
18
+ -moz-transition: all 0.5s;
19
+ display: flex;
20
+ justify-content: center;
21
+ }
22
+
23
+ .tree li {
24
+ text-align: center;
25
+ list-style-type: none;
26
+ position: relative;
27
+ padding: 20px 10px 0 10px;
28
+ transition: all 0.5s;
29
+ -webkit-transition: all 0.5s;
30
+ -moz-transition: all 0.5s;
31
+ }
32
+
33
+ /* We will use ::before and ::after to draw the connectors */
34
+ .tree li::before, .tree li::after{
35
+ content: '';
36
+ position: absolute;
37
+ top: 0;
38
+ right: 50%;
39
+ border-top: 2px solid #ccc;
40
+ width: 50%;
41
+ height: 20px;
42
+ }
43
+ .tree li::after{
44
+ right: auto;
45
+ left: 50%;
46
+ border-left: 2px solid #ccc;
47
+ }
48
+
49
+ /* We need to remove left-right connectors from elements without
50
+ any siblings */
51
+ .tree li:only-child::after, .tree li:only-child::before {
52
+ display: none;
53
+ }
54
+
55
+ /* Remove space from the top of single children */
56
+ .tree li:only-child{
57
+ padding-top: 0;
58
+ }
59
+
60
+ /* Remove left connector from first child and
61
+ right connector from last child */
62
+ .tree li:first-child::before, .tree li:last-child::after{
63
+ border: 0 none;
64
+ }
65
+ /* Adding back the vertical connector to the last nodes */
66
+ .tree li:last-child::before{
67
+ border-right: 2px solid #ccc;
68
+ border-radius: 0 5px 0 0;
69
+ -webkit-border-radius: 0 5px 0 0;
70
+ -moz-border-radius: 0 5px 0 0;
71
+ }
72
+ .tree li:first-child::after{
73
+ border-radius: 5px 0 0 0;
74
+ -webkit-border-radius: 5px 0 0 0;
75
+ -moz-border-radius: 5px 0 0 0;
76
+ }
77
+
78
+ /* Time to add downward connectors from parents */
79
+ .tree ul ul::before{
80
+ content: '';
81
+ position: absolute;
82
+ top: 0;
83
+ left: 50%;
84
+ border-left: 2px solid #ccc;
85
+ width: 0;
86
+ height: 20px;
87
+ }
88
+
89
+ .node-card {
90
+ display: inline-block;
91
+ padding: 10px;
92
+ text-decoration: none;
93
+ color: #666;
94
+ font-family: arial, verdana, tahoma;
95
+ font-size: 14px;
96
+ border-radius: 5px;
97
+ transition: all 0.5s;
98
+ -webkit-transition: all 0.5s;
99
+ -moz-transition: all 0.5s;
100
+ background: white;
101
+ min-width: 200px;
102
+ max-width: 260px;
103
+ position: relative;
104
+ z-index: 10;
105
+ }
106
+
107
+ /* Hover effects */
108
+ .node-card:hover, .node-card:hover+ul li .node-card {
109
+ /* background: #c8e4f8; color: #000; border: 1px solid #94a0b4; */
110
+ transform: scale(1.02);
111
+ box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
112
+ }
113
+
114
+ /* Connector styles on hover */
115
+ /* .tree li a:hover+ul li::after,
116
+ .tree li a:hover+ul li::before,
117
+ .tree li a:hover+ul::before,
118
+ .tree li a:hover+ul ul::before{
119
+ border-color: #94a0b4;
120
+ } */
121
+
122
+ [v-cloak] { display: none; }
123
+
124
+ .bg-pattern {
125
+ background-color: #f3f4f6;
126
+ background-image: radial-gradient(#e5e7eb 1px, transparent 1px);
127
+ background-size: 20px 20px;
128
+ }
129
+ </style>
130
+ </head>
131
+ <body class="bg-pattern h-screen flex flex-col overflow-hidden text-gray-800">
132
+
133
+ <div id="app" v-cloak class="flex flex-col h-full">
134
+ <!-- Navbar -->
135
+ <header class="bg-white border-b border-gray-200 px-6 py-4 flex justify-between items-center shadow-sm z-20">
136
+ <div class="flex items-center space-x-3">
137
+ <div class="w-10 h-10 bg-indigo-600 rounded-lg flex items-center justify-center text-white text-xl font-bold shadow-lg">
138
+ <i class="fas fa-sitemap"></i>
139
+ </div>
140
+ <div>
141
+ <h1 class="text-xl font-bold text-gray-900">OKR 目标地图工作室</h1>
142
+ <p class="text-xs text-gray-500">可视化战略与关键结果管���</p>
143
+ </div>
144
+ </div>
145
+ <div class="flex space-x-3">
146
+ <button @click="resetData" class="px-4 py-2 text-sm text-gray-600 hover:text-gray-900 bg-gray-100 hover:bg-gray-200 rounded-md transition">
147
+ <i class="fas fa-undo mr-2"></i>重置
148
+ </button>
149
+ <button @click="importData" class="px-4 py-2 text-sm text-indigo-600 hover:text-indigo-700 bg-indigo-50 hover:bg-indigo-100 rounded-md transition">
150
+ <i class="fas fa-file-import mr-2"></i>导入 JSON
151
+ </button>
152
+ <button @click="exportJSON" class="px-4 py-2 text-sm text-indigo-600 hover:text-indigo-700 bg-indigo-50 hover:bg-indigo-100 rounded-md transition">
153
+ <i class="fas fa-file-code mr-2"></i>导出 JSON
154
+ </button>
155
+ <button @click="exportImage" class="px-4 py-2 text-sm text-white bg-indigo-600 hover:bg-indigo-700 rounded-md shadow-md transition flex items-center">
156
+ <i class="fas fa-camera mr-2"></i>导出图片
157
+ </button>
158
+ </div>
159
+ </header>
160
+
161
+ <!-- Main Content -->
162
+ <main class="flex-1 overflow-auto relative cursor-grab active:cursor-grabbing p-10" id="canvas-container" @mousedown="startDrag" @mousemove="onDrag" @mouseup="stopDrag" @mouseleave="stopDrag" ref="container">
163
+ <div class="tree transform origin-top-center transition-transform duration-200" :style="{ transform: `scale(${scale}) translate(${translateX}px, ${translateY}px)` }" id="okr-tree">
164
+ <ul>
165
+ <tree-item :model="treeData" @add-child="addChild" @delete-node="deleteNode" @update-node="updateNode"></tree-item>
166
+ </ul>
167
+ </div>
168
+
169
+ <!-- Zoom Controls -->
170
+ <div class="absolute bottom-6 right-6 bg-white rounded-lg shadow-lg flex flex-col overflow-hidden border border-gray-200">
171
+ <button @click="zoomIn" class="p-2 hover:bg-gray-100 text-gray-600 border-b border-gray-100"><i class="fas fa-plus"></i></button>
172
+ <button @click="resetZoom" class="p-2 hover:bg-gray-100 text-gray-600 text-xs font-mono">{{ Math.round(scale * 100) }}%</button>
173
+ <button @click="zoomOut" class="p-2 hover:bg-gray-100 text-gray-600 border-t border-gray-100"><i class="fas fa-minus"></i></button>
174
+ </div>
175
+ </main>
176
+
177
+ <!-- Edit Modal -->
178
+ <div v-if="editingNode" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
179
+ <div class="bg-white rounded-xl shadow-2xl w-96 p-6 animate-fade-in-up">
180
+ <h3 class="text-lg font-bold mb-4 text-gray-900 border-b pb-2">编辑节点</h3>
181
+
182
+ <div class="space-y-4">
183
+ <div>
184
+ <label class="block text-sm font-medium text-gray-700 mb-1">标题 / 目标</label>
185
+ <input v-model="editingNode.title" type="text" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition" placeholder="例如:Q1 业绩翻倍">
186
+ </div>
187
+
188
+ <div>
189
+ <label class="block text-sm font-medium text-gray-700 mb-1">负责人</label>
190
+ <input v-model="editingNode.owner" type="text" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition" placeholder="例如:产品部">
191
+ </div>
192
+
193
+ <div>
194
+ <label class="block text-sm font-medium text-gray-700 mb-1">类型</label>
195
+ <select v-model="editingNode.type" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 outline-none transition">
196
+ <option value="company">公司级目标 (O)</option>
197
+ <option value="objective">部门级目标 (O)</option>
198
+ <option value="key_result">关键结果 (KR)</option>
199
+ <option value="task">具体任务 (Task)</option>
200
+ </select>
201
+ </div>
202
+
203
+ <div>
204
+ <label class="block text-sm font-medium text-gray-700 mb-1">进度: {{ editingNode.progress }}%</label>
205
+ <input v-model.number="editingNode.progress" type="range" min="0" max="100" class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-indigo-600">
206
+ </div>
207
+ </div>
208
+
209
+ <div class="mt-6 flex justify-end space-x-3">
210
+ <button @click="closeEdit" class="px-4 py-2 text-gray-600 hover:bg-gray-100 rounded-md transition">取消</button>
211
+ <button @click="saveEdit" class="px-4 py-2 bg-indigo-600 text-white hover:bg-indigo-700 rounded-md shadow-md transition">保存</button>
212
+ </div>
213
+ </div>
214
+ </div>
215
+
216
+ <!-- Hidden File Input for Import -->
217
+ <input type="file" ref="fileInput" @change="handleFileChange" class="hidden" accept=".json">
218
+ </div>
219
+
220
+ <!-- Recursive Component Template -->
221
+ <script type="text/x-template" id="item-template">
222
+ <li>
223
+ <div class="node-card border border-gray-200 shadow-sm hover:shadow-md transition-shadow duration-300"
224
+ :class="{
225
+ 'border-l-4 border-l-indigo-500': model.type === 'company',
226
+ 'border-l-4 border-l-blue-500': model.type === 'objective',
227
+ 'border-l-4 border-l-green-500': model.type === 'key_result',
228
+ 'border-l-4 border-l-gray-400': model.type === 'task'
229
+ }"
230
+ @click="$emit('update-node', model)">
231
+
232
+ <!-- Header -->
233
+ <div class="flex justify-between items-start mb-2">
234
+ <span class="text-xs font-bold uppercase tracking-wider px-2 py-0.5 rounded-full"
235
+ :class="{
236
+ 'bg-indigo-100 text-indigo-800': model.type === 'company',
237
+ 'bg-blue-100 text-blue-800': model.type === 'objective',
238
+ 'bg-green-100 text-green-800': model.type === 'key_result',
239
+ 'bg-gray-100 text-gray-800': model.type === 'task'
240
+ }">
241
+ {{ getTypeLabel(model.type) }}
242
+ </span>
243
+ <div class="flex space-x-1 opacity-0 hover:opacity-100 transition-opacity absolute top-2 right-2 group-hover:opacity-100">
244
+ <button @click.stop="$emit('add-child', model)" class="w-6 h-6 rounded-full bg-indigo-50 text-indigo-600 hover:bg-indigo-100 flex items-center justify-center" title="添加子节点">
245
+ <i class="fas fa-plus text-xs"></i>
246
+ </button>
247
+ <button v-if="!isRoot" @click.stop="$emit('delete-node', model.id)" class="w-6 h-6 rounded-full bg-red-50 text-red-600 hover:bg-red-100 flex items-center justify-center" title="删除节点">
248
+ <i class="fas fa-trash text-xs"></i>
249
+ </button>
250
+ </div>
251
+ </div>
252
+
253
+ <!-- Content -->
254
+ <h3 class="font-bold text-gray-800 mb-1 leading-tight text-left">{{ model.title }}</h3>
255
+ <div class="flex items-center text-xs text-gray-500 mb-2">
256
+ <i class="fas fa-user-circle mr-1"></i>
257
+ <span>{{ model.owner || '未分配' }}</span>
258
+ </div>
259
+
260
+ <!-- Progress -->
261
+ <div class="w-full bg-gray-100 rounded-full h-1.5 mb-1 overflow-hidden">
262
+ <div class="bg-indigo-600 h-1.5 rounded-full" :style="{ width: model.progress + '%' }"></div>
263
+ </div>
264
+ <div class="text-right text-xs font-mono text-gray-400">{{ model.progress }}%</div>
265
+ </div>
266
+
267
+ <ul v-if="model.children && model.children.length">
268
+ <tree-item
269
+ v-for="child in model.children"
270
+ :key="child.id"
271
+ :model="child"
272
+ :is-root="false"
273
+ @add-child="$emit('add-child', $event)"
274
+ @delete-node="$emit('delete-node', $event)"
275
+ @update-node="$emit('update-node', $event)">
276
+ </tree-item>
277
+ </ul>
278
+ </li>
279
+ </script>
280
+
281
+ <script>
282
+ const { createApp, ref, reactive, onMounted, computed } = Vue;
283
+
284
+ // Component definition
285
+ const TreeItem = {
286
+ name: 'TreeItem',
287
+ template: '#item-template',
288
+ props: {
289
+ model: Object,
290
+ isRoot: {
291
+ type: Boolean,
292
+ default: false
293
+ }
294
+ },
295
+ emits: ['add-child', 'delete-node', 'update-node'],
296
+ setup(props) {
297
+ const getTypeLabel = (type) => {
298
+ const map = {
299
+ 'company': '公司',
300
+ 'objective': '目标',
301
+ 'key_result': '关键结果',
302
+ 'task': '任务'
303
+ };
304
+ return map[type] || '节点';
305
+ };
306
+ return { getTypeLabel };
307
+ }
308
+ };
309
+
310
+ createApp({
311
+ components: {
312
+ TreeItem
313
+ },
314
+ delimiters: ['${', '}'],
315
+ setup() {
316
+ const defaultData = {
317
+ id: Date.now(),
318
+ title: '2026年度战略目标',
319
+ type: 'company',
320
+ progress: 30,
321
+ owner: 'CEO',
322
+ children: [
323
+ {
324
+ id: Date.now() + 1,
325
+ title: '市场占有率达到 25%',
326
+ type: 'objective',
327
+ progress: 45,
328
+ owner: '市场部',
329
+ children: [
330
+ {
331
+ id: Date.now() + 2,
332
+ title: 'Q1 新增用户 10万',
333
+ type: 'key_result',
334
+ progress: 80,
335
+ owner: '增长组',
336
+ children: []
337
+ },
338
+ {
339
+ id: Date.now() + 3,
340
+ title: '品牌曝光量提升 50%',
341
+ type: 'key_result',
342
+ progress: 20,
343
+ owner: '品牌组',
344
+ children: []
345
+ }
346
+ ]
347
+ },
348
+ {
349
+ id: Date.now() + 4,
350
+ title: '产品研发效率提升',
351
+ type: 'objective',
352
+ progress: 15,
353
+ owner: '产研中心',
354
+ children: [
355
+ {
356
+ id: Date.now() + 5,
357
+ title: '上线自动化测试平台',
358
+ type: 'key_result',
359
+ progress: 10,
360
+ owner: '测试组',
361
+ children: []
362
+ }
363
+ ]
364
+ }
365
+ ]
366
+ };
367
+
368
+ const treeData = ref(JSON.parse(JSON.stringify(defaultData)));
369
+
370
+ // Load from localStorage
371
+ onMounted(() => {
372
+ const saved = localStorage.getItem('okr-map-data');
373
+ if (saved) {
374
+ try {
375
+ treeData.value = JSON.parse(saved);
376
+ } catch(e) {
377
+ console.error('Failed to load data', e);
378
+ }
379
+ }
380
+ });
381
+
382
+ // Auto save
383
+ const saveData = () => {
384
+ localStorage.setItem('okr-map-data', JSON.stringify(treeData.value));
385
+ };
386
+
387
+ // Editing Logic
388
+ const editingNode = ref(null);
389
+
390
+ const updateNode = (node) => {
391
+ // Create a copy to edit
392
+ editingNode.value = { ...node, _original: node };
393
+ };
394
+
395
+ const saveEdit = () => {
396
+ if (!editingNode.value) return;
397
+ const original = editingNode.value._original;
398
+ original.title = editingNode.value.title;
399
+ original.owner = editingNode.value.owner;
400
+ original.progress = editingNode.value.progress;
401
+ original.type = editingNode.value.type;
402
+
403
+ editingNode.value = null;
404
+ saveData();
405
+ };
406
+
407
+ const closeEdit = () => {
408
+ editingNode.value = null;
409
+ };
410
+
411
+ // Tree Manipulation
412
+ const addChild = (parentNode) => {
413
+ if (!parentNode.children) parentNode.children = [];
414
+ parentNode.children.push({
415
+ id: Date.now(),
416
+ title: '新节点',
417
+ type: 'key_result',
418
+ progress: 0,
419
+ owner: '未分配',
420
+ children: []
421
+ });
422
+ saveData();
423
+ };
424
+
425
+ const deleteNode = (nodeId) => {
426
+ if (confirm('确定要删除这个节点及其子节点吗?')) {
427
+ const remove = (nodes, id) => {
428
+ // Cannot remove root directly this way, but UI hides delete button for root
429
+ if (!nodes) return false;
430
+ const idx = nodes.findIndex(n => n.id === id);
431
+ if (idx !== -1) {
432
+ nodes.splice(idx, 1);
433
+ return true;
434
+ }
435
+ for (const node of nodes) {
436
+ if (node.children && remove(node.children, id)) return true;
437
+ }
438
+ return false;
439
+ };
440
+
441
+ // Handle root children check
442
+ if (treeData.value.children) {
443
+ remove(treeData.value.children, nodeId);
444
+ }
445
+ saveData();
446
+ }
447
+ };
448
+
449
+ const resetData = () => {
450
+ if (confirm('确定重置所有数据到默认状态吗?')) {
451
+ treeData.value = JSON.parse(JSON.stringify(defaultData));
452
+ saveData();
453
+ }
454
+ };
455
+
456
+ // Zoom & Pan
457
+ const scale = ref(1);
458
+ const translateX = ref(0);
459
+ const translateY = ref(0);
460
+ const container = ref(null);
461
+ let isDragging = false;
462
+ let startX = 0;
463
+ let startY = 0;
464
+
465
+ const zoomIn = () => scale.value = Math.min(scale.value + 0.1, 2);
466
+ const zoomOut = () => scale.value = Math.max(scale.value - 0.1, 0.5);
467
+ const resetZoom = () => { scale.value = 1; translateX.value = 0; translateY.value = 0; };
468
+
469
+ const startDrag = (e) => {
470
+ if (e.target.closest('.node-card')) return; // Don't drag if clicking card
471
+ isDragging = true;
472
+ startX = e.clientX - translateX.value;
473
+ startY = e.clientY - translateY.value;
474
+ container.value.style.cursor = 'grabbing';
475
+ };
476
+
477
+ const onDrag = (e) => {
478
+ if (!isDragging) return;
479
+ e.preventDefault();
480
+ translateX.value = e.clientX - startX;
481
+ translateY.value = e.clientY - startY;
482
+ };
483
+
484
+ const stopDrag = () => {
485
+ isDragging = false;
486
+ if(container.value) container.value.style.cursor = 'grab';
487
+ };
488
+
489
+ // Export/Import
490
+ const exportJSON = () => {
491
+ const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(treeData.value, null, 2));
492
+ const downloadAnchorNode = document.createElement('a');
493
+ downloadAnchorNode.setAttribute("href", dataStr);
494
+ downloadAnchorNode.setAttribute("download", "okr_map.json");
495
+ document.body.appendChild(downloadAnchorNode);
496
+ downloadAnchorNode.click();
497
+ downloadAnchorNode.remove();
498
+ };
499
+
500
+ const fileInput = ref(null);
501
+ const importData = () => {
502
+ fileInput.value.click();
503
+ };
504
+
505
+ const handleFileChange = (e) => {
506
+ const file = e.target.files[0];
507
+ if (!file) return;
508
+
509
+ // 1. File Size Check (5MB limit)
510
+ const maxSize = 5 * 1024 * 1024;
511
+ if (file.size > maxSize) {
512
+ alert('文件过大,请上传小于 5MB 的文件');
513
+ e.target.value = '';
514
+ return;
515
+ }
516
+
517
+ const reader = new FileReader();
518
+ reader.onload = (event) => {
519
+ const content = event.target.result;
520
+
521
+ // 2. Binary Check (Null byte check)
522
+ if (content.indexOf('\0') !== -1) {
523
+ alert('检测到二进制内容,请上传有效的 JSON 文本文件');
524
+ e.target.value = '';
525
+ return;
526
+ }
527
+
528
+ try {
529
+ const json = JSON.parse(content);
530
+ // Relaxed check: id OR children (in case root doesn't have ID for some reason, though it should)
531
+ if (json && (json.id || json.children)) {
532
+ treeData.value = json;
533
+ saveData();
534
+ alert('导入成功');
535
+ } else {
536
+ alert('文件格式不正确:缺少必要的节点数据');
537
+ }
538
+ } catch (err) {
539
+ console.error(err);
540
+ alert('JSON 解析失败,请检查文件内容是否正确');
541
+ }
542
+ };
543
+
544
+ reader.onerror = () => {
545
+ alert('读取文件出错');
546
+ };
547
+
548
+ reader.readAsText(file);
549
+ e.target.value = ''; // Reset input
550
+ };
551
+
552
+ const exportImage = () => {
553
+ const element = document.getElementById('okr-tree');
554
+ // Save current transform
555
+ const originalTransform = element.style.transform;
556
+ // Reset transform for clean capture
557
+ element.style.transform = 'scale(1) translate(0,0)';
558
+ // element.style.width = 'fit-content';
559
+
560
+ html2canvas(element, {
561
+ backgroundColor: null, // Transparent or white
562
+ scale: 2 // High res
563
+ }).then(canvas => {
564
+ const link = document.createElement('a');
565
+ link.download = 'okr-map.png';
566
+ link.href = canvas.toDataURL();
567
+ link.click();
568
+ // Restore
569
+ element.style.transform = originalTransform;
570
+ });
571
+ };
572
+
573
+ return {
574
+ treeData,
575
+ editingNode,
576
+ updateNode,
577
+ saveEdit,
578
+ closeEdit,
579
+ addChild,
580
+ deleteNode,
581
+ resetData,
582
+ scale,
583
+ translateX,
584
+ translateY,
585
+ container,
586
+ zoomIn,
587
+ zoomOut,
588
+ resetZoom,
589
+ startDrag,
590
+ onDrag,
591
+ stopDrag,
592
+ exportJSON,
593
+ importData,
594
+ fileInput,
595
+ handleFileChange,
596
+ exportImage
597
+ };
598
+ }
599
+ }).mount('#app');
600
+ </script>
601
+ </body>
602
+ </html>