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
This commit is contained in:
		@@ -140,24 +140,7 @@ class Editor
 | 
				
			|||||||
                    break;                    
 | 
					                    break;                    
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                case 'D':
 | 
					                case 'D':
 | 
				
			||||||
                    $node = $this->list->getNodeForIndex($this->currentRow);
 | 
					                    $this->doDeleteValue();
 | 
				
			||||||
                    $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");
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // FIXME make sure this clones; editing a clone updates original as well
 | 
					                // FIXME make sure this clones; editing a clone updates original as well
 | 
				
			||||||
@@ -226,7 +209,7 @@ class Editor
 | 
				
			|||||||
                            'discard' => "Discard changes",
 | 
					                            'discard' => "Discard changes",
 | 
				
			||||||
                            //'sep0' => "---",
 | 
					                            //'sep0' => "---",
 | 
				
			||||||
                            //'__paste' => "Paste from clipboard",
 | 
					                            //'__paste' => "Paste from clipboard",
 | 
				
			||||||
                        ], 'Insert');
 | 
					                        ], 'Document is modified');
 | 
				
			||||||
                        $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]);
 | 
					                        $this->redrawInfoBar([ '^C' => 'Cancel', '↑/↓' => 'Select option', 'Enter' => 'Accept' ]);
 | 
				
			||||||
                        $sel = $menu->display(0, 0, 30, 0, "value");
 | 
					                        $sel = $menu->display(0, 0, 30, 0, "value");
 | 
				
			||||||
                        $this->redrawEditor();
 | 
					                        $this->redrawEditor();
 | 
				
			||||||
@@ -343,6 +326,31 @@ class Editor
 | 
				
			|||||||
                    break;
 | 
					                    break;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
                case "\x0e": // ctrl-n
 | 
					                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->document->load((object)[]);
 | 
				
			||||||
                    $this->list->parseTree();
 | 
					                    $this->list->parseTree();
 | 
				
			||||||
                    $this->filename = "untitled.json";
 | 
					                    $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
 | 
					     * Insert a new value
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -89,10 +89,13 @@ class Menu
 | 
				
			|||||||
        $thumbTop = round($scrollTop * $visibleItems);
 | 
					        $thumbTop = round($scrollTop * $visibleItems);
 | 
				
			||||||
        $thumbBottom = round($visibleItems * $scrollBottom);
 | 
					        $thumbBottom = round($visibleItems * $scrollBottom);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $tleft = round(($width / 2) - ((mb_strlen($this->title) + 2) / 2));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // draw head
 | 
					        // draw head
 | 
				
			||||||
        echo "\e[40;37m";
 | 
					        echo "\e[40;37m";
 | 
				
			||||||
        $this->terminal
 | 
					        $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, $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, $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 + 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)
 | 
					            //->writeAt($left, $top + 2, self::U_EDGE_VERTICAL_RIGHT.str_repeat(self::U_EDGE_HORIZONTAL,$width - 2).self::U_EDGE_VERTICAL_LEFT)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user