Prevent duplicate emit from codemirror editors (#18194)

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
Hannes Küttner
2023-04-14 20:56:26 +02:00
committed by GitHub
parent 1e271fa1d0
commit 74adbbc3e8
3 changed files with 22 additions and 5 deletions

View File

@@ -40,6 +40,7 @@ const { width } = useWindowSize();
const codemirrorEl = ref<HTMLTextAreaElement | null>();
let codemirror: CodeMirror.Editor | null;
let previousContent: string | null = null;
const isMultiLine = computed(() => ['text', 'json'].includes(props.type));
@@ -84,8 +85,14 @@ onMounted(async () => {
}
codemirror.on('change', (doc, { origin }) => {
if (origin === 'setValue') return;
const content = doc.getValue();
// prevent duplicate emits with same content
if (content === previousContent) return;
previousContent = content;
if (origin === 'setValue') return;
if (typeof props.value === 'object') {
try {
emit('input', content !== '' ? parseJSON(content) : null);

View File

@@ -77,6 +77,7 @@ export default defineComponent({
const codemirrorEl = ref<HTMLTextAreaElement | null>(null);
let codemirror: CodeMirror.Editor | null;
let previousContent: string | null = null;
onMounted(async () => {
if (codemirrorEl.value) {
@@ -94,10 +95,14 @@ export default defineComponent({
await setLanguage();
codemirror.on('change', (cm, { origin }) => {
if (origin === 'setValue') return;
const content = cm.getValue();
// prevent duplicate emits with same content
if (content === previousContent) return;
previousContent = content;
if (origin === 'setValue') return;
if (props.type === 'json') {
if (content.length === 0) {
return emit('input', null);

View File

@@ -289,6 +289,7 @@ export default defineComponent({
const markdownInterface = ref<HTMLElement>();
const codemirrorEl = ref<HTMLTextAreaElement>();
let codemirror: CodeMirror.Editor | null = null;
let previousContent: string | null = null;
const view = ref(['editor']);
@@ -321,10 +322,14 @@ export default defineComponent({
});
codemirror.on('change', (cm, { origin }) => {
if (origin === 'setValue') return;
const content = cm.getValue();
// prevent duplicate emits with same content
if (content === previousContent) return;
previousContent = content;
if (origin === 'setValue') return;
emit('input', content);
});
}