grafana/public/app/features/explore/slate-plugins/braces.ts
kay delaney 68d6da77da
Chore: Update Slate to 0.47.8 (#19197)
* Chore: Update Slate to 0.47.8
Closes #17430
2019-09-23 12:26:05 +01:00

71 lines
2.0 KiB
TypeScript

import { Plugin } from '@grafana/slate-react';
import { Editor as CoreEditor } from 'slate';
const BRACES: any = {
'[': ']',
'{': '}',
'(': ')',
};
export default function BracesPlugin(): Plugin {
return {
onKeyDown(event: KeyboardEvent, editor: CoreEditor, next: Function) {
const { value } = editor;
switch (event.key) {
case '(':
case '{':
case '[': {
event.preventDefault();
const {
start: { offset: startOffset, key: startKey },
end: { offset: endOffset, key: endKey },
focus: { offset: focusOffset },
} = value.selection;
const text = value.focusText.text;
// If text is selected, wrap selected text in parens
if (value.selection.isExpanded) {
editor
.insertTextByKey(startKey, startOffset, event.key)
.insertTextByKey(endKey, endOffset + 1, BRACES[event.key])
.moveEndBackward(1);
} else if (
focusOffset === text.length ||
text[focusOffset] === ' ' ||
Object.values(BRACES).includes(text[focusOffset])
) {
editor.insertText(`${event.key}${BRACES[event.key]}`).moveBackward(1);
} else {
editor.insertText(event.key);
}
return true;
}
case 'Backspace': {
const text = value.anchorText.text;
const offset = value.selection.anchor.offset;
const previousChar = text[offset - 1];
const nextChar = text[offset];
if (BRACES[previousChar] && BRACES[previousChar] === nextChar) {
event.preventDefault();
// Remove closing brace if directly following
editor
.deleteBackward(1)
.deleteForward(1)
.focus();
return true;
}
}
default: {
break;
}
}
return next();
},
};
}