Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
onTab(new CustomEvent('shortcut', { detail: 'Shift+Tab' }));
return true;
}
if (block && (force || block !== paper.blocks.getDefault() && !block.defaultFollows)) {
editor.formatLine([ range[0], range[0] ], {}, SOURCE_USER);
return true;
}
}
if (range[0] + range[1] === 0) {
const line = getLine(editor.contents, range[0]);
if (flattenBlock(line, true)) return true;
} else {
// The "from" block needs to stay the same. The "to" block gets merged into it
if (range[0] === range[1]) {
const line = getLine(editor.contents, range[0]);
if (range[0] === line.start && flattenBlock(line)) {
return;
}
range[0]--;
}
editor.deleteText(range, SOURCE_USER);
}
}
function flattenBlock(line: Line, force?: boolean) {
if (!range) return;
const block = paper.blocks.findByAttributes(line.attributes) || paper.blocks.getDefault();
if (block.indentable && line.attributes.indent) {
onTab(new CustomEvent('shortcut', { detail: 'Shift+Tab' }));
return true;
}
if (block && (force || block !== paper.blocks.getDefault() && !block.defaultFollows)) {
editor.formatLine([ range[0], range[0] ], {}, SOURCE_USER);
return true;
}
}
if (range[0] + range[1] === 0) {
const line = getLine(editor.contents, range[0]);
if (flattenBlock(line, true)) return true;
} else {
// The "from" block needs to stay the same. The "to" block gets merged into it
if (range[0] === range[1]) {
const line = getLine(editor.contents, range[0]);
if (range[0] === line.start && flattenBlock(line)) {
return;
}
range[0]--;
}
editor.deleteText(range, SOURCE_USER);
}
}
function onEnter(event: KeyboardEvent) {
if (event.defaultPrevented) return;
event.preventDefault();
const range = editor.getSelectedRange();
if (!range) return;
const isCollapsed = range[0] === range[1];
const line = getLine(editor.contents, range[0]);
let attributes = line.attributes;
const block = paper.blocks.findByAttributes(attributes) || paper.blocks.getDefault();
const isDefault = block === paper.blocks.getDefault();
const length = line.end - line.start - 1;
const atEnd = range[1] === line.end - 1;
if (atEnd && !isDefault && block.defaultFollows) {
attributes = {};
} else if (typeof block.getNextLineAttributes === 'function') {
attributes = block.getNextLineAttributes(attributes);
}
const activeFormats = editor.activeFormats;
if (!length && !isDefault && !block.defaultFollows && isCollapsed) {
editor.formatLine(range, {}, SOURCE_USER);
} else {
const selection: EditorRange = [ range[0] + 1, range[0] + 1 ];
editor.insertText(range, '\n', attributes, SOURCE_USER, selection);