@elementor/editor-controls 3.35.0-367 → 3.35.0-369

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": "3.35.0-367",
4
+ "version": "3.35.0-369",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -40,22 +40,22 @@
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@elementor/editor-current-user": "3.35.0-367",
44
- "@elementor/editor-elements": "3.35.0-367",
45
- "@elementor/editor-props": "3.35.0-367",
46
- "@elementor/editor-responsive": "3.35.0-367",
47
- "@elementor/editor-ui": "3.35.0-367",
48
- "@elementor/editor-v1-adapters": "3.35.0-367",
49
- "@elementor/env": "3.35.0-367",
50
- "@elementor/http-client": "3.35.0-367",
43
+ "@elementor/editor-current-user": "3.35.0-369",
44
+ "@elementor/editor-elements": "3.35.0-369",
45
+ "@elementor/editor-props": "3.35.0-369",
46
+ "@elementor/editor-responsive": "3.35.0-369",
47
+ "@elementor/editor-ui": "3.35.0-369",
48
+ "@elementor/editor-v1-adapters": "3.35.0-369",
49
+ "@elementor/env": "3.35.0-369",
50
+ "@elementor/http-client": "3.35.0-369",
51
51
  "@elementor/icons": "^1.62.0",
52
- "@elementor/locations": "3.35.0-367",
53
- "@elementor/mixpanel": "3.35.0-367",
54
- "@elementor/query": "3.35.0-367",
55
- "@elementor/session": "3.35.0-367",
52
+ "@elementor/locations": "3.35.0-369",
53
+ "@elementor/mixpanel": "3.35.0-369",
54
+ "@elementor/query": "3.35.0-369",
55
+ "@elementor/session": "3.35.0-369",
56
56
  "@elementor/ui": "1.36.17",
57
- "@elementor/utils": "3.35.0-367",
58
- "@elementor/wp-media": "3.35.0-367",
57
+ "@elementor/utils": "3.35.0-369",
58
+ "@elementor/wp-media": "3.35.0-369",
59
59
  "@wordpress/i18n": "^5.13.0",
60
60
  "@monaco-editor/react": "^4.7.0",
61
61
  "dayjs": "^1.11.18",
@@ -44,6 +44,24 @@ const useOnUpdate = ( callback: () => void, dependencies: DependencyList ): void
44
44
  }, dependencies );
45
45
  };
46
46
 
47
+ const calcSelectionCenter = (
48
+ view: EditorView,
49
+ container: { left: number; top: number } | undefined
50
+ ): { left: number; top: number } | null => {
51
+ if ( ! container ) {
52
+ return null;
53
+ }
54
+
55
+ const { from, to } = view.state.selection;
56
+ const start = view.coordsAtPos( from );
57
+ const end = view.coordsAtPos( to );
58
+
59
+ const left = ( start.left + end.left ) / 2 - container.left;
60
+ const top = Math.min( start.top, end.top ) - container.top;
61
+
62
+ return { left, top };
63
+ };
64
+
47
65
  export const InlineEditor = React.forwardRef(
48
66
  (
49
67
  {
@@ -63,9 +81,19 @@ export const InlineEditor = React.forwardRef(
63
81
  const popupState = usePopupState( { variant: 'popover', disableAutoFocus: true } );
64
82
  const [ hasSelectedContent, setHasSelectedContent ] = React.useState( false );
65
83
  const documentContentSettings = !! expectedTag ? 'block+' : 'inline*';
84
+ const [ selectionRect, setSelectionRect ] = React.useState< { left: number; top: number } | null >( null );
66
85
 
67
86
  const onSelectionEnd = ( view: EditorView ) => {
68
- setHasSelectedContent( () => ! view.state.selection.empty );
87
+ const hasSelection = ! view.state.selection.empty;
88
+ setHasSelectedContent( hasSelection );
89
+
90
+ if ( hasSelection ) {
91
+ const container = containerRef.current?.getBoundingClientRect();
92
+ setSelectionRect( calcSelectionCenter( view, container ) );
93
+ } else {
94
+ setSelectionRect( null );
95
+ }
96
+
69
97
  queueMicrotask( () => view.focus() );
70
98
  };
71
99
 
@@ -157,13 +185,20 @@ export const InlineEditor = React.forwardRef(
157
185
  }, [ editor, value ] );
158
186
 
159
187
  const computePopupPosition = () => {
160
- const positionFallback = { left: 0, top: 0 };
161
- const { left, top } = containerRef.current?.getBoundingClientRect() ?? positionFallback;
162
- const initial = getInitialPopoverPosition?.() ?? positionFallback;
188
+ if ( ! selectionRect ) {
189
+ return { left: 0, top: 0 };
190
+ }
191
+
192
+ const container = containerRef.current?.getBoundingClientRect();
193
+ if ( ! container ) {
194
+ return { left: 0, top: 0 };
195
+ }
196
+
197
+ const initial = getInitialPopoverPosition?.() ?? { left: 0, top: 0 };
163
198
 
164
199
  return {
165
- left: left + initial.left,
166
- top: top + initial.top,
200
+ left: container.left + selectionRect.left + initial.left,
201
+ top: container.top + selectionRect.top + initial.top,
167
202
  };
168
203
  };
169
204
 
@@ -209,11 +244,11 @@ export const InlineEditor = React.forwardRef(
209
244
  },
210
245
  } }
211
246
  { ...bindPopover( popupState ) }
212
- open={ hasSelectedContent }
247
+ open={ hasSelectedContent && selectionRect !== null }
213
248
  anchorReference="anchorPosition"
214
249
  anchorPosition={ computePopupPosition() }
215
- anchorOrigin={ { vertical: 'top', horizontal: 'left' } }
216
- transformOrigin={ { vertical: 'bottom', horizontal: 'left' } }
250
+ anchorOrigin={ { vertical: 'top', horizontal: 'center' } }
251
+ transformOrigin={ { vertical: 'bottom', horizontal: 'center' } }
217
252
  >
218
253
  <InlineEditorToolbar editor={ editor } />
219
254
  </Popover>