Skip to content

Commit 06ba3e8

Browse files
committed
New: First commit
0 parents  commit 06ba3e8

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

Configuration/Settings.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Neos:
2+
Neos:
3+
fusion:
4+
autoInclude:
5+
Garagist.Fontawesome.AlpineJS: true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
prototype(Garagist.Fontawesome:Component.Icon) {
2+
tagName = 'i'
3+
4+
iconParts = ${Garagist.Fontawesome.parts(this.style, this.icon)}
5+
content >
6+
@if.hasContent = ${this.iconParts}
7+
8+
renderer.overwriteAttributes.x-data = ${AlpineJS.xData('icon', props.iconParts)}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: **/*.fusion

Resources/Private/Main.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import lscache from "lscache";
2+
3+
function replaceTag(element, markup) {
4+
const attributes = [...element.attributes]
5+
.filter((item) => item.name != "x-data")
6+
.map((item) => [item.name, item.value]);
7+
const div = document.createElement("div");
8+
div.innerHTML = markup;
9+
const newElement = div.firstElementChild;
10+
attributes.forEach(([name, value]) => {
11+
newElement.setAttribute(name, value);
12+
});
13+
element.replaceWith(newElement);
14+
}
15+
16+
window.addEventListener("alpine:init", () => {
17+
window.Alpine.data("icon", (segment) => ({
18+
init() {
19+
lscache.setBucket("icons");
20+
const cache = lscache.get(segment);
21+
if (cache && typeof cache === "string") {
22+
replaceTag(this.$el, cache);
23+
return;
24+
}
25+
fetch(`/garagist/fontawesome/${segment.replaceAll(":", "/")}.svg`)
26+
.then((response) => {
27+
if (!response.ok) {
28+
throw new Error("Failed to load icon");
29+
}
30+
return response.text();
31+
})
32+
.then((data) => {
33+
// Store for 1 week 60 min * 24 hours * 7 days = 10080
34+
lscache.set(segment, data, 10080);
35+
replaceTag(this.$el, data);
36+
})
37+
.catch((error) => {
38+
console.warn(error);
39+
this.$el.remove();
40+
});
41+
},
42+
}));
43+
});

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "garagist/fontawesome-alpinejs",
3+
"description": "Fetch fontawesome icons with AlpineJS. Cached with LocalStorage",
4+
"type": "neos-plugin",
5+
"license": "GPL-3.0-or-later",
6+
"require": {
7+
"garagist/fontawesome": "^0.1"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"Garagist\\Fontawesome\\AlpineJS\\": "Classes/"
12+
}
13+
},
14+
"extra": {
15+
"neos": {
16+
"package-key": "Garagist.Fontawesome.AlpineJS"
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)