fix: fix bugs

This commit is contained in:
xion 2025-04-06 01:47:03 +08:00
parent f724074b20
commit 346be9e235
5 changed files with 32 additions and 10 deletions

View File

@ -24,10 +24,6 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@kevisual/router": "0.0.10", "@kevisual/router": "0.0.10",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
"lodash-es": "^4.17.21",
"lucide-react": "^0.487.0",
"@tiptap/core": "^2.11.7", "@tiptap/core": "^2.11.7",
"@tiptap/extension-code-block-lowlight": "^2.11.7", "@tiptap/extension-code-block-lowlight": "^2.11.7",
"@tiptap/extension-document": "^2.11.7", "@tiptap/extension-document": "^2.11.7",
@ -39,18 +35,23 @@
"@tiptap/pm": "^2.11.7", "@tiptap/pm": "^2.11.7",
"@tiptap/starter-kit": "^2.11.7", "@tiptap/starter-kit": "^2.11.7",
"@tiptap/suggestion": "^2.11.7", "@tiptap/suggestion": "^2.11.7",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
"github-markdown-css": "^5.8.1", "github-markdown-css": "^5.8.1",
"highlight.js": "^11.11.1", "highlight.js": "^11.11.1",
"idb": "^8.0.2", "idb": "^8.0.2",
"idb-keyval": "^6.2.1", "idb-keyval": "^6.2.1",
"immer": "^10.1.1", "immer": "^10.1.1",
"lodash-es": "^4.17.21",
"lowlight": "^3.3.0", "lowlight": "^3.3.0",
"lucide-react": "^0.487.0",
"marked": "^15.0.7", "marked": "^15.0.7",
"tiptap-markdown": "^0.8.10",
"nanoid": "^5.1.5", "nanoid": "^5.1.5",
"react": "^19.1.0", "react": "^19.1.0",
"react-dom": "^19.1.0", "react-dom": "^19.1.0",
"react-toastify": "^11.0.5", "react-toastify": "^11.0.5",
"tiptap-markdown": "^0.8.10",
"turndown": "^7.2.0",
"zustand": "^5.0.3" "zustand": "^5.0.3"
}, },
"exports": { "exports": {
@ -67,6 +68,7 @@
"@types/node": "^22.14.0", "@types/node": "^22.14.0",
"@types/react": "^19.1.0", "@types/react": "^19.1.0",
"@types/react-dom": "^19.1.1", "@types/react-dom": "^19.1.1",
"@types/turndown": "^5.0.5",
"@vitejs/plugin-basic-ssl": "^2.0.0", "@vitejs/plugin-basic-ssl": "^2.0.0",
"@vitejs/plugin-react": "^4.3.4", "@vitejs/plugin-react": "^4.3.4",
"tailwindcss": "^4.1.3", "tailwindcss": "^4.1.3",

View File

@ -26,14 +26,21 @@ lowlight.register('js', js);
lowlight.register('ts', ts); lowlight.register('ts', ts);
lowlight.register('markdown', markdown); lowlight.register('markdown', markdown);
export type TextEditorProps = {
markdown?: string;
html?: string;
items?: CommandItem[];
placeholder?: string;
onUpdateHtml?: (html: string) => void; //
};
export class TextEditor { export class TextEditor {
private editor?: Editor; private editor?: Editor;
private opts?: { markdown?: string; html?: string; items?: CommandItem[]; onUpdateHtml?: (html: string) => void }; private opts?: TextEditorProps;
private element?: HTMLElement; private element?: HTMLElement;
private isInitialSetup: boolean = true; private isInitialSetup: boolean = true;
constructor() {} constructor() {}
createEditor(el: HTMLElement, opts?: { markdown?: string; html?: string; items?: CommandItem[]; onUpdateHtml?: (html: string) => void }) { createEditor(el: HTMLElement, opts?: TextEditorProps) {
if (this.editor) { if (this.editor) {
this.destroy(); this.destroy();
} }
@ -41,19 +48,21 @@ export class TextEditor {
this.element = el; this.element = el;
const html = opts?.html || ''; const html = opts?.html || '';
const items = opts?.items || getSuggestionItems(); const items = opts?.items || getSuggestionItems();
const placeholder = opts?.placeholder || 'Type @ to see commands (e.g., @today, @list @test )...';
const suggestionConfig = createSuggestionConfig(items); const suggestionConfig = createSuggestionConfig(items);
this.isInitialSetup = true; this.isInitialSetup = true;
this.editor = new Editor({ this.editor = new Editor({
element: el, // 指定编辑器容器 element: el, // 指定编辑器容器
extensions: [ extensions: [
StarterKit, // 使用 StarterKit 包含基础功能 StarterKit.configure({}), // 使用 StarterKit 包含基础功能
Highlight, Highlight,
Placeholder.configure({ Placeholder.configure({
placeholder: 'Type @ to see commands (e.g., @today, @list @test )...', placeholder,
}), }),
Typography, Typography,
Markdown, Markdown,
CodeBlockLowlight.extend({ CodeBlockLowlight.extend({
name: 'ai-code-block',
addKeyboardShortcuts() { addKeyboardShortcuts() {
return { return {
Tab: () => { Tab: () => {

View File

@ -11,7 +11,7 @@ export interface CommandItem {
} }
export const Commands = Extension.create({ export const Commands = Extension.create({
name: 'commands', name: 'ai-commands',
addOptions() { addOptions() {
return { return {

View File

@ -0,0 +1 @@
export { md2html, html2md } from './utils';

10
src/tiptap/utils/index.ts Normal file
View File

@ -0,0 +1,10 @@
import { marked } from 'marked';
import TurndownService from 'turndown';
export const md2html = async (md: string) => {
return marked.parse(md);
};
export const html2md = async (html: string, opts?: TurndownService.Options) => {
const turndownService = new TurndownService(opts);
return turndownService.turndown(html);
};