Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 3 additions & 47 deletions src/NMRiumWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NMRiumChangeCb, NMRiumRefAPI, NMRiumState } from 'nmrium';
import type { NMRiumChangeCb, NMRiumRefAPI } from 'nmrium';
import { NMRium } from 'nmrium';
import type { CSSProperties } from 'react';
import { useCallback, useEffect, useRef } from 'react';
Expand All @@ -7,9 +7,6 @@ import { RootLayout } from 'react-science/ui';
import { LoadingIndicator } from './Loadingindicator.js';
import { loadSpectraFromSource } from './data-source/loadSpectraFromSource.js';
import events from './events/event.js';
import type { HighlightPeakParams } from './events/types.js';
import { applyPeakHighlight } from './highlight/highlightCommands.js';
import { resolveHighlightPeak } from './highlight/resolvePeak.js';
import { useLoadSpectra } from './hooks/useLoadSpectra.js';
import { usePreferences } from './hooks/usePreferences.js';
import { useWhiteList } from './hooks/useWhiteList.js';
Expand All @@ -21,20 +18,9 @@ const containerStyle: CSSProperties = {
position: 'relative',
};

const ACCEPTED_ACTION_TYPES =
"'exportSpectraViewerAsBlob', 'selectTab', 'highlightPeak', or 'clearHighlight'";

function reportActionError(message: string) {
const error = new Error(message);
events.trigger('error', error);
// eslint-disable-next-line no-console
console.error(error);
}

export default function NMRiumWrapper() {
const { allowedOrigins, isFetchAllowedOriginsPending } = useWhiteList();
const nmriumRef = useRef<NMRiumRefAPI>(null);
const latestStateRef = useRef<NMRiumState | null>(null);
const {
workspace,
preferences,
Expand All @@ -50,31 +36,9 @@ export default function NMRiumWrapper() {
if (source === 'view' && state.data.actionType === 'SET_2D_LEVEL') {
return;
}
latestStateRef.current = state;
events.trigger('data-change', { state, source });
}, []);

const handleHighlightPeak = useCallback(
(params: HighlightPeakParams) => {
const resolved = resolveHighlightPeak(latestStateRef.current, params);
if (!resolved.ok) {
reportActionError(resolved.message);
return;
}

const nucleus = params.nucleus?.trim();
if (nucleus) {
setActiveTab({ tab: nucleus.toUpperCase() });
}

applyPeakHighlight(
resolved.ids,
params.permanent === false ? 'show' : 'permanent',
);
},
[setActiveTab],
);

useEffect(() => {
if (!spectraSource) return;

Expand Down Expand Up @@ -114,17 +78,9 @@ export default function NMRiumWrapper() {
setActiveTab({ tab: tab.toUpperCase() });
break;
}
case 'highlightPeak': {
handleHighlightPeak(request.params);
break;
}
case 'clearHighlight': {
applyPeakHighlight([], 'clear');
break;
}
default: {
reportActionError(
`ERROR! Property 'type' accepts only ${ACCEPTED_ACTION_TYPES}.`,
throw new Error(
`ERROR! Property 'type' accepts only 'exportViewerAsBlob'.`,
);
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/demo/NMRiumWrapperDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,6 @@ export default function NMRiumWrapperDemo() {
>
Test Logger
</Button>
<Button
className="highlight-peak-btn"
style={{ marginRight: '10px' }}
onClick={() => {
events.trigger('action-request', {
type: 'highlightPeak',
params: {
nucleus: '13C',
ppm: 77.95,
tolerance: 0.05,
},
});
}}
>
Test highlight peak
</Button>
<Button
className="clear-highlight-btn"
onClick={() => {
events.trigger('action-request', {
type: 'clearHighlight',
});
}}
>
Clear highlight
</Button>
</Header>

<NMRiumWrapper />
Expand Down
25 changes: 1 addition & 24 deletions src/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,13 @@ type LoadData =
activeTab?: string;
};

interface HighlightPeakParams {
/** Peak id from `spectrum.peaks.values[].id`. */
id?: string;
/** Nucleus tab to activate before highlighting, for example `1H` or `13C`. */
nucleus?: string;
/** Chemical shift in ppm. Used when `id` is omitted. */
ppm?: number;
/** Maximum distance in ppm when matching by `nucleus` and `ppm`. Defaults to 0.05. */
tolerance?: number;
/**
* `true` keeps the highlight until the next highlight or `clearHighlight`.
* `false` applies a temporary highlight. Defaults to `true`.
*/
permanent?: boolean;
}

type ActionRequest =
| {
type: 'exportSpectraViewerAsBlob';
}
| {
type: 'selectTab';
params: { tab: string };
}
| {
type: 'highlightPeak';
params: HighlightPeakParams;
}
| {
type: 'clearHighlight';
};

interface ActionResponse {
Expand All @@ -81,4 +58,4 @@ type EventData<T extends EventType> = T extends 'data-change'
: T extends 'error'
? Error
: never;
export type { EventData, EventType, HighlightPeakParams };
export type { EventData, EventType };
69 changes: 0 additions & 69 deletions src/highlight/highlightBridge.tsx

This file was deleted.

30 changes: 0 additions & 30 deletions src/highlight/highlightCommands.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/highlight/nmrium-highlight-original.d.ts

This file was deleted.

Loading
Loading