2022-06-14 08:19:32 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useCallback, useEffect, useState } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useActiveEditorType } from './use-active-editor-type';
|
|
|
|
|
|
|
|
export type ProductTourStepName =
|
|
|
|
| 'product-name'
|
|
|
|
| 'product-description'
|
|
|
|
| 'product-data'
|
|
|
|
| 'product-short-description'
|
|
|
|
| 'product-image'
|
|
|
|
| 'product-tags'
|
|
|
|
| 'product-categories';
|
|
|
|
|
|
|
|
const getInputValue = ( id: string ) => {
|
|
|
|
return ( document.querySelector( id ) as HTMLInputElement ).value;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getTinyMceValue = ( id: string ) => {
|
2022-06-16 06:02:28 +00:00
|
|
|
const iframe = document.querySelector< HTMLIFrameElement >( id );
|
2022-06-21 08:37:34 +00:00
|
|
|
const tinymce =
|
|
|
|
iframe?.contentWindow?.document.querySelector< HTMLElement >(
|
|
|
|
'#tinymce'
|
|
|
|
);
|
2022-06-16 06:02:28 +00:00
|
|
|
return tinymce?.innerHTML || '';
|
2022-06-14 08:19:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getTextareaValue = ( id: string ) => {
|
2022-06-16 06:02:28 +00:00
|
|
|
return document.querySelector< HTMLTextAreaElement >( id )?.value || '';
|
2022-06-14 08:19:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getProductDescriptionValue = ( isContentEditorTmceActive: boolean ) => {
|
|
|
|
return isContentEditorTmceActive
|
|
|
|
? getTinyMceValue( '#content_ifr' )
|
|
|
|
: getTextareaValue( '#wp-content-editor-container > .wp-editor-area' );
|
|
|
|
};
|
|
|
|
|
|
|
|
const getProductShortDescriptionValue = (
|
|
|
|
isExcerptEditorTmceActive: boolean
|
|
|
|
) => {
|
|
|
|
return isExcerptEditorTmceActive
|
|
|
|
? getTinyMceValue( '#excerpt_ifr' )
|
|
|
|
: getTextareaValue( '#wp-excerpt-editor-container > .wp-editor-area' );
|
|
|
|
};
|
|
|
|
|
|
|
|
const getProductImageValue = () => {
|
|
|
|
return (
|
2022-06-16 06:02:28 +00:00
|
|
|
document.querySelector< HTMLImageElement >( '#set-post-thumbnail img' )
|
|
|
|
?.src || ''
|
2022-06-14 08:19:32 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parses categories into a string of true/false. Should be enough to catch any change.
|
|
|
|
const getProductCategoriesValue = () => {
|
|
|
|
return Array.from(
|
2022-06-16 06:02:28 +00:00
|
|
|
document.querySelectorAll< HTMLInputElement >(
|
2022-06-14 08:19:32 +00:00
|
|
|
'#product_cat-all #product_catchecklist input'
|
|
|
|
)
|
|
|
|
)
|
2022-06-16 06:02:28 +00:00
|
|
|
.map( ( x ) => x.checked )
|
2022-06-14 08:19:32 +00:00
|
|
|
.join( ',' );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parses all tags as string of tags separated by comma.
|
|
|
|
const getProductTagsValue = () => {
|
2022-06-16 06:02:28 +00:00
|
|
|
return Array.from(
|
|
|
|
document.querySelectorAll< HTMLLIElement >( '#product_tag li' )
|
|
|
|
)
|
|
|
|
.map( ( x ) => ( x.lastChild as Text ).textContent )
|
2022-06-14 08:19:32 +00:00
|
|
|
.join( ',' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom hook that is used to detect if the product form has any changes and isn't empty.
|
|
|
|
* This hook returns two functions:
|
|
|
|
* 1. setIsLoaded which is used to save initial product form values when form is ready.
|
|
|
|
* 2. hasChanged which is used for querying for the step's input changes.
|
|
|
|
*/
|
|
|
|
export const useProductStepChange = () => {
|
|
|
|
const { isTmce: isContentEditorTmceActive } = useActiveEditorType( {
|
|
|
|
editorWrapSelector: '#wp-content-wrap',
|
|
|
|
} );
|
|
|
|
const { isTmce: isExcerptEditorTmceActive } = useActiveEditorType( {
|
|
|
|
editorWrapSelector: '#wp-excerpt-wrap',
|
|
|
|
} );
|
|
|
|
const [ initialValues, setInitialValues ] = useState<
|
|
|
|
Partial< Record< ProductTourStepName, string > >
|
|
|
|
>( {} );
|
|
|
|
const [ isLoaded, setIsLoaded ] = useState( false );
|
2022-06-21 08:37:34 +00:00
|
|
|
const getValues: () => Partial< Record< ProductTourStepName, string > > =
|
|
|
|
useCallback( () => {
|
|
|
|
return {
|
|
|
|
'product-name': getInputValue( '#title' ),
|
|
|
|
'product-description': getProductDescriptionValue(
|
|
|
|
isContentEditorTmceActive
|
|
|
|
),
|
|
|
|
// For product data, we're just going to detect change if price is changed.
|
|
|
|
'product-data': getInputValue( '#_regular_price' ),
|
|
|
|
'product-short-description': getProductShortDescriptionValue(
|
|
|
|
isExcerptEditorTmceActive
|
|
|
|
),
|
|
|
|
'product-image': getProductImageValue(),
|
|
|
|
'product-tags': getProductTagsValue(),
|
|
|
|
'product-categories': getProductCategoriesValue(),
|
|
|
|
};
|
|
|
|
}, [ isContentEditorTmceActive, isExcerptEditorTmceActive ] );
|
2022-06-14 08:19:32 +00:00
|
|
|
|
|
|
|
// If value has changed and isn't empty, returns as changed.
|
|
|
|
const hasUpdatedInfo: ( key: ProductTourStepName ) => boolean = useCallback(
|
|
|
|
( key ) => {
|
|
|
|
const newValues = getValues();
|
|
|
|
return (
|
|
|
|
initialValues[ key ] !== newValues[ key ] &&
|
|
|
|
newValues[ key ] !== ''
|
|
|
|
);
|
|
|
|
},
|
|
|
|
[ getValues, initialValues ]
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isLoaded ) {
|
|
|
|
setInitialValues( getValues() );
|
|
|
|
}
|
|
|
|
}, [ setInitialValues, isLoaded, getValues ] );
|
|
|
|
|
|
|
|
return { setIsLoaded, hasUpdatedInfo };
|
|
|
|
};
|