From bf4b0c2b5174fb0d0624dd9b39da2e915d8a3951 Mon Sep 17 00:00:00 2001 From: Patrick Boivin Date: Fri, 10 Dec 2021 17:36:42 -0500 Subject: [PATCH 1/3] feat: Support vendor form field partials --- src/TwillServiceProvider.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/TwillServiceProvider.php b/src/TwillServiceProvider.php index fa202ab90..4c789f2dc 100644 --- a/src/TwillServiceProvider.php +++ b/src/TwillServiceProvider.php @@ -329,6 +329,8 @@ private function registerCommands() } /** + * Resolve and include a given view expression in the project, Twill internals or a package. + * * @param string $view * @param string $expression * @return string @@ -337,7 +339,13 @@ private function includeView($view, $expression) { [$name] = str_getcsv($expression, ',', '\''); - $partialNamespace = view()->exists('admin.' . $view . $name) ? 'admin.' : 'twill::'; + if (preg_match('/::/', $name)) { + // if there's a namespace separator, we'll assume it's a package + [$namespace, $name] = preg_split('/::/', $name); + $partialNamespace = "$namespace::admin."; + } else { + $partialNamespace = view()->exists('admin.' . $view . $name) ? 'admin.' : 'twill::'; + } $view = $partialNamespace . $view . $name; From b0373d9afa4026984d369ce2b1caaeb8a6e79340 Mon Sep 17 00:00:00 2001 From: Patrick Boivin Date: Fri, 10 Dec 2021 17:37:11 -0500 Subject: [PATCH 2/3] feat: Support vendor custom Vue components --- .gitignore | 2 ++ config/twill.php | 1 + frontend/js/components/customs-vendor/.keep | 0 frontend/js/main-form.js | 7 ++++++ src/Commands/Build.php | 24 +++++++++++++++++++++ 5 files changed, 34 insertions(+) create mode 100644 frontend/js/components/customs-vendor/.keep diff --git a/.gitignore b/.gitignore index a0cb62aaa..92397cf96 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ assets/dist/* frontend/js/components/blocks/customs/* frontend/js/components/customs/* !frontend/js/components/customs/.keep +frontend/js/components/customs-vendor/* +!frontend/js/components/customs-vendor/.keep .DS_Store public/ tests/coverage diff --git a/config/twill.php b/config/twill.php index 286bb7e31..d3adc70d2 100644 --- a/config/twill.php +++ b/config/twill.php @@ -181,6 +181,7 @@ 'manifest_file' => 'twill-manifest.json', 'vendor_path' => 'vendor/area17/twill', 'custom_components_resource_path' => 'assets/js/components', + 'vendor_components_resource_path' => 'assets/vendor/js/components', 'build_timeout' => 300, 'internal_icons' => [ 'content-editor.svg', diff --git a/frontend/js/components/customs-vendor/.keep b/frontend/js/components/customs-vendor/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/frontend/js/main-form.js b/frontend/js/main-form.js index 0252f3f0a..4caed18d8 100644 --- a/frontend/js/main-form.js +++ b/frontend/js/main-form.js @@ -140,6 +140,13 @@ importedComponents.keys().map(block => { return Vue.component(componentName, importedComponents(block).default) }) +// Vendor form components +const importedVendorComponents = require.context('@/components/customs-vendor/', true, /\.(js|vue)$/i) +importedVendorComponents.keys().map(block => { + const componentName = extractComponentNameFromContextKey(block) + return Vue.component(componentName, importedVendorComponents(block).default) +}) + /* eslint-disable no-new */ /* eslint no-unused-vars: "off" */ window[process.env.VUE_APP_NAME].vm = window.vm = new Vue({ diff --git a/src/Commands/Build.php b/src/Commands/Build.php index d5dfeddcd..7ff92ccb6 100644 --- a/src/Commands/Build.php +++ b/src/Commands/Build.php @@ -84,6 +84,9 @@ private function fullBuild() $this->copyComponents(); sleep(1); + $this->copyVendorComponents(); + sleep(1); + $this->info(''); $progressBar->setMessage("Building assets...\n\n"); $progressBar->advance(); @@ -208,9 +211,30 @@ private function copyComponents() } $this->filesystem->cleanDirectory($twillCustomComponentsPath); + $this->filesystem->put($twillCustomComponentsPath . '/.keep', ''); if ($this->filesystem->exists($localCustomComponentsPath)) { $this->filesystem->copyDirectory($localCustomComponentsPath, $twillCustomComponentsPath); } } + + /** + * @return void + */ + private function copyVendorComponents() + { + $localVendorComponentsPath = resource_path(config('twill.vendor_components_resource_path', 'assets/vendor/js/components')); + $twillVendorComponentsPath = base_path(config('twill.vendor_path')) . '/frontend/js/components/customs-vendor'; + + if (!$this->filesystem->exists($twillVendorComponentsPath)) { + $this->filesystem->makeDirectory($twillVendorComponentsPath, 0755, true); + } + + $this->filesystem->cleanDirectory($twillVendorComponentsPath); + $this->filesystem->put($twillVendorComponentsPath . '/.keep', ''); + + if ($this->filesystem->exists($localVendorComponentsPath)) { + $this->filesystem->copyDirectory($localVendorComponentsPath, $twillVendorComponentsPath); + } + } } From 127aef117b58373aabae47d7cd9a3212900992cf Mon Sep 17 00:00:00 2001 From: Patrick Boivin Date: Fri, 17 Dec 2021 17:24:52 -0500 Subject: [PATCH 3/3] Add PackageServiceProvider --- src/PackageServiceProvider.php | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/PackageServiceProvider.php diff --git a/src/PackageServiceProvider.php b/src/PackageServiceProvider.php new file mode 100644 index 000000000..d4749d8c3 --- /dev/null +++ b/src/PackageServiceProvider.php @@ -0,0 +1,41 @@ +publishes( + [$path => resource_path(config('twill.vendor_components_resource_path'))], + 'components' + ); + } + + protected function registerBlocksDirectory($path) + { + $blocks = Config::get('twill.block_editor.directories.source.blocks'); + + $blocks[] = [ + 'path' => $path, + 'source' => 'vendor', + ]; + + Config::set('twill.block_editor.directories.source.blocks', $blocks); + } + + protected function registerRepeatersDirectory($path) + { + $repeaters = Config::get('twill.block_editor.directories.source.repeaters'); + + $repeaters[] = [ + 'path' => $path, + 'source' => 'vendor', + ]; + + Config::set('twill.block_editor.directories.source.repeaters', $repeaters); + } +}