     }
     
     open func replace(with character: Character) {
-        guard selectedNodeLayer != nil else { return }
+        hideMenu()
         
         if let rect = manager.replaceSelectedNode(with: character) {
             setNeedsDisplay(rect)
         }
-        
-        selectedNodeLayer!.removeFromSuperlayer()
-        candidatesView!.removeFromSuperview()
     }
     
     // MARK: - MyScriptParser delegate

    // MARK: - Target action
    
    @objc private func tapAction(_ sender: UITapGestureRecognizer) {
        guard let node = selectNode(at: sender.location(in: self)) else { return }
        showMenu(for: node)
    }
    
    @objc private func longPressAction(_ sender: UILongPressGestureRecognizer) {
        guard let node = selectNode(at: sender.location(in: self)) else { return }
        showCursor(for: node)
    }
    
    @IBAction open func undoAction(_ sender: UIButton?) {
        if !isWritingMode { isWritingMode = true }
        if let rect = manager.undo() {
            setNeedsDisplay(rect)
        }
    }
    
    @IBAction open func redoAction(_ sender: UIButton?) {
        if !isWritingMode { isWritingMode = true }
        if let rect = manager.redo() {
            setNeedsDisplay(rect)
        }
    }
    
    @IBAction open func removeAction(_ sender: UIButton?) {
        guard selectedNodeLayer != nil else { return }
        
        selectedNodeLayer!.removeFromSuperlayer()
        candidatesView!.removeFromSuperview()
        
        if let rect = manager.removeSelectedNode() {
            setNeedsDisplay(rect)

            ctx.fill(CGRect(origin: CGPoint.zero, size: node.frame.size))
            ctx.translateBy(x: -node.frame.origin.x, y: -node.frame.origin.y)

            ctx.setLineWidth(lineWidth)
            ctx.setLineCap(.round)
            
            for ink in node.ink {
                if let strokeInk = ink as? StrokeInk {
                    ctx.addPath(strokeInk.path.cgPath)
                } else {
                    let charInk = ink as! CharacterInk
                    guard let image = getImage(for: charInk, strokeColor: selectionStrokeColor)?.cgImage else { return }
                    ctx.draw(image, in: charInk.frame)
                }
            }
            
            ctx.strokePath()
            
            ctx.restoreGState()
            image = UIGraphicsGetImageFromCurrentImageContext()?.cgImage
