@atlaskit/editor-plugin-code-block
Version:
Code block plugin for @atlaskit/editor-core
49 lines (48 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAllChangedCodeBlocksInTransaction = getAllChangedCodeBlocksInTransaction;
exports.getAllCodeBlockNodesInDoc = getAllCodeBlockNodesInDoc;
exports.getCursor = getCursor;
function getCursor(selection) {
return selection.$cursor || undefined;
}
function getAllCodeBlockNodesInDoc(state) {
var codeBlockNodes = [];
state.doc.descendants(function (node, pos) {
if (node.type === state.schema.nodes.codeBlock) {
codeBlockNodes.push({
node: node,
pos: pos
});
return false;
}
return true;
});
return codeBlockNodes;
}
function getAllChangedCodeBlocksInTransaction(tr) {
var changedCodeBlocks = [];
var nodePositions = new Set();
tr.steps.forEach(function (step) {
var mapResult = step.getMap();
mapResult.forEach(function (oldStart, oldEnd, newStart, newEnd) {
tr.doc.nodesBetween(newStart, Math.min(newEnd, tr.doc.content.size), function (node, pos) {
if (node.type.name === 'codeBlock') {
if (!nodePositions.has(pos)) {
nodePositions.add(pos);
changedCodeBlocks.push({
node: node,
pos: pos
});
}
}
});
});
});
if (changedCodeBlocks.length < 1) {
return null;
}
return changedCodeBlocks;
}