import { Atom } from '../core/atom' ; import type { ModelPrivate } from './model-private' ; import { Range } from '../public/mathfield' ; import { Style } from '../public/core' ; import { isArray } from '../common/types' ; import { DEFAULT_FONT_SIZE } from '../core/font-metrics' ; import { PrivateStyle } from '../core/context' ; export function applyStyleToUnstyledAtoms ( atom , style ? ) { if ( ! atom || ! style ) return ; if ( isArray < Atom > ( atom ) ) { atom . forEach ( ( x ) => applyStyleToUnstyledAtoms ( x , style ) ) ; } else if ( typeof atom === 'object' ) { if ( ! atom . style . color && ! atom . style . backgroundColor && ! atom . style . fontFamily && ! atom . style . fontShape && ! atom . style . fontSeries && ! atom . style . fontSize && ! atom . style . variant && ! atom . style . variantStyle ) { atom . applyStyle ( style ) ; applyStyleToUnstyledAtoms ( atom . body , style ) ; applyStyleToUnstyledAtoms ( atom . above , style ) ; applyStyleToUnstyledAtoms ( atom . below , style ) ; applyStyleToUnstyledAtoms ( atom . subscript , style ) ; applyStyleToUnstyledAtoms ( atom . superscript , style ) ; } } } /**
 * Apply a style (color, background) to the selection.
 *
 * If the style is already applied to the selection, remove it. If the selection
 * has the style partially applied (i.e. only some sections), remove it from
 * those sections, and apply it to the entire selection.
 */ export function applyStyle ( model , range , style , options ) { function everyStyle ( property , value ) { for ( const atom of atoms ) { if ( atom . style [ property ] !== value ) return false ; } return true ; } range = model . normalizeRange ( range ) ; if ( range [ 0 ] === range [ 1 ] ) return false ; const atoms = model . getAtoms ( range , { includeChildren : true } ) ; if ( options . operation === 'toggle' ) { if ( style . color && everyStyle ( 'color' , style . color ) ) { style . color = 'none' ; style . verbatimColor = undefined ; } if ( style . backgroundColor && everyStyle ( 'backgroundColor' , style . backgroundColor ) ) { style . backgroundColor = 'none' ; style . verbatimBackgroundColor = undefined ; } if ( style . fontFamily && everyStyle ( 'fontFamily' , style . fontFamily ) ) { style . fontFamily = 'none' ; } if ( style . fontSeries && everyStyle ( 'fontSeries' , style . fontSeries ) ) { style . fontSeries = 'auto' ; } if ( style . fontShape && everyStyle ( 'fontShape' , style . fontShape ) ) { style . fontShape = 'auto' ; } if ( style . fontSize && everyStyle ( 'fontSize' , style . fontSize ) ) { style . fontSize = DEFAULT_FONT_SIZE ; } } for ( const atom of atoms ) atom . applyStyle ( style ) ; return true ; }