slate-edit-table
Version:
A Slate plugin to handle keyboard events in tables.
62 lines (46 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
require('slate');
var _changes = require('../changes');
function onBackspace(event, change, editor, opts) {
var value = change.value;
var startBlock = value.startBlock,
startOffset = value.startOffset,
isCollapsed = value.isCollapsed,
endBlock = value.endBlock,
document = value.document;
var startCell = document.getClosest(startBlock.key, opts.isCell);
var endCell = document.getClosest(endBlock.key, opts.isCell);
var startBlockIndex = startCell.nodes.findIndex(function (block) {
return block.key == startBlock.key;
});
// If a cursor is collapsed at the start of the first block, do nothing
if (startOffset === 0 && isCollapsed && startBlockIndex === 0) {
event.preventDefault();
return change;
}
// If "normal" deletion, we continue
if (startCell === endCell) {
return undefined;
}
// If cursor is between multiple blocks,
// we clear the content of the cells
event.preventDefault();
var blocks = value.blocks;
// Get all cells that contains the selection
var cells = blocks.map(function (node) {
return node.type === opts.typeCell ? node : document.getClosest(node.key, function (a) {
return a.type === opts.typeCell;
});
}).toSet();
// Clear all the selection
cells.forEach(function (block) {
return (0, _changes.clearCell)(opts, change, block);
});
// Update the selection to avoid reset of selection
var updatedCell = change.value.document.getDescendant(cells.last().key);
return change.collapseToStartOf(updatedCell);
}
exports.default = onBackspace;