@atlaskit/editor-plugin-local-id
Version:
LocalId plugin for @atlaskit/editor-core
54 lines (53 loc) • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findNodeByLocalId = findNodeByLocalId;
exports.replaceNode = exports.getNode = void 0;
function findNodeByLocalId(tr, localId) {
var result;
tr.doc.descendants(function (node, pos) {
if (result) {
return false;
}
if (node.attrs.localId === localId) {
result = {
node: node,
pos: pos
};
}
return result === undefined;
});
return result;
}
var replaceNode = exports.replaceNode = function replaceNode(api) {
return function (_ref) {
var _api$core$actions$exe;
var localId = _ref.localId,
value = _ref.value;
var nodeWithPos = getNode(api)({
localId: localId
});
if (!nodeWithPos) {
return false;
}
var pos = nodeWithPos.pos,
node = nodeWithPos.node;
return (_api$core$actions$exe = api === null || api === void 0 ? void 0 : api.core.actions.execute(function (_ref2) {
var tr = _ref2.tr;
return tr.replaceWith(pos, pos + node.nodeSize, value).scrollIntoView();
})) !== null && _api$core$actions$exe !== void 0 ? _api$core$actions$exe : false;
};
};
var getNode = exports.getNode = function getNode(api) {
return function (_ref3) {
var localId = _ref3.localId;
var result;
api === null || api === void 0 || api.core.actions.execute(function (_ref4) {
var tr = _ref4.tr;
result = findNodeByLocalId(tr, localId);
return null;
});
return result;
};
};