import AbstractView from "../view/base-view" ; import { OperatorType , JSONType } from "./component" ; import Block from "./block" ; import StructureCollection from "./structure-collection" ; import ComponentType from "../const/component-type" ; import StructureType from "../const/structure-type" ; import ComponentFactory from "../factory" ; class Article extends StructureCollection < Block > { type = ComponentType . article ; structureType = StructureType . structure ; static create ( componentFactory , json ) { let children = ( json . children || [ ] ) . map ( ( each ) => { return componentFactory . typeMap [ each . type ] . create ( componentFactory , each ) ; } ) ; let article = componentFactory . buildArticle ( ) ; article . modifyDecorate ( json . style , json . data ) ; if ( json . id ) { article . id = json . id ; } article . add ( 0 , ... children ) ; return article ; } isEmpty ( ) { return this . getSize ( ) === 1 && this . getChild ( 0 ) ?. getSize ( ) === 0 ; } childHeadDelete ( block ) { let prev = this . getPrev ( block ) ; if ( prev ) { return block . sendTo ( prev ) ; } if ( block . type !== ComponentType . paragraph ) { let exchanged = block . exchangeTo ( this . getComponentFactory ( ) . typeMap . PARAGRAPH , [ ] ) ; return [ { id : exchanged [ 0 ] . id , offset : 0 } ] ; } if ( ! block . decorate . isEmpty ( ) ) { block . modifyDecorate ( { remove : "all" } , { remove : "all" } ) ; } return [ { id : block . id , offset : 0 } ] ; } remove ( start , end ? ) { let operator = super . remove ( start , end ) ; if ( this . getSize ( ) === 0 ) { return this . add ( 0 , this . getComponentFactory ( ) . buildParagraph ( ) ) ; } return operator ; } getJSON ( ) { let raw = super . getJSON ( ) ; raw . id = this . id ; return raw ; } render ( contentView ) { return contentView . buildArticle ( this . id , ( ) => this . children . toArray ( ) . map ( ( each ) => each . render ( contentView ) ) , this . decorate . getStyle ( ) , this . decorate . getData ( ) ) ; } } export default Article ;