Skip to content

[Feature] Preserve view state when refreshing an editor preview / 编辑预览刷新时保留视图状态 #5

Closed

Description

@dotnet9

English

Use case

Vex is evaluating StreamMark as the reading/preview renderer beside a source editor. Typing can modify any part of the source, so the preview must refresh complete text rather than append chat tokens.

Environment: Windows, .NET SDK 10.0.303 / net10.0, Avalonia 12.1.4, TKCode.StreamMark.Avalonia 0.1.0-alpha.23.

Reproduction (executed against the published NuGet package)

using StreamMark.Avalonia;
using StreamMark.Avalonia.Selection;

var view = new MarkdownView {
    Markdown = "# Title\n\nFirst paragraph.\n\nLast paragraph."
};
// Model-level selection setup; this probe does not exercise pointer input/layout.
view.Selection.Set(new TextPoint(0, 9, 0, 0), new TextPoint(0, 9, 0, 5));
Console.WriteLine(view.Selection.IsEmpty); // False

view.Markdown = "# Title\n\nFirst paragraph.\n\nChanged last paragraph.";
Console.WriteLine(view.Selection.IsEmpty); // True

Source inspection confirms MarkdownView.OnSourceChanged() calls _presenter.Clear(), _state.Clear() and Selection.Clear(): MarkdownView.cs. The experiment proves selection reset; it does not establish a measured UI flicker/scroll regression.

Requested improvement

Offer an opt-in complete-document refresh path for editor previews that:

  • keeps old content visible until a current replacement snapshot is ready;
  • preserves/remaps unaffected selection, reading anchor, and per-block state where possible;
  • discards stale asynchronous results when updates arrive quickly;
  • defines fallback behavior when the selected/anchored content is deleted.

The host can provide a change range or a source-position mapping if needed. This can be a separate preview adapter/document type; it need not make StreamingDocument mutable or change its append-only contract.

Acceptance criteria

  • Editing the last paragraph preserves a selection in the unchanged first paragraph.
  • Insert/delete before the selected block remaps positions correctly; deleted anchors have a documented fallback.
  • Rapid A → B → C updates never leave A/B rendered after C has been accepted.
  • New-document assignment still supports explicit state reset.
  • Existing streaming equivalence, threading and NativeAOT behavior remain intact.

中文

使用场景与当前行为

Vex 希望在源码编辑器旁使用 StreamMark 预览。普通编辑会修改任意位置,不能完全套用聊天 token 追加模式。

上面的 NuGet 实测代码显示:修改文末后,之前在未修改段落中设置的选区也被清空。源码还会清理呈现器和块状态。此次实测仅确认选区重置,并未测出界面闪烁或滚动性能问题。

建议

为编辑预览提供可选的全文刷新入口:新快照完成前保留旧画面,尽可能映射并保留未受影响的选区、阅读锚点与块状态;快速更新时丢弃过期结果;被删除内容的状态采用明确回退策略。

宿主可传入改动范围或位置映射。这可以通过独立适配层实现,不要求改变 StreamingDocument 的只追加约束。

验收

修改末段不清空首段选区;在前面插入/删除内容后坐标可正确映射;删除选中内容时行为明确;连续 A/B/C 更新不会回退到过期版本;切换到新文档仍可主动清空状态;保持流式一致性、线程规则与 NativeAOT 兼容性。

Activity

  1. added
    enhancement
    on Oct 21, 2026
  2. 239573049 commented on Oct 21, 2026

    @239573049
    Collaborator

    Implemented in #8 (released in 0.1.0-alpha.24). StreamingDocument stays append-only; this is a separate entry point on MarkdownView.

    • RefreshMarkdown(text) replaces the whole text without clearing the view. Assigning Markdown still means "a new document" and resets selection and block state (DocumentId changes).
    • The old content stays visible until the new text has been parsed. Rapid A → B → C refreshes never show A or B once C is accepted.
    • The old and new snapshots are aligned by content, so blocks that did not change, or only moved because text was inserted or deleted before them, are updated in place: their views, layout and block state are kept. The selection and the reading position move with them.
    • Fallbacks (documented): an endpoint inside a rewritten block stays on the paired new block, clamped to the new text; inside a deleted block it moves to the start of the next surviving block (or the document end); a selection whose ends land on the same point is cleared.
    • Tested: editing the last paragraph keeps a selection in the first one; insert/delete before the selection remaps it; A/B/C; assignment still resets; reading position stays put when a tall paragraph is inserted above the viewport. A randomised test applies 1–3 edits to three fixtures and checks the result against a fresh parse.

    Limits: MarkdownView only (not TranscriptView); it throws if the text comes from Document or Stream; the find highlights (view.Search) are not recomputed after a refresh. I did not add a host-supplied change range: content alignment already handles edits, and a range would not make the result more precise.

    已在 #8 中实现(已随 0.1.0-alpha.24 发布)。StreamingDocument 仍是只追加的,这是 MarkdownView 上独立的入口。

    • RefreshMarkdown(text) 整篇替换文本但不清空视图;直接给 Markdown 赋值仍表示换一个新文档,会清空选区与块状态(DocumentId 变化)。
    • 新文本解析完成之前继续显示旧内容;连续 A → B → C 刷新时,C 被接受后不会再出现 A、B。
    • 新旧快照按内容对齐:没变的块,或只是因为前面增删文字而平移的块,原地更新,视图、排版和块状态都保留;选区和阅读位置随之平移。
    • 回退规则(已写入文档):端点在被改写的块里,留在配对的新块上并钳到新文本范围;在被删除的块里,退到后面第一个存活块的开头(没有则文档末尾);选区两端落到同一点则清空。
    • 测试:改末段保留首段选区;前面插入 / 删除后选区平移;A/B/C;赋值仍然清空;视口上方插入一大段后阅读位置不动。另有随机测试:对三份 fixture 做 1–3 处编辑,结果与重新整篇解析一致。

    局限:只做了 MarkdownView(没做 TranscriptView);文本来自 Document / Stream 时调用会抛异常;刷新后查找高亮(view.Search)不会自动重算。没有加宿主传入的改动范围:按内容对齐已经能处理编辑,范围参数不会让结果更准。

  3. added a commit that references this issue on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions