UNPKG

slate-edit-code

Version:

A Slate plugin to handle keyboard events in code blocks.

38 lines (30 loc) 1.03 kB
'use strict'; var LineAnchor = require('./LineAnchor'); var getLines = require('./getLines'); var getNewLine = require('./getNewLine'); var getIndent = require('./getIndent'); /** * User pressed Enter in an editor: * Insert a soft line and start it with the indentation from previous line */ function onEnter(event, data, state) { if (!state.isCollapsed) { return; } // Exit the code block // By letting the next plugin/core handle this event if (data.isMod) { return; } event.preventDefault(); var startBlock = state.startBlock; var startOffset = state.startOffset; var blockText = startBlock.text; var newLine = getNewLine(blockText); var lines = getLines(blockText, newLine); var anchor = LineAnchor.getForOffset(blockText, startOffset, newLine); var currentLine = lines.get(anchor.line); var lineToInsert = newLine + getIndent(currentLine, ''); return state.transform().insertText(lineToInsert).focus().apply(); } module.exports = onEnter;