-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathatom.ts
128 lines (112 loc) · 5.46 KB
/
atom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { isArray } from '../common/types';
import { Atom, AtomJson, AtomType, NAMED_BRANCHES } from './atom-class';
import { AccentAtom } from '../core-atoms/accent';
import { ArrayAtom } from '../core-atoms/array';
import { BoxAtom } from '../core-atoms/box';
import { CompositionAtom } from '../core-atoms/composition';
import { ChemAtom } from '../core-definitions/mhchem';
import { ChoiceAtom } from '../core-atoms/choice';
import { DelimAtom } from '../core-atoms/delim';
import { EncloseAtom } from '../core-atoms/enclose';
import { ErrorAtom } from '../core-atoms/error';
import { GenfracAtom } from '../core-atoms/genfrac';
import { GroupAtom } from '../core-atoms/group';
import { LatexAtom, LatexGroupAtom } from '../core-atoms/latex';
import { LeftRightAtom } from '../core-atoms/leftright';
import { LineAtom } from '../core-atoms/line';
import { MacroAtom } from '../core-atoms/macro';
import { OperatorAtom } from '../core-atoms/operator';
import { OverlapAtom } from '../core-atoms/overlap';
import { OverunderAtom } from '../core-atoms/overunder';
import { PlaceholderAtom } from '../core-atoms/placeholder';
import { PhantomAtom } from '../core-atoms/phantom';
import { RuleAtom } from '../core-atoms/rule';
import { SizedDelimAtom } from '../core-atoms/delim';
import { SpacingAtom } from '../core-atoms/spacing';
import { SubsupAtom } from '../core-atoms/subsup';
import { SurdAtom } from '../core-atoms/surd';
import { TextAtom } from '../core-atoms/text';
import { TooltipAtom } from '../core-atoms/tooltip';
import { PromptAtom } from '../core-atoms/prompt';
import { NewLineAtom } from '../core-atoms/newline';
import type { GlobalContext } from '../core/types';
export * from './atom-class';
export function fromJson(json: AtomJson, context: GlobalContext): Atom;
export function fromJson(json: AtomJson[], context: GlobalContext): Atom[];
export function fromJson(
json: AtomJson | AtomJson[],
context: GlobalContext
): Atom | Atom[] {
if (isArray<AtomJson>(json)) return json.map((x) => fromJson(x, context));
json = { ...json };
// Restore the branches
for (const branch of NAMED_BRANCHES) {
if (json[branch])
json[branch] = fromJson(json[branch] as AtomJson[], context);
}
if (json.array) json.array = fromJson(json.array, context);
const type: AtomType = json.type;
let result: Atom | undefined = undefined;
if (type === 'accent') result = AccentAtom.fromJson(json, context);
if (type === 'array') result = ArrayAtom.fromJson(json, context);
if (type === 'box') result = BoxAtom.fromJson(json, context);
if (type === 'chem') result = ChemAtom.fromJson(json, context);
if (type === 'choice') result = ChoiceAtom.fromJson(json, context);
if (type === 'composition') result = CompositionAtom.fromJson(json, context);
if (type === 'delim') result = DelimAtom.fromJson(json, context);
if (type === 'enclose') result = EncloseAtom.fromJson(json, context);
if (type === 'error') result = ErrorAtom.fromJson(json, context);
if (type === 'genfrac') result = GenfracAtom.fromJson(json, context);
if (type === 'group') result = GroupAtom.fromJson(json, context);
if (type === 'latex') result = LatexAtom.fromJson(json, context);
if (type === 'latexgroup') result = LatexGroupAtom.fromJson(json, context);
if (type === 'leftright') result = LeftRightAtom.fromJson(json, context);
if (type === 'line') result = LineAtom.fromJson(json, context);
if (type === 'macro') result = MacroAtom.fromJson(json, context);
if (type === 'newline') result = NewLineAtom.fromJson(json, context);
if (type === 'subsup') result = SubsupAtom.fromJson(json, context);
if (type === 'overlap') result = OverlapAtom.fromJson(json, context);
if (type === 'overunder') result = OverunderAtom.fromJson(json, context);
if (type === 'placeholder') result = PlaceholderAtom.fromJson(json, context);
if (type === 'prompt') result = PromptAtom.fromJson(json, context);
if (type === 'phantom') result = PhantomAtom.fromJson(json, context);
if (type === 'rule') result = RuleAtom.fromJson(json, context);
if (type === 'sizeddelim') result = SizedDelimAtom.fromJson(json, context);
if (type === 'spacing') result = SpacingAtom.fromJson(json, context);
if (type === 'surd') result = SurdAtom.fromJson(json, context);
if (type === 'text') result = TextAtom.fromJson(json, context);
if (type === 'tooltip') result = TooltipAtom.fromJson(json, context);
if (type === 'mop') result = OperatorAtom.fromJson(json, context);
// @todo root;
// @todo space;
if (!result) {
console.assert(
[
'first',
'mbin',
'mrel',
'mclose',
'minner',
'mopen',
'mord',
'mpunct',
'root',
'space',
].includes(type),
`MathLive {{SDK_VERSION}}: an unexpected atom type "${type}" was encountered. Add new atom constructors to \`fromJson()\` in "atom.ts"`
);
result = Atom.fromJson(json, context);
}
for (const branch of NAMED_BRANCHES)
if (json[branch]) result.setChildren(json[branch], branch);
// Restore properties
if (json.verbatimLatex !== undefined)
result.verbatimLatex = json.verbatimLatex;
if (json.subsupPlacement) result.subsupPlacement = json.subsupPlacement;
if (json.explicitSubsupPlacement) result.explicitSubsupPlacement = true;
if (json.isFunction) result.isFunction = true;
if (json.isExtensibleSymbol) result.isExtensibleSymbol = true;
if (json.skipBoundary) result.skipBoundary = true;
if (json.captureSelection) result.captureSelection = true;
return result;
}