import * as monaco from "monaco-editor" ; import "./languages/bsl/contribution" ; import "./languages/turbo-gherkin/contribution" ; import { language as gherkin } from './languages/turbo-gherkin/configuration' ; import { IVanessaEditor , EventsManager , createModel , VanessaEditorEvent , disposeModel , VAEditorType } from "./common" ; import { VanessaEditor } from "./vanessa-editor" ; import { VanessaTabs } from "./vanessa-tabs" ; export class VanessaDiffEditor implements IVanessaEditor { static editors : Array < VanessaDiffEditor > = [ ] ; private static standaloneInstance : VanessaDiffEditor ; public navigator : monaco . editor . IDiffNavigator ; public editor : monaco . editor . IStandaloneDiffEditor ; public eventsManager : EventsManager ; public static createStandalone ( original = "" , modified = "" , language = gherkin . id ) { if ( this . standaloneInstance ) return this . standaloneInstance ; VanessaEditor . disposeStandalone ( ) ; VanessaTabs . disposeStandalone ( ) ; const model = { original : monaco . editor . createModel ( original , language ) , modified : monaco . editor . createModel ( modified , language ) , } ; return this . standaloneInstance = new VanessaDiffEditor ( model ) ; } public static getStandalone ( ) { return this . standaloneInstance ; } public static disposeStandalone ( ) { if ( this . standaloneInstance ) { this . standaloneInstance . dispose ( ) ; this . standaloneInstance = null ; } } constructor ( model , readOnly = false ) { let node = document . getElementById ( "VanessaEditorContainer" ) ; this . editor = monaco . editor . createDiffEditor ( node , { contextmenu : false , originalEditable : false , scrollBeyondLastLine : false , glyphMargin : true , automaticLayout : true , readOnly : readOnly , useShadowDOM : false , } ) ; this . editor . setModel ( model ) ; this . eventsManager = new EventsManager ( this ) ; this . navigator = monaco . editor . createDiffNavigator ( this . editor ) ; VanessaDiffEditor . editors . push ( this ) ; } public dispose ( ) { if ( VanessaDiffEditor . standaloneInstance === this ) VanessaDiffEditor . standaloneInstance = null ; const index = VanessaDiffEditor . editors . indexOf ( this ) ; if ( index >= 0 ) VanessaDiffEditor . editors . splice ( index , 1 ) ; const oe = this . editor . getOriginalEditor ( ) ; const me = this . editor . getModifiedEditor ( ) ; const original = oe ? oe . getModel ( ) : null ; const modified = me ? me . getModel ( ) : null ; this . eventsManager . dispose ( ) ; this . navigator . dispose ( ) ; this . editor . dispose ( ) ; disposeModel ( original ) ; disposeModel ( modified ) ; } public resetModel ( ) { const oe = this . editor . getOriginalEditor ( ) ; const me = this . editor . getModifiedEditor ( ) ; const original = oe ? oe . getModel ( ) : null ; const modified = me ? me . getModel ( ) : null ; this . editor . setModel ( { original : monaco . editor . createModel ( '' ) , modified : monaco . editor . createModel ( '' ) , } ) ; disposeModel ( original ) ; disposeModel ( modified ) ; } ; public setValue = ( oldValue , oldFile , newValue , newFile ) => { this . editor . setModel ( { original : createModel ( oldValue , oldFile ) , modified : createModel ( newValue , newFile ) , } ) ; } ; public static findModel ( model ) { return this . editors . some ( e => { const original = e . editor . getOriginalEditor ( ) ; if ( original && original . getModel ( ) === model ) return true ; const modified = e . editor . getModifiedEditor ( ) ; if ( modified && modified . getModel ( ) === model ) return true ; return false ; } ) ; } public domNode = ( ) => this . editor . _containerDomElement ; public popMessage = ( ) => EventsManager . popMessage ( ) ; public onFileSave = ( ) => this . fireEvent ( VanessaEditorEvent . PRESS_CTRL_S , this . getModel ( ) ) ; public fireEvent = ( event , arg = undefined ) => this . eventsManager . fireEvent ( event , arg ) ; public setReadOnly = ( arg ) => this . editor . updateOptions ( { readOnly : arg } ) ; public setSideBySide = ( value ) => this . editor . updateOptions ( { renderSideBySide : value } ) ; public setTheme = ( theme ) => monaco . editor . setTheme ( theme ) ; public getModel = ( ) => this . editor . getModifiedEditor ( ) . getModel ( ) ; public canNavigate = ( ) => this . navigator . canNavigate ( ) ? true : false ; public previous = ( ) => this . navigator . previous ( ) ; public next = ( ) => this . navigator . next ( ) ; public focus ( ) { this . editor . focus ( ) ; } public trigger = ( source , handlerId , payload = undefined ) => this . editor . trigger ( source , handlerId , payload ) ; public get type ( ) { return VAEditorType . DiffEditor ; } }