react-cheminfo 0.136.0 → 0.138.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.
Files changed (107) hide show
  1. package/README.md +1 -1
  2. package/lib/crystal3d/core/crystalView.d.ts +10 -0
  3. package/lib/crystal3d/core/crystalView.d.ts.map +1 -1
  4. package/lib/crystal3d/core/crystalView.js +7 -0
  5. package/lib/crystal3d/core/crystalView.js.map +1 -1
  6. package/lib/crystal3d/core/index.d.ts +2 -2
  7. package/lib/crystal3d/core/index.d.ts.map +1 -1
  8. package/lib/crystal3d/core/index.js +1 -1
  9. package/lib/crystal3d/core/index.js.map +1 -1
  10. package/lib/crystal3d/ui/CrystalCanvas3D.d.ts.map +1 -1
  11. package/lib/crystal3d/ui/CrystalCanvas3D.js +9 -1
  12. package/lib/crystal3d/ui/CrystalCanvas3D.js.map +1 -1
  13. package/lib/crystal3d/ui/CrystalStructureView.js +5 -2
  14. package/lib/crystal3d/ui/CrystalStructureView.js.map +1 -1
  15. package/lib/crystal3d/ui/CrystalViewControls.d.ts +7 -3
  16. package/lib/crystal3d/ui/CrystalViewControls.d.ts.map +1 -1
  17. package/lib/crystal3d/ui/CrystalViewControls.js +6 -4
  18. package/lib/crystal3d/ui/CrystalViewControls.js.map +1 -1
  19. package/lib/crystal3d/ui/crystalScene.d.ts +6 -0
  20. package/lib/crystal3d/ui/crystalScene.d.ts.map +1 -1
  21. package/lib/crystal3d/viewer/renderStructure.d.ts +25 -0
  22. package/lib/crystal3d/viewer/renderStructure.d.ts.map +1 -1
  23. package/lib/crystal3d/viewer/renderStructure.js +41 -1
  24. package/lib/crystal3d/viewer/renderStructure.js.map +1 -1
  25. package/lib/crystal3d/viewer/viewer.d.ts +11 -2
  26. package/lib/crystal3d/viewer/viewer.d.ts.map +1 -1
  27. package/lib/crystal3d/viewer/viewer.js +17 -2
  28. package/lib/crystal3d/viewer/viewer.js.map +1 -1
  29. package/lib/download/core/croppedMarkup.d.ts.map +1 -1
  30. package/lib/download/core/croppedMarkup.js +16 -40
  31. package/lib/download/core/croppedMarkup.js.map +1 -1
  32. package/lib/download/core/dataUrl.d.ts +27 -0
  33. package/lib/download/core/dataUrl.d.ts.map +1 -0
  34. package/lib/download/core/dataUrl.js +54 -0
  35. package/lib/download/core/dataUrl.js.map +1 -0
  36. package/lib/download/core/index.d.ts +3 -0
  37. package/lib/download/core/index.d.ts.map +1 -1
  38. package/lib/download/core/index.js +2 -0
  39. package/lib/download/core/index.js.map +1 -1
  40. package/lib/download/core/svgMarkup.d.ts +39 -0
  41. package/lib/download/core/svgMarkup.d.ts.map +1 -0
  42. package/lib/download/core/svgMarkup.js +100 -0
  43. package/lib/download/core/svgMarkup.js.map +1 -0
  44. package/lib/drawing/render/motion.d.ts +2 -2
  45. package/lib/drawing/render/motion.d.ts.map +1 -1
  46. package/lib/drawing/render/motion.js +23 -4
  47. package/lib/drawing/render/motion.js.map +1 -1
  48. package/lib/drawing/render/primitives.d.ts +14 -0
  49. package/lib/drawing/render/primitives.d.ts.map +1 -1
  50. package/lib/drawing/render/svg.d.ts.map +1 -1
  51. package/lib/drawing/render/svg.js +27 -7
  52. package/lib/drawing/render/svg.js.map +1 -1
  53. package/lib/drawing/ui/movingFigure.d.ts.map +1 -1
  54. package/lib/drawing/ui/movingFigure.js +4 -30
  55. package/lib/drawing/ui/movingFigure.js.map +1 -1
  56. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  57. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  58. package/lib/locales/de.json +7 -0
  59. package/lib/locales/en.json +7 -0
  60. package/lib/locales/es.json +7 -0
  61. package/lib/locales/fr.json +7 -0
  62. package/lib/locales/it.json +7 -0
  63. package/lib/referents/core/figureReferents.d.ts +3 -2
  64. package/lib/referents/core/figureReferents.d.ts.map +1 -1
  65. package/lib/referents/core/figureReferents.js +6 -3
  66. package/lib/referents/core/figureReferents.js.map +1 -1
  67. package/lib/referents/core/index.d.ts +1 -0
  68. package/lib/referents/core/index.d.ts.map +1 -1
  69. package/lib/referents/core/index.js +1 -0
  70. package/lib/referents/core/index.js.map +1 -1
  71. package/lib/referents/core/quoteParts.d.ts +21 -0
  72. package/lib/referents/core/quoteParts.d.ts.map +1 -0
  73. package/lib/referents/core/quoteParts.js +71 -0
  74. package/lib/referents/core/quoteParts.js.map +1 -0
  75. package/lib/referents/core/readReferents.d.ts +2 -1
  76. package/lib/referents/core/readReferents.d.ts.map +1 -1
  77. package/lib/referents/core/readReferents.js +28 -2
  78. package/lib/referents/core/readReferents.js.map +1 -1
  79. package/lib/referents/core/types.d.ts +44 -0
  80. package/lib/referents/core/types.d.ts.map +1 -1
  81. package/package.json +1 -1
  82. package/src/crystal3d/core/crystalView.ts +17 -0
  83. package/src/crystal3d/core/index.ts +2 -0
  84. package/src/crystal3d/ui/CrystalCanvas3D.tsx +9 -0
  85. package/src/crystal3d/ui/CrystalStructureView.tsx +7 -2
  86. package/src/crystal3d/ui/CrystalViewControls.tsx +33 -4
  87. package/src/crystal3d/ui/crystalScene.ts +6 -0
  88. package/src/crystal3d/viewer/renderStructure.ts +66 -1
  89. package/src/crystal3d/viewer/viewer.ts +19 -3
  90. package/src/download/core/croppedMarkup.ts +16 -42
  91. package/src/download/core/dataUrl.ts +59 -0
  92. package/src/download/core/index.ts +3 -0
  93. package/src/download/core/svgMarkup.ts +113 -0
  94. package/src/drawing/render/motion.ts +23 -4
  95. package/src/drawing/render/primitives.ts +14 -0
  96. package/src/drawing/render/svg.ts +34 -7
  97. package/src/drawing/ui/movingFigure.ts +4 -28
  98. package/src/locales/de.json +7 -0
  99. package/src/locales/en.json +7 -0
  100. package/src/locales/es.json +7 -0
  101. package/src/locales/fr.json +7 -0
  102. package/src/locales/it.json +7 -0
  103. package/src/referents/core/figureReferents.ts +8 -3
  104. package/src/referents/core/index.ts +1 -0
  105. package/src/referents/core/quoteParts.ts +82 -0
  106. package/src/referents/core/readReferents.ts +27 -2
  107. package/src/referents/core/types.ts +45 -0
@@ -0,0 +1,82 @@
1
+ /**
2
+ * The text of a quote a figure carries, read out of its inline HTML as runs of
3
+ * text and the marks over them: what a document inserts, and what a menu
4
+ * shows. Only the marks a quote may carry are read; every other tag is
5
+ * dropped, its text kept, so nothing a figure carries is ever run as markup.
6
+ */
7
+
8
+ import { unescapeXml } from '../../drawing/render/markup.ts';
9
+
10
+ /** A mark over a run of a quote's text. */
11
+ export type QuoteMark = 'superscript' | 'subscript' | 'italic' | 'bold';
12
+
13
+ /** A run of a quote's text, under the same marks. */
14
+ export interface QuotePart {
15
+ text: string;
16
+ /** The marks over it, each once, in the order they were opened. */
17
+ marks: QuoteMark[];
18
+ }
19
+
20
+ const MARKS: Readonly<Record<string, QuoteMark>> = {
21
+ sup: 'superscript',
22
+ sub: 'subscript',
23
+ i: 'italic',
24
+ em: 'italic',
25
+ b: 'bold',
26
+ strong: 'bold',
27
+ };
28
+
29
+ const TAG = /^<(?<close>\/)?(?<name>[a-z][a-z\d]*)\b[^>]*>$/i;
30
+
31
+ /**
32
+ * The runs of a quote's text.
33
+ * @param html - The quote, as inline HTML.
34
+ * @returns Its text, run by run; adjacent runs under the same marks are one.
35
+ */
36
+ export function quoteParts(html: string): QuotePart[] {
37
+ const parts: QuotePart[] = [];
38
+ const open: QuoteMark[] = [];
39
+ for (const token of html.split(/(?<tag><[^>]*>)/)) {
40
+ if (token === '') continue;
41
+ if (token.startsWith('<')) {
42
+ const groups = TAG.exec(token)?.groups;
43
+ const mark = MARKS[groups?.name?.toLowerCase() ?? ''];
44
+ if (mark === undefined) continue;
45
+ if (groups?.close === undefined) {
46
+ open.push(mark);
47
+ } else {
48
+ const index = open.lastIndexOf(mark);
49
+ if (index !== -1) open.splice(index, 1);
50
+ }
51
+ continue;
52
+ }
53
+ append(parts, decoded(token), [...new Set(open)]);
54
+ }
55
+ return parts;
56
+ }
57
+
58
+ function append(parts: QuotePart[], text: string, marks: QuoteMark[]): void {
59
+ if (text === '') return;
60
+ const last = parts.at(-1);
61
+ if (last !== undefined && sameMarks(last.marks, marks)) {
62
+ last.text += text;
63
+ } else {
64
+ parts.push({ text, marks });
65
+ }
66
+ }
67
+
68
+ function sameMarks(first: QuoteMark[], second: QuoteMark[]): boolean {
69
+ return (
70
+ first.length === second.length &&
71
+ first.every((mark) => second.includes(mark))
72
+ );
73
+ }
74
+
75
+ function decoded(text: string): string {
76
+ try {
77
+ return unescapeXml(text);
78
+ } catch {
79
+ // A character reference past the last code point: kept as written.
80
+ return text;
81
+ }
82
+ }
@@ -4,6 +4,7 @@
4
4
  */
5
5
 
6
6
  import type {
7
+ FigureQuote,
7
8
  FigureReferent,
8
9
  FigureReferents,
9
10
  ReferentBox,
@@ -15,7 +16,8 @@ import type {
15
16
  /**
16
17
  * Referents from parsed JSON.
17
18
  * @param value - What was parsed.
18
- * @returns The referents and series that read; empty lists for anything else.
19
+ * @returns The referents, series and quotes that read; empty lists for
20
+ * anything else, and no quotes when none read.
19
21
  */
20
22
  export function readReferents(value: unknown): FigureReferents {
21
23
  const record = isRecord(value) ? value : {};
@@ -29,7 +31,30 @@ export function readReferents(value: unknown): FigureReferents {
29
31
  const one = readSeries(item);
30
32
  if (one) series.push(one);
31
33
  }
32
- return { referents, series };
34
+ const quotes: FigureQuote[] = [];
35
+ for (const item of listOf(record.quotes)) {
36
+ const quote = readQuote(item);
37
+ if (quote) quotes.push(quote);
38
+ }
39
+ return quotes.length === 0
40
+ ? { referents, series }
41
+ : { referents, series, quotes };
42
+ }
43
+
44
+ function readQuote(value: unknown): FigureQuote | undefined {
45
+ if (!isRecord(value) || !isKey(value.key) || !isKey(value.kind)) {
46
+ return undefined;
47
+ }
48
+ if (!isText(value.title) || !isText(value.html)) return undefined;
49
+ const quote: FigureQuote = {
50
+ key: value.key,
51
+ kind: value.kind,
52
+ title: value.title,
53
+ html: value.html,
54
+ };
55
+ if (isText(value.picture)) quote.picture = value.picture;
56
+ copyStructure(value, quote);
57
+ return quote;
33
58
  }
34
59
 
35
60
  function readReferent(value: unknown): FigureReferent | undefined {
@@ -131,12 +131,57 @@ export interface ReferentSeries {
131
131
  numbering?: SeriesNumbering;
132
132
  }
133
133
 
134
+ /**
135
+ * A text a figure lets a document quote as it stands, rather than number: the
136
+ * description of an NMR spectrum as a journal prints it, offered by the `@`
137
+ * menu of the document the figure is placed in.
138
+ */
139
+ export interface FigureQuote {
140
+ /** What names it, stable for as long as the figure carries it. */
141
+ key: string;
142
+ /** What it is, `nmr`: quotes of one kind are offered together. */
143
+ kind: string;
144
+ /** What it is offered as, `¹H NMR · ethylbenzene`. */
145
+ title: string;
146
+ /**
147
+ * The text, as inline HTML: only `<sup>`, `<sub>`, `<i>`, `<em>`, `<b>` and
148
+ * `<strong>` are read, by `quoteParts`.
149
+ */
150
+ html: string;
151
+ /**
152
+ * What it describes, as an SVG document: the structure the spectrum was
153
+ * recorded on.
154
+ * @default undefined — named by its title alone
155
+ */
156
+ picture?: string;
157
+ /**
158
+ * The structure as an OpenChemLib ID code.
159
+ * @default undefined
160
+ */
161
+ idCode?: string;
162
+ /**
163
+ * The structure as SMILES.
164
+ * @default undefined
165
+ */
166
+ smiles?: string;
167
+ /**
168
+ * The structure's molecular formula.
169
+ * @default undefined
170
+ */
171
+ mf?: string;
172
+ }
173
+
134
174
  /** What a figure numbers. */
135
175
  export interface FigureReferents {
136
176
  /** The referents, in the order their labels are read. */
137
177
  referents: FigureReferent[];
138
178
  /** The series the figure defines. */
139
179
  series: ReferentSeries[];
180
+ /**
181
+ * The texts it lets a document quote.
182
+ * @default undefined — none
183
+ */
184
+ quotes?: FigureQuote[];
140
185
  }
141
186
 
142
187
  /** A label, or a reference in the text: a referent, or one of its members. */