import Line , { LineIds , LineIterator } from './Line' ; import Op , { OpIterator } from '../delta/Op' ; namespace LineOp { export function iterator ( lines , lineIds ? ) { return new LineOpIterator ( lines , lineIds ) ; } export function length ( op ) { return Op . length ( op ) ; } } export default LineOp ; export class LineOpIterator { lineIterator : LineIterator ; opIterator : OpIterator ; constructor ( lines , lineIds ? ) { this . lineIterator = Line . iterator ( lines , lineIds ) ; const line = this . lineIterator . peek ( ) ; this . opIterator = Op . iterator ( line ?. content . ops || [ ] ) ; } hasNext ( ) { return this . opIterator . hasNext ( ) || this . lineIterator . hasNext ( ) ; } next ( length ? ) { let op = this . opIterator . next ( length ) ; if ( op . retain === Infinity && this . lineIterator . hasNext ( ) ) { op = getLineOp ( this . nextLine ( ) ) ; } return op ; } nextLine ( ) { const line = this . lineIterator . next ( ) ; const nextLine = this . lineIterator . peek ( ) ; this . opIterator = new OpIterator ( nextLine ?. content . ops || [ ] ) ; return line ; } peek ( ) { if ( this . opIterator . hasNext ( ) || ! this . lineIterator . hasNext ( ) ) { return this . opIterator . peek ( ) ; } else { return getLineOp ( this . peekLine ( ) ) ; } } peekLine ( ) { return this . lineIterator . peek ( ) ; } peekLength ( ) { if ( this . opIterator . hasNext ( ) || ! this . lineIterator . hasNext ( ) ) { return this . opIterator . peekLength ( ) ; } else { return 1 ; } } peekLineLength ( ) { return this . lineIterator . peekLength ( ) ; } peekType ( ) { if ( this . opIterator . hasNext ( ) ) { return this . opIterator . peekType ( ) ; } else if ( this . lineIterator . hasNext ( ) ) { return 'insert' ; } else { return 'retain' ; } } restCurrentLine ( ) { return this . opIterator . rest ( ) ; } restLines ( ) { if ( this . opIterator . offset ) { this . lineIterator . next ( this . opIterator . offset ) ; } return this . lineIterator . rest ( ) ; } } function getLineOp ( line ) { const op = { insert : '\n' } as Op ; if ( line . attributes ) op . attributes = line . attributes ; return op ; }