-
Notifications
You must be signed in to change notification settings - Fork 47
/
DefaultAligner.js
71 lines (62 loc) · 1.97 KB
/
DefaultAligner.js
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
// @flow
import { Aligner } from './Aligner';
import type { Alignment } from './Alignment';
import type { AlignOptions } from '../../Options';
const LEFT_ALIGN = 'left';
const CENTER_ALIGN = 'center';
const RIGHT_ALIGN = 'right';
export default class DefaultAligner implements Aligner {
alignments: { [string]: Alignment };
alignAttribute: string;
applyStyle: boolean;
constructor(options: AlignOptions) {
this.applyStyle = options.aligner.applyStyle;
this.alignAttribute = options.attribute;
this.alignments = {
[LEFT_ALIGN]: {
name: LEFT_ALIGN,
icon: options.icons.left,
apply: (el: HTMLElement) => {
this.setAlignment(el, LEFT_ALIGN);
this.setStyle(el, 'inline', 'left', '0 1em 1em 0');
},
},
[CENTER_ALIGN]: {
name: CENTER_ALIGN,
icon: options.icons.center,
apply: (el: HTMLElement) => {
this.setAlignment(el, CENTER_ALIGN);
this.setStyle(el, 'block', null, 'auto');
},
},
[RIGHT_ALIGN]: {
name: RIGHT_ALIGN,
icon: options.icons.right,
apply: (el: HTMLElement) => {
this.setAlignment(el, RIGHT_ALIGN);
this.setStyle(el, 'inline', 'right', '0 0 1em 1em');
},
},
};
}
getAlignments(): Alignment[] {
return Object.keys(this.alignments).map(k => this.alignments[k]);
}
clear(el: HTMLElement): void {
el.removeAttribute(this.alignAttribute);
this.setStyle(el, null, null, null);
}
isAligned(el: HTMLElement, alignment: Alignment): boolean {
return el.getAttribute(this.alignAttribute) === alignment.name;
}
setAlignment(el: HTMLElement, value: string) {
el.setAttribute(this.alignAttribute, value);
}
setStyle(el: HTMLElement, display: ?string, float: ?string, margin: ?string) {
if (this.applyStyle) {
el.style.setProperty('display', display);
el.style.setProperty('float', float);
el.style.setProperty('margin', margin);
}
}
}