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