-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmrow.ts
85 lines (83 loc) · 2.46 KB
/
mrow.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
import { MathMLElement, html, TemplateResult, customElement } from './mathml-element.js';
import { HorizFlex } from './styles/common-styles.js';
@customElement('math-row')
export class MathRowElement extends MathMLElement {
render(): TemplateResult {
return html`
<style>
${HorizFlex}
:host {
display: inline-block;
color: var(--math-color, inherit);
background: var(--math-background, inherit);
}
.layout.horizontal {
align-items: baseline;
}
.layout.horizontal.centered {
align-items: center;
}
.layout.horizontal.justified {
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
}
</style>
<div id="mrowPanel" class="horizontal layout"><slot @slotchange="${this.onSlotChange}"></slot></div>
`;
}
private onSlotChange() {
if (!this.shadowRoot) {
return;
}
const slot = this.shadowRoot!.querySelector('slot') as HTMLSlotElement;
const panel = this.shadowRoot.querySelector('#mrowPanel') as HTMLElement;
if (!slot || !panel) {
return;
}
panel.classList.remove('centered');
const nodes = slot.assignedNodes().filter((d) => d.nodeType === Node.ELEMENT_NODE);
let opCount = 0;
let centeringNodeCount = 0;
let center = false;
for (let i = 0; i < nodes.length; i++) {
const text = (nodes[i].textContent || '').trim();
if (text === '=') {
center = true;
break;
}
const tagName = (nodes[i] as HTMLElement).tagName.toLowerCase();
switch (tagName) {
case 'math-underover':
case 'math-under':
case 'math-over':
case 'math-subsup':
centeringNodeCount++;
break;
case 'math-o':
opCount++;
break;
case 'math-table':
center = true;
break;
default:
break;
}
}
if (!center) {
if (centeringNodeCount && (!opCount)) {
center = true;
}
}
if (center) {
panel.classList.add('centered');
}
panel.classList.remove('justified');
if ((getComputedStyle(this).getPropertyValue('--math-underover-align') || '').trim() === 'center'
|| (getComputedStyle(this).getPropertyValue('--math-under-align') || '').trim() === 'center'
|| (getComputedStyle(this).getPropertyValue('--math-over-align') || '').trim() === 'center'
) {
panel.classList.add('justified');
}
}
}