openprototype 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +58 -0
- package/CONVENTIONS.md +54 -0
- package/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/cli.js +297 -0
- package/lib/config.js +162 -0
- package/package.json +32 -10
- package/product/demo/pc/customer/customer_form.html +134 -0
- package/product/demo/pc/customer/customer_form.md +33 -0
- package/product/demo/pc/customer/customer_list.html +129 -0
- package/product/demo/pc/customer/customer_list.md +35 -0
- package/product/demo/pc/demo-data.js +23 -0
- package/product/demo/pc/index.html +1069 -0
- package/product/demo/pc/nav-tree.json +15 -0
- package/proto-kit.config.json +13 -0
- package/runtime/server.js +664 -0
- package/runtime/shared/ai-polish.js +159 -0
- package/runtime/shared/base-manager.js +168 -0
- package/runtime/shared/common/component-renderer.js +917 -0
- package/runtime/shared/common/copy-manager.js +134 -0
- package/runtime/shared/common/data-bridge.js +45 -0
- package/runtime/shared/common/form-renderer.js +211 -0
- package/runtime/shared/common/form.js +357 -0
- package/runtime/shared/common/formatter.js +78 -0
- package/runtime/shared/common/index.js +37 -0
- package/runtime/shared/common/line-chart.js +357 -0
- package/runtime/shared/common/modal-manager.js +82 -0
- package/runtime/shared/common/prd-panel.js +1014 -0
- package/runtime/shared/common/prd-popup.js +168 -0
- package/runtime/shared/common/search-renderer.js +95 -0
- package/runtime/shared/common/table-renderer.js +177 -0
- package/runtime/shared/common/table.js +160 -0
- package/runtime/shared/common/toast.js +99 -0
- package/runtime/shared/common/ui-preference-manager.js +74 -0
- package/runtime/shared/common/view-mode.js +30 -0
- package/runtime/shared/common.js +43 -0
- package/runtime/shared/constants/icons.js +28 -0
- package/runtime/shared/constants/status.js +67 -0
- package/runtime/shared/libs/marked.min.js +69 -0
- package/runtime/shared/prd-content.js +9 -0
- package/runtime/shared/styles.css +7673 -0
- package/runtime/shared/utils.js +110 -0
- package/runtime/shared/view-controls.js +148 -0
- package/runtime/shell/agent-panel.css +429 -0
- package/runtime/shell/agent-panel.js +629 -0
- package/scripts/check/run-checks.js +58 -0
- package/scripts/check/smoke-test.js +244 -0
- package/scripts/check/static-check.js +248 -0
- package/scripts/check/sync-nav-tree.js +139 -0
- package/templates/AGENTS.md +58 -0
- package/templates/CONVENTIONS.md +54 -0
- package/templates/gitignore +10 -0
- package/templates/product-shell/index.html +1069 -0
- package/templates/skills/auto-test/SKILL.md +62 -0
- package/templates/skills/xiaojia/SKILL.md +299 -0
- package/cli.js +0 -4
|
@@ -0,0 +1,1014 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PRD 面板模块
|
|
3
|
+
* 管理 PRD 文档的加载、渲染和交互
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
var prdPanelInitialized = false;
|
|
7
|
+
var prdPanelWidth = null;
|
|
8
|
+
var PRD_PANEL_DEFAULT_WIDTH = 680;
|
|
9
|
+
var PRD_PANEL_MIN_WIDTH = 360;
|
|
10
|
+
var PRD_PANEL_MAX_RATIO = 0.86;
|
|
11
|
+
|
|
12
|
+
function getPrdPanelMaxWidth() {
|
|
13
|
+
return Math.floor(window.innerWidth * PRD_PANEL_MAX_RATIO);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function clampPrdPanelWidth(width) {
|
|
17
|
+
width = parseInt(width, 10);
|
|
18
|
+
if (!isFinite(width)) {
|
|
19
|
+
width = PRD_PANEL_DEFAULT_WIDTH;
|
|
20
|
+
}
|
|
21
|
+
return Math.max(PRD_PANEL_MIN_WIDTH, Math.min(getPrdPanelMaxWidth(), width));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function applyPrdPanelWidth(prdPanel, width) {
|
|
25
|
+
prdPanelWidth = clampPrdPanelWidth(width);
|
|
26
|
+
prdPanel.style.width = prdPanelWidth + 'px';
|
|
27
|
+
prdPanel.style.minWidth = prdPanelWidth + 'px';
|
|
28
|
+
prdPanel.style.maxWidth = 'none';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function ensurePrdPanelStructure(prdPanel) {
|
|
32
|
+
if (!prdPanel) return null;
|
|
33
|
+
|
|
34
|
+
prdPanel.id = 'prdPanel';
|
|
35
|
+
if (!prdPanel.classList.contains('prd-slide-panel')) {
|
|
36
|
+
prdPanel.classList.add('prd-slide-panel');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!prdPanel.querySelector('.prd-panel-resizer') ||
|
|
40
|
+
!prdPanel.querySelector('#prdPanelTitle') ||
|
|
41
|
+
!prdPanel.querySelector('#prdPanelFileName') ||
|
|
42
|
+
!prdPanel.querySelector('#prdPanelCopyBtn') ||
|
|
43
|
+
!prdPanel.querySelector('#prdPanelEditBtn') ||
|
|
44
|
+
!prdPanel.querySelector('#prdPanelCloseBtn') ||
|
|
45
|
+
!prdPanel.querySelector('#prdPanelTabs') ||
|
|
46
|
+
!prdPanel.querySelector('#prdPanelContent') ||
|
|
47
|
+
!prdPanel.querySelector('#prdPanelEditorWrap')) {
|
|
48
|
+
prdPanel.innerHTML = [
|
|
49
|
+
'<div class="prd-panel-resizer"></div>',
|
|
50
|
+
'<div class="prd-panel-header">',
|
|
51
|
+
' <div class="prd-panel-header-left">',
|
|
52
|
+
' <h3 id="prdPanelTitle">PRD文档</h3>',
|
|
53
|
+
' <span class="prd-panel-file-name" id="prdPanelFileName"></span>',
|
|
54
|
+
' <button class="prd-panel-copy-btn" id="prdPanelCopyBtn" title="复制 MD 原文">',
|
|
55
|
+
' <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">',
|
|
56
|
+
' <rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>',
|
|
57
|
+
' <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>',
|
|
58
|
+
' </svg>',
|
|
59
|
+
' </button>',
|
|
60
|
+
' <button class="prd-panel-edit-btn" id="prdPanelEditBtn" title="编辑 PRD">✎ 编辑</button>',
|
|
61
|
+
' <span class="prd-panel-toast" id="prdPanelToast"></span>',
|
|
62
|
+
' </div>',
|
|
63
|
+
' <button class="prd-panel-close" id="prdPanelCloseBtn">×</button>',
|
|
64
|
+
'</div>',
|
|
65
|
+
'<div class="prd-panel-tabs" id="prdPanelTabs"></div>',
|
|
66
|
+
'<div class="prd-panel-content" id="prdPanelContent"></div>',
|
|
67
|
+
'<div class="prd-panel-editor-wrap" id="prdPanelEditorWrap" style="display:none;">',
|
|
68
|
+
' <div class="prd-panel-editor-toolbar">',
|
|
69
|
+
' <button type="button" class="prd-panel-editor-btn" id="prdPanelRedBtn">标红</button>',
|
|
70
|
+
' <button type="button" class="prd-panel-editor-btn" id="prdPanelUnredBtn">取消标红</button>',
|
|
71
|
+
' <button type="button" class="prd-panel-editor-btn" id="prdPanelUnredAllBtn">全部取消标红</button>',
|
|
72
|
+
' <span class="prd-panel-editor-msg" id="prdPanelEditorMsg"></span>',
|
|
73
|
+
' <span class="prd-panel-editor-spacer"></span>',
|
|
74
|
+
' <button type="button" class="prd-panel-editor-btn prd-panel-editor-cancel" id="prdPanelCancelBtn">取消</button>',
|
|
75
|
+
' <button type="button" class="prd-panel-editor-btn prd-panel-editor-save" id="prdPanelSaveBtn">保存</button>',
|
|
76
|
+
' </div>',
|
|
77
|
+
' <textarea class="prd-panel-editor-textarea" id="prdPanelEditorTextarea" spellcheck="false"></textarea>',
|
|
78
|
+
'</div>'
|
|
79
|
+
].join('');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return prdPanel;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function ensurePrdPanelDom() {
|
|
86
|
+
if (!document.body) return null;
|
|
87
|
+
|
|
88
|
+
var prdFloatBtn = document.getElementById('prdFloatBtn');
|
|
89
|
+
if (!prdFloatBtn) {
|
|
90
|
+
prdFloatBtn = document.createElement('div');
|
|
91
|
+
prdFloatBtn.id = 'prdFloatBtn';
|
|
92
|
+
prdFloatBtn.className = 'prd-float-btn';
|
|
93
|
+
prdFloatBtn.textContent = 'PRD';
|
|
94
|
+
document.body.appendChild(prdFloatBtn);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var prdPanel = document.getElementById('prdPanel');
|
|
98
|
+
if (!prdPanel) {
|
|
99
|
+
prdPanel = document.createElement('div');
|
|
100
|
+
document.body.appendChild(prdPanel);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return ensurePrdPanelStructure(prdPanel);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function initPrdPanel() {
|
|
107
|
+
if (prdPanelInitialized) return;
|
|
108
|
+
// 弹窗页面(prd-popup.js 已接管)跳过,避免覆盖其面板结构
|
|
109
|
+
if (window.__prdPopupReady) return;
|
|
110
|
+
|
|
111
|
+
var prdPanel = ensurePrdPanelDom();
|
|
112
|
+
if (!prdPanel) return;
|
|
113
|
+
|
|
114
|
+
prdPanelInitialized = true;
|
|
115
|
+
|
|
116
|
+
var prdFloatBtn = document.getElementById('prdFloatBtn');
|
|
117
|
+
if (prdFloatBtn) {
|
|
118
|
+
prdFloatBtn.addEventListener('click', togglePrdPanel);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
var prdPanelCloseBtn = document.getElementById('prdPanelCloseBtn');
|
|
122
|
+
if (prdPanelCloseBtn) {
|
|
123
|
+
prdPanelCloseBtn.addEventListener('click', function() {
|
|
124
|
+
prdPanel.classList.remove('open');
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
var prdPanelCopyBtn = document.getElementById('prdPanelCopyBtn');
|
|
129
|
+
if (prdPanelCopyBtn) {
|
|
130
|
+
prdPanelCopyBtn.addEventListener('click', copyPrdRawContent);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var prdPanelEditBtn = document.getElementById('prdPanelEditBtn');
|
|
134
|
+
if (prdPanelEditBtn) {
|
|
135
|
+
prdPanelEditBtn.addEventListener('click', enterPrdEditMode);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
var prdPanelRedBtn = document.getElementById('prdPanelRedBtn');
|
|
139
|
+
if (prdPanelRedBtn) {
|
|
140
|
+
prdPanelRedBtn.addEventListener('click', applyPrdRedColor);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
var prdPanelUnredBtn = document.getElementById('prdPanelUnredBtn');
|
|
144
|
+
if (prdPanelUnredBtn) {
|
|
145
|
+
prdPanelUnredBtn.addEventListener('click', removePrdRedColor);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
var prdPanelUnredAllBtn = document.getElementById('prdPanelUnredAllBtn');
|
|
149
|
+
if (prdPanelUnredAllBtn) {
|
|
150
|
+
prdPanelUnredAllBtn.addEventListener('click', removeAllPrdRedColor);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
var prdPanelCancelBtn = document.getElementById('prdPanelCancelBtn');
|
|
154
|
+
if (prdPanelCancelBtn) {
|
|
155
|
+
prdPanelCancelBtn.addEventListener('click', exitPrdEditMode);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var prdPanelSaveBtn = document.getElementById('prdPanelSaveBtn');
|
|
159
|
+
if (prdPanelSaveBtn) {
|
|
160
|
+
prdPanelSaveBtn.addEventListener('click', savePrdEdits);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
var savedWidth = localStorage.getItem('prdPanelWidth');
|
|
164
|
+
if (savedWidth) {
|
|
165
|
+
applyPrdPanelWidth(prdPanel, parseInt(savedWidth, 10));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
var resizeHandle = prdPanel.querySelector('.prd-panel-resizer');
|
|
169
|
+
if (resizeHandle) {
|
|
170
|
+
var isResizing = false;
|
|
171
|
+
resizeHandle.addEventListener('mousedown', function(e) {
|
|
172
|
+
isResizing = true;
|
|
173
|
+
resizeHandle.classList.add('resizing');
|
|
174
|
+
document.body.style.cursor = 'ew-resize';
|
|
175
|
+
document.body.style.userSelect = 'none';
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
});
|
|
178
|
+
document.addEventListener('mousemove', function(e) {
|
|
179
|
+
if (!isResizing) return;
|
|
180
|
+
var newWidth = window.innerWidth - e.clientX;
|
|
181
|
+
applyPrdPanelWidth(prdPanel, newWidth);
|
|
182
|
+
});
|
|
183
|
+
document.addEventListener('mouseup', function() {
|
|
184
|
+
if (isResizing) {
|
|
185
|
+
isResizing = false;
|
|
186
|
+
resizeHandle.classList.remove('resizing');
|
|
187
|
+
document.body.style.cursor = '';
|
|
188
|
+
document.body.style.userSelect = '';
|
|
189
|
+
if (prdPanelWidth) {
|
|
190
|
+
localStorage.setItem('prdPanelWidth', prdPanelWidth);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
document.addEventListener('click', function(e) {
|
|
197
|
+
if (!prdPanel.classList.contains('open')) return;
|
|
198
|
+
if (prdEditMode) return;
|
|
199
|
+
|
|
200
|
+
var target = e.target;
|
|
201
|
+
var isInsidePanel = target.closest('#prdPanel');
|
|
202
|
+
var isFloatBtn = target.closest('.prd-float-btn');
|
|
203
|
+
|
|
204
|
+
if (!isInsidePanel && !isFloatBtn) {
|
|
205
|
+
prdPanel.classList.remove('open');
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
updatePrdContent();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function togglePrdPanel() {
|
|
213
|
+
var prdPanel = document.getElementById('prdPanel');
|
|
214
|
+
if (!prdPanel) return;
|
|
215
|
+
|
|
216
|
+
if (prdPanel.classList.contains('open')) {
|
|
217
|
+
prdPanel.classList.remove('open');
|
|
218
|
+
} else {
|
|
219
|
+
if (prdPanelWidth) {
|
|
220
|
+
applyPrdPanelWidth(prdPanel, prdPanelWidth);
|
|
221
|
+
} else {
|
|
222
|
+
applyPrdPanelWidth(prdPanel, PRD_PANEL_DEFAULT_WIDTH);
|
|
223
|
+
}
|
|
224
|
+
prdPanel.classList.add('open');
|
|
225
|
+
updatePrdContent();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
var prdCurrentFileName = '';
|
|
230
|
+
var prdCurrentPath = '';
|
|
231
|
+
var prdCurrentRawContent = '';
|
|
232
|
+
var prdWorkspaceTabs = [];
|
|
233
|
+
var prdEditMode = false;
|
|
234
|
+
|
|
235
|
+
function notifyNavigatorPrdContext() {
|
|
236
|
+
if (window.parent === window) return;
|
|
237
|
+
var targetOrigin = window.location.origin === 'null' ? '*' : window.location.origin;
|
|
238
|
+
var prdPath = '';
|
|
239
|
+
if (prdCurrentPath) {
|
|
240
|
+
prdPath = decodeURIComponent(new URL(prdCurrentPath, window.location.href).pathname);
|
|
241
|
+
}
|
|
242
|
+
window.parent.postMessage({
|
|
243
|
+
type: 'pak-prd-context',
|
|
244
|
+
htmlPath: decodeURIComponent(window.location.pathname || ''),
|
|
245
|
+
prdPath: prdPath,
|
|
246
|
+
title: prdCurrentFileName || ''
|
|
247
|
+
}, targetOrigin);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function getDefaultPrdFileName(currentPage) {
|
|
251
|
+
return currentPage.replace(/\.html$/i, '.md');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function getPrdFileCandidates(currentPage, mode) {
|
|
255
|
+
var mappedFileName = PRD_FILE_MAP[currentPage];
|
|
256
|
+
var defaultFileName = getDefaultPrdFileName(currentPage);
|
|
257
|
+
var candidates = [];
|
|
258
|
+
|
|
259
|
+
if (mode === 'view' || mode === 'approval') {
|
|
260
|
+
var viewModeMap = {
|
|
261
|
+
'annual_budget_form.html': 'annual_budget_approval.md',
|
|
262
|
+
'quarterly_budget_form.html': 'quarterly_budget_approval.md',
|
|
263
|
+
'monthly_budget_form.html': 'monthly_budget_approval.md',
|
|
264
|
+
'resource_price_form.html': 'resource_price_approval.md',
|
|
265
|
+
'kol_form.html': 'kol_list.md'
|
|
266
|
+
};
|
|
267
|
+
if (viewModeMap[currentPage]) {
|
|
268
|
+
mappedFileName = viewModeMap[currentPage];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
[mappedFileName, defaultFileName].forEach(function(fileName) {
|
|
273
|
+
if (fileName && candidates.indexOf(fileName) === -1) {
|
|
274
|
+
candidates.push(fileName);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
return candidates;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function loadPrdCandidates(fileNames) {
|
|
282
|
+
var paths = [];
|
|
283
|
+
fileNames.forEach(function(fileName) {
|
|
284
|
+
var localPath = './' + fileName;
|
|
285
|
+
var configuredPath = PRD_BASE_PATH + fileName;
|
|
286
|
+
if (paths.indexOf(localPath) === -1) {
|
|
287
|
+
paths.push(localPath);
|
|
288
|
+
}
|
|
289
|
+
if (paths.indexOf(configuredPath) === -1) {
|
|
290
|
+
paths.push(configuredPath);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
function loadNext(index) {
|
|
295
|
+
if (index >= paths.length) {
|
|
296
|
+
return Promise.resolve(null);
|
|
297
|
+
}
|
|
298
|
+
return loadMarkdownFile(paths[index]).then(function(content) {
|
|
299
|
+
if (content) {
|
|
300
|
+
return {
|
|
301
|
+
path: paths[index],
|
|
302
|
+
fileName: paths[index].split('/').pop(),
|
|
303
|
+
content: content
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
return loadNext(index + 1);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return loadNext(0);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function getPrdWorkspaceConfig(currentPage) {
|
|
314
|
+
if (typeof PRD_WORKSPACE_MAP === 'undefined') {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
return PRD_WORKSPACE_MAP[currentPage] || null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function getPrdDisplayName(filePath) {
|
|
321
|
+
return (filePath || '').split('/').pop().replace(/\.md(?:#.*)?$/i, '').replace(/_/g, ' ');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function normalizePrdPath(filePath) {
|
|
325
|
+
var path = decodeURIComponent(String(filePath || '')).split('#')[0].split('?')[0];
|
|
326
|
+
var hasDotPrefix = path.indexOf('./') === 0;
|
|
327
|
+
path = path.replace(/\\/g, '/').replace(/^\.\/+/, '');
|
|
328
|
+
var parts = [];
|
|
329
|
+
path.split('/').forEach(function(part) {
|
|
330
|
+
if (!part || part === '.') return;
|
|
331
|
+
if (part === '..') {
|
|
332
|
+
parts.pop();
|
|
333
|
+
} else {
|
|
334
|
+
parts.push(part);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
return (hasDotPrefix ? './' : './') + parts.join('/');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function resolvePrdPath(href, basePath) {
|
|
341
|
+
var cleanHref = String(href || '').split('#')[0].split('?')[0];
|
|
342
|
+
if (!cleanHref || /^[a-z]+:/i.test(cleanHref) || cleanHref.charAt(0) === '#') {
|
|
343
|
+
return '';
|
|
344
|
+
}
|
|
345
|
+
if (cleanHref.indexOf('/') === 0) {
|
|
346
|
+
return normalizePrdPath(cleanHref);
|
|
347
|
+
}
|
|
348
|
+
var base = normalizePrdPath(basePath || prdCurrentPath || './');
|
|
349
|
+
var baseDir = base.substring(0, base.lastIndexOf('/') + 1);
|
|
350
|
+
return normalizePrdPath(baseDir + cleanHref);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function isInternalPrdHref(href) {
|
|
354
|
+
var cleanHref = String(href || '').split('#')[0].split('?')[0];
|
|
355
|
+
return /\.md$/i.test(cleanHref) && !/^[a-z]+:/i.test(cleanHref);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function dedupePrdTabs(tabs) {
|
|
359
|
+
var seen = {};
|
|
360
|
+
return tabs.filter(function(tab) {
|
|
361
|
+
var key = normalizePrdPath(tab.path);
|
|
362
|
+
if (seen[key]) return false;
|
|
363
|
+
seen[key] = true;
|
|
364
|
+
tab.path = key;
|
|
365
|
+
return true;
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function buildWorkspaceTabsFromConfig(config, rootPath) {
|
|
370
|
+
if (!config || !Array.isArray(config.tabs)) {
|
|
371
|
+
return [];
|
|
372
|
+
}
|
|
373
|
+
return dedupePrdTabs(config.tabs.map(function(tab) {
|
|
374
|
+
return {
|
|
375
|
+
title: tab.title || getPrdDisplayName(tab.file || tab.path),
|
|
376
|
+
path: resolvePrdPath(tab.file || tab.path, rootPath)
|
|
377
|
+
};
|
|
378
|
+
}));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function buildWorkspaceTabsFromContent(contentEl, rootPath) {
|
|
382
|
+
var tabs = [{
|
|
383
|
+
title: '主文档',
|
|
384
|
+
path: rootPath
|
|
385
|
+
}];
|
|
386
|
+
if (!contentEl) {
|
|
387
|
+
return tabs;
|
|
388
|
+
}
|
|
389
|
+
contentEl.querySelectorAll('a[href]').forEach(function(link) {
|
|
390
|
+
var href = link.getAttribute('href');
|
|
391
|
+
if (!isInternalPrdHref(href)) return;
|
|
392
|
+
tabs.push({
|
|
393
|
+
title: link.textContent.trim() || getPrdDisplayName(href),
|
|
394
|
+
path: resolvePrdPath(href, rootPath)
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
return dedupePrdTabs(tabs);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
function renderPrdTabs(activePath) {
|
|
401
|
+
var tabsEl = document.getElementById('prdPanelTabs');
|
|
402
|
+
if (!tabsEl) return;
|
|
403
|
+
|
|
404
|
+
if (!prdWorkspaceTabs || prdWorkspaceTabs.length <= 1) {
|
|
405
|
+
tabsEl.innerHTML = '';
|
|
406
|
+
tabsEl.style.display = 'none';
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
tabsEl.style.display = '';
|
|
411
|
+
tabsEl.innerHTML = prdWorkspaceTabs.map(function(tab) {
|
|
412
|
+
var isActive = normalizePrdPath(tab.path) === normalizePrdPath(activePath);
|
|
413
|
+
return '<button type="button" class="prd-panel-tab' + (isActive ? ' active' : '') + '" data-prd-path="' + escapeHtmlAttr(tab.path) + '">' + escapeHtml(tab.title) + '</button>';
|
|
414
|
+
}).join('');
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function wrapPrdTables(contentEl) {
|
|
418
|
+
if (!contentEl) return;
|
|
419
|
+
var tables = contentEl.querySelectorAll('table');
|
|
420
|
+
tables.forEach(function(table) {
|
|
421
|
+
if (!table.parentElement.classList.contains('table-wrapper')) {
|
|
422
|
+
var wrapper = document.createElement('div');
|
|
423
|
+
wrapper.className = 'table-wrapper';
|
|
424
|
+
table.parentNode.insertBefore(wrapper, table);
|
|
425
|
+
wrapper.appendChild(table);
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function bindPrdContentLinks(contentEl) {
|
|
431
|
+
if (!contentEl) return;
|
|
432
|
+
contentEl.querySelectorAll('a[href]').forEach(function(link) {
|
|
433
|
+
var href = link.getAttribute('href');
|
|
434
|
+
if (!isInternalPrdHref(href)) return;
|
|
435
|
+
link.classList.add('prd-doc-link');
|
|
436
|
+
link.addEventListener('click', function(e) {
|
|
437
|
+
e.preventDefault();
|
|
438
|
+
var targetPath = resolvePrdPath(href, prdCurrentPath);
|
|
439
|
+
openPrdPath(targetPath, { title: link.textContent.trim() });
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function bindPrdTabs() {
|
|
445
|
+
var tabsEl = document.getElementById('prdPanelTabs');
|
|
446
|
+
if (!tabsEl) return;
|
|
447
|
+
tabsEl.querySelectorAll('.prd-panel-tab').forEach(function(tabEl) {
|
|
448
|
+
tabEl.addEventListener('click', function() {
|
|
449
|
+
openPrdPath(tabEl.getAttribute('data-prd-path'), {
|
|
450
|
+
title: tabEl.textContent.trim()
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function setPrdHeader(filePath, title) {
|
|
457
|
+
var prdTitleEl = document.getElementById('prdPanelTitle');
|
|
458
|
+
var prdFileNameEl = document.getElementById('prdPanelFileName');
|
|
459
|
+
var fileName = (filePath || '').split('/').pop();
|
|
460
|
+
|
|
461
|
+
prdCurrentFileName = fileName;
|
|
462
|
+
prdCurrentPath = normalizePrdPath(filePath || fileName);
|
|
463
|
+
|
|
464
|
+
if (prdTitleEl) {
|
|
465
|
+
prdTitleEl.textContent = (title || getPrdDisplayName(fileName)) + ' - PRD';
|
|
466
|
+
}
|
|
467
|
+
if (prdFileNameEl) {
|
|
468
|
+
prdFileNameEl.textContent = '';
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function renderLoadedPrd(result, options) {
|
|
473
|
+
var prdContentEl = document.getElementById('prdPanelContent');
|
|
474
|
+
if (!prdContentEl || !result || !result.content) return;
|
|
475
|
+
|
|
476
|
+
var currentPath = normalizePrdPath(result.path || result.fileName);
|
|
477
|
+
setPrdHeader(currentPath, options && options.title);
|
|
478
|
+
prdCurrentRawContent = result.content;
|
|
479
|
+
prdContentEl.innerHTML = renderMarkdownToHtml(result.content);
|
|
480
|
+
wrapPrdTables(prdContentEl);
|
|
481
|
+
|
|
482
|
+
if (options && options.resetWorkspace) {
|
|
483
|
+
var configuredTabs = buildWorkspaceTabsFromConfig(options.workspaceConfig, currentPath);
|
|
484
|
+
prdWorkspaceTabs = configuredTabs.length ? configuredTabs : buildWorkspaceTabsFromContent(prdContentEl, currentPath);
|
|
485
|
+
} else if (options && options.title) {
|
|
486
|
+
var targetPath = normalizePrdPath(currentPath);
|
|
487
|
+
var exists = prdWorkspaceTabs.some(function(tab) {
|
|
488
|
+
return normalizePrdPath(tab.path) === targetPath;
|
|
489
|
+
});
|
|
490
|
+
if (!exists) {
|
|
491
|
+
prdWorkspaceTabs.push({
|
|
492
|
+
title: options.title,
|
|
493
|
+
path: targetPath
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
renderPrdTabs(currentPath);
|
|
499
|
+
bindPrdTabs();
|
|
500
|
+
bindPrdContentLinks(prdContentEl);
|
|
501
|
+
notifyNavigatorPrdContext();
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function openPrdPath(filePath, options) {
|
|
505
|
+
var prdContentEl = document.getElementById('prdPanelContent');
|
|
506
|
+
var normalizedPath = normalizePrdPath(filePath);
|
|
507
|
+
if (!prdContentEl || !normalizedPath) return Promise.resolve(null);
|
|
508
|
+
|
|
509
|
+
prdContentEl.innerHTML = '<div style="padding:20px;text-align:center;color:var(--text-secondary);">加载中...</div>';
|
|
510
|
+
|
|
511
|
+
return loadMarkdownFile(normalizedPath).then(function(content) {
|
|
512
|
+
if (content) {
|
|
513
|
+
var result = {
|
|
514
|
+
path: normalizedPath,
|
|
515
|
+
fileName: normalizedPath.split('/').pop(),
|
|
516
|
+
content: content
|
|
517
|
+
};
|
|
518
|
+
renderLoadedPrd(result, options || {});
|
|
519
|
+
return result;
|
|
520
|
+
}
|
|
521
|
+
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--danger);">PRD文档加载失败,请检查文件路径:' + normalizedPath + '</div>';
|
|
522
|
+
return null;
|
|
523
|
+
}).catch(function(err) {
|
|
524
|
+
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--danger);">PRD文档加载失败:' + err.message + '</div>';
|
|
525
|
+
return null;
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function updatePrdContent() {
|
|
530
|
+
var prdContentEl = document.getElementById('prdPanelContent');
|
|
531
|
+
var prdTitleEl = document.getElementById('prdPanelTitle');
|
|
532
|
+
var prdFileNameEl = document.getElementById('prdPanelFileName');
|
|
533
|
+
if (!prdContentEl) return;
|
|
534
|
+
|
|
535
|
+
var currentPage = decodeURIComponent(window.location.pathname.split('/').pop()) || 'index.html';
|
|
536
|
+
var urlParams = new URLSearchParams(window.location.search);
|
|
537
|
+
var mode = urlParams.get('mode');
|
|
538
|
+
|
|
539
|
+
var prdFileCandidates = getPrdFileCandidates(currentPage, mode);
|
|
540
|
+
var prdFileName = prdFileCandidates[0];
|
|
541
|
+
var workspaceConfig = getPrdWorkspaceConfig(currentPage);
|
|
542
|
+
|
|
543
|
+
if (!prdFileName) {
|
|
544
|
+
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--text-secondary);">当前页面暂无对应的PRD文档</div>';
|
|
545
|
+
prdCurrentPath = '';
|
|
546
|
+
notifyNavigatorPrdContext();
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
prdCurrentFileName = prdFileName;
|
|
551
|
+
var displayName = prdFileName.replace(/\.md$/, '').replace(/_/g, ' ');
|
|
552
|
+
|
|
553
|
+
if (prdTitleEl) {
|
|
554
|
+
prdTitleEl.textContent = displayName + ' - PRD';
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
if (prdFileNameEl) {
|
|
558
|
+
prdFileNameEl.textContent = '';
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
prdContentEl.innerHTML = '<div style="padding:20px;text-align:center;color:var(--text-secondary);">加载中...</div>';
|
|
562
|
+
|
|
563
|
+
loadPrdCandidates(prdFileCandidates).then(function(result) {
|
|
564
|
+
if (result && result.content) {
|
|
565
|
+
renderLoadedPrd(result, {
|
|
566
|
+
resetWorkspace: true,
|
|
567
|
+
workspaceConfig: workspaceConfig
|
|
568
|
+
});
|
|
569
|
+
} else {
|
|
570
|
+
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--danger);">PRD文档加载失败,请检查文件路径:' + prdFileCandidates.join(' / ') + '</div>';
|
|
571
|
+
prdCurrentPath = '';
|
|
572
|
+
notifyNavigatorPrdContext();
|
|
573
|
+
}
|
|
574
|
+
}).catch(function(err) {
|
|
575
|
+
prdContentEl.innerHTML = '<div class="error" style="padding:20px;color:var(--danger);">PRD文档加载失败:' + err.message + '</div>';
|
|
576
|
+
prdCurrentPath = '';
|
|
577
|
+
notifyNavigatorPrdContext();
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function showPrdMessage(elId, text, isError) {
|
|
582
|
+
var msgEl = document.getElementById(elId);
|
|
583
|
+
if (!msgEl) return;
|
|
584
|
+
msgEl.textContent = text || '';
|
|
585
|
+
msgEl.classList.toggle('is-error', !!isError);
|
|
586
|
+
if (text) {
|
|
587
|
+
setTimeout(function() {
|
|
588
|
+
if (msgEl.textContent === text) {
|
|
589
|
+
msgEl.textContent = '';
|
|
590
|
+
msgEl.classList.remove('is-error');
|
|
591
|
+
}
|
|
592
|
+
}, 2000);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function showPrdToast(text, isError) {
|
|
597
|
+
showPrdMessage('prdPanelToast', text, isError);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function showPrdEditorMsg(text, isError) {
|
|
601
|
+
showPrdMessage('prdPanelEditorMsg', text, isError);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function enterPrdEditMode() {
|
|
605
|
+
if (isLocalFileProtocol()) {
|
|
606
|
+
showPrdToast('请通过本地服务器(npm run serve)打开原型后再编辑', true);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
var contentEl = document.getElementById('prdPanelContent');
|
|
611
|
+
var editorWrap = document.getElementById('prdPanelEditorWrap');
|
|
612
|
+
var tabsEl = document.getElementById('prdPanelTabs');
|
|
613
|
+
var textarea = document.getElementById('prdPanelEditorTextarea');
|
|
614
|
+
var editBtn = document.getElementById('prdPanelEditBtn');
|
|
615
|
+
if (!contentEl || !editorWrap || !textarea) return;
|
|
616
|
+
|
|
617
|
+
prdEditMode = true;
|
|
618
|
+
textarea.value = prdCurrentRawContent;
|
|
619
|
+
contentEl.style.display = 'none';
|
|
620
|
+
if (tabsEl) tabsEl.style.display = 'none';
|
|
621
|
+
if (editBtn) editBtn.style.display = 'none';
|
|
622
|
+
editorWrap.style.display = '';
|
|
623
|
+
textarea.focus();
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function exitPrdEditMode() {
|
|
627
|
+
var contentEl = document.getElementById('prdPanelContent');
|
|
628
|
+
var editorWrap = document.getElementById('prdPanelEditorWrap');
|
|
629
|
+
var editBtn = document.getElementById('prdPanelEditBtn');
|
|
630
|
+
|
|
631
|
+
prdEditMode = false;
|
|
632
|
+
if (editorWrap) editorWrap.style.display = 'none';
|
|
633
|
+
if (contentEl) contentEl.style.display = '';
|
|
634
|
+
if (editBtn) editBtn.style.display = '';
|
|
635
|
+
renderPrdTabs(prdCurrentPath);
|
|
636
|
+
bindPrdTabs();
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function applyPrdRedColor() {
|
|
640
|
+
var textarea = document.getElementById('prdPanelEditorTextarea');
|
|
641
|
+
if (!textarea) return;
|
|
642
|
+
|
|
643
|
+
var start = textarea.selectionStart;
|
|
644
|
+
var end = textarea.selectionEnd;
|
|
645
|
+
if (start === end) {
|
|
646
|
+
showPrdEditorMsg('请先选中要标红的文字', true);
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
var value = textarea.value;
|
|
651
|
+
var selected = value.slice(start, end);
|
|
652
|
+
var wrapped = wrapPrdSelectionWithRed(selected);
|
|
653
|
+
var scrollTop = textarea.scrollTop;
|
|
654
|
+
textarea.value = value.slice(0, start) + wrapped + value.slice(end);
|
|
655
|
+
textarea.focus();
|
|
656
|
+
textarea.setSelectionRange(start, start + wrapped.length);
|
|
657
|
+
restorePrdEditorScroll(textarea, scrollTop);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
function wrapPrdSelectionWithRed(text) {
|
|
661
|
+
if (!isPrdMarkdownTableSelection(text)) {
|
|
662
|
+
return '<span style="color:#e53935">' + text + '</span>';
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
return text.split('\n').map(function(line) {
|
|
666
|
+
if (!isPrdMarkdownTableRow(line) || isPrdMarkdownTableDivider(line)) {
|
|
667
|
+
return line;
|
|
668
|
+
}
|
|
669
|
+
return wrapPrdMarkdownTableRowCells(line);
|
|
670
|
+
}).join('\n');
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
function isPrdMarkdownTableSelection(text) {
|
|
674
|
+
return text.split('\n').some(function(line) {
|
|
675
|
+
return isPrdMarkdownTableRow(line) && !isPrdMarkdownTableDivider(line);
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
function isPrdMarkdownTableRow(line) {
|
|
680
|
+
return /^\s*\|.*\|\s*$/.test(line);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function isPrdMarkdownTableDivider(line) {
|
|
684
|
+
return /^\s*\|[\s:|-]+\|\s*$/.test(line);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function wrapPrdMarkdownTableRowCells(line) {
|
|
688
|
+
var leading = line.match(/^\s*/)[0];
|
|
689
|
+
var trailing = line.match(/\s*$/)[0];
|
|
690
|
+
var body = line.trim();
|
|
691
|
+
var cells = body.slice(1, -1).split('|');
|
|
692
|
+
|
|
693
|
+
return leading + '|' + cells.map(function(cell) {
|
|
694
|
+
var left = cell.match(/^\s*/)[0];
|
|
695
|
+
var right = cell.match(/\s*$/)[0];
|
|
696
|
+
var content = cell.trim();
|
|
697
|
+
if (!content || /<span\b[^>]*\bstyle\s*=\s*["'][^"']*\bcolor\s*:\s*(?:#e53935|red)\b[^"']*["'][^>]*>[\s\S]*<\/span>/i.test(content)) {
|
|
698
|
+
return cell;
|
|
699
|
+
}
|
|
700
|
+
return left + '<span style="color:#e53935">' + content + '</span>' + right;
|
|
701
|
+
}).join('|') + '|' + trailing;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function removePrdRedColor() {
|
|
705
|
+
var textarea = document.getElementById('prdPanelEditorTextarea');
|
|
706
|
+
if (!textarea) return;
|
|
707
|
+
|
|
708
|
+
var start = textarea.selectionStart;
|
|
709
|
+
var end = textarea.selectionEnd;
|
|
710
|
+
var value = textarea.value;
|
|
711
|
+
var spanRegex = /<span\b[^>]*\bstyle\s*=\s*["'][^"']*\bcolor\s*:\s*(?:#e53935|red)\b[^"']*["'][^>]*>([\s\S]*?)<\/span>/gi;
|
|
712
|
+
var match;
|
|
713
|
+
var matches = [];
|
|
714
|
+
|
|
715
|
+
while ((match = spanRegex.exec(value)) !== null) {
|
|
716
|
+
var spanStart = match.index;
|
|
717
|
+
var spanEnd = match.index + match[0].length;
|
|
718
|
+
if (start <= spanEnd && end >= spanStart) {
|
|
719
|
+
matches.push({
|
|
720
|
+
start: spanStart,
|
|
721
|
+
end: spanEnd,
|
|
722
|
+
full: match[0],
|
|
723
|
+
inner: match[1]
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (!matches.length) {
|
|
729
|
+
showPrdEditorMsg('请先选中或将光标放在已标红的文字上', true);
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
var nextValue = value;
|
|
734
|
+
var scrollTop = textarea.scrollTop;
|
|
735
|
+
var nextCaret = matches[0].start;
|
|
736
|
+
matches.reverse().forEach(function(item) {
|
|
737
|
+
nextValue = nextValue.slice(0, item.start) + item.inner + nextValue.slice(item.end);
|
|
738
|
+
});
|
|
739
|
+
textarea.value = nextValue;
|
|
740
|
+
textarea.focus();
|
|
741
|
+
textarea.setSelectionRange(nextCaret, nextCaret);
|
|
742
|
+
restorePrdEditorScroll(textarea, scrollTop);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
function removeAllPrdRedColor() {
|
|
746
|
+
var textarea = document.getElementById('prdPanelEditorTextarea');
|
|
747
|
+
if (!textarea) return;
|
|
748
|
+
|
|
749
|
+
var value = textarea.value;
|
|
750
|
+
var nextValue = removePrdRedSpans(value);
|
|
751
|
+
if (nextValue === value) {
|
|
752
|
+
showPrdEditorMsg('当前文档没有标红内容', true);
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
var scrollTop = textarea.scrollTop;
|
|
757
|
+
var caret = textarea.selectionStart;
|
|
758
|
+
textarea.value = nextValue;
|
|
759
|
+
textarea.focus();
|
|
760
|
+
textarea.setSelectionRange(Math.min(caret, nextValue.length), Math.min(caret, nextValue.length));
|
|
761
|
+
restorePrdEditorScroll(textarea, scrollTop);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
function removePrdRedSpans(text) {
|
|
765
|
+
return text.replace(/<span\b[^>]*\bstyle\s*=\s*["'][^"']*\bcolor\s*:\s*(?:#e53935|red)\b[^"']*["'][^>]*>([\s\S]*?)<\/span>/gi, '$1');
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
function restorePrdEditorScroll(textarea, scrollTop) {
|
|
769
|
+
textarea.scrollTop = scrollTop;
|
|
770
|
+
var restore = function() {
|
|
771
|
+
textarea.scrollTop = scrollTop;
|
|
772
|
+
};
|
|
773
|
+
if (window.requestAnimationFrame) {
|
|
774
|
+
window.requestAnimationFrame(restore);
|
|
775
|
+
} else {
|
|
776
|
+
setTimeout(restore, 0);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function savePrdEdits() {
|
|
781
|
+
var textarea = document.getElementById('prdPanelEditorTextarea');
|
|
782
|
+
var contentEl = document.getElementById('prdPanelContent');
|
|
783
|
+
if (!textarea || !contentEl || !prdCurrentPath) {
|
|
784
|
+
return Promise.reject(new Error('当前没有可保存的 PRD'));
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
var newContent = textarea.value;
|
|
788
|
+
var absolutePath = decodeURIComponent(new URL(prdCurrentPath, window.location.href).pathname);
|
|
789
|
+
|
|
790
|
+
return fetch('/api/save-prd', {
|
|
791
|
+
method: 'POST',
|
|
792
|
+
headers: { 'Content-Type': 'application/json' },
|
|
793
|
+
body: JSON.stringify({ path: absolutePath, content: newContent })
|
|
794
|
+
}).then(function(response) {
|
|
795
|
+
return response.text().then(function(text) {
|
|
796
|
+
var data;
|
|
797
|
+
try {
|
|
798
|
+
data = JSON.parse(text);
|
|
799
|
+
} catch (e) {
|
|
800
|
+
throw new Error('当前地址不支持保存,请通过本地服务器(npm run serve,默认 http://127.0.0.1:8082)打开原型后再编辑');
|
|
801
|
+
}
|
|
802
|
+
if (!response.ok || !data.ok) {
|
|
803
|
+
throw new Error(data.error || ('保存失败: ' + response.status));
|
|
804
|
+
}
|
|
805
|
+
return data;
|
|
806
|
+
});
|
|
807
|
+
}).then(function() {
|
|
808
|
+
PRD_CACHE[prdCurrentPath] = newContent;
|
|
809
|
+
prdCurrentRawContent = newContent;
|
|
810
|
+
contentEl.innerHTML = renderMarkdownToHtml(newContent);
|
|
811
|
+
wrapPrdTables(contentEl);
|
|
812
|
+
bindPrdContentLinks(contentEl);
|
|
813
|
+
exitPrdEditMode();
|
|
814
|
+
showPrdToast('已保存', false);
|
|
815
|
+
return true;
|
|
816
|
+
}).catch(function(err) {
|
|
817
|
+
showPrdEditorMsg('保存失败:' + err.message, true);
|
|
818
|
+
throw err;
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
function copyPrdRawContent() {
|
|
823
|
+
var editorTextarea = document.getElementById('prdPanelEditorTextarea');
|
|
824
|
+
var copyText = prdEditMode && editorTextarea ? editorTextarea.value : prdCurrentRawContent;
|
|
825
|
+
if (!copyText && prdCurrentPath) {
|
|
826
|
+
loadMarkdownFile(prdCurrentPath).then(function(content) {
|
|
827
|
+
copyTextToClipboard(content || '');
|
|
828
|
+
});
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
copyTextToClipboard(copyText || '');
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function copyTextToClipboard(text) {
|
|
836
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
837
|
+
navigator.clipboard.writeText(text).then(function() {
|
|
838
|
+
showCopySuccess();
|
|
839
|
+
}).catch(function(err) {
|
|
840
|
+
fallbackCopy(text);
|
|
841
|
+
});
|
|
842
|
+
} else {
|
|
843
|
+
fallbackCopy(text);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function fallbackCopy(text) {
|
|
848
|
+
var textarea = document.createElement('textarea');
|
|
849
|
+
textarea.value = text;
|
|
850
|
+
textarea.style.position = 'fixed';
|
|
851
|
+
textarea.style.opacity = '0';
|
|
852
|
+
document.body.appendChild(textarea);
|
|
853
|
+
textarea.select();
|
|
854
|
+
try {
|
|
855
|
+
document.execCommand('copy');
|
|
856
|
+
showCopySuccess();
|
|
857
|
+
} catch (err) {
|
|
858
|
+
console.error('复制失败:', err);
|
|
859
|
+
}
|
|
860
|
+
document.body.removeChild(textarea);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
function escapeHtml(str) {
|
|
864
|
+
return String(str || '').replace(/[&<>"']/g, function(ch) {
|
|
865
|
+
var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
866
|
+
return map[ch] || ch;
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function escapeHtmlAttr(str) {
|
|
871
|
+
return escapeHtml(str).replace(/`/g, '`');
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
function showCopySuccess() {
|
|
875
|
+
var copyBtn = document.getElementById('prdPanelCopyBtn');
|
|
876
|
+
if (!copyBtn) return;
|
|
877
|
+
|
|
878
|
+
var originalHTML = copyBtn.innerHTML;
|
|
879
|
+
copyBtn.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
|
|
880
|
+
copyBtn.style.color = '#52c41a';
|
|
881
|
+
|
|
882
|
+
setTimeout(function() {
|
|
883
|
+
copyBtn.innerHTML = originalHTML;
|
|
884
|
+
copyBtn.style.color = '';
|
|
885
|
+
}, 2000);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function isLocalFileProtocol() {
|
|
889
|
+
return window.location.protocol === 'file:';
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function loadMarkdownFile(filePath) {
|
|
893
|
+
var inlineKey = filePath.replace(PRD_BASE_PATH, '').replace(/^\.\//, '');
|
|
894
|
+
if (typeof INLINE_PRD_CONTENT !== 'undefined' && INLINE_PRD_CONTENT[inlineKey]) {
|
|
895
|
+
var content = INLINE_PRD_CONTENT[inlineKey];
|
|
896
|
+
PRD_CACHE[filePath] = content;
|
|
897
|
+
return Promise.resolve(content);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
return new Promise(function(resolve, reject) {
|
|
901
|
+
if (isLocalFileProtocol()) {
|
|
902
|
+
var xhr = new XMLHttpRequest();
|
|
903
|
+
xhr.open('GET', filePath, true);
|
|
904
|
+
xhr.onload = function() {
|
|
905
|
+
if (xhr.status === 200 || xhr.status === 0) {
|
|
906
|
+
PRD_CACHE[filePath] = xhr.responseText;
|
|
907
|
+
resolve(xhr.responseText);
|
|
908
|
+
} else {
|
|
909
|
+
resolve(null);
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
xhr.onerror = function() {
|
|
913
|
+
console.error('XHR加载失败:', filePath);
|
|
914
|
+
resolve(null);
|
|
915
|
+
};
|
|
916
|
+
xhr.send();
|
|
917
|
+
} else {
|
|
918
|
+
fetch(filePath, { cache: 'no-store' })
|
|
919
|
+
.then(function(response) {
|
|
920
|
+
if (!response.ok) {
|
|
921
|
+
throw new Error('文件加载失败: ' + response.status);
|
|
922
|
+
}
|
|
923
|
+
return response.text();
|
|
924
|
+
})
|
|
925
|
+
.then(function(text) {
|
|
926
|
+
PRD_CACHE[filePath] = text;
|
|
927
|
+
resolve(text);
|
|
928
|
+
})
|
|
929
|
+
.catch(function(error) {
|
|
930
|
+
console.error('加载 Markdown 文件失败:', error);
|
|
931
|
+
resolve(null);
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
function renderMarkdownToHtml(markdown) {
|
|
938
|
+
if (typeof marked !== 'undefined') {
|
|
939
|
+
marked.setOptions({
|
|
940
|
+
breaks: true,
|
|
941
|
+
gfm: true,
|
|
942
|
+
tables: true
|
|
943
|
+
});
|
|
944
|
+
var html = marked.parse(markdown);
|
|
945
|
+
|
|
946
|
+
if (html.indexOf('<div class="table-wrapper">') === -1) {
|
|
947
|
+
html = html.replace(/<table>/g, '<div class="table-wrapper"><table>');
|
|
948
|
+
html = html.replace(/<\/table>/g, '</table></div>');
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
return html;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
var html = markdown;
|
|
955
|
+
|
|
956
|
+
html = html.replace(/^###### (.*$)/gim, '<h6>$1</h6>');
|
|
957
|
+
html = html.replace(/^##### (.*$)/gim, '<h5>$1</h5>');
|
|
958
|
+
html = html.replace(/^#### (.*$)/gim, '<h4>$1</h4>');
|
|
959
|
+
html = html.replace(/^### (.*$)/gim, '<h3>$1</h3>');
|
|
960
|
+
html = html.replace(/^## (.*$)/gim, '<h2>$1</h2>');
|
|
961
|
+
html = html.replace(/^# (.*$)/gim, '<h1>$1</h1>');
|
|
962
|
+
|
|
963
|
+
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
|
|
964
|
+
html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
|
|
965
|
+
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
|
|
966
|
+
|
|
967
|
+
var tableRegex = /\|(.+)\|\n\|[-| :]+\|\n((?:\|.+\|\n?)+)/g;
|
|
968
|
+
html = html.replace(tableRegex, function(match, header, body) {
|
|
969
|
+
var headers = header.split('|').slice(1, -1);
|
|
970
|
+
var rows = body.trim().split('\n');
|
|
971
|
+
|
|
972
|
+
var tableHtml = '<div class="table-wrapper"><table><thead><tr>';
|
|
973
|
+
headers.forEach(function(h) {
|
|
974
|
+
tableHtml += '<th>' + h.trim() + '</th>';
|
|
975
|
+
});
|
|
976
|
+
tableHtml += '</tr></thead><tbody>';
|
|
977
|
+
|
|
978
|
+
rows.forEach(function(row) {
|
|
979
|
+
var cells = row.split('|').slice(1, -1);
|
|
980
|
+
tableHtml += '<tr>';
|
|
981
|
+
cells.forEach(function(c) {
|
|
982
|
+
tableHtml += '<td>' + c.trim() + '</td>';
|
|
983
|
+
});
|
|
984
|
+
tableHtml += '</tr>';
|
|
985
|
+
});
|
|
986
|
+
tableHtml += '</tbody></table></div>';
|
|
987
|
+
return tableHtml;
|
|
988
|
+
});
|
|
989
|
+
|
|
990
|
+
html = html.replace(/^- (.*$)/gim, '<li>$1</li>');
|
|
991
|
+
html = html.replace(/(<li>.*<\/li>\n?)+/g, '<ul>$&</ul>');
|
|
992
|
+
|
|
993
|
+
html = html.replace(/^> (.*$)/gim, '<blockquote>$1</blockquote>');
|
|
994
|
+
|
|
995
|
+
html = html.replace(/---/g, '<hr>');
|
|
996
|
+
|
|
997
|
+
html = html.replace(/\n/g, '<br>');
|
|
998
|
+
|
|
999
|
+
return html;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
if (document.readyState === 'loading') {
|
|
1003
|
+
document.addEventListener('DOMContentLoaded', function() {
|
|
1004
|
+
if (!prdPanelInitialized) {
|
|
1005
|
+
initPrdPanel();
|
|
1006
|
+
}
|
|
1007
|
+
});
|
|
1008
|
+
} else {
|
|
1009
|
+
setTimeout(function() {
|
|
1010
|
+
if (!prdPanelInitialized) {
|
|
1011
|
+
initPrdPanel();
|
|
1012
|
+
}
|
|
1013
|
+
}, 0);
|
|
1014
|
+
}
|