Skip to content

Commit

Permalink
fix: cdn file models were accidentally disposed
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jul 5, 2023
1 parent 01778b7 commit 4301d86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
26 changes: 12 additions & 14 deletions src/monaco/env.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { watchEffect } from 'vue'
import * as monaco from 'monaco-editor-core'
import { jsDelivrUriBase } from '@volar/cdn'
import * as volar from '@volar/monaco'
import { editor, languages, Uri } from 'monaco-editor-core'
import editorWorker from 'monaco-editor-core/esm/vs/editor/editor.worker?worker'
import vueWorker from './vue.worker?worker'
import * as onigasm from 'onigasm'
import onigasmWasm from 'onigasm/lib/onigasm.wasm?url'
import { editor, languages, Uri } from 'monaco-editor-core'
import * as volar from '@volar/monaco'
import { watchEffect } from 'vue'
import { Store } from '../store'
import { getOrCreateModel } from './utils'
import type { CreateData } from './vue.worker'
import vueWorker from './vue.worker?worker'

let initted = false
export function initMonaco(store: Store) {
Expand All @@ -20,36 +20,34 @@ export function initMonaco(store: Store) {
// create a model for each file in the store
for (const filename in store.state.files) {
const file = store.state.files[filename]
if (monaco.editor.getModel(monaco.Uri.parse(`file:///${filename}`)))
continue
if (editor.getModel(Uri.parse(`file:///${filename}`))) continue
getOrCreateModel(
monaco.Uri.parse(`file:///${filename}`),
Uri.parse(`file:///${filename}`),
file.language,
file.code
)
}

// dispose of any models that are not in the store
for (const model of monaco.editor.getModels()) {
for (const model of editor.getModels()) {
const uri = model.uri.toString()
if (store.state.files[uri.substring('file:///'.length)]) continue
if (uri.startsWith('file:///node_modules/')) continue
if (uri.startsWith(jsDelivrUriBase + '/')) continue
if (uri.startsWith('inmemory://')) continue

model.dispose()
}
})

// Support for go to definition
monaco.editor.registerEditorOpener({
editor.registerEditorOpener({
openCodeEditor(_, resource) {
if (resource.scheme === 'https') {
// ignore cdn files
if (resource.toString().startsWith(jsDelivrUriBase + '/')) {
return true
}

const path = resource.path
if (/^\//.test(path) && !/^\/node_modules/.test(path)) {
if (/^\//.test(path)) {
const fileName = path.replace('/', '')
if (fileName !== store.state.activeFile.filename) {
store.setActive(fileName)
Expand Down
2 changes: 1 addition & 1 deletion src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SFCTemplateCompileOptions,
} from 'vue/compiler-sfc'
import { OutputModes } from './output/types'
import { Selection } from 'monaco-editor-core'
import type { Selection } from 'monaco-editor-core'

const defaultMainFile = 'src/App.vue'

Expand Down

0 comments on commit 4301d86

Please sign in to comment.