1616-->
1717
1818<template >
19- <div :id = " editorId " class =" rule-diff-editor" :style =" { height: editorHeight }" ></div >
19+ <div ref = " editorEl " class =" rule-diff-editor" :style =" { height: editorHeight }" ></div >
2020</template >
2121
2222<script lang="ts" setup>
23- import { computed , onBeforeUnmount , onMounted , watch } from ' vue'
23+ import { computed , onBeforeUnmount , onMounted , ref , watch } from ' vue'
2424import * as monaco from ' monaco-editor'
2525
2626const props = defineProps ({
@@ -32,36 +32,39 @@ const props = defineProps({
3232 type: String ,
3333 default: ' '
3434 },
35- editorId: {
36- type: String ,
37- default: ' rule-diff-editor'
38- },
3935 height: {
4036 type: [String , Number ],
4137 default: ' 420px'
4238 }
4339})
4440
4541let diffEditor: monaco .editor .IStandaloneDiffEditor | null = null
42+ const editorEl = ref <HTMLElement | null >(null )
4643const editorHeight = computed (() =>
4744 typeof props .height === ' number' ? ` ${props .height }px ` : props .height
4845)
4946
47+ const disposeModels = () => {
48+ const model = diffEditor ?.getModel ()
49+ model ?.original .dispose ()
50+ model ?.modified .dispose ()
51+ }
52+
5053const render = () => {
5154 if (! diffEditor ) {
5255 return
5356 }
57+ disposeModels ()
5458 const originalModel = monaco .editor .createModel (props .original || ' ' , ' json' )
5559 const modifiedModel = monaco .editor .createModel (props .modified || ' ' , ' json' )
5660 diffEditor .setModel ({ original: originalModel , modified: modifiedModel })
5761}
5862
5963onMounted (() => {
60- const el = document .getElementById (props .editorId )
61- if (! el ) {
64+ if (! editorEl .value ) {
6265 return
6366 }
64- diffEditor = monaco .editor .createDiffEditor (el , {
67+ diffEditor = monaco .editor .createDiffEditor (editorEl . value , {
6568 automaticLayout: true ,
6669 renderSideBySide: true ,
6770 readOnly: true ,
7679)
7780
7881onBeforeUnmount (() => {
82+ disposeModels ()
7983 diffEditor ?.dispose ()
8084 diffEditor = null
8185})
0 commit comments