@elementor/editor-controls 4.3.0-998 → 4.3.0-beta1

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-998",
4
+ "version": "4.3.0-beta1",
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-998",
44
- "@elementor/editor-elements": "4.3.0-998",
45
- "@elementor/editor-props": "4.3.0-998",
46
- "@elementor/editor-responsive": "4.3.0-998",
47
- "@elementor/editor-ui": "4.3.0-998",
48
- "@elementor/editor-v1-adapters": "4.3.0-998",
49
- "@elementor/env": "4.3.0-998",
50
- "@elementor/events": "4.3.0-998",
51
- "@elementor/http-client": "4.3.0-998",
43
+ "@elementor/editor-current-user": "4.3.0-beta1",
44
+ "@elementor/editor-elements": "4.3.0-beta1",
45
+ "@elementor/editor-props": "4.3.0-beta1",
46
+ "@elementor/editor-responsive": "4.3.0-beta1",
47
+ "@elementor/editor-ui": "4.3.0-beta1",
48
+ "@elementor/editor-v1-adapters": "4.3.0-beta1",
49
+ "@elementor/env": "4.3.0-beta1",
50
+ "@elementor/events": "4.3.0-beta1",
51
+ "@elementor/http-client": "4.3.0-beta1",
52
52
  "@elementor/icons": "~1.75.1",
53
- "@elementor/locations": "4.3.0-998",
54
- "@elementor/query": "4.3.0-998",
55
- "@elementor/schema": "4.3.0-998",
56
- "@elementor/session": "4.3.0-998",
53
+ "@elementor/locations": "4.3.0-beta1",
54
+ "@elementor/query": "4.3.0-beta1",
55
+ "@elementor/schema": "4.3.0-beta1",
56
+ "@elementor/session": "4.3.0-beta1",
57
57
  "@elementor/ui": "1.37.5",
58
- "@elementor/utils": "4.3.0-998",
59
- "@elementor/wp-media": "4.3.0-998",
58
+ "@elementor/utils": "4.3.0-beta1",
59
+ "@elementor/wp-media": "4.3.0-beta1",
60
60
  "@monaco-editor/react": "^4.7.0",
61
61
  "@tiptap/extension-bold": "^3.11.1",
62
62
  "@tiptap/extension-document": "^3.11.1",
@@ -32,13 +32,19 @@ export type InlineEditorToolbarProps = {
32
32
  editor: Editor;
33
33
  elementId?: ElementID;
34
34
  sx?: SxProps< Theme >;
35
+ inControlPanel?: boolean;
35
36
  };
36
37
 
37
38
  type ToolbarButtonKeys = keyof typeof toolbarButtons;
38
39
 
39
40
  type FormatAction = Omit< ToolbarButtonKeys, 'clear' >;
40
41
 
41
- export const InlineEditorToolbar = ( { editor, elementId, sx = {} }: InlineEditorToolbarProps ) => {
42
+ export const InlineEditorToolbar = ( {
43
+ editor,
44
+ elementId,
45
+ sx = {},
46
+ inControlPanel = false,
47
+ }: InlineEditorToolbarProps ) => {
42
48
  const [ urlValue, setUrlValue ] = useState( '' );
43
49
  const [ openInNewTab, setOpenInNewTab ] = useState( false );
44
50
  const toolbarRef = useRef< HTMLDivElement >( null );
@@ -101,8 +107,10 @@ export const InlineEditorToolbar = ( { editor, elementId, sx = {} }: InlineEdito
101
107
  };
102
108
 
103
109
  useEffect( () => {
104
- editor?.commands?.focus();
105
- }, [ editor ] );
110
+ if ( ! inControlPanel ) {
111
+ editor?.commands?.focus();
112
+ }
113
+ }, [ editor, inControlPanel ] );
106
114
 
107
115
  return (
108
116
  <Box
@@ -118,6 +126,23 @@ export const InlineEditorToolbar = ( { editor, elementId, sx = {} }: InlineEdito
118
126
  visibility: linkPopupState.isOpen ? 'hidden' : 'visible',
119
127
  pointerEvents: linkPopupState.isOpen ? 'none' : 'all',
120
128
  ...sx,
129
+ ...( inControlPanel && {
130
+ width: '100%',
131
+ justifyContent: 'center',
132
+ flexDirection: 'row',
133
+ backgroundColor: 'transparent',
134
+ boxShadow: 'none',
135
+ borderWidth: '0',
136
+ borderBottom: '1px solid',
137
+ borderBottomColor: ( theme: Theme ) => theme.palette.text.secondary,
138
+ borderRadius: '0',
139
+ position: 'absolute',
140
+ top: '0',
141
+ left: '0',
142
+ '&, & .MuiIconButton-root, & .MuiToggleButton-root': {
143
+ color: ( theme: Theme ) => theme.palette.text.primary,
144
+ },
145
+ } ),
121
146
  } }
122
147
  >
123
148
  <Tooltip title={ clearButton.label } placement="top" sx={ { borderRadius: '8px' } }>
@@ -144,6 +169,14 @@ export const InlineEditorToolbar = ( { editor, elementId, sx = {} }: InlineEdito
144
169
  marginLeft: 0,
145
170
  },
146
171
  },
172
+ ...( inControlPanel && {
173
+ justifyContent: 'space-between',
174
+ width: '100%',
175
+ '& svg': {
176
+ width: '0.7rem',
177
+ height: '0.7rem',
178
+ },
179
+ } ),
147
180
  } }
148
181
  >
149
182
  { formatButtonsList.map( ( button ) => (
@@ -33,6 +33,7 @@ type InlineEditorProps = {
33
33
  autofocus?: boolean;
34
34
  expectedTag?: string | null;
35
35
  onEditorCreate?: Dispatch< SetStateAction< Editor | null > >;
36
+ onEditorDestroy?: () => void;
36
37
  wrapperClassName?: string;
37
38
  onSelectionEnd?: ( view: EditorView ) => void;
38
39
  mountElement?: HTMLElement | null;
@@ -50,6 +51,7 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
50
51
  onBlur = undefined,
51
52
  expectedTag = null,
52
53
  onEditorCreate,
54
+ onEditorDestroy,
53
55
  wrapperClassName,
54
56
  onSelectionEnd,
55
57
  mountElement = null,
@@ -146,6 +148,7 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
146
148
  },
147
149
  },
148
150
  onCreate: onEditorCreate ? ( { editor: mountedEditor } ) => onEditorCreate( mountedEditor ) : undefined,
151
+ onDestroy: onEditorDestroy ? () => onEditorDestroy() : undefined,
149
152
  onBlur: mountElement ? undefined : () => onBlurRef.current?.(),
150
153
  onSelectionUpdate: onSelectionEnd
151
154
  ? ( { editor: updatedEditor } ) => onSelectionEnd( updatedEditor.view )
@@ -51,8 +51,8 @@ export const AspectRatioControl = createControl( ( { label }: { label: string }
51
51
 
52
52
  if ( isCustomValue ) {
53
53
  const [ width, height ] = aspectRatioValue.split( '/' );
54
- setCustomWidth( width || '' );
55
- setCustomHeight( height || '' );
54
+ setCustomWidth( width.trim() || '' );
55
+ setCustomHeight( height.trim() || '' );
56
56
  setSelectedValue( CUSTOM_RATIO );
57
57
  setIsCustom( true );
58
58
  } else {
@@ -64,9 +64,9 @@ export const BackgroundGradientColorControl = createControl( () => {
64
64
  return {
65
65
  type: type.value,
66
66
  angle: angle?.value || 0,
67
- stops: stops.value.map( ( { value: { color, offset } }: ColorStop ) => ( {
67
+ stops: stops.value.map( ( { value: { color, offset } }: ColorStop, index: number, arr: ColorStop[] ) => ( {
68
68
  color: color.value,
69
- offset: offset.value,
69
+ offset: offset?.value ?? ( arr.length > 1 ? ( index / ( arr.length - 1 ) ) * 100 : 0 ),
70
70
  } ) ),
71
71
  positions: positions?.value.split( ' ' ),
72
72
  };
@@ -305,7 +305,9 @@ const getGradientValue = ( value: BackgroundOverlayItemPropValue ) => {
305
305
  const gradient = value.value;
306
306
 
307
307
  const stops = gradient.stops.value
308
- ?.map( ( { value: { color, offset } }: ColorStop ) => `${ color.value } ${ offset.value ?? 0 }%` )
308
+ ?.map( ( { value: { color, offset } }: ColorStop ) =>
309
+ offset ? `${ color.value } ${ offset.value ?? 0 }%` : `${ color.value }`
310
+ )
309
311
  ?.join( ',' );
310
312
 
311
313
  if ( gradient.type.value === 'linear' ) {
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { type KeyboardEvent, type SyntheticEvent, useState } from 'react';
2
+ import { type KeyboardEvent, type SyntheticEvent, useMemo, useState } from 'react';
3
3
  import { stringArrayPropTypeUtil, stringPropTypeUtil } from '@elementor/editor-props';
4
4
  import { Autocomplete, Grid, TextField } from '@elementor/ui';
5
5
 
@@ -8,13 +8,28 @@ import { ChipsList } from '../../components/chips-list';
8
8
  import { ControlFormLabel } from '../../components/control-form-label';
9
9
  import ControlActions from '../../control-actions/control-actions';
10
10
  import { createControl } from '../../create-control';
11
- import { CHIP_TRIGGER_KEYS, isValidEmail } from './utils';
11
+ import { type Suggestion } from '../../hooks/use-form-field-suggestions';
12
+ import { createMentionPattern } from '../mention-text-area-control';
13
+ import { CHIP_TRIGGER_KEYS, isFormFieldShortcode, isValidEmail } from './utils';
14
+
15
+ const isValidRecipient = ( address: string ) => isValidEmail( address ) || isFormFieldShortcode( address );
16
+
17
+ function resolveMention( raw: string, suggestions: Suggestion[] ): string {
18
+ if ( ! raw.startsWith( '@' ) ) {
19
+ return raw;
20
+ }
21
+
22
+ const match = suggestions.find( ( suggestion ) => createMentionPattern( suggestion.value, 'start' ).test( raw ) );
23
+
24
+ return match ? `[${ match.value }]` : raw;
25
+ }
12
26
 
13
27
  type EmailChipsControlProps = {
14
28
  placeholder?: string;
29
+ suggestions?: Suggestion[];
15
30
  };
16
31
 
17
- export const EmailChipsControl = createControl( ( { placeholder }: EmailChipsControlProps ) => {
32
+ export const EmailChipsControl = createControl( ( { placeholder, suggestions = [] }: EmailChipsControlProps ) => {
18
33
  const { value, setValue, disabled } = useBoundProp( stringArrayPropTypeUtil );
19
34
  const [ inputValue, setInputValue ] = useState( '' );
20
35
 
@@ -24,10 +39,15 @@ export const EmailChipsControl = createControl( ( { placeholder }: EmailChipsCon
24
39
  .map( ( item ) => stringPropTypeUtil.extract( item ) )
25
40
  .filter( ( val ): val is string => val !== null );
26
41
 
42
+ const suggestionOptions = useMemo(
43
+ () => suggestions.map( ( suggestion ) => `[${ suggestion.value }]` ),
44
+ [ suggestions ]
45
+ );
46
+
27
47
  const tryAddChip = ( raw: string ) => {
28
- const address = raw.trim();
48
+ const address = resolveMention( raw.trim(), suggestions );
29
49
 
30
- if ( ! address || selectedValues.includes( address ) || ! isValidEmail( address ) ) {
50
+ if ( ! address || selectedValues.includes( address ) || ! isValidRecipient( address ) ) {
31
51
  return;
32
52
  }
33
53
 
@@ -39,9 +59,9 @@ export const EmailChipsControl = createControl( ( { placeholder }: EmailChipsCon
39
59
  const updated = [];
40
60
 
41
61
  for ( const entry of newValue ) {
42
- const address = entry.trim();
62
+ const address = resolveMention( entry.trim(), suggestions );
43
63
 
44
- if ( ! address || ! isValidEmail( address ) ) {
64
+ if ( ! address || ! isValidRecipient( address ) ) {
45
65
  continue;
46
66
  }
47
67
 
@@ -82,7 +102,13 @@ export const EmailChipsControl = createControl( ( { placeholder }: EmailChipsCon
82
102
  } }
83
103
  value={ selectedValues }
84
104
  onChange={ handleChange }
85
- options={ [] }
105
+ options={ suggestionOptions }
106
+ filterOptions={ ( options, state ) => {
107
+ const query = state.inputValue.trim().replace( /^@/, '' ).toLowerCase();
108
+
109
+ return query ? options.filter( ( option ) => option.toLowerCase().includes( query ) ) : options;
110
+ } }
111
+ filterSelectedOptions
86
112
  onBlur={ handleBlur }
87
113
  getOptionLabel={ ( option ) => option }
88
114
  isOptionEqualToValue={ ( option, val ) => option === val }
@@ -100,15 +126,16 @@ export const EmailChipsControl = createControl( ( { placeholder }: EmailChipsCon
100
126
  type EmailChipsFieldProps = {
101
127
  fieldLabel: string;
102
128
  placeholder?: string;
129
+ suggestions?: Suggestion[];
103
130
  };
104
131
 
105
- export const EmailChipsField = ( { fieldLabel, placeholder }: EmailChipsFieldProps ) => (
132
+ export const EmailChipsField = ( { fieldLabel, placeholder, suggestions }: EmailChipsFieldProps ) => (
106
133
  <Grid container direction="column" gap={ 0.5 }>
107
134
  <Grid item>
108
135
  <ControlFormLabel>{ fieldLabel }</ControlFormLabel>
109
136
  </Grid>
110
137
  <Grid item>
111
- <EmailChipsControl placeholder={ placeholder } />
138
+ <EmailChipsControl placeholder={ placeholder } suggestions={ suggestions } />
112
139
  </Grid>
113
140
  </Grid>
114
141
  );
@@ -13,11 +13,26 @@ import { EmailChipsField } from './email-chips-field';
13
13
  import { EmailField } from './email-field';
14
14
  import { shouldShowMentionsInfo } from './utils';
15
15
 
16
- export const SendToField = ( { placeholder }: { placeholder?: string } ) => (
17
- <PropKeyProvider bind="to">
18
- <EmailChipsField fieldLabel={ __( 'Send to', 'elementor' ) } placeholder={ placeholder } />
19
- </PropKeyProvider>
20
- );
16
+ export const SendToField = ( { placeholder }: { placeholder?: string } ) => {
17
+ const suggestions = useFormFieldSuggestions( { inputType: 'email' } );
18
+
19
+ return (
20
+ <PropKeyProvider bind="to">
21
+ <Stack gap={ 0.5 }>
22
+ <EmailChipsField
23
+ fieldLabel={ __( 'Send to', 'elementor' ) }
24
+ placeholder={ placeholder }
25
+ suggestions={ suggestions }
26
+ />
27
+ { shouldShowMentionsInfo() && (
28
+ <InfoAlert>
29
+ { __( 'Type @ or an email field name to insert its submitted value.', 'elementor' ) }
30
+ </InfoAlert>
31
+ ) }
32
+ </Stack>
33
+ </PropKeyProvider>
34
+ );
35
+ };
21
36
 
22
37
  export const SubjectField = () => (
23
38
  <EmailField
@@ -92,17 +107,25 @@ export const ReplyToField = () => {
92
107
  );
93
108
  };
94
109
 
95
- export const CcField = () => (
96
- <PropKeyProvider bind="cc">
97
- <EmailChipsField fieldLabel={ __( 'Cc', 'elementor' ) } />
98
- </PropKeyProvider>
99
- );
110
+ export const CcField = () => {
111
+ const suggestions = useFormFieldSuggestions( { inputType: 'email' } );
100
112
 
101
- export const BccField = () => (
102
- <PropKeyProvider bind="bcc">
103
- <EmailChipsField fieldLabel={ __( 'Bcc', 'elementor' ) } />
104
- </PropKeyProvider>
105
- );
113
+ return (
114
+ <PropKeyProvider bind="cc">
115
+ <EmailChipsField fieldLabel={ __( 'Cc', 'elementor' ) } suggestions={ suggestions } />
116
+ </PropKeyProvider>
117
+ );
118
+ };
119
+
120
+ export const BccField = () => {
121
+ const suggestions = useFormFieldSuggestions( { inputType: 'email' } );
122
+
123
+ return (
124
+ <PropKeyProvider bind="bcc">
125
+ <EmailChipsField fieldLabel={ __( 'Bcc', 'elementor' ) } suggestions={ suggestions } />
126
+ </PropKeyProvider>
127
+ );
128
+ };
106
129
 
107
130
  export const MetaDataField = () => (
108
131
  <PropKeyProvider bind="meta-data">
@@ -9,6 +9,12 @@ export function isValidEmail( email: string ): boolean {
9
9
  return z.string().email().safeParse( email ).success;
10
10
  }
11
11
 
12
+ const FORM_FIELD_SHORTCODE_PATTERN = /^\[[^[\]]+]$/;
13
+
14
+ export function isFormFieldShortcode( value: string ): boolean {
15
+ return FORM_FIELD_SHORTCODE_PATTERN.test( value );
16
+ }
17
+
12
18
  export const shouldShowMentionsInfo = (): boolean => {
13
19
  if ( ! hasProInstalled() ) {
14
20
  return true;
@@ -1,79 +1,73 @@
1
1
  import * as React from 'react';
2
- import { type ComponentProps, useCallback, useEffect, useMemo } from 'react';
3
- import { htmlV3PropTypeUtil, parseHtmlChildren, stringPropTypeUtil } from '@elementor/editor-props';
2
+ import { type ComponentProps, useCallback, useState } from 'react';
3
+ import { escapedHtmlPropTypeUtil } from '@elementor/editor-props';
4
4
  import { Box, type SxProps, type Theme } from '@elementor/ui';
5
- import { debounce } from '@elementor/utils';
5
+ import { type Editor } from '@tiptap/react';
6
6
 
7
- import { useBoundProp } from '../bound-prop-context';
7
+ import { useBoundProp, usePropKeyContext } from '../bound-prop-context';
8
8
  import { InlineEditor } from '../components/inline-editor';
9
+ import { InlineEditorToolbar } from '../components/inline-editor-toolbar';
9
10
  import ControlActions from '../control-actions/control-actions';
10
11
  import { createControl } from '../create-control';
12
+ import { extractInlineHtmlContent } from '../utils/inline-editing';
13
+ import { type ControlProps } from '../utils/types';
11
14
 
12
- const CHILDREN_PARSE_DEBOUNCE_MS = 300;
15
+ type Props = ControlProps< {
16
+ sx?: SxProps< Theme >;
17
+ attributes?: Record< string, string >;
18
+ props?: ComponentProps< 'div' >;
19
+ } >;
13
20
 
14
- export const InlineEditingControl = createControl(
15
- ( {
16
- sx,
17
- attributes,
18
- props,
19
- }: {
20
- sx?: SxProps< Theme >;
21
- attributes?: Record< string, string >;
22
- props?: ComponentProps< 'div' >;
23
- } ) => {
24
- const { value, setValue, placeholder } = useBoundProp( htmlV3PropTypeUtil );
25
- const content = stringPropTypeUtil.extract( value?.content ?? null ) ?? '';
21
+ export const InlineEditingControl = createControl( ( { sx, attributes, props, context: { elementId } }: Props ) => {
22
+ const { setValue, placeholder, value } = useBoundProp( escapedHtmlPropTypeUtil );
23
+ const { value: rawValue } = usePropKeyContext();
24
+ const content = value ?? extractInlineHtmlContent( rawValue );
25
+ const [ editor, setEditor ] = useState< Editor | null >( null );
26
26
 
27
- const debouncedParse = useMemo(
28
- () =>
29
- debounce( ( html: string ) => {
30
- const parsed = parseHtmlChildren( html );
27
+ const handleChange = useCallback(
28
+ ( newValue: unknown ) => {
29
+ const html = ( newValue ?? '' ) as string;
31
30
 
32
- setValue( {
33
- content: parsed.content ? stringPropTypeUtil.create( parsed.content ) : null,
34
- children: parsed.children,
35
- } );
36
- }, CHILDREN_PARSE_DEBOUNCE_MS ),
37
- [ setValue ]
38
- );
31
+ setValue( html );
32
+ },
33
+ [ setValue ]
34
+ );
39
35
 
40
- const handleChange = useCallback(
41
- ( newValue: unknown ) => {
42
- const html = ( newValue ?? '' ) as string;
43
-
44
- setValue( {
45
- content: html ? stringPropTypeUtil.create( html ) : null,
46
- children: value?.children ?? [],
47
- } );
48
-
49
- debouncedParse( html );
50
- },
51
- [ setValue, value?.children, debouncedParse ]
52
- );
53
-
54
- useEffect( () => () => debouncedParse.cancel(), [ debouncedParse ] );
55
-
56
- return (
57
- <ControlActions>
36
+ return (
37
+ <ControlActions>
38
+ <Box sx={ { position: 'relative' } }>
39
+ { editor && editor.isEditable && (
40
+ <InlineEditorToolbar
41
+ editor={ editor }
42
+ elementId={ elementId }
43
+ sx={ ( theme: Theme ) => ( {
44
+ boxShadow: 'none',
45
+ border: '1px solid',
46
+ borderColor: theme.palette.text.secondary,
47
+ mb: 0.5,
48
+ } ) }
49
+ inControlPanel={ true }
50
+ />
51
+ ) }
58
52
  <Box
59
- sx={ {
53
+ sx={ ( theme: Theme ) => ( {
60
54
  p: 0.8,
61
55
  border: '1px solid',
62
- borderColor: 'grey.200',
56
+ borderColor: theme.palette.text.secondary,
63
57
  borderRadius: '8px',
64
58
  transition: 'border-color .2s ease, box-shadow .2s ease',
65
59
  '&:hover': {
66
- borderColor: 'black',
60
+ borderColor: theme.palette.text.primary,
67
61
  },
68
62
  '&:focus-within': {
69
- borderColor: 'black',
70
- boxShadow: '0 0 0 1px black',
63
+ borderColor: theme.palette.text.primary,
64
+ boxShadow: `0 0 0 1px ${ theme.palette.text.primary }`,
71
65
  },
72
66
  '& .ProseMirror:focus': {
73
67
  outline: 'none',
74
68
  },
75
69
  '& .ProseMirror': {
76
- minHeight: '70px',
70
+ minHeight: '100px',
77
71
  fontSize: '12px',
78
72
  '& a': {
79
73
  color: 'inherit',
@@ -94,17 +88,22 @@ export const InlineEditingControl = createControl(
94
88
  all: 'unset',
95
89
  },
96
90
  ...sx,
97
- } }
91
+ } ) }
98
92
  { ...attributes }
99
93
  { ...props }
100
94
  >
101
95
  <InlineEditor
102
96
  value={ content }
103
97
  setValue={ handleChange }
104
- placeholder={ placeholder?.content?.value ?? null }
98
+ placeholder={ placeholder ?? null }
99
+ onEditorCreate={ setEditor }
100
+ onEditorDestroy={ () => setEditor( null ) }
101
+ sx={ {
102
+ paddingBlockStart: 5,
103
+ } }
105
104
  />
106
105
  </Box>
107
- </ControlActions>
108
- );
109
- }
110
- );
106
+ </Box>
107
+ </ControlActions>
108
+ );
109
+ } );
@@ -81,9 +81,9 @@ const MentionWrapper = styled( 'div' )( ( { theme } ) => ( {
81
81
  },
82
82
  } ) );
83
83
 
84
- type TriggerPosition = 'start' | 'auto';
84
+ export type TriggerPosition = 'start' | 'auto';
85
85
 
86
- function createMentionPattern( value: string, triggerPosition: TriggerPosition ): RegExp {
86
+ export function createMentionPattern( value: string, triggerPosition: TriggerPosition ): RegExp {
87
87
  const escaped = value.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' );
88
88
  const prefix = 'start' === triggerPosition ? '^' : '';
89
89
 
@@ -0,0 +1,82 @@
1
+ import { stringPropTypeUtil } from '@elementor/editor-props';
2
+
3
+ export const SVG_ICON_LIBRARY = 'svg';
4
+
5
+ export type IconLibraryMediaValue = {
6
+ id?: number;
7
+ url?: string;
8
+ };
9
+
10
+ export type IconLibrarySelection = {
11
+ value: string | IconLibraryMediaValue;
12
+ library: string;
13
+ };
14
+
15
+ type IconManagerControlView = {
16
+ model: {
17
+ get: ( key: string ) => unknown;
18
+ };
19
+ getControlValue: () => IconLibrarySelection;
20
+ setValue: ( icon: IconLibrarySelection ) => void;
21
+ applySavedValue: () => void;
22
+ };
23
+
24
+ type IconManager = {
25
+ loadIconLibraries: () => void;
26
+ show: ( options: { view: IconManagerControlView } ) => void;
27
+ };
28
+
29
+ type ElementorWithIconManager = {
30
+ iconManager?: IconManager;
31
+ };
32
+
33
+ type OpenIconLibraryOptions = {
34
+ selected?: IconLibrarySelection;
35
+ onSelect?: ( icon: IconLibrarySelection ) => void;
36
+ };
37
+
38
+ export function isSvgLibrarySelection(
39
+ icon: IconLibrarySelection
40
+ ): icon is IconLibrarySelection & { value: IconLibraryMediaValue } {
41
+ return icon.library === SVG_ICON_LIBRARY && typeof icon.value === 'object' && icon.value !== null;
42
+ }
43
+
44
+ export function openIconLibrary( { selected, onSelect }: OpenIconLibraryOptions = {} ) {
45
+ const iconManager = ( window.elementor as ElementorWithIconManager | undefined )?.iconManager;
46
+
47
+ if ( ! iconManager ) {
48
+ return;
49
+ }
50
+
51
+ iconManager.loadIconLibraries();
52
+ iconManager.show( {
53
+ view: createIconManagerControlView( selected, onSelect ),
54
+ } );
55
+ }
56
+
57
+ export function enqueueIconFonts( library: string ) {
58
+ window.elementor?.helpers?.enqueueIconFonts?.( library );
59
+ }
60
+
61
+ function createIconManagerControlView(
62
+ selected?: IconLibrarySelection,
63
+ onSelect?: ( icon: IconLibrarySelection ) => void
64
+ ): IconManagerControlView {
65
+ return {
66
+ model: {
67
+ get: () => false,
68
+ },
69
+ getControlValue: () => selected ?? { value: '', library: '' },
70
+ setValue: ( icon ) => {
71
+ onSelect?.( icon );
72
+ },
73
+ applySavedValue: () => undefined,
74
+ };
75
+ }
76
+
77
+ export function createIconPropValue( icon: Extract< IconLibrarySelection[ 'value' ], string >, library: string ) {
78
+ return {
79
+ value: stringPropTypeUtil.create( icon ),
80
+ library: stringPropTypeUtil.create( library ),
81
+ };
82
+ }