-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmo.ts
148 lines (140 loc) · 4.33 KB
/
mo.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { MathMLElement, html, TemplateResult, customElement, property, PropertyValues } from './mathml-element.js';
export declare type MathOperatorForm = 'prefix' | 'infix' | 'postfix';
@customElement('math-o')
export class MathOElement extends MathMLElement {
@property({ type: String }) form?: MathOperatorForm;
@property({ type: String }) stretchy?: string;
private formStyle = '';
render(): TemplateResult {
return html`
<style>
:host {
display: -ms-inline-flexbox;
display: -webkit-inline-flex;
display: inline-flex;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
position: relative;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
color: var(--math-color, inherit);
background: var(--math-background, inherit);
}
:host(.mo-infix) {
margin: 0 0.2em;
}
:host(.mo-separator) {
margin: 0 0.2em 0 0;
}
:host(.mo-product) {
margin: 0;
}
:host(.mo-begin-brace) {
margin: 0 0.05em 0 0.2em;
}
:host(.mo-end-brace) {
margin: 0 0.2em 0 0.05em;
}
:host(.mo-neut-brace) {
margin: 0 0.16em;
}
:host(.mo-stretchy) {
align-self: stretch;
}
:host(.mo-bigger) {
line-height: 1.1;
font-size: 1.8em;
}
.invisible {
opacity: 0;
}
.fullWidthSpan {
width: 100%;
box-sizing: border-box;
text-align: center;
}
</style>
<span class="invisible"><slot @slotchange="${this.onSlotChange}"></slot></span>
`;
}
updated(propVals: PropertyValues) {
super.updated(propVals);
this.onSlotChange();
}
private onSlotChange() {
if (!this.shadowRoot) {
return;
}
const span = this.shadowRoot.querySelector('span');
if (!span) {
return;
}
let specialRule = '';
const text = (this.textContent || '').trim();
if (text === ',' || text === ';') {
specialRule = 'separator';
} else if (text === '.' || text === '⋅') {
specialRule = 'product';
} else if (text.match(/^[\[{(]*$/)) {
specialRule = 'begin-brace';
} else if (text.match(/^[\]})]*$/)) {
specialRule = 'end-brace';
} else if (text.match(/^[|]*$/)) {
specialRule = 'neut-brace';
}
// bigger text for math ops
this.classList.remove('mo-bigger');
if (text && (0x2200 <= text.charCodeAt(0)) && (text.charCodeAt(0) <= 0x2233)) {
if ((getComputedStyle(this).getPropertyValue('--math-style-level') || '').trim() !== 'sub') {
this.classList.add('mo-bigger');
}
}
const newFormStyle = specialRule || 'infix';
if (this.formStyle !== newFormStyle) {
if (this.formStyle) {
this.classList.remove(this.formStyle);
}
this.formStyle = `mo-${newFormStyle}`;
this.classList.add(this.formStyle);
}
let effectiveStretch = this.stretchy && this.stretchy.trim().toLowerCase() === 'true';
if (!this.stretchy) {
if (getComputedStyle(this).getPropertyValue('--math-style-stretchy').trim() === 'true') {
effectiveStretch = true;
} else {
effectiveStretch = specialRule === 'begin-brace' || specialRule === 'end-brace' || specialRule === 'neut-brace';
}
}
span.style.width = null;
if (!effectiveStretch) {
span.classList.add('fullWidthSpan');
this.classList.remove('mo-stretchy');
span.style.transform = null;
span.style.lineHeight = null;
span.classList.remove('invisible');
} else {
span.classList.remove('fullWidthSpan');
this.classList.add('mo-stretchy');
span.style.lineHeight = '1';
setTimeout(() => {
if (span.style.transform) {
return;
}
span.classList.remove('invisible');
const spanSize = span.getBoundingClientRect();
const size = this.getBoundingClientRect();
const scaleY = spanSize.height ? (size.height / spanSize.height) : 1;
const scaleX = spanSize.width ? (size.width / spanSize.width) : 1;
if (scaleY <= 1) {
span.style.lineHeight = null;
}
if (scaleX !== 1) {
span.style.width = '100%';
}
span.style.transform = `scale(${scaleX}, ${scaleY})`;
}, 50);
}
}
}