cladogram 0.5.5-beta.6 → 0.5.5-beta.7

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.
@@ -6,6 +6,7 @@
6
6
  const post = (type, detail = {}) => parent.postMessage({ type: 'clado-library:' + type, file, ...detail }, '*');
7
7
  document.addEventListener('pointerdown', () => post('dismiss-directory'), true);
8
8
  document.addEventListener('keydown', event => { if (event.key === 'Escape') post('dismiss-directory'); });
9
+ let cardSelectionActive = false;
9
10
  if (!window.__CLADO_OFFLINE__ && window.__CLADO_FILE_VERSION__) {
10
11
  let paths = [];
11
12
  const frames = new WeakSet();
@@ -57,7 +58,8 @@
57
58
  bindSelection(document, runtime);
58
59
  window.addEventListener('message', event => {
59
60
  if (event.source !== parent || event.origin !== new URL(document.baseURI).origin || event.data?.type !== 'clado-workspace:selection') return;
60
- paths = event.data.nodes;
61
+ paths = event.data.nodes || [];
62
+ cardSelectionActive = paths.length > 0;
61
63
  paint(document, runtime);
62
64
  const win = document.querySelector('.canvas-chain-dialog iframe')?.contentWindow;
63
65
  if (win?.CladoRuntime) paint(win.document, win.CladoRuntime);
@@ -137,8 +139,310 @@
137
139
  if (deferredDesign) { const next=deferredDesign;deferredDesign=null;setDesign(next); }
138
140
  if (deferredDisplay) { const next=deferredDisplay;deferredDisplay=null;window.dispatchEvent(new MessageEvent('message', { source:parent,data:next })); }
139
141
  });
142
+ // ============ 多实例画布对象渲染、拖拽、内联编辑与双级长按菜单 ============
143
+ const worldEl = document.querySelector('#world');
144
+ const stageEl = document.querySelector('#stage');
145
+ const objDomMap = new Map();
146
+ let currentObjects = [];
147
+ let selectedObjKeys = new Set();
148
+
149
+ function getWorldTransform() {
150
+ const v = runtime.captureView?.() || {};
151
+ return { scale: v.scale || 1, tx: v.tx || 0, ty: v.ty || 0 };
152
+ }
153
+
154
+ function clientToWorld(cx, cy) {
155
+ const { scale, tx, ty } = getWorldTransform();
156
+ return { wx: Math.round((cx - tx) / scale), wy: Math.round((cy - ty) / scale) };
157
+ }
158
+
159
+ function paintObjectSelections() {
160
+ for (const [id, el] of objDomMap) {
161
+ el.classList.toggle('clado-obj-selected', selectedObjKeys.has('obj:' + id));
162
+ const obj = currentObjects.find(o => o.id === id);
163
+ const customMode = obj?.data?.selRenderMode === 'custom';
164
+ el.querySelectorAll('[data-subtarget="track"]').forEach(row => {
165
+ const key = `sub:${id}:${row.dataset.trackId}`;
166
+ const on = selectedObjKeys.has(key);
167
+ row.classList.toggle('sel-default', on && !customMode);
168
+ row.classList.toggle('sel-custom', on && customMode);
169
+ });
170
+ }
171
+ }
172
+
173
+ function renderCanvasObjects(list) {
174
+ if (!worldEl || !Array.isArray(list)) return;
175
+ currentObjects = list;
176
+ const keepIds = new Set(list.map(o => o.id));
177
+ for (const [id, el] of objDomMap) {
178
+ if (!keepIds.has(id)) {
179
+ el.remove();
180
+ objDomMap.delete(id);
181
+ }
182
+ }
183
+
184
+ for (const obj of list) {
185
+ let el = objDomMap.get(obj.id);
186
+ const isMusic = obj.kind === 'music';
187
+ if (!el) {
188
+ el = document.createElement('div');
189
+ el.dataset.objId = obj.id;
190
+ bindObjectInteractions(el);
191
+ worldEl.append(el);
192
+ objDomMap.set(obj.id, el);
193
+ }
194
+ el.className = 'clado-canvas-obj ' + (isMusic ? 'widget' : 'sticky');
195
+ el.dataset.kind = obj.kind || 'sticky';
196
+ el.dataset.tint = obj.tint || 'amber';
197
+ el.style.left = (obj.x ?? 180) + 'px';
198
+ el.style.top = (obj.y ?? 140) + 'px';
199
+ el.style.width = (isMusic ? (obj.data?.expanded ? 376 : 326) : (obj.width || 184)) + 'px';
200
+
201
+ if (isMusic) {
202
+ const tracks = Array.isArray(obj.data?.tracks) ? obj.data.tracks : [];
203
+ el.innerHTML = `
204
+ <div class="clado-obj-head" style="opacity:1;margin-bottom:6px;">
205
+ <div style="display:flex;align-items:center;gap:7px;">
206
+ <span style="width:21px;height:21px;border-radius:7px;background:linear-gradient(135deg,#6366f1,#ec4899);color:#fff;display:inline-flex;align-items:center;justify-content:center;font-size:11px;">♫</span>
207
+ <span style="font-size:12px;font-weight:650;">${escapeHtml(obj.title || 'Lo-Fi 专注播放器')}</span>
208
+ </div>
209
+ <span style="font-size:10px;opacity:.65;padding:1px 6px;border-radius:6px;background:var(--bg);">${obj.data?.expanded ? '放大' : '标准'}</span>
210
+ </div>
211
+ <div style="display:flex;flex-direction:column;gap:3px;">
212
+ ${tracks.map(t => `
213
+ <div class="clado-track-row" data-subtarget="track" data-track-id="${escapeHtml(t.id)}" data-track-title="${escapeHtml(t.title)}" data-liked="${Boolean(t.liked)}">
214
+ <span class="clado-eq-bars"><i></i><i></i><i></i></span>
215
+ <span style="font-size:12.5px;color:${t.liked ? '#f43f5e' : 'var(--fg-2)'};">${t.liked ? '♥' : '♡'}</span>
216
+ <span style="flex:1;min-width:0;font-size:12px;font-weight:560;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">${escapeHtml(t.title)}</span>
217
+ <span style="font-size:10px;color:var(--fg-2);">${escapeHtml(t.duration || '')}</span>
218
+ </div>`).join('')}
219
+ </div>`;
220
+ } else {
221
+ const activeEdit = el.querySelector('.clado-sticky-body[contenteditable="true"]');
222
+ if (!activeEdit) {
223
+ el.innerHTML = `
224
+ <div class="clado-obj-head">
225
+ <span>📌 #${escapeHtml(obj.id)}</span>
226
+ <span>多实例</span>
227
+ </div>
228
+ <div class="clado-sticky-body"></div>`;
229
+ el.querySelector('.clado-sticky-body').textContent = obj.text || '';
230
+ }
231
+ }
232
+ }
233
+ paintObjectSelections();
234
+ }
235
+
236
+ function escapeHtml(s) {
237
+ return String(s || '').replace(/[&<>"']/g, ch => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[ch]));
238
+ }
239
+
240
+ function startStickyInlineEdit(id) {
241
+ const el = objDomMap.get(id);
242
+ const body = el?.querySelector('.clado-sticky-body');
243
+ const obj = currentObjects.find(o => o.id === id);
244
+ if (!body || !obj) return;
245
+ body.contentEditable = 'true';
246
+ body.focus();
247
+ const sel = window.getSelection();
248
+ if (sel) {
249
+ const range = document.createRange();
250
+ range.selectNodeContents(body);
251
+ range.collapse(false);
252
+ sel.removeAllRanges();
253
+ sel.addRange(range);
254
+ }
255
+ const finishEdit = () => {
256
+ if (body.contentEditable !== 'true') return;
257
+ body.contentEditable = 'false';
258
+ const nextText = body.textContent.trim();
259
+ if (nextText !== obj.text) {
260
+ obj.text = nextText;
261
+ post('object-save', { object: { ...obj, text: nextText } });
262
+ }
263
+ };
264
+ body.onblur = finishEdit;
265
+ body.onkeydown = ev => {
266
+ if (ev.key === 'Escape' || (ev.key === 'Enter' && (ev.metaKey || ev.ctrlKey))) {
267
+ ev.preventDefault();
268
+ body.blur();
269
+ }
270
+ ev.stopPropagation();
271
+ };
272
+ }
273
+
274
+ function bindObjectInteractions(el) {
275
+ let dragging = false;
276
+ let moved = false;
277
+ let startX = 0;
278
+ let startY = 0;
279
+ let origX = 0;
280
+ let origY = 0;
281
+
282
+ el.addEventListener('pointerdown', ev => {
283
+ if (ev.button === 2) return;
284
+ if (ev.target.closest('[contenteditable="true"]')) {
285
+ ev.stopPropagation();
286
+ return;
287
+ }
288
+ ev.stopPropagation();
289
+ const obj = currentObjects.find(o => o.id === el.dataset.objId);
290
+ if (!obj) return;
291
+ dragging = true;
292
+ moved = false;
293
+ startX = ev.clientX;
294
+ startY = ev.clientY;
295
+ origX = obj.x || 0;
296
+ origY = obj.y || 0;
297
+ el.setPointerCapture?.(ev.pointerId);
298
+ });
299
+
300
+ el.addEventListener('pointermove', ev => {
301
+ if (!dragging) return;
302
+ const dx = ev.clientX - startX;
303
+ const dy = ev.clientY - startY;
304
+ if (Math.hypot(dx, dy) > 5) {
305
+ moved = true;
306
+ el.classList.add('dragging');
307
+ }
308
+ if (moved) {
309
+ const { scale } = getWorldTransform();
310
+ el.style.left = Math.round(origX + dx / scale) + 'px';
311
+ el.style.top = Math.round(origY + dy / scale) + 'px';
312
+ }
313
+ });
314
+
315
+ const endObjPtr = ev => {
316
+ if (!dragging) return;
317
+ dragging = false;
318
+ el.classList.remove('dragging');
319
+ const obj = currentObjects.find(o => o.id === el.dataset.objId);
320
+ if (moved && obj) {
321
+ obj.x = parseInt(el.style.left, 10) || 0;
322
+ obj.y = parseInt(el.style.top, 10) || 0;
323
+ post('object-save', { object: { id: obj.id, x: obj.x, y: obj.y } });
324
+ return;
325
+ }
326
+ // 轻点:若处于多选模式或按住 Ctrl/Cmd,则切换组件/子项选中态
327
+ const sub = ev.target.closest?.('[data-subtarget="track"]');
328
+ const apple = /Mac|iPhone|iPad/.test(navigator.platform);
329
+ const modKey = apple ? ev.metaKey : ev.ctrlKey;
330
+ if (selectedObjKeys.size > 0 || cardSelectionActive || modKey) {
331
+ if (sub && obj) {
332
+ post('object-toggle-select', {
333
+ key: `sub:${obj.id}:${sub.dataset.trackId}`,
334
+ item: { id: obj.id, kind: obj.kind, title: `${sub.dataset.trackTitle.split(' · ')[0]}`, pluginId: obj.pluginId, subtarget: sub.dataset.trackId },
335
+ });
336
+ } else if (obj) {
337
+ post('object-toggle-select', {
338
+ key: `obj:${obj.id}`,
339
+ item: { id: obj.id, kind: obj.kind, title: obj.title, pluginId: obj.pluginId },
340
+ });
341
+ }
342
+ }
343
+ };
344
+ el.addEventListener('pointerup', endObjPtr);
345
+ el.addEventListener('pointercancel', endObjPtr);
346
+
347
+ el.addEventListener('dblclick', ev => {
348
+ ev.stopPropagation();
349
+ const obj = currentObjects.find(o => o.id === el.dataset.objId);
350
+ if (obj?.kind === 'sticky') startStickyInlineEdit(obj.id);
351
+ });
352
+ }
353
+
354
+ function resolveContextTarget(targetEl, clientX, clientY) {
355
+ const { wx, wy } = clientToWorld(clientX, clientY);
356
+ const subTrack = targetEl?.closest?.('[data-subtarget="track"]');
357
+ const objEl = targetEl?.closest?.('.clado-canvas-obj');
358
+ if (subTrack && objEl) {
359
+ const obj = currentObjects.find(o => o.id === objEl.dataset.objId);
360
+ if (obj) {
361
+ return {
362
+ kind: 'subtarget',
363
+ object: obj,
364
+ trackId: subTrack.dataset.trackId,
365
+ trackTitle: subTrack.dataset.trackTitle,
366
+ liked: subTrack.dataset.liked === 'true',
367
+ wx,
368
+ wy,
369
+ };
370
+ }
371
+ }
372
+ if (objEl) {
373
+ const obj = currentObjects.find(o => o.id === objEl.dataset.objId);
374
+ if (obj) return { kind: 'object', object: obj, wx, wy };
375
+ }
376
+ const cardEl = targetEl?.closest?.('#world > .card');
377
+ if (cardEl && document.body.dataset.mode === 'cards') {
378
+ const path = runtime.selectionPath?.(cardEl);
379
+ const cardX = parseFloat(cardEl.style.left) || wx;
380
+ const cardY = parseFloat(cardEl.style.top) || wy;
381
+ const cardW = cardEl.offsetWidth || 360;
382
+ return {
383
+ kind: 'card',
384
+ node: path || null,
385
+ title: path?.[0] || cardEl.querySelector('.card-title h1')?.textContent || '卡片',
386
+ version: window.__CLADO_FILE_VERSION__,
387
+ wx: Math.round(cardX + cardW + 24),
388
+ wy: Math.round(cardY + 12),
389
+ };
390
+ }
391
+ return { kind: 'blank', wx, wy };
392
+ }
393
+
394
+ if (!window.__CLADO_OFFLINE__ && stageEl) {
395
+ stageEl.addEventListener('contextmenu', ev => {
396
+ if (document.body.dataset.mode !== 'cards' || chainOpen()) return;
397
+ ev.preventDefault();
398
+ post('context-menu', {
399
+ x: ev.clientX,
400
+ y: ev.clientY,
401
+ detail: resolveContextTarget(ev.target, ev.clientX, ev.clientY),
402
+ });
403
+ });
404
+
405
+ // 移动端/触屏 440ms 长按支持(避开标题长按链路提取与代码链接)
406
+ let lpTimer = null;
407
+ let lpStart = null;
408
+ const clearLp = () => { if (lpTimer) { clearTimeout(lpTimer); lpTimer = null; } };
409
+ stageEl.addEventListener('pointerdown', ev => {
410
+ clearLp();
411
+ if (ev.button === 2 || document.body.dataset.mode !== 'cards' || chainOpen()) return;
412
+ if (ev.target.closest?.('.card-title h1, .slice-head, a, button, dialog, [contenteditable="true"]')) return;
413
+ const target = ev.target;
414
+ lpStart = { x: ev.clientX, y: ev.clientY };
415
+ lpTimer = setTimeout(() => {
416
+ lpTimer = null;
417
+ post('context-menu', {
418
+ x: lpStart.x,
419
+ y: lpStart.y,
420
+ detail: resolveContextTarget(target, lpStart.x, lpStart.y),
421
+ });
422
+ }, 440);
423
+ }, true);
424
+ stageEl.addEventListener('pointermove', ev => {
425
+ if (lpTimer && lpStart && Math.hypot(ev.clientX - lpStart.x, ev.clientY - lpStart.y) > 7) clearLp();
426
+ }, true);
427
+ stageEl.addEventListener('pointerup', clearLp, true);
428
+ stageEl.addEventListener('pointercancel', clearLp, true);
429
+ }
430
+
140
431
  window.addEventListener('message', async event => {
141
432
  if (event.source !== parent) return;
433
+ if (event.data?.type === 'clado-workspace:objects') {
434
+ renderCanvasObjects(event.data.objects || []);
435
+ return;
436
+ }
437
+ if (event.data?.type === 'clado-workspace:object-selection') {
438
+ selectedObjKeys = new Set(event.data.keys || []);
439
+ paintObjectSelections();
440
+ return;
441
+ }
442
+ if (event.data?.type === 'clado-workspace:edit-sticky' && event.data.id) {
443
+ startStickyInlineEdit(event.data.id);
444
+ return;
445
+ }
142
446
  if (event.data?.type === 'clado-workspace:ai-touch') {
143
447
  runtime.highlightActivity?.(event.data);
144
448
  return;