Skip to content

Commit

Permalink
Merge pull request #19 from Sebobo/feature/noderesult-commandbar-inte…
Browse files Browse the repository at this point in the history
…gration

FEATURE: Improved node result commandbar integration
  • Loading branch information
Sebobo committed Jun 24, 2024
2 parents 38712c4 + 4edefc4 commit 2dc7211
Show file tree
Hide file tree
Showing 14 changed files with 608 additions and 4,133 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ jobs:
strategy:
matrix:
neosVersion:
- '7.3'
- '8.2'
- '8.3'

name: 'Terminal with Neos ${{ matrix.neosVersion }} test'

Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
22
64 changes: 26 additions & 38 deletions Classes/Command/SearchCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Shel\Neos\Terminal\Command;
Expand All @@ -22,6 +23,7 @@
use Neos\Neos\Service\LinkingService;
use Shel\Neos\Terminal\Domain\CommandContext;
use Shel\Neos\Terminal\Domain\CommandInvocationResult;
use Shel\Neos\Terminal\Domain\Dto\NodeResult;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
Expand All @@ -32,29 +34,18 @@ class SearchCommand implements TerminalCommandInterface
{

/**
* @Flow\Inject
* @var CacheManager
*/
protected $cacheManager;

/**
* @Flow\Inject
* @var Translator
*/
protected $translator;

/**
* @Flow\Inject
* @var LinkingService
*/
protected $linkingService;

/**
* @Flow\Inject
* @var NodeSearchServiceInterface
*/
#[Flow\Inject]
protected $nodeSearchService;

public function __construct(
protected Translator $translator,
protected LinkingService $linkingService,
protected CacheManager $cacheManager,
) {
}

public static function getCommandName(): string
{
return 'search';
Expand Down Expand Up @@ -107,9 +98,17 @@ public function invokeCommand(string $argument, CommandContext $commandContext):
}

if (!$contextNode) {
return new CommandInvocationResult(false,
$this->translator->translateById('command.search.noContext', [], null, null, 'Main',
'Shel.Neos.Terminal'));
return new CommandInvocationResult(
false,
$this->translator->translateById(
'command.search.noContext',
[],
null,
null,
'Main',
'Shel.Neos.Terminal'
)
);
}

// The NodeSearchInterface does not yet have a 4th argument for the startingPoint but all known implementations do
Expand All @@ -121,21 +120,10 @@ public function invokeCommand(string $argument, CommandContext $commandContext):
);

$results = array_map(function ($node) use ($documentNode, $commandContext) {
$breadcrumbs = [];
$parent = $node->getParent();
while ($parent) {
if ($parent->getNodeType()->isOfType('Neos.Neos:Document')) {
$breadcrumbs[] = $parent->getLabel();
}
$parent = $parent->getParent();
}

return [
'label' => $node->getLabel(),
'nodeType' => $node->getNodeType()->getName(),
'breadcrumb' => implode(' / ', array_reverse($breadcrumbs)),
'uri' => $this->getUriForNode($commandContext->getControllerContext(), $documentNode, $documentNode),
];
return NodeResult::fromNode(
$node,
$this->getUriForNode($commandContext->getControllerContext(), $documentNode, $documentNode)
);
}, $nodes);

return new CommandInvocationResult(true, $results);
Expand All @@ -154,7 +142,7 @@ protected function getUriForNode(
$controllerContext->getRequest()->getFormat(),
true
);
} catch (\Exception $e) {
} catch (\Exception) {
}
return '';
}
Expand Down
69 changes: 69 additions & 0 deletions Classes/Domain/Dto/NodeResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Shel\Neos\Terminal\Domain\Dto;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;

#[Flow\Proxy(false)]
class NodeResult implements \JsonSerializable
{

private function __construct(
public readonly string $identifier,
public readonly string $label,
public readonly string $nodeType,
public readonly string $icon,
public readonly string $breadcrumb,
public readonly string $uri,
public readonly string $score = '',
) {
}

public static function fromNode(NodeInterface $node, string $uri, mixed $score = ''): self
{
$breadcrumbs = [];
$parent = $node->getParent();
while ($parent) {
if ($parent->getNodeType()->isOfType('Neos.Neos:Document')) {
$breadcrumbs[] = $parent->getLabel();
}
$parent = $parent->getParent();
}

return new self(
$node->getIdentifier(),
$node->getLabel(),
$node->getNodeType()->getLabel(),
$node->getNodeType()->getConfiguration('ui.icon') ?? 'question',
implode(' / ', array_reverse($breadcrumbs)),
$uri,
$score,
);
}

/**
* @return array{__typename: string, identifier: string, label: string, nodeType: string, icon: string, breadcrumb: string, uri: string, score: float}
*/
public function toArray(): array
{
return [
'__typename' => 'NodeResult',
'identifier' => $this->identifier,
'label' => $this->label,
'nodeType' => $this->nodeType,
'icon' => $this->icon,
'breadcrumb' => $this->breadcrumb,
'uri' => $this->uri,
'score' => $this->score,
];
}

/**
* @return array{__typename: string, identifier: string, label: string, nodeType: string, icon: string, breadcrumb: string, uri: string, score: float}
*/
public function jsonSerialize(): array
{
return $this->toArray();
}
}
5 changes: 2 additions & 3 deletions Resources/Private/JavaScript/Terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
"private": true,
"scripts": {
"lint": "eslint --ext .ts,.tsx,.js src",
"build": "NODE_ENV=production neos-react-scripts build",
"build": "NODE_ENV=production NODE_OPTIONS=--openssl-legacy-provider neos-react-scripts build",
"watch": "neos-react-scripts watch"
},
"neos": {
"buildTargetDirectory": "../../../Public/Assets"
},
"dependencies": {
"@neos-project/build-essentials": "^7.3.9",
"@neos-project/neos-ui-extensibility": "^7.3.9",
"@neos-project/neos-ui-extensibility-webpack-adapter": "^8.3.8",
"react": "^16.14.0",
"react-console-emulator": "^5.0.2",
"react-redux": "^7.2.9",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

// @ts-ignore
import { selectors } from '@neos-project/neos-ui-redux-store';
import { selectors, actions } from '@neos-project/neos-ui-redux-store';

import fetchCommands from '../helpers/fetchCommands';
import { CommandList, I18nRegistry, NeosRootState } from '../interfaces';
Expand Down Expand Up @@ -61,25 +61,25 @@ class TerminalCommandRegistry {
const invokeCommand = this.invokeCommand;
return Object.keys(commands).length > 0
? {
'shel.neos.terminal': {
name: 'Terminal',
description: 'Execute terminal commands',
icon: 'terminal',
subCommands: Object.values(commands).reduce((acc, { name, description }) => {
acc[name] = {
name,
icon: 'terminal',
description: this.translate(description),
action: async function* (arg) {
yield* invokeCommand(name, arg);
},
canHandleQueries: true,
executeManually: true,
};
return acc;
}, {}),
},
}
'shel.neos.terminal': {
name: 'Terminal',
description: 'Execute terminal commands',
icon: 'terminal',
subCommands: Object.values(commands).reduce((acc, { name, description }) => {
acc[name] = {
name,
icon: 'terminal',
description: this.translate(description),
action: async function* (arg) {
yield* invokeCommand(name, arg);
},
canHandleQueries: true,
executeManually: true
};
return acc;
}, {})
}
}
: {};
};

Expand All @@ -88,6 +88,7 @@ class TerminalCommandRegistry {
const siteNode = selectors.CR.Nodes.siteNodeSelector(state);
const documentNode = selectors.CR.Nodes.documentNodeSelector(state);
const focusedNodes = selectors.CR.Nodes.focusedNodePathsSelector(state);
const setActiveContentCanvasSrc = actions.UI.ContentCanvas.setSrc;
const command = this.commands[commandName] as Command;

if (!arg) {
Expand All @@ -103,7 +104,7 @@ class TerminalCommandRegistry {
<p>{this.translate(command.description)}</p>
<code>{command.usage}</code>
</div>
),
)
};
} else {
const response = await doInvokeCommand(
Expand All @@ -121,6 +122,47 @@ class TerminalCommandRegistry {
try {
const parsedResult = JSON.parse(result);
if (typeof parsedResult !== 'string') {
if (Array.isArray(parsedResult)) {
const resultType = parsedResult[0].__typename ?? '';
if (resultType === 'NodeResult') {
yield {
success: true,
message: this.translate(
'TerminalCommandRegistry.message.nodeResults',
`${parsedResult.length} results`,
{ matches: parsedResult.length }
),
options: (parsedResult as NodeResult[]).reduce((carry, {
identifier,
label,
nodeType,
breadcrumb,
uri,
icon,
score
}) => {
if (!uri) {
// Skip nodes without uri
return carry;
}

carry[identifier] = {
id: identifier,
name: label + (score ? ` ${score}` : ''),
description: breadcrumb,
category: nodeType,
action: async () => {
this.store.dispatch(setActiveContentCanvasSrc(uri));
},
closeOnExecute: true,
icon
};
return carry;
}, {})
};
return;
}
}
result = (
<pre>
<code>{JSON.stringify(parsedResult, null, 2)}</code>
Expand All @@ -140,7 +182,7 @@ class TerminalCommandRegistry {
`Result of command "${commandName}"`,
{ commandName }
),
view: result,
view: result
};
}
};
Expand Down
11 changes: 11 additions & 0 deletions Resources/Private/JavaScript/Terminal/src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ interface TerminalTheme {
promptLabelStyle: Record<string, any> | string;
inputTextStyle: Record<string, any> | string;
}

interface NodeResult {
__typename: 'NodeResult';
identifier: string;
label: string;
nodeType: string;
icon: string;
breadcrumb: string;
uri: string;
score: string;
}
4 changes: 2 additions & 2 deletions Resources/Private/Translations/de/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
</trans-unit>

<trans-unit id="command.search.description">
<source>Fulltext search on nodes</source>
<target>Volltextsuche auf Nodes</target>
<source>Search for nodes by their properties</source>
<target>Nodes anhand ihrer Eigenschaften suchen</target>
</trans-unit>
<trans-unit id="command.search.noContext">
<source>Context node not available</source>
Expand Down
2 changes: 1 addition & 1 deletion Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</trans-unit>

<trans-unit id="command.search.description">
<source>Fulltext search on nodes</source>
<source>Search for nodes by their properties</source>
</trans-unit>
<trans-unit id="command.search.noContext">
<source>Context node not available</source>
Expand Down
2 changes: 0 additions & 2 deletions Resources/Public/Assets/Plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@
left: 0;
}


/*# sourceMappingURL=Plugin.css.map*/
13 changes: 10 additions & 3 deletions Resources/Public/Assets/Plugin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Resources/Public/Assets/Plugin.js.map

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"eel"
],
"require": {
"php": ">=7.4",
"neos/neos": "^7.3 || ^8.0",
"neos/neos-ui": "^7.3 || ^8.0",
"php": ">=8.1",
"neos/neos": "^8.3",
"neos/neos-ui": "^8.3",
"symfony/console": "^4.2 || ^5.1"
},
"suggest": {
Expand All @@ -30,4 +30,3 @@
}
}
}

Loading

0 comments on commit 2dc7211

Please sign in to comment.