post-locking.js
88 lines
| 1 | /** |
| 2 | * WordPress post locking utilities for ACF blocks |
| 3 | * Handles locking/unlocking post saving during block operations |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * Locks post saving in the WordPress editor for a specific block |
| 8 | * Used when block operations are in progress for a specific block instance |
| 9 | * |
| 10 | * @param {string} clientId - The block's client ID |
| 11 | */ |
| 12 | export const lockPostSaving = ( clientId ) => { |
| 13 | const dispatch = wp.data.dispatch( 'core/editor' ); |
| 14 | if ( dispatch ) { |
| 15 | dispatch.lockPostSaving( 'acf/block/' + clientId ); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | /** |
| 20 | * Unlocks post saving in the WordPress editor for a specific block |
| 21 | * Called when block operations are complete for a specific block instance |
| 22 | * |
| 23 | * @param {string} clientId - The block's client ID |
| 24 | */ |
| 25 | export const unlockPostSaving = ( clientId ) => { |
| 26 | const dispatch = wp.data.dispatch( 'core/editor' ); |
| 27 | if ( dispatch ) { |
| 28 | dispatch.unlockPostSaving( 'acf/block/' + clientId ); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * Checks if post saving is currently locked for a specific block |
| 34 | * |
| 35 | * @param {string} clientId - The block's client ID |
| 36 | * @returns {boolean} - True if post saving is locked for this block |
| 37 | */ |
| 38 | export const isPostSavingLocked = ( clientId ) => { |
| 39 | const dispatch = wp.data.dispatch( 'core/editor' ); |
| 40 | if ( ! dispatch ) { |
| 41 | return false; |
| 42 | } |
| 43 | return wp.data.select( 'core/editor' ).isPostSavingLocked( `acf/block/${ clientId }` ); |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * Locks post saving with a custom lock name |
| 48 | * Used for global operations that aren't tied to a specific block |
| 49 | * |
| 50 | * @param {string} lockName - The name of the lock |
| 51 | */ |
| 52 | export const lockPostSavingByName = ( lockName ) => { |
| 53 | const dispatch = wp.data.dispatch( 'core/editor' ); |
| 54 | if ( dispatch ) { |
| 55 | dispatch.lockPostSaving( 'acf/block/' + lockName ); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Unlocks post saving with a custom lock name |
| 61 | * Used for global operations that aren't tied to a specific block |
| 62 | * |
| 63 | * @param {string} lockName - The name of the lock |
| 64 | */ |
| 65 | export const unlockPostSavingByName = ( lockName ) => { |
| 66 | const dispatch = wp.data.dispatch( 'core/editor' ); |
| 67 | if ( dispatch ) { |
| 68 | dispatch.unlockPostSaving( 'acf/block/' + lockName ); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * Sorts an object's keys alphabetically and returns a new object |
| 74 | * Used for consistent object serialization and comparison |
| 75 | * Ensures that objects with same properties in different order produce same hash |
| 76 | * |
| 77 | * @param {Object} obj - Object to sort |
| 78 | * @returns {Object} - New object with sorted keys in alphabetical order |
| 79 | */ |
| 80 | export const sortObjectKeys = ( obj ) => { |
| 81 | return Object.keys( obj ) |
| 82 | .sort() |
| 83 | .reduce( ( sortedObj, key ) => { |
| 84 | sortedObj[ key ] = obj[ key ]; |
| 85 | return sortedObj; |
| 86 | }, {} ); |
| 87 | }; |
| 88 |