import { v4 as uuidv4 } from "uuid" ; import Editor from "../editor" ; import Component , { OperatorType , JSONType , Snapshoot } from "./component" ; import ComponentFactory from "../factory" ; import { AnyObject } from "../decorate/index" ; import StructureCollection from "./structure-collection" ; import { createError } from "../util" ; export type BlockType = typeof Block ; export interface BlockSnapshoot extends Snapshoot { active : boolean ; } abstract class Block extends Component { active : boolean = false ; parent ? : StructureCollection < Block > ; editor ? : Editor ; static create ( componentFactory , json ) { throw createError ( "component need implement static create method" , this ) ; } static exchange ( componentFactory , component , args ? ) { throw createError ( "component need implement static exchange method" , this ) ; } constructor ( editor ? ) { super ( ) ; this . editor = editor ; this . init ( ) ; } init ( ) { } exchangeTo ( builder , args = [ ] ) { if ( builder === this . constructor ) { return [ this ] ; } return builder . exchange ( this . getComponentFactory ( ) , this , args ) ; } addInto ( collection , index = - 1 ) { return collection . add ( index , this ) ; } removeSelf ( ) { let parent = this . getParent ( ) ; let index = parent . findChildrenIndex ( this ) ; return parent . remove ( index , index + 1 ) ; } replaceSelf ( ... blockList ) { let parent = this . getParent ( ) ; parent . replaceChild ( blockList , this ) ; return [ { id : blockList [ 0 ] . id , offset : 0 } ] ; } modifyDecorate ( style ? , data ? ) { this . componentWillChange ( ) ; super . modifyDecorate ( style , data ) ; this . updateComponent ( [ this ] ) ; return ; } modifyContentDecorate ( start , end , style ? , data ? ) { return ; } addEmptyParagraph ( bottom ) { let parent = this . getParent ( ) ; let index = parent . findChildrenIndex ( this ) ; let paragraph = this . getComponentFactory ( ) . buildParagraph ( ) ; parent . add ( index + ( bottom ? 1 : 0 ) , paragraph ) ; return [ { id : paragraph . id , offset : 0 } ] ; } add ( index , ... children ) { return ; } remove ( start , end ? ) { return ; } split ( index , ... componentList ) { return ; } sendTo ( component ) { return ; } receive ( component ) { return ; } destory ( ) { this . active = false ; this . parent = undefined ; super . destory ( ) ; } setEditor ( editor ? ) { this . editor = editor ; } isEmpty ( ) { return true ; } createEmpty ( ) { throw createError ( "component need implement createEmpty method." , this ) ; } clone ( ) { let newBlock = { ... this } ; newBlock . style = { ... this . style , } ; newBlock . data = { ... this . data , } ; Object . setPrototypeOf ( newBlock , Object . getPrototypeOf ( this ) ) ; newBlock . id = uuidv4 ( ) ; return newBlock ; } getParent ( ) { let parent = this . parent ; if ( ! parent ) throw createError ( "the node has expired." , this ) ; return parent ; } getSize ( ) { return 0 ; } componentWillChange ( ) { this . editor ?. $emit ( "componentWillChange" ) ; } updateComponent ( componentList ) { this . editor ?. $emit ( "updateComponent" , componentList ) ; } getComponentFactory ( ) { return this . editor ? this . editor . componentFactory : ComponentFactory . getInstance ( ) ; } } export default Block ;