forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockNoteTheme.ts
More file actions
153 lines (142 loc) · 3.61 KB
/
BlockNoteTheme.ts
File metadata and controls
153 lines (142 loc) · 3.61 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
export type CombinedColor = Partial<{
text: string;
background: string;
}>;
export type ColorScheme = Partial<{
editor: CombinedColor;
menu: CombinedColor;
tooltip: CombinedColor;
hovered: CombinedColor;
selected: CombinedColor;
disabled: CombinedColor;
shadow: string;
border: string;
sideMenu: string;
highlights: Partial<{
gray: CombinedColor;
brown: CombinedColor;
red: CombinedColor;
orange: CombinedColor;
yellow: CombinedColor;
green: CombinedColor;
blue: CombinedColor;
purple: CombinedColor;
pink: CombinedColor;
}>;
}>;
export type Theme = Partial<{
colors: ColorScheme;
borderRadius: number;
fontFamily: string;
}>;
type NestedObject = { [key: string]: number | string | NestedObject };
const cssVariablesHelper = (
theme: Theme,
editorDOM: HTMLElement,
unset = false
) => {
const result: string[] = [];
function traverse(current: NestedObject, currentKey = "--bn") {
for (const key in current) {
const kebabCaseKey = key
.replace(/([a-z])([A-Z])/g, "$1-$2")
.toLowerCase();
const fullKey = `${currentKey}-${kebabCaseKey}`;
if (typeof current[key] !== "object") {
// Convert numbers to px
if (typeof current[key] === "number") {
current[key] = `${current[key]}px`;
}
if (unset) {
editorDOM.style.removeProperty(fullKey);
} else {
editorDOM.style.setProperty(fullKey, current[key].toString());
}
} else {
traverse(current[key] as NestedObject, fullKey);
}
}
}
traverse(theme);
return result;
};
export const applyBlockNoteCSSVariablesFromTheme = (
theme: Theme,
editorDOM: HTMLElement
) => cssVariablesHelper(theme, editorDOM);
// We don't need a theme to remove the CSS variables, but having access to a
// theme object allows us to use the same logic to set/unset them, so this
// placeholder theme is used.
const placeholderTheme: Theme = {
colors: {
editor: {
text: undefined as any,
background: undefined as any,
},
menu: {
text: undefined as any,
background: undefined as any,
},
tooltip: {
text: undefined as any,
background: undefined as any,
},
hovered: {
text: undefined as any,
background: undefined as any,
},
selected: {
text: undefined as any,
background: undefined as any,
},
disabled: {
text: undefined as any,
background: undefined as any,
},
shadow: undefined as any,
border: undefined as any,
sideMenu: undefined as any,
highlights: {
gray: {
text: undefined as any,
background: undefined as any,
},
brown: {
text: undefined as any,
background: undefined as any,
},
red: {
text: undefined as any,
background: undefined as any,
},
orange: {
text: undefined as any,
background: undefined as any,
},
yellow: {
text: undefined as any,
background: undefined as any,
},
green: {
text: undefined as any,
background: undefined as any,
},
blue: {
text: undefined as any,
background: undefined as any,
},
purple: {
text: undefined as any,
background: undefined as any,
},
pink: {
text: undefined as any,
background: undefined as any,
},
},
},
borderRadius: undefined as any,
fontFamily: undefined as any,
};
export const removeBlockNoteCSSVariables = (editorDOM: HTMLElement) =>
cssVariablesHelper(placeholderTheme, editorDOM, true);