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
58 changes: 29 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
"@emotion/styled": "^11.14.1",
"@hookform/resolvers": "^5.2.2",
"@tanstack/react-form": "^1.27.7",
"@zakodium/nmr-types": "^0.5.0",
"@zakodium/nmrium-core": "^0.6.0",
"@zakodium/nmrium-core-plugins": "^0.6.29",
"@zakodium/nmr-types": "^0.5.1",
"@zakodium/nmrium-core": "^0.6.1",
"@zakodium/nmrium-core-plugins": "^0.6.33",
"@zakodium/pdnd-esm": "^1.0.2",
"@zip.js/zip.js": "^2.8.15",
"cheminfo-font": "^1.13.1",
Expand Down Expand Up @@ -113,7 +113,7 @@
"react-ocl-nmr": "^4.1.1",
"react-plot": "^3.1.2",
"react-rnd": "^10.5.2",
"react-science": "^19.5.1",
"react-science": "^19.7.1",
"react-table": "^7.8.0",
"smart-array-filter": "^5.0.0",
"yup": "^1.7.1",
Expand Down
104 changes: 54 additions & 50 deletions src/component/1d/FloatPublicationString.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import styled from '@emotion/styled';
import type { BoundingBox } from '@zakodium/nmrium-core';
import { rangesToACS } from 'nmr-processing';
import { useEffect, useState } from 'react';
import type { BoundingBox, TextStyle } from '@zakodium/nmrium-core';
import { useEffect, useMemo, useState } from 'react';
import { BsArrowsMove } from 'react-icons/bs';
import { FaTimes } from 'react-icons/fa';
import { Rnd } from 'react-rnd';
import { SVGStyledText } from 'react-science/ui';

import { isSpectrum1D } from '../../data/data1d/Spectrum1D/isSpectrum1D.js';
import { isSpectrum1D } from '../../data/data1d/Spectrum1D/index.ts';
import { useChartData } from '../context/ChartContext.js';
import { useDispatch } from '../context/DispatchContext.js';
import { useGlobal } from '../context/GlobalContext.js';
import type { ActionsButtonsPopoverProps } from '../elements/ActionsButtonsPopover.js';
import { ActionsButtonsPopover } from '../elements/ActionsButtonsPopover.js';
import { useActiveNucleusTab } from '../hooks/useActiveNucleusTab.ts';
import { usePanelPreferences } from '../hooks/usePanelPreferences.js';
import { useSVGUnitConverter } from '../hooks/useSVGUnitConverter.js';
import useSpectraByActiveNucleus from '../hooks/useSpectraPerNucleus.js';
import { useTextMetrics } from '../hooks/useTextMetrics.ts';
import { useCheckExportStatus } from '../hooks/useViewportSize.js';
import { useACSSettings } from '../hooks/use_acs_settings.ts';
import { usePublicationStrings } from '../hooks/use_publication_strings.ts';

const ReactRnd = styled(Rnd)`
border: 1px solid transparent;
Expand All @@ -36,11 +35,17 @@
text: string;
width: number;
fontSize: number;
fontStyle: string | undefined;
fontWeight: string | undefined;
}

function useWrapSVGText(params: UseWrapSVGTextParams) {
const { text, width, fontSize } = params;
const { getTextWidth } = useTextMetrics(fontSize);
const { text, width, fontSize, fontStyle, fontWeight } = params;
const { getTextWidth } = useTextMetrics({
labelSize: fontSize,
labelStyle: fontStyle,
labelWeight: fontWeight,
});

const formattedText = text
.replaceAll(/<sup>(?<n>.*?)<\/sup>/g, '++$1++ ')
Expand Down Expand Up @@ -73,28 +78,36 @@

interface PublicationTextProps {
text: string;
textStyle: TextStyle;
fontSize?: number;
width: number;
padding?: number;
}

function PublicationText(props: PublicationTextProps) {
const { fontSize = 12, padding = 10, width, text } = props;
const { text, width } = props;
const textStyle = {
...props.textStyle,
fontSize: props.textStyle.fontSize ?? props.fontSize ?? 12,
};
const { fontSize = textStyle.fontSize, padding = 10 } = props;
const boxWidth = width - padding * 2;

const { lineHeight, lines } = useWrapSVGText({
width: boxWidth,
fontSize,
fontStyle: textStyle.fontStyle,
fontWeight: textStyle.fontWeight,
text,
});

return (
<g transform={`translate(${padding} ${padding})`}>
{lines.map((line, lineIndex) => (
<text
<SVGStyledText
// eslint-disable-next-line react/no-array-index-key
key={lineIndex}
fontSize={fontSize}
{...textStyle}
fontFamily="Arial"
y={lineIndex * lineHeight}
dominantBaseline="hanging"
Expand All @@ -119,7 +132,7 @@
return <tspan key={wordIndex}>{word} </tspan>;
}
})}
</text>
</SVGStyledText>
))}
</g>
);
Expand All @@ -129,19 +142,21 @@
value: string;
bonding: BoundingBox;
spectrumKey: string;
nucleus: string | undefined;
}

function DraggablePublicationString(props: DraggablePublicationStringProps) {
const { value, bonding: externalBounding, spectrumKey } = props;
const { value, bonding: externalBounding, spectrumKey, nucleus } = props;
const dispatch = useDispatch();
const { viewerRef } = useGlobal();
const [bounding, setBounding] = useState<BoundingBox>(externalBounding);
const [isMoveActive, setIsMoveActive] = useState(false);
const { percentToPixel, pixelToPercent } = useSVGUnitConverter();
const isExportProcessStart = useCheckExportStatus();
const acsOptions = useACSSettings(nucleus);

useEffect(() => {
setBounding({ ...externalBounding });

Check warning on line 159 in src/component/1d/FloatPublicationString.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Avoid storing derived state. Compute "bounding" directly during render, optionally with `useMemo` if it's expensive
}, [externalBounding]);

function handleResize(
Expand Down Expand Up @@ -254,7 +269,11 @@
if (isExportProcessStart) {
return (
<g transform={`translate(${x} ${y})`}>
<PublicationText text={value} width={width} />
<PublicationText
text={value}
width={width}
textStyle={acsOptions.textStyle}
/>
</g>
);
}
Expand Down Expand Up @@ -298,51 +317,35 @@
y={y}
>
<svg width={width} height={'auto'} xmlns="http://www.w3.org/2000/svg">
<PublicationText text={value} width={width} />
<PublicationText
text={value}
width={width}
textStyle={acsOptions.textStyle}
/>
</svg>
</ActionsButtonsPopover>
</ReactRnd>
);
}

function usePublicationString() {
const spectra = useSpectraByActiveNucleus();
const activeTab = useActiveNucleusTab();
const rangesPreferences = usePanelPreferences('ranges', activeTab);

const output: Record<string, string> = {};

for (const spectrum of spectra) {
if (!isSpectrum1D(spectrum)) {
continue;
}
const { id: spectrumKey, info, ranges } = spectrum;

if (!Array.isArray(ranges?.values) || ranges.values.length === 0) {
continue;
}

const { originFrequency: observedFrequency, nucleus } = info;

const value = rangesToACS(ranges.values, {
nucleus, // '19f'
deltaFormat: rangesPreferences.deltaPPM.format,
couplingFormat: rangesPreferences.coupling.format,
observedFrequency, //400
});

output[spectrumKey] = value;
}

return output;
}

export function FloatPublicationString() {
const publicationString = usePublicationString();
const publicationString = usePublicationStrings();
const {
data: spectra,
view: { ranges },
} = useChartData();
const options = Object.entries(ranges);
const options = useMemo(() => Object.entries(ranges), [ranges]);
const spectraToNucleusMap = useMemo(() => {
const map = new Map<string, string>();

for (const spectrum of spectra) {
if (!isSpectrum1D(spectrum)) continue;
const { nucleus } = spectrum.info;
map.set(spectrum.id, nucleus);
}

return map;
}, [spectra]);

return options.map(([spectrumKey, viewOptions]) => {
const { showPublicationString, publicationStringBounding } = viewOptions;
Expand All @@ -352,6 +355,7 @@
<DraggablePublicationString
key={spectrumKey}
spectrumKey={spectrumKey}
nucleus={spectraToNucleusMap.get(spectrumKey)}
bonding={publicationStringBounding}
value={publicationString[spectrumKey]}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/component/1d/peaks/PeakAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function resolveYOverlaps(
}

function useDetectPeakOverlaps(peaks: Peak[], format: string) {
const { getTextWidth } = useTextMetrics(textSize);
const { getTextWidth } = useTextMetrics({ labelSize: textSize });
const overlapPeaksById: Record<string, boolean> = {};

let cluster: Peak[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/component/1d/ranges/Ranges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ProcessedRange = RangeType & {
function useStackRangesAssignmentsLabels(ranges: RangeType[]) {
const { scaleX } = useScaleChecked();

const { getTextWidth } = useTextMetrics(labelSize);
const { getTextWidth } = useTextMetrics({ labelSize });

if (ranges.length === 0) return null;

Expand Down
2 changes: 1 addition & 1 deletion src/component/2d/YAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function YAxis(props: YAxisProps) {
const { width, height, margin } = useChartData();
const nucleusStr = useActiveNucleusTab();
const [, unit] = nucleusStr.split(',');
const { getTextWidth } = useTextMetrics(10);
const { getTextWidth } = useTextMetrics({ labelSize: 10 });

const scaleY = useScale2DY();

Expand Down
2 changes: 1 addition & 1 deletion src/component/2d/zones/SignalsGuideLines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function useSignalsOverlap(axis: IndicationLinesAxis, spectrum: Spectrum1D) {
});
const scaleX = useScale2DX();
const scaleY = useScale2DY();
const { getTextWidth } = useTextMetrics(labelSize);
const { getTextWidth } = useTextMetrics({ labelSize });

if (!signals) return null;

Expand Down
Loading
Loading