Skip to content

Commit

Permalink
load fa svg files & make faIconExists efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviiu committed Nov 17, 2023
1 parent 029a159 commit 309d693
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
34 changes: 30 additions & 4 deletions core/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ if(!preloadedConfig) invoke(`getConfig`).then(conf => {
preloadedConfig = conf;
})

const exposedConfiguration = {
contextBridge.exposeInMainWorld(`configuration`, {
action: (name) => invoke(`configAction`, name),
actionUpdate: (key, cb) => on(`configActionUpdate-${key}`, cb),
get: (name) => new Promise(async res => {
Expand All @@ -179,9 +179,7 @@ const exposedConfiguration = {
}),
set: (name, newObj) => invoke(`setConfig`, [name, newObj]),
hook: (cb) => configHooks.push(cb)
}

contextBridge.exposeInMainWorld(`configuration`, exposedConfiguration);
});

contextBridge.exposeInMainWorld(`notifications`, {
handler: (callback) => on(`notification`, (_e, content) => callback(content)),
Expand Down Expand Up @@ -230,6 +228,8 @@ contextBridge.exposeInMainWorld(`preload`, {
oncomplete: (cb) => script.addEventListener(`load`, cb)
});

let fa = null;

const scriptsObj = {
libs: () => new Promise(async res => {
addScript(`./lib.js`).then(res).catch(async e => {
Expand Down Expand Up @@ -272,6 +272,32 @@ const scriptsObj = {
});
});
}),
misc: {
get fa() {
if(!fa) {
const files = {
fab: 'fa-brands-400.svg',
far: 'fa-regular-400.svg',
fas: 'fa-solid-900.svg',
};

for(const file of Object.keys(files)) {
const filedata = fs.readFileSync(require(`path`).join(__dirname, `../html/assets/fonts/${files[file]}`), `utf8`);

const o = document.createElement(`div`);
o.innerHTML = filedata;

console.log(`svg (${file})`, o)

files[file] = o.firstElementChild.lastChild.firstElementChild;
};

fa = files;
};

return fa;
}
},
}

contextBridge.exposeInMainWorld(`scripts`, scriptsObj);
Expand Down
48 changes: 24 additions & 24 deletions html/topjs/faIconExists.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
var faIconExists = (faType, name, returnIcon, iconStyle) => {
const tempElement = document.createElement(`i`);

tempElement.classList.add(`${faType}`);
tempElement.classList.add(`fa-${name}`);

tempElement.style.display = `none`;
tempElement.style.position = `absolute`

document.body.appendChild(tempElement);

const exists = window.getComputedStyle(tempElement, '::before').getPropertyValue('content') != `none`;

tempElement.remove();

if(returnIcon && exists) {
tempElement.style.display = ``;
tempElement.style.position = ``;

if(iconStyle) for(const key of Object.keys(iconStyle)) {
tempElement.style[key] = iconStyle[key];
}

return tempElement;
} else return exists;
if(faType && typeof faType == `string`) {
const exists = scripts.misc.fa[faType]?.querySelector(`glyph[glyph-name="${name}"]`) ? true : false;

if(exists && returnIcon) {
const tempElement = document.createElement(`i`);
console.log(`objToDOM: icon2 (${faType} / ${name})`)
tempElement.className = `${faType} fa-${name}`;

if(iconStyle) for(const key of Object.keys(iconStyle)) {
tempElement.style[key] = iconStyle[key];
}

return tempElement;
} else return exists;
} else {
const fallbacks = Array.isArray(faType) && faType || [`fas`, `far`, `fab`]

const found = fallbacks.map(f => faIconExists(f, name, returnIcon, iconStyle));

console.log(`objToDOM: icon3 (${faType} / ${name})`, found)

return found.find(Boolean);
}
}

0 comments on commit 309d693

Please sign in to comment.