UNPKG

@dialpad/dialtone

Version:

Dialpad's Dialtone design system monorepo

88 lines (87 loc) 2.71 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); function lastActiveNodes(state, typesOrGroup) { if (!state) return []; const { from, to } = state.selection; let types; if (typeof typesOrGroup === "string") { types = Object.entries(state.schema.nodes).filter(([name, nodeType]) => nodeType.groups.includes(typesOrGroup)).map(([name, nodeType]) => { return { type: nodeType }; }); } else { types = typesOrGroup; for (const item of types) { item.type = item.type ? getNodeType(item.type, state.schema) : null; } } let lastNode = null; let lastMatchedType = null; const matchedTypes = /* @__PURE__ */ new Set(); const notFoundTypes = new Set(types); state.doc.nodesBetween(from, to, (node, pos, parent) => { if (notFoundTypes.size === 0) return false; if (!node.isText) { const matchedType = types.filter((item) => { if (!item.type) { return true; } if (typeof item.type === "string") return false; return node.type.name === item.type.name; }).find((item) => { if (!item.attributes) return true; return objectIncludes(node.attrs, item.attributes); }); if (matchedType) { if (lastMatchedType && lastNode && lastNode !== parent) { notFoundTypes.delete(lastMatchedType); matchedTypes.add(lastMatchedType); } lastMatchedType = matchedType; } lastNode = node; } }); if (lastMatchedType) { matchedTypes.add(lastMatchedType); } return [...matchedTypes.values()].map((item) => { var _a; if (item.key) { return item.key; } else if (typeof item.type === "string") { return item.type; } else if ((_a = item.type) == null ? void 0 : _a.name) { return item.type.name; } else { return ""; } }); } function getNodeType(nameOrType, schema) { if (typeof nameOrType === "string") { if (!schema.nodes[nameOrType]) { throw Error( `There is no node type named '${nameOrType}'. Maybe you forgot to add the extension?` ); } return schema.nodes[nameOrType]; } return nameOrType; } function objectIncludes(object1, object2, options) { const keys = Object.keys(object2); if (!keys.length) { return true; } return keys.every((key) => { if (Object.prototype.toString.call(object2[key]) === "[object RegExp]") { return object2[key].test(object1[key]); } return object2[key] === object1[key]; }); } exports.default = lastActiveNodes; exports.objectIncludes = objectIncludes; //# sourceMappingURL=last_active_nodes.cjs.map