checkSelection()

in packages/ketcher-react/src/script/editor/tool/sgroup.ts [58:167]


  checkSelection() {
    let selection = this.editor.selection() || {};
    const struct = this.editor.render.ctab;
    const molecule = struct.molecule;
    const filteredAtomsAndBonds = filterNotPartOfSuperatomWithoutLabel(
      { atoms: selection.atoms, bonds: selection.bonds },
      molecule,
    );

    selection = {
      ...selection,
      atoms: filteredAtomsAndBonds.atoms,
      bonds: filteredAtomsAndBonds.bonds,
    };

    selection = this.editor.selection(selection) || {};
    this.editor.rotateController.rerender();
    this.editor.update(true);

    if (selection.atoms && selection.bonds) {
      const selectedAtoms = this.editor.selection()?.atoms;

      const sgroups: Pool<SGroup> = molecule.sgroups;
      const newSelected: { atoms: Array<any>; bonds: Array<any> } = {
        atoms: [],
        bonds: [],
      };
      let actualSgroupId;
      let atomsResult: Array<number> = [];
      let extraAtoms;
      const functionalGroups = molecule.functionalGroups;
      const result: Array<number> = [];

      const id = sgroups.find((_, sgroup) =>
        isEqual(sgroup.atoms, selectedAtoms),
      );

      if (selectedAtoms && functionalGroups.size) {
        for (const atom of selectedAtoms) {
          const atomId = FunctionalGroup.atomsInFunctionalGroup(
            functionalGroups,
            atom,
          );

          if (atomId == null) {
            extraAtoms = true;
          }

          const atomFromStruct = atomId !== null && struct.atoms.get(atomId)?.a;

          if (atomFromStruct) {
            for (const sgId of atomFromStruct.sgs.values()) {
              actualSgroupId = sgId;
            }
          }

          if (
            atomFromStruct &&
            (FunctionalGroup.isAtomInContractedFunctionalGroup(
              atomFromStruct,
              sgroups,
              functionalGroups,
              false,
            ) ||
              SGroup.isAtomInContractedSGroup(atomFromStruct, sgroups))
          ) {
            const sgroupAtoms =
              actualSgroupId !== undefined &&
              SGroup.getAtoms(molecule, sgroups.get(actualSgroupId));
            const sgroupBonds =
              actualSgroupId !== undefined &&
              SGroup.getBonds(molecule, sgroups.get(actualSgroupId));
            atom === sgroupAtoms[0] &&
              newSelected.atoms.push(...(sgroupAtoms as Array<any>)) &&
              newSelected.bonds.push(...(sgroupBonds as Array<any>));
          }

          if (atomFromStruct) {
            atomsResult.push(atomId);
          }
        }
      }

      if (extraAtoms) {
        atomsResult = [];
      }

      if (atomsResult && atomsResult.length > 0) {
        for (const id of atomsResult) {
          const fgId = FunctionalGroup.findFunctionalGroupByAtom(
            functionalGroups,
            id,
          );

          if (fgId !== null && !result.includes(fgId)) {
            result.push(fgId);
          }
        }
      }

      if (result.length) {
        this.editor.selection(null);
        this.editor.event.removeFG.dispatch({ fgIds: result });
        return;
      }

      SGroupTool.sgroupDialog(this.editor, id);
      this.isNotActiveTool = true;
    }
  }