From ed57eec3b0c9688010c48a16e232356b4887e7e3 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Fri, 4 Oct 2024 17:08:54 +0200 Subject: [PATCH] Refactor editor code, confirm on new if modified * Break out the doDeleteValue method in Editor * Confirm to save/discard when document modified and new requested * Re-add the title to menus --- src/Editor/Editor.php | 68 +++++++++++++++++++++++++++++++------------ src/Editor/Menu.php | 3 ++ 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Editor/Editor.php b/src/Editor/Editor.php index de1de20..bf1ac3c 100644 --- a/src/Editor/Editor.php +++ b/src/Editor/Editor.php @@ -140,24 +140,7 @@ class Editor break; case 'D': - $node = $this->list->getNodeForIndex($this->currentRow); - $path = $this->list->getPathForIndex($this->currentRow); - $parentPath = dirname($path); - $deleteKey = basename($path); - $collNode = $this->list->getNodeForPath($parentPath); - if ($collNode instanceof ArrayNode) { - $collNode->removeIndex($deleteKey); - $this->list->parseTree(); - $this->redrawEditor(); - $this->modified = true; - } elseif ($collNode instanceof ObjectNode) { - $collNode->unset($deleteKey); - $this->list->parseTree(); - $this->redrawEditor(); - $this->modified = true; - } else { - $this->showMessage("\e[97;41mCan only delete from object, array"); - } + $this->doDeleteValue(); break; // FIXME make sure this clones; editing a clone updates original as well @@ -226,7 +209,7 @@ class Editor 'discard' => "Discard changes", //'sep0' => "---", //'__paste' => "Paste from clipboard", - ], 'Insert'); + ], 'Document is modified'); $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); $sel = $menu->display(0, 0, 30, 0, "value"); $this->redrawEditor(); @@ -343,6 +326,31 @@ class Editor break; case "\x0e": // ctrl-n + + if ($this->modified) { + $menu = new Menu($this->term, [ + 'cancel' => "Return to editor", + 'save' => "Save changes", + 'discard' => "Discard changes", + //'sep0' => "---", + //'__paste' => "Paste from clipboard", + ], 'Document is modified'); + $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]); + $sel = $menu->display(0, 0, 30, 0, "value"); + $this->redrawEditor(); + switch ($sel) { + case 'cancel': + break(2); + case 'save': + if (!$this->doWriteFile()) { + break(2); + } + break; + case 'discard': + break; + } + } + $this->document->load((object)[]); $this->list->parseTree(); $this->filename = "untitled.json"; @@ -631,6 +639,28 @@ class Editor } } + private function doDeleteValue(): void + { + $node = $this->list->getNodeForIndex($this->currentRow); + $path = $this->list->getPathForIndex($this->currentRow); + $parentPath = dirname($path); + $deleteKey = basename($path); + $collNode = $this->list->getNodeForPath($parentPath); + if ($collNode instanceof ArrayNode) { + $collNode->removeIndex($deleteKey); + $this->list->parseTree(); + $this->redrawEditor(); + $this->modified = true; + } elseif ($collNode instanceof ObjectNode) { + $collNode->unset($deleteKey); + $this->list->parseTree(); + $this->redrawEditor(); + $this->modified = true; + } else { + $this->showMessage("\e[97;41mCan only delete from object, array"); + } + } + /** * Insert a new value * diff --git a/src/Editor/Menu.php b/src/Editor/Menu.php index 4a72064..16b1da0 100644 --- a/src/Editor/Menu.php +++ b/src/Editor/Menu.php @@ -89,10 +89,13 @@ class Menu $thumbTop = round($scrollTop * $visibleItems); $thumbBottom = round($visibleItems * $scrollBottom); + $tleft = round(($width / 2) - ((mb_strlen($this->title) + 2) / 2)); + // draw head echo "\e[40;37m"; $this->terminal ->writeAt($left, $top + 0, self::U_CORNER_ROUNDED_TOPLEFT.str_repeat(self::U_EDGE_HORIZONTAL,$width - 2).self::U_CORNER_ROUNDED_TOPRIGHT) + ->writeAt($left + $tleft, $top + 0, " \e[1m{$this->title}\e[22m ") //->writeAt($left, $top + 1, self::U_EDGE_VERTICAL.str_repeat(" ",$width - 2).self::U_EDGE_VERTICAL) //->writeAt($left + 2, $top + 1, "\e[1m" . $this->title . "\e[22m") //->writeAt($left, $top + 2, self::U_EDGE_VERTICAL_RIGHT.str_repeat(self::U_EDGE_HORIZONTAL,$width - 2).self::U_EDGE_VERTICAL_LEFT)