mouseup()

in packages/ketcher-react/src/script/editor/tool/eraser.ts [93:239]


  mouseup() {
    const struct = this.editor.render.ctab;
    const sgroups = struct.sgroups;
    const molecule = struct.molecule;
    const functionalGroups = molecule.functionalGroups;
    const selected = this.editor.selection();
    const newSelected: Record<string, any> = { atoms: [], bonds: [] };
    let actualSgroupId;
    const atomsResult: Array<number> = [];
    const bondsResult: Array<number> = [];
    const preResult: Array<number> = [];

    if (selected && functionalGroups.size && selected.atoms) {
      for (const atom of selected.atoms) {
        const atomId = FunctionalGroup.atomsInFunctionalGroup(
          functionalGroups,
          atom,
        );
        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,
            true,
          )
        ) {
          const sgroupAtoms =
            actualSgroupId !== undefined &&
            SGroup.getAtoms(molecule, struct.sgroups.get(actualSgroupId)?.item);
          const sgroupBonds =
            actualSgroupId !== undefined &&
            SGroup.getBonds(molecule, struct.sgroups.get(actualSgroupId)?.item);
          atom === sgroupAtoms[0] &&
            newSelected.atoms.push(...(sgroupAtoms as Array<any>)) &&
            newSelected.bonds.push(...(sgroupBonds as Array<any>));
        }

        if (
          atomFromStruct &&
          !FunctionalGroup.isAtomInContractedFunctionalGroup(
            atomFromStruct,
            sgroups,
            functionalGroups,
            true,
          )
        ) {
          atomsResult.push(atomId);
        }
      }
    }

    if (selected && functionalGroups.size && selected.bonds) {
      for (const bond of selected.bonds) {
        const bondId = FunctionalGroup.bondsInFunctionalGroup(
          molecule,
          functionalGroups,
          bond,
        );
        const bondFromStruct = bondId !== null && struct.bonds.get(bondId)?.b;

        if (
          bondFromStruct &&
          !FunctionalGroup.isBondInContractedFunctionalGroup(
            bondFromStruct,
            sgroups,
            functionalGroups,
          )
        ) {
          bondsResult.push(bondId);
        }
      }
    }

    if (selected && functionalGroups.size && selected.rgroupAttachmentPoints) {
      for (const rgroupAttachmentPointId of selected.rgroupAttachmentPoints) {
        const attachedAtomId =
          FunctionalGroup.isRGroupAttachmentPointInsideFunctionalGroup(
            molecule,
            rgroupAttachmentPointId,
          );
        if (attachedAtomId !== null) {
          atomsResult.push(attachedAtomId);
        }
      }
    }

    if (atomsResult.length > 0) {
      for (const id of atomsResult) {
        const fgId = FunctionalGroup.findFunctionalGroupByAtom(
          functionalGroups,
          id,
        );
        fgId !== null && !preResult.includes(fgId) && preResult.push(fgId);
      }
    }

    if (bondsResult.length > 0) {
      for (const id of bondsResult) {
        const fgId = FunctionalGroup.findFunctionalGroupByBond(
          molecule,
          functionalGroups,
          id,
        );
        fgId !== null && !preResult.includes(fgId) && preResult.push(fgId);
      }
    }

    if (preResult.length > 0) {
      const result: Array<number> = [];
      preResult.forEach((fgId) => {
        const sgAtoms = sgroups.get(fgId)?.item?.atoms;
        sgAtoms.forEach((atom) => {
          !atomsResult.includes(atom) &&
            !result.includes(fgId) &&
            result.push(fgId);
        });
      });

      if (result.length > 0) {
        this.editor.selection(null);
        this.editor.event.removeFG.dispatch({ fgIds: result });
        this.lassoHelper.cancel();
      }
    }

    // eslint-disable-line max-statements
    const rnd = this.editor.render;

    if (this.lassoHelper.running()) {
      // TODO it catches more events than needed, to be re-factored
      const sel =
        newSelected.atoms.length > 0
          ? selMerge(this.lassoHelper.end(), newSelected, false)
          : this.lassoHelper.end();
      this.editor.update(fromFragmentDeletion(rnd.ctab, sel));
      this.editor.selection(null);
    }
  }