import { TextAtom } from '../core-atoms/text' ; import { ModelPrivate } from '../editor-model/model-private' ; import { range } from '../editor-model/selection-utils' ; import { MODE_SHIFT_COMMANDS } from '../editor/parse-math-string' ; import { ParseMode } from '../public/core' ; import { InsertOptions , Range } from '../public/mathfield' ; import { MathfieldPrivate } from './mathfield-private' ; const CLIPBOARD_LATEX_BEGIN = '\\begin{equation*}' ; const CLIPBOARD_LATEX_END = '\\end{equation*}' ; export const defaultExportHook = ( _from , latex , _range ) : string => { if ( ! MODE_SHIFT_COMMANDS . some ( ( x ) => latex . startsWith ( x [ 0 ] ) && latex . endsWith ( x [ 1 ] ) ) ) { latex = `${ CLIPBOARD_LATEX_BEGIN } ${ latex } ${ CLIPBOARD_LATEX_END }` ; } return latex ; } ; export class ModeEditor { static _registry : Record < string , ModeEditor > = { } ; constructor ( name ) { ModeEditor . _registry [ name ] = this ; } static onPaste ( mode , mathfield , ev ) { return ModeEditor . _registry [ mode ] . onPaste ( mathfield , ev ) ; } static onCopy ( mathfield , ev ) { if ( ! ev . clipboardData ) return ; const model = mathfield . model ; const exportRange = model . selectionIsCollapsed ? [ 0 , model . lastOffset ] : range ( model . selection ) ; const atoms = model . getAtoms ( exportRange ) ; if ( atoms . every ( ( x ) => x . mode === 'text' || ! x . mode ) ) { ev . clipboardData . setData ( 'text/plain' , atoms . filter ( ( x ) => x instanceof TextAtom ) . map ( ( x ) => x . value ) . join ( '' ) ) ; } else if ( atoms . every ( ( x ) => x . mode === 'latex' ) ) { ev . clipboardData . setData ( 'text/plain' , model . getAtoms ( exportRange , { includeChildren : true } ) . map ( ( x ) => x . value ?? '' ) . join ( '' ) ) ; } else { let latex = '' ; if ( atoms . length === 1 && atoms [ 0 ] . verbatimLatex ) { latex = atoms [ 0 ] . verbatimLatex ; } if ( ! latex ) { latex = model . getValue ( exportRange , 'latex-expanded' ) ; } ev . clipboardData . setData ( 'application/x-latex' , latex ) ; ev . clipboardData . setData ( 'text/plain' , mathfield . options . onExport ( mathfield , latex , exportRange ) ) ; const mathJson = model . getValue ( exportRange , 'math-json' ) ; if ( mathJson ) { ev . clipboardData . setData ( 'application/json' , mathJson ) ; } const mathMl = model . getValue ( exportRange , 'math-ml' ) ; if ( mathMl ) { ev . clipboardData . setData ( 'application/mathml+xml' , mathMl ) ; } } ev . preventDefault ( ) ; } static insert ( mode , model , text , options = { } ) { return ModeEditor . _registry [ mode ] . insert ( model , text , options ) ; } onPaste ( _mathfield , _ev ) { return false ; } insert ( _model , _text , _options ) { return false ; } }