Files
grafana/packages/grafana-ui/src/slate-plugins/newline.ts
kay delaney a70aba1384 Chore: Update slate and related packages (#54566)
* Chore: Update slate and related packages
2022-09-06 15:23:48 +01:00

38 lines
773 B
TypeScript

import { Plugin } from 'slate-react';
function getIndent(text: string) {
let offset = text.length - text.trimLeft().length;
if (offset) {
let indent = text[0];
while (--offset) {
indent += text[0];
}
return indent;
}
return '';
}
export function NewlinePlugin(): Plugin {
return {
onKeyDown(event, editor, next) {
const value = editor.value;
if (value.selection.isExpanded) {
return next();
}
if (event.key === 'Enter') {
event.preventDefault();
const { startBlock } = value;
const currentLineText = startBlock.text;
const indent = getIndent(currentLineText);
return editor.splitBlock().insertText(indent).focus();
}
return next();
},
};
}