Skip to content

Commit 3766d36

Browse files
committed
feat!: migrate UI.form/UI.renderTwig to Twig module and convert AMD files to ESM
Requires visualizer >= 4.2.0: Twig.form and Twig.renderTwig have moved from src/util/ui to src/util/twig. Also fixes a long-standing bug in ExtSample.js where newData was used but never declared, causing extra converted spectra to be silently dropped. BREAKING CHANGE: visualizer >= 4.2.0 is now required. Views using UI.form or UI.renderTwig must update to import from src/util/twig.
1 parent 3a4f42f commit 3766d36

11 files changed

Lines changed: 314 additions & 207 deletions

File tree

eln/ExtSample.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ class Sample {
192192
// we store the data in the view
193193
let droppedDatas = API.getData(name);
194194
droppedDatas = droppedDatas.file || droppedDatas.str;
195+
const newData = [];
195196
for (let droppedData of droppedDatas) {
196197
if (!droppedData.filename.includes('.')) droppedData.filename += '.txt';
197198

@@ -235,6 +236,17 @@ class Sample {
235236
{ keepContent: true },
236237
);
237238
}
239+
for (let extra of newData) {
240+
elnPlugin.process(
241+
types[name],
242+
this.sample.$content,
243+
extra,
244+
{},
245+
{
246+
keepContent: true,
247+
},
248+
);
249+
}
238250
this.sample.triggerChange();
239251
}
240252

eln/Sample.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Datas from 'src/main/datas';
22
import API from 'src/util/api';
3+
import Twig from 'src/util/twig';
34
import UI from 'src/util/ui';
45

56
import Roc from '../rest-on-couch/Roc';
@@ -577,7 +578,7 @@ Your local changes will be lost.</p>`;
577578
'',
578579
)}.jdx`;
579580
// we will ask for meta information
580-
let meta = await UI.form(
581+
let meta = await Twig.form(
581582
`
582583
<style>
583584
#jcamp {

eln/__tests__/sampleMigrations.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const { migrateIupac } = require('../sampleMigrations');
33
const sample = require('./data/sample.json');
44

55
test('kind: true is migrated to iupac', () => {
6-
const s = { $content: { general: { name: [{ value: 'ethylbenzene', kind: true }] } } };
6+
const s = {
7+
$content: { general: { name: [{ value: 'ethylbenzene', kind: true }] } },
8+
};
79
migrateIupac(s);
810
expect(s.$content.general.name[0].kind).toBe('iupac');
911
});
@@ -15,15 +17,24 @@ test('kind: false is migrated to trivial', () => {
1517
});
1618

1719
test('kind: undefined is migrated to iupac', () => {
18-
const s = { $content: { general: { name: [{ value: 'ethylbenzene', kind: undefined }] } } };
20+
const s = {
21+
$content: {
22+
general: { name: [{ value: 'ethylbenzene', kind: undefined }] },
23+
},
24+
};
1925
migrateIupac(s);
2026
expect(s.$content.general.name[0].kind).toBe('iupac');
2127
});
2228

2329
test('already-migrated string kind is left unchanged', () => {
2430
const s = {
2531
$content: {
26-
general: { name: [{ value: 'ethylbenzene', kind: 'iupac' }, { value: 'EB', kind: 'trivial' }] },
32+
general: {
33+
name: [
34+
{ value: 'ethylbenzene', kind: 'iupac' },
35+
{ value: 'EB', kind: 'trivial' },
36+
],
37+
},
2738
},
2839
};
2940
migrateIupac(s);

eln/pubchem/ghs.js

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
import OCL from 'openchemlib';
2+
import API from 'src/util/api';
3+
import Twig from 'src/util/twig';
4+
import UI from 'src/util/ui';
25

3-
// returns GHS information based on pubchem and a smiles
6+
/**
7+
* @typedef {object} GhsOptions
8+
* @property {boolean} [detailed=false] - Show full GHS data instead of summary
9+
*/
410

5-
define(['src/util/ui', 'src/util/api'], (UI, API) => {
6-
async function fromIDCode(oclCode, options) {
7-
const molecule = OCL.Molecule.fromIDCode(oclCode);
8-
const smiles = molecule.toSmiles();
9-
return fromSMILES(smiles, options);
10-
}
11+
/**
12+
* @param {string} oclCode
13+
* @param {GhsOptions} [options]
14+
*/
15+
async function fromIDCode(oclCode, options) {
16+
const molecule = OCL.Molecule.fromIDCode(oclCode);
17+
const smiles = molecule.toSmiles();
18+
return fromSMILES(smiles, options);
19+
}
1120

12-
async function fromSMILES(smiles, options = {}) {
13-
const { detailed = false } = options;
21+
/**
22+
* @param {string} smiles
23+
* @param {GhsOptions} [options]
24+
*/
25+
async function fromSMILES(smiles, options = {}) {
26+
const { detailed = false } = options;
1427

15-
const { Compound } = await API.require(
16-
'https://www.lactame.com/lib/pubchem/0.2.0/pubchem.js',
17-
);
18-
const compound = await Compound.fromSmiles(String(smiles));
19-
const compoundData = await compound.getData();
28+
const { Compound } = await API.require(
29+
'https://www.lactame.com/lib/pubchem/0.2.0/pubchem.js',
30+
);
31+
const compound = await Compound.fromSmiles(String(smiles));
32+
const compoundData = await compound.getData();
2033

21-
const ghs = compoundData.ghs;
22-
const ghsFull = compoundData.getGHS();
34+
const ghs = compoundData.ghs;
35+
const ghsFull = compoundData.getGHS();
2336

24-
const html = detailed
25-
? await UI.renderTwig(ghsFullTemplate, { ghsFull })
26-
: await UI.renderTwig(ghsTemplate, { ghs });
37+
const html = detailed
38+
? await Twig.renderTwig(ghsFullTemplate, { ghsFull })
39+
: await Twig.renderTwig(ghsTemplate, { ghs });
2740

28-
UI.dialog(html, {
29-
width: 1000,
30-
height: 800,
31-
title: 'GHS information',
32-
});
33-
}
41+
UI.dialog(html, {
42+
width: 1000,
43+
height: 800,
44+
title: 'GHS information',
45+
});
46+
}
3447

35-
return { fromIDCode, fromSMILES };
36-
});
48+
export default { fromIDCode, fromSMILES };
3749

3850
const ghsTemplate = `
3951
<style>
4052
#ghsSmmary {
41-
53+
4254
}
4355
#ghsSummary table {
4456
border-collapse: collapse;
@@ -110,7 +122,7 @@ const ghsTemplate = `
110122
const ghsFullTemplate = `
111123
<style>
112124
#ghsFull {
113-
125+
114126
}
115127
#ghsFull table {
116128
border-collapse: collapse;

eln/request/processAction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import API from 'src/util/api';
2+
import Twig from 'src/util/twig';
23
import UI from 'src/util/ui';
34

45
import Status from './Status';
@@ -133,7 +134,7 @@ async function askNewStatus(request) {
133134
currentStatus++;
134135
}
135136

136-
let newStatusObject = await UI.form(
137+
let newStatusObject = await Twig.form(
137138
` <style>
138139
#status {
139140
zoom: 1.5;

eln/stock/printer/printProcessors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
define([
22
'src/main/datas',
3-
'src/util/ui',
3+
'src/util/twig',
44
'browserified/twig/twig',
55
'canvg',
66
'../../libs/Image',
77
'openchemlib',
8-
], (Datas, UI, twig, canvg, IJS, OCL) => {
8+
], (Datas, Twig, twig, canvg, IJS, OCL) => {
99
IJS = IJS.default;
1010
const DataObject = Datas.DataObject;
1111
let chars =
@@ -239,7 +239,7 @@ define([
239239
}
240240
if (allDefined) return data;
241241

242-
return UI.form(
242+
return Twig.form(
243243
`
244244
<div>
245245
<form>

0 commit comments

Comments
 (0)