|
|
@@ -1,35 +1,41 @@
|
|
|
import CodeMirror from 'codemirror';
|
|
|
|
|
|
const ID = 'autocompleteOnTyping';
|
|
|
-const DEFAULT = true;
|
|
|
+const DEFAULT = 100;
|
|
|
+const OPTIONS = 'options';
|
|
|
+const STATE = 'state';
|
|
|
const HINT_OPTIONS = 'hintOptions';
|
|
|
const COMPLETE_SINGLE = 'completeSingle';
|
|
|
const PICKED = 'picked';
|
|
|
const TIMER = 'timer';
|
|
|
-const DELAY = 100;
|
|
|
|
|
|
// eslint-disable-next-line no-return-assign
|
|
|
-const getMyState = ({ state }) => (state[ID] || (state[ID] = {}));
|
|
|
+const getMyState = ({ [STATE]: state }) => (state[ID] || (state[ID] = {}));
|
|
|
|
|
|
const delayedComplete = cm => {
|
|
|
- const { options } = cm;
|
|
|
+ const options = cm[OPTIONS];
|
|
|
const hintOptions = options[HINT_OPTIONS] || (options[HINT_OPTIONS] = {});
|
|
|
+ const myState = getMyState(cm);
|
|
|
hintOptions[COMPLETE_SINGLE] = false;
|
|
|
- getMyState(cm)[PICKED] = false;
|
|
|
+ myState[TIMER] = 0;
|
|
|
+ myState[PICKED] = false;
|
|
|
cm.execCommand('autocomplete');
|
|
|
setTimeout(() => {
|
|
|
hintOptions[COMPLETE_SINGLE] = true;
|
|
|
});
|
|
|
};
|
|
|
|
|
|
-const cancelDelay = ({ [TIMER]: timer }) => {
|
|
|
- if (timer) clearTimeout(timer);
|
|
|
+const cancelDelay = myState => {
|
|
|
+ if (myState[TIMER]) {
|
|
|
+ clearTimeout(myState[TIMER]);
|
|
|
+ myState[TIMER] = 0;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
const onChanges = (cm, [info]) => {
|
|
|
const myState = getMyState(cm);
|
|
|
const lastTyped = info.text[info.text.length - 1];
|
|
|
- if (cm.state.completionActive
|
|
|
+ if (cm[STATE].completionActive
|
|
|
|| info.origin && !info.origin.includes('input')
|
|
|
|| !lastTyped) {
|
|
|
return;
|
|
|
@@ -40,7 +46,7 @@ const onChanges = (cm, [info]) => {
|
|
|
}
|
|
|
if (/[-a-z!]$/i.test(lastTyped)) {
|
|
|
cancelDelay(myState);
|
|
|
- myState[TIMER] = setTimeout(delayedComplete, DELAY, cm);
|
|
|
+ myState[TIMER] = setTimeout(delayedComplete, cm[OPTIONS][ID], cm);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -55,6 +61,6 @@ CodeMirror.defineOption(ID, DEFAULT, (cm, value) => {
|
|
|
cm[onOff]('pick', onPicked);
|
|
|
if (myState && !value) {
|
|
|
cancelDelay(myState);
|
|
|
- delete cm.state[ID];
|
|
|
+ delete cm[STATE][ID];
|
|
|
}
|
|
|
});
|