123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <script lang="ts">
- import { marked } from 'marked';
- import TurndownService from 'turndown';
- const turndownService = new TurndownService();
- import { onMount, onDestroy } from 'svelte';
- import { createEventDispatcher } from 'svelte';
- const eventDispatch = createEventDispatcher();
- import { EditorState, Plugin, TextSelection } from 'prosemirror-state';
- 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 = '';
- export let id = '';
- export let messageInput = false;
- export let shiftEnter = false;
- export let largeTextAsFile = false;
- let element;
- let editor;
- // Function to find the next template in the document
- function findNextTemplate(doc, from = 0) {
- const patterns = [
- { start: '[', end: ']' },
- { start: '{{', end: '}}' }
- ];
- let result = null;
- doc.nodesBetween(from, doc.content.size, (node, pos) => {
- if (result) return false; // Stop if we've found a match
- if (node.isText) {
- const text = node.text;
- let index = Math.max(0, from - pos);
- while (index < text.length) {
- for (const pattern of patterns) {
- if (text.startsWith(pattern.start, index)) {
- const endIndex = text.indexOf(pattern.end, index + pattern.start.length);
- if (endIndex !== -1) {
- result = {
- from: pos + index,
- to: pos + endIndex + pattern.end.length
- };
- return false; // Stop searching
- }
- }
- }
- index++;
- }
- }
- });
- return result;
- }
- // Function to select the next template in the document
- function selectNextTemplate(state, dispatch) {
- const { doc, selection } = state;
- const from = selection.to;
- let template = findNextTemplate(doc, from);
- if (!template) {
- // If not found, search from the beginning
- template = findNextTemplate(doc, 0);
- }
- if (template) {
- if (dispatch) {
- const tr = state.tr.setSelection(TextSelection.create(doc, template.from, template.to));
- dispatch(tr);
- }
- return true;
- }
- return false;
- }
- export const setContent = (content) => {
- editor.commands.setContent(content);
- };
- const selectTemplate = () => {
- if (value !== '') {
- // After updating the state, try to find and select the next template
- setTimeout(() => {
- const templateFound = selectNextTemplate(editor.view.state, editor.view.dispatch);
- if (!templateFound) {
- // If no template found, set cursor at the end
- const endPos = editor.view.state.doc.content.size;
- editor.view.dispatch(
- editor.view.state.tr.setSelection(TextSelection.create(editor.view.state.doc, endPos))
- );
- }
- }, 0);
- }
- };
- onMount(() => {
- editor = new Editor({
- element: element,
- extensions: [
- StarterKit,
- CodeBlockLowlight.configure({
- lowlight
- }),
- Highlight,
- Typography,
- Placeholder.configure({ placeholder })
- ],
- content: marked.parse(value),
- autofocus: true,
- onTransaction: () => {
- // force re-render so `editor.isActive` works as expected
- editor = editor;
- const newValue = turndownService.turndown(editor.getHTML());
- if (value !== newValue) {
- value = newValue; // Trigger parent updates
- }
- },
- editorProps: {
- attributes: { id },
- handleDOMEvents: {
- focus: (view, event) => {
- eventDispatch('focus', { event });
- return false;
- },
- keypress: (view, event) => {
- eventDispatch('keypress', { event });
- return false;
- },
- keydown: (view, event) => {
- // Handle Tab Key
- if (event.key === 'Tab') {
- const handled = selectNextTemplate(view.state, view.dispatch);
- if (handled) {
- event.preventDefault();
- return true;
- }
- }
- 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;
- }
- if (event.key === 'Enter') {
- eventDispatch('enter', { event });
- event.preventDefault();
- return true;
- }
- }
- if (event.key === 'Enter') {
- eventDispatch('enter', { event });
- event.preventDefault();
- return true;
- }
- }
- eventDispatch('keydown', { event });
- return false;
- },
- paste: (view, event) => {
- if (event.clipboardData) {
- // Extract plain text from clipboard and paste it without formatting
- const plainText = event.clipboardData.getData('text/plain');
- if (plainText) {
- if (largeTextAsFile) {
- if (plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
- // Dispatch paste event to parent component
- eventDispatch('paste', { event });
- event.preventDefault();
- return true;
- }
- }
- return false;
- }
- // Check if the pasted content contains image files
- const hasImageFile = Array.from(event.clipboardData.files).some((file) =>
- file.type.startsWith('image/')
- );
- // Check for image in dataTransfer items (for cases where files are not available)
- const hasImageItem = Array.from(event.clipboardData.items).some((item) =>
- item.type.startsWith('image/')
- );
- if (hasImageFile) {
- // If there's an image, dispatch the event to the parent
- eventDispatch('paste', { event });
- event.preventDefault();
- return true;
- }
- if (hasImageItem) {
- // If there's an image item, dispatch the event to the parent
- eventDispatch('paste', { event });
- event.preventDefault();
- return true;
- }
- }
- // For all other cases (text, formatted text, etc.), let ProseMirror handle it
- view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor after pasting
- return false;
- }
- }
- }
- });
- selectTemplate();
- });
- onDestroy(() => {
- if (editor) {
- editor.destroy();
- }
- });
- // Update the editor content if the external `value` changes
- $: if (editor && value !== turndownService.turndown(editor.getHTML())) {
- editor.commands.setContent(marked.parse(value)); // Update editor content
- selectTemplate();
- }
- </script>
- <div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />
|