@atlaskit/editor-plugin-block-controls
Version:
Block controls plugin for @atlaskit/editor-core
50 lines (46 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getMatchingBlockMarks = void 0;
/** True when `mark` has an equal counterpart (type + attrs) in `marks`. */
var hasMatchingMark = function hasMatchingMark(mark, marks) {
var found = mark.type.isInSet(marks);
return !!found && mark.eq(found);
};
/**
* Returns supported block marks at `pos` only when both adjacent siblings
* share the exact same set of those marks. Returns `[]` when they differ,
* so the widget sits outside all mark wrappers and avoids mis-nesting.
*/
var getMatchingBlockMarks = exports.getMatchingBlockMarks = function getMatchingBlockMarks(state, pos, supportedMarkTypes) {
var _nodeAfter$marks, _nodeBefore$marks;
var resolvedPos = state.doc.resolve(pos);
var validMarkTypes = supportedMarkTypes.filter(Boolean);
var supportedMarks = resolvedPos.marks().filter(function (mark) {
return validMarkTypes.includes(mark.type);
});
if (supportedMarks.length === 0) {
return [];
}
var nodeAfter = resolvedPos.nodeAfter,
nodeBefore = resolvedPos.nodeBefore;
var nextMarks = (_nodeAfter$marks = nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.marks) !== null && _nodeAfter$marks !== void 0 ? _nodeAfter$marks : [];
var prevMarks = (_nodeBefore$marks = nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.marks) !== null && _nodeBefore$marks !== void 0 ? _nodeBefore$marks : [];
var nextSupported = nextMarks.filter(function (m) {
return validMarkTypes.includes(m.type);
});
var allMatchNext = supportedMarks.every(function (m) {
return hasMatchingMark(m, nextMarks);
});
// First node — no previous sibling to compare against.
if (!nodeBefore) {
return nextSupported.length === supportedMarks.length && allMatchNext ? supportedMarks : [];
}
// `supportedMarks` already comes from `nodeBefore` (via resolvedPos.marks()).
// Compare counts to guard against extra supported marks on either side.
var prevSupported = prevMarks.filter(function (m) {
return validMarkTypes.includes(m.type);
});
return prevSupported.length === nextSupported.length && allMatchNext ? supportedMarks : [];
};