Skip to content

Commit e04fd17

Browse files
committed
fix(#1013): 修复代码块换行问题
1 parent 80882c6 commit e04fd17

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use client'
2+
3+
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
4+
5+
export const StableCodeBlockLowlight = CodeBlockLowlight.extend({
6+
addKeyboardShortcuts() {
7+
const parentShortcuts = this.parent?.() ?? {}
8+
9+
return {
10+
...parentShortcuts,
11+
Enter: ({ editor }) => {
12+
const { selection } = editor.state
13+
const { $from, empty } = selection
14+
15+
if (!empty || $from.parent.type !== this.type) {
16+
return false
17+
}
18+
19+
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2
20+
const endsWithDoubleNewline = $from.parent.textContent.endsWith('\n\n')
21+
const action = isAtEnd && endsWithDoubleNewline ? 'exit' : 'newline'
22+
23+
if (action === 'exit') {
24+
return editor.chain()
25+
.command(({ tr }) => {
26+
tr.delete($from.pos - 2, $from.pos)
27+
return true
28+
})
29+
.exitCode()
30+
.run()
31+
}
32+
33+
return editor.commands.insertContent('\n')
34+
},
35+
}
36+
},
37+
})

src/app/core/main/editor/markdown/tiptap-editor.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import Placeholder from '@tiptap/extension-placeholder'
66
import Link from '@tiptap/extension-link'
77
import TaskList from '@tiptap/extension-task-list'
88
import TaskItem from '@tiptap/extension-task-item'
9-
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight'
109
import CharacterCount from '@tiptap/extension-character-count'
1110
import Highlight from '@tiptap/extension-highlight'
1211
import Underline from '@tiptap/extension-underline'
@@ -52,6 +51,7 @@ import { AISuggestionFloating } from './ai-suggestion-floating'
5251
import emitter from '@/lib/emitter'
5352
import { QuoteMark } from './quote-mark'
5453
import { MarkdownParagraph } from './markdown-paragraph'
54+
import { StableCodeBlockLowlight } from './code-block-extension'
5555
import useSettingStore from '@/stores/setting'
5656
import useChatStore from '@/stores/chat'
5757
import { Loader2, X } from 'lucide-react'
@@ -315,7 +315,7 @@ export function TipTapEditor({
315315
TaskItem.configure({
316316
nested: true,
317317
}),
318-
CodeBlockLowlight.configure({
318+
StableCodeBlockLowlight.configure({
319319
lowlight,
320320
}),
321321
CharacterCount,

0 commit comments

Comments
 (0)