openprototype 0.2.0 → 1.0.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.
@@ -285,6 +285,29 @@ body.is-resizing-sidebar .preview-frame {
285
285
  .folder.collapsed > .folder-head .folder-arrow { transform: rotate(-90deg); }
286
286
  .folder-icon { color: #f5a400; flex-shrink: 0; }
287
287
  .folder-icon svg { width: 13px; height: 13px; fill: currentColor; display: block; }
288
+ .folder-name {
289
+ flex: 1;
290
+ min-width: 0;
291
+ overflow: hidden;
292
+ text-overflow: ellipsis;
293
+ white-space: nowrap;
294
+ }
295
+ .tree-add-btn {
296
+ flex-shrink: 0;
297
+ height: 17px;
298
+ padding: 0 4px;
299
+ border: 1px solid #d8dfe9;
300
+ border-radius: 3px;
301
+ background: #fff;
302
+ color: #8a94a6;
303
+ font-size: 10px;
304
+ font-weight: 400;
305
+ line-height: 15px;
306
+ cursor: pointer;
307
+ visibility: hidden;
308
+ }
309
+ .folder-head:hover .tree-add-btn { visibility: visible; }
310
+ .tree-add-btn:hover { border-color: #1677ff; color: #1677ff; background: #f0f5ff; }
288
311
 
289
312
  /* ── File row ── */
290
313
  .file-row {
@@ -403,6 +426,43 @@ body.is-resizing-sidebar .preview-frame {
403
426
  }
404
427
  .toast.show { opacity: 1; }
405
428
 
429
+ /* ── Create modal ── */
430
+ .create-modal-mask {
431
+ position: fixed;
432
+ inset: 0;
433
+ background: rgba(15, 23, 42, 0.35);
434
+ display: none;
435
+ align-items: center;
436
+ justify-content: center;
437
+ z-index: 10000;
438
+ }
439
+ .create-modal-mask.show { display: flex; }
440
+ .create-modal {
441
+ width: 320px;
442
+ background: #fff;
443
+ border-radius: 10px;
444
+ box-shadow: 0 12px 40px rgba(15, 23, 42, 0.18);
445
+ padding: 16px;
446
+ }
447
+ .create-modal-title { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
448
+ .create-modal-location { font-size: 11px; color: #7b8495; margin-bottom: 12px; word-break: break-all; }
449
+ .create-modal-field { margin-bottom: 10px; }
450
+ .create-modal-field label { display: block; font-size: 11px; color: #4f5665; margin-bottom: 4px; }
451
+ .create-modal-field input {
452
+ width: 100%;
453
+ border: 1px solid #dfe5ee;
454
+ border-radius: 4px;
455
+ padding: 5px 8px;
456
+ font-size: 12px;
457
+ color: #20242d;
458
+ outline: none;
459
+ }
460
+ .create-modal-field input:focus { border-color: #1677ff; }
461
+ .create-modal-error { font-size: 11px; color: #d91f2f; min-height: 15px; margin-bottom: 6px; }
462
+ .create-modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
463
+ .create-modal-actions .btn { padding: 4px 14px; font-size: 12px; }
464
+ .create-modal-actions .btn-primary { margin-left: 0; }
465
+
406
466
  /* ── Version badge ── */
407
467
  .version-badge {
408
468
  flex-shrink: 0;
@@ -482,6 +542,8 @@ body.is-resizing-sidebar .preview-frame {
482
542
  </label>
483
543
  <button class="btn" id="expandAllBtn">全部展开</button>
484
544
  <button class="btn" id="collapseAllBtn">全部折叠</button>
545
+ <button class="btn" id="newFolderBtn">新建文件夹</button>
546
+ <button class="btn" id="newPageBtn">新建页面</button>
485
547
  </div>
486
548
  </div>
487
549
 
@@ -502,6 +564,27 @@ body.is-resizing-sidebar .preview-frame {
502
564
  <iframe class="preview-frame" id="previewFrame" style="display:none"></iframe>
503
565
  </div>
504
566
 
567
+ <!-- ═══ CREATE MODAL ═══ -->
568
+ <div class="create-modal-mask" id="createModalMask">
569
+ <div class="create-modal" role="dialog" aria-modal="true" aria-labelledby="createModalTitle">
570
+ <div class="create-modal-title" id="createModalTitle">新建页面</div>
571
+ <div class="create-modal-location" id="createModalLocation"></div>
572
+ <div class="create-modal-field">
573
+ <label for="createNameInput">名称(作为文件 / 目录名,支持中文、字母、数字、_ 和 -)</label>
574
+ <input id="createNameInput" type="text" placeholder="如 order_list" autocomplete="off">
575
+ </div>
576
+ <div class="create-modal-field" id="createTitleField">
577
+ <label for="createTitleInput">页面标题(可选,用于页面 &lt;title&gt; 与 PRD)</label>
578
+ <input id="createTitleInput" type="text" placeholder="如 订单列表" autocomplete="off">
579
+ </div>
580
+ <div class="create-modal-error" id="createModalError"></div>
581
+ <div class="create-modal-actions">
582
+ <button class="btn" id="createCancelBtn" type="button">取消</button>
583
+ <button class="btn btn-primary" id="createConfirmBtn" type="button">创建</button>
584
+ </div>
585
+ </div>
586
+ </div>
587
+
505
588
  <div class="toast" id="toast"></div>
506
589
 
507
590
  <script>
@@ -548,6 +631,7 @@ function saveVersionHistory() {
548
631
  let reviewStatus = loadReviewStatus();
549
632
  let versionHistory = loadVersionHistory();
550
633
  let activeRow = null;
634
+ let previewLoadRequest = 0;
551
635
 
552
636
  // ─────────────────────────────────────────────
553
637
  // Sidebar resize
@@ -713,6 +797,18 @@ function loadPreview(path, row) {
713
797
  const openBtn = document.getElementById('previewOpenBtn');
714
798
 
715
799
  const url = path;
800
+ const requestId = ++previewLoadRequest;
801
+ const expectedUrl = new URL(url, window.location.href).href;
802
+ frame.style.visibility = 'hidden';
803
+ frame.addEventListener('load', function revealLoadedPreview() {
804
+ if (requestId !== previewLoadRequest) {
805
+ frame.removeEventListener('load', revealLoadedPreview);
806
+ return;
807
+ }
808
+ if (frame.contentWindow.location.href !== expectedUrl) return;
809
+ frame.style.visibility = 'visible';
810
+ frame.removeEventListener('load', revealLoadedPreview);
811
+ });
716
812
  frame.src = url;
717
813
  frame.style.display = 'block';
718
814
  placeholder.style.display = 'none';
@@ -905,7 +1001,7 @@ function applyTitleSearch() {
905
1001
  // ─────────────────────────────────────────────
906
1002
  let totalCount = 0;
907
1003
 
908
- function renderNodes(nodes, depth) {
1004
+ function renderNodes(nodes, depth, trail) {
909
1005
  const wrap = document.createElement('div');
910
1006
  wrap.style.paddingLeft = depth === 0 ? '0' : '14px';
911
1007
 
@@ -940,17 +1036,25 @@ function renderNodes(nodes, depth) {
940
1036
 
941
1037
  wrap.appendChild(row);
942
1038
  } else {
1039
+ const folderKey = trail ? trail + '/' + node.name : node.name;
943
1040
  const folder = document.createElement('div');
944
1041
  folder.className = 'folder';
945
1042
  folder.dataset.searchText = node.name;
1043
+ folder.dataset.pathKey = folderKey;
946
1044
 
947
1045
  const head = document.createElement('div');
948
1046
  head.className = 'folder-head';
949
- head.innerHTML = `<span class="folder-arrow">${I_ARROW}</span><span class="folder-icon">${I_FOLDER}</span><span>${node.name}</span>`;
1047
+ head.innerHTML = `<span class="folder-arrow">${I_ARROW}</span><span class="folder-icon">${I_FOLDER}</span><span class="folder-name">${node.name}</span><button type="button" class="tree-add-btn" data-create="page" title="在此文件夹新建页面">+页</button><button type="button" class="tree-add-btn" data-create="folder" title="在此文件夹新建子文件夹">+夹</button>`;
950
1048
  head.addEventListener('click', () => folder.classList.toggle('collapsed'));
1049
+ head.querySelectorAll('.tree-add-btn').forEach(btn => {
1050
+ btn.addEventListener('click', e => {
1051
+ e.stopPropagation();
1052
+ openCreateModal(btn.dataset.create, folderKey);
1053
+ });
1054
+ });
951
1055
 
952
1056
  folder.appendChild(head);
953
- folder.appendChild(renderNodes(node.children || [], depth + 1));
1057
+ folder.appendChild(renderNodes(node.children || [], depth + 1, folderKey));
954
1058
  folder.classList.add('folder-children-wrap');
955
1059
 
956
1060
  // wrap children into .folder-children div for collapse
@@ -964,6 +1068,100 @@ function renderNodes(nodes, depth) {
964
1068
  return wrap;
965
1069
  }
966
1070
 
1071
+ // 重建整棵树;重渲染时保留折叠状态与当前选中页
1072
+ function renderTree() {
1073
+ const treeWrap = document.getElementById('sidebarTree');
1074
+ const collapsedKeys = new Set(
1075
+ Array.from(treeWrap.querySelectorAll('.folder.collapsed')).map(f => f.dataset.pathKey)
1076
+ );
1077
+ const activePath = activeRow ? activeRow.dataset.path : '';
1078
+ totalCount = 0;
1079
+ Object.keys(breadcrumbMap).forEach(key => delete breadcrumbMap[key]);
1080
+ buildBreadcrumbs(TREE, '');
1081
+ treeWrap.replaceChildren(renderNodes(TREE, 0, ''));
1082
+ treeWrap.querySelectorAll('.folder').forEach(f => {
1083
+ if (collapsedKeys.has(f.dataset.pathKey)) f.classList.add('collapsed');
1084
+ });
1085
+ activeRow = null;
1086
+ if (activePath) {
1087
+ const row = treeWrap.querySelector(`.file-row[data-path="${CSS.escape(activePath)}"]`);
1088
+ if (row) { row.classList.add('active'); activeRow = row; }
1089
+ }
1090
+ document.getElementById('totalCount').textContent = totalCount;
1091
+ applyTitleSearch();
1092
+ updateStats();
1093
+ }
1094
+
1095
+ // ─────────────────────────────────────────────
1096
+ // Create folder / page
1097
+ // ─────────────────────────────────────────────
1098
+ const PAGE_BASE = decodeURIComponent(new URL('.', window.location.href).pathname).replace(/\/$/, '');
1099
+ let createModalState = null;
1100
+
1101
+ function openCreateModal(type, parentPath) {
1102
+ createModalState = { type, parentPath: parentPath || '' };
1103
+ document.getElementById('createModalTitle').textContent = type === 'folder' ? '新建文件夹' : '新建页面';
1104
+ document.getElementById('createModalLocation').textContent = '位置:' + (parentPath ? parentPath + '/' : '根目录');
1105
+ document.getElementById('createTitleField').style.display = type === 'page' ? '' : 'none';
1106
+ document.getElementById('createNameInput').value = '';
1107
+ document.getElementById('createTitleInput').value = '';
1108
+ document.getElementById('createModalError').textContent = '';
1109
+ document.getElementById('createModalMask').classList.add('show');
1110
+ document.getElementById('createNameInput').focus();
1111
+ }
1112
+
1113
+ function closeCreateModal() {
1114
+ createModalState = null;
1115
+ document.getElementById('createModalMask').classList.remove('show');
1116
+ }
1117
+
1118
+ function expandFolderTrail(key) {
1119
+ document.querySelectorAll('.folder').forEach(f => {
1120
+ const k = f.dataset.pathKey || '';
1121
+ if (k === key || key.startsWith(k + '/')) f.classList.remove('collapsed');
1122
+ });
1123
+ }
1124
+
1125
+ async function submitCreateModal() {
1126
+ if (!createModalState) return;
1127
+ const { type, parentPath } = createModalState;
1128
+ const errorEl = document.getElementById('createModalError');
1129
+ const name = document.getElementById('createNameInput').value.trim();
1130
+ if (!name) { errorEl.textContent = '请输入名称'; return; }
1131
+ if (!/^[\w一-龥-]+$/.test(name)) {
1132
+ errorEl.textContent = '名称只能包含中文、字母、数字、下划线或短横线';
1133
+ return;
1134
+ }
1135
+ const confirmBtn = document.getElementById('createConfirmBtn');
1136
+ confirmBtn.disabled = true;
1137
+ errorEl.textContent = '';
1138
+ try {
1139
+ const payload = { base: PAGE_BASE, parent: parentPath, name };
1140
+ if (type === 'page') payload.title = document.getElementById('createTitleInput').value.trim();
1141
+ const res = await fetch(type === 'folder' ? '/api/create-folder' : '/api/create-page', {
1142
+ method: 'POST',
1143
+ headers: { 'Content-Type': 'application/json' },
1144
+ body: JSON.stringify(payload)
1145
+ });
1146
+ const data = await res.json().catch(() => ({}));
1147
+ if (!res.ok || !data.ok) throw new Error(data.error || '创建失败:' + res.status);
1148
+ closeCreateModal();
1149
+ await loadTree();
1150
+ renderTree();
1151
+ if (parentPath) expandFolderTrail(parentPath);
1152
+ showToast((type === 'folder' ? '文件夹已创建:' : '页面已创建:') + data.path);
1153
+ if (type === 'page') {
1154
+ const row = document.querySelector(`.file-row[data-path="${CSS.escape(data.path)}"]`);
1155
+ if (row) loadPreview(data.path, row);
1156
+ }
1157
+ fetchAllVersions();
1158
+ } catch (err) {
1159
+ errorEl.textContent = err.message;
1160
+ } finally {
1161
+ confirmBtn.disabled = false;
1162
+ }
1163
+ }
1164
+
967
1165
  // ─────────────────────────────────────────────
968
1166
  // Stats
969
1167
  // ─────────────────────────────────────────────
@@ -1004,8 +1202,6 @@ window.addEventListener('DOMContentLoaded', async () => {
1004
1202
  document.getElementById('sidebarTree').textContent = '页面树加载失败:' + e.message;
1005
1203
  return;
1006
1204
  }
1007
- buildBreadcrumbs(TREE, '');
1008
-
1009
1205
  const savedVer = navStorage.getItem(VERSION_KEY);
1010
1206
  if (savedVer) document.getElementById('versionInput').value = savedVer;
1011
1207
  const versionInput = document.getElementById('versionInput');
@@ -1025,9 +1221,7 @@ window.addEventListener('DOMContentLoaded', async () => {
1025
1221
  document.getElementById('navSearchInput').addEventListener('input', applyTitleSearch);
1026
1222
 
1027
1223
  const treeWrap = document.getElementById('sidebarTree');
1028
- treeWrap.appendChild(renderNodes(TREE, 0));
1029
- document.getElementById('totalCount').textContent = totalCount;
1030
- updateStats();
1224
+ renderTree();
1031
1225
 
1032
1226
  // version filter: re-evaluate all badges on input change
1033
1227
  document.getElementById('versionInput').addEventListener('input', () => {
@@ -1058,6 +1252,21 @@ window.addEventListener('DOMContentLoaded', async () => {
1058
1252
  copyText(window.location.href, '本页地址已复制到剪贴板');
1059
1253
  });
1060
1254
 
1255
+ // 新建文件夹 / 页面
1256
+ document.getElementById('newFolderBtn').addEventListener('click', () => openCreateModal('folder', ''));
1257
+ document.getElementById('newPageBtn').addEventListener('click', () => openCreateModal('page', ''));
1258
+ document.getElementById('createCancelBtn').addEventListener('click', closeCreateModal);
1259
+ document.getElementById('createConfirmBtn').addEventListener('click', submitCreateModal);
1260
+ document.getElementById('createModalMask').addEventListener('click', e => {
1261
+ if (e.target === e.currentTarget) closeCreateModal();
1262
+ });
1263
+ [document.getElementById('createNameInput'), document.getElementById('createTitleInput')].forEach(input => {
1264
+ input.addEventListener('keydown', e => {
1265
+ if (e.key === 'Enter') { e.preventDefault(); submitCreateModal(); }
1266
+ else if (e.key === 'Escape') closeCreateModal();
1267
+ });
1268
+ });
1269
+
1061
1270
  });
1062
1271
  </script>
1063
1272
  <!-- agent-panel:start (本地开发专用;发布时可由 openprototype publish 自动剥离) -->