Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/features/editor/codemirror/use-markdown-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ export const useMarkdownEditor = (
view.dispatch({
changes: { from: 0, to: state.doc.length, insert: formattedText },
});
// Persist formatted content to state/storage and notify listeners
setContent((prevContent) => (prevContent !== formattedText ? formattedText : prevContent));
if (onChange) {
onChange(formattedText);
}
setCharCount(formattedText.length);
Comment on lines 116 to +124

Choose a reason for hiding this comment

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

high

This PR introduces two different strategies to fix the issue of formatted text not being saved. In command-menu.tsx, you've used annotations: Transaction.userEvent.of("input") to mark the change as user input, which allows the existing EditorView.updateListener to handle saving the content. This is a clean approach that reuses existing logic.

To maintain consistency and avoid duplicating state update logic, I suggest applying the same strategy here in onFormat. This will centralize the content update logic within the updateListener.

You can replace the view.dispatch call and the subsequent state update logic with a single dispatch that includes the annotation. You'll also need to add Transaction to the import from @codemirror/state at the top of the file.

Suggested change
view.dispatch({
changes: { from: 0, to: state.doc.length, insert: formattedText },
});
// Persist formatted content to state/storage and notify listeners
setContent((prevContent) => (prevContent !== formattedText ? formattedText : prevContent));
if (onChange) {
onChange(formattedText);
}
setCharCount(formattedText.length);
view.dispatch({
changes: { from: 0, to: state.doc.length, insert: formattedText },
annotations: Transaction.userEvent.of("input"),
});

// Restore cursor position after formatting
try {
const newState = view.state;
Expand Down
2 changes: 2 additions & 0 deletions src/features/menu/command-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useFontFamily, FONT_FAMILIES, FONT_FAMILY_OPTIONS } from "../../utils/h
import type { EditorView } from "@codemirror/view";
import { fetchGitHubIssuesTaskList } from "../integration/github/github-api";
import { DprintMarkdownFormatter } from "../editor/markdown/formatter/dprint-markdown-formatter";
import { Transaction } from "@codemirror/state";
import {
CheckCircleIcon,
FileIcon,
Expand Down Expand Up @@ -185,6 +186,7 @@ export const CommandMenu = ({
if (formattedText !== currentText) {
editorView.dispatch({
changes: { from: 0, to: state.doc.length, insert: formattedText },
annotations: Transaction.userEvent.of("input"),
});

// Restore cursor position after formatting
Expand Down