Skip to content

feat(editor): 增加MD文件预览功能 - #522

Open
humanfans wants to merge 1 commit into
linuxdeepin:masterfrom
humanfans:master
Open

feat(editor): 增加MD文件预览功能#522
humanfans wants to merge 1 commit into
linuxdeepin:masterfrom
humanfans:master

Conversation

@humanfans

@humanfans humanfans commented Aug 14, 2026

Copy link
Copy Markdown
  • Add MarkdownPreview widget (QTextBrowser + QTextDocument::setMarkdown, Qt >= 6.5)
  • Show live side-by-side preview automatically for .md/.markdown files
  • Add checkable 'Markdown preview' menu action and Ctrl+Shift+M shortcut
  • Sync action state with current tab and save-as path changes
  • Update AT-SPI expected names and add unit tests

Summary by Sourcery

Add an optional, accessible markdown preview pane to the editor with menu and shortcut controls, active for markdown files when supported by the Qt version.

New Features:

  • Introduce a Markdown preview widget that renders markdown content as rich text when Qt 6.5+ is available
  • Provide a side-by-side markdown preview pane in the editor that can be toggled per tab and auto-enabled for .md/.markdown files
  • Add a checkable "Markdown preview" titlebar menu action with keyboard shortcut support and tab-aware state sync

Enhancements:

  • Refactor the editor layout to use a splitter container so the text editor and markdown preview can share horizontal space with sensible default sizing
  • Improve accessibility metadata by naming the markdown preview view and its menu item for AT-SPI

Tests:

  • Add unit tests covering MarkdownPreview support detection, file-type recognition, rendering behavior, and link-handling semantics
  • Update AT-SPI expected names to cover the new markdown preview controls

- Add MarkdownPreview widget (QTextBrowser + QTextDocument::setMarkdown, Qt >= 6.5)
- Show live side-by-side preview automatically for .md/.markdown files
- Add checkable 'Markdown preview' menu action and Ctrl+Shift+M shortcut
- Sync action state with current tab and save-as path changes
- Update AT-SPI expected names and add unit tests

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @humanfans, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: humanfans

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

Hi @humanfans. Thanks for your PR. 😃

@github-actions

Copy link
Copy Markdown

CLA Assistant Lite bot:

如果你是以企业贡献者的身份进行提交,请联系我们签署企业贡献者许可协议
If you submit as corporate contributor, please contact us to sign our Corporate Contributor License Agreement

感谢您的提交,我们非常感谢。 像许多开源项目一样,在接受您的贡献之前,我们要求您签署我们的个人贡献者许可协议。 您只需发布与以下格式相同的评论即可签署个人贡献者许可协议
Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Individual Contributor License Agreement before we can accept your contribution. You can sign the Individual Contributor License Agreement by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA.

Meteorology Platform seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You can retrigger this bot by commenting recheck in this Pull Request

@deepin-ci-robot

Copy link
Copy Markdown

Hi @humanfans. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sourcery-ai

sourcery-ai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduce a Qt-based MarkdownPreview widget and wire it into the editor/window so that markdown files can be previewed side-by-side, with menu/shortcut control, accessibility naming, and unit tests.

Sequence diagram for toggling Markdown preview in the editor

sequenceDiagram
    actor User
    participant Window
    participant EditWrapper
    participant MarkdownPreview

    User->>Window: markdownPreviewAction.triggered / toggleMarkdownPreview
    Window->>Window: toggleMarkdownPreview()
    Window->>EditWrapper: currentWrapper()
    alt wrapper == nullptr or !MarkdownPreview::isSupported() or !MarkdownPreview::isMarkdownFile(filePath)
        Window-->>User: [no-op]
    else validMarkdownPreview
        Window->>EditWrapper: filePath()
        Window->>EditWrapper: isMarkdownPreviewVisible()
        Window->>EditWrapper: setMarkdownPreviewVisible(!isMarkdownPreviewVisible)
        Note over EditWrapper,MarkdownPreview: setMarkdownPreviewVisible triggers MarkdownPreview::updatePreview
        Window->>Window: updateMarkdownPreviewActionState()
        Window->>EditWrapper: currentWrapper()
        Window->>MarkdownPreview: isMarkdownFile(wrapper->filePath())
        Window->>Window: m_markdownPreviewAction->setEnabled(isMarkdown)
        Window->>Window: m_markdownPreviewAction->setChecked(isMarkdown && wrapper->isMarkdownPreviewVisible())
    end

    EditWrapper->>MarkdownPreview: updatePreview(m_pTextEdit->toPlainText())
    EditWrapper->>MarkdownPreview: [on QPlainTextEdit::textChanged when visible] updatePreview(m_pTextEdit->toPlainText())
    EditWrapper->>MarkdownPreview: [on updatePath for .md/.markdown] setMarkdownPreviewVisible(isMarkdownFile(file))
Loading

File-Level Changes

Change Details Files
Add a dedicated MarkdownPreview widget for rendering markdown content and basic unit tests for its behavior.
  • Implement MarkdownPreview as a QTextBrowser wrapper using QTextDocument::setMarkdown when Qt >= 6.5 and falling back gracefully on older Qt versions.
  • Provide helper methods isSupported, isMarkdownFile, and updatePreview, plus override doSetSource to keep the preview read-only with no link navigation.
  • Add gtest-based unit tests to verify support detection, markdown-file detection, preview rendering, and link navigation behavior.
src/editor/markdownpreview.h
src/editor/markdownpreview.cpp
tests/src/editor/ut_markdownpreview.h
tests/src/editor/ut_markdownpreview.cpp
Refactor the editor layout to support an optional side-by-side Markdown preview pane and expose preview visibility control from EditWrapper.
  • Wrap the existing left-area + text editor widgets into an editorContainer with its own horizontal layout.
  • Introduce a horizontal QSplitter (m_pEditSplitter) containing the editorContainer and, conditionally, the MarkdownPreview widget, configured with non-collapsible children and custom handle width.
  • Create MarkdownPreview instance only when supported, connect textChanged on the editor to update the preview live when visible, and default the preview to hidden.
  • Add setMarkdownPreviewVisible and isMarkdownPreviewVisible on EditWrapper, including initial splitter size ratio (55/45) when first showing preview and auto-toggle based on opened file path in updatePath.
src/editor/editwrapper.h
src/editor/editwrapper.cpp
Add a checkable "Markdown preview" menu action, keyboard shortcut, and logic to synchronize its enabled/checked state with the current tab and file type.
  • Create a QAction "Markdown preview" in Window::initTitlebar, make it checkable, initially disabled, store it as m_markdownPreviewAction, and add it to the main menu.
  • Connect the action to Window::toggleMarkdownPreview, which toggles the current EditWrapper’s preview visibility only for supported markdown files and then refreshes the action state.
  • Introduce updateMarkdownPreviewActionState to enable/disable and check/uncheck the action based on the current wrapper, Qt support, and whether the file is markdown, and call it when the current tab changes and after save-as operations.
  • Wire a new keyboard shortcut "togglemarkdownpreview" into Window::keyPressEvent and settings translation, mapping it to toggleMarkdownPreview.
  • Add m_markdownPreviewAction and updateMarkdownPreviewActionState declarations in window.h.
src/widgets/window.h
src/widgets/window.cpp
src/controls/settingsdialog.cpp
src/resources/settings.json.in
Update accessibility expectations to account for the new markdown preview controls.
  • Register the "MarkdownPreview" menu item and "MarkdownPreviewView" text widget in the AT-SPI expected_names.yaml with appropriate roles and descriptions.
test/at/spi/expected_names.yaml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants