From 1bcee47ec648c9f562be6502e3a05becc62ae855 Mon Sep 17 00:00:00 2001 From: apple <245524539+apples-kksk@users.noreply.github.com> Date: Sun, 10 May 2026 14:59:51 +0800 Subject: [PATCH 1/2] Add markdown formatter toolbar action --- src/cloud/components/Editor/EditorToolbar.tsx | 49 +++++++++++++++++++ src/cloud/lib/editor/formatMarkdown.ts | 39 +++++++++++++++ src/cloud/lib/i18n/enUS.ts | 1 + src/cloud/lib/i18n/fr.ts | 1 + src/cloud/lib/i18n/ja.ts | 1 + src/cloud/lib/i18n/types.ts | 1 + src/cloud/lib/i18n/zhCN.ts | 1 + typings/prettier.d.ts | 12 +++++ 8 files changed, 105 insertions(+) create mode 100644 src/cloud/lib/editor/formatMarkdown.ts create mode 100644 typings/prettier.d.ts diff --git a/src/cloud/components/Editor/EditorToolbar.tsx b/src/cloud/components/Editor/EditorToolbar.tsx index d6c7d5d1c9..e3936a8f6b 100644 --- a/src/cloud/components/Editor/EditorToolbar.tsx +++ b/src/cloud/components/Editor/EditorToolbar.tsx @@ -13,6 +13,7 @@ import { mdiPageNextOutline, mdiMathIntegralBox, mdiCodeBrackets, + mdiFormatTextWrappingWrap, } from '@mdi/js' import { Position } from 'codemirror' import EditorToolButton from './EditorToolButton' @@ -26,6 +27,7 @@ import { } from '../../lib/utils/events' import { useI18n } from '../../lib/hooks/useI18n' import { lngKeys } from '../../lib/i18n/types' +import { formatMarkdown } from '../../lib/editor/formatMarkdown' interface EditorToolbarProps { editorRef?: React.MutableRefObject @@ -205,6 +207,47 @@ const EditorToolbar = ({ editorRef }: EditorToolbarProps) => { } }, [applyItalicStyle]) + const formatEditorContent = useCallback(() => { + if (editorRef == null || editorRef.current == null) { + return + } + + const editor = editorRef.current + const hasSelection = editor.somethingSelected() + const from = hasSelection ? editor.getCursor('from') : { line: 0, ch: 0 } + const lastLine = editor.lineCount() - 1 + const to = hasSelection + ? editor.getCursor('to') + : { line: lastLine, ch: (editor.getLine(lastLine) || '').length } + const source = editor.getRange(from, to, '\n') + + if (source.trim() === '') { + return + } + + const fromOffset = editor.indexFromPos(from) + const cursorOffset = Math.max( + 0, + editor.indexFromPos(editor.getCursor()) - fromOffset + ) + + try { + const result = formatMarkdown(source, cursorOffset) + + if (result.formatted === source) { + return + } + + editor.operation(() => { + editor.replaceRange(result.formatted, from, to, source) + editor.setCursor(editor.posFromIndex(fromOffset + result.cursorOffset)) + }) + editor.focus() + } catch (error) { + console.warn('Unable to format markdown content', error) + } + }, [editorRef]) + return ( { style={{ marginRight: 20 }} onClick={() => onFormatCallback('taskList')} /> + Date: Sun, 10 May 2026 15:00:17 +0800 Subject: [PATCH 2/2] Add markdown content formatter --- src/cloud/components/Editor/EditorToolbar.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cloud/components/Editor/EditorToolbar.tsx b/src/cloud/components/Editor/EditorToolbar.tsx index e3936a8f6b..089bd88c6c 100644 --- a/src/cloud/components/Editor/EditorToolbar.tsx +++ b/src/cloud/components/Editor/EditorToolbar.tsx @@ -239,7 +239,7 @@ const EditorToolbar = ({ editorRef }: EditorToolbarProps) => { } editor.operation(() => { - editor.replaceRange(result.formatted, from, to, source) + editor.replaceRange(result.formatted, from, to, '+format') editor.setCursor(editor.posFromIndex(fromOffset + result.cursorOffset)) }) editor.focus()