| 1 |
/** |
| 2 |
* Internal dependencies |
| 3 |
*/ |
| 4 |
import { CUSTOM_VALUE } from './constants'; |
| 5 |
|
| 6 |
/** |
| 7 |
* The suggestion features that expose site-wide generation defaults. |
| 8 |
* |
| 9 |
* @since 3.24.0 |
| 10 |
*/ |
| 11 |
export type DefaultsFeature = 'excerptSuggestions' | 'titleSuggestions'; |
| 12 |
|
| 13 |
/** |
| 14 |
* Returns a feature's site-wide default for the given setting. |
| 15 |
* |
| 16 |
* PHP resolves these from the plugin's settings and injects them. Read at call |
| 17 |
* time rather than at module load, so a screen that injects them late is still |
| 18 |
* served. |
| 19 |
* |
| 20 |
* @since 3.24.0 |
| 21 |
* |
| 22 |
* @param {DefaultsFeature} feature The feature to read the default of. |
| 23 |
* @param {'persona'|'tone'} setting The setting to read. |
| 24 |
* @param {string} fallback The value to use when none was injected. |
| 25 |
* |
| 26 |
* @return {string} The site-wide default. |
| 27 |
*/ |
| 28 |
export const getSiteDefault = ( |
| 29 |
feature: DefaultsFeature, |
| 30 |
setting: 'persona' | 'tone', |
| 31 |
fallback: string |
| 32 |
): string => { |
| 33 |
const value = window.wpParselyContentHelperDefaults?.[ feature ]?.[ setting ]; |
| 34 |
|
| 35 |
return 'string' === typeof value && '' !== value ? value : fallback; |
| 36 |
}; |
| 37 |
|
| 38 |
/** |
| 39 |
* Resolves a stored tone or persona into the value to generate with. |
| 40 |
* |
| 41 |
* The custom sentinel means "Custom" was selected without a value entered, so |
| 42 |
* it carries no value of its own. The site's default stands in rather than the |
| 43 |
* shipped one, which would ignore how the site is configured. |
| 44 |
* |
| 45 |
* @since 3.24.0 |
| 46 |
* |
| 47 |
* @param {string} stored The user's stored value. |
| 48 |
* @param {DefaultsFeature} feature The feature being generated for. |
| 49 |
* @param {'persona'|'tone'} setting The setting being resolved. |
| 50 |
* @param {string} shipped The default to use when the site injects none. |
| 51 |
* |
| 52 |
* @return {string} The value to send with the generation request. |
| 53 |
*/ |
| 54 |
export const forGeneration = ( |
| 55 |
stored: string, |
| 56 |
feature: DefaultsFeature, |
| 57 |
setting: 'persona' | 'tone', |
| 58 |
shipped: string |
| 59 |
): string => { |
| 60 |
if ( CUSTOM_VALUE !== stored && '' !== stored ) { |
| 61 |
return stored; |
| 62 |
} |
| 63 |
|
| 64 |
return getSiteDefault( feature, setting, shipped ); |
| 65 |
}; |
| 66 |
|