enh: rich text input

This commit is contained in:
Timothy Jaeryang Baek
2024-11-20 22:56:26 -08:00
parent e30c5e628c
commit aca06f92e8
4 changed files with 138 additions and 1 deletions

View File

@@ -11,13 +11,19 @@
import { Editor } from '@tiptap/core';
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
import Placeholder from '@tiptap/extension-placeholder';
import Highlight from '@tiptap/extension-highlight';
import Typography from '@tiptap/extension-typography';
import StarterKit from '@tiptap/starter-kit';
import { all, createLowlight } from 'lowlight';
import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
// create a lowlight instance with all languages loaded
const lowlight = createLowlight(all);
export let className = 'input-prose';
export let placeholder = 'Type here...';
export let value = '';
@@ -109,7 +115,15 @@
onMount(() => {
editor = new Editor({
element: element,
extensions: [StarterKit, Highlight, Typography, Placeholder.configure({ placeholder })],
extensions: [
StarterKit,
CodeBlockLowlight.configure({
lowlight
}),
Highlight,
Typography,
Placeholder.configure({ placeholder })
],
content: marked.parse(value),
autofocus: true,
onTransaction: () => {
@@ -144,10 +158,22 @@
}
if (messageInput) {
if (event.key === 'Enter') {
// Check if the current selection is inside a code block
const { state } = view;
const { $head } = state.selection;
const isInCodeBlock = $head.parent.type.name === 'codeBlock';
if (isInCodeBlock) {
return false; // Prevent Enter action inside a code block
}
}
// Handle shift + Enter for a line break
if (shiftEnter) {
if (event.key === 'Enter' && event.shiftKey) {
editor.commands.setHardBreak(); // Insert a hard break
view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
event.preventDefault();
return true;
}