@@ -13,14 +13,11 @@ import { visualBuilderStyles } from "../visualBuilder.style";
1313import { VB_EmptyBlockParentClass } from "../.." ;
1414import Config from "../../configManager/configManager" ;
1515import { isCollabThread } from "../generators/generateThread" ;
16- import { getEntryPermissionsCached } from "../utils/getEntryPermissionsCached" ;
17- import { EntryPermissions } from "../utils/getEntryPermissions" ;
18- import visualBuilderPostMessage from "../utils/visualBuilderPostMessage" ;
19- import { VisualBuilderPostMessageEvents } from "../utils/types/postMessage.types" ;
2016import { HandleBuilderInteractionParams } from "./mouseClick" ;
21- import { appendFieldPathDropdown , removeFieldToolbar } from "../generators/generateToolbar" ;
17+ import { appendFieldPathDropdown } from "../generators/generateToolbar" ;
2218import { VisualBuilderCslpEventDetails } from "../types/visualBuilder.types" ;
2319import { CslpData } from "../../cslp/types/cslp.types" ;
20+ import { fetchEntryPermissionsAndStageDetails } from "../utils/fetchEntryPermissionsAndStageDetails" ;
2421
2522const config = Config . get ( ) ;
2623export interface HandleMouseHoverParams
@@ -71,46 +68,43 @@ function handleCursorPosition(
7168 }
7269}
7370
74- function addOutline ( params ?: AddOutlineParams ) : void {
75- if ( ! params ) {
71+ async function addOutline ( params ?: AddOutlineParams ) : Promise < void > {
72+ if ( ! params ) {
7673 return ;
7774 }
78- const { editableElement, eventDetails, content_type_uid, fieldPath, fieldMetadata, fieldDisabled } = params ;
75+ const {
76+ editableElement,
77+ eventDetails,
78+ content_type_uid,
79+ fieldPath,
80+ fieldMetadata,
81+ fieldDisabled,
82+ } = params ;
7983 if ( ! editableElement ) return ;
8084 addHoverOutline ( editableElement as HTMLElement , fieldDisabled ) ;
81- FieldSchemaMap . getFieldSchema ( content_type_uid , fieldPath ) . then (
82- ( fieldSchema ) => {
83- let entryAcl : EntryPermissions | undefined ;
84- if ( ! fieldSchema ) return ;
85- getEntryPermissionsCached ( {
86- entryUid : fieldMetadata . entry_uid ,
87- contentTypeUid : fieldMetadata . content_type_uid ,
88- locale : fieldMetadata . locale ,
89- } )
90- . then ( ( data ) => {
91- entryAcl = data ;
92- } )
93- . catch ( ( error ) => {
94- console . error (
95- "[Visual Builder] Error retrieving entry permissions:" ,
96- error
97- ) ;
98- } )
99- . finally ( ( ) => {
100- const { isDisabled : fieldDisabled } =
101- isFieldDisabled (
102- fieldSchema ,
103- eventDetails ,
104- entryAcl
105- ) ;
106- addHoverOutline ( editableElement , fieldDisabled ) ;
107- } ) ;
108- }
85+ const fieldSchema = await FieldSchemaMap . getFieldSchema (
86+ content_type_uid ,
87+ fieldPath
10988 ) ;
89+ if ( ! fieldSchema ) return ;
90+ const { acl : entryAcl , workflowStage : entryWorkflowStageDetails } =
91+ await fetchEntryPermissionsAndStageDetails ( {
92+ entryUid : fieldMetadata . entry_uid ,
93+ contentTypeUid : fieldMetadata . content_type_uid ,
94+ locale : fieldMetadata . locale ,
95+ variantUid : fieldMetadata . variant ,
96+ } ) ;
97+ const { isDisabled } = isFieldDisabled (
98+ fieldSchema ,
99+ eventDetails ,
100+ entryAcl ,
101+ entryWorkflowStageDetails
102+ ) ;
103+ addHoverOutline ( editableElement , fieldDisabled || isDisabled ) ;
110104}
111105
112106const debouncedAddOutline = debounce ( addOutline , 50 , { trailing : true } ) ;
113- const showOutline = ( params ?: AddOutlineParams ) : void => debouncedAddOutline ( params ) ;
107+ const showOutline = ( params ?: AddOutlineParams ) : Promise < void > | undefined => debouncedAddOutline ( params ) ;
114108
115109function hideDefaultCursor ( ) : void {
116110 if (
@@ -219,7 +213,7 @@ const throttledMouseHover = throttle(async (params: HandleMouseHoverParams) => {
219213 hideCustomCursor ( params . customCursor ) ;
220214 return ;
221215 }
222- if (
216+ if (
223217 eventTarget &&
224218 isFieldPathDropdown ( eventTarget )
225219 ) {
@@ -287,10 +281,7 @@ const throttledMouseHover = throttle(async (params: HandleMouseHoverParams) => {
287281 handleCursorPosition ( params . event , params . customCursor ) ;
288282 showCustomCursor ( params . customCursor ) ;
289283 return ;
290- } else if (
291- config ?. collab . enable &&
292- ! config ?. collab . isFeedbackMode
293- ) {
284+ } else if ( config ?. collab . enable && ! config ?. collab . isFeedbackMode ) {
294285 hideCustomCursor ( params . customCursor ) ;
295286 return ;
296287 }
@@ -314,48 +305,11 @@ const throttledMouseHover = throttle(async (params: HandleMouseHoverParams) => {
314305 } ) ;
315306 }
316307
317- /**
318- * We called it seperately inside the code block to ensure that
319- * the code will not wait for the promise to resolve.
320- * If we get a cache miss, we will send a message to the iframe
321- * without blocking the code.
322- */
323- FieldSchemaMap . getFieldSchema ( content_type_uid , fieldPath ) . then (
324- ( fieldSchema ) => {
325- if ( ! fieldSchema ) return ;
326-
327- let entryAcl : EntryPermissions | undefined ;
328- getEntryPermissionsCached ( {
329- entryUid : fieldMetadata . entry_uid ,
330- contentTypeUid : fieldMetadata . content_type_uid ,
331- locale : fieldMetadata . locale ,
332- } )
333- . then ( ( data ) => {
334- entryAcl = data ;
335- } )
336- . catch ( ( error ) => {
337- console . error (
338- "[Visual Builder] Error retrieving entry permissions:" ,
339- error
340- ) ;
341- } )
342- . finally ( ( ) => {
343- if ( ! params . customCursor ) return ;
344- const { isDisabled : fieldDisabled } =
345- isFieldDisabled (
346- fieldSchema ,
347- eventDetails ,
348- entryAcl
349- ) ;
350- const fieldType = getFieldType ( fieldSchema ) ;
351- generateCustomCursor ( {
352- fieldType,
353- customCursor : params . customCursor ,
354- fieldDisabled,
355- } ) ;
356- } ) ;
357- }
358- ) ;
308+ // we can generate the cursor asynchronously
309+ generateCursor ( {
310+ eventDetails,
311+ customCursor : params . customCursor ,
312+ } ) ;
359313
360314 handleCursorPosition ( params . event , params . customCursor ) ;
361315 showCustomCursor ( params . customCursor ) ;
@@ -398,6 +352,45 @@ const throttledMouseHover = throttle(async (params: HandleMouseHoverParams) => {
398352 editableElement ;
399353} , 10 ) ;
400354
401- const handleMouseHover = async ( params : HandleMouseHoverParams ) : Promise < void > => await throttledMouseHover ( params ) ;
355+ async function generateCursor ( {
356+ eventDetails,
357+ customCursor,
358+ } : {
359+ eventDetails : VisualBuilderCslpEventDetails ;
360+ customCursor : HTMLDivElement | null ;
361+ } ) {
362+ if ( ! customCursor ) return ;
363+ const { fieldMetadata } = eventDetails ;
364+ const fieldSchema = await FieldSchemaMap . getFieldSchema (
365+ fieldMetadata . content_type_uid ,
366+ fieldMetadata . fieldPath
367+ ) ;
368+ if ( ! fieldSchema ) {
369+ return ;
370+ }
371+ const { acl : entryAcl , workflowStage : entryWorkflowStageDetails } =
372+ await fetchEntryPermissionsAndStageDetails ( {
373+ entryUid : fieldMetadata . entry_uid ,
374+ contentTypeUid : fieldMetadata . content_type_uid ,
375+ locale : fieldMetadata . locale ,
376+ variantUid : fieldMetadata . variant ,
377+ } ) ;
378+ const { isDisabled : fieldDisabled } = isFieldDisabled (
379+ fieldSchema ,
380+ eventDetails ,
381+ entryAcl ,
382+ entryWorkflowStageDetails
383+ ) ;
384+ const fieldType = getFieldType ( fieldSchema ) ;
385+ generateCustomCursor ( {
386+ fieldType,
387+ customCursor,
388+ fieldDisabled,
389+ } ) ;
390+ }
391+
392+ const handleMouseHover = async (
393+ params : HandleMouseHoverParams
394+ ) : Promise < void > => await throttledMouseHover ( params ) ;
402395
403396export default handleMouseHover ;
0 commit comments