@elementor/editor-controls 4.4.0-1099 → 4.4.0-1100
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/index.d.mts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +247 -199
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +171 -123
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -16
- package/src/controls/number-control.tsx +81 -25
- package/src/utils/number-value.ts +17 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-controls",
|
|
3
3
|
"description": "This package contains the controls model and utils for the Elementor editor",
|
|
4
|
-
"version": "4.4.0-
|
|
4
|
+
"version": "4.4.0-1100",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
|
@@ -40,23 +40,23 @@
|
|
|
40
40
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@elementor/editor-current-user": "4.4.0-
|
|
44
|
-
"@elementor/editor-elements": "4.4.0-
|
|
45
|
-
"@elementor/editor-props": "4.4.0-
|
|
46
|
-
"@elementor/editor-responsive": "4.4.0-
|
|
47
|
-
"@elementor/editor-ui": "4.4.0-
|
|
48
|
-
"@elementor/editor-v1-adapters": "4.4.0-
|
|
49
|
-
"@elementor/env": "4.4.0-
|
|
50
|
-
"@elementor/events": "4.4.0-
|
|
51
|
-
"@elementor/http-client": "4.4.0-
|
|
43
|
+
"@elementor/editor-current-user": "4.4.0-1100",
|
|
44
|
+
"@elementor/editor-elements": "4.4.0-1100",
|
|
45
|
+
"@elementor/editor-props": "4.4.0-1100",
|
|
46
|
+
"@elementor/editor-responsive": "4.4.0-1100",
|
|
47
|
+
"@elementor/editor-ui": "4.4.0-1100",
|
|
48
|
+
"@elementor/editor-v1-adapters": "4.4.0-1100",
|
|
49
|
+
"@elementor/env": "4.4.0-1100",
|
|
50
|
+
"@elementor/events": "4.4.0-1100",
|
|
51
|
+
"@elementor/http-client": "4.4.0-1100",
|
|
52
52
|
"@elementor/icons": "~1.75.1",
|
|
53
|
-
"@elementor/locations": "4.4.0-
|
|
54
|
-
"@elementor/query": "4.4.0-
|
|
55
|
-
"@elementor/schema": "4.4.0-
|
|
56
|
-
"@elementor/session": "4.4.0-
|
|
53
|
+
"@elementor/locations": "4.4.0-1100",
|
|
54
|
+
"@elementor/query": "4.4.0-1100",
|
|
55
|
+
"@elementor/schema": "4.4.0-1100",
|
|
56
|
+
"@elementor/session": "4.4.0-1100",
|
|
57
57
|
"@elementor/ui": "1.37.5",
|
|
58
|
-
"@elementor/utils": "4.4.0-
|
|
59
|
-
"@elementor/wp-media": "4.4.0-
|
|
58
|
+
"@elementor/utils": "4.4.0-1100",
|
|
59
|
+
"@elementor/wp-media": "4.4.0-1100",
|
|
60
60
|
"@monaco-editor/react": "^4.7.0",
|
|
61
61
|
"@tanstack/react-virtual": "^3.13.3",
|
|
62
62
|
"@tiptap/extension-bold": "^3.11.1",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { useRef, useState } from 'react';
|
|
2
3
|
import { numberPropTypeUtil, type PropType } from '@elementor/editor-props';
|
|
3
4
|
import { InputAdornment, Typography } from '@elementor/ui';
|
|
4
5
|
|
|
@@ -6,7 +7,9 @@ import { useBoundProp } from '../bound-prop-context';
|
|
|
6
7
|
import { NumberInput } from '../components/number-input';
|
|
7
8
|
import ControlActions from '../control-actions/control-actions';
|
|
8
9
|
import { createControl } from '../create-control';
|
|
10
|
+
import { clamp, isEmptyDraft, isInRange, parseNumberDraft } from '../utils/number-value';
|
|
9
11
|
|
|
12
|
+
// Bound value and placeholder only. Draft parsing uses isEmptyDraft / parseNumberDraft.
|
|
10
13
|
const isEmptyOrNaN = ( value?: string | number | null ) =>
|
|
11
14
|
value === null || value === undefined || value === '' || Number.isNaN( Number( value ) );
|
|
12
15
|
|
|
@@ -26,45 +29,98 @@ const renderSuffix = ( propType: PropType ) => {
|
|
|
26
29
|
export const NumberControl = createControl(
|
|
27
30
|
( {
|
|
28
31
|
placeholder: labelPlaceholder,
|
|
29
|
-
max
|
|
30
|
-
min
|
|
31
|
-
step
|
|
32
|
+
max: maxProp,
|
|
33
|
+
min: minProp,
|
|
34
|
+
step: stepProp,
|
|
32
35
|
shouldForceInt = false,
|
|
33
36
|
startIcon,
|
|
34
37
|
disabled: inputDisabled,
|
|
35
38
|
}: {
|
|
36
39
|
placeholder?: string;
|
|
37
|
-
max?: number;
|
|
38
|
-
min?: number;
|
|
39
|
-
step?: number;
|
|
40
|
+
max?: number | null;
|
|
41
|
+
min?: number | null;
|
|
42
|
+
step?: number | null;
|
|
40
43
|
shouldForceInt?: boolean;
|
|
41
44
|
startIcon?: React.ReactNode;
|
|
42
45
|
disabled?: boolean;
|
|
43
46
|
} ) => {
|
|
47
|
+
const max = maxProp ?? Number.MAX_SAFE_INTEGER;
|
|
48
|
+
const min = minProp ?? -Number.MAX_SAFE_INTEGER;
|
|
49
|
+
const step = stepProp ?? 1;
|
|
44
50
|
const { value, setValue, placeholder, disabled, restoreValue, propType } = useBoundProp( numberPropTypeUtil );
|
|
45
51
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
validity: { valid: isInputValid },
|
|
50
|
-
} = event.target;
|
|
52
|
+
// `null` means "not editing" — the input mirrors the bound value.
|
|
53
|
+
const [ draft, setDraft ] = useState< string | null >( null );
|
|
54
|
+
const committedNullOnInputRef = useRef( false );
|
|
51
55
|
|
|
52
|
-
|
|
56
|
+
const handleInput = ( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
57
|
+
const raw = event.target.value;
|
|
53
58
|
|
|
54
|
-
|
|
55
|
-
updatedValue = null;
|
|
56
|
-
} else {
|
|
57
|
-
const formattedValue = shouldForceInt ? +parseInt( eventValue ) : Number( eventValue );
|
|
59
|
+
setDraft( raw );
|
|
58
60
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
if ( isEmptyDraft( raw ) ) {
|
|
62
|
+
if ( ! propType.settings.required ) {
|
|
63
|
+
setValue( null );
|
|
64
|
+
committedNullOnInputRef.current = true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
committedNullOnInputRef.current = false;
|
|
71
|
+
|
|
72
|
+
const parsed = parseNumberDraft( raw, shouldForceInt );
|
|
73
|
+
|
|
74
|
+
// Type-required: `isInRange` and `clamp` both take a number.
|
|
75
|
+
if ( parsed === null ) {
|
|
76
|
+
return;
|
|
63
77
|
}
|
|
64
78
|
|
|
65
|
-
|
|
79
|
+
if ( isInRange( parsed, min, max ) ) {
|
|
80
|
+
setValue( parsed );
|
|
81
|
+
}
|
|
66
82
|
};
|
|
67
83
|
|
|
84
|
+
const handleBlur = () => {
|
|
85
|
+
const raw = draft;
|
|
86
|
+
const committedNullOnInput = committedNullOnInputRef.current;
|
|
87
|
+
|
|
88
|
+
committedNullOnInputRef.current = false;
|
|
89
|
+
setDraft( null );
|
|
90
|
+
// restoreValue() only clears the validation latch in use-bound-prop; it does not revert the model.
|
|
91
|
+
restoreValue();
|
|
92
|
+
|
|
93
|
+
if ( raw === null ) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if ( isEmptyDraft( raw ) ) {
|
|
98
|
+
// handleInput already committed null for optional clears; skip the redundant blur write.
|
|
99
|
+
if ( ! propType.settings.required && ! committedNullOnInput ) {
|
|
100
|
+
setValue( null );
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const parsed = parseNumberDraft( raw, shouldForceInt );
|
|
107
|
+
|
|
108
|
+
// As above — falling through would commit `clamp( null, … )`, a value never typed.
|
|
109
|
+
if ( parsed === null ) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// handleInput already committed anything in range; re-committing adds a redundant
|
|
114
|
+
// store write and, past the history debounce, a no-op undo step.
|
|
115
|
+
if ( isInRange( parsed, min, max ) ) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
setValue( clamp( parsed, min, max ) );
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const displayValue = draft ?? ( isEmptyOrNaN( value ) ? '' : String( value ) );
|
|
123
|
+
|
|
68
124
|
return (
|
|
69
125
|
<ControlActions>
|
|
70
126
|
<NumberInput
|
|
@@ -72,11 +128,11 @@ export const NumberControl = createControl(
|
|
|
72
128
|
type="number"
|
|
73
129
|
fullWidth
|
|
74
130
|
disabled={ inputDisabled ?? disabled }
|
|
75
|
-
value={
|
|
76
|
-
onInput={
|
|
77
|
-
onBlur={
|
|
131
|
+
value={ displayValue }
|
|
132
|
+
onInput={ handleInput }
|
|
133
|
+
onBlur={ handleBlur }
|
|
78
134
|
placeholder={ labelPlaceholder ?? ( isEmptyOrNaN( placeholder ) ? '' : String( placeholder ) ) }
|
|
79
|
-
inputProps={ { step, min } }
|
|
135
|
+
inputProps={ { step, min, ...( maxProp !== null && maxProp !== undefined && { max } ) } }
|
|
80
136
|
InputProps={ {
|
|
81
137
|
startAdornment: startIcon ? (
|
|
82
138
|
<InputAdornment position="start" disabled={ inputDisabled ?? disabled }>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const isEmptyDraft = ( raw: string ) => raw.trim() === '';
|
|
2
|
+
|
|
3
|
+
export const parseNumberDraft = ( raw: string, shouldForceInt = false ): number | null => {
|
|
4
|
+
const trimmed = raw.trim();
|
|
5
|
+
|
|
6
|
+
if ( trimmed === '' ) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const parsed = shouldForceInt ? parseInt( trimmed, 10 ) : Number( trimmed );
|
|
11
|
+
|
|
12
|
+
return Number.isFinite( parsed ) ? parsed : null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const isInRange = ( value: number, min: number, max: number ) => value >= min && value <= max;
|
|
16
|
+
|
|
17
|
+
export const clamp = ( value: number, min: number, max: number ) => Math.min( Math.max( value, min ), max );
|