@elementor/editor-controls 4.3.0-beta3 → 4.3.1

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/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.3.0-beta3",
4
+ "version": "4.3.1",
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.3.0-beta3",
44
- "@elementor/editor-elements": "4.3.0-beta3",
45
- "@elementor/editor-props": "4.3.0-beta3",
46
- "@elementor/editor-responsive": "4.3.0-beta3",
47
- "@elementor/editor-ui": "4.3.0-beta3",
48
- "@elementor/editor-v1-adapters": "4.3.0-beta3",
49
- "@elementor/env": "4.3.0-beta3",
50
- "@elementor/events": "4.3.0-beta3",
51
- "@elementor/http-client": "4.3.0-beta3",
43
+ "@elementor/editor-current-user": "4.3.1",
44
+ "@elementor/editor-elements": "4.3.1",
45
+ "@elementor/editor-props": "4.3.1",
46
+ "@elementor/editor-responsive": "4.3.1",
47
+ "@elementor/editor-ui": "4.3.1",
48
+ "@elementor/editor-v1-adapters": "4.3.1",
49
+ "@elementor/env": "4.3.1",
50
+ "@elementor/events": "4.3.1",
51
+ "@elementor/http-client": "4.3.1",
52
52
  "@elementor/icons": "~1.75.1",
53
- "@elementor/locations": "4.3.0-beta3",
54
- "@elementor/query": "4.3.0-beta3",
55
- "@elementor/schema": "4.3.0-beta3",
56
- "@elementor/session": "4.3.0-beta3",
53
+ "@elementor/locations": "4.3.1",
54
+ "@elementor/query": "4.3.1",
55
+ "@elementor/schema": "4.3.1",
56
+ "@elementor/session": "4.3.1",
57
57
  "@elementor/ui": "1.37.5",
58
- "@elementor/utils": "4.3.0-beta3",
59
- "@elementor/wp-media": "4.3.0-beta3",
58
+ "@elementor/utils": "4.3.1",
59
+ "@elementor/wp-media": "4.3.1",
60
60
  "@monaco-editor/react": "^4.7.0",
61
61
  "@tanstack/react-virtual": "^3.13.3",
62
62
  "@tiptap/extension-bold": "^3.11.1",
@@ -39,6 +39,8 @@ type LinkSessionValue = {
39
39
 
40
40
  export type DestinationProp = LinkPropValue[ 'value' ][ 'destination' ];
41
41
 
42
+ type ClearActiveLinkWhen = 'restricted' | 'newly-restricted';
43
+
42
44
  const SIZE = 'tiny';
43
45
 
44
46
  export const LinkControl = createControl( ( props: Props ) => {
@@ -63,10 +65,13 @@ export const LinkControl = createControl( ( props: Props ) => {
63
65
 
64
66
  const shouldDisableAddingLink = ! isActive && linkInLinkRestriction.shouldRestrict;
65
67
 
66
- const debouncedCheckRestriction = useDebouncedCallback( () => {
68
+ const syncLinkRestriction = ( clearActiveLinkWhen: ClearActiveLinkWhen = 'restricted' ) => {
67
69
  const newRestriction = getLinkInLinkRestriction( elementId, value ?? linkPlaceholder );
70
+ const becameRestricted = newRestriction.shouldRestrict && ! linkInLinkRestriction.shouldRestrict;
71
+ const shouldClearActiveLink =
72
+ clearActiveLinkWhen === 'newly-restricted' ? becameRestricted : newRestriction.shouldRestrict;
68
73
 
69
- if ( newRestriction.shouldRestrict && isActive && ! linkPlaceholder ) {
74
+ if ( shouldClearActiveLink && isActive && ! linkPlaceholder ) {
70
75
  setIsActive( false );
71
76
 
72
77
  if ( value !== null ) {
@@ -75,21 +80,26 @@ export const LinkControl = createControl( ( props: Props ) => {
75
80
  }
76
81
 
77
82
  setLinkInLinkRestriction( ( prev ) => ( isSameRestriction( prev, newRestriction ) ? prev : newRestriction ) );
78
- }, 300 );
83
+ };
84
+
85
+ const debouncedSyncLinkRestriction = useDebouncedCallback(
86
+ ( clearActiveLinkWhen: ClearActiveLinkWhen = 'restricted' ) => syncLinkRestriction( clearActiveLinkWhen ),
87
+ 300
88
+ );
79
89
 
80
90
  useListenTo(
81
91
  commandEndEvent( 'document/elements/set-settings' ),
82
92
  () => {
83
- debouncedCheckRestriction();
93
+ debouncedSyncLinkRestriction( 'newly-restricted' );
84
94
  },
85
- [ debouncedCheckRestriction ]
95
+ [ debouncedSyncLinkRestriction ]
86
96
  );
87
97
 
88
98
  useEffect( () => {
89
- debouncedCheckRestriction();
99
+ debouncedSyncLinkRestriction();
90
100
 
91
101
  const handleInlineLinkChanged = () => {
92
- debouncedCheckRestriction();
102
+ debouncedSyncLinkRestriction();
93
103
  };
94
104
 
95
105
  window.addEventListener( 'elementor:inline-link-changed', handleInlineLinkChanged );
@@ -97,7 +107,7 @@ export const LinkControl = createControl( ( props: Props ) => {
97
107
  return () => {
98
108
  window.removeEventListener( 'elementor:inline-link-changed', handleInlineLinkChanged );
99
109
  };
100
- }, [ elementId, debouncedCheckRestriction ] );
110
+ }, [ elementId, debouncedSyncLinkRestriction ] );
101
111
 
102
112
  const onEnabledChange = () => {
103
113
  setLinkInLinkRestriction( getLinkInLinkRestriction( elementId, value ?? linkPlaceholder ) );
@@ -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 = Number.MAX_SAFE_INTEGER,
30
- min = -Number.MAX_SAFE_INTEGER,
31
- step = 1,
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
- const handleChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
47
- const {
48
- value: eventValue,
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
- let updatedValue;
56
+ const handleInput = ( event: React.ChangeEvent< HTMLInputElement > ) => {
57
+ const raw = event.target.value;
53
58
 
54
- if ( isEmptyOrNaN( eventValue ) ) {
55
- updatedValue = null;
56
- } else {
57
- const formattedValue = shouldForceInt ? +parseInt( eventValue ) : Number( eventValue );
59
+ setDraft( raw );
58
60
 
59
- updatedValue = Math.min(
60
- Math.max( formattedValue, min ?? Number.MIN_SAFE_INTEGER ),
61
- max ?? Number.MAX_SAFE_INTEGER
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
- setValue( updatedValue, undefined, { validation: () => isInputValid } );
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={ isEmptyOrNaN( value ) ? '' : value }
76
- onInput={ handleChange }
77
- onBlur={ restoreValue }
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 );