dom_is-form-dirty.js

/**
 * @module isFormDirty
 * @description Uses old variables and functions from form_editor.js that lets us know if the form is dirty in
 * the form editor.
 *
 * @since 1.0.6
 *
 * @return {boolean} Whether the current form is dirty.
 *
 * @example
 * import { isFormDirty } from "@gravityforms/utils";
 *
 * function Example() {
 *   if ( isFormDirty() ) {
 *       // do something
 *   }
 * }
 *
 */
export default function isFormDirty() {
	if ( ! window.gforms_original_json || ! window.UpdateFormObject ) {
		return false;
	}
	window.UpdateFormObject();
	const legacyHtml = window?.gf_legacy?.is_legacy === '1';
	const original = JSON.parse( JSON.stringify( JSON.parse( window.gforms_original_json ) ) );
	const current = JSON.parse( JSON.stringify( window.form ) );
	if ( legacyHtml ) {
		original.fields.forEach( ( field, i ) => {
			delete original.fields[ i ].layoutGroupId;
		} );
		current.fields.forEach( ( field, i ) => {
			delete current.fields[ i ].layoutGroupId;
		} );
	}

	return JSON.stringify( original ) !== JSON.stringify( current );
}