@complat/react-spectra-editor 1.5.4 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -51,6 +51,24 @@ require("./__tests__/style/svg.css");
51
51
  var _jsxRuntime = require("react/jsx-runtime");
52
52
  /* eslint-disable prefer-object-spread, default-param-last, no-nested-ternary */
53
53
 
54
+ const pickSelectedSpectrumFromPayload = payload => {
55
+ const spectraList = payload?.spectra_list;
56
+ if (!Array.isArray(spectraList)) return payload || {};
57
+ if (spectraList.length === 0) return {};
58
+ const selectedIdx = Number.isFinite(payload?.curveSt?.curveIdx) ? payload.curveSt.curveIdx : 0;
59
+ return spectraList[selectedIdx] || spectraList[0] || {};
60
+ };
61
+ const normalizeShiftForFormatting = shift => {
62
+ if (shift && Array.isArray(shift.shifts)) return shift;
63
+ return {
64
+ selectedIdx: 0,
65
+ shifts: [shift || {
66
+ ref: {},
67
+ peak: false,
68
+ enable: true
69
+ }]
70
+ };
71
+ };
54
72
  const nmr1HEntity = _app.FN.ExtractJcamp(_nmr1h_jcamp.default);
55
73
  const nmr1HEntity2 = _app.FN.ExtractJcamp(_nmr1h_2_jcamp.default);
56
74
  const nmr13CEntity = _app.FN.ExtractJcamp(_nmr13c_jcamp.default);
@@ -280,6 +298,7 @@ class DemoWriteIr extends _react.default.Component {
280
298
  curveSt
281
299
  }) {
282
300
  const entity = this.loadEntity();
301
+ const safeLayout = layout || entity?.layout;
283
302
  const {
284
303
  features
285
304
  } = entity;
@@ -294,11 +313,12 @@ class DemoWriteIr extends _react.default.Component {
294
313
  maxY,
295
314
  minY
296
315
  };
316
+ const shiftForFormatting = normalizeShiftForFormatting(shift);
297
317
  const body = _app.FN.peaksBody({
298
318
  peaks,
299
- layout,
319
+ layout: safeLayout,
300
320
  decimal,
301
- shift,
321
+ shift: shiftForFormatting,
302
322
  isAscend,
303
323
  isIntensity,
304
324
  boundary,
@@ -306,9 +326,9 @@ class DemoWriteIr extends _react.default.Component {
306
326
  waveLength,
307
327
  temperature
308
328
  });
309
- const wrapper = _app.FN.peaksWrapper(layout, shift);
329
+ const wrapper = _app.FN.peaksWrapper(safeLayout, shiftForFormatting);
310
330
  let desc = this.rmDollarSign(wrapper.head) + body + wrapper.tail;
311
- if (_app.FN.isCyclicVoltaLayout(layout)) {
331
+ if (_app.FN.isCyclicVoltaLayout(safeLayout) && cyclicvoltaSt?.spectraList && curveSt?.listCurves) {
312
332
  const {
313
333
  spectraList
314
334
  } = cyclicvoltaSt;
@@ -318,6 +338,7 @@ class DemoWriteIr extends _react.default.Component {
318
338
  } = curveSt;
319
339
  const selectedVolta = spectraList[curveIdx];
320
340
  const selectedCurve = listCurves[curveIdx];
341
+ if (!selectedVolta || !selectedCurve?.feature) return desc;
321
342
  const {
322
343
  feature
323
344
  } = selectedCurve;
@@ -380,10 +401,12 @@ class DemoWriteIr extends _react.default.Component {
380
401
  };
381
402
  const area = it.area * refFactor / refArea; // eslint-disable-line
382
403
  const center = _app.FN.calcMpyCenter(peaks, shiftVal, mpyType);
404
+ const safeCenter = Number.isFinite(center) ? center : 0;
383
405
  const xs = m.peaks.map(p => p.x).sort((a, b) => a - b);
384
406
  const [aIdx, bIdx] = isAscend ? [0, xs.length - 1] : [xs.length - 1, 0];
385
- const mxA = mpyType === 'm' ? (xs[aIdx] - shiftVal).toFixed(decimal) : 0;
386
- const mxB = mpyType === 'm' ? (xs[bIdx] - shiftVal).toFixed(decimal) : 0;
407
+ const hasValidRange = xs.length > 0 && Number.isFinite(shiftVal);
408
+ const mxA = mpyType === 'm' && hasValidRange ? (xs[aIdx] - shiftVal).toFixed(decimal) : safeCenter.toFixed(decimal);
409
+ const mxB = mpyType === 'm' && hasValidRange ? (xs[bIdx] - shiftVal).toFixed(decimal) : safeCenter.toFixed(decimal);
387
410
  return Object.assign({}, m, {
388
411
  area,
389
412
  center,
@@ -400,22 +423,25 @@ class DemoWriteIr extends _react.default.Component {
400
423
  const location = type === 'm' ? `${m.mxA}–${m.mxB}` : `${c.toFixed(decimal)}`;
401
424
  return m.js.length === 0 ? `${location} (${type}${atomCount})` : `${location} (${type}, ${js}${atomCount})`;
402
425
  }).join(', ');
426
+ const shiftRef = shift?.ref || {};
403
427
  const {
404
428
  label,
405
429
  value,
406
430
  name
407
- } = shift.ref;
408
- const solvent = label ? `${name.split('(')[0].trim()} [${value.toFixed(decimal)} ppm], ` : '';
431
+ } = shiftRef;
432
+ const hasValidShiftRef = !!label && Number.isFinite(value) && typeof name === 'string';
433
+ const solvent = hasValidShiftRef ? `${name.split('(')[0].trim()} [${value.toFixed(decimal)} ppm], ` : '';
409
434
  return `${layout} NMR (${freqStr}${solvent}ppm) δ = ${str}.`;
410
435
  }
411
- writeMpy({
412
- layout,
413
- shift,
414
- isAscend,
415
- decimal,
416
- multiplicity,
417
- integration
418
- }) {
436
+ writeMpy(payload) {
437
+ const {
438
+ layout,
439
+ shift,
440
+ isAscend,
441
+ decimal,
442
+ multiplicity,
443
+ integration
444
+ } = pickSelectedSpectrumFromPayload(payload);
419
445
  if (!_app.FN.isNmrLayout(layout)) return;
420
446
  const desc = this.formatMpy({
421
447
  multiplicity,
@@ -429,18 +455,19 @@ class DemoWriteIr extends _react.default.Component {
429
455
  desc
430
456
  });
431
457
  }
432
- writePeak({
433
- peaks,
434
- layout,
435
- shift,
436
- isAscend,
437
- decimal,
438
- isIntensity,
439
- integration,
440
- waveLength,
441
- cyclicvoltaSt,
442
- curveSt
443
- }) {
458
+ writePeak(payload) {
459
+ const {
460
+ peaks,
461
+ layout,
462
+ shift,
463
+ isAscend,
464
+ decimal,
465
+ isIntensity,
466
+ integration,
467
+ waveLength,
468
+ cyclicvoltaSt,
469
+ curveSt
470
+ } = pickSelectedSpectrumFromPayload(payload);
444
471
  const desc = this.formatPks({
445
472
  peaks,
446
473
  layout,
@@ -458,19 +485,21 @@ class DemoWriteIr extends _react.default.Component {
458
485
  desc
459
486
  });
460
487
  }
461
- savePeaks({
462
- peaks,
463
- layout,
464
- shift,
465
- isAscend,
466
- decimal,
467
- analysis,
468
- isIntensity,
469
- integration,
470
- multiplicity,
471
- waveLength
472
- }) {
488
+ savePeaks(payload) {
489
+ const {
490
+ peaks,
491
+ layout,
492
+ shift,
493
+ isAscend,
494
+ decimal,
495
+ analysis,
496
+ isIntensity,
497
+ integration,
498
+ multiplicity,
499
+ waveLength
500
+ } = pickSelectedSpectrumFromPayload(payload);
473
501
  const entity = this.loadEntity();
502
+ const safeLayout = layout || entity?.layout;
474
503
  const {
475
504
  features
476
505
  } = entity;
@@ -485,11 +514,12 @@ class DemoWriteIr extends _react.default.Component {
485
514
  maxY,
486
515
  minY
487
516
  };
517
+ const shiftForFormatting = normalizeShiftForFormatting(shift);
488
518
  const body = _app.FN.peaksBody({
489
519
  peaks,
490
- layout,
520
+ layout: safeLayout,
491
521
  decimal,
492
- shift,
522
+ shift: shiftForFormatting,
493
523
  isAscend,
494
524
  isIntensity,
495
525
  boundary,
@@ -9,7 +9,10 @@ var _chem = require("../helpers/chem");
9
9
  /* eslint-disable prefer-object-spread, default-param-last */
10
10
 
11
11
  const initialState = {
12
- spectraList: []
12
+ spectraList: [],
13
+ areaValue: 1.0,
14
+ areaUnit: 'cm²',
15
+ useCurrentDensity: false
13
16
  };
14
17
  const initSpectra = {
15
18
  list: [],
@@ -563,6 +566,36 @@ const cyclicVoltaReducer = (state = initialState, action) => {
563
566
  return Object.assign({}, state, {
564
567
  spectraList: []
565
568
  });
569
+ case _action_type.CYCLIC_VOLTA_METRY.SET_AREA_VALUE:
570
+ {
571
+ const {
572
+ value
573
+ } = action.payload;
574
+ if (value === '') {
575
+ return Object.assign({}, state, {
576
+ areaValue: ''
577
+ });
578
+ }
579
+ const areaValue = Number.isFinite(value) ? value : state.areaValue;
580
+ return Object.assign({}, state, {
581
+ areaValue
582
+ });
583
+ }
584
+ case _action_type.CYCLIC_VOLTA_METRY.SET_AREA_UNIT:
585
+ {
586
+ const {
587
+ unit
588
+ } = action.payload;
589
+ return Object.assign({}, state, {
590
+ areaUnit: unit
591
+ });
592
+ }
593
+ case _action_type.CYCLIC_VOLTA_METRY.TOGGLE_DENSITY:
594
+ {
595
+ return Object.assign({}, state, {
596
+ useCurrentDensity: !state.useCurrentDensity
597
+ });
598
+ }
566
599
  default:
567
600
  return state;
568
601
  }
@@ -38,6 +38,66 @@ function* setCyclicVoltametry(action) {
38
38
  if (layout !== _list_layout.LIST_LAYOUT.CYCLIC_VOLTAMMETRY) {
39
39
  return;
40
40
  }
41
+ const cvSt = yield (0, _effects.select)(state => state.cyclicvolta);
42
+ const {
43
+ feature
44
+ } = firstCurve;
45
+ if (feature) {
46
+ const {
47
+ weAreaValue,
48
+ weAreaUnit,
49
+ currentMode
50
+ } = feature;
51
+ if (typeof weAreaUnit === 'string' && weAreaUnit.length > 0) {
52
+ const unit = weAreaUnit.replace('^2', '²');
53
+ yield (0, _effects.put)({
54
+ type: _action_type.CYCLIC_VOLTA_METRY.SET_AREA_UNIT,
55
+ payload: {
56
+ unit
57
+ }
58
+ });
59
+ } else {
60
+ yield (0, _effects.put)({
61
+ type: _action_type.CYCLIC_VOLTA_METRY.SET_AREA_UNIT,
62
+ payload: {
63
+ unit: 'cm²'
64
+ }
65
+ });
66
+ }
67
+ if (weAreaValue !== undefined && weAreaValue !== null) {
68
+ let numeric = null;
69
+ if (typeof weAreaValue === 'string') {
70
+ const parsed = parseFloat(weAreaValue);
71
+ if (!Number.isNaN(parsed)) numeric = parsed;
72
+ } else if (Number.isFinite(weAreaValue)) {
73
+ numeric = weAreaValue;
74
+ }
75
+ if (Number.isFinite(numeric)) {
76
+ yield (0, _effects.put)({
77
+ type: _action_type.CYCLIC_VOLTA_METRY.SET_AREA_VALUE,
78
+ payload: {
79
+ value: numeric
80
+ }
81
+ });
82
+ } else {
83
+ yield (0, _effects.put)({
84
+ type: _action_type.CYCLIC_VOLTA_METRY.SET_AREA_VALUE,
85
+ payload: {
86
+ value: 1.0
87
+ }
88
+ });
89
+ }
90
+ }
91
+ if (typeof currentMode === 'string' && currentMode.length > 0) {
92
+ const wantDensity = currentMode.toUpperCase() === 'DENSITY';
93
+ if (!!cvSt.useCurrentDensity !== wantDensity) {
94
+ yield (0, _effects.put)({
95
+ type: _action_type.CYCLIC_VOLTA_METRY.TOGGLE_DENSITY,
96
+ payload: null
97
+ });
98
+ }
99
+ }
100
+ }
41
101
  for (let index = 0; index < listCurves.length; index++) {
42
102
  const curve = listCurves[index];
43
103
  const maxminPeak = getMaxMinPeak(curve);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@complat/react-spectra-editor",
3
- "version": "1.5.4",
3
+ "version": "1.7.0",
4
4
  "description": "An editor to View and Edit Chemical Spectra data (NMR, IR, MS, CV, UIVIS, XRD, GC, and DSC).",
5
5
  "repository": {
6
6
  "type": "git",