Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 816f1e6

Browse files
chore(ssr): cleanup 'generateCss()' logic
1 parent eda12c3 commit 816f1e6

File tree

1 file changed

+65
-40
lines changed

1 file changed

+65
-40
lines changed

src/lib/server/server-provider.ts

+65-40
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,6 @@ import {
1919
ServerMatchMedia
2020
} from '@angular/flex-layout';
2121

22-
let nextId = 0;
23-
const IS_DEBUG_MODE = false;
24-
25-
/**
26-
* create @media queries based on a virtual stylesheet
27-
* * Adds a unique class to each element and stores it
28-
* in a shared classMap for later reuse
29-
* @param stylesheet the virtual stylesheet that stores styles for each
30-
* element
31-
* @param mediaQuery the given @media CSS selector for the current breakpoint
32-
* @param classMap the map of HTML elements to class names to avoid duplications
33-
*/
34-
function generateCss(stylesheet: Map<HTMLElement, Map<string, string|number>>,
35-
mediaQuery: string,
36-
classMap: Map<HTMLElement, string>) {
37-
let styleText = IS_DEBUG_MODE ? `
38-
@media ${mediaQuery} {` : `@media ${mediaQuery}{`;
39-
stylesheet.forEach((styles, el) => {
40-
let className = classMap.get(el);
41-
if (!className) {
42-
className = `${CLASS_NAME}${nextId++}`;
43-
classMap.set(el, className);
44-
}
45-
el.classList.add(className);
46-
styleText += IS_DEBUG_MODE ? `
47-
.${className} {` : `.${className}{`;
48-
styles.forEach((v, k) => {
49-
if (v) {
50-
styleText += IS_DEBUG_MODE ? `
51-
${k}: ${v};` : `${k}:${v};`;
52-
}
53-
});
54-
styleText += IS_DEBUG_MODE ? `
55-
}` : '}';
56-
});
57-
styleText += IS_DEBUG_MODE ? `
58-
}\n` : '}';
59-
60-
return styleText;
61-
}
6222

6323
/**
6424
* Activate all of the registered breakpoints in sequence, and then
@@ -139,3 +99,68 @@ export const SERVER_PROVIDERS = [
13999
useClass: ServerMatchMedia
140100
}
141101
];
102+
103+
104+
let nextId = 0;
105+
const IS_DEBUG_MODE = false;
106+
107+
export type StyleSheet = Map<HTMLElement, Map<string, string|number>>;
108+
export type ClassMap = Map<HTMLElement, string>;
109+
110+
/**
111+
* create @media queries based on a virtual stylesheet
112+
* * Adds a unique class to each element and stores it
113+
* in a shared classMap for later reuse
114+
* @param stylesheet the virtual stylesheet that stores styles for each
115+
* element
116+
* @param mediaQuery the given @media CSS selector for the current breakpoint
117+
* @param classMap the map of HTML elements to class names to avoid duplications
118+
*/
119+
function generateCss(stylesheet: StyleSheet, mediaQuery: string, classMap: ClassMap) {
120+
let css = '';
121+
stylesheet.forEach((styles, el) => {
122+
let keyVals = '', className = getClassName(el, classMap);
123+
124+
styles.forEach((v, k) => {
125+
keyVals += v ? format(`${k}:${v};`) : '';
126+
});
127+
128+
// Build list of CSS styles; each with a className
129+
css += format(`.${className} {`, keyVals, '}');
130+
});
131+
132+
// Group 1 or more styles (each with className) in a specific mediaQuery
133+
return format(`@media ${mediaQuery} {`, css, '}');
134+
}
135+
136+
/**
137+
* For debugging purposes, prefix css segment with linefeed(s) for easy
138+
* debugging purposes.
139+
*/
140+
function format(...list: string[]): string {
141+
let result = '';
142+
list.forEach((css, i) => {
143+
result += IS_DEBUG_MODE ? formatSegment(css, i != 0) : css;
144+
});
145+
return result;
146+
}
147+
148+
function formatSegment(css: string, asPrefix: boolean = true): string {
149+
return asPrefix ? '\n' + css : css + '\n';
150+
}
151+
152+
/**
153+
* Get className associated with CSS styling
154+
* If not found, generate global className and set
155+
* association.
156+
*/
157+
function getClassName(stylesheet, classMap) {
158+
let className = classMap.get(stylesheet);
159+
if (!className) {
160+
className = `${CLASS_NAME}${nextId++}`;
161+
classMap.set(stylesheet, className);
162+
}
163+
stylesheet.classList.add(className);
164+
165+
return className;
166+
}

0 commit comments

Comments
 (0)