11<script setup>
22import CodeMirror from ' codemirror' ;
3- import { computed , markRaw , nextTick , onMounted , ref , useAttrs , useTemplateRef , watch } from ' vue' ;
3+ import { computed , markRaw , nextTick , onBeforeUnmount , onMounted , ref , useAttrs , useTemplateRef , watch } from ' vue' ;
44import Select from ' ./Select/Select.vue' ;
55import { colorMode as colorModeApi } from ' @api' ;
66
@@ -99,6 +99,7 @@ const modes = ref([
9999const codemirror = ref (null );
100100const codemirrorElement = useTemplateRef (' codemirrorElement' );
101101const fullScreenMode = ref (false );
102+ const visibilityObserver = ref (null );
102103
103104defineOptions ({
104105 inheritAttrs: false ,
@@ -110,7 +111,19 @@ defineExpose({
110111});
111112
112113onMounted (() => {
113- nextTick (() => initCodeMirror ());
114+ nextTick (() => {
115+ initCodeMirror ();
116+ initVisibilityObserver ();
117+ });
118+ });
119+
120+ onBeforeUnmount (() => {
121+ visibilityObserver .value ? .disconnect ();
122+
123+ if (codemirror .value ) {
124+ codemirror .value .getWrapperElement ().remove ();
125+ codemirror .value = null ;
126+ }
114127});
115128
116129function initCodeMirror () {
@@ -149,6 +162,21 @@ function initCodeMirror() {
149162 });
150163}
151164
165+ function initVisibilityObserver () {
166+ visibilityObserver .value = new IntersectionObserver (
167+ (entries ) => {
168+ entries .forEach ((entry ) => {
169+ if (entry .isIntersecting ) {
170+ codemirror .value ? .refresh ();
171+ }
172+ });
173+ },
174+ { threshold: 0.01 },
175+ );
176+
177+ visibilityObserver .value .observe (codemirrorElement .value );
178+ }
179+
152180watch (
153181 () => props .disabled ,
154182 (value ) => {
0 commit comments