Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 Frontend Asset Tweaks #1371

Merged
merged 29 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
81cc1ad
Fix assertViewIs is not working (#67)
lrljoe Sep 24, 2023
288f2b5
Check for "original" property
lrljoe Sep 24, 2023
4239cf6
Fix styling
lrljoe Sep 24, 2023
6a180d1
Adjusting order of request validations
lrljoe Sep 24, 2023
ea14f2e
Fix styling
lrljoe Sep 24, 2023
80bb100
Updates to Asset Injection Methods
lrljoe Sep 24, 2023
710857d
Fix styling
lrljoe Sep 24, 2023
a80e261
Fix ServiceProvider true/false mistake
lrljoe Sep 24, 2023
b1b29ba
Using the app singleton for RappasoftFrontendAssets, adding comments
lrljoe Sep 24, 2023
7237ca8
Fix styling
lrljoe Sep 24, 2023
a38b711
Test fixes
lrljoe Sep 25, 2023
9ff60d6
Fix styling
lrljoe Sep 25, 2023
2ba2c7a
Tweaks for Auto Injection Options
lrljoe Sep 26, 2023
373e81a
Fix styling
lrljoe Sep 26, 2023
cdcc286
Fix all scripts have been rendered checks
lrljoe Sep 26, 2023
1d299eb
Fix errant brackets
lrljoe Sep 26, 2023
5b24875
Test Fixes - Config Variable Changes
lrljoe Sep 26, 2023
34ab660
Test Fixes - AutoInjection
lrljoe Sep 26, 2023
8e445bb
Fix styling
lrljoe Sep 26, 2023
cbb3f60
Test Updates - Frontend Assets
lrljoe Sep 26, 2023
d333442
Fix styling
lrljoe Sep 26, 2023
3b28da1
Add ThirdParty Basic Header Tests
lrljoe Sep 26, 2023
3f0d2da
Fix styling
lrljoe Sep 26, 2023
5a01d69
Add Initial Debuggable DTO Test
lrljoe Sep 26, 2023
0019aab
Fix styling
lrljoe Sep 26, 2023
82dc27f
Fix namespace issue
lrljoe Sep 26, 2023
fc7b694
Fix styling
lrljoe Sep 26, 2023
896506c
Fix missing class
lrljoe Sep 26, 2023
b200fde
Fix styling
lrljoe Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## [Unreleased] - 3.x - Updates to Injection Methods
- Amend AutoInjection/FrontendAsset to ensure it returns the original content correctly
- Remove errant disabling of Blade Directives when disabling auto-injection
- Amended in-line config documentation

## [Unreleased] - 3.x - setSearchFieldAttributes
- Add setSearchFieldAttributes() and getSearchFieldAttributes()

Expand Down
94 changes: 0 additions & 94 deletions README copy.md

This file was deleted.

26 changes: 17 additions & 9 deletions config/livewire-tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@
*/
'theme' => 'tailwind',

'cache_assets' => true,
/**
* Enable or Disable automatic injection of assets
* Filter Frontend Asset Options
*/
'inject_assets' => true,

/**
* Enable or Disable automatic injection of assets
* Cache Rappasoft Frontend Assets
*/
'inject_third_party_assets' => false,
'cache_assets' => false,

/**
* Enable or Disable inclusion of published third-party assets
* Enable or Disable automatic injection of core assets
*/
'published_third_party_assets' => false,
'inject_core_assets_enabled' => true,

/**
* Enable or Disable remote third-party assets
* Enable or Disable automatic injection of third-party assets
*/
'remote_third_party_assets' => true,
'inject_third_party_assets_enabled' => true,

/**
* Enable Blade Directives (Not required if automatically injecting or using bundler approaches)
*/
'enable_blade_directives ' => false,

/**
* Filter Default Configuration Options
*
* */

/**
* Configuration options for DateFilter
Expand Down
2 changes: 2 additions & 0 deletions resources/imports/laravel-livewire-tables-thirdparty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './css/laravel-livewire-tables-thirdparty.css'
import './js/laravel-livewire-tables-thirdparty.min.js'
2 changes: 2 additions & 0 deletions resources/imports/laravel-livewire-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './css/laravel-livewire-tables.css'
import './js/laravel-livewire-tables.js'
104 changes: 56 additions & 48 deletions src/Features/AutoInjectRappasoftAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,61 @@ class AutoInjectRappasoftAssets extends ComponentHook

public static bool $forceAssetInjection = false;

public static bool $shouldInjectRappasoftThirdPartyAssets = false;

public static bool $shouldInjectRappasoftAssets = false;

public static function provide(): void
{
static::$shouldInjectRappasoftAssets = config('livewire-tables.inject_core_assets_enabled', true);
static::$shouldInjectRappasoftThirdPartyAssets = config('livewire-tables.inject_third_party_assets_enabled', true);

on('flush-state', function () {
static::$hasRenderedAComponentThisRequest = false;
static::$forceAssetInjection = false;
});

if (config('livewire-tables.inject_assets', true) === false) {
return;
if (static::$shouldInjectRappasoftAssets || static::$shouldInjectRappasoftThirdPartyAssets) {

app('events')->listen(RequestHandled::class, function (RequestHandled $handled) {

if (! static::$shouldInjectRappasoftAssets && ! static::$shouldInjectRappasoftThirdPartyAssets) {
return;
}

// If All Scripts Have Been Rendered - Return
if (
(
! static::$shouldInjectRappasoftAssets || app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts
) &&
(
! static::$shouldInjectRappasoftThirdPartyAssets || app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts
)
) {
return;
}

if (! str($handled->response->headers->get('content-type'))->contains('text/html')) {
return;
}

if (! method_exists($handled->response, 'status') || ! method_exists($handled->response, 'getContent') || ! method_exists($handled->response, 'setContent') || ! method_exists($handled->response, 'getOriginalContent') || ! property_exists($handled->response, 'original')) {
return;
}

if ($handled->response->status() !== 200) {
return;
}

$html = $handled->response->getContent();

if (str($html)->contains('</html>')) {
$original = $handled->response->getOriginalContent();
$handled->response->setContent(static::injectAssets($html));
$handled->response->original = $original;
}
});
}

app('events')->listen(RequestHandled::class, function ($handled) {

if (! static::$forceAssetInjection && config('livewire-tables.inject_assets', true) === false) {
return;
}
if (! str($handled->response->headers->get('content-type'))->contains('text/html')) {
return;
}
if (! method_exists($handled->response, 'status') || $handled->response->status() !== 200) {
return;
}

if (! method_exists($handled->response, 'getContent') || ! method_exists($handled->response, 'setContent')) {
return;
}

if ((! static::$hasRenderedAComponentThisRequest) && (! static::$forceAssetInjection)) {
return;
}
if (config('livewire-tables.inject_assets', true) === false && config('livewire-tables.inject_third_party_assets', true) === false) {
return;
}
if (config('livewire-tables.inject_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts && config('livewire-tables.inject_third_party_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts) {
return;
} elseif (config('livewire-tables.inject_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableScripts) {
return;
} elseif (config('livewire-tables.inject_third_party_assets', true) === true && app(RappasoftFrontendAssets::class)->hasRenderedRappsoftTableThirdPartyScripts) {
return;
}
$html = $handled->response->getContent();

if (str($html)->contains('</html>')) {
$handled->response->setContent(static::injectAssets($html));
}
});
}

public function dehydrate(): void
Expand All @@ -69,25 +78,24 @@ public function dehydrate(): void

public static function injectAssets(mixed $html): string
{
$rappasoftTableStyles = config('livewire-tables.inject_assets', true) ? RappasoftFrontendAssets::tableStyles() : '';
$rappasoftTableScripts = config('livewire-tables.inject_assets', true) ? RappasoftFrontendAssets::tableScripts() : '';
$rappasoftTableThirdPartyStyles = config('livewire-tables.inject_third_party_assets', true) ? RappasoftFrontendAssets::tableThirdPartyStyles() : '';
//$rappasoftTableThirdPartyStyles = '';
$rappasoftTableThirdPartyScripts = config('livewire-tables.inject_third_party_assets', true) ? RappasoftFrontendAssets::tableThirdPartyScripts() : '';
//$rappasoftTableThirdPartyScripts = '';

$html = str($html);
$rappaStyles = ((static::$shouldInjectRappasoftAssets === true) ? RappasoftFrontendAssets::tableStyles() : '').' '.((static::$shouldInjectRappasoftThirdPartyAssets === true) ? RappasoftFrontendAssets::tableThirdPartyStyles() : '');
$rappaScripts = ((static::$shouldInjectRappasoftAssets === true) ? RappasoftFrontendAssets::tableScripts() : '').' '.((static::$shouldInjectRappasoftThirdPartyAssets === true) ? RappasoftFrontendAssets::tableThirdPartyScripts() : '');

if ($html->test('/<\s*head(?:\s|\s[^>])*>/i') && $html->test('/<\s*\/\s*body\s*>/i')) {
static::$shouldInjectRappasoftAssets = static::$shouldInjectRappasoftThirdPartyAssets = false;

return $html
->replaceMatches('/(<\s*head(?:\s|\s[^>])*>)/i', '$1'.($rappasoftTableStyles.' '.$rappasoftTableThirdPartyStyles))
->replaceMatches('/(<\s*\/\s*head\s*>)/i', ($rappasoftTableScripts.' '.$rappasoftTableThirdPartyScripts).'$1')
->replaceMatches('/(<\s*head(?:\s|\s[^>])*>)/i', '$1'.$rappaStyles)
->replaceMatches('/(<\s*\/\s*head\s*>)/i', $rappaScripts.'$1')
->toString();
}
static::$shouldInjectRappasoftAssets = static::$shouldInjectRappasoftThirdPartyAssets = false;

return $html
->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1'.($rappasoftTableStyles.' '.$rappasoftTableThirdPartyStyles))
->replaceMatches('/(<\s*\/\s*head\s*>)/i', ($rappasoftTableScripts.' '.$rappasoftTableThirdPartyScripts).'$1')
->replaceMatches('/(<\s*html(?:\s[^>])*>)/i', '$1'.$rappaStyles)
->replaceMatches('/(<\s*\/\s*head\s*>)/i', $rappaScripts.'$1')
->toString();
}
}
22 changes: 12 additions & 10 deletions src/LaravelLivewireTablesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function boot(): void

$this->loadViewsFrom(__DIR__.'/../resources/views', 'livewire-tables');

$this->consoleCommands();

(new RappasoftFrontendAssets)->boot();

}

public function consoleCommands()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../resources/lang' => $this->app->langPath('livewire-tables'),
Expand All @@ -45,23 +53,17 @@ public function boot(): void
MakeCommand::class,
]);
}

if (config('livewire-tables.inject_assets', true) === true) {

(new RappasoftFrontendAssets)->boot();
}

}

public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/livewire-tables.php', 'livewire-tables'
);
if (config('livewire-tables.inject_assets', true) === true) {

(new RappasoftFrontendAssets)->register();
ComponentHookRegistry::register(AutoInjectRappasoftAssets::class);
}
(new RappasoftFrontendAssets)->register();

ComponentHookRegistry::register(AutoInjectRappasoftAssets::class);

}
}
Loading