@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
59 lines • 2.56 kB
JavaScript
export const getBlockControlsSurfaceControlSide = (context, reliableAnchorEnabled) => (context === null || context === void 0 ? void 0 : context.targetNode.type.name) === 'layoutColumn' || Boolean(context) && reliableAnchorEnabled && (context === null || context === void 0 ? void 0 : context.targetNode.parentType) !== 'doc' ? undefined : 'left';
const getNodeContext = (state, pos) => {
const node = state.doc.nodeAt(pos);
if (!node) {
return undefined;
}
const $pos = state.doc.resolve(pos);
const ancestors = [];
for (let depth = 0; depth <= $pos.depth; depth++) {
const ancestor = $pos.node(depth);
ancestors.push({
depth,
pos: depth === 0 ? 0 : $pos.before(depth),
type: {
name: ancestor.type.name
}
});
}
return {
ancestors,
depth: $pos.depth + 1,
node,
parentType: $pos.parent.type.name,
pos,
type: {
name: node.type.name
}
};
};
export const createBlockControlsSurfaceContextForPosition = (state, targetPos, activeNode) => {
var _activeNode$rootPos, _getNodeContext, _activeNode$rootNodeT;
const targetNodeContext = getNodeContext(state, targetPos);
if (!targetNodeContext) {
return undefined;
}
const activeNodeContext = activeNode ? getNodeContext(state, activeNode.pos) : undefined;
const rootPos = activeNode ? (_activeNode$rootPos = activeNode.rootPos) !== null && _activeNode$rootPos !== void 0 ? _activeNode$rootPos : activeNode.pos : targetPos;
const rootNodeContext = (_getNodeContext = getNodeContext(state, rootPos)) !== null && _getNodeContext !== void 0 ? _getNodeContext : targetNodeContext;
return {
activeControlKey: activeNode === null || activeNode === void 0 ? void 0 : activeNode.controlKey,
activeNode: activeNode && activeNodeContext ? {
...activeNodeContext,
rootPos,
rootType: {
name: (_activeNode$rootNodeT = activeNode.rootNodeType) !== null && _activeNode$rootNodeT !== void 0 ? _activeNode$rootNodeT : rootNodeContext.type.name
}
} : undefined,
rootNode: rootNodeContext,
targetNode: targetNodeContext
};
};
export const createBlockControlsSurfaceContext = (view, activeNode, target = 'root') => {
var _activeNode$rootPos2;
if (!activeNode) {
return undefined;
}
const rootPos = (_activeNode$rootPos2 = activeNode.rootPos) !== null && _activeNode$rootPos2 !== void 0 ? _activeNode$rootPos2 : activeNode.pos;
return createBlockControlsSurfaceContextForPosition(view.state, target === 'active' ? activeNode.pos : rootPos, activeNode);
};