mousedown()

in packages/ketcher-react/src/script/editor/tool/chain.ts [45:148]


  mousedown(event) {
    if (this.dragCtx) return;
    if (isBondingWithMacroMolecule(this.editor, event)) {
      return;
    }
    const struct = this.editor.render.ctab;
    const molecule = struct.molecule;
    const functionalGroups = molecule.functionalGroups;
    const rnd = this.editor.render;
    const ci = this.editor.findItem(event, [
      'atoms',
      'bonds',
      'functionalGroups',
    ]);
    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);
        }
      }
      if (result.length) {
        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);
        }
      }
      if (result.length) {
        this.editor.event.removeFG.dispatch({ fgIds: result });
        return;
      }
    }

    this.editor.hover(null);
    this.dragCtx = {
      xy0: rnd.page2obj(event),
      item: ci,
    };

    if (ci && ci.map === 'atoms') {
      this.editor.selection({ atoms: [ci.id] }); // for change atom
      // this event has to be stopped in others events by `tool.dragCtx.stopTapping()`
      atomLongtapEvent(this, rnd);
    }

    if (ci?.map === 'functionalGroups') {
      const functionalGroup = molecule.functionalGroups.get(ci.id);
      if (!SGroup.isSaltOrSolvent(functionalGroup?.name || '')) {
        const sGroupId = ci.id;
        const sGroup = molecule.sgroups.get(sGroupId);
        const attachmentAtomId = sGroup?.getAttachmentAtomId();
        this.dragCtx.item = {
          map: 'atoms',
          id: attachmentAtomId,
        };
      }
    }

    if (!this.dragCtx.item)
      // ci.type == 'Canvas'
      delete this.dragCtx.item;
    return true;
  }