import { ArrayExt } from '@lumino/algorithm' ; import { CommandRegistry } from '@lumino/commands' ; import { BoxPanel , DockPanel , Widget } from '@lumino/widgets' ; import { WSAdapter } from './wsadapter' ; import { CodeMirrorEditor , EDITOR_SCHEMA } from './widget' ; import '../style/index.css' ; let commands = new CommandRegistry ( ) ; /**
 * Initialize the applicaiton.
 */ async function init ( ) { let adapter = new WSAdapter ( ( ) => new WebSocket ( window . location . origin . replace ( /^http/ , 'ws' ) ) ) ; let store = await adapter . createStore ( [ EDITOR_SCHEMA ] ) ; let editorTable = store . get ( EDITOR_SCHEMA ) ; if ( editorTable . isEmpty ) { store . beginTransaction ( ) ; try { editorTable . update ( { e1 : { } , e2 : { } , e3 : { } } ) ; } finally { store . endTransaction ( ) ; } } let e1 = new CodeMirrorEditor ( store , 'e1' ) ; e1 . title . label = 'First Document' ; let e2 = new CodeMirrorEditor ( store , 'e2' ) ; e2 . title . label = 'Second Document' ; let e3 = new CodeMirrorEditor ( store , 'e3' ) ; e3 . title . label = 'Third Document' ; let dock = new DockPanel ( { spacing : 4 } ) ; dock . addWidget ( e1 ) ; dock . addWidget ( e2 , { mode : 'split-right' , ref : e1 } ) ; dock . addWidget ( e3 , { mode : 'split-bottom' , ref : e2 } ) ; dock . id = 'dock' ; let box = new BoxPanel ( { spacing : 2 } ) ; box . id = 'main' ; box . addWidget ( dock ) ; window . onresize = ( ) => { box . update ( ) ; } ; Widget . attach ( box , document . body ) ; commands . addCommand ( 'undo' , { label : 'Undo' , execute : ( ) => { let editor = ArrayExt . findFirstValue ( [ e1 , e2 , e3 ] , ( e ) => e . hasFocus ( ) ) ; if ( editor ) { editor . undo ( ) ; } } } ) ; commands . addCommand ( 'redo' , { label : 'Redo' , execute : ( ) => { let editor = ArrayExt . findFirstValue ( [ e1 , e2 , e3 ] , ( e ) => e . hasFocus ( ) ) ; if ( editor ) { editor . redo ( ) ; } } } ) ; commands . addKeyBinding ( { keys : [ 'Accel Z' ] , selector : 'body' , command : 'undo' } ) ; commands . addKeyBinding ( { keys : [ 'Accel Shift Z' ] , selector : 'body' , command : 'redo' } ) ; document . addEventListener ( 'keydown' , ( event ) => { commands . processKeydownEvent ( event ) ; } , true ) ; } window . onload = init ;