mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-17 23:13:51 +01:00
Debounce autocomplete
This commit is contained in:
@@ -67,7 +67,7 @@ export default function Editor({
|
||||
// Create codemirror instance when ref initializes
|
||||
useEffect(() => {
|
||||
if (ref.current === null) return;
|
||||
console.log('INIT EDITOR');
|
||||
// console.log('INIT EDITOR');
|
||||
let view: EditorView | null = null;
|
||||
try {
|
||||
const langHolder = new Compartment();
|
||||
@@ -102,7 +102,7 @@ export default function Editor({
|
||||
// Update language extension when contentType changes
|
||||
useEffect(() => {
|
||||
if (cm === null) return;
|
||||
console.log('UPDATE LANG');
|
||||
// console.log('UPDATE LANG');
|
||||
const ext = getLanguageExtension({ contentType, useTemplating });
|
||||
cm.view.dispatch({ effects: cm.langHolder.reconfigure(ext) });
|
||||
}, [contentType]);
|
||||
@@ -110,6 +110,7 @@ export default function Editor({
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
dangerouslySetInnerHTML={{ __html: '' }}
|
||||
className={classnames(
|
||||
className,
|
||||
'cm-wrapper text-base bg-background',
|
||||
|
||||
29
src-web/components/Editor/autocomplete.ts
Normal file
29
src-web/components/Editor/autocomplete.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { closeCompletion, startCompletion } from '@codemirror/autocomplete';
|
||||
import { EditorView } from 'codemirror';
|
||||
import { debounce } from 'lodash';
|
||||
|
||||
/*
|
||||
* Debounce autocomplete until user stops typing for `millis` milliseconds.
|
||||
*/
|
||||
export function debouncedAutocompletionDisplay({ millis }: { millis: number }) {
|
||||
// TODO: Figure out how to show completion without setting context.explicit = true
|
||||
const debouncedStartCompletion = debounce(function (view: EditorView) {
|
||||
startCompletion(view);
|
||||
}, millis);
|
||||
|
||||
return EditorView.updateListener.of(({ view, docChanged }) => {
|
||||
// const completions = currentCompletions(view.state);
|
||||
// const status = completionStatus(view.state);
|
||||
|
||||
// If the document hasn't changed, we don't need to do anything
|
||||
if (!docChanged) return;
|
||||
|
||||
if (view.state.doc.length === 0) {
|
||||
debouncedStartCompletion.cancel();
|
||||
closeCompletion(view);
|
||||
return;
|
||||
}
|
||||
|
||||
debouncedStartCompletion(view);
|
||||
});
|
||||
}
|
||||
@@ -33,6 +33,7 @@ import {
|
||||
rectangularSelection,
|
||||
} from '@codemirror/view';
|
||||
import { tags as t } from '@lezer/highlight';
|
||||
import { debouncedAutocompletionDisplay } from './autocomplete';
|
||||
import { twig } from './twig/extension';
|
||||
import { url } from './url/extension';
|
||||
|
||||
@@ -107,7 +108,8 @@ export const baseExtensions = [
|
||||
drawSelection(),
|
||||
dropCursor(),
|
||||
bracketMatching(),
|
||||
autocompletion({ closeOnBlur: true, interactionDelay: 200 }),
|
||||
debouncedAutocompletionDisplay({ millis: 1000 }),
|
||||
autocompletion({ closeOnBlur: true, interactionDelay: 200, activateOnTyping: false }),
|
||||
syntaxHighlighting(myHighlightStyle),
|
||||
EditorState.allowMultipleSelections.of(true),
|
||||
];
|
||||
|
||||
@@ -47,6 +47,7 @@ export function completions(context: CompletionContext) {
|
||||
label: toStartOfVariable ? `${openTag}${v.name}${closeTag}` : v.name,
|
||||
apply: `${openTag}${v.name}${closeTag}`,
|
||||
type: 'variable',
|
||||
matchLen,
|
||||
}))
|
||||
// Filter out exact matches
|
||||
.filter((o) => o.label !== toMatch.text),
|
||||
|
||||
Reference in New Issue
Block a user