import AbstractView from "../view/base-view" ; import { JSONType } from "./component" ; import PlainText from "./plain-text" ; import ContentCollection from "./content-collection" ; import ComponentType from "../const/component-type" ; import Block from "./block" ; import ComponentFactory from "../factory" ; import { Editor } from ".." ; class CodeBlock extends PlainText { type = ComponentType . codeBlock ; language : string ; static create ( componentFactory , json ) { const code = componentFactory . buildCode ( json . content , json . language ) ; code . modifyDecorate ( json . style , json . data ) ; return code ; } static exchange ( componentFactory , block , args = [ ] ) { let parent = block . getParent ( ) ; let prev = parent . getPrev ( block ) ; if ( prev instanceof CodeBlock ) { prev . receive ( block ) ; return [ prev ] ; } else { let code = componentFactory . buildCode ( ) ; if ( block instanceof ContentCollection ) { code . add ( 0 , block . children . map ( ( each ) => each . content ) . join ( "" ) ) ; } block . replaceSelf ( code ) ; return [ code ] ; } } constructor ( content = "" , language = "" , editor ? ) { super ( content , editor ) ; this . language = language ; } setLanguage ( language ) { this . componentWillChange ( ) ; this . language = language ; this . updateComponent ( [ this ] ) ; } createEmpty ( ) { const code = this . getComponentFactory ( ) . buildCode ( "\n" , this . language ) ; code . modifyDecorate ( this . decorate . copyStyle ( ) , this . decorate . copyData ( ) ) ; return code ; } getJSON ( ) { let json = super . getJSON ( ) ; json . language = this . language ; return json ; } render ( contentView ) { return contentView . buildCodeBlock ( this . id , this . content . join ( "" ) , this . language , this . decorate . getStyle ( ) , this . decorate . getData ( ) ) ; } } export default CodeBlock ;