-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathnormalizeMarkdownInputStyle.ts
More file actions
80 lines (72 loc) · 1.92 KB
/
normalizeMarkdownInputStyle.ts
File metadata and controls
80 lines (72 loc) · 1.92 KB
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
import { processColor, type ColorValue } from 'react-native';
import type { MarkdownInputStyle } from './EnrichedMarkdownInput';
import { normalizeColor } from './styleUtils';
interface MarkdownInputStyleInternal {
strong: {
color?: ColorValue;
};
em: {
color?: ColorValue;
};
link: {
color: ColorValue;
underline: boolean;
};
spoiler: {
color: ColorValue;
backgroundColor: ColorValue;
};
}
const DEFAULT_LINK_COLOR = '#2563EB';
const DEFAULT_SPOILER_COLOR = '#374151';
const DEFAULT_SPOILER_BG_COLOR = '#E5E7EB';
const defaultInternal: MarkdownInputStyleInternal = Object.freeze({
strong: {
color: undefined,
},
em: {
color: undefined,
},
link: {
color: processColor(DEFAULT_LINK_COLOR)!,
underline: true,
},
spoiler: {
color: processColor(DEFAULT_SPOILER_COLOR)!,
backgroundColor: processColor(DEFAULT_SPOILER_BG_COLOR)!,
},
});
let cachedInput: MarkdownInputStyle | undefined;
let cachedResult: MarkdownInputStyleInternal | undefined;
export const normalizeMarkdownInputStyle = (
style?: MarkdownInputStyle
): MarkdownInputStyleInternal => {
if (!style || Object.keys(style).length === 0) {
return defaultInternal;
}
if (style === cachedInput && cachedResult) {
return cachedResult;
}
const result: MarkdownInputStyleInternal = {
strong: {
color: normalizeColor(style.strong?.color),
},
em: {
color: normalizeColor(style.em?.color),
},
link: {
color: normalizeColor(style.link?.color) ?? defaultInternal.link.color,
underline: style.link?.underline ?? defaultInternal.link.underline,
},
spoiler: {
color:
normalizeColor(style.spoiler?.color) ?? defaultInternal.spoiler.color,
backgroundColor:
normalizeColor(style.spoiler?.backgroundColor) ??
defaultInternal.spoiler.backgroundColor,
},
};
cachedInput = style;
cachedResult = result;
return result;
};