-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Decompose imageCreator into separate module
- Loading branch information
Showing
2 changed files
with
39 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const getFileExtension = filePath => filePath.split('.').pop(); | ||
|
||
const imageCreator = { | ||
|
||
imgBasePath: { | ||
local: "img/", | ||
external: "https://raw.githubusercontent.com/astik-dev/portfolio/content/images/", | ||
}, | ||
|
||
px1: `img/1x1.png`, | ||
|
||
fullPath: function (source, path) {return this.imgBasePath[source] + path}, | ||
|
||
newWebpPic: function (source, webpPath, fallbackPath, alt, lazy) { | ||
const types = { | ||
jpg: "jpeg", | ||
} | ||
|
||
const src = lazy ? "data-src" : "srcset"; | ||
|
||
const fallbackExt = getFileExtension(fallbackPath); | ||
const fallbackType = types[fallbackExt] || fallbackExt; | ||
|
||
return `<picture> | ||
<source type="image/webp" ${src}="${this.fullPath(source, webpPath)}"> | ||
<source type="image/${fallbackType}" ${src}="${this.fullPath(source, fallbackPath)}"> | ||
${this.newImg(source, fallbackPath, alt, lazy)} | ||
</picture>`; | ||
}, | ||
|
||
newImg: function (source, path, alt, lazy) { | ||
const fullPath = this.fullPath(source, path); | ||
const src = lazy ? `"${this.px1}" data-src="${fullPath}"` : `"${fullPath}"`; | ||
return `<img src=${src} alt="${alt}">`; | ||
} | ||
} | ||
|
||
export default imageCreator; |