@elementor/editor-controls 4.3.0-998 → 4.4.0-1064

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.
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { stringPropTypeUtil, type StringPropValue } from '@elementor/editor-props';
3
3
  import { MenuListItem } from '@elementor/editor-ui';
4
- import { Select, type SelectChangeEvent, type SelectProps, Typography } from '@elementor/ui';
4
+ import { MenuSubheader, Select, type SelectChangeEvent, type SelectProps, Typography } from '@elementor/ui';
5
5
 
6
6
  import { useBoundProp } from '../bound-prop-context';
7
7
  import ControlActions from '../control-actions/control-actions';
@@ -13,8 +13,14 @@ export type SelectOption = {
13
13
  disabled?: boolean;
14
14
  };
15
15
 
16
- type SelectControlProps = {
16
+ export type SelectOptionGroup = {
17
+ label: string;
17
18
  options: SelectOption[];
19
+ };
20
+
21
+ type SelectControlProps = {
22
+ options?: SelectOption[];
23
+ groups?: SelectOptionGroup[];
18
24
  onChange?: ( newValue: string | null, previousValue: string | null | undefined ) => void;
19
25
  MenuProps?: SelectProps[ 'MenuProps' ];
20
26
  ariaLabel?: string;
@@ -29,7 +35,7 @@ const DEFAULT_MENU_PROPS = {
29
35
  };
30
36
 
31
37
  export const SelectControl = createControl(
32
- ( { options, onChange, MenuProps = DEFAULT_MENU_PROPS, ariaLabel }: SelectControlProps ) => {
38
+ ( { options = [], groups = [], onChange, MenuProps = DEFAULT_MENU_PROPS, ariaLabel }: SelectControlProps ) => {
33
39
  const { value, setValue, disabled, placeholder } = useBoundProp( stringPropTypeUtil );
34
40
  const handleChange = ( event: SelectChangeEvent< StringPropValue[ 'value' ] > ) => {
35
41
  const newValue = event.target.value || null;
@@ -37,7 +43,8 @@ export const SelectControl = createControl(
37
43
  onChange?.( newValue, value );
38
44
  setValue( newValue );
39
45
  };
40
- const isDisabled = disabled || options.length === 0;
46
+ const flatOptions = flattenGroupedOptions( options, groups );
47
+ const isDisabled = disabled || flatOptions.length === 0;
41
48
 
42
49
  return (
43
50
  <ControlActions>
@@ -48,24 +55,54 @@ export const SelectControl = createControl(
48
55
  MenuProps={ MenuProps }
49
56
  aria-label={ ariaLabel || placeholder }
50
57
  renderValue={ ( selectedValue: string | null ) =>
51
- getSelectRenderValue( options, placeholder, selectedValue )
58
+ getSelectRenderValue( flatOptions, placeholder, selectedValue )
52
59
  }
53
60
  value={ value ?? '' }
54
61
  onChange={ handleChange }
55
62
  disabled={ isDisabled }
56
63
  fullWidth
57
64
  >
58
- { options.map( ( { label, ...props } ) => (
59
- <MenuListItem key={ props.value } { ...props } value={ props.value ?? '' }>
60
- { label }
61
- </MenuListItem>
62
- ) ) }
65
+ { groups.length
66
+ ? groups.flatMap( ( group ) => renderGroup( group ) )
67
+ : options.map( ( option ) => renderOption( option ) ) }
63
68
  </Select>
64
69
  </ControlActions>
65
70
  );
66
71
  }
67
72
  );
68
73
 
74
+ const GROUPED_OPTION_INDENT = 3.5;
75
+
76
+ function renderGroup( group: SelectOptionGroup ): React.ReactNode[] {
77
+ return [
78
+ <MenuSubheader key={ `group-${ group.label }` } sx={ { fontWeight: 400, color: 'text.tertiary' } }>
79
+ { group.label }
80
+ </MenuSubheader>,
81
+ ...group.options.map( ( option ) => renderOption( option, true ) ),
82
+ ];
83
+ }
84
+
85
+ function renderOption( { label, ...props }: SelectOption, isGrouped = false ): React.ReactNode {
86
+ return (
87
+ <MenuListItem
88
+ key={ props.value }
89
+ { ...props }
90
+ value={ props.value ?? '' }
91
+ sx={ isGrouped ? { pl: GROUPED_OPTION_INDENT } : undefined }
92
+ >
93
+ { label }
94
+ </MenuListItem>
95
+ );
96
+ }
97
+
98
+ function flattenGroupedOptions( options: SelectOption[], groups: SelectOptionGroup[] ): SelectOption[] {
99
+ if ( ! groups.length ) {
100
+ return options;
101
+ }
102
+
103
+ return groups.flatMap( ( group ) => group.options );
104
+ }
105
+
69
106
  export function getSelectRenderValue(
70
107
  options: SelectOption[],
71
108
  placeholder: string | null | undefined,
@@ -1,9 +1,19 @@
1
1
  import * as React from 'react';
2
- import { useState } from 'react';
2
+ import { useEffect, useState } from 'react';
3
3
  import { useCurrentUserCapabilities } from '@elementor/editor-current-user';
4
- import { svgSrcPropTypeUtil, urlPropTypeUtil } from '@elementor/editor-props';
4
+ import { iconPropTypeUtil, svgSrcPropTypeUtil, urlPropTypeUtil } from '@elementor/editor-props';
5
5
  import { UploadIcon } from '@elementor/icons';
6
- import { Button, Card, CardMedia, CardOverlay, CircularProgress, Stack, styled, ThemeProvider } from '@elementor/ui';
6
+ import {
7
+ Box,
8
+ Button,
9
+ Card,
10
+ CardMedia,
11
+ CardOverlay,
12
+ CircularProgress,
13
+ Stack,
14
+ styled,
15
+ ThemeProvider,
16
+ } from '@elementor/ui';
7
17
  import { type OpenOptions, useWpMediaAttachment, useWpMediaFrame } from '@elementor/wp-media';
8
18
  import { __ } from '@wordpress/i18n';
9
19
 
@@ -13,10 +23,18 @@ import { EnableUnfilteredModal } from '../components/enable-unfiltered-modal';
13
23
  import ControlActions from '../control-actions/control-actions';
14
24
  import { createControl } from '../create-control';
15
25
  import { useUnfilteredFilesUpload } from '../hooks/use-unfiltered-files-upload';
26
+ import {
27
+ createIconPropValue,
28
+ enqueueIconFonts,
29
+ type IconLibrarySelection,
30
+ isSvgLibrarySelection,
31
+ openIconLibrary,
32
+ } from './open-icon-library';
16
33
 
17
34
  const TILE_SIZE = 8;
18
35
  const TILE_WHITE = 'transparent';
19
36
  const TILE_BLACK = '#c1c1c1';
37
+ const ICON_PREVIEW_FONT_SIZE = 50;
20
38
  export const TILES_GRADIENT_FORMULA = `linear-gradient(45deg, ${ TILE_BLACK } 25%, ${ TILE_WHITE } 0, ${ TILE_WHITE } 75%, ${ TILE_BLACK } 0, ${ TILE_BLACK })`;
21
39
 
22
40
  const StyledCard = styled( Card )`
@@ -42,22 +60,31 @@ const StyledCardMediaContainer = styled( Stack )`
42
60
  const MODE_BROWSE: OpenOptions = { mode: 'browse' };
43
61
  const MODE_UPLOAD: OpenOptions = { mode: 'upload' };
44
62
 
45
- export const SvgMediaControl = createControl( () => {
46
- const { value, setValue } = useBoundProp( svgSrcPropTypeUtil );
47
- const id = value?.id;
48
- const url = value?.url;
63
+ type SvgMediaControlProps = {
64
+ showIconLibrary?: boolean;
65
+ };
66
+
67
+ export const SvgMediaControl = createControl( ( { showIconLibrary = false }: SvgMediaControlProps ) => {
68
+ const { value: svgValue, setValue: setSvgValue } = useBoundProp( svgSrcPropTypeUtil );
69
+ const { value: iconValue, setValue: setIconValue } = useBoundProp( iconPropTypeUtil );
70
+ const id = svgValue?.id;
71
+ const url = svgValue?.url;
49
72
  const { data: attachment, isFetching } = useWpMediaAttachment( id?.value || null );
50
73
  const src = attachment?.url ?? url?.value ?? null;
51
74
  const { data: allowSvgUpload } = useUnfilteredFilesUpload();
52
75
  const [ unfilteredModalOpenState, setUnfilteredModalOpenState ] = useState( false );
53
76
  const { isAdmin } = useCurrentUserCapabilities();
77
+ const selectedIconClass =
78
+ showIconLibrary && typeof iconValue?.value?.value === 'string' ? iconValue.value.value : null;
79
+ const selectedIconLibrary =
80
+ showIconLibrary && typeof iconValue?.library?.value === 'string' ? iconValue.library.value : null;
54
81
 
55
82
  const { open } = useWpMediaFrame( {
56
83
  mediaTypes: [ 'svg' ],
57
84
  multiple: false,
58
85
  selected: id?.value || null,
59
86
  onSelect: ( selectedAttachment ) => {
60
- setValue( {
87
+ setSvgValue( {
61
88
  id: {
62
89
  $$type: 'image-attachment-id',
63
90
  value: selectedAttachment.id,
@@ -83,6 +110,28 @@ export const SvgMediaControl = createControl( () => {
83
110
  }
84
111
  };
85
112
 
113
+ const handleIconLibrarySelect = ( icon: IconLibrarySelection ) => {
114
+ if ( ! showIconLibrary ) {
115
+ return;
116
+ }
117
+ if ( isSvgLibrarySelection( icon ) ) {
118
+ setSvgValue( {
119
+ id: icon.value.id
120
+ ? {
121
+ $$type: 'image-attachment-id',
122
+ value: icon.value.id,
123
+ }
124
+ : null,
125
+ url: icon.value.url ? urlPropTypeUtil.create( icon.value.url ) : null,
126
+ } );
127
+ return;
128
+ }
129
+
130
+ if ( typeof icon.value === 'string' ) {
131
+ setIconValue( createIconPropValue( icon.value, icon.library ) );
132
+ }
133
+ };
134
+
86
135
  const infotipProps = {
87
136
  title: __( "Sorry, you can't upload that file yet.", 'elementor' ),
88
137
  description: (
@@ -101,16 +150,12 @@ export const SvgMediaControl = createControl( () => {
101
150
  <ControlActions>
102
151
  <StyledCard variant="outlined">
103
152
  <StyledCardMediaContainer>
104
- { isFetching ? (
105
- <CircularProgress role="progressbar" />
106
- ) : (
107
- <CardMedia
108
- component="img"
109
- image={ src }
110
- alt={ __( 'Preview SVG', 'elementor' ) }
111
- sx={ { maxHeight: '140px', width: '50px' } }
112
- />
113
- ) }
153
+ <SvgMediaPreview
154
+ isFetching={ isFetching }
155
+ src={ src }
156
+ iconClassName={ selectedIconClass }
157
+ iconLibrary={ selectedIconLibrary }
158
+ />
114
159
  </StyledCardMediaContainer>
115
160
  <CardOverlay
116
161
  sx={ {
@@ -129,6 +174,25 @@ export const SvgMediaControl = createControl( () => {
129
174
  >
130
175
  { __( 'Select SVG', 'elementor' ) }
131
176
  </Button>
177
+ { showIconLibrary ? (
178
+ <Button
179
+ size="tiny"
180
+ variant="text"
181
+ color="inherit"
182
+ onClick={ () =>
183
+ openIconLibrary( {
184
+ selected:
185
+ selectedIconClass && selectedIconLibrary
186
+ ? { value: selectedIconClass, library: selectedIconLibrary }
187
+ : undefined,
188
+ onSelect: handleIconLibrarySelect,
189
+ } )
190
+ }
191
+ aria-label={ __( 'Icon library', 'elementor' ) }
192
+ >
193
+ { __( 'Icon library', 'elementor' ) }
194
+ </Button>
195
+ ) : null }
132
196
  <ConditionalControlInfotip { ...infotipProps }>
133
197
  <span>
134
198
  <ThemeProvider colorScheme={ isAdmin ? 'light' : 'dark' }>
@@ -153,3 +217,45 @@ export const SvgMediaControl = createControl( () => {
153
217
  </Stack>
154
218
  );
155
219
  } );
220
+
221
+ function SvgMediaPreview( {
222
+ isFetching,
223
+ src,
224
+ iconClassName,
225
+ iconLibrary,
226
+ }: {
227
+ isFetching: boolean;
228
+ src: string | null;
229
+ iconClassName: string | null;
230
+ iconLibrary: string | null;
231
+ } ) {
232
+ useEffect( () => {
233
+ if ( iconLibrary ) {
234
+ enqueueIconFonts( iconLibrary );
235
+ }
236
+ }, [ iconLibrary ] );
237
+
238
+ if ( isFetching ) {
239
+ return <CircularProgress role="progressbar" />;
240
+ }
241
+
242
+ if ( iconClassName ) {
243
+ return (
244
+ <Box
245
+ component="i"
246
+ className={ iconClassName }
247
+ aria-label={ __( 'Preview icon', 'elementor' ) }
248
+ sx={ { fontSize: ICON_PREVIEW_FONT_SIZE } }
249
+ />
250
+ );
251
+ }
252
+
253
+ return (
254
+ <CardMedia
255
+ component="img"
256
+ image={ src }
257
+ alt={ __( 'Preview SVG', 'elementor' ) }
258
+ sx={ { maxHeight: '140px', width: `${ ICON_PREVIEW_FONT_SIZE }px` } }
259
+ />
260
+ );
261
+ }
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { videoSrcPropTypeUtil } from '@elementor/editor-props';
2
+ import { urlPropTypeUtil, videoSrcPropTypeUtil } from '@elementor/editor-props';
3
3
  import { UploadIcon } from '@elementor/icons';
4
4
  import { Button, Card, CardMedia, CardOverlay, CircularProgress, Stack } from '@elementor/ui';
5
5
  import { useWpMediaAttachment, useWpMediaFrame } from '@elementor/wp-media';
@@ -13,16 +13,20 @@ import { TILES_GRADIENT_FORMULA } from './svg-media-control';
13
13
  const PLACEHOLDER_IMAGE = window.elementorCommon?.config?.urls?.assets + '/shapes/play-triangle.svg';
14
14
 
15
15
  export const VideoMediaControl = createControl( () => {
16
- const { value, setValue } = useBoundProp( videoSrcPropTypeUtil );
16
+ const { value, setValue, propType } = useBoundProp( videoSrcPropTypeUtil );
17
17
  const { id, url } = value ?? {};
18
18
 
19
19
  const { data: attachment, isFetching } = useWpMediaAttachment( id?.value || null );
20
20
  const videoUrl = attachment?.url ?? url?.value ?? null;
21
21
 
22
+ const defaultUrl = videoSrcPropTypeUtil.extract( propType.default ?? null )?.url?.value;
23
+ const currentUrlForModal = url?.value && url.value !== defaultUrl ? url.value : undefined;
24
+
22
25
  const { open } = useWpMediaFrame( {
23
26
  mediaTypes: [ 'video' ],
24
27
  multiple: false,
25
28
  selected: id?.value || null,
29
+ allowUrlImport: true,
26
30
  onSelect: ( selectedAttachment ) => {
27
31
  setValue( {
28
32
  id: {
@@ -32,6 +36,12 @@ export const VideoMediaControl = createControl( () => {
32
36
  url: null,
33
37
  } );
34
38
  },
39
+ onSelectUrl: ( selectedUrl ) => {
40
+ setValue( {
41
+ id: null,
42
+ url: urlPropTypeUtil.create( selectedUrl ),
43
+ } );
44
+ },
35
45
  } );
36
46
 
37
47
  return (
@@ -71,6 +81,14 @@ export const VideoMediaControl = createControl( () => {
71
81
  >
72
82
  { __( 'Upload', 'elementor' ) }
73
83
  </Button>
84
+ <Button
85
+ size="tiny"
86
+ variant="text"
87
+ color="inherit"
88
+ onClick={ () => open( { mode: 'url', currentUrl: currentUrlForModal } ) }
89
+ >
90
+ { __( 'Insert from URL', 'elementor' ) }
91
+ </Button>
74
92
  </Stack>
75
93
  </CardOverlay>
76
94
  </Card>
@@ -86,6 +104,7 @@ const VideoPreview = ( { isFetching = false, videoUrl }: { isFetching?: boolean;
86
104
  if ( videoUrl ) {
87
105
  return (
88
106
  <video
107
+ aria-label={ __( 'Video preview', 'elementor' ) }
89
108
  src={ videoUrl }
90
109
  muted
91
110
  preload="metadata"
@@ -1,3 +1,5 @@
1
+ import { escapedHtmlPropTypeUtil, htmlV3PropTypeUtil, stringPropTypeUtil } from '@elementor/editor-props';
2
+
1
3
  export function isEmpty( value: string | null = '' ) {
2
4
  if ( ! value ) {
3
5
  return true;
@@ -20,3 +22,13 @@ export function htmlToPlainText( html: string | null ): string {
20
22
 
21
23
  return doc.body.textContent ?? '';
22
24
  }
25
+
26
+ export function extractInlineHtmlContent( propValue: unknown ): string {
27
+ if ( escapedHtmlPropTypeUtil.isValid( propValue ) ) {
28
+ return escapedHtmlPropTypeUtil.extract( propValue ) ?? '';
29
+ }
30
+
31
+ const htmlV3 = htmlV3PropTypeUtil.extract( propValue );
32
+
33
+ return stringPropTypeUtil.extract( htmlV3?.content ?? null ) ?? '';
34
+ }