Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tab functionality in Editor #20

Merged
merged 1 commit into from
Jan 11, 2025
Merged
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
64 changes: 39 additions & 25 deletions src/components/editor/Tiptap.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
import { useEditor, EditorContent } from "@tiptap/react";
import { useEditor, EditorContent, Editor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import Underline from "@tiptap/extension-underline";
import TextEditorMenuBar from "./TextEditorMenu";

import { EditorView } from "@tiptap/pm/view";

type TextEditorProps = {
onChange: (content: string) => void;
initialContent?: string;
};

onChange: (content: string) => void;
initialContent?: string;
};

export default function RichTextEditor({
onChange,
initialContent,
}: TextEditorProps) {
onChange,
initialContent,
}: TextEditorProps) {
const editor = useEditor({
extensions: [StarterKit, Underline],
content: initialContent,
onUpdate: ({ editor }) => {
onChange(editor.getHTML());
},
editorProps: {
attributes: {
class:
"cursor-text rounded-md border-2 shadow p-5 ring-offset-background focus-within:outline-none min-h-[50vh] sm:min-h-[70vh]",
},
handleKeyDown: (view:EditorView, event:KeyboardEvent) => {
if (event.key === 'Tab') {
event.preventDefault();

const { state } = view;
const { selection } = state;
const { from, to } = selection;
const spaces = ' ';
const transaction = state.tr.insertText(spaces, from, to);

const editor = useEditor({
extensions: [StarterKit, Underline],
content: initialContent,
onUpdate : ({editor}) => {
onChange(editor.getHTML())
},
editorProps: {
attributes: {
class: "cursor-text rounded-md border-2 shadow p-5 ring-offset-background focus-within:outline-none min-h-[50vh] sm:min-h-[70vh]"
view.dispatch(transaction);
return true;
}
return false;
},
},
immediatelyRender: false,
});

}
},
immediatelyRender: false
})
return (
<div className="h-full">
<TextEditorMenuBar editor={editor} />
<EditorContent editor={editor} className="h-full"/>
<EditorContent editor={editor} className="h-full" />
</div>
)
}
);
}