Class Sealed
public sealed class EditHistory

Namespace: Moka.Blazor.Json.Models

Snapshot-based undo/redo history for JSON editing. Stores full JSON strings, capped at a maximum number of entries.

Constructors

NameDescription
EditHistory(int maxSnapshots) Snapshot-based undo/redo history for JSON editing. Stores full JSON strings, capped at a maximum number of entries.

EditHistory(int maxSnapshots)

EditHistory.EditHistory(int maxSnapshots = 50)

Snapshot-based undo/redo history for JSON editing. Stores full JSON strings, capped at a maximum number of entries.

Properties

NameDescription
CanRedo
CanUndo

Methods

View Source
/// <summary>
///     Snapshot-based undo/redo history for JSON editing.
///     Stores full JSON strings, capped at a maximum number of entries.
/// </summary>
public sealed class EditHistory(int maxSnapshots = 50)
{
    private readonly List<string> _snapshots = [];
    private int _currentIndex = -1;
    public bool CanUndo => _currentIndex > 0;
    public bool CanRedo => _currentIndex < _snapshots.Count - 1;

    public void PushSnapshot(string json)
    {
        // Truncate any redo entries beyond current position
        if (_currentIndex < _snapshots.Count - 1)
        {
            _snapshots.RemoveRange(_currentIndex + 1, _snapshots.Count - _currentIndex - 1);
        }

        _snapshots.Add(json);
        _currentIndex = _snapshots.Count - 1;
        // Evict oldest if over capacity
        if (_snapshots.Count > maxSnapshots)
        {
            _snapshots.RemoveAt(0);
            _currentIndex--;
        }
    }

    public string? Undo()
    {
        if (!CanUndo)
        {
            return null;
        }

        _currentIndex--;
        return _snapshots[_currentIndex];
    }

    public string? Redo()
    {
        if (!CanRedo)
        {
            return null;
        }

        _currentIndex++;
        return _snapshots[_currentIndex];
    }

    public void Clear()
    {
        _snapshots.Clear();
        _currentIndex = -1;
    }
}
Was this page helpful?