# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Purpose

Wraps the ChemAxon **MarvinJS** molecule sketcher so Datagrok users can pick it alongside other sketchers (OpenChemLib, Ketcher) wherever `grok.chem.Sketcher` is used. The package registers a `moleculeSketcher` function whose widget hosts MarvinJS inside an iframe and bridges structure formats (MOL V2000, MOL V3000, SMILES, SMARTS) between the sketcher and Datagrok's `DG.chem` notations.

Auth / credentials: MarvinJS itself is a licensed product. The license XML URL and optional ChemAxon web-service endpoints are configured through package properties (see `Conventions` below) — the package does no built-in credential handling.

## Architecture

Everything meaningful is in `src/` (3 files). The `marvin/` folder holds vendored ChemAxon assets shipped as `sources` in `package.json` — **do not** edit them; they are loaded into the iframe via `marvin/editor.html`.

- **`src/package.ts`** — the whole package. Registers `initMarvin` (`//tags: init`) and `marvinSketcher` (`//tags: moleculeSketcher`, returns `widget sketcher`). Defines `class MarvinSketcher extends grok.chem.SketcherBase`, which creates the iframe, wires the MarvinJS `molchange` event, caches the four structure representations (`_molV2000`, `_molV3000`, `_smiles`, `_smarts`), and implements the getters/setters the base class requires. A module-level `marvinUploaded` RxJS `Subject<string>` is pushed from `window.refreshMarvin(sketchId)` (called by `marvin/js/marvinjslauncher.js` after the iframe finishes loading) and subscribed per-instance inside `init()` so a re-parent can redraw.
- **`src/constants.ts`** — the exact format-name strings MarvinJS expects in `exportStructure` / `importStructure` (`'smiles'`, `'mol'`, `'mol:V3'`, `'smarts'`). Use these; do not inline the strings.
- **`src/package-test.ts`** + **`src/tests/test-examples.ts`** — `grok test` harness. Tests run with `npm test`.

### The four-representation cache and MolConvertPath fallback

The `molchange` handler and every setter maintain an invariant: after any mutation, **exactly one** of `_molV2000` / `_molV3000` / `_smiles` / `_smarts` is authoritative and the others are either freshly recomputed or set to `null` so the next getter derives them lazily.

Conversions between representations go through one of two paths depending on whether `_properties.MolConvertPath` is set:

- **Empty string** → local conversion via `DG.chem.convert(...)`. Fast, no network, but limited (query features, some Markush constructs won't round-trip).
- **Non-empty** → remote ChemAxon `molconvertws` endpoint, reached by calling `sketcher.exportStructure(FORMAT)` / `sketcher.importStructure(FORMAT, value)`. Wrapped in `promiseWithTimeout` (3 s) and falls back to `DG.chem.convert` on timeout. The timeout also aborts early if the sketcher dialog was closed mid-flight (`sketcherDialogOpened()` + `SKETCHER_MODE.EXTERNAL`).

When adding a new format or a new operation, follow the same branch-on-`MolConvertPath` shape and add a `DG.chem.convert` fallback.

### Property → service mapping

`SERVICES_PROPS_MAPPING` (top of `package.ts`) maps package-property names (`MolConvertPath`, `Clean2dPath`, …) to the MarvinJS service keys (`molconvertws`, `clean2dws`, …) passed to `sketcher.setServices(...)` in `enableServices()`. To expose a new ChemAxon web service: add a property to `package.json` `properties[]`, add the matching entry to this map.

## Glossary — domain concepts → code

| Concept | Code type(s) | One-line meaning | Key relationships |
|---|---|---|---|
| MarvinJS | external JS library, loaded in iframe from `marvin/editor.html` | ChemAxon's in-browser molecule editor | Accessed through the global `MarvinJSUtil.getEditor(sketchId)` that `marvinjslauncher.js` installs on `window` |
| Sketcher dialog / inplace mode | `DG.chem.SKETCHER_MODE.EXTERNAL` vs `INPLACE` | Whether the sketcher is in a floating dialog or embedded directly in a property panel | Controls whether `exportStructure` is awaited or skipped (the external dialog can be closed mid-export, which is what `promiseWithTimeout` watches for) |
| molV2000 / molV3000 | `MARVIN_MOLV2000 = 'mol'`, `MARVIN_MOLV3000 = 'mol:V3'` | Two MDL MOL-file variants; V3000 handles larger/complex structures V2000 can't | `DG.chem.Notation.MolBlock` ↔ V2000; `DG.chem.Notation.V3KMolBlock` ↔ V3000 |
| SMARTS vs SMILES | `MARVIN_SMARTS = 'smarts'`, `MARVIN_SMILES = 'smiles'` | SMILES is a compact structure string; SMARTS adds query features (atom lists, bond queries) | `smiles` → `smarts` conversion warns (`DG.chem.smilesFromSmartsWarning()`) because info is lost |
| `WHITE_MOLBLOCK` | `DG.WHITE_MOLBLOCK` | Empty-molecule MOL V2000 placeholder | Used when `molFile` is set to `''` to keep MarvinJS from erroring on an empty import |

## Conventions specific to this package

- **Never edit `marvin/`** — vendored ChemAxon assets, shipped to clients verbatim via `package.json` `sources`. Version bumps come from ChemAxon drops, not hand edits.
- **Use the `MARVIN_*` constants from `constants.ts`** for every `exportStructure` / `importStructure` call. The strings are the MarvinJS contract; typos there fail silently at runtime.
- **`_properties` is populated in `initMarvin`, not the constructor.** Any new code that reads package properties must run after `initMarvin()` has awaited `_package.getProperties()`, or guard against `undefined`.
- **Log through `_package.logger.debug`** (not `console.log`). The existing code is verbose on purpose — MarvinJS failures usually only reproduce against a licensed server, so the debug trail is the primary diagnostic.
- **New ChemAxon web service?** Add a property in `package.json` `properties[]` + an entry in `SERVICES_PROPS_MAPPING` in `package.ts`. The `enableServices()` loop picks it up automatically.
- **Don't call `fetch` for ChemAxon endpoints.** The sketcher talks to its services directly once `setServices` is configured; there is no Datagrok-side HTTP in this package today. If you ever need one, route through `grok.dapi.fetchProxy` per the repo-level rule.
