import isObject from 'lodash.isobject' ; import forIn from 'lodash.forin' ; import axios from 'axios' ; export type emitter = ( event : string , ... args : any [ ] ) => void ; export default class CabinetBase { public plugins : { [ key ] : any ; } ; protected element : HTMLElement ; protected availablePlugins : string [ ] ; protected pluginOptions : ShimoSDK . Document . Plugins | ShimoSDK . Sheet . Plugins ; protected emitter : emitter ; protected entrypoint : string ; constructor ( element ) { this . element = element ; this . availablePlugins = [ ] ; this . plugins = { } ; this . emitter = ( event , ... args ) => { } ; } param element param tag param attrs param parentElement protected getElement ( element , tag , attrs ? , parentElmenent ? ) ; protected getElement ( element , tag ? , attrs ? , parentElmenent ? ) ; protected getElement ( element , tag ? , attrs ? , parentElement ? ) { if ( typeof element === 'string' ) { const elm = document . querySelector ( element ) ; if ( elm instanceof HTMLElement ) { element = elm ; } } const elmentExists = element instanceof HTMLElement ; if ( ! elmentExists ) { if ( ! tag ) { return null ; } element = document . createElement ( tag ) ; } if ( attrs ) { const elm = element as HTMLElement ; forIn ( attrs , ( value , key ) => { if ( key === 'classList' ) { elm . classList . add ( ... value ) ; return ; } elm . setAttribute ( key , value ) ; } ) ; } if ( ! elmentExists && parentElement instanceof HTMLElement ) { parentElement . appendChild ( element as HTMLElement ) ; } return element as HTMLElement ; } protected insertAfter ( referenceNode , newNode ) { referenceNode . parentNode ! . insertBefore ( newNode , referenceNode . nextSibling ) ; } /**
     * 过滤未启用的插件
     */ protected preparePlugins < T > ( plugins ? , defaultOptions ? , filter ? ) { if ( ! isObject ( plugins ) ) { plugins = this . availablePlugins . reduce ( ( result , plugin ) => { if ( typeof filter === 'function' && ! filter ( plugin ) ) { return result ; } result [ plugin ] = defaultOptions && defaultOptions [ plugin ] != null ? defaultOptions [ plugin ] : true ; return result ; } , { } ) as T ; } const result = { } as T ; for ( const plugin of this . availablePlugins ) { if ( typeof filter === 'function' && ! filter ( plugin ) ) { continue ; } const p = plugins [ plugin ] ; if ( p === false ) { continue ; } if ( typeof p === 'object' && p != null ) { result [ plugin ] = p ; } else { if ( p == null && defaultOptions && defaultOptions [ plugin ] != null ) { result [ plugin ] = defaultOptions [ plugin ] ; } else { result [ plugin ] = { } ; } } } return result ; } /**
     * 初始化所有启用的插件
     */ protected initPlugins ( editor ) { for ( const name of Object . keys ( this . pluginOptions ) ) { const method = `init${ name }` ; if ( typeof this [ method ] === 'function' ) { this [ method ] ( editor ) ; } } } protected async log ( level , data ) { if ( ! this . entrypoint ) { return ; } try { await axios . post ( `${ this . entrypoint }/log` , { level , data } ) ; } catch ( e ) { console . error ( 'Log to server error' , e , level , data ) ; } } protected promptIfHasUnsavedChanges ( collaboration ) { window . addEventListener ( 'beforeunload' , ( e ) => { if ( collaboration . haveUnsavedChange ( ) ) { e . preventDefault ( ) ; e . returnValue = '' ; } } ) ; } }