click()

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


  click(event) {
    const rnd = this.editor.render;
    const restruct = rnd.ctab;
    const sgroups = restruct.sgroups;
    const molecule = restruct.molecule;
    const functionalGroups = molecule.functionalGroups;
    const ci = this.editor.findItem(event, this.maps);
    const atomResult: Array<number> = [];
    const bondResult: Array<number> = [];
    const result: Array<number> = [];

    if (
      ci &&
      functionalGroups &&
      ci.map === 'functionalGroups' &&
      !FunctionalGroup.isContractedFunctionalGroup(ci.id, functionalGroups)
    ) {
      const sGroup = sgroups.get(ci.id);

      if (FunctionalGroup.isFunctionalGroup(sGroup?.item)) {
        result.push(ci.id);
      }
    } else if (ci && functionalGroups && ci.map === 'atoms') {
      const atomId = FunctionalGroup.atomsInFunctionalGroup(
        functionalGroups,
        ci.id,
      );
      const atomFromStruct = atomId !== null && restruct.atoms.get(atomId)?.a;

      if (
        atomFromStruct &&
        !FunctionalGroup.isAtomInContractedFunctionalGroup(
          atomFromStruct,
          sgroups,
          functionalGroups,
          true,
        )
      ) {
        atomResult.push(atomId);
      }
    } else if (ci && functionalGroups && ci.map === 'bonds') {
      const bondId = FunctionalGroup.bondsInFunctionalGroup(
        molecule,
        functionalGroups,
        ci.id,
      );
      const bondFromStruct = bondId !== null && restruct.bonds.get(bondId)?.b;

      if (
        bondFromStruct &&
        !FunctionalGroup.isBondInContractedFunctionalGroup(
          bondFromStruct,
          sgroups,
          functionalGroups,
        )
      ) {
        bondResult.push(bondId);
      }
    } else if (functionalGroups && ci?.map === 'rgroupAttachmentPoints') {
      const attachedAtomId =
        FunctionalGroup.isRGroupAttachmentPointInsideFunctionalGroup(
          molecule,
          ci.id,
        );
      if (attachedAtomId !== null) {
        atomResult.push(attachedAtomId);
      }
    }

    if (atomResult.length) {
      for (const id of atomResult) {
        const fgId = FunctionalGroup.findFunctionalGroupByAtom(
          functionalGroups,
          id,
        );

        if (fgId !== null && !result.includes(fgId)) {
          result.push(fgId);
        }
      }
    } else if (bondResult.length) {
      for (const id of bondResult) {
        const fgId = FunctionalGroup.findFunctionalGroupByBond(
          molecule,
          functionalGroups,
          id,
        );

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

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

    if (!ci) return; // ci.type == 'Canvas'

    this.editor.hover(null);

    if (ci.map === 'atoms') {
      this.editor.update(fromOneAtomDeletion(restruct, ci.id));
    } else if (ci.map === 'bonds') {
      this.editor.update(fromOneBondDeletion(restruct, ci.id));
    } else if (
      (ci.map === 'sgroups' || ci.map === 'functionalGroups') &&
      FunctionalGroup.isContractedFunctionalGroup(ci.id, functionalGroups)
    ) {
      const sGroup = sgroups.get(ci.id);

      this.editor.update(
        fromFragmentDeletion(rnd.ctab, {
          atoms: [...SGroup.getAtoms(molecule, sGroup?.item)],
          bonds: [...SGroup.getBonds(molecule, sGroup?.item)],
        }),
      );
    } else if (ci.map === 'sgroups' || ci.map === 'sgroupData') {
      const sGroup = sgroups.get(ci.id);

      if (
        FunctionalGroup.isFunctionalGroup(sGroup?.item) ||
        sGroup?.item instanceof MonomerMicromolecule
      ) {
        this.editor.event.removeFG.dispatch({ fgIds: [ci.id] });
      } else {
        this.editor.update(fromSgroupDeletion(restruct, ci.id));
      }
    } else if (ci.map === 'rxnArrows') {
      this.editor.update(fromArrowDeletion(restruct, ci.id));
    } else if (ci.map === 'rxnPluses') {
      this.editor.update(fromPlusDeletion(restruct, ci.id));
    } else if (ci.map === 'simpleObjects') {
      this.editor.update(fromSimpleObjectDeletion(restruct, ci.id));
    } else if (ci.map === 'texts') {
      this.editor.update(fromTextDeletion(restruct, ci.id));
    } else if (ci.map === 'rgroupAttachmentPoints') {
      this.editor.update(fromRGroupAttachmentPointDeletion(restruct, ci.id));
    } else if (ci.map === IMAGE_KEY) {
      this.editor.update(fromImageDeletion(restruct, ci.id));
    } else if (ci.map === MULTITAIL_ARROW_KEY) {
      this.editor.update(fromMultitailArrowDeletion(restruct, ci.id));
    } else {
      // TODO re-factoring needed - should be "map-independent"
      console.error(
        'EraserTool: unable to delete the object ' + ci.map + '[' + ci.id + ']',
      );
      return;
    }
    this.editor.selection(null);
  }