Skip to content

Commit

Permalink
Merge master (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Zubair Slamdien <[email protected]>
Co-authored-by: Zubair Slamdien <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2023
1 parent df51bcf commit 8667f89
Showing 1 changed file with 37 additions and 61 deletions.
98 changes: 37 additions & 61 deletions lib/CourseThemeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ class CourseThemeModule extends AbstractApiModule {
* Writes the customStyle and themeVariables attributes to LESS files. themeVariables are reduced into a string of variables, in the format `@key: value;`
* @param {AdaptFrameworkBuild} fwBuild Reference to the current build
*/
async writeCustomLess(fwBuild) {
const fontImportString = await this.processFileVariables(fwBuild);
async writeCustomLess (fwBuild) {
const fontImportString = await this.processFileVariables(fwBuild)
const { customStyle, [this.attributeKey]: variables, themeCustomStyle } = fwBuild.courseData.course.data

this.processCustomStyling(fwBuild);

const { customStyle, [this.attributeKey]: variables, themeCustomStyle } = fwBuild.courseData.course.data;

return Promise.all([
this.writeFile(fwBuild, '1-variables.less', fontImportString + this.getVariablesString(variables) ),
this.writeFile(fwBuild, '1-variables.less', fontImportString + this.getVariablesString(variables)),
this.writeFile(fwBuild, '2-customStyles.less', customStyle),
this.writeFile(fwBuild, '3-themeCustomStyles.less', themeCustomStyle)
]);
])
}

/**
Expand Down Expand Up @@ -132,72 +133,47 @@ class CourseThemeModule extends AbstractApiModule {
* Copies uploaded font files into the build
* @param {object} data The data to process
*/
async processFileVariables(fwBuild) {
const assets = await this.app.waitForModule('assets');
const fontDest = `${fwBuild.dir}/src/theme/${fwBuild.courseData.config.data._theme}/fonts`;
const fontData = fwBuild.courseData.course.data.themeVariables._font;

if(!fontData) {
return "";
}
let fontImportCSS = "";
async processFileVariables (fwBuild) {
const assets = await this.app.waitForModule('assets')
const fontDest = `${fwBuild.dir}/src/theme/${fwBuild.courseData.config.data._theme}/fonts`
const fontData = fwBuild.courseData.course.data.themeVariables._font
let fontImportCSS = ''

var fontWeightCheck = {
'woff2-light': function () {
return `font-weight: light;\n\tfont-style: normal;`;
},
'woff2-lightItalic': function () {
return `font-weight: light;\n\tfont-style: italic;`;
},
'woff2-normal': function () {
return `font-weight: normal;\n\tfont-style: normal;`;
},
'woff2-normalItalic': function () {
return `font-weight: normal;\n\tfont-style: italic;`;
},
'woff2-bold': function () {
return `font-weight: bold;\n\tfont-style: normal;`;
},
'woff2-boldItalic': function () {
return `font-weight: bold;\n\tfont-style: italic;`;
}
const fontWeightCheck = (font) => {
const weight = font.includes('Italic') ? 'italic' : 'normal'
const style = font.includes('light') ? 'light' : font.includes('bold') ? 'bold' : 'normal'
return `font-weight: ${weight};\n\tfont-style: ${style};`
}
// font files
if(fontData._items) {
// Font files
if (fontData._items) {
await Promise.all(fontData._items.map(async f => {
if(!f["font-family"]) return;
if (!f['font-family']) return
// copy each uploaded font file
for (const font in f._files) {
const [data] = await assets.find({ _id: f._files[font] });
const asset = assets.createFsWrapper(data);
const [data] = await assets.find({ _id: f._files[font] })
const asset = assets.createFsWrapper(data)
try {
await fs.writeFile(`${fontDest}/${f._files[font]}.${asset.data.subtype}`, await asset.read());
await fs.writeFile(`${fontDest}/${f._files[font]}.${asset.data.subtype}`, await asset.read())
// construct font import styling
fontImportCSS += `@font-face {\n\tfont-family: ${f["font-family"]};\n\tsrc: url('./fonts/${f._files[font]}.${asset.data.subtype}') format('${asset.data.subtype}');\n\t${fontWeightCheck[font]()}\n}\n`;
} catch(e) {
this.log('error', `failed to write ${f._files[font]}, ${e.message}`);
fontImportCSS += `@font-face {\n\tfont-family: ${f['font-family']};\n\tsrc: url('./fonts/${f._files[font]}.${asset.data.subtype}') format('${asset.data.subtype}');\n\t${fontWeightCheck[font]()}\n}\n`
} catch (e) {
this.log('error', `failed to write ${f._files[font]}, ${e.message}`)
}
}
}));
delete fontData._items;
}
// instruction style checkbox
const _instruction = fontData._fontAssignments._instruction;
if(_instruction["instruction-font-style"]) {
_instruction["instruction-font-style"] = _instruction._isInstructionItalic ? 'italic' : 'normal';
delete _instruction._isInstructionItalic;
}
// external fonts
if(fontData._externalFonts) {
fontData._externalFonts.forEach(f => {
if(f) fontImportCSS += `@import url('${f}');\n`;
});
delete fontData._externalFonts;
}))
delete fontData._items
}

return fontImportCSS;
const instruction = fontData._fontAssignments._instruction
// Instruction style checkbox
instruction['instruction-font-style'] = instruction._isInstructionItalic ? 'italic' : 'normal'
delete instruction._isInstructionItalic
// External fonts
fontData._externalFonts?.forEach(f => {
if (f) fontImportCSS += `@import url('${f}');\n`
})
return fontImportCSS
}

processCustomStyling(fwBuild) {
const customStyling = fwBuild.courseData.course.data.themeVariables.themeCustomStyle;

Expand Down

0 comments on commit 8667f89

Please sign in to comment.