@vue-puzzle-vcode/core 2.0.0 → 2.0.2

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/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # @vue-puzzle-vcode/core
2
+
3
+ vue-puzzle-vcode 的 headless 核心:滑动拼图验证码的状态机与组合式函数(composables),同时兼容 Vue 2.7 与 Vue 3(基于 vue-demi)。
4
+
5
+ > 一般不需要直接使用本包 —— 请安装组件包 [`@vue-puzzle-vcode/ui`](https://www.npmjs.com/package/@vue-puzzle-vcode/ui)。只有想完全自绘 UI、只复用校验逻辑时才需要它。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ pnpm add @vue-puzzle-vcode/core
11
+ # 需已安装 vue@^2.7 || ^3(peer dependency)
12
+ ```
13
+
14
+ ## 导出
15
+
16
+ - `useVcode(props, emits)` —— 核心状态机:画布数据、滑块偏移、成功/失败判定、重置
17
+ - `provideVcodeContext` / `useVcodeContext` —— 跨部件共享状态的上下文(Radix 风格组合)
18
+ - 全部 TypeScript 类型(props / events / context)
19
+
20
+ ## 文档
21
+
22
+ 完整文档与在线 Demo 见主仓库:[github.com/daguanren21/vue-puzzle-vcode](https://github.com/daguanren21/vue-puzzle-vcode#readme)
package/dist/index.cjs CHANGED
@@ -117,8 +117,13 @@ function useVcode(props, emits) {
117
117
  const w = startWidth.value + newX.value - startX.value;
118
118
  return w < sliderBaseSize.value ? sliderBaseSize.value : w > props.canvasWidth ? props.canvasWidth : w;
119
119
  });
120
+ /**
121
+ * Travel distance of the track (slider end - start). Floored at 1 so the
122
+ * compensation term never divides by zero when canvasWidth ≈ sliderSize.
123
+ */
124
+ const trackTravel = (0, vue_demi.computed)(() => (0, _vue_puzzle_vcode_shared.computeTrackTravel)(props.canvasWidth, sliderBaseSize.value));
120
125
  /** translateX of the floating puzzle piece, synced to the slider. */
121
- const puzzleTranslateX = (0, vue_demi.computed)(() => styleWidth.value - sliderBaseSize.value - (puzzleBaseSize.value - sliderBaseSize.value) * ((styleWidth.value - sliderBaseSize.value) / (props.canvasWidth - sliderBaseSize.value)));
126
+ const puzzleTranslateX = (0, vue_demi.computed)(() => (0, _vue_puzzle_vcode_shared.computePuzzleTranslateX)(styleWidth.value, sliderBaseSize.value, puzzleBaseSize.value, trackTravel.value));
122
127
  const canvases = {
123
128
  main: (0, vue_demi.shallowRef)(),
124
129
  puzzle: (0, vue_demi.shallowRef)(),
@@ -179,7 +184,10 @@ function useVcode(props, emits) {
179
184
  pinY: pinY.value
180
185
  });
181
186
  } catch {
182
- return init(true);
187
+ if (!withCanvas) return init(true);
188
+ loading.value = false;
189
+ isCanSlide.value = false;
190
+ return;
183
191
  }
184
192
  loading.value = false;
185
193
  isCanSlide.value = true;
@@ -204,7 +212,7 @@ function useVcode(props, emits) {
204
212
  }
205
213
  function submit() {
206
214
  isSubmting.value = true;
207
- const x = Math.abs(pinX.value - (styleWidth.value - sliderBaseSize.value) + (puzzleBaseSize.value - sliderBaseSize.value) * ((styleWidth.value - sliderBaseSize.value) / (props.canvasWidth - sliderBaseSize.value)) - 3);
215
+ const x = (0, _vue_puzzle_vcode_shared.computeDragDeviation)(pinX.value, styleWidth.value, sliderBaseSize.value, puzzleBaseSize.value, trackTravel.value);
208
216
  clearTimer();
209
217
  if (x < props.range) {
210
218
  infoText.value = props.successText;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { computed, getCurrentScope, h, inject, onScopeDispose, provide, ref, shallowRef, version } from "vue-demi";
2
- import { computePuzzleBaseSize, computeSliderBaseSize, drawPuzzleFrame, generateRandomImage, loadImage, randomInt } from "@vue-puzzle-vcode/shared";
2
+ import { computeDragDeviation, computePuzzleBaseSize, computePuzzleTranslateX, computeSliderBaseSize, computeTrackTravel, drawPuzzleFrame, generateRandomImage, loadImage, randomInt } from "@vue-puzzle-vcode/shared";
3
3
  //#region src/context.ts
4
4
  /**
5
5
  * Radix-style typed context factory built on provide/inject.
@@ -116,8 +116,13 @@ function useVcode(props, emits) {
116
116
  const w = startWidth.value + newX.value - startX.value;
117
117
  return w < sliderBaseSize.value ? sliderBaseSize.value : w > props.canvasWidth ? props.canvasWidth : w;
118
118
  });
119
+ /**
120
+ * Travel distance of the track (slider end - start). Floored at 1 so the
121
+ * compensation term never divides by zero when canvasWidth ≈ sliderSize.
122
+ */
123
+ const trackTravel = computed(() => computeTrackTravel(props.canvasWidth, sliderBaseSize.value));
119
124
  /** translateX of the floating puzzle piece, synced to the slider. */
120
- const puzzleTranslateX = computed(() => styleWidth.value - sliderBaseSize.value - (puzzleBaseSize.value - sliderBaseSize.value) * ((styleWidth.value - sliderBaseSize.value) / (props.canvasWidth - sliderBaseSize.value)));
125
+ const puzzleTranslateX = computed(() => computePuzzleTranslateX(styleWidth.value, sliderBaseSize.value, puzzleBaseSize.value, trackTravel.value));
121
126
  const canvases = {
122
127
  main: shallowRef(),
123
128
  puzzle: shallowRef(),
@@ -178,7 +183,10 @@ function useVcode(props, emits) {
178
183
  pinY: pinY.value
179
184
  });
180
185
  } catch {
181
- return init(true);
186
+ if (!withCanvas) return init(true);
187
+ loading.value = false;
188
+ isCanSlide.value = false;
189
+ return;
182
190
  }
183
191
  loading.value = false;
184
192
  isCanSlide.value = true;
@@ -203,7 +211,7 @@ function useVcode(props, emits) {
203
211
  }
204
212
  function submit() {
205
213
  isSubmting.value = true;
206
- const x = Math.abs(pinX.value - (styleWidth.value - sliderBaseSize.value) + (puzzleBaseSize.value - sliderBaseSize.value) * ((styleWidth.value - sliderBaseSize.value) / (props.canvasWidth - sliderBaseSize.value)) - 3);
214
+ const x = computeDragDeviation(pinX.value, styleWidth.value, sliderBaseSize.value, puzzleBaseSize.value, trackTravel.value);
207
215
  clearTimer();
208
216
  if (x < props.range) {
209
217
  infoText.value = props.successText;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@vue-puzzle-vcode/core",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Headless core for vue-puzzle-vcode: context factory, v2/v3 render compat, state machine",
5
5
  "license": "ISC",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/javaLuo/vue-puzzle-vcode.git",
8
+ "url": "git+https://github.com/daguanren21/vue-puzzle-vcode.git",
9
9
  "directory": "packages/core"
10
10
  },
11
11
  "type": "module",
@@ -30,7 +30,7 @@
30
30
  "sideEffects": false,
31
31
  "dependencies": {
32
32
  "vue-demi": "0.14.10",
33
- "@vue-puzzle-vcode/shared": "^2.0.0"
33
+ "@vue-puzzle-vcode/shared": "^2.0.2"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "vue": "^2.7.0 || ^3.0.0"