| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
|
| 6 |
// Always-present diagnostics appended to the copied payload only — never counted |
| 7 |
// as removable post data. The data-key lists stay in PHP (single source of truth). |
| 8 |
export const SYSTEM_META_KEYS = [ |
| 9 |
'plugin_version', |
| 10 |
'wp_version', |
| 11 |
'wp_post_id', |
| 12 |
]; |
| 13 |
|
| 14 |
/** |
| 15 |
* Does the post hold any BeyondWords data worth removing? |
| 16 |
* |
| 17 |
* Checks the data fields only (never the always-present system fields), so the |
| 18 |
* Remove button tracks the post's current state, not a mount-time snapshot. |
| 19 |
* |
| 20 |
* @param {Object} meta Live custom-field values keyed by field name. |
| 21 |
* @param {string[]} dataKeys Current + deprecated meta keys (sourced from PHP). |
| 22 |
* @return {boolean} Whether any data field has a non-empty value. |
| 23 |
*/ |
| 24 |
export const hasBeyondwordsData = ( meta, dataKeys = [] ) => |
| 25 |
dataKeys.some( ( key ) => !! meta[ key ]?.length ); |
| 26 |
|
| 27 |
/** |
| 28 |
* Build the plain-text diagnostics payload for the Copy button. |
| 29 |
* |
| 30 |
* Reads the same live `meta` as {@link hasBeyondwordsData} so Copy and Remove |
| 31 |
* can never disagree; the key lists render in the order supplied. |
| 32 |
* |
| 33 |
* @param {Object} meta Live values keyed by field name (incl. system). |
| 34 |
* @param {string[]} currentKeys Current meta keys, in display order. |
| 35 |
* @param {string[]} deprecatedKeys Deprecated meta keys, in display order. |
| 36 |
* @return {string} The clipboard payload. |
| 37 |
*/ |
| 38 |
export const getTextToCopy = ( |
| 39 |
meta, |
| 40 |
currentKeys = [], |
| 41 |
deprecatedKeys = [] |
| 42 |
) => { |
| 43 |
const line = ( key ) => `${ key }\r\n${ meta[ key ] }`; |
| 44 |
|
| 45 |
return ( |
| 46 |
[ |
| 47 |
...currentKeys.map( line ), |
| 48 |
`=== ${ __( 'Deprecated', 'speechkit' ) } ===`, |
| 49 |
...deprecatedKeys.map( line ), |
| 50 |
`=== ${ __( 'System', 'speechkit' ) } ===`, |
| 51 |
...SYSTEM_META_KEYS.map( line ), |
| 52 |
`=== ${ __( 'Copied using the Block Editor', 'speechkit' ) } ===`, |
| 53 |
].join( '\r\n\r\n' ) + '\r\n\r\n' |
| 54 |
); |
| 55 |
}; |
| 56 |
|