@melt-js/comps-vue2 0.0.0-stage → 2026.11.6

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.
Files changed (47) hide show
  1. package/README.md +51 -2
  2. package/hel_dist/css/chunk-f032efe6.332a9b03.css +1 -0
  3. package/hel_dist/entry.js +37 -0
  4. package/hel_dist/favicon.ico +0 -0
  5. package/hel_dist/hel-meta.json +90 -0
  6. package/hel_dist/img/clouds_back.d1a6739b.jpg +0 -0
  7. package/hel_dist/img/clouds_front.b35c0bd4.jpg +0 -0
  8. package/hel_dist/img/clouds_left.4f5664b9.jpg +0 -0
  9. package/hel_dist/img/clouds_right.0e9ec637.jpg +0 -0
  10. package/hel_dist/img/clouds_top.31039c75.jpg +0 -0
  11. package/hel_dist/img/logo.82b9c7a5.png +0 -0
  12. package/hel_dist/index.html +1 -0
  13. package/hel_dist/js/app~493df0b3.39d7ea1f.js +16 -0
  14. package/hel_dist/js/app~493df0b3.39d7ea1f.js.map +1 -0
  15. package/hel_dist/js/chunk-f032efe6.d73e0465.js +2 -0
  16. package/hel_dist/js/chunk-f032efe6.d73e0465.js.map +1 -0
  17. package/hel_proxy/entry.js +37 -0
  18. package/hel_proxy_es/entry.js +28 -0
  19. package/package.json +88 -6
  20. package/scripts/check.js +13 -0
  21. package/scripts/meta.js +21 -0
  22. package/scripts/prepublishOnly.js +26 -0
  23. package/src/assets/logo.png +0 -0
  24. package/src/components/App.vue +32 -0
  25. package/src/components/Butterflies3d/assets/clouds_back.jpg +0 -0
  26. package/src/components/Butterflies3d/assets/clouds_front.jpg +0 -0
  27. package/src/components/Butterflies3d/assets/clouds_left.jpg +0 -0
  28. package/src/components/Butterflies3d/assets/clouds_right.jpg +0 -0
  29. package/src/components/Butterflies3d/assets/clouds_top.jpg +0 -0
  30. package/src/components/Butterflies3d/assets/wing-pattern.gif +0 -0
  31. package/src/components/Butterflies3d/index.vue +245 -0
  32. package/src/components/Butterflies3d/source/README.md +17 -0
  33. package/src/components/Butterflies3d/source/index.html +305 -0
  34. package/src/components/Butterflies3d/source/reset.css +1 -0
  35. package/src/components/Butterflies3d/source/style.css +831 -0
  36. package/src/components/Butterflies3d/style.css +295 -0
  37. package/src/components/HelloWorld.vue +37 -0
  38. package/src/components/TierRanking/index.vue +1012 -0
  39. package/src/components/TierRanking/ranking.css +650 -0
  40. package/src/components/TierRanking/source/ranking.css +657 -0
  41. package/src/components/TierRanking/source/ranking.html +1532 -0
  42. package/src/components/index.js +18 -0
  43. package/src/configs/subApp.js +9 -0
  44. package/src/entrance/libProperties.js +10 -0
  45. package/src/entrance/libTypes.ts +34 -0
  46. package/src/loadApp.js +20 -0
  47. package/src/main.js +25 -0
@@ -0,0 +1,1012 @@
1
+ <template>
2
+ <div class="ranking-board">
3
+ <button class="help-btn" type="button" title="使用说明" @click="showHelp = true">
4
+ ?
5
+ </button>
6
+
7
+ <div class="ranking-board-container">
8
+ <div ref="rankingContainer" class="ranking-container">
9
+ <div
10
+ v-for="tier in tiers"
11
+ :key="tier.id"
12
+ class="tier"
13
+ :class="{ 'fixed-height': fixedHeight, 'drag-over': dragOverTierId === tier.id }"
14
+ :data-tier-id="tier.id"
15
+ >
16
+ <div
17
+ class="tier-label"
18
+ :class="{ editing: editingTierId === tier.id }"
19
+ :style="{ backgroundColor: tier.color }"
20
+ @click="activateTier(tier, $event)"
21
+ >
22
+ <input
23
+ class="color-picker"
24
+ type="color"
25
+ :value="tier.color"
26
+ title="编辑颜色"
27
+ @click.stop
28
+ @input="onColorChange(tier, $event)"
29
+ >
30
+ <button
31
+ class="delete-tier-btn"
32
+ type="button"
33
+ title="删除该评级"
34
+ @click.stop="removeTier(tier)"
35
+ >
36
+ ×
37
+ </button>
38
+ <span
39
+ :ref="'tier-name-' + tier.id"
40
+ class="tier-name"
41
+ contenteditable="true"
42
+ spellcheck="false"
43
+ v-text="tier.name"
44
+ @input="onNameInput(tier, $event)"
45
+ @blur="commitName(tier)"
46
+ @keydown.enter.prevent="onNameEnter"
47
+ >{{ tier.name }}</span>
48
+ </div>
49
+
50
+ <div
51
+ :ref="'tier-content-' + tier.id"
52
+ class="tier-content"
53
+ :style="tierContentStyle(tier)"
54
+ >
55
+ <div
56
+ v-for="image in tier.images"
57
+ :key="image.id"
58
+ class="image-item"
59
+ :class="imageClass(tier)"
60
+ :style="imageStyle(tier)"
61
+ :data-image-id="image.id"
62
+ @mousedown="onPointerDown($event, 'tier', tier.id, image.id)"
63
+ @touchstart="onPointerDown($event, 'tier', tier.id, image.id)"
64
+ >
65
+ <img :src="image.src" :alt="image.name" draggable="false">
66
+ <div v-show="showImageNames" class="image-name-overlay">{{ image.name }}</div>
67
+ </div>
68
+ </div>
69
+ </div>
70
+ </div>
71
+
72
+ <div class="image-gallery">
73
+ <div
74
+ v-for="image in gallery"
75
+ :key="image.id"
76
+ class="image-item"
77
+ :data-image-id="image.id"
78
+ @mousedown="onPointerDown($event, 'gallery', null, image.id)"
79
+ @touchstart="onPointerDown($event, 'gallery', null, image.id)"
80
+ >
81
+ <img :src="image.src" :alt="image.name" draggable="false">
82
+ <div v-show="showImageNames" class="image-name-overlay">{{ image.name }}</div>
83
+ </div>
84
+
85
+ <div
86
+ class="upload-btn"
87
+ :class="{ 'drag-over': fileDragOver }"
88
+ title="点击或拖入图片"
89
+ @click="pickFiles"
90
+ @dragover.prevent="fileDragOver = true"
91
+ @dragleave.prevent="fileDragOver = false"
92
+ @drop.prevent="onFileDrop"
93
+ >
94
+ +
95
+ </div>
96
+
97
+ <div class="delete-zone" :class="{ 'drag-over': overDeleteZone }" title="把图片拖到这里删除">
98
+ -
99
+ </div>
100
+
101
+ <div class="button-container">
102
+ <div class="add-tier-btn" @click="addTier">+ 添加评级</div>
103
+ <div class="reset-tier-btn" @click="showReset = true">↻ 重置排行榜</div>
104
+ </div>
105
+ </div>
106
+
107
+ <input
108
+ ref="fileInput"
109
+ class="rb-file-input"
110
+ type="file"
111
+ accept="image/*"
112
+ multiple
113
+ @change="onFilesChange"
114
+ >
115
+ </div>
116
+
117
+ <!-- 使用说明 -->
118
+ <div v-if="showHelp" class="help-modal" @click.self="showHelp = false">
119
+ <div class="help-content">
120
+ <button class="close-help" type="button" @click="showHelp = false">×</button>
121
+ <h2>📝 使用说明</h2>
122
+ <ul>
123
+ <li>添加图片:点击"+"按钮或拖拽图片到该按钮区域</li>
124
+ <li>删除图片:将图片拖拽到"-"区域删除</li>
125
+ <li>放大图片:点击图片可放大,再次点击任意区域恢复</li>
126
+ <li>编辑评级:点击评级名称可编辑文字</li>
127
+ <li>编辑颜色:点击评级名称后右侧颜色按钮可编辑颜色</li>
128
+ <li>删除评级:点击评级名称后右侧删除按钮可删除评级</li>
129
+ <li>创建新评级:点击底部"+添加评级"按钮创建新的评级</li>
130
+ </ul>
131
+ <div class="toggle-container">
132
+ <label>
133
+ <input v-model="showImageNames" type="checkbox">
134
+ <span class="toggle-label">显示图片名称</span>
135
+ </label>
136
+ <label>
137
+ <input v-model="fixedHeight" type="checkbox">
138
+ <span class="toggle-label">固定评级高度</span>
139
+ </label>
140
+ </div>
141
+ <div class="save-image-container">
142
+ <button class="save-image-btn" type="button" @click="saveAsImage">💾 保存为图片</button>
143
+ </div>
144
+ </div>
145
+ </div>
146
+
147
+ <!-- 重置确认 -->
148
+ <div v-if="showReset" class="reset-modal" @click.self="showReset = false">
149
+ <div class="reset-content">
150
+ <h3>⚠️ 确认重置</h3>
151
+ <p>您确定要重置排行榜吗?<br>这将清除所有评级设置并移除所有图片,此操作无法撤销。</p>
152
+ <div class="reset-buttons">
153
+ <button class="reset-confirm-btn" type="button" @click="resetRanking">确认重置</button>
154
+ <button class="reset-cancel-btn" type="button" @click="showReset = false">取消</button>
155
+ </div>
156
+ </div>
157
+ </div>
158
+
159
+ <!-- 图片放大 -->
160
+ <div v-if="previewSrc" class="image-modal" @click="previewSrc = null">
161
+ <img :src="previewSrc" alt="放大的图片">
162
+ </div>
163
+
164
+ <!-- 拖拽时的跟随预览 -->
165
+ <div v-if="dragging" class="drag-preview" :style="dragPreviewStyle">
166
+ <img :src="dragPreviewSrc" alt="">
167
+ </div>
168
+
169
+ <div v-if="toast" class="rb-toast">{{ toast }}</div>
170
+ </div>
171
+ </template>
172
+
173
+ <script>
174
+ /**
175
+ * 从夯到拉排行榜
176
+ *
177
+ * 由 https://github.com/dayun-cloud/tools/blob/main/ranking.html 抽取的 Vue2 组件:
178
+ * - 原始 HTML / CSS 见同目录 source/,本组件把原来的 DOM 操作改写为数据驱动
179
+ * - 支持上传(压缩)、拖拽排序、跨评级拖拽、拖到删除区删除、点击放大
180
+ * - 评级可改名、改色、删除、新增、整体重置
181
+ * - 显示图片名称 / 固定评级高度 / html2canvas 导出图片
182
+ * - 默认把状态存到 IndexedDB(可通过 persist=false 关闭)
183
+ *
184
+ * 对外:props = persist / storageKey / maxImageSize / imageQuality
185
+ * events = change(状态变化,参数为可持久化快照)/ saved(导出图片后的 base64)
186
+ * methods = getState() / reset()
187
+ */
188
+ const DEFAULT_TIERS = [
189
+ { name: '夯', color: '#e74d5a' },
190
+ { name: '顶级', color: '#ff9f43' },
191
+ { name: '人上人', color: '#5a61e7' },
192
+ { name: 'NPC', color: '#2ed573' },
193
+ { name: '拉完了', color: '#747d8c' }
194
+ ];
195
+
196
+ const DEFAULT_TIER_COLOR = '#808080';
197
+ const IMAGE_SIZE = 120;
198
+ const IMAGE_GAP = 5;
199
+ const MIN_IMAGE_SIZE = 70;
200
+ const DB_STORE = 'state';
201
+ const DB_KEY = 'current';
202
+
203
+ const HTML2CANVAS_URLS = [
204
+ 'https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js',
205
+ 'https://unpkg.com/html2canvas@1.4.1/dist/html2canvas.min.js'
206
+ ];
207
+
208
+ // 把 rgb()/rgba() 颜色转成 <input type="color"> 能识别的 #rrggbb
209
+ function toHexColor(value, fallback) {
210
+ const color = typeof value === 'string' ? value.trim() : '';
211
+ if (!color) return fallback || DEFAULT_TIER_COLOR;
212
+ if (color.charAt(0) === '#') return color;
213
+ const match = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)/);
214
+ if (!match) return fallback || DEFAULT_TIER_COLOR;
215
+ const toPart = (num) => {
216
+ const hex = parseInt(num, 10).toString(16);
217
+ return hex.length === 1 ? '0' + hex : hex;
218
+ };
219
+ return '#' + toPart(match[1]) + toPart(match[2]) + toPart(match[3]);
220
+ }
221
+
222
+ export default {
223
+ name: 'TierRanking',
224
+ props: {
225
+ // 是否把排行榜状态存到 IndexedDB
226
+ persist: {
227
+ type: Boolean,
228
+ default: true
229
+ },
230
+ // IndexedDB 库名(多实例/多页面可区分)
231
+ storageKey: {
232
+ type: String,
233
+ default: 'tier-ranking'
234
+ },
235
+ // 上传图片压缩后的最大边长
236
+ maxImageSize: {
237
+ type: Number,
238
+ default: 800
239
+ },
240
+ // 压缩质量
241
+ imageQuality: {
242
+ type: Number,
243
+ default: 0.7
244
+ },
245
+ // 自定义 html2canvas 地址(不传则依次尝试内置 CDN)
246
+ html2canvasUrl: {
247
+ type: String,
248
+ default: ''
249
+ }
250
+ },
251
+ data() {
252
+ return {
253
+ tiers: [],
254
+ gallery: [],
255
+ editingTierId: null,
256
+ showHelp: false,
257
+ showReset: false,
258
+ previewSrc: null,
259
+ showImageNames: false,
260
+ fixedHeight: false,
261
+ tierSizes: {},
262
+ dragging: false,
263
+ dragPreviewSrc: '',
264
+ dragPoint: { x: 0, y: 0 },
265
+ dragOverTierId: null,
266
+ overDeleteZone: false,
267
+ fileDragOver: false,
268
+ toast: ''
269
+ };
270
+ },
271
+ computed: {
272
+ dragPreviewStyle() {
273
+ return {
274
+ left: this.dragPoint.x - IMAGE_SIZE / 2 + 'px',
275
+ top: this.dragPoint.y - IMAGE_SIZE / 2 + 'px'
276
+ };
277
+ }
278
+ },
279
+ watch: {
280
+ fixedHeight() {
281
+ this.$nextTick(() => {
282
+ if (this.fixedHeight) {
283
+ this.recalcSizes();
284
+ } else {
285
+ this.tierSizes = {};
286
+ }
287
+ });
288
+ this.scheduleSave();
289
+ },
290
+ showImageNames() {
291
+ this.scheduleSave();
292
+ }
293
+ },
294
+ created() {
295
+ // 以下均为非响应式的内部状态,避免多余的重新渲染
296
+ this.imageSeq = 0;
297
+ this.tierSeq = 0;
298
+ this.nameDrafts = {};
299
+ this.pointer = null;
300
+ this.pressTimer = null;
301
+ this.saveTimer = null;
302
+ this.resizeTimer = null;
303
+ this.toastTimer = null;
304
+ this.dbPromise = null;
305
+ this.html2canvasPromise = null;
306
+
307
+ this.tiers = this.createDefaultTiers();
308
+ },
309
+ mounted() {
310
+ document.addEventListener('mousemove', this.onDocMouseMove);
311
+ document.addEventListener('mouseup', this.onDocMouseUp);
312
+ document.addEventListener('touchmove', this.onDocTouchMove, { passive: false });
313
+ document.addEventListener('touchend', this.onDocTouchEnd);
314
+ document.addEventListener('click', this.onDocClick);
315
+ document.addEventListener('keydown', this.onKeydown);
316
+ window.addEventListener('resize', this.onWindowResize);
317
+
318
+ this.restoreState();
319
+ },
320
+ beforeDestroy() {
321
+ document.removeEventListener('mousemove', this.onDocMouseMove);
322
+ document.removeEventListener('mouseup', this.onDocMouseUp);
323
+ document.removeEventListener('touchmove', this.onDocTouchMove);
324
+ document.removeEventListener('touchend', this.onDocTouchEnd);
325
+ document.removeEventListener('click', this.onDocClick);
326
+ document.removeEventListener('keydown', this.onKeydown);
327
+ window.removeEventListener('resize', this.onWindowResize);
328
+
329
+ clearTimeout(this.pressTimer);
330
+ clearTimeout(this.saveTimer);
331
+ clearTimeout(this.resizeTimer);
332
+ clearTimeout(this.toastTimer);
333
+
334
+ if (this.dbPromise) {
335
+ this.dbPromise.then((db) => {
336
+ if (db) db.close();
337
+ }).catch(() => {});
338
+ }
339
+ },
340
+ methods: {
341
+ /* ---------------------------- 基础数据 ---------------------------- */
342
+
343
+ nextTierId() {
344
+ this.tierSeq += 1;
345
+ return 'tier-' + this.tierSeq;
346
+ },
347
+
348
+ nextImageId() {
349
+ this.imageSeq += 1;
350
+ return 'img-' + this.imageSeq;
351
+ },
352
+
353
+ createDefaultTiers() {
354
+ return DEFAULT_TIERS.map((tier) => ({
355
+ id: this.nextTierId(),
356
+ name: tier.name,
357
+ color: tier.color,
358
+ images: []
359
+ }));
360
+ },
361
+
362
+ createImage(src, name) {
363
+ return {
364
+ id: this.nextImageId(),
365
+ src: src,
366
+ name: name || '图片'
367
+ };
368
+ },
369
+
370
+ getRefEl(name) {
371
+ const ref = this.$refs[name];
372
+ if (Array.isArray(ref)) return ref[0] || null;
373
+ return ref || null;
374
+ },
375
+
376
+ findImage(source) {
377
+ if (!source) return null;
378
+ if (source.kind === 'gallery') {
379
+ return this.gallery.find((image) => image.id === source.imageId) || null;
380
+ }
381
+ const tier = this.tiers.find((item) => item.id === source.tierId);
382
+ if (!tier) return null;
383
+ return tier.images.find((image) => image.id === source.imageId) || null;
384
+ },
385
+
386
+ detachImage(source) {
387
+ if (!source) return null;
388
+ if (source.kind === 'gallery') {
389
+ const index = this.gallery.findIndex((image) => image.id === source.imageId);
390
+ if (index < 0) return null;
391
+ return this.gallery.splice(index, 1)[0];
392
+ }
393
+ const tier = this.tiers.find((item) => item.id === source.tierId);
394
+ if (!tier) return null;
395
+ const index = tier.images.findIndex((image) => image.id === source.imageId);
396
+ if (index < 0) return null;
397
+ return tier.images.splice(index, 1)[0];
398
+ },
399
+
400
+ removeImage(source) {
401
+ const image = this.detachImage(source);
402
+ if (!image) return;
403
+ if (this.fixedHeight) this.$nextTick(this.recalcSizes);
404
+ this.scheduleSave();
405
+ },
406
+
407
+ resolveInsertIndex(list, point) {
408
+ if (!point || !point.refId) return list.length;
409
+ const index = list.findIndex((image) => image.id === point.refId);
410
+ if (index < 0) return list.length;
411
+ return point.after ? index + 1 : index;
412
+ },
413
+
414
+ /* ---------------------------- 评级操作 ---------------------------- */
415
+
416
+ activateTier(tier, event) {
417
+ this.editingTierId = tier.id;
418
+ const nameEl = this.getRefEl('tier-name-' + tier.id);
419
+ if (!nameEl || !event) return;
420
+ if (event.target !== nameEl) {
421
+ this.$nextTick(() => nameEl.focus());
422
+ }
423
+ },
424
+
425
+ onNameInput(tier, event) {
426
+ // 只记录草稿,不写回 tier.name,避免 contenteditable 光标被重置
427
+ this.nameDrafts[tier.id] = event.target.textContent;
428
+ },
429
+
430
+ commitName(tier) {
431
+ const draft = this.nameDrafts[tier.id];
432
+ if (typeof draft !== 'string') return;
433
+ delete this.nameDrafts[tier.id];
434
+ const name = draft.replace(/\s+/g, ' ').trim() || '未命名';
435
+ if (name === tier.name) return;
436
+ tier.name = name;
437
+ this.scheduleSave();
438
+ },
439
+
440
+ onNameEnter(event) {
441
+ if (event && event.target && event.target.blur) event.target.blur();
442
+ },
443
+
444
+ onColorChange(tier, event) {
445
+ tier.color = toHexColor(event.target.value, DEFAULT_TIER_COLOR);
446
+ this.scheduleSave();
447
+ },
448
+
449
+ addTier() {
450
+ this.tiers.push({
451
+ id: this.nextTierId(),
452
+ name: '新评级' + (this.tiers.length + 1),
453
+ color: DEFAULT_TIER_COLOR,
454
+ images: []
455
+ });
456
+ if (this.fixedHeight) this.$nextTick(this.recalcSizes);
457
+ this.scheduleSave();
458
+ },
459
+
460
+ removeTier(tier) {
461
+ const index = this.tiers.findIndex((item) => item.id === tier.id);
462
+ if (index < 0) return;
463
+ // 该评级中的图片回到图片库
464
+ const images = this.tiers[index].images.slice();
465
+ this.tiers.splice(index, 1);
466
+ images.forEach((image) => {
467
+ this.gallery.push(image);
468
+ });
469
+ if (this.editingTierId === tier.id) this.editingTierId = null;
470
+ if (this.fixedHeight) this.$nextTick(this.recalcSizes);
471
+ this.scheduleSave();
472
+ },
473
+
474
+ resetRanking() {
475
+ this.gallery = [];
476
+ this.tiers = this.createDefaultTiers();
477
+ this.editingTierId = null;
478
+ this.showReset = false;
479
+ this.imageSeq = 0;
480
+ this.tierSizes = {};
481
+ this.scheduleSave();
482
+ },
483
+
484
+ /* ---------------------------- 图片上传 ---------------------------- */
485
+
486
+ pickFiles() {
487
+ const input = this.$refs.fileInput;
488
+ if (!input) return;
489
+ input.value = '';
490
+ input.click();
491
+ },
492
+
493
+ onFilesChange(event) {
494
+ const input = event.target;
495
+ const files = Array.from(input.files || []);
496
+ input.value = '';
497
+ this.ingestFiles(files);
498
+ },
499
+
500
+ onFileDrop(event) {
501
+ this.fileDragOver = false;
502
+ const transfer = event.dataTransfer;
503
+ const files = Array.from((transfer && transfer.files) || []);
504
+ this.ingestFiles(files);
505
+ },
506
+
507
+ ingestFiles(files) {
508
+ files
509
+ .filter((file) => file && file.type && file.type.indexOf('image/') === 0)
510
+ .forEach((file) => {
511
+ const reader = new FileReader();
512
+ reader.onload = (event) => {
513
+ this.compressImage(event.target.result)
514
+ .then((src) => {
515
+ this.gallery.push(this.createImage(src, this.displayNameFromFile(file.name)));
516
+ if (this.fixedHeight) this.$nextTick(this.recalcSizes);
517
+ this.scheduleSave();
518
+ })
519
+ .catch((error) => {
520
+ console.error('[TierRanking] 图片处理失败:', error);
521
+ this.showToast('图片处理失败:' + error.message);
522
+ });
523
+ };
524
+ reader.onerror = () => {
525
+ this.showToast('读取图片失败:' + file.name);
526
+ };
527
+ reader.readAsDataURL(file);
528
+ });
529
+ },
530
+
531
+ displayNameFromFile(fileName) {
532
+ let name = fileName || '图片';
533
+ const lastDot = name.lastIndexOf('.');
534
+ if (lastDot > 0) name = name.substring(0, lastDot);
535
+ return name || '图片';
536
+ },
537
+
538
+ compressImage(base64String) {
539
+ return new Promise((resolve, reject) => {
540
+ const image = new Image();
541
+ image.onload = () => {
542
+ const canvas = document.createElement('canvas');
543
+ const ctx = canvas.getContext('2d');
544
+ let width = image.width;
545
+ let height = image.height;
546
+ const maxSize = this.maxImageSize > 0 ? this.maxImageSize : 800;
547
+
548
+ if (width > maxSize || height > maxSize) {
549
+ if (width > height) {
550
+ height = Math.round((height / width) * maxSize);
551
+ width = maxSize;
552
+ } else {
553
+ width = Math.round((width / height) * maxSize);
554
+ height = maxSize;
555
+ }
556
+ }
557
+
558
+ canvas.width = width;
559
+ canvas.height = height;
560
+ ctx.drawImage(image, 0, 0, width, height);
561
+ resolve(canvas.toDataURL('image/jpeg', this.imageQuality));
562
+ };
563
+ image.onerror = () => reject(new Error('图片解码失败'));
564
+ image.src = base64String;
565
+ });
566
+ },
567
+
568
+ /* ---------------------------- 拖拽排序 ---------------------------- */
569
+
570
+ onPointerDown(event, kind, tierId, imageId) {
571
+ const isTouch = event.type === 'touchstart';
572
+ if (!isTouch && event.button !== 0) return;
573
+ if (isTouch && event.touches.length !== 1) return;
574
+
575
+ const point = isTouch ? event.touches[0] : event;
576
+ const element = event.currentTarget;
577
+
578
+ this.pointer = {
579
+ source: { kind: kind, tierId: tierId, imageId: imageId },
580
+ startX: point.clientX,
581
+ startY: point.clientY,
582
+ dragging: false,
583
+ element: element
584
+ };
585
+
586
+ clearTimeout(this.pressTimer);
587
+ this.pressTimer = setTimeout(() => {
588
+ if (this.pointer && !this.pointer.dragging && this.pointer.element) {
589
+ this.pointer.element.classList.add('is-pressed');
590
+ }
591
+ }, 150);
592
+
593
+ event.preventDefault();
594
+ },
595
+
596
+ onDocMouseMove(event) {
597
+ if (!this.pointer) return;
598
+ this.handlePointerMove(event.clientX, event.clientY);
599
+ },
600
+
601
+ onDocTouchMove(event) {
602
+ if (!this.pointer) return;
603
+ const touch = event.touches[0];
604
+ if (!touch) return;
605
+ event.preventDefault();
606
+ this.handlePointerMove(touch.clientX, touch.clientY);
607
+ },
608
+
609
+ handlePointerMove(x, y) {
610
+ const pointer = this.pointer;
611
+ if (!pointer) return;
612
+
613
+ if (!pointer.dragging) {
614
+ const deltaX = Math.abs(x - pointer.startX);
615
+ const deltaY = Math.abs(y - pointer.startY);
616
+ if (deltaX <= 5 && deltaY <= 5) return;
617
+
618
+ pointer.dragging = true;
619
+ clearTimeout(this.pressTimer);
620
+ this.pressTimer = null;
621
+ if (pointer.element) pointer.element.classList.remove('is-pressed');
622
+
623
+ const image = this.findImage(pointer.source);
624
+ this.dragPreviewSrc = image ? image.src : '';
625
+ this.dragging = true;
626
+ }
627
+
628
+ this.dragPoint = { x: x, y: y };
629
+ this.updateDropHighlight(x, y);
630
+ },
631
+
632
+ updateDropHighlight(x, y) {
633
+ const target = this.elementAt(x, y);
634
+ this.dragOverTierId = null;
635
+ this.overDeleteZone = false;
636
+ if (!target) return;
637
+ if (target.closest('.delete-zone')) {
638
+ this.overDeleteZone = true;
639
+ return;
640
+ }
641
+ const tierEl = target.closest('.tier');
642
+ if (tierEl) this.dragOverTierId = tierEl.getAttribute('data-tier-id');
643
+ },
644
+
645
+ onDocMouseUp(event) {
646
+ this.finishPointer(event.clientX, event.clientY);
647
+ },
648
+
649
+ onDocTouchEnd(event) {
650
+ if (!this.pointer) return;
651
+ const touch = event.changedTouches[0];
652
+ this.finishPointer(touch ? touch.clientX : 0, touch ? touch.clientY : 0);
653
+ },
654
+
655
+ finishPointer(x, y) {
656
+ const pointer = this.pointer;
657
+ if (!pointer) return;
658
+ this.pointer = null;
659
+ clearTimeout(this.pressTimer);
660
+ this.pressTimer = null;
661
+ if (pointer.element) pointer.element.classList.remove('is-pressed');
662
+
663
+ const deltaX = Math.abs(x - pointer.startX);
664
+ const deltaY = Math.abs(y - pointer.startY);
665
+
666
+ if (pointer.dragging) {
667
+ this.applyDrop(pointer.source, x, y);
668
+ this.dragging = false;
669
+ this.dragPreviewSrc = '';
670
+ this.dragOverTierId = null;
671
+ this.overDeleteZone = false;
672
+ if (this.fixedHeight) this.$nextTick(this.recalcSizes);
673
+ } else if (deltaX < 5 && deltaY < 5) {
674
+ // 没有拖拽,视为点击,放大图片
675
+ const image = this.findImage(pointer.source);
676
+ if (image) this.previewSrc = image.src;
677
+ }
678
+ },
679
+
680
+ applyDrop(source, x, y) {
681
+ const target = this.elementAt(x, y);
682
+ if (!target) return;
683
+
684
+ if (target.closest('.delete-zone')) {
685
+ this.removeImage(source);
686
+ return;
687
+ }
688
+
689
+ const tierEl = target.closest('.tier');
690
+ if (tierEl) {
691
+ const tierId = tierEl.getAttribute('data-tier-id');
692
+ const tier = this.tiers.find((item) => item.id === tierId);
693
+ if (!tier) return;
694
+ const point = this.resolveDropPoint(target, source.imageId, x);
695
+ if (!point) return;
696
+ const image = this.detachImage(source);
697
+ if (!image) return;
698
+ tier.images.splice(this.resolveInsertIndex(tier.images, point), 0, image);
699
+ this.scheduleSave();
700
+ return;
701
+ }
702
+
703
+ if (target.closest('.image-gallery')) {
704
+ const point = this.resolveDropPoint(target, source.imageId, x);
705
+ if (!point) return;
706
+ const image = this.detachImage(source);
707
+ if (!image) return;
708
+ this.gallery.splice(this.resolveInsertIndex(this.gallery, point), 0, image);
709
+ this.scheduleSave();
710
+ }
711
+ },
712
+
713
+ // 返回 null 表示拖回原位,不需要移动;否则给出「插到哪张图的前/后」
714
+ resolveDropPoint(target, sourceImageId, x) {
715
+ const imageEl = target.closest('.image-item');
716
+ if (!imageEl) return { refId: null, after: false };
717
+
718
+ const refId = imageEl.getAttribute('data-image-id');
719
+ if (!refId) return { refId: null, after: false };
720
+ if (refId === sourceImageId) return null;
721
+
722
+ const rect = imageEl.getBoundingClientRect();
723
+ return {
724
+ refId: refId,
725
+ after: (x - rect.left) > rect.width / 2
726
+ };
727
+ },
728
+
729
+ elementAt(x, y) {
730
+ if (typeof document === 'undefined' || !document.elementFromPoint) return null;
731
+ return document.elementFromPoint(x, y);
732
+ },
733
+
734
+ /* ---------------------------- 固定高度布局 ---------------------------- */
735
+
736
+ recalcSizes() {
737
+ if (!this.fixedHeight) {
738
+ if (Object.keys(this.tierSizes).length) this.tierSizes = {};
739
+ return;
740
+ }
741
+ const sizes = {};
742
+ this.tiers.forEach((tier) => {
743
+ if (!tier.images.length) return;
744
+ const contentEl = this.getRefEl('tier-content-' + tier.id);
745
+ if (!contentEl) return;
746
+
747
+ const containerWidth = contentEl.clientWidth - 10;
748
+ const maxPerRow = Math.floor(containerWidth / (IMAGE_SIZE + IMAGE_GAP));
749
+
750
+ if (maxPerRow <= 0) {
751
+ sizes[tier.id] = { mode: 'multi-row', width: MIN_IMAGE_SIZE };
752
+ return;
753
+ }
754
+ if (tier.images.length <= maxPerRow) {
755
+ sizes[tier.id] = { mode: 'single-row', width: IMAGE_SIZE };
756
+ return;
757
+ }
758
+
759
+ const availableWidth = containerWidth - (tier.images.length - 1) * IMAGE_GAP;
760
+ const width = Math.floor(availableWidth / tier.images.length);
761
+ sizes[tier.id] = width >= MIN_IMAGE_SIZE
762
+ ? { mode: 'adaptive', width: width }
763
+ : { mode: 'multi-row', width: MIN_IMAGE_SIZE };
764
+ });
765
+ this.tierSizes = sizes;
766
+ },
767
+
768
+ imageClass(tier) {
769
+ if (!this.fixedHeight) return null;
770
+ const size = this.tierSizes[tier.id];
771
+ return size ? size.mode : 'single-row';
772
+ },
773
+
774
+ imageStyle(tier) {
775
+ if (!this.fixedHeight) return null;
776
+ const size = this.tierSizes[tier.id];
777
+ if (!size) return null;
778
+ return { width: size.width + 'px', height: size.width + 'px' };
779
+ },
780
+
781
+ tierContentStyle(tier) {
782
+ if (!this.fixedHeight) return null;
783
+ const size = this.tierSizes[tier.id];
784
+ return { overflowY: size && size.mode === 'multi-row' ? 'auto' : 'hidden' };
785
+ },
786
+
787
+ onWindowResize() {
788
+ if (!this.fixedHeight) return;
789
+ clearTimeout(this.resizeTimer);
790
+ this.resizeTimer = setTimeout(() => {
791
+ this.recalcSizes();
792
+ }, 100);
793
+ },
794
+
795
+ /* ---------------------------- 全局交互 ---------------------------- */
796
+
797
+ onDocClick(event) {
798
+ const root = this.$el;
799
+ const target = event.target;
800
+ if (!root || !target || (target.nodeType !== 1)) return;
801
+ if (!root.contains(target)) {
802
+ this.editingTierId = null;
803
+ return;
804
+ }
805
+ // 点击评级标签(含颜色选择器、删除按钮)时保持编辑态
806
+ if (target.closest('.tier-label')) return;
807
+ this.editingTierId = null;
808
+ },
809
+
810
+ onKeydown(event) {
811
+ if (event.key !== 'Escape') return;
812
+ this.showHelp = false;
813
+ this.showReset = false;
814
+ this.previewSrc = null;
815
+ },
816
+
817
+ showToast(message) {
818
+ this.toast = message;
819
+ clearTimeout(this.toastTimer);
820
+ this.toastTimer = setTimeout(() => {
821
+ this.toast = '';
822
+ }, 2400);
823
+ },
824
+
825
+ /* ---------------------------- 保存为图片 ---------------------------- */
826
+
827
+ saveAsImage() {
828
+ const target = this.$refs.rankingContainer;
829
+ if (!target) return;
830
+ this.showHelp = false;
831
+ const options = {
832
+ scale: 2,
833
+ useCORS: true,
834
+ allowTaint: true,
835
+ backgroundColor: '#1a1a1a',
836
+ scrollX: 0,
837
+ scrollY: 0,
838
+ windowWidth: document.documentElement.scrollWidth,
839
+ windowHeight: document.documentElement.scrollHeight
840
+ };
841
+
842
+ this.loadHtml2Canvas()
843
+ .then((html2canvas) => html2canvas(target, options))
844
+ .then((canvas) => {
845
+ const dataUrl = canvas.toDataURL('image/png');
846
+ const blob = this.dataURLToBlob(dataUrl);
847
+ const url = URL.createObjectURL(blob);
848
+ const link = document.createElement('a');
849
+ link.href = url;
850
+ link.download = '从夯到拉排行榜.png';
851
+ document.body.appendChild(link);
852
+ link.click();
853
+ document.body.removeChild(link);
854
+ setTimeout(() => URL.revokeObjectURL(url), 10000);
855
+ this.showToast('已保存为图片');
856
+ this.$emit('saved', dataUrl);
857
+ })
858
+ .catch((error) => {
859
+ console.error('[TierRanking] 截图失败:', error);
860
+ this.showToast('截图失败:' + (error && error.message ? error.message : error));
861
+ });
862
+ },
863
+
864
+ loadHtml2Canvas() {
865
+ if (window.html2canvas) return Promise.resolve(window.html2canvas);
866
+ if (!this.html2canvasPromise) {
867
+ this.html2canvasPromise = this.loadScript(HTML2CANVAS_URLS[0])
868
+ .catch(() => this.loadScript(HTML2CANVAS_URLS[1]))
869
+ .then(() => {
870
+ if (!window.html2canvas) throw new Error('html2canvas 加载后未生效');
871
+ return window.html2canvas;
872
+ })
873
+ .catch((error) => {
874
+ this.html2canvasPromise = null;
875
+ throw error;
876
+ });
877
+ }
878
+ return this.html2canvasPromise;
879
+ },
880
+
881
+ loadScript(src) {
882
+ return new Promise((resolve, reject) => {
883
+ const script = document.createElement('script');
884
+ script.src = src;
885
+ script.async = true;
886
+ script.onload = () => resolve();
887
+ script.onerror = () => reject(new Error('脚本加载失败:' + src));
888
+ document.head.appendChild(script);
889
+ });
890
+ },
891
+
892
+ dataURLToBlob(dataURL) {
893
+ const parts = dataURL.split(',');
894
+ const mime = parts[0].match(/:(.*?);/)[1];
895
+ const bstr = atob(parts[1]);
896
+ let n = bstr.length;
897
+ const u8arr = new Uint8Array(n);
898
+ while (n--) {
899
+ u8arr[n] = bstr.charCodeAt(n);
900
+ }
901
+ return new Blob([u8arr], { type: mime });
902
+ },
903
+
904
+ /* ---------------------------- 状态持久化 ---------------------------- */
905
+
906
+ getState() {
907
+ return {
908
+ tiers: this.tiers.map((tier) => ({
909
+ name: tier.name,
910
+ color: tier.color,
911
+ images: tier.images.map((image) => ({ src: image.src, name: image.name }))
912
+ })),
913
+ gallery: this.gallery.map((image) => ({ src: image.src, name: image.name })),
914
+ showImageNames: !!this.showImageNames,
915
+ fixedHeight: !!this.fixedHeight
916
+ };
917
+ },
918
+
919
+ applyState(state) {
920
+ if (!state) return;
921
+ if (Array.isArray(state.tiers) && state.tiers.length) {
922
+ this.tiers = state.tiers.map((tier) => ({
923
+ id: this.nextTierId(),
924
+ name: tier.name || '未命名',
925
+ color: toHexColor(tier.color, DEFAULT_TIER_COLOR),
926
+ images: (tier.images || [])
927
+ .filter((image) => image && image.src)
928
+ .map((image) => this.createImage(image.src, image.name))
929
+ }));
930
+ }
931
+ this.gallery = (state.gallery || [])
932
+ .filter((image) => image && image.src)
933
+ .map((image) => this.createImage(image.src, image.name));
934
+ this.showImageNames = !!state.showImageNames;
935
+ this.fixedHeight = !!state.fixedHeight;
936
+ this.$nextTick(this.recalcSizes);
937
+ },
938
+
939
+ openDB() {
940
+ if (!this.persist || typeof window === 'undefined' || !window.indexedDB) {
941
+ return Promise.resolve(null);
942
+ }
943
+ if (!this.dbPromise) {
944
+ this.dbPromise = new Promise((resolve, reject) => {
945
+ const request = window.indexedDB.open(this.storageKey + '-db', 1);
946
+ request.onupgradeneeded = (event) => {
947
+ const db = event.target.result;
948
+ if (!db.objectStoreNames.contains(DB_STORE)) {
949
+ db.createObjectStore(DB_STORE);
950
+ }
951
+ };
952
+ request.onsuccess = () => resolve(request.result);
953
+ request.onerror = () => reject(request.error || new Error('IndexedDB 打开失败'));
954
+ });
955
+ }
956
+ return this.dbPromise;
957
+ },
958
+
959
+ restoreState() {
960
+ if (!this.persist) return;
961
+ this.openDB()
962
+ .then((db) => {
963
+ if (!db) return null;
964
+ return new Promise((resolve, reject) => {
965
+ const request = db.transaction([DB_STORE], 'readonly').objectStore(DB_STORE).get(DB_KEY);
966
+ request.onsuccess = () => resolve(request.result || null);
967
+ request.onerror = () => reject(request.error);
968
+ });
969
+ })
970
+ .then((state) => {
971
+ if (state) this.applyState(state);
972
+ })
973
+ .catch((error) => {
974
+ console.warn('[TierRanking] 恢复本地数据失败:', error);
975
+ });
976
+ },
977
+
978
+ scheduleSave() {
979
+ this.$emit('change', this.getState());
980
+ if (!this.persist) return;
981
+ clearTimeout(this.saveTimer);
982
+ this.saveTimer = setTimeout(() => {
983
+ this.saveState();
984
+ }, 300);
985
+ },
986
+
987
+ saveState() {
988
+ const state = this.getState();
989
+ this.openDB()
990
+ .then((db) => {
991
+ if (!db) return;
992
+ return new Promise((resolve, reject) => {
993
+ const transaction = db.transaction([DB_STORE], 'readwrite');
994
+ const request = transaction.objectStore(DB_STORE).put(state, DB_KEY);
995
+ request.onsuccess = () => resolve();
996
+ request.onerror = () => reject(request.error);
997
+ });
998
+ })
999
+ .catch((error) => {
1000
+ console.warn('[TierRanking] 保存失败:', error);
1001
+ this.showToast('保存失败,可能是浏览器存储空间不足');
1002
+ });
1003
+ },
1004
+
1005
+ reset() {
1006
+ this.resetRanking();
1007
+ }
1008
+ }
1009
+ };
1010
+ </script>
1011
+
1012
+ <style scoped src="./ranking.css"></style>