@5even7/dlc-ui 0.2.2 → 0.2.4

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/dist/vue3.cjs CHANGED
@@ -418,9 +418,13 @@ function getPreset(kind, ref) {
418
418
  var list = kind === 'capsule' ? CAPSULE_PRESETS : kind === 'progress' ? PROGRESS_PRESETS : null;
419
419
  if (!list) throw new Error("Unknown preset kind: ".concat(kind, " (use \"capsule\" or \"progress\")"));
420
420
  var key = String(ref).trim().toLowerCase();
421
- var found = list.find(function (preset) {
422
- return preset.name.toLowerCase() === key;
423
- });
421
+ var found = null;
422
+ for (var index = 0; index < list.length; index += 1) {
423
+ if (list[index].name.toLowerCase() === key) {
424
+ found = list[index];
425
+ break;
426
+ }
427
+ }
424
428
  if (!found) throw new Error("Unknown ".concat(kind, " preset: ").concat(ref));
425
429
  return found;
426
430
  }
@@ -441,8 +445,8 @@ var DEFAULTS = {
441
445
  min: 0,
442
446
  max: 100,
443
447
  draggable: true,
444
- keyboard: true,
445
448
  disabled: false,
449
+ readonly: false,
446
450
  valueSuffix: '%',
447
451
  edgeStyle: 'flow',
448
452
  quality: 'auto',
@@ -631,7 +635,7 @@ function normalizeRgbArgs(args) {
631
635
  if (parts.length < 3) return null;
632
636
  var to255 = function to255(value) {
633
637
  if (typeof value !== 'string' || !value) return null;
634
- if (value.endsWith('%')) return clampChannel(parseFloat(value) / 100 * 255);
638
+ if (value.charAt(value.length - 1) === '%') return clampChannel(parseFloat(value) / 100 * 255);
635
639
  var number = Number(value);
636
640
  return Number.isFinite(number) ? clampChannel(number) : null;
637
641
  };
@@ -639,9 +643,11 @@ function normalizeRgbArgs(args) {
639
643
  var green = to255(parts[1]);
640
644
  var blue = to255(parts[2]);
641
645
  if (red === null || green === null || blue === null) return null;
642
- return "#".concat([red, green, blue].map(function (n) {
643
- return n.toString(16).padStart(2, '0');
644
- }).join(''));
646
+ var hex = function hex(n) {
647
+ var text = n.toString(16);
648
+ return text.length === 1 ? "0".concat(text) : text;
649
+ };
650
+ return "#".concat(hex(red)).concat(hex(green)).concat(hex(blue));
645
651
  }
646
652
 
647
653
  /**
@@ -906,6 +912,7 @@ var FallbackRenderer = /*#__PURE__*/function () {
906
912
  this.preset = preset;
907
913
  this.context = canvas.getContext('2d');
908
914
  this.visible = true;
915
+ this.timePhase = 0;
909
916
  }
910
917
  return _createClass(FallbackRenderer, [{
911
918
  key: "resize",
@@ -926,7 +933,7 @@ var FallbackRenderer = /*#__PURE__*/function () {
926
933
  var _this$canvas = this.canvas,
927
934
  width = _this$canvas.width,
928
935
  height = _this$canvas.height;
929
- var t = time * (this.preset.speed || 1);
936
+ var t = (time + this.timePhase) * (this.preset.speed || 1);
930
937
  var phase = (this.preset.seed || 0) * 0.7;
931
938
  var gradient = ctx.createLinearGradient(0, 0, width, height);
932
939
  gradient.addColorStop(0, this.preset.colors[0]);
@@ -963,7 +970,10 @@ var FallbackRenderer = /*#__PURE__*/function () {
963
970
  }, {
964
971
  key: "randomize",
965
972
  value: function randomize() {
966
- if (this.preset) this.preset.seed = Math.random() * 100;
973
+ if (this.preset) {
974
+ this.preset.seed = Math.random() * 100;
975
+ this.timePhase = Math.random() * Math.PI * 2;
976
+ }
967
977
  }
968
978
  }, {
969
979
  key: "setMouseColor",
@@ -1209,6 +1219,8 @@ function createCapsule(container) {
1209
1219
  }
1210
1220
  if (merged.textRatio !== undefined) {
1211
1221
  root.style.setProperty('--hj-text-width', "".concat(merged.textRatio, "%"));
1222
+ var userMinWidth = merged.cssVars && merged.cssVars['--hj-text-min-width'];
1223
+ if (merged.textRatio === 0) root.style.setProperty('--hj-text-min-width', '0');else if (userMinWidth) root.style.setProperty('--hj-text-min-width', userMinWidth);else root.style.removeProperty('--hj-text-min-width');
1212
1224
  }
1213
1225
  renderer.resize();
1214
1226
  };
@@ -1332,6 +1344,8 @@ function createCapsule(container) {
1332
1344
  dirty.textRatio = true;
1333
1345
  merged.textRatio = value;
1334
1346
  root.style.setProperty('--hj-text-width', "".concat(value, "%"));
1347
+ var userMinWidth = merged.cssVars && merged.cssVars['--hj-text-min-width'];
1348
+ if (value === 0) root.style.setProperty('--hj-text-min-width', '0');else if (userMinWidth) root.style.setProperty('--hj-text-min-width', userMinWidth);else root.style.removeProperty('--hj-text-min-width');
1335
1349
  return this;
1336
1350
  },
1337
1351
  setCssVars: function setCssVars(vars) {
@@ -1350,8 +1364,14 @@ function createCapsule(container) {
1350
1364
  return this;
1351
1365
  },
1352
1366
  setSize: function setSize(width, height) {
1353
- if (width !== undefined) merged.width = width;
1354
- if (height !== undefined) merged.height = height;
1367
+ if (width !== undefined) {
1368
+ parseSize(width); // 非法尺寸直接抛错前先校验,避免污染内部状态
1369
+ merged.width = width;
1370
+ }
1371
+ if (height !== undefined) {
1372
+ parseSize(height);
1373
+ merged.height = height;
1374
+ }
1355
1375
  applySize();
1356
1376
  return this;
1357
1377
  },
@@ -1380,8 +1400,12 @@ function createCapsule(container) {
1380
1400
  renderer.dispose();
1381
1401
  root.remove();
1382
1402
  },
1383
- textRatio: merged.textRatio,
1384
- cssVars: merged.cssVars,
1403
+ get textRatio() {
1404
+ return merged.textRatio;
1405
+ },
1406
+ get cssVars() {
1407
+ return merged.cssVars;
1408
+ },
1385
1409
  dirty: dirty
1386
1410
  };
1387
1411
  }
@@ -1677,8 +1701,12 @@ function drawCloud(context, x, y, radiusX, radiusY, color, alpha) {
1677
1701
  context.translate(x, y);
1678
1702
  context.scale(1, radiusY / radiusX);
1679
1703
  var gradient = context.createRadialGradient(0, 0, 0, 0, 0, radiusX);
1680
- gradient.addColorStop(0, "".concat(color).concat(Math.round(alpha * 255).toString(16).padStart(2, '0')));
1681
- gradient.addColorStop(0.46, "".concat(color).concat(Math.round(alpha * 0.42 * 255).toString(16).padStart(2, '0')));
1704
+ var alphaHex = function alphaHex(value) {
1705
+ var text = Math.round(value).toString(16);
1706
+ return text.length === 1 ? "0".concat(text) : text;
1707
+ };
1708
+ gradient.addColorStop(0, "".concat(color).concat(alphaHex(alpha * 255)));
1709
+ gradient.addColorStop(0.46, "".concat(color).concat(alphaHex(alpha * 0.42 * 255)));
1682
1710
  gradient.addColorStop(1, "".concat(color, "00"));
1683
1711
  context.fillStyle = gradient;
1684
1712
  context.fillRect(-radiusX, -radiusX, radiusX * 2, radiusX * 2);
@@ -2128,6 +2156,11 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2128
2156
  }, {
2129
2157
  key: "setRange",
2130
2158
  value: function setRange(min, max) {
2159
+ if (min > max) {
2160
+ var swap = min;
2161
+ min = max;
2162
+ max = swap;
2163
+ }
2131
2164
  this.min = min;
2132
2165
  this.max = max;
2133
2166
  var clamped = clamp(this.value, this.min, this.max);
@@ -2263,6 +2296,14 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2263
2296
  value: function beginDrag(event) {
2264
2297
  if (event.button !== undefined && event.button !== 0) return;
2265
2298
  event.preventDefault();
2299
+ // preventDefault 会吞掉点击聚焦,主动聚焦让键盘方向键立即可用。
2300
+ try {
2301
+ this.root.focus({
2302
+ preventScroll: true
2303
+ });
2304
+ } catch (_unused) {
2305
+ this.root.focus();
2306
+ }
2266
2307
  this.dragging = true;
2267
2308
  this.pendingPointer = null;
2268
2309
  this._dragRaf = 0;
@@ -2270,7 +2311,7 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2270
2311
  try {
2271
2312
  var _this$root$setPointer, _this$root;
2272
2313
  (_this$root$setPointer = (_this$root = this.root).setPointerCapture) === null || _this$root$setPointer === void 0 || _this$root$setPointer.call(_this$root, event.pointerId);
2273
- } catch (_unused) {}
2314
+ } catch (_unused2) {}
2274
2315
  this.emitter.emit('dragstart', {
2275
2316
  value: this.value
2276
2317
  });
@@ -2310,7 +2351,7 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2310
2351
  if ((event === null || event === void 0 ? void 0 : event.pointerId) !== undefined && (_this$root$hasPointer = (_this$root2 = this.root).hasPointerCapture) !== null && _this$root$hasPointer !== void 0 && _this$root$hasPointer.call(_this$root2, event.pointerId)) {
2311
2352
  this.root.releasePointerCapture(event.pointerId);
2312
2353
  }
2313
- } catch (_unused2) {}
2354
+ } catch (_unused3) {}
2314
2355
  this.emitter.emit('dragend', {
2315
2356
  value: this.value
2316
2357
  });
@@ -2337,32 +2378,6 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2337
2378
  this.root.addEventListener('pointerup', this.handlers.pointerup);
2338
2379
  this.root.addEventListener('pointercancel', this.handlers.pointercancel);
2339
2380
  }
2340
- if (this.options.keyboard !== false) {
2341
- this.handlers.keydown = function (event) {
2342
- var step = (_this3.max - _this3.min) * 0.02;
2343
- var actions = {
2344
- ArrowLeft: -step,
2345
- ArrowDown: -step,
2346
- ArrowRight: step,
2347
- ArrowUp: step,
2348
- PageDown: -step * 5,
2349
- PageUp: step * 5
2350
- };
2351
- if (event.key === 'Home') {
2352
- event.preventDefault();
2353
- _this3.setProgress(_this3.min, 'keyboard');
2354
- } else if (event.key === 'End') {
2355
- event.preventDefault();
2356
- _this3.setProgress(_this3.max, 'keyboard');
2357
- } else if (actions[event.key]) {
2358
- event.preventDefault();
2359
- _this3.setProgress(_this3.value + actions[event.key], 'keyboard');
2360
- } else {
2361
- return;
2362
- }
2363
- };
2364
- this.root.addEventListener('keydown', this.handlers.keydown);
2365
- }
2366
2381
  }
2367
2382
  }, {
2368
2383
  key: "update",
@@ -2409,7 +2424,7 @@ var ProgressCapsuleController = /*#__PURE__*/function () {
2409
2424
  * Mount a fluid progress capsule into `container`.
2410
2425
  *
2411
2426
  * Options: preset (id/code/name or object), width, height, value, min, max,
2412
- * draggable, keyboard, colors, edgeStyle, textRatio (0-100, text region
2427
+ * draggable, colors, edgeStyle, textRatio (0-100, text region
2413
2428
  * width in percent), text (HTML string or DOM nodes for the text slot),
2414
2429
  * colorContent (HTML string or DOM nodes for the color slot),
2415
2430
  * showValue (show/hide the right-side percentage), quality,
@@ -2423,6 +2438,11 @@ function createProgressCapsule(container) {
2423
2438
  }
2424
2439
  var preset = _objectSpread2({}, getPreset('progress', (_options$preset = options.preset) !== null && _options$preset !== void 0 ? _options$preset : '星火'));
2425
2440
  var merged = normalizeOptions(DEFAULTS.progress, preset, options);
2441
+ if (merged.min > merged.max) {
2442
+ var swap = merged.min;
2443
+ merged.min = merged.max;
2444
+ merged.max = swap;
2445
+ }
2426
2446
  var normalizedColors = merged.colors.map(normalizeColor);
2427
2447
  if (normalizedColors.every(Boolean)) preset.colors = normalizedColors;
2428
2448
  preset.edgeStyle = merged.edgeStyle;
@@ -2431,8 +2451,9 @@ function createProgressCapsule(container) {
2431
2451
  // Vue 的裸布尔属性(<ProgressCapsule disabled />)会传成空字符串,
2432
2452
  // 这里统一按 true 处理。
2433
2453
  var disabled = merged.disabled === true || merged.disabled === '' || merged.disabled === 'true';
2434
- var effectiveDraggable = disabled ? false : merged.draggable !== false;
2435
- var effectiveKeyboard = disabled ? false : merged.keyboard !== false;
2454
+ var readonly = merged.readonly === true || merged.readonly === '' || merged.readonly === 'true';
2455
+ var locked = disabled || readonly;
2456
+ var effectiveDraggable = locked ? false : merged.draggable !== false;
2436
2457
  var dirty = {
2437
2458
  preset: false,
2438
2459
  colors: false,
@@ -2444,12 +2465,16 @@ function createProgressCapsule(container) {
2444
2465
  var root = document.createElement('div');
2445
2466
  root.className = 'hj-capsule-root hj-progress-root';
2446
2467
  root.setAttribute('role', 'slider');
2447
- root.setAttribute('tabindex', '0');
2448
2468
  root.setAttribute('data-draggable', String(effectiveDraggable));
2449
2469
  if (disabled) {
2450
2470
  root.setAttribute('aria-disabled', 'true');
2451
2471
  root.classList.add('is-disabled');
2452
2472
  }
2473
+ if (readonly) {
2474
+ root.setAttribute('aria-readonly', 'true');
2475
+ root.classList.add('is-readonly');
2476
+ }
2477
+ if (locked) root.setAttribute('tabindex', '-1');
2453
2478
  root.setAttribute('aria-valuemin', String((_merged$min = merged.min) !== null && _merged$min !== void 0 ? _merged$min : 0));
2454
2479
  root.setAttribute('aria-valuemax', String((_merged$max = merged.max) !== null && _merged$max !== void 0 ? _merged$max : 100));
2455
2480
  root.setAttribute('aria-valuenow', String(preset.initialProgress));
@@ -2461,7 +2486,6 @@ function createProgressCapsule(container) {
2461
2486
  }));
2462
2487
  };
2463
2488
  updateAria();
2464
- if (!effectiveKeyboard) root.setAttribute('tabindex', '-1');
2465
2489
  var canvas = document.createElement('canvas');
2466
2490
  canvas.className = 'hj-progress-canvas';
2467
2491
  canvas.setAttribute('aria-hidden', 'true');
@@ -2528,8 +2552,7 @@ function createProgressCapsule(container) {
2528
2552
  preset: preset,
2529
2553
  emitter: emitter,
2530
2554
  options: _objectSpread2(_objectSpread2({}, merged), {}, {
2531
- draggable: effectiveDraggable,
2532
- keyboard: effectiveKeyboard
2555
+ draggable: effectiveDraggable
2533
2556
  }),
2534
2557
  copy: copy,
2535
2558
  dirty: dirty
@@ -2689,8 +2712,14 @@ function createProgressCapsule(container) {
2689
2712
  return this;
2690
2713
  },
2691
2714
  setSize: function setSize(width, height) {
2692
- if (width !== undefined) merged.width = width;
2693
- if (height !== undefined) merged.height = height;
2715
+ if (width !== undefined) {
2716
+ parseSize(width);
2717
+ merged.width = width;
2718
+ }
2719
+ if (height !== undefined) {
2720
+ parseSize(height);
2721
+ merged.height = height;
2722
+ }
2694
2723
  applySize();
2695
2724
  controller.resizeCanvas();
2696
2725
  return this;
@@ -2721,8 +2750,12 @@ function createProgressCapsule(container) {
2721
2750
  controller.dispose();
2722
2751
  root.remove();
2723
2752
  },
2724
- textRatio: merged.textRatio,
2725
- cssVars: merged.cssVars,
2753
+ get textRatio() {
2754
+ return merged.textRatio;
2755
+ },
2756
+ get cssVars() {
2757
+ return merged.cssVars;
2758
+ },
2726
2759
  dirty: dirty
2727
2760
  };
2728
2761
  }
@@ -2927,9 +2960,15 @@ function createColorBackground(host) {
2927
2960
  renderer.dispose();
2928
2961
  layer.remove();
2929
2962
  },
2930
- opacity: merged.opacity,
2931
- fallbackColor: merged.fallbackColor,
2932
- mouseColor: merged.mouseColor,
2963
+ get opacity() {
2964
+ return merged.opacity;
2965
+ },
2966
+ get fallbackColor() {
2967
+ return merged.fallbackColor;
2968
+ },
2969
+ get mouseColor() {
2970
+ return merged.mouseColor;
2971
+ },
2933
2972
  dirty: dirty
2934
2973
  };
2935
2974
  }
@@ -2982,17 +3021,23 @@ function makeWrapper(_ref) {
2982
3021
  this.applyAttrs();
2983
3022
  },
2984
3023
  recreate: function recreate() {
3024
+ var _this2 = this;
2985
3025
  var extraState = this.runtimeState();
2986
3026
  if (this.controller) this.controller.dispose();
2987
3027
  this.mountController(extraState);
3028
+ // 结构属性连续变化时,插槽内容可能在重建后才被 Vue 写入容器,
3029
+ // 补一次搬运确保不丢。
3030
+ this.$nextTick(function () {
3031
+ return _this2.moveSlots();
3032
+ });
2988
3033
  },
2989
3034
  scheduleRecreate: function scheduleRecreate() {
2990
- var _this2 = this;
3035
+ var _this3 = this;
2991
3036
  if (this._recreateQueued) return;
2992
3037
  this._recreateQueued = true;
2993
3038
  this.$nextTick(function () {
2994
- _this2._recreateQueued = false;
2995
- _this2.recreate();
3039
+ _this3._recreateQueued = false;
3040
+ _this3.recreate();
2996
3041
  });
2997
3042
  },
2998
3043
  moveSlots: function moveSlots() {
@@ -3000,22 +3045,21 @@ function makeWrapper(_ref) {
3000
3045
  var root = this.controller.element;
3001
3046
  var textLayer = root.querySelector('.hj-capsule-text, .hj-progress-text');
3002
3047
  var visualLayer = root.querySelector('.hj-capsule-visual, .hj-progress-visual');
3003
- var move = function move(wrapper, layer) {
3004
- if (!wrapper || !layer) return;
3005
- for (var _i = 0, _Array$from = Array.from(wrapper.childNodes); _i < _Array$from.length; _i++) {
3006
- var node = _Array$from[_i];
3007
- layer.appendChild(node);
3008
- }
3009
- };
3010
3048
  var refs = this.$refs || {};
3011
- move(refs.slotText, textLayer);
3012
- move(refs.slotColor, visualLayer);
3049
+ // 把插槽容器整体搬进对应区域,而不是搬子节点:Vue 重渲染/重建组件时
3050
+ // 内容始终留在区域内,不会丢(子节点搬运会在 recreate 时序下丢失)。
3051
+ if (refs.slotText && textLayer && refs.slotText.parentElement !== textLayer) {
3052
+ textLayer.appendChild(refs.slotText);
3053
+ }
3054
+ if (refs.slotColor && visualLayer && refs.slotColor.parentElement !== visualLayer) {
3055
+ visualLayer.appendChild(refs.slotColor);
3056
+ }
3013
3057
  },
3014
3058
  applyAttrs: function applyAttrs() {
3015
3059
  if (!this.controller || !this.controller.element || !this.$attrs) return;
3016
3060
  var root = this.controller.element;
3017
- for (var _i2 = 0, _Object$keys = Object.keys(this.$attrs); _i2 < _Object$keys.length; _i2++) {
3018
- var key = _Object$keys[_i2];
3061
+ for (var _i = 0, _Object$keys = Object.keys(this.$attrs); _i < _Object$keys.length; _i++) {
3062
+ var key = _Object$keys[_i];
3019
3063
  if (key === 'class' || key === 'style') continue;
3020
3064
  var value = this.$attrs[key];
3021
3065
  if (value == null || value === false) root.removeAttribute(key);else root.setAttribute(key, String(value));
@@ -3163,7 +3207,7 @@ function makeCapsuleWrapper(createCapsule, render) {
3163
3207
  function makeProgressWrapper(createProgressCapsule, render) {
3164
3208
  return makeWrapper({
3165
3209
  name: 'ProgressCapsule',
3166
- props: ['preset', 'width', 'height', 'value', 'min', 'max', 'draggable', 'keyboard', 'disabled', 'edgeStyle', 'colors', 'textRatio', 'label', 'showValue', 'valueSuffix', 'quality', 'renderer', 'respectReducedMotion', 'cssVars'],
3210
+ props: ['preset', 'width', 'height', 'value', 'min', 'max', 'draggable', 'disabled', 'readonly', 'edgeStyle', 'colors', 'textRatio', 'label', 'showValue', 'valueSuffix', 'quality', 'renderer', 'respectReducedMotion', 'cssVars'],
3167
3211
  events: PROGRESS_EVENTS,
3168
3212
  create: createProgressCapsule,
3169
3213
  render: render,
@@ -3198,10 +3242,10 @@ function makeProgressWrapper(createProgressCapsule, render) {
3198
3242
  disabled: function disabled() {
3199
3243
  this.scheduleRecreate();
3200
3244
  },
3201
- draggable: function draggable() {
3245
+ readonly: function readonly() {
3202
3246
  this.scheduleRecreate();
3203
3247
  },
3204
- keyboard: function keyboard() {
3248
+ draggable: function draggable() {
3205
3249
  this.scheduleRecreate();
3206
3250
  },
3207
3251
  renderer: function renderer() {