/
mods.ts
67 lines (54 loc) · 1.34 KB
/
mods.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
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* @module traits
*/
import type {
IComponent,
IContainer,
IDictionary,
IMods,
ModType
} from 'jodit/types';
import { isVoid } from 'jodit/core/helpers/checker/is-void';
export abstract class Mods implements IMods {
abstract mods: IDictionary;
afterSetMod(name: string, value: ModType): void {}
/**
* Set/remove BEM class modification
*
* @param value - if null, mod will be removed
*/
setMod<T extends IComponent & IContainer & IMods>(
this: T,
name: string,
value: ModType,
container?: HTMLElement
): T {
name = name.toLowerCase();
const oldValue = this.mods[name];
if (oldValue === value) {
return this;
}
const mod = `${this.componentName}_${name}_`,
cl = (container || this.container).classList;
if (oldValue != null) {
cl.remove(`${mod}${oldValue.toString().toLowerCase()}`);
}
!isVoid(value) &&
value !== '' &&
cl.add(`${mod}${value.toString().toLowerCase()}`);
this.mods[name] = value;
this.afterSetMod(name, value);
return this;
}
/**
* Get BEM class modification value
*/
getMod(this: IMods, name: string): ModType {
return this.mods[name] ?? null;
}
}