@elementor/editor-editing-panel 1.30.0 → 1.31.0
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/CHANGELOG.md +23 -0
- package/dist/index.d.mts +10 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +846 -609
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +750 -506
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/components/creatable-autocomplete/autocomplete-option-internal-properties.ts +21 -0
- package/src/components/creatable-autocomplete/creatable-autocomplete.tsx +175 -0
- package/src/components/creatable-autocomplete/index.ts +3 -0
- package/src/components/creatable-autocomplete/types.ts +38 -0
- package/src/components/creatable-autocomplete/use-autocomplete-change.ts +75 -0
- package/src/components/creatable-autocomplete/use-autocomplete-states.ts +42 -0
- package/src/components/creatable-autocomplete/use-create-option.ts +45 -0
- package/src/components/creatable-autocomplete/use-filter-options.ts +50 -0
- package/src/components/css-classes/css-class-item.tsx +2 -2
- package/src/components/css-classes/css-class-selector.tsx +44 -27
- package/src/components/editing-panel-tabs.tsx +19 -14
- package/src/components/style-sections/position-section/offset-field.tsx +22 -0
- package/src/components/style-sections/position-section/position-section.tsx +4 -0
- package/src/components/style-tab.tsx +26 -3
- package/src/contexts/scroll-context.tsx +60 -0
- package/src/index.ts +1 -0
- package/src/components/multi-combobox.tsx +0 -165
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elementor/editor-editing-panel",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.31.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"author": "Elementor Team",
|
|
6
6
|
"homepage": "https://elementor.com/",
|
|
@@ -39,24 +39,24 @@
|
|
|
39
39
|
"dev": "tsup --config=../../tsup.dev.ts"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@elementor/editor": "0.19.
|
|
43
|
-
"@elementor/editor-canvas": "0.19.
|
|
44
|
-
"@elementor/editor-controls": "0.
|
|
42
|
+
"@elementor/editor": "0.19.1",
|
|
43
|
+
"@elementor/editor-canvas": "0.19.1",
|
|
44
|
+
"@elementor/editor-controls": "0.28.0",
|
|
45
45
|
"@elementor/editor-current-user": "0.3.0",
|
|
46
46
|
"@elementor/editor-elements": "0.8.1",
|
|
47
|
-
"@elementor/editor-panels": "0.15.
|
|
47
|
+
"@elementor/editor-panels": "0.15.1",
|
|
48
48
|
"@elementor/editor-props": "0.12.0",
|
|
49
49
|
"@elementor/editor-responsive": "0.13.4",
|
|
50
50
|
"@elementor/editor-styles": "0.6.6",
|
|
51
|
-
"@elementor/editor-styles-repository": "0.8.
|
|
52
|
-
"@elementor/editor-ui": "0.8.
|
|
51
|
+
"@elementor/editor-styles-repository": "0.8.5",
|
|
52
|
+
"@elementor/editor-ui": "0.8.1",
|
|
53
53
|
"@elementor/editor-v1-adapters": "0.11.0",
|
|
54
|
-
"@elementor/icons": "1.
|
|
54
|
+
"@elementor/icons": "1.40.1",
|
|
55
55
|
"@elementor/locations": "0.7.7",
|
|
56
56
|
"@elementor/menus": "0.1.4",
|
|
57
57
|
"@elementor/schema": "0.1.2",
|
|
58
58
|
"@elementor/session": "0.1.0",
|
|
59
|
-
"@elementor/ui": "1.
|
|
59
|
+
"@elementor/ui": "1.34.2",
|
|
60
60
|
"@elementor/utils": "0.4.0",
|
|
61
61
|
"@wordpress/i18n": "^5.13.0"
|
|
62
62
|
},
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type InternalOption, type Option } from './types';
|
|
2
|
+
|
|
3
|
+
export function addGroupToOptions< TOption extends Option >(
|
|
4
|
+
options: TOption[],
|
|
5
|
+
pluralEntityName?: string
|
|
6
|
+
): InternalOption< TOption >[] {
|
|
7
|
+
return options.map( ( option ) => {
|
|
8
|
+
return {
|
|
9
|
+
...option,
|
|
10
|
+
_group: `Existing ${ pluralEntityName ?? 'options' }`,
|
|
11
|
+
};
|
|
12
|
+
} );
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function removeOptionsInternalKeys< TOption extends Option >( options: InternalOption< TOption >[] ): TOption[] {
|
|
16
|
+
return options.map( ( option ) => {
|
|
17
|
+
const { _group, _action, ...rest } = option;
|
|
18
|
+
|
|
19
|
+
return rest as unknown as TOption;
|
|
20
|
+
} );
|
|
21
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { type HTMLAttributes, useId, useMemo } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
Autocomplete,
|
|
5
|
+
type AutocompleteProps,
|
|
6
|
+
type AutocompleteRenderGroupParams,
|
|
7
|
+
Box,
|
|
8
|
+
Chip,
|
|
9
|
+
styled,
|
|
10
|
+
TextField,
|
|
11
|
+
type Theme,
|
|
12
|
+
Typography,
|
|
13
|
+
} from '@elementor/ui';
|
|
14
|
+
|
|
15
|
+
import { addGroupToOptions } from './autocomplete-option-internal-properties';
|
|
16
|
+
import { type CreatableAutocompleteProps, type InternalOption, type SafeOptionConstraint } from './types';
|
|
17
|
+
import { useAutocompleteChange } from './use-autocomplete-change';
|
|
18
|
+
import { useInputState, useOpenState } from './use-autocomplete-states';
|
|
19
|
+
import { useCreateOption } from './use-create-option';
|
|
20
|
+
import { useFilterOptions } from './use-filter-options';
|
|
21
|
+
|
|
22
|
+
export function CreatableAutocomplete< TOption extends SafeOptionConstraint >( {
|
|
23
|
+
selected,
|
|
24
|
+
options,
|
|
25
|
+
entityName,
|
|
26
|
+
onSelect,
|
|
27
|
+
placeholder,
|
|
28
|
+
onCreate,
|
|
29
|
+
validate,
|
|
30
|
+
...props
|
|
31
|
+
}: CreatableAutocompleteProps< TOption > ) {
|
|
32
|
+
const { inputValue, setInputValue, error, setError, handleInputChange } = useInputState( validate );
|
|
33
|
+
const { open, openDropdown, closeDropdown } = useOpenState( props.open );
|
|
34
|
+
const { createOption, loading } = useCreateOption( { onCreate, validate, setInputValue, setError, closeDropdown } );
|
|
35
|
+
|
|
36
|
+
const [ internalOptions, internalSelected ] = useMemo(
|
|
37
|
+
() => [ options, selected ].map( ( optionsArr ) => addGroupToOptions( optionsArr, entityName?.plural ) ),
|
|
38
|
+
[ options, selected, entityName?.plural ]
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const handleChange = useAutocompleteChange( {
|
|
42
|
+
options: internalOptions,
|
|
43
|
+
onSelect,
|
|
44
|
+
createOption,
|
|
45
|
+
setInputValue,
|
|
46
|
+
closeDropdown,
|
|
47
|
+
} );
|
|
48
|
+
const filterOptions = useFilterOptions( { options, selected, onCreate, entityName } );
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<Autocomplete< InternalOption< TOption >, true, true, true >
|
|
52
|
+
renderTags={ ( tagValue, getTagProps ) => {
|
|
53
|
+
return tagValue.map( ( option, index ) => (
|
|
54
|
+
<Chip
|
|
55
|
+
size="tiny"
|
|
56
|
+
{ ...getTagProps( { index } ) }
|
|
57
|
+
key={ option.key ?? option.value ?? option.label }
|
|
58
|
+
label={ option.label }
|
|
59
|
+
/>
|
|
60
|
+
) );
|
|
61
|
+
} }
|
|
62
|
+
{ ...( props as AutocompleteProps< InternalOption< TOption >, true, true, true > ) }
|
|
63
|
+
freeSolo
|
|
64
|
+
multiple
|
|
65
|
+
clearOnBlur
|
|
66
|
+
selectOnFocus
|
|
67
|
+
disableClearable
|
|
68
|
+
handleHomeEndKeys
|
|
69
|
+
disabled={ loading }
|
|
70
|
+
open={ open }
|
|
71
|
+
onOpen={ openDropdown }
|
|
72
|
+
onClose={ closeDropdown }
|
|
73
|
+
disableCloseOnSelect
|
|
74
|
+
value={ internalSelected }
|
|
75
|
+
options={ internalOptions }
|
|
76
|
+
ListboxComponent={
|
|
77
|
+
error
|
|
78
|
+
? React.forwardRef< HTMLElement, ErrorTextProps >( ( _, ref ) => (
|
|
79
|
+
<ErrorText ref={ ref } error={ error } />
|
|
80
|
+
) )
|
|
81
|
+
: undefined
|
|
82
|
+
}
|
|
83
|
+
renderGroup={ ( params ) => <Group { ...params } /> }
|
|
84
|
+
inputValue={ inputValue }
|
|
85
|
+
renderInput={ ( params ) => {
|
|
86
|
+
return (
|
|
87
|
+
<TextField
|
|
88
|
+
{ ...params }
|
|
89
|
+
placeholder={ placeholder }
|
|
90
|
+
error={ Boolean( error ) }
|
|
91
|
+
onChange={ handleInputChange }
|
|
92
|
+
sx={ ( theme: Theme ) => ( {
|
|
93
|
+
'.MuiAutocomplete-inputRoot.MuiInputBase-adornedStart': {
|
|
94
|
+
paddingLeft: theme.spacing( 0.25 ),
|
|
95
|
+
paddingRight: theme.spacing( 0.25 ),
|
|
96
|
+
},
|
|
97
|
+
} ) }
|
|
98
|
+
/>
|
|
99
|
+
);
|
|
100
|
+
} }
|
|
101
|
+
onChange={ handleChange }
|
|
102
|
+
getOptionLabel={ ( option ) => ( typeof option === 'string' ? option : option.label ) }
|
|
103
|
+
getOptionKey={ ( option ) => {
|
|
104
|
+
if ( typeof option === 'string' ) {
|
|
105
|
+
return option;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return option.key ?? option.value ?? option.label;
|
|
109
|
+
} }
|
|
110
|
+
filterOptions={ filterOptions }
|
|
111
|
+
groupBy={ ( option ) => option._group ?? '' }
|
|
112
|
+
renderOption={ ( optionProps, option ) => {
|
|
113
|
+
const { _group, label } = option;
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<li
|
|
117
|
+
{ ...optionProps }
|
|
118
|
+
style={ { display: 'block', textOverflow: 'ellipsis' } }
|
|
119
|
+
data-group={ _group }
|
|
120
|
+
>
|
|
121
|
+
{ label }
|
|
122
|
+
</li>
|
|
123
|
+
);
|
|
124
|
+
} }
|
|
125
|
+
/>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const Group = ( params: Omit< AutocompleteRenderGroupParams, 'key' > ) => {
|
|
130
|
+
const id = `combobox-group-${ useId().replace( /:/g, '_' ) }`;
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<StyledGroup role="group" aria-labelledby={ id }>
|
|
134
|
+
<StyledGroupHeader id={ id }> { params.group }</StyledGroupHeader>
|
|
135
|
+
<StyledGroupItems role="listbox">{ params.children }</StyledGroupItems>
|
|
136
|
+
</StyledGroup>
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
type ErrorTextProps = {
|
|
141
|
+
error?: string;
|
|
142
|
+
} & HTMLAttributes< HTMLElement >;
|
|
143
|
+
|
|
144
|
+
const ErrorText = React.forwardRef< HTMLElement, ErrorTextProps >( ( { error = 'error' }, ref ) => {
|
|
145
|
+
return (
|
|
146
|
+
<Box
|
|
147
|
+
ref={ ref }
|
|
148
|
+
sx={ ( theme: Theme ) => ( {
|
|
149
|
+
padding: theme.spacing( 2 ),
|
|
150
|
+
} ) }
|
|
151
|
+
>
|
|
152
|
+
<Typography variant="caption" sx={ { color: 'error.main' } }>
|
|
153
|
+
{ error }
|
|
154
|
+
</Typography>
|
|
155
|
+
</Box>
|
|
156
|
+
);
|
|
157
|
+
} );
|
|
158
|
+
|
|
159
|
+
const StyledGroup = styled( 'li' )`
|
|
160
|
+
&:not( :last-of-type ) {
|
|
161
|
+
border-bottom: 1px solid ${ ( { theme } ) => theme.palette.divider };
|
|
162
|
+
}
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
const StyledGroupHeader = styled( Box )( ( { theme } ) => ( {
|
|
166
|
+
position: 'sticky',
|
|
167
|
+
top: '-8px',
|
|
168
|
+
padding: theme.spacing( 1, 2 ),
|
|
169
|
+
color: theme.palette.text.tertiary,
|
|
170
|
+
backgroundColor: theme.palette.primary.contrastText,
|
|
171
|
+
} ) );
|
|
172
|
+
|
|
173
|
+
const StyledGroupItems = styled( 'ul' )`
|
|
174
|
+
padding: 0;
|
|
175
|
+
`;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type AutocompleteProps } from '@elementor/ui';
|
|
2
|
+
|
|
3
|
+
export type Option = {
|
|
4
|
+
label: string;
|
|
5
|
+
value: string | null;
|
|
6
|
+
fixed?: boolean;
|
|
7
|
+
key?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type InternalKeys = '_group' | '_action';
|
|
11
|
+
|
|
12
|
+
export type InternalOption< TOption extends Option > = Omit< TOption, InternalKeys > & {
|
|
13
|
+
_group: string;
|
|
14
|
+
_action?: 'create';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type SafeOptionConstraint = Option & {
|
|
18
|
+
[ K in InternalKeys ]?: never;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ValidationResult = { isValid: true; errorMessage: null } | { isValid: false; errorMessage: string };
|
|
22
|
+
export type ValidationEvent = 'inputChange' | 'create';
|
|
23
|
+
|
|
24
|
+
export type CreatableAutocompleteProps< TOption extends SafeOptionConstraint > = Omit<
|
|
25
|
+
AutocompleteProps< TOption, true, true, true >,
|
|
26
|
+
'renderInput' | 'onSelect' | 'options'
|
|
27
|
+
> & {
|
|
28
|
+
selected: TOption[];
|
|
29
|
+
options: TOption[];
|
|
30
|
+
placeholder?: string;
|
|
31
|
+
entityName?: {
|
|
32
|
+
singular: string;
|
|
33
|
+
plural: string;
|
|
34
|
+
};
|
|
35
|
+
onSelect?: ( value: TOption[] ) => void;
|
|
36
|
+
onCreate?: ( value: string ) => unknown;
|
|
37
|
+
validate?: ( value: string, event: ValidationEvent ) => ValidationResult;
|
|
38
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { removeOptionsInternalKeys } from './autocomplete-option-internal-properties';
|
|
2
|
+
import { type InternalOption, type Option } from './types';
|
|
3
|
+
|
|
4
|
+
export function useAutocompleteChange< TOption extends Option >( params: {
|
|
5
|
+
options: InternalOption< TOption >[];
|
|
6
|
+
onSelect?: ( value: TOption[] ) => void;
|
|
7
|
+
createOption: ( value: string ) => Promise< unknown >;
|
|
8
|
+
setInputValue: ( value: string ) => void;
|
|
9
|
+
closeDropdown: () => void;
|
|
10
|
+
} ) {
|
|
11
|
+
const { options, onSelect, createOption, setInputValue, closeDropdown } = params;
|
|
12
|
+
|
|
13
|
+
const handleChange = async (
|
|
14
|
+
_: React.SyntheticEvent,
|
|
15
|
+
selectedOrInputValue: Array< InternalOption< TOption > | string >,
|
|
16
|
+
reason: string
|
|
17
|
+
) => {
|
|
18
|
+
// Separate options and new input value
|
|
19
|
+
const selectedOptions = selectedOrInputValue.filter( ( option ) => typeof option !== 'string' );
|
|
20
|
+
|
|
21
|
+
const newInputValue = selectedOrInputValue.reduce( ( acc: string | null, option ): string | null => {
|
|
22
|
+
if ( typeof option === 'string' ) {
|
|
23
|
+
return option;
|
|
24
|
+
} else if ( option._action === 'create' ) {
|
|
25
|
+
return option.value;
|
|
26
|
+
}
|
|
27
|
+
return acc;
|
|
28
|
+
}, null );
|
|
29
|
+
|
|
30
|
+
const inputValueMatchesExistingOption =
|
|
31
|
+
newInputValue && options.find( ( option ) => option.label === newInputValue );
|
|
32
|
+
|
|
33
|
+
// Handle creation of new option
|
|
34
|
+
if (
|
|
35
|
+
newInputValue &&
|
|
36
|
+
shouldCreateNewOption( reason, selectedOptions, newInputValue, Boolean( inputValueMatchesExistingOption ) )
|
|
37
|
+
) {
|
|
38
|
+
return createOption( newInputValue );
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Handle selection of existing option
|
|
42
|
+
if ( reason === 'createOption' && inputValueMatchesExistingOption ) {
|
|
43
|
+
selectedOptions.push( inputValueMatchesExistingOption );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
updateSelectedOptions( selectedOptions );
|
|
47
|
+
setInputValue( '' );
|
|
48
|
+
closeDropdown();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return handleChange;
|
|
52
|
+
|
|
53
|
+
function shouldCreateNewOption(
|
|
54
|
+
reason: string,
|
|
55
|
+
selectedOptions: InternalOption< TOption >[],
|
|
56
|
+
newInputValue: string | undefined,
|
|
57
|
+
inputValueMatchesExistingOption: boolean
|
|
58
|
+
) {
|
|
59
|
+
const createOptionWasClicked =
|
|
60
|
+
reason === 'selectOption' && selectedOptions.some( ( option ) => option._action === 'create' );
|
|
61
|
+
|
|
62
|
+
const enterWasPressed =
|
|
63
|
+
reason === 'createOption' && ! options.some( ( option ) => option.label === newInputValue );
|
|
64
|
+
const createOptionWasDisplayed = ! inputValueMatchesExistingOption;
|
|
65
|
+
|
|
66
|
+
return createOptionWasClicked || ( enterWasPressed && createOptionWasDisplayed );
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function updateSelectedOptions( selectedOptions: InternalOption< TOption >[] ) {
|
|
70
|
+
const fixedOptions = options.filter( ( option ) => !! option.fixed );
|
|
71
|
+
const updatedOptions = [ ...fixedOptions, ...selectedOptions.filter( ( option ) => ! option.fixed ) ];
|
|
72
|
+
|
|
73
|
+
onSelect?.( removeOptionsInternalKeys( updatedOptions ) );
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { type ValidationEvent, type ValidationResult } from './types';
|
|
4
|
+
|
|
5
|
+
export function useInputState( validate?: ( value: string, event: ValidationEvent ) => ValidationResult ) {
|
|
6
|
+
const [ inputValue, setInputValue ] = useState( '' );
|
|
7
|
+
const [ error, setError ] = useState< string | null >( null );
|
|
8
|
+
|
|
9
|
+
const handleInputChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
10
|
+
const { value } = event.target;
|
|
11
|
+
|
|
12
|
+
setInputValue( value );
|
|
13
|
+
|
|
14
|
+
if ( ! validate ) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if ( ! value ) {
|
|
19
|
+
setError( null );
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const { isValid, errorMessage } = validate( value, 'inputChange' );
|
|
24
|
+
|
|
25
|
+
if ( isValid ) {
|
|
26
|
+
setError( null );
|
|
27
|
+
} else {
|
|
28
|
+
setError( errorMessage );
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return { inputValue, setInputValue, error, setError, handleInputChange };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useOpenState( initialOpen: boolean = false ) {
|
|
36
|
+
const [ open, setOpen ] = useState( initialOpen );
|
|
37
|
+
|
|
38
|
+
const openDropdown = () => setOpen( true );
|
|
39
|
+
const closeDropdown = () => setOpen( false );
|
|
40
|
+
|
|
41
|
+
return { open, openDropdown, closeDropdown };
|
|
42
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { type ValidationEvent, type ValidationResult } from './types';
|
|
4
|
+
|
|
5
|
+
export function useCreateOption( params: {
|
|
6
|
+
onCreate?: ( value: string ) => Promise< unknown > | unknown;
|
|
7
|
+
validate?: ( value: string, event: ValidationEvent ) => ValidationResult;
|
|
8
|
+
setInputValue: ( value: string ) => void;
|
|
9
|
+
setError: ( error: string | null ) => void;
|
|
10
|
+
closeDropdown: () => void;
|
|
11
|
+
} ) {
|
|
12
|
+
const { onCreate, validate, setInputValue, setError, closeDropdown } = params;
|
|
13
|
+
|
|
14
|
+
const [ loading, setLoading ] = useState( false );
|
|
15
|
+
|
|
16
|
+
const createOption = async ( value: string ) => {
|
|
17
|
+
if ( ! onCreate ) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
setLoading( true );
|
|
22
|
+
|
|
23
|
+
if ( validate ) {
|
|
24
|
+
const { isValid, errorMessage } = validate( value, 'create' );
|
|
25
|
+
|
|
26
|
+
if ( ! isValid ) {
|
|
27
|
+
setError( errorMessage );
|
|
28
|
+
setLoading( false );
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
setInputValue( '' );
|
|
35
|
+
closeDropdown();
|
|
36
|
+
await onCreate( value );
|
|
37
|
+
} catch {
|
|
38
|
+
// TODO: Do something with the error.
|
|
39
|
+
} finally {
|
|
40
|
+
setLoading( false );
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return { createOption, loading };
|
|
45
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createFilterOptions } from '@elementor/ui';
|
|
2
|
+
|
|
3
|
+
import { type InternalOption, type Option } from './types';
|
|
4
|
+
|
|
5
|
+
export function useFilterOptions< TOption extends Option >( parameters: {
|
|
6
|
+
options: TOption[];
|
|
7
|
+
selected: TOption[];
|
|
8
|
+
onCreate?: ( value: string ) => Promise< unknown > | unknown;
|
|
9
|
+
entityName?: { singular: string; plural: string };
|
|
10
|
+
} ) {
|
|
11
|
+
const { options, selected, onCreate, entityName } = parameters;
|
|
12
|
+
|
|
13
|
+
const filter = createFilterOptions< InternalOption< TOption > >();
|
|
14
|
+
|
|
15
|
+
const filterOptions = (
|
|
16
|
+
optionList: InternalOption< TOption >[],
|
|
17
|
+
params: {
|
|
18
|
+
inputValue: string;
|
|
19
|
+
getOptionLabel: ( option: InternalOption< TOption > ) => string;
|
|
20
|
+
}
|
|
21
|
+
) => {
|
|
22
|
+
const selectedValues = selected.map( ( option ) => option.value );
|
|
23
|
+
|
|
24
|
+
const filteredOptions = filter(
|
|
25
|
+
optionList.filter( ( option ) => ! selectedValues.includes( option.value ) ),
|
|
26
|
+
params
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const isExisting = options.some( ( option ) => params.inputValue === option.label );
|
|
30
|
+
const allowCreate =
|
|
31
|
+
Boolean( onCreate ) &&
|
|
32
|
+
params.inputValue !== '' &&
|
|
33
|
+
! selectedValues.includes( params.inputValue ) &&
|
|
34
|
+
! isExisting;
|
|
35
|
+
|
|
36
|
+
if ( allowCreate ) {
|
|
37
|
+
filteredOptions.unshift( {
|
|
38
|
+
label: `Create "${ params.inputValue }"`,
|
|
39
|
+
value: params.inputValue,
|
|
40
|
+
_group: `Create a new ${ entityName?.singular ?? 'option' }`,
|
|
41
|
+
key: `create-${ params.inputValue }`,
|
|
42
|
+
_action: 'create',
|
|
43
|
+
} as InternalOption< TOption > );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return filteredOptions;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return filterOptions;
|
|
50
|
+
}
|
|
@@ -154,11 +154,11 @@ export function CssClassItem( {
|
|
|
154
154
|
}
|
|
155
155
|
|
|
156
156
|
const validateLabel = ( newLabel: string ) => {
|
|
157
|
-
const result = validateStyleLabel( newLabel );
|
|
157
|
+
const result = validateStyleLabel( newLabel, 'rename' );
|
|
158
158
|
|
|
159
159
|
if ( result.isValid ) {
|
|
160
160
|
return null;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
return result.
|
|
163
|
+
return result.errorMessage;
|
|
164
164
|
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
type StylesProvider,
|
|
9
9
|
stylesRepository,
|
|
10
10
|
type UpdateActionPayload,
|
|
11
|
-
|
|
11
|
+
useGetStylesRepositoryCreateAction,
|
|
12
12
|
useProviders,
|
|
13
13
|
validateStyleLabel,
|
|
14
14
|
} from '@elementor/editor-styles-repository';
|
|
@@ -20,7 +20,13 @@ import { __ } from '@wordpress/i18n';
|
|
|
20
20
|
import { useClassesProp } from '../../contexts/classes-prop-context';
|
|
21
21
|
import { useElement } from '../../contexts/element-context';
|
|
22
22
|
import { useStyle } from '../../contexts/style-context';
|
|
23
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
CreatableAutocomplete,
|
|
25
|
+
type CreatableAutocompleteProps,
|
|
26
|
+
type Option,
|
|
27
|
+
type ValidationEvent,
|
|
28
|
+
type ValidationResult,
|
|
29
|
+
} from '../creatable-autocomplete';
|
|
24
30
|
import { CssClassItem } from './css-class-item';
|
|
25
31
|
|
|
26
32
|
const ID = 'elementor-css-class-selector';
|
|
@@ -54,9 +60,8 @@ export function CssClassSelector() {
|
|
|
54
60
|
const { value: appliedIds, setValue: setAppliedIds, pushValue: pushAppliedId } = useAppliedClassesIds();
|
|
55
61
|
const { id: activeId, setId: setActiveId } = useStyle();
|
|
56
62
|
|
|
57
|
-
const actions = useCreateActions( { pushAppliedId, setActiveId } );
|
|
58
|
-
|
|
59
63
|
const handleApply = useHandleApply( appliedIds, setAppliedIds );
|
|
64
|
+
const { create, validate, entityName } = useCreateAction( { pushAppliedId, setActiveId } );
|
|
60
65
|
|
|
61
66
|
const applied = useAppliedOptions( options, appliedIds );
|
|
62
67
|
const active = applied.find( ( option ) => option.value === activeId ) ?? EMPTY_OPTION;
|
|
@@ -73,15 +78,17 @@ export function CssClassSelector() {
|
|
|
73
78
|
<ClassSelectorActionsSlot />
|
|
74
79
|
</Stack>
|
|
75
80
|
</Stack>
|
|
76
|
-
<
|
|
81
|
+
<CreatableAutocomplete
|
|
77
82
|
id={ ID }
|
|
78
83
|
size="tiny"
|
|
79
84
|
placeholder={ showPlaceholder ? __( 'Type class name', 'elementor' ) : undefined }
|
|
80
85
|
options={ options }
|
|
81
86
|
selected={ applied }
|
|
87
|
+
entityName={ entityName }
|
|
82
88
|
onSelect={ handleApply }
|
|
89
|
+
onCreate={ create ?? undefined }
|
|
90
|
+
validate={ validate ?? undefined }
|
|
83
91
|
limitTags={ TAGS_LIMIT }
|
|
84
|
-
actions={ actions }
|
|
85
92
|
getLimitTagsText={ ( more ) => (
|
|
86
93
|
<Chip size="tiny" variant="standard" label={ `+${ more }` } clickable />
|
|
87
94
|
) }
|
|
@@ -156,39 +163,49 @@ function useOptions() {
|
|
|
156
163
|
color: isElements ? 'accent' : 'global',
|
|
157
164
|
icon: isElements ? <MapPinIcon /> : null,
|
|
158
165
|
provider: provider.getKey(),
|
|
159
|
-
// translators: %s is the plural label of the provider (e.g "Existing classes").
|
|
160
|
-
group: __( 'Existing %s', 'elementor' ).replace( '%s', provider.labels?.plural ?? '' ),
|
|
161
166
|
};
|
|
162
167
|
} );
|
|
163
168
|
} );
|
|
164
169
|
}
|
|
165
170
|
|
|
166
|
-
function
|
|
171
|
+
function useCreateAction( {
|
|
167
172
|
pushAppliedId,
|
|
168
173
|
setActiveId,
|
|
169
174
|
}: {
|
|
170
175
|
pushAppliedId: ( id: StyleDefinitionID ) => void;
|
|
171
176
|
setActiveId: ( id: StyleDefinitionID ) => void;
|
|
172
177
|
} ) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// translators: %s is the singular label of css class provider (e.g "CSS Class").
|
|
178
|
-
group: __( 'Create a new %s', 'elementor' ).replace( '%s', provider.labels?.singular ?? '' ),
|
|
179
|
-
condition: ( _, inputValue ) => validateStyleLabel( inputValue ).isValid && ! hasReachedLimit( provider ),
|
|
180
|
-
apply: ( label ) => {
|
|
181
|
-
const createdId = create( label );
|
|
182
|
-
|
|
183
|
-
if ( ! createdId ) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
178
|
+
const [ provider, createAction ] = useGetStylesRepositoryCreateAction() ?? [ null, null ];
|
|
179
|
+
if ( ! provider || ! createAction ) {
|
|
180
|
+
return {};
|
|
181
|
+
}
|
|
186
182
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
183
|
+
const create = ( newClassLabel: string ) => {
|
|
184
|
+
const createdId = createAction( newClassLabel );
|
|
185
|
+
|
|
186
|
+
pushAppliedId( createdId );
|
|
187
|
+
setActiveId( createdId );
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const validate = ( newClassLabel: string, event: ValidationEvent ): ValidationResult => {
|
|
191
|
+
if ( hasReachedLimit( provider ) ) {
|
|
192
|
+
return {
|
|
193
|
+
isValid: false,
|
|
194
|
+
errorMessage: __(
|
|
195
|
+
'You’ve reached the limit of 50 classes. Please remove an existing one to create a new class.',
|
|
196
|
+
'elementor'
|
|
197
|
+
),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return validateStyleLabel( newClassLabel, event );
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const entityName =
|
|
204
|
+
provider.labels.singular && provider.labels.plural
|
|
205
|
+
? ( provider.labels as CreatableAutocompleteProps< StyleDefOption >[ 'entityName' ] )
|
|
206
|
+
: undefined;
|
|
207
|
+
|
|
208
|
+
return { create, validate, entityName };
|
|
192
209
|
}
|
|
193
210
|
|
|
194
211
|
function hasReachedLimit( provider: StylesProvider ) {
|
|
@@ -4,8 +4,9 @@ import { Divider, Stack, Tab, TabPanel, Tabs, useTabs } from '@elementor/ui';
|
|
|
4
4
|
import { __ } from '@wordpress/i18n';
|
|
5
5
|
|
|
6
6
|
import { useElement } from '../contexts/element-context';
|
|
7
|
+
import { ScrollProvider } from '../contexts/scroll-context';
|
|
7
8
|
import { SettingsTab } from './settings-tab';
|
|
8
|
-
import { StyleTab } from './style-tab';
|
|
9
|
+
import { stickyHeaderStyles, StyleTab } from './style-tab';
|
|
9
10
|
|
|
10
11
|
type TabValue = 'settings' | 'style';
|
|
11
12
|
|
|
@@ -18,19 +19,23 @@ export const EditingPanelTabs = () => {
|
|
|
18
19
|
// When switching between elements, the local states should be reset. We are using key to rerender the tabs.
|
|
19
20
|
// Reference: https://react.dev/learn/preserving-and-resetting-state#resetting-a-form-with-a-key
|
|
20
21
|
<Fragment key={ element.id }>
|
|
21
|
-
<
|
|
22
|
-
<
|
|
23
|
-
<
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
22
|
+
<ScrollProvider>
|
|
23
|
+
<Stack direction="column" sx={ { width: '100%' } }>
|
|
24
|
+
<Stack sx={ { ...stickyHeaderStyles, top: 0 } }>
|
|
25
|
+
<Tabs variant="fullWidth" size="small" sx={ { mt: 0.5 } } { ...getTabsProps() }>
|
|
26
|
+
<Tab label={ __( 'General', 'elementor' ) } { ...getTabProps( 'settings' ) } />
|
|
27
|
+
<Tab label={ __( 'Style', 'elementor' ) } { ...getTabProps( 'style' ) } />
|
|
28
|
+
</Tabs>
|
|
29
|
+
<Divider />
|
|
30
|
+
</Stack>
|
|
31
|
+
<TabPanel { ...getTabPanelProps( 'settings' ) } disablePadding>
|
|
32
|
+
<SettingsTab />
|
|
33
|
+
</TabPanel>
|
|
34
|
+
<TabPanel { ...getTabPanelProps( 'style' ) } disablePadding>
|
|
35
|
+
<StyleTab />
|
|
36
|
+
</TabPanel>
|
|
37
|
+
</Stack>
|
|
38
|
+
</ScrollProvider>
|
|
34
39
|
</Fragment>
|
|
35
40
|
);
|
|
36
41
|
};
|