(url: string, currentFile: string, setCurrentFile: (f: string) => void)
| 68 | }) |
| 69 | } |
| 70 | async function loadFileInner(url: string, currentFile: string, setCurrentFile: (f: string) => void) { |
| 71 | if (currentFile) { |
| 72 | const state = editor.saveViewState() |
| 73 | if (state) { |
| 74 | editorStates.set(currentFile, state) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // FIXME: destroy previous model? |
| 79 | const parsedUri = monaco.Uri.parse(url) |
| 80 | let maybeModel = monaco.editor.getModel(parsedUri) |
| 81 | if (maybeModel) { |
| 82 | editor.setModel(maybeModel) |
| 83 | const state = editorStates.get(url) |
| 84 | if (state) { |
| 85 | editor.restoreViewState(state) |
| 86 | } |
| 87 | } else { |
| 88 | const res = await fetch(url) |
| 89 | if (!res.ok) { |
| 90 | console.error('failed to load file', url, res.status, res.statusText) |
| 91 | return |
| 92 | } |
| 93 | const text = await res.text() |
| 94 | |
| 95 | let model: monaco.editor.ITextModel |
| 96 | if (url.endsWith('.json')) { |
| 97 | model = monaco.editor.createModel(text, 'json', parsedUri) |
| 98 | } else { |
| 99 | model = monaco.editor.createModel(text, 'leafConf', parsedUri) |
| 100 | validateModel(model) |
| 101 | model.onDidChangeContent(() => validateModel(model)) |
| 102 | } |
| 103 | editor.setModel(model) |
| 104 | console.log('loaded url: ' + url) |
| 105 | } |
| 106 | setCurrentFile(url) |
| 107 | } |
| 108 | function getCurrent(): Promise<{ url: string, text: string } | undefined> { |
| 109 | return currentFileLock.withLock((currentFile, _) => { |
| 110 | if (!currentFile) { |
no test coverage detected