import { Attributes } from '@delta/attributes' ; import { SelectionShape , Selection } from '@delta/Selection' ; import { GenericOp } from '@delta/operations' ; import clone from 'ramda/es/clone' ; import { DocumentDelta } from '@delta/DocumentDelta' ; import mergeLeft from 'ramda/es/mergeLeft' ; public export interface Document { /**
     * A list of operations as per deltajs definition.
     */ readonly ops : GenericOp [ ] ; /**
     * A contiguous range of selectable items.
     */ readonly currentSelection : SelectionShape ; /**
     * The attributes encompassed by  Document . currentSelection or the attributes at cursor.
     * `null` values represent attributes to be removed.
      readonly selectedTextAttributes : Attributes . Map ; /**
     * The diff ops which were used to produce current ops by combining previous ops.
     */ readonly lastDiff : GenericOp [ ] ; remarks readonly schemaVersion : number ; } param nextDocument public export type DocumentUpdater = ( nextDocument : Document ) => Promise < void > ; public export function buildEmptyDocument ( ) { return { currentSelection : { start : 0 , end : 0 } , ops : [ { insert : '\n' } ] , selectedTextAttributes : { } , lastDiff : [ ] , schemaVersion : 1 , } ; } /**
 * Clone a peace of  Document .
 *
 *  param content public export function cloneDocument ( content ) { return { ops : clone ( content . ops ) , currentSelection : content . currentSelection , selectedTextAttributes : content . selectedTextAttributes , lastDiff : content . lastDiff , schemaVersion : content . schemaVersion , } ; } export function applyTextTransformToSelection ( attributeName , attributeValue , document ) { const { currentSelection , ops , selectedTextAttributes } = document ; const delta = new DocumentDelta ( ops ) ; const selection = Selection . fromShape ( currentSelection ) ; const userAttributes = { [ attributeName ] : attributeValue } ; const atomicUpdate = delta . applyTextTransformToSelection ( selection , attributeName , attributeValue ) ; const nextSelectedAttributes = mergeLeft ( userAttributes , selectedTextAttributes ) ; return { selectedTextAttributes : nextSelectedAttributes , ops : atomicUpdate . delta . ops , lastDiff : atomicUpdate . diff . ops , } ; }