Skip to content

Commit e48e818

Browse files
committed
chore: reconfigure autocompletion instead of recreating editor state
1 parent b7b0865 commit e48e818

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

lib/components/Input/InputEditor.jsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useMemo, useRef, useState } from 'react';
22
import { renderToStaticMarkup } from 'react-dom/server';
33

44
import { basicSetup } from 'codemirror';
5-
import { EditorState, Annotation } from '@codemirror/state';
5+
import { Compartment, EditorState, Annotation } from '@codemirror/state';
66
import { EditorView, placeholder } from '@codemirror/view';
77
import { linter } from '@codemirror/lint';
88
import { json, jsonParseLinter } from '@codemirror/lang-json';
@@ -11,7 +11,9 @@ import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';
1111

1212
import { getAutocompletionExtensions } from '../../utils/autocompletion';
1313

14-
const fromProp = Annotation.define();
14+
const fromPropAnnotation = Annotation.define();
15+
16+
const autocompletionCompartment = new Compartment();
1517

1618
const DEFAULT_OUTPUT = {},
1719
DEFAULT_VARIABLES_FOR_ELEMENT = [];
@@ -76,17 +78,17 @@ export default function InputEditor({
7678
return errors;
7779
};
7880

79-
const startState = EditorState.create({
81+
const editorState = EditorState.create({
8082
doc: value,
8183
extensions: [
8284
basicSetup,
8385
json(),
8486
linter(source, { delay: 100 }),
85-
...getAutocompletionExtensions(autocompletion),
87+
autocompletionCompartment.of(getAutocompletionExtensions(autocompletion)),
8688
placeholder('Provide process variables in JSON format'),
8789
EditorView.updateListener.of((update) => {
8890
if (update.docChanged) {
89-
if (update.transactions.some(transaction => transaction.annotation(fromProp))) {
91+
if (update.transactions.some(transaction => transaction.annotation(fromPropAnnotation))) {
9092
return;
9193
}
9294

@@ -99,7 +101,7 @@ export default function InputEditor({
99101
});
100102

101103
const view = new EditorView({
102-
state: startState,
104+
state: editorState,
103105
parent: ref.current,
104106
});
105107

@@ -108,7 +110,17 @@ export default function InputEditor({
108110
return () => {
109111
view.destroy();
110112
};
111-
}, [ autocompletion ]);
113+
}, []);
114+
115+
useEffect(() => {
116+
if (!editorView) return;
117+
118+
editorView.dispatch({
119+
effects: autocompletionCompartment.reconfigure(
120+
getAutocompletionExtensions(autocompletion)
121+
)
122+
});
123+
}, [ autocompletion, editorView ]);
112124

113125
useEffect(() => {
114126
if (!editorView) return;
@@ -122,7 +134,7 @@ export default function InputEditor({
122134
to: editorValue.length,
123135
insert: value
124136
},
125-
annotations: fromProp.of(true)
137+
annotations: fromPropAnnotation.of(true)
126138
});
127139
}
128140
}, [ editorView, value ]);

0 commit comments

Comments
 (0)