mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
1723ad9bdb
* Fixes several usability issues with QueryField component - Can now indent with tab and `mod+[`, `mod+]` - Copy/Cut preserves new lines, and paste correctly splits blocks now - Adds support for selection hotkeys
39 lines
816 B
TypeScript
39 lines
816 B
TypeScript
// @ts-ignore
|
|
import { Change } from 'slate';
|
|
|
|
function getIndent(text: any) {
|
|
let offset = text.length - text.trimLeft().length;
|
|
if (offset) {
|
|
let indent = text[0];
|
|
while (--offset) {
|
|
indent += text[0];
|
|
}
|
|
return indent;
|
|
}
|
|
return '';
|
|
}
|
|
|
|
export default function NewlinePlugin() {
|
|
return {
|
|
onKeyDown(event: KeyboardEvent, change: Change) {
|
|
const { value } = change;
|
|
if (!value.isCollapsed) {
|
|
return undefined;
|
|
}
|
|
|
|
if (event.key === 'Enter' && event.shiftKey) {
|
|
event.preventDefault();
|
|
|
|
const { startBlock } = value;
|
|
const currentLineText = startBlock.text;
|
|
const indent = getIndent(currentLineText);
|
|
|
|
return change
|
|
.splitBlock()
|
|
.insertText(indent)
|
|
.focus();
|
|
}
|
|
},
|
|
};
|
|
}
|