-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmsqrt.ts
75 lines (71 loc) · 1.95 KB
/
msqrt.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
import { MathMLElement, PropertyValues, html, TemplateResult, customElement } from './mathml-element.js';
import { HorizFlex } from './styles/common-styles.js';
@customElement('math-sqrt')
export class MathSqrtElement extends MathMLElement {
render(): TemplateResult {
return html`
<style>
${HorizFlex}
:host {
display: inline-block;
vertical-align: bottom;
align-self: center;
color: var(--math-color, inherit);
background: var(--math-background, inherit);
}
.msqrtContent {
padding: 1px 0.05em 0 0.2em;
border-top: solid thin;
white-space: nowrap;
}
#msqrtGlyphSpan {
width: 1.1em;
position: relative;
}
svg {
position: absolute;
left: 0;
right: 0;
width: 100%;
height: 100%;
}
path {
fill: none;
stroke: currentColor;
stroke-width: 1;
}
</style>
<div class="horizontal layout">
<div id="msqrtGlyphSpan"><svg><path id="rootPath"></path></svg></div>
<div class="flex msqrtContent"><slot @slotchange="${this.onSlotCange}"></slot></div>
</div>
`;
}
updated(propVals: PropertyValues) {
super.updated(propVals);
this.onSlotCange();
}
private onSlotCange() {
if (!this.shadowRoot) {
return;
}
const slot = this.shadowRoot!.querySelector('slot') as HTMLSlotElement;
if (!slot) {
return;
}
setTimeout(() => {
this.drawRoot();
}, 10);
}
private drawRoot() {
const span = this.shadowRoot!.querySelector('#msqrtGlyphSpan');
const path = this.shadowRoot!.querySelector('#rootPath');
if (span && path) {
const size = (span as HTMLElement).getBoundingClientRect();
const width = size.width;
const height = size.height;
const d = `M0 ${height * 0.55} H${width * 0.13} L${width * 0.45} ${height - 2} L${width} 0`;
path.setAttribute('d', d);
}
}
}