Skip to content

Commit ec212fe

Browse files
[6.x] Refresh CodeMirror instance when switching tabs (#14199)
1 parent ac9fa1c commit ec212fe

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

resources/js/components/ui/CodeEditor.vue

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import 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';
44
import Select from './Select/Select.vue';
55
import { colorMode as colorModeApi } from '@api';
66
@@ -99,6 +99,7 @@ const modes = ref([
9999
const codemirror = ref(null);
100100
const codemirrorElement = useTemplateRef('codemirrorElement');
101101
const fullScreenMode = ref(false);
102+
const visibilityObserver = ref(null);
102103
103104
defineOptions({
104105
inheritAttrs: false,
@@ -110,7 +111,19 @@ defineExpose({
110111
});
111112
112113
onMounted(() => {
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
116129
function 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+
152180
watch(
153181
() => props.disabled,
154182
(value) => {

0 commit comments

Comments
 (0)