feat(editor): Improve UX for brace completion from selection (#5024)

 Improve UX for brace completion from selection
This commit is contained in:
Iván Ovejero
2022-12-27 09:21:32 +01:00
committed by GitHub
parent afc529799d
commit 52077e2c45
2 changed files with 41 additions and 23 deletions

View File

@@ -39,15 +39,32 @@ export default mixins(expressionManager, workflowHelpers).extend({
},
watch: {
value(newValue) {
const payload: Record<string, unknown> = {
changes: {
from: 0,
to: this.editor?.state.doc.length,
insert: newValue,
},
selection: { anchor: this.cursorPosition, head: this.cursorPosition },
};
/**
* If completion from selection, preserve selection.
*/
if (this.editor) {
const [range] = this.editor.state.selection.ranges;
const isBraceAutoinsertion =
this.editor.state.sliceDoc(range.from - 1, range.from) === '{' &&
this.editor.state.sliceDoc(range.to, range.to + 1) === '}';
if (isBraceAutoinsertion) {
payload.selection = { anchor: range.from, head: range.to };
}
}
try {
this.editor?.dispatch({
changes: {
from: 0,
to: this.editor.state.doc.length,
insert: newValue,
},
selection: { anchor: this.cursorPosition, head: this.cursorPosition },
});
this.editor?.dispatch(payload);
} catch (_) {
// ignore out-of-range selection error on drop
}