Skip to content

Commit

Permalink
V3 Frontend Asset Tweaks (#1371)
Browse files Browse the repository at this point in the history
* Check for "original" property

* Adjusting order of request validations

* Updates to Asset Injection Methods

* Fix assertViewIs is not working (#67)
Contribs By: Jackson Tong <[email protected]

* Tweaks for Auto Injection Options

* Test Updates - Frontend Assets

* Add ThirdParty Basic Header Tests

* Add Initial Debuggable DTO Test
  • Loading branch information
lrljoe authored Sep 26, 2023
1 parent 62db217 commit 65ce494
Show file tree
Hide file tree
Showing 14 changed files with 504 additions and 197 deletions.
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,

Check warning on line 16 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L16

Added line #L16 was not covered by tests

/**
* 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,

Check warning on line 21 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L21

Added line #L21 was not covered by tests

/**
* 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,

Check warning on line 26 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L26

Added line #L26 was not covered by tests

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

Check warning on line 31 in config/livewire-tables.php

View check run for this annotation

Codecov / codecov/patch

config/livewire-tables.php#L31

Added line #L31 was not covered by tests

/**
* 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;

Check warning on line 36 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L36

Added line #L36 was not covered by tests
}

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

Check warning on line 48 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L48

Added line #L48 was not covered by tests
}

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;

Check warning on line 56 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L56

Added line #L56 was not covered by tests
}

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

Check warning on line 60 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L60

Added line #L60 was not covered by tests
}

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

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

Check warning on line 68 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L66-L68

Added lines #L66 - L68 were not covered by tests
}
});
}

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;

Check warning on line 94 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L94

Added line #L94 was not covered by tests

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')

Check warning on line 98 in src/Features/AutoInjectRappasoftAssets.php

View check run for this annotation

Codecov / codecov/patch

src/Features/AutoInjectRappasoftAssets.php#L97-L98

Added lines #L97 - L98 were not covered by tests
->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

0 comments on commit 65ce494

Please sign in to comment.