| 64 | |
| 65 | // Return existing model or create a new model if not exists |
| 66 | _initializeFile(path) { |
| 67 | const fs = window.require('fs'); |
| 68 | |
| 69 | const value = fs.readFileSync(path, { encoding: 'utf-8' }); |
| 70 | let model = monaco.editor |
| 71 | .getModels() |
| 72 | .find(model => model.uri.path === path); |
| 73 | |
| 74 | if (model) { |
| 75 | // If a model exists, we need to update it's value |
| 76 | // This is needed because the content for the file might have been modified externally |
| 77 | // Use `pushEditOperations` instead of `setValue` or `applyEdits` to preserve undo stack |
| 78 | model.pushEditOperations( |
| 79 | [], |
| 80 | [ |
| 81 | { |
| 82 | range: model.getFullModelRange(), |
| 83 | text: value, |
| 84 | }, |
| 85 | ] |
| 86 | ); |
| 87 | } else { |
| 88 | model = monaco.editor.createModel( |
| 89 | value, |
| 90 | this._getLanguage(this.props.path), |
| 91 | new monaco.Uri().with({ path }), |
| 92 | |
| 93 | ); |
| 94 | model.updateOptions({ |
| 95 | tabSize: 2, |
| 96 | insertSpaces: true, |
| 97 | }); |
| 98 | } |
| 99 | this.editor.setModel(model); |
| 100 | return model; |
| 101 | }; |
| 102 | |
| 103 | // Setup or restore monaco model for the opening file path |
| 104 | _openFile(path) { |