Skip to content

Commit

Permalink
3.2.0 (#32)
Browse files Browse the repository at this point in the history
* Allow baseUrl to be customized for subdirectory-driven installs

* Formatting

* Fix indentation
  • Loading branch information
aaronbushnell authored Jul 12, 2023
1 parent d2eb714 commit 46f4131
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Palette

## 3.2.0 - 2023-07-12

### Added
- Palette now allows the ability to customize the `baseUrl` in the `palette.php` config file. This ensures Palette loads correctly when running Craft in a subdirectory (Ex: https://mysite.com/craft/). ([Discussion](https://github.com/trendyminds/craft-palette/discussions/27))

## 3.1.5 - 2023-05-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trendyminds/craft-palette",
"description": "A command palette to easily jump to specific areas within Craft",
"type": "craft-plugin",
"version": "3.1.5",
"version": "3.2.0",
"keywords": ["palette", "craft", "craft cms", "cmdk", "spotlight", "craft plugin"],
"license": "MIT",
"authors": [
Expand Down
12 changes: 11 additions & 1 deletion scripts/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
return
}

// If a custom baseUrl was set we need to use that. Otherwise use the webroot
let url = '/'

if (window.palette && window.palette.baseUrl) {
url = `${window.palette.baseUrl}/`

// If URL has multiple slashes, remove them all and add a single slash
url = url.replace(/\/+$/, '/')
}

// Run the request to see if the user is authenticated
fetch(`/actions/palette/access`)
fetch(`${url}actions/palette/access`)
.then((res) => {
// If we received anything other than an OK, exit out
if (res.status !== 200) {
Expand Down
12 changes: 11 additions & 1 deletion scripts/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,17 @@ export default function Modal() {
}

useEffect(() => {
fetch('/actions/palette/actions')
// If a custom baseUrl was set we need to use that. Otherwise use the webroot
let url = '/'

if (window.palette && window.palette.baseUrl) {
url = `${window.palette.baseUrl}/`

// If URL has multiple slashes, remove them all and add a single slash
url = url.replace(/\/+$/, '/')
}

fetch(`${url}actions/palette/actions`)
.then((res) => res.json())
.then((data) => {
setOptions(data)
Expand Down
12 changes: 12 additions & 0 deletions src/Palette.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function init()
function (TemplateEvent $event) {
try {
Craft::$app->getView()->registerAssetBundle(AccessBundle::class);

// If the baseUrl was set we need to use that URL in our JS files for requests
// Otherwise, let's skip this part entirely and not try and set window-level properties
if (! is_null($this->getSettings()->baseUrl)) {
Craft::$app->getView()->registerJs(
"
window.palette = window.palette || {};
window.palette.baseUrl = '{$this->getSettings()->baseUrl}'
",
View::POS_HEAD
);
}
} catch (InvalidConfigException $e) {
Craft::error('Error registering Palette asset bundle');
}
Expand Down
5 changes: 5 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ class Settings extends Model
* @var array|callable A custom list of URLs to add to Palette
*/
public $customUrls = [];

/**
* @var ?string If Craft is installed outside of the webroot
*/
public $baseUrl = null;
}
2 changes: 1 addition & 1 deletion src/assetbundles/resources/access.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/assetbundles/resources/init.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@
* }
*/
'customUrls' => [],

/**
* Used if your Craft install is within a subfolder of your webroot
*
* (e.g. http://example.com/mycraft instead of http://example.com)
* 'baseUrl' => '/mycraft',
*
* You likely have an .env variable that you can use here. For example:
* 'baseUrl' => \craft\helpers\App::env('SITE_URL'),
*
* When left as `null` Palette will load scripts from the webroot.
*/
'baseUrl' => null,
];
2 changes: 1 addition & 1 deletion src/controllers/ActionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private function _getContextActions(): array
// Find the matching element in Craft
$element = Craft::$app->getElements()->getElementByUri($uri);

// Return an empty array if no element was found
// Return an empty array if no element was found
if (! $element) {
return [];
}
Expand Down

0 comments on commit 46f4131

Please sign in to comment.