UNPKG

@iktos-oss/molecule-representation

Version:

exports interactif molecule represnetations as react components

218 lines (214 loc) 10.6 kB
"use strict"; /* MIT License Copyright (c) 2023 Iktos Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyUserStyles = exports.buildAtomsHitboxes = exports.buildBondsHitboxes = exports.computeIconsCoords = exports.createSvgElement = void 0; const react_1 = require("react"); const react_from_dom_1 = __importDefault(require("react-from-dom")); const html_1 = require("./html"); const identifiers_1 = require("./identifiers"); const createSvgElement = (svg, svgProps) => { if (!svg) throw new Error('Missing svg'); const element = (0, react_from_dom_1.default)(svg); if (!element) throw new Error('Could not convert to element'); return (0, react_1.cloneElement)(element, Object.assign({}, svgProps)); }; exports.createSvgElement = createSvgElement; const computeIconsCoords = ({ svg, attachedIcons, }) => { var _a, _b, _c, _d; if (!svg) return []; const coords = []; const parentWrapper = document.createElement('div'); parentWrapper.innerHTML = svg; const parentSvg = parentWrapper.getElementsByTagName('svg')[0]; if (!parentSvg) return []; for (const attachedIcon of attachedIcons) { const attachedIconPlacements = []; const svgDimenssions = (0, html_1.getSvgDimensionsWithAppendedElements)(attachedIcon.svg); if (!svgDimenssions) { continue; } const { width: svgWidth, height: svgHeight } = svgDimenssions; const referenceAtoms = Array.from(parentSvg.querySelectorAll('ellipse.atom-0')); const scale = referenceAtoms.length ? Math.min((parseFloat((_a = referenceAtoms[0].getAttribute('rx')) !== null && _a !== void 0 ? _a : '0.5') * 2) / svgWidth, (parseFloat((_b = referenceAtoms[0].getAttribute('ry')) !== null && _b !== void 0 ? _b : '0.5') * 2) / svgHeight) : 1; const processedBondIds = new Set(); for (const atomId of attachedIcon.atomIds) { const matchedElems = Array.from(parentSvg.querySelectorAll(`ellipse.atom-${atomId}`)); for (const matchedElem of matchedElems) { const { x, y } = matchedElem.getBoundingClientRect(); const cx = parseFloat((_c = matchedElem.getAttribute('cx')) !== null && _c !== void 0 ? _c : `${x}`); const cy = parseFloat((_d = matchedElem.getAttribute('cy')) !== null && _d !== void 0 ? _d : `${y}`); attachedIconPlacements.push({ xTranslate: cx, yTranslate: cy, }); } } for (const bondId of attachedIcon.bondIds) { const matchedElems = Array.from(parentSvg.querySelectorAll(`.bond-${bondId}`)); for (const matchedElem of matchedElems) { if (processedBondIds.has(bondId)) { // ignore double bonds continue; } if (matchedElem.id.includes('clickable') || (0, identifiers_1.isHighlightingPath)(matchedElem)) { // ignore clickable & highlighted (they are duplicates of the original bond) continue; } const { start, end } = (0, html_1.getPathEdgePoints)(matchedElem); attachedIconPlacements.push({ xTranslate: (start.x + end.x) / 2, yTranslate: (start.y + end.y) / 2, }); processedBondIds.add(bondId); } } coords.push({ svg: attachedIcon.svg, scale, placements: attachedIconPlacements.map((placement) => (Object.assign(Object.assign({}, placement), { xTranslate: placement.xTranslate - (svgWidth * scale) / 2, yTranslate: placement.yTranslate - (svgHeight * scale) / 2 }))), }); } return coords; }; exports.computeIconsCoords = computeIconsCoords; const buildBondsHitboxes = ({ // this doesn't look into dom, but uses the svg string instead numAtoms, svg, isClickable, }) => { const parentWrapper = document.createElement('div'); parentWrapper.innerHTML = svg; const parentSvg = parentWrapper.getElementsByTagName('svg')[0]; if (!parentSvg) return []; const clickablePaths = []; for (let atomIdx = 0; atomIdx < numAtoms; atomIdx++) { const matchedElems = Array.from(parentSvg.querySelectorAll((0, identifiers_1.getBondSelectorIdentifier)(atomIdx))); for (const elem of matchedElems) { const atomIndicesInBond = (0, identifiers_1.getAtomIdsFromClassnames)(elem.classList); const bondIndicies = (0, identifiers_1.getBondIdFromClassnames)(elem.classList); if (bondIndicies.length !== 1 || atomIndicesInBond.length !== 2) { console.error('[@iktos-oss/molecule-representation] invalid bond classname', bondIndicies, elem.classList); continue; } if ((0, identifiers_1.isHighlightingPath)(elem)) { continue; } const hitboxPath = (0, html_1.createHitboxPathFromPath)({ path: elem, id: (0, identifiers_1.getClickableBondId)({ bondId: bondIndicies[0], startAtomId: atomIndicesInBond[0], endAtomId: atomIndicesInBond[1], }), isClickable, }); clickablePaths.push(hitboxPath); } } return clickablePaths; }; exports.buildBondsHitboxes = buildBondsHitboxes; const buildAtomsHitboxes = ({ // this doesn't look into dom, but uses the svg string instead numAtoms, svg, clickableAtoms, isClickable, }) => { const parentWrapper = document.createElement('div'); parentWrapper.innerHTML = svg; const parentSvg = parentWrapper.getElementsByTagName('svg')[0]; if (!parentSvg) return []; const clickablePaths = []; for (let atomIdx = 0; atomIdx < numAtoms; atomIdx++) { if (clickableAtoms && !clickableAtoms.has(atomIdx)) { // ignore non clickableAtoms if clickableAtoms is specified continue; } const matchedElems = Array.from(parentSvg.querySelectorAll((0, identifiers_1.getAtomHighliteEllipseIdentifier)(atomIdx))); for (const elem of matchedElems) { const atomIndicesInBond = (0, identifiers_1.getAtomIdsFromClassnames)(elem.classList); if (atomIndicesInBond.length !== 1) { console.warn('Found an ellipse with more than one atomid'); continue; } const hitboxPath = (0, html_1.createHitboxFromAtomEllipse)({ ellipse: elem, id: (0, identifiers_1.getClickableAtomIdFromAtomIdx)(atomIdx), isClickable, }); clickablePaths.push(hitboxPath); } } return clickablePaths; }; exports.buildAtomsHitboxes = buildAtomsHitboxes; const applyUserStyles = ({ svg, numAtoms, atomsStyles, bondsStyles, }) => { if (!atomsStyles && !bondsStyles) return svg; const parentWrapper = document.createElement('div'); parentWrapper.innerHTML = svg; const parentSvg = parentWrapper.getElementsByTagName('svg')[0]; if (!parentSvg) return svg; const _a = atomsStyles || {}, { default: defaultAtomsStyles } = _a, specificAtomsStyles = __rest(_a, ["default"]); const _b = bondsStyles || {}, { default: defaultBondsStyles } = _b, specificBondsStyles = __rest(_b, ["default"]); for (let atomIdx = 0; atomIdx < numAtoms; atomIdx++) { const matchedAtomsElems = Array.from(parentSvg.querySelectorAll((0, identifiers_1.getAtomSelectorIdentifier)(atomIdx))); const matchedBondsElems = Array.from(parentSvg.querySelectorAll((0, identifiers_1.getBondSelectorIdentifier)(atomIdx))); for (const matchedElem of matchedAtomsElems) { const stylesToApply = specificAtomsStyles[atomIdx] || defaultAtomsStyles || {}; Object.assign(matchedElem.style, stylesToApply); } for (const matchedElem of matchedBondsElems) { const [bondId] = (0, identifiers_1.getBondIdFromClassnames)(matchedElem.classList); const [startAtomId, endAtomId] = Array.from(matchedElem.classList) .filter((c) => c.includes('atom')) .map((c) => parseInt(c.replace('atom-', ''))); const stylesToApply = specificBondsStyles[bondId] || specificBondsStyles[`${startAtomId}-${endAtomId}`] || specificBondsStyles[`${startAtomId}-*`] || specificBondsStyles[`*-${endAtomId}`] || defaultBondsStyles || {}; Object.assign(matchedElem.style, stylesToApply); } } return parentWrapper.innerHTML; }; exports.applyUserStyles = applyUserStyles; //# sourceMappingURL=svg-computation.js.map