click()

in packages/ketcher-react/src/script/editor/tool/attach.ts [55:134]


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

    if (ci && functionalGroups.size && ci.map === 'atoms') {
      const atomId = FunctionalGroup.atomsInFunctionalGroup(
        functionalGroups,
        ci.id,
      );

      if (atomId !== null) {
        atomResult.push(atomId);
      }
    }

    if (ci && functionalGroups.size && ci.map === 'bonds') {
      const bondId = FunctionalGroup.bondsInFunctionalGroup(
        molecule,
        functionalGroups,
        ci.id,
      );

      if (bondId !== null) {
        bondResult.push(bondId);
      }
    }

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

        if (fgId !== null && !result.includes(fgId)) {
          result.push(fgId);
        }
      }
      this.editor.event.removeFG.dispatch({ fgIds: result });
      return;
    } else if (bondResult.length > 0) {
      for (const id of bondResult) {
        const fgId = FunctionalGroup.findFunctionalGroupByBond(
          molecule,
          functionalGroups,
          id,
        );

        if (fgId !== null && !result.includes(fgId)) {
          result.push(fgId);
        }
      }
      this.editor.event.removeFG.dispatch({ fgIds: result });
      return;
    }

    if (
      ci &&
      ((ci.map === 'atoms' &&
        Elements.get(molecule.atoms.get(ci.id)?.label as string)) ||
        ci.map === 'bonds')
    ) {
      if (ci.map === 'atoms') {
        this.attach.atomid = ci.id;
      } else this.attach.bondid = ci.id;

      this.editor.selection({
        atoms: [this.attach.atomid],
        bonds: [this.attach.bondid],
      });
      this.editor.event.attachEdit.dispatch(this.attach);
    }
    return true;
  }