Skip to content

Commit aba0821

Browse files
authored
Big performance gains
1 parent 261f1e8 commit aba0821

File tree

10 files changed

+96
-32
lines changed

10 files changed

+96
-32
lines changed

resources/dist/js/cp.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"/js/cp.js": "/js/cp.js?id=a521fe1077c471aea50c"
2+
"/js/cp.js": "/js/cp.js?id=a687ae0d0e3f52f48703"
33
}

resources/js/FontAwesome.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<div :class="this.classes">
2+
<div :class="this.classes" v-if="icons">
33
<v-select
44
ref="input"
55
:name="name"
@@ -23,7 +23,7 @@
2323
</template>
2424

2525
<template #list-footer>
26-
<span v-show="hasNextPage" ref="load" />
26+
<span class="chrome-fix" v-show="hasNextPage" ref="load" />
2727
</template>
2828
</v-select>
2929
</div>
@@ -48,12 +48,18 @@ export default {
4848
},
4949
5050
computed: {
51+
icons() {
52+
return this.$store.state.publish.fontAwesome.icons
53+
},
54+
5155
classes() {
5256
return `font-awesome ${this.meta.license} version-${this.meta.version.charAt(0)}`
5357
},
5458
5559
filtered() {
56-
return this.meta.icons.filter((icon) => icon.label.toLowerCase().includes(this.search.toLowerCase()))
60+
return this.icons
61+
.filter((icon) => icon.label.toLowerCase().includes(this.search.toLowerCase()))
62+
.filter((icon) => this.meta.styles.includes(icon.style))
5763
},
5864
5965
paginated() {
@@ -136,4 +142,9 @@ export default {
136142
font-family: "Font Awesome 6 Brands" !important;
137143
}
138144
145+
.chrome-fix {
146+
display: inline-block;
147+
height: 1px;
148+
}
149+
139150
</style>

resources/js/cp.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import './store'
12
import FontAwesome from './FontAwesome.vue'
23

34
Statamic.$components.register('font_awesome-fieldtype', FontAwesome)
5+
6+
Statamic.booted(() => {
7+
Statamic.$store.dispatch("publish/fontAwesome/fetchIcons")
8+
})

resources/js/store.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Statamic.$store.registerModule(['publish', 'fontAwesome'], {
2+
3+
namespaced: true,
4+
5+
state: {
6+
icons: null,
7+
},
8+
9+
getters: {
10+
icons: state => state.icons,
11+
},
12+
13+
actions: {
14+
fetchIcons({ commit }) {
15+
return Statamic.$request.post(`/!/font-awesome/icons`)
16+
.then(response => commit('setIcons', response.data))
17+
.catch(function (error) {
18+
console.log(error);
19+
});
20+
},
21+
},
22+
23+
mutations: {
24+
setIcons(state, icons) {
25+
state.icons = icons;
26+
},
27+
}
28+
29+
})

routes/actions.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Aerni\FontAwesome\FontAwesomeController;
4+
use Illuminate\Support\Facades\Route;
5+
6+
Route::name('font-awesome.')->group(function () {
7+
Route::post('/icons', [FontAwesomeController::class, 'index'])->name('font-awesome.icons');
8+
});

src/FontAwesome.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Aerni\FontAwesome;
44

5-
use Illuminate\Support\Str;
65
use Illuminate\Support\Collection;
7-
use Illuminate\Support\Facades\Http;
86
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Str;
99

1010
class FontAwesome
1111
{
@@ -25,9 +25,9 @@ public function all(): Collection
2525
return $this->icons()->flatten(1)->sortBy('id')->values();
2626
}
2727

28-
public function get(string|array $style): Collection
28+
public function icon(string $icon): ?array
2929
{
30-
return $this->icons()->only($style)->flatten(1)->sortBy('id')->values();
30+
return $this->all()->firstWhere('class', $icon);
3131
}
3232

3333
public function styles(): Collection
@@ -44,7 +44,7 @@ public function kit(string $token = null): Collection
4444
return Cache::rememberForever("font_awesome::kit::{$this->kitToken}", function () {
4545
$response = Http::withToken($this->authToken())
4646
->post('https://api.fontawesome.com', [
47-
'query' => $this->kitQuery()
47+
'query' => $this->kitQuery(),
4848
])->json()['data']['me']['kit'];
4949

5050
return collect([
@@ -61,20 +61,20 @@ protected function icons(): Collection
6161
{
6262
return Cache::rememberForever('font_awesome::icons', function () {
6363
$response = Http::post('https://api.fontawesome.com', [
64-
'query' => $this->iconsQuery()
65-
])->json()['data']['release']['icons'];
64+
'query' => $this->iconsQuery(),
65+
])->json()['data']['release']['icons'];
6666

6767
$icons = collect($response)->flatMap(function ($icon) {
6868
// The styles available for the license type of the kit.
6969
$styles = $icon['membership'][$this->kit()->get('license')];
7070

7171
return collect($styles)->map(function ($style) use ($icon) {
7272
return [
73-
'style' => $style,
74-
'id' => "{$icon['id']}-{$style}",
75-
'label' => $icon['label'] . " ($style)",
76-
'class' => $this->iconClass($icon['id'], $style)
77-
];
73+
'style' => $style,
74+
'id' => "{$icon['id']}-{$style}",
75+
'label' => $icon['label']." ($style)",
76+
'class' => $this->iconClass($icon['id'], $style),
77+
];
7878
})->toArray();
7979
})->groupBy('style');
8080

@@ -92,7 +92,7 @@ protected function uploadedIcons(): Collection
9292
return [
9393
'style' => 'uploaded',
9494
'id' => "{$icon['name']}-uploaded",
95-
'label' => Str::title($icon['name']) . " (uploaded)",
95+
'label' => Str::title($icon['name']).' (uploaded)',
9696
'class' => "fak fa-{$icon['name']}",
9797
];
9898
})->sortBy('id');
@@ -101,7 +101,7 @@ protected function uploadedIcons(): Collection
101101
protected function iconClass(string $icon, string $style): string
102102
{
103103
return Str::startsWith($this->kit()->get('version'), '5')
104-
? 'fa' . substr($style, 0, 1) . ' fa-' . $icon
104+
? 'fa'.substr($style, 0, 1).' fa-'.$icon
105105
: "fa-$style fa-$icon";
106106
}
107107

@@ -124,11 +124,11 @@ protected function iconsQuery(): string
124124
{
125125
return
126126
'query {
127-
release (version:' . '"' . $this->kit()->get('version') . '"' . ') {
127+
release (version:'.'"'.$this->kit()->get('version').'"'.') {
128128
icons {
129129
label
130130
id
131-
membership {' . $this->kit()->get('license') . '}
131+
membership {'.$this->kit()->get('license').'}
132132
}
133133
}
134134
}';
@@ -139,11 +139,11 @@ protected function kitQuery(): string
139139
return
140140
'query {
141141
me {
142-
kit (token:' . '"' . $this->kitToken . '"' . ') {
142+
kit (token:'.'"'.$this->kitToken.'"'.') {
143143
token
144144
licenseSelected
145145
version
146-
iconUploads {' . "name" . '}
146+
iconUploads {'.'name'.'}
147147
}
148148
}
149149
}';

src/FontAwesomeController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Aerni\FontAwesome;
4+
5+
use Facades\Aerni\FontAwesome\FontAwesome;
6+
7+
class FontAwesomeController
8+
{
9+
public function index()
10+
{
11+
return response()->json(FontAwesome::all());
12+
}
13+
}

src/FontAwesomeFieldtype.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Aerni\FontAwesome;
44

5-
use Statamic\Fields\Fieldtype;
65
use Facades\Aerni\FontAwesome\FontAwesome;
6+
use Statamic\Fields\Fieldtype;
77

88
class FontAwesomeFieldtype extends Fieldtype
99
{
@@ -28,22 +28,16 @@ protected function configFieldItems(): array
2828

2929
public function preload(): array
3030
{
31-
$icons = $this->config('styles')
32-
? FontAwesome::get($this->config('styles'))
33-
: FontAwesome::all();
34-
3531
return [
36-
'icons' => $icons,
32+
'styles' => $this->config('styles') ?? FontAwesome::styles(),
3733
'license' => FontAwesome::kit()->get('license'),
3834
'version' => FontAwesome::kit()->get('version'),
3935
];
4036
}
4137

4238
public function preProcess($data): ?array
4339
{
44-
return $this->preload()['icons']->first(function ($icon) use ($data) {
45-
return $icon['class'] === $data;
46-
});
40+
return $data ? FontAwesome::icon($data) : null;
4741
}
4842

4943
public function process($data): ?string

src/ServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class ServiceProvider extends AddonServiceProvider
1515
FontAwesomeTags::class,
1616
];
1717

18+
protected $routes = [
19+
'actions' => __DIR__.'/../routes/actions.php',
20+
];
21+
1822
protected $scripts = [
1923
__DIR__.'/../resources/dist/js/cp.js',
2024
];

0 commit comments

Comments
 (0)