Add popup menus for actions
This commit is contained in:
@ -172,35 +172,29 @@ class Editor
|
||||
// }
|
||||
// break;
|
||||
|
||||
case 'i':
|
||||
$coll = $this->list->findNearestCollection($this->currentRow);
|
||||
$node = $this->list->getNodeForIndex($coll);
|
||||
if ($node instanceof ObjectNode) {
|
||||
$key = $this->ask("\e[97mkey:\e[0m ");
|
||||
if ($key === null) {
|
||||
$this->redrawInfoBar();
|
||||
case 'I':
|
||||
$menu = new Menu($this->term, [
|
||||
'value' => "Insert Value",
|
||||
'object' => "Insert Object{}",
|
||||
'array' => "Insert Array[]",
|
||||
], 'Insert');
|
||||
$sel = $menu->display(5, 2, 30, 0, "value");
|
||||
$this->redrawEditor();
|
||||
switch ($sel) {
|
||||
case 'value':
|
||||
$this->doInsertValue();
|
||||
break;
|
||||
case 'array':
|
||||
$this->doInsertValue('[]');
|
||||
break;
|
||||
case 'object':
|
||||
$this->doInsertValue('{}');
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
$value = $this->ask("\e[97mvalue:\e[0m ");
|
||||
if ($value !== null) {
|
||||
$value = json_decode($value);
|
||||
$valueNode = match (true) {
|
||||
is_array($value) => new ArrayNode([]),
|
||||
is_object($value) => new ObjectNode([]),
|
||||
default => new ValueNode($value)
|
||||
};
|
||||
if ($node instanceof ArrayNode) {
|
||||
$node->append($valueNode);
|
||||
} elseif ($node instanceof ObjectNode) {
|
||||
$node->set($key, $valueNode);
|
||||
}
|
||||
$this->list->parseTree();
|
||||
$this->redrawEditor();
|
||||
} else {
|
||||
$this->redrawInfoBar();
|
||||
}
|
||||
case 'i':
|
||||
$this->doInsertValue();
|
||||
break;
|
||||
|
||||
case 'Q':
|
||||
@ -288,6 +282,39 @@ class Editor
|
||||
|
||||
}
|
||||
|
||||
private function doInsertValue(mixed $value = null): void
|
||||
{
|
||||
$coll = $this->list->findNearestCollection($this->currentRow);
|
||||
$node = $this->list->getNodeForIndex($coll);
|
||||
if ($node instanceof ObjectNode) {
|
||||
$key = $this->ask("\e[97mkey:\e[0m ");
|
||||
if ($key === null) {
|
||||
$this->redrawInfoBar();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ($value === null)
|
||||
$value = $this->ask("\e[97mvalue:\e[0m ");
|
||||
if ($value !== null) {
|
||||
$value = json_decode($value);
|
||||
$valueNode = match (true) {
|
||||
is_array($value) => new ArrayNode([]),
|
||||
is_object($value) => new ObjectNode([]),
|
||||
default => new ValueNode($value)
|
||||
};
|
||||
if ($node instanceof ArrayNode) {
|
||||
$node->append($valueNode);
|
||||
} elseif ($node instanceof ObjectNode) {
|
||||
$node->set($key, $valueNode);
|
||||
}
|
||||
$this->list->parseTree();
|
||||
$this->redrawEditor();
|
||||
} else {
|
||||
$this->redrawInfoBar();
|
||||
}
|
||||
}
|
||||
|
||||
public function ask(string $prompt, $value = ''): ?string
|
||||
{
|
||||
$plainPrompt = preg_replace('<\e\[.+?m>', '', $prompt);
|
||||
|
Reference in New Issue
Block a user