@zag-js/number-input 2.0.0-next.3 → 2.0.0-next.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.
Files changed (41) hide show
  1. package/dist/index.d.mts +6 -9
  2. package/dist/index.d.ts +6 -9
  3. package/dist/index.js +12 -38
  4. package/dist/index.mjs +2 -7
  5. package/dist/number-input.anatomy.d.mts +2 -6
  6. package/dist/number-input.anatomy.d.ts +2 -6
  7. package/dist/number-input.anatomy.js +8 -43
  8. package/dist/number-input.anatomy.mjs +4 -17
  9. package/dist/number-input.connect.d.mts +3 -8
  10. package/dist/number-input.connect.d.ts +3 -8
  11. package/dist/number-input.connect.js +483 -377
  12. package/dist/number-input.connect.mjs +478 -353
  13. package/dist/number-input.dom.d.mts +33 -33
  14. package/dist/number-input.dom.d.ts +33 -33
  15. package/dist/number-input.dom.js +99 -142
  16. package/dist/number-input.dom.mjs +78 -103
  17. package/dist/number-input.machine.d.mts +2 -8
  18. package/dist/number-input.machine.d.ts +2 -8
  19. package/dist/number-input.machine.js +600 -528
  20. package/dist/number-input.machine.mjs +596 -512
  21. package/dist/number-input.props.d.mts +3 -9
  22. package/dist/number-input.props.d.ts +3 -9
  23. package/dist/number-input.props.js +43 -69
  24. package/dist/number-input.props.mjs +39 -43
  25. package/dist/number-input.translations.d.mts +3 -8
  26. package/dist/number-input.translations.d.ts +3 -8
  27. package/dist/number-input.translations.js +9 -31
  28. package/dist/number-input.translations.mjs +5 -6
  29. package/dist/number-input.types.d.mts +418 -408
  30. package/dist/number-input.types.d.ts +418 -408
  31. package/dist/number-input.types.js +4 -18
  32. package/dist/number-input.types.mjs +1 -0
  33. package/dist/number-input.utils.d.mts +16 -12
  34. package/dist/number-input.utils.d.ts +16 -12
  35. package/dist/number-input.utils.js +51 -57
  36. package/dist/number-input.utils.mjs +39 -27
  37. package/package.json +8 -8
  38. package/dist/cursor.d.mts +0 -12
  39. package/dist/cursor.d.ts +0 -12
  40. package/dist/cursor.js +0 -111
  41. package/dist/cursor.mjs +0 -84
@@ -1,18 +1,4 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/number-input.types.ts
17
- var number_input_types_exports = {};
18
- module.exports = __toCommonJS(number_input_types_exports);
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
4
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -1,13 +1,17 @@
1
- import { NumberParser } from '@internationalized/number';
2
- import { Params } from '@zag-js/core';
3
- import { NumberInputSchema } from './number-input.types.mjs';
4
- import '@zag-js/types';
5
-
6
- declare const createFormatter: (locale: string, options?: Intl.NumberFormatOptions) => Intl.NumberFormat;
7
- declare const createParser: (locale: string, options?: Intl.NumberFormatOptions) => NumberParser;
1
+ import { NumberInputSchema } from "./number-input.types.mjs";
2
+ import { NumberParser } from "@internationalized/number";
3
+ import { Params } from "@zag-js/core";
4
+ export declare const createFormatter: (locale: string, options?: Intl.NumberFormatOptions) => Intl.NumberFormat;
5
+ export declare const createParser: (locale: string, options?: Intl.NumberFormatOptions) => NumberParser;
8
6
  type Ctx = Pick<Params<NumberInputSchema>, "prop" | "computed">;
9
- declare const parseValue: (value: string, params: Ctx) => number;
10
- declare const formatValue: (value: number, params: Ctx) => string;
11
- declare const getDefaultStep: (step: number | undefined, formatOptions: Intl.NumberFormatOptions | undefined) => number;
12
-
13
- export { createFormatter, createParser, formatValue, getDefaultStep, parseValue };
7
+ export declare const normalizeNumberText: (value: string) => string;
8
+ export declare const parseValue: (input: string, params: Ctx) => number;
9
+ export declare const formatNumber: (value: number | null | undefined, formatter: Intl.NumberFormat, formatOptions: Intl.NumberFormatOptions | undefined) => string;
10
+ export declare const formatValue: (value: number | null, params: Ctx) => string;
11
+ export declare const getDefaultStep: (step: number | undefined, formatOptions: Intl.NumberFormatOptions | undefined) => number;
12
+ /**
13
+ * Snaps a stepped value onto the step grid, rounding toward where it came from,
14
+ * so 5.5 + 1 lands on 6 rather than 7.
15
+ */
16
+ export declare function snapToStep(value: number, base: number, step: number, direction: number): number;
17
+ export {};
@@ -1,13 +1,17 @@
1
- import { NumberParser } from '@internationalized/number';
2
- import { Params } from '@zag-js/core';
3
- import { NumberInputSchema } from './number-input.types.js';
4
- import '@zag-js/types';
5
-
6
- declare const createFormatter: (locale: string, options?: Intl.NumberFormatOptions) => Intl.NumberFormat;
7
- declare const createParser: (locale: string, options?: Intl.NumberFormatOptions) => NumberParser;
1
+ import { NumberInputSchema } from "./number-input.types.js";
2
+ import { NumberParser } from "@internationalized/number";
3
+ import { Params } from "@zag-js/core";
4
+ export declare const createFormatter: (locale: string, options?: Intl.NumberFormatOptions) => Intl.NumberFormat;
5
+ export declare const createParser: (locale: string, options?: Intl.NumberFormatOptions) => NumberParser;
8
6
  type Ctx = Pick<Params<NumberInputSchema>, "prop" | "computed">;
9
- declare const parseValue: (value: string, params: Ctx) => number;
10
- declare const formatValue: (value: number, params: Ctx) => string;
11
- declare const getDefaultStep: (step: number | undefined, formatOptions: Intl.NumberFormatOptions | undefined) => number;
12
-
13
- export { createFormatter, createParser, formatValue, getDefaultStep, parseValue };
7
+ export declare const normalizeNumberText: (value: string) => string;
8
+ export declare const parseValue: (input: string, params: Ctx) => number;
9
+ export declare const formatNumber: (value: number | null | undefined, formatter: Intl.NumberFormat, formatOptions: Intl.NumberFormatOptions | undefined) => string;
10
+ export declare const formatValue: (value: number | null, params: Ctx) => string;
11
+ export declare const getDefaultStep: (step: number | undefined, formatOptions: Intl.NumberFormatOptions | undefined) => number;
12
+ /**
13
+ * Snaps a stepped value onto the step grid, rounding toward where it came from,
14
+ * so 5.5 + 1 lands on 6 rather than 7.
15
+ */
16
+ export declare function snapToStep(value: number, base: number, step: number, direction: number): number;
17
+ export {};
@@ -1,63 +1,57 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/number-input.utils.ts
21
- var number_input_utils_exports = {};
22
- __export(number_input_utils_exports, {
23
- createFormatter: () => createFormatter,
24
- createParser: () => createParser,
25
- formatValue: () => formatValue,
26
- getDefaultStep: () => getDefaultStep,
27
- parseValue: () => parseValue
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
28
4
  });
29
- module.exports = __toCommonJS(number_input_utils_exports);
30
- var import_number = require("@internationalized/number");
31
- var createFormatter = (locale, options = {}) => {
32
- return new Intl.NumberFormat(locale, options);
5
+ let _zag_js_utils = require("@zag-js/utils");
6
+ let _internationalized_number = require("@internationalized/number");
7
+ // src/number-input.utils.ts
8
+ const createFormatter = (locale, options = {}) => {
9
+ return new Intl.NumberFormat(locale, options);
33
10
  };
34
- var createParser = (locale, options = {}) => {
35
- return new import_number.NumberParser(locale, options);
11
+ const createParser = (locale, options = {}) => {
12
+ return new _internationalized_number.NumberParser(locale, options);
36
13
  };
37
- var parseValue = (value, params) => {
38
- const { prop, computed } = params;
39
- if (!prop("formatOptions")) return parseFloat(value);
40
- if (value === "") return Number.NaN;
41
- return computed("parser").parse(value);
14
+ const MINUS_SIGNS = /[\u2012\u2013\u2014\u2212\uFE63\uFF0D]/g;
15
+ const PERSIAN_DIGITS = /[\u06F0-\u06F9]/g;
16
+ const FULLWIDTH_DECIMAL = /\uFF0E/g;
17
+ const FULLWIDTH_GROUP = /\uFF0C/g;
18
+ const normalizeNumberText = (value) => value.replace(MINUS_SIGNS, "-").replace(PERSIAN_DIGITS, (d) => String(d.charCodeAt(0) - 1776)).replace(FULLWIDTH_DECIMAL, ".").replace(FULLWIDTH_GROUP, "");
19
+ const parseValue = (input, params) => {
20
+ const { computed } = params;
21
+ const value = normalizeNumberText(input);
22
+ if (value === "") return NaN;
23
+ return computed("parser").parse(value);
42
24
  };
43
- var formatValue = (value, params) => {
44
- const { prop, computed } = params;
45
- if (Number.isNaN(value)) return "";
46
- if (!prop("formatOptions")) return value.toString();
47
- return computed("formatter").format(value);
25
+ const formatNumber = (value, formatter, formatOptions) => {
26
+ if (value == null || Number.isNaN(value)) return "";
27
+ if (formatOptions) return formatter.format(value);
28
+ const decimal = formatter.formatToParts(1.1).find((part) => part.type === "decimal")?.value ?? ".";
29
+ return value.toString().replace(".", decimal);
48
30
  };
49
- var getDefaultStep = (step, formatOptions) => {
50
- let defaultStep = step !== void 0 && !Number.isNaN(step) ? step : 1;
51
- if (formatOptions?.style === "percent" && (step === void 0 || Number.isNaN(step))) {
52
- defaultStep = 0.01;
53
- }
54
- return defaultStep;
31
+ const formatValue = (value, params) => {
32
+ const { prop, computed } = params;
33
+ return formatNumber(value, computed("formatter"), prop("formatOptions"));
55
34
  };
56
- // Annotate the CommonJS export names for ESM import in node:
57
- 0 && (module.exports = {
58
- createFormatter,
59
- createParser,
60
- formatValue,
61
- getDefaultStep,
62
- parseValue
63
- });
35
+ const getDefaultStep = (step, formatOptions) => {
36
+ let defaultStep = step !== void 0 && !Number.isNaN(step) ? step : 1;
37
+ if (formatOptions?.style === "percent" && (step === void 0 || Number.isNaN(step))) defaultStep = .01;
38
+ return defaultStep;
39
+ };
40
+ /**
41
+ * Snaps a stepped value onto the step grid, rounding toward where it came from,
42
+ * so 5.5 + 1 lands on 6 rather than 7.
43
+ */
44
+ function snapToStep(value, base, step, direction) {
45
+ const tolerance = 1e-10;
46
+ const steps = (value - base) / step;
47
+ const snapped = direction > 0 ? Math.floor(steps + tolerance) : Math.ceil(steps - tolerance);
48
+ return (0, _zag_js_utils.roundToStepPrecision)(base + snapped * step, step);
49
+ }
50
+ exports.createFormatter = createFormatter;
51
+ exports.createParser = createParser;
52
+ exports.formatNumber = formatNumber;
53
+ exports.formatValue = formatValue;
54
+ exports.getDefaultStep = getDefaultStep;
55
+ exports.normalizeNumberText = normalizeNumberText;
56
+ exports.parseValue = parseValue;
57
+ exports.snapToStep = snapToStep;
@@ -1,34 +1,46 @@
1
- // src/number-input.utils.ts
1
+ import { roundToStepPrecision } from "@zag-js/utils";
2
2
  import { NumberParser } from "@internationalized/number";
3
- var createFormatter = (locale, options = {}) => {
4
- return new Intl.NumberFormat(locale, options);
3
+ // src/number-input.utils.ts
4
+ const createFormatter = (locale, options = {}) => {
5
+ return new Intl.NumberFormat(locale, options);
5
6
  };
6
- var createParser = (locale, options = {}) => {
7
- return new NumberParser(locale, options);
7
+ const createParser = (locale, options = {}) => {
8
+ return new NumberParser(locale, options);
8
9
  };
9
- var parseValue = (value, params) => {
10
- const { prop, computed } = params;
11
- if (!prop("formatOptions")) return parseFloat(value);
12
- if (value === "") return Number.NaN;
13
- return computed("parser").parse(value);
10
+ const MINUS_SIGNS = /[\u2012\u2013\u2014\u2212\uFE63\uFF0D]/g;
11
+ const PERSIAN_DIGITS = /[\u06F0-\u06F9]/g;
12
+ const FULLWIDTH_DECIMAL = /\uFF0E/g;
13
+ const FULLWIDTH_GROUP = /\uFF0C/g;
14
+ const normalizeNumberText = (value) => value.replace(MINUS_SIGNS, "-").replace(PERSIAN_DIGITS, (d) => String(d.charCodeAt(0) - 1776)).replace(FULLWIDTH_DECIMAL, ".").replace(FULLWIDTH_GROUP, "");
15
+ const parseValue = (input, params) => {
16
+ const { computed } = params;
17
+ const value = normalizeNumberText(input);
18
+ if (value === "") return NaN;
19
+ return computed("parser").parse(value);
14
20
  };
15
- var formatValue = (value, params) => {
16
- const { prop, computed } = params;
17
- if (Number.isNaN(value)) return "";
18
- if (!prop("formatOptions")) return value.toString();
19
- return computed("formatter").format(value);
21
+ const formatNumber = (value, formatter, formatOptions) => {
22
+ if (value == null || Number.isNaN(value)) return "";
23
+ if (formatOptions) return formatter.format(value);
24
+ const decimal = formatter.formatToParts(1.1).find((part) => part.type === "decimal")?.value ?? ".";
25
+ return value.toString().replace(".", decimal);
20
26
  };
21
- var getDefaultStep = (step, formatOptions) => {
22
- let defaultStep = step !== void 0 && !Number.isNaN(step) ? step : 1;
23
- if (formatOptions?.style === "percent" && (step === void 0 || Number.isNaN(step))) {
24
- defaultStep = 0.01;
25
- }
26
- return defaultStep;
27
+ const formatValue = (value, params) => {
28
+ const { prop, computed } = params;
29
+ return formatNumber(value, computed("formatter"), prop("formatOptions"));
27
30
  };
28
- export {
29
- createFormatter,
30
- createParser,
31
- formatValue,
32
- getDefaultStep,
33
- parseValue
31
+ const getDefaultStep = (step, formatOptions) => {
32
+ let defaultStep = step !== void 0 && !Number.isNaN(step) ? step : 1;
33
+ if (formatOptions?.style === "percent" && (step === void 0 || Number.isNaN(step))) defaultStep = .01;
34
+ return defaultStep;
34
35
  };
36
+ /**
37
+ * Snaps a stepped value onto the step grid, rounding toward where it came from,
38
+ * so 5.5 + 1 lands on 6 rather than 7.
39
+ */
40
+ function snapToStep(value, base, step, direction) {
41
+ const tolerance = 1e-10;
42
+ const steps = (value - base) / step;
43
+ const snapped = direction > 0 ? Math.floor(steps + tolerance) : Math.ceil(steps - tolerance);
44
+ return roundToStepPrecision(base + snapped * step, step);
45
+ }
46
+ export { createFormatter, createParser, formatNumber, formatValue, getDefaultStep, normalizeNumberText, parseValue, snapToStep };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/number-input",
3
- "version": "2.0.0-next.3",
3
+ "version": "2.0.0-next.4",
4
4
  "description": "Core logic for the number-input widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,12 +29,12 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@internationalized/number": "3.6.7",
33
- "@zag-js/anatomy": "2.0.0-next.3",
34
- "@zag-js/core": "2.0.0-next.3",
35
- "@zag-js/dom-query": "2.0.0-next.3",
36
- "@zag-js/types": "2.0.0-next.3",
37
- "@zag-js/utils": "2.0.0-next.3"
32
+ "@internationalized/number": "3.6.9",
33
+ "@zag-js/anatomy": "2.0.0-next.4",
34
+ "@zag-js/core": "2.0.0-next.4",
35
+ "@zag-js/dom-query": "2.0.0-next.4",
36
+ "@zag-js/types": "2.0.0-next.4",
37
+ "@zag-js/utils": "2.0.0-next.4"
38
38
  },
39
39
  "devDependencies": {
40
40
  "clean-package": "2.2.0"
@@ -67,7 +67,7 @@
67
67
  "./package.json": "./package.json"
68
68
  },
69
69
  "scripts": {
70
- "build": "tsup",
70
+ "build": "tsdown",
71
71
  "lint": "oxlint src",
72
72
  "typecheck": "tsc --noEmit"
73
73
  }
package/dist/cursor.d.mts DELETED
@@ -1,12 +0,0 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- interface Selection {
4
- start: number;
5
- end: number;
6
- value: string;
7
- }
8
- declare function recordCursor(inputEl: HTMLInputElement | null, scope: Scope): Selection | undefined;
9
- declare function restoreCursor(inputEl: HTMLInputElement | null, selection: Selection | undefined, scope: Scope): void;
10
- declare function getNextCursorPosition(oldValue: string, newValue: string, oldPosition: number): number;
11
-
12
- export { getNextCursorPosition, recordCursor, restoreCursor };
package/dist/cursor.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import { Scope } from '@zag-js/core';
2
-
3
- interface Selection {
4
- start: number;
5
- end: number;
6
- value: string;
7
- }
8
- declare function recordCursor(inputEl: HTMLInputElement | null, scope: Scope): Selection | undefined;
9
- declare function restoreCursor(inputEl: HTMLInputElement | null, selection: Selection | undefined, scope: Scope): void;
10
- declare function getNextCursorPosition(oldValue: string, newValue: string, oldPosition: number): number;
11
-
12
- export { getNextCursorPosition, recordCursor, restoreCursor };
package/dist/cursor.js DELETED
@@ -1,111 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/cursor.ts
21
- var cursor_exports = {};
22
- __export(cursor_exports, {
23
- getNextCursorPosition: () => getNextCursorPosition,
24
- recordCursor: () => recordCursor,
25
- restoreCursor: () => restoreCursor
26
- });
27
- module.exports = __toCommonJS(cursor_exports);
28
- function recordCursor(inputEl, scope) {
29
- if (!inputEl || !scope.isActiveElement(inputEl)) return;
30
- try {
31
- const { selectionStart: start, selectionEnd: end, value } = inputEl;
32
- if (start == null || end == null) return void 0;
33
- return { start, end, value };
34
- } catch {
35
- return void 0;
36
- }
37
- }
38
- function restoreCursor(inputEl, selection, scope) {
39
- if (!inputEl || !scope.isActiveElement(inputEl)) return;
40
- if (!selection) {
41
- const len = inputEl.value.length;
42
- inputEl.setSelectionRange(len, len);
43
- return;
44
- }
45
- try {
46
- const newValue = inputEl.value;
47
- const { start, end, value: oldValue } = selection;
48
- if (newValue === oldValue) {
49
- inputEl.setSelectionRange(start, end);
50
- return;
51
- }
52
- const newStart = getNextCursorPosition(oldValue, newValue, start);
53
- const newEnd = start === end ? newStart : getNextCursorPosition(oldValue, newValue, end);
54
- const clampedStart = Math.max(0, Math.min(newStart, newValue.length));
55
- const clampedEnd = Math.max(clampedStart, Math.min(newEnd, newValue.length));
56
- inputEl.setSelectionRange(clampedStart, clampedEnd);
57
- } catch {
58
- const len = inputEl.value.length;
59
- inputEl.setSelectionRange(len, len);
60
- }
61
- }
62
- function getNextCursorPosition(oldValue, newValue, oldPosition) {
63
- const beforeCursor = oldValue.slice(0, oldPosition);
64
- const afterCursor = oldValue.slice(oldPosition);
65
- let prefixLength = 0;
66
- const maxPrefixLength = Math.min(beforeCursor.length, newValue.length);
67
- for (let i = 0; i < maxPrefixLength; i++) {
68
- if (beforeCursor[i] === newValue[i]) {
69
- prefixLength = i + 1;
70
- } else {
71
- break;
72
- }
73
- }
74
- let suffixLength = 0;
75
- const maxSuffixLength = Math.min(afterCursor.length, newValue.length - prefixLength);
76
- for (let i = 0; i < maxSuffixLength; i++) {
77
- const oldIndex = afterCursor.length - 1 - i;
78
- const newIndex = newValue.length - 1 - i;
79
- if (afterCursor[oldIndex] === newValue[newIndex]) {
80
- suffixLength = i + 1;
81
- } else {
82
- break;
83
- }
84
- }
85
- if (beforeCursor.length > 0 && prefixLength >= beforeCursor.length) {
86
- return prefixLength;
87
- }
88
- if (suffixLength >= afterCursor.length) {
89
- return newValue.length - suffixLength;
90
- }
91
- if (prefixLength > 0) {
92
- return prefixLength;
93
- }
94
- if (suffixLength > 0) {
95
- return newValue.length - suffixLength;
96
- }
97
- if (oldPosition === 0 && prefixLength === 0 && suffixLength === 0) {
98
- return newValue.length;
99
- }
100
- if (oldValue.length > 0) {
101
- const ratio = oldPosition / oldValue.length;
102
- return Math.round(ratio * newValue.length);
103
- }
104
- return newValue.length;
105
- }
106
- // Annotate the CommonJS export names for ESM import in node:
107
- 0 && (module.exports = {
108
- getNextCursorPosition,
109
- recordCursor,
110
- restoreCursor
111
- });
package/dist/cursor.mjs DELETED
@@ -1,84 +0,0 @@
1
- // src/cursor.ts
2
- function recordCursor(inputEl, scope) {
3
- if (!inputEl || !scope.isActiveElement(inputEl)) return;
4
- try {
5
- const { selectionStart: start, selectionEnd: end, value } = inputEl;
6
- if (start == null || end == null) return void 0;
7
- return { start, end, value };
8
- } catch {
9
- return void 0;
10
- }
11
- }
12
- function restoreCursor(inputEl, selection, scope) {
13
- if (!inputEl || !scope.isActiveElement(inputEl)) return;
14
- if (!selection) {
15
- const len = inputEl.value.length;
16
- inputEl.setSelectionRange(len, len);
17
- return;
18
- }
19
- try {
20
- const newValue = inputEl.value;
21
- const { start, end, value: oldValue } = selection;
22
- if (newValue === oldValue) {
23
- inputEl.setSelectionRange(start, end);
24
- return;
25
- }
26
- const newStart = getNextCursorPosition(oldValue, newValue, start);
27
- const newEnd = start === end ? newStart : getNextCursorPosition(oldValue, newValue, end);
28
- const clampedStart = Math.max(0, Math.min(newStart, newValue.length));
29
- const clampedEnd = Math.max(clampedStart, Math.min(newEnd, newValue.length));
30
- inputEl.setSelectionRange(clampedStart, clampedEnd);
31
- } catch {
32
- const len = inputEl.value.length;
33
- inputEl.setSelectionRange(len, len);
34
- }
35
- }
36
- function getNextCursorPosition(oldValue, newValue, oldPosition) {
37
- const beforeCursor = oldValue.slice(0, oldPosition);
38
- const afterCursor = oldValue.slice(oldPosition);
39
- let prefixLength = 0;
40
- const maxPrefixLength = Math.min(beforeCursor.length, newValue.length);
41
- for (let i = 0; i < maxPrefixLength; i++) {
42
- if (beforeCursor[i] === newValue[i]) {
43
- prefixLength = i + 1;
44
- } else {
45
- break;
46
- }
47
- }
48
- let suffixLength = 0;
49
- const maxSuffixLength = Math.min(afterCursor.length, newValue.length - prefixLength);
50
- for (let i = 0; i < maxSuffixLength; i++) {
51
- const oldIndex = afterCursor.length - 1 - i;
52
- const newIndex = newValue.length - 1 - i;
53
- if (afterCursor[oldIndex] === newValue[newIndex]) {
54
- suffixLength = i + 1;
55
- } else {
56
- break;
57
- }
58
- }
59
- if (beforeCursor.length > 0 && prefixLength >= beforeCursor.length) {
60
- return prefixLength;
61
- }
62
- if (suffixLength >= afterCursor.length) {
63
- return newValue.length - suffixLength;
64
- }
65
- if (prefixLength > 0) {
66
- return prefixLength;
67
- }
68
- if (suffixLength > 0) {
69
- return newValue.length - suffixLength;
70
- }
71
- if (oldPosition === 0 && prefixLength === 0 && suffixLength === 0) {
72
- return newValue.length;
73
- }
74
- if (oldValue.length > 0) {
75
- const ratio = oldPosition / oldValue.length;
76
- return Math.round(ratio * newValue.length);
77
- }
78
- return newValue.length;
79
- }
80
- export {
81
- getNextCursorPosition,
82
- recordCursor,
83
- restoreCursor
84
- };