2024-10-18 21:55:39 +00:00
|
|
|
<script lang="ts">
|
2024-11-21 06:46:51 +00:00
|
|
|
import { marked } from 'marked';
|
|
|
|
|
import TurndownService from 'turndown';
|
2025-05-24 06:24:12 +00:00
|
|
|
import { gfm } from 'turndown-plugin-gfm';
|
2024-11-24 04:31:33 +00:00
|
|
|
const turndownService = new TurndownService({
|
2024-11-30 22:15:08 +00:00
|
|
|
codeBlockStyle: 'fenced',
|
|
|
|
|
headingStyle: 'atx'
|
2024-11-24 04:31:33 +00:00
|
|
|
});
|
|
|
|
|
turndownService.escape = (string) => string;
|
2024-11-21 06:46:51 +00:00
|
|
|
|
2025-05-24 06:24:12 +00:00
|
|
|
// Use turndown-plugin-gfm for proper GFM table support
|
|
|
|
|
turndownService.use(gfm);
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
import { onMount, onDestroy } from 'svelte';
|
2024-10-19 05:40:15 +00:00
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
|
const eventDispatch = createEventDispatcher();
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2024-11-29 07:22:53 +00:00
|
|
|
import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
2025-06-18 12:50:16 +00:00
|
|
|
import { Fragment } from 'prosemirror-model';
|
2024-11-29 07:22:53 +00:00
|
|
|
import { Decoration, DecorationSet } from 'prosemirror-view';
|
2024-11-21 06:46:51 +00:00
|
|
|
import { Editor } from '@tiptap/core';
|
|
|
|
|
|
2024-11-29 07:22:53 +00:00
|
|
|
import { AIAutocompletion } from './RichTextInput/AutoCompletion.js';
|
2025-05-24 06:24:12 +00:00
|
|
|
import Table from '@tiptap/extension-table';
|
|
|
|
|
import TableRow from '@tiptap/extension-table-row';
|
|
|
|
|
import TableHeader from '@tiptap/extension-table-header';
|
|
|
|
|
import TableCell from '@tiptap/extension-table-cell';
|
2024-11-29 07:22:53 +00:00
|
|
|
|
2024-11-21 06:56:26 +00:00
|
|
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
|
2024-11-21 06:46:51 +00:00
|
|
|
import Placeholder from '@tiptap/extension-placeholder';
|
2025-05-24 06:24:12 +00:00
|
|
|
import { all, createLowlight } from 'lowlight';
|
|
|
|
|
import StarterKit from '@tiptap/starter-kit';
|
2024-11-21 06:46:51 +00:00
|
|
|
import Highlight from '@tiptap/extension-highlight';
|
|
|
|
|
import Typography from '@tiptap/extension-typography';
|
2024-11-21 06:56:26 +00:00
|
|
|
|
2024-11-19 04:50:12 +00:00
|
|
|
import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2025-03-06 03:36:30 +00:00
|
|
|
export let oncompositionstart = (e) => {};
|
|
|
|
|
export let oncompositionend = (e) => {};
|
2025-05-03 18:53:23 +00:00
|
|
|
export let onChange = (e) => {};
|
2025-03-06 03:36:30 +00:00
|
|
|
|
2024-11-21 06:56:26 +00:00
|
|
|
// create a lowlight instance with all languages loaded
|
|
|
|
|
const lowlight = createLowlight(all);
|
|
|
|
|
|
2024-10-18 21:55:39 +00:00
|
|
|
export let className = 'input-prose';
|
|
|
|
|
export let placeholder = 'Type here...';
|
2025-05-03 18:53:23 +00:00
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
export let id = '';
|
2025-05-03 18:53:23 +00:00
|
|
|
export let value = '';
|
|
|
|
|
export let html = '';
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
export let json = false;
|
2025-02-14 06:37:01 +00:00
|
|
|
export let raw = false;
|
2025-05-04 21:35:43 +00:00
|
|
|
export let editable = true;
|
2025-02-14 06:37:01 +00:00
|
|
|
|
2024-11-30 23:05:08 +00:00
|
|
|
export let preserveBreaks = false;
|
2024-11-29 08:16:49 +00:00
|
|
|
export let generateAutoCompletion: Function = async () => null;
|
2024-11-29 07:22:53 +00:00
|
|
|
export let autocomplete = false;
|
2024-11-21 06:46:51 +00:00
|
|
|
export let messageInput = false;
|
|
|
|
|
export let shiftEnter = false;
|
|
|
|
|
export let largeTextAsFile = false;
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
let element;
|
|
|
|
|
let editor;
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2024-11-22 05:32:19 +00:00
|
|
|
const options = {
|
|
|
|
|
throwOnError: false
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-04 21:35:43 +00:00
|
|
|
$: if (editor) {
|
|
|
|
|
editor.setOptions({
|
|
|
|
|
editable: editable
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$: if (value === null && html !== null && editor) {
|
|
|
|
|
editor.commands.setContent(html);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
// Function to find the next template in the document
|
2024-10-19 07:23:59 +00:00
|
|
|
function findNextTemplate(doc, from = 0) {
|
2025-02-14 20:40:42 +00:00
|
|
|
const patterns = [{ start: '{{', end: '}}' }];
|
2024-10-19 07:23:59 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
// Function to select the next template in the document
|
2024-10-19 07:23:59 +00:00
|
|
|
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;
|
|
|
|
|
}
|
2024-10-19 10:15:40 +00:00
|
|
|
return false;
|
2024-10-19 07:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
export const setContent = (content) => {
|
|
|
|
|
editor.commands.setContent(content);
|
|
|
|
|
};
|
2024-10-25 18:51:49 +00:00
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2024-11-22 06:20:57 +00:00
|
|
|
onMount(async () => {
|
2025-02-14 06:37:01 +00:00
|
|
|
let content = value;
|
|
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
if (!json) {
|
|
|
|
|
if (preserveBreaks) {
|
|
|
|
|
turndownService.addRule('preserveBreaks', {
|
|
|
|
|
filter: 'br', // Target <br> elements
|
|
|
|
|
replacement: function (content) {
|
|
|
|
|
return '<br/>';
|
2025-02-14 06:37:01 +00:00
|
|
|
}
|
2025-05-03 18:53:23 +00:00
|
|
|
});
|
2024-11-22 06:20:57 +00:00
|
|
|
}
|
2024-11-22 06:15:04 +00:00
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
if (!raw) {
|
|
|
|
|
async function tryParse(value, attempts = 3, interval = 100) {
|
|
|
|
|
try {
|
|
|
|
|
// Try parsing the value
|
|
|
|
|
return marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
|
|
|
|
|
breaks: false
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// If no attempts remain, fallback to plain text
|
|
|
|
|
if (attempts <= 1) {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
// Wait for the interval, then retry
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
|
|
|
return tryParse(value, attempts - 1, interval); // Recursive call
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Usage example
|
|
|
|
|
content = await tryParse(value);
|
|
|
|
|
}
|
2025-05-04 19:04:15 +00:00
|
|
|
} else {
|
|
|
|
|
if (html && !content) {
|
|
|
|
|
content = html;
|
|
|
|
|
}
|
2025-02-14 06:37:01 +00:00
|
|
|
}
|
2024-11-22 06:20:57 +00:00
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
console.log('content', content);
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
editor = new Editor({
|
|
|
|
|
element: element,
|
2024-11-21 06:56:26 +00:00
|
|
|
extensions: [
|
|
|
|
|
StarterKit,
|
|
|
|
|
CodeBlockLowlight.configure({
|
|
|
|
|
lowlight
|
|
|
|
|
}),
|
|
|
|
|
Highlight,
|
|
|
|
|
Typography,
|
2024-11-29 07:22:53 +00:00
|
|
|
Placeholder.configure({ placeholder }),
|
2025-05-24 06:24:12 +00:00
|
|
|
Table.configure({ resizable: true }),
|
|
|
|
|
TableRow,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableCell,
|
2024-11-29 07:26:09 +00:00
|
|
|
...(autocomplete
|
|
|
|
|
? [
|
|
|
|
|
AIAutocompletion.configure({
|
|
|
|
|
generateCompletion: async (text) => {
|
|
|
|
|
if (text.trim().length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-29 08:16:49 +00:00
|
|
|
const suggestion = await generateAutoCompletion(text).catch(() => null);
|
|
|
|
|
if (!suggestion || suggestion.trim().length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return suggestion;
|
2024-11-29 07:26:09 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
]
|
|
|
|
|
: [])
|
2024-11-21 06:56:26 +00:00
|
|
|
],
|
2024-11-22 05:32:19 +00:00
|
|
|
content: content,
|
2024-11-30 23:44:04 +00:00
|
|
|
autofocus: messageInput ? true : false,
|
2024-11-21 06:46:51 +00:00
|
|
|
onTransaction: () => {
|
|
|
|
|
// force re-render so `editor.isActive` works as expected
|
|
|
|
|
editor = editor;
|
|
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
html = editor.getHTML();
|
2024-12-11 22:07:25 +00:00
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
onChange({
|
|
|
|
|
html: editor.getHTML(),
|
|
|
|
|
json: editor.getJSON(),
|
|
|
|
|
md: turndownService.turndown(editor.getHTML())
|
|
|
|
|
});
|
2024-11-24 07:57:05 +00:00
|
|
|
|
2025-05-03 18:53:23 +00:00
|
|
|
if (json) {
|
|
|
|
|
value = editor.getJSON();
|
|
|
|
|
} else {
|
|
|
|
|
if (!raw) {
|
|
|
|
|
let newValue = turndownService
|
|
|
|
|
.turndown(
|
|
|
|
|
editor
|
|
|
|
|
.getHTML()
|
|
|
|
|
.replace(/<p><\/p>/g, '<br/>')
|
|
|
|
|
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
|
|
|
|
)
|
|
|
|
|
.replace(/\u00a0/g, ' ');
|
|
|
|
|
|
|
|
|
|
if (!preserveBreaks) {
|
|
|
|
|
newValue = newValue.replace(/<br\/>/g, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value !== newValue) {
|
|
|
|
|
value = newValue;
|
|
|
|
|
|
|
|
|
|
// check if the node is paragraph as well
|
|
|
|
|
if (editor.isActive('paragraph')) {
|
|
|
|
|
if (value === '') {
|
|
|
|
|
editor.commands.clearContent();
|
|
|
|
|
}
|
2025-02-14 06:37:01 +00:00
|
|
|
}
|
2024-11-30 22:15:08 +00:00
|
|
|
}
|
2025-05-03 18:53:23 +00:00
|
|
|
} else {
|
|
|
|
|
value = editor.getHTML();
|
2024-11-24 07:57:05 +00:00
|
|
|
}
|
2024-11-21 06:46:51 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
editorProps: {
|
|
|
|
|
attributes: { id },
|
|
|
|
|
handleDOMEvents: {
|
2025-03-06 03:36:30 +00:00
|
|
|
compositionstart: (view, event) => {
|
|
|
|
|
oncompositionstart(event);
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
compositionend: (view, event) => {
|
|
|
|
|
oncompositionend(event);
|
|
|
|
|
return false;
|
|
|
|
|
},
|
2024-11-21 06:46:51 +00:00
|
|
|
focus: (view, event) => {
|
|
|
|
|
eventDispatch('focus', { event });
|
|
|
|
|
return false;
|
2024-10-19 05:56:04 +00:00
|
|
|
},
|
2024-11-26 06:43:34 +00:00
|
|
|
keyup: (view, event) => {
|
|
|
|
|
eventDispatch('keyup', { event });
|
2024-10-19 05:56:04 +00:00
|
|
|
return false;
|
|
|
|
|
},
|
2024-11-21 06:46:51 +00:00
|
|
|
keydown: (view, event) => {
|
2024-11-30 23:44:04 +00:00
|
|
|
if (messageInput) {
|
|
|
|
|
// Handle Tab Key
|
|
|
|
|
if (event.key === 'Tab') {
|
|
|
|
|
const handled = selectNextTemplate(view.state, view.dispatch);
|
|
|
|
|
if (handled) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-11-21 06:46:51 +00:00
|
|
|
}
|
2024-10-18 21:55:39 +00:00
|
|
|
|
2024-11-21 06:56:26 +00:00
|
|
|
if (event.key === 'Enter') {
|
2025-05-28 10:27:34 +00:00
|
|
|
const isCtrlPressed = event.ctrlKey || event.metaKey; // metaKey is for Cmd key on Mac
|
|
|
|
|
if (event.shiftKey && !isCtrlPressed) {
|
2025-05-27 22:10:54 +00:00
|
|
|
editor.commands.setHardBreak(); // Insert a hard break
|
|
|
|
|
view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// Check if the current selection is inside a structured block (like codeBlock or list)
|
|
|
|
|
const { state } = view;
|
|
|
|
|
const { $head } = state.selection;
|
|
|
|
|
|
|
|
|
|
// Recursive function to check ancestors for specific node types
|
|
|
|
|
function isInside(nodeTypes: string[]): boolean {
|
|
|
|
|
let currentNode = $head;
|
|
|
|
|
while (currentNode) {
|
|
|
|
|
if (nodeTypes.includes(currentNode.parent.type.name)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (!currentNode.depth) break; // Stop if we reach the top
|
|
|
|
|
currentNode = state.doc.resolve(currentNode.before()); // Move to the parent node
|
2024-11-21 07:14:06 +00:00
|
|
|
}
|
2025-05-27 22:10:54 +00:00
|
|
|
return false;
|
2024-11-21 07:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 22:10:54 +00:00
|
|
|
const isInCodeBlock = isInside(['codeBlock']);
|
|
|
|
|
const isInList = isInside(['listItem', 'bulletList', 'orderedList']);
|
|
|
|
|
const isInHeading = isInside(['heading']);
|
2024-11-21 07:14:06 +00:00
|
|
|
|
2025-05-27 22:10:54 +00:00
|
|
|
if (isInCodeBlock || isInList || isInHeading) {
|
|
|
|
|
// Let ProseMirror handle the normal Enter behavior
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-11-21 06:56:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
// Handle shift + Enter for a line break
|
|
|
|
|
if (shiftEnter) {
|
2024-11-26 06:43:34 +00:00
|
|
|
if (event.key === 'Enter' && event.shiftKey && !event.ctrlKey && !event.metaKey) {
|
2024-11-21 06:46:51 +00:00
|
|
|
editor.commands.setHardBreak(); // Insert a hard break
|
2024-11-21 06:56:26 +00:00
|
|
|
view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
|
2024-11-21 06:46:51 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-25 18:51:49 +00:00
|
|
|
}
|
2024-11-21 06:46:51 +00:00
|
|
|
eventDispatch('keydown', { event });
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
paste: (view, event) => {
|
|
|
|
|
if (event.clipboardData) {
|
|
|
|
|
const plainText = event.clipboardData.getData('text/plain');
|
|
|
|
|
if (plainText) {
|
2025-06-18 12:50:16 +00:00
|
|
|
if (largeTextAsFile && plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
|
|
|
|
|
// Delegate handling of large text pastes to the parent component.
|
|
|
|
|
eventDispatch('paste', { event });
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
2024-11-21 06:46:51 +00:00
|
|
|
}
|
2025-06-18 12:50:16 +00:00
|
|
|
|
|
|
|
|
// Workaround for mobile WebViews that strip line breaks when pasting from
|
|
|
|
|
// clipboard suggestions (e.g., Gboard clipboard history).
|
|
|
|
|
const isMobile = /Android|iPhone|iPad|iPod|Windows Phone/i.test(
|
|
|
|
|
navigator.userAgent
|
|
|
|
|
);
|
|
|
|
|
const isWebView =
|
|
|
|
|
typeof window !== 'undefined' &&
|
|
|
|
|
(/wv/i.test(navigator.userAgent) || // Standard Android WebView flag
|
|
|
|
|
(navigator.userAgent.includes('Android') &&
|
|
|
|
|
!navigator.userAgent.includes('Chrome')) || // Other generic Android WebViews
|
|
|
|
|
(navigator.userAgent.includes('Safari') &&
|
|
|
|
|
!navigator.userAgent.includes('Version'))); // iOS WebView (in-app browsers)
|
|
|
|
|
|
|
|
|
|
if (isMobile && isWebView && plainText.includes('\n')) {
|
|
|
|
|
// Manually deconstruct the pasted text and insert it with hard breaks
|
|
|
|
|
// to preserve the multi-line formatting.
|
|
|
|
|
const { state, dispatch } = view;
|
|
|
|
|
const { from, to } = state.selection;
|
|
|
|
|
|
|
|
|
|
const lines = plainText.split('\n');
|
|
|
|
|
const nodes = [];
|
|
|
|
|
|
|
|
|
|
lines.forEach((line, index) => {
|
|
|
|
|
if (index > 0) {
|
|
|
|
|
nodes.push(state.schema.nodes.hardBreak.create());
|
|
|
|
|
}
|
|
|
|
|
if (line.length > 0) {
|
|
|
|
|
nodes.push(state.schema.text(line));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const fragment = Fragment.fromArray(nodes);
|
|
|
|
|
const tr = state.tr.replaceWith(from, to, fragment);
|
|
|
|
|
dispatch(tr.scrollIntoView());
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// Let ProseMirror handle normal text paste in non-problematic environments.
|
2024-11-21 06:46:51 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2024-10-19 06:54:35 +00:00
|
|
|
|
2025-06-18 12:50:16 +00:00
|
|
|
// Delegate image paste handling to the parent component.
|
2024-11-21 06:46:51 +00:00
|
|
|
const hasImageFile = Array.from(event.clipboardData.files).some((file) =>
|
|
|
|
|
file.type.startsWith('image/')
|
|
|
|
|
);
|
2025-06-18 12:50:16 +00:00
|
|
|
// Fallback for cases where an image is in dataTransfer.items but not clipboardData.files.
|
2024-11-21 06:46:51 +00:00
|
|
|
const hasImageItem = Array.from(event.clipboardData.items).some((item) =>
|
|
|
|
|
item.type.startsWith('image/')
|
|
|
|
|
);
|
2025-06-18 12:50:16 +00:00
|
|
|
if (hasImageFile || hasImageItem) {
|
2024-11-21 06:46:51 +00:00
|
|
|
eventDispatch('paste', { event });
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-10-19 06:54:35 +00:00
|
|
|
}
|
2025-06-18 12:50:16 +00:00
|
|
|
// For all other cases, let ProseMirror perform its default paste behavior.
|
|
|
|
|
view.dispatch(view.state.tr.scrollIntoView());
|
2024-11-21 06:46:51 +00:00
|
|
|
return false;
|
2024-10-19 21:56:30 +00:00
|
|
|
}
|
2024-10-19 05:40:15 +00:00
|
|
|
}
|
2024-11-21 06:46:51 +00:00
|
|
|
}
|
2024-10-18 21:55:39 +00:00
|
|
|
});
|
2024-10-19 07:23:59 +00:00
|
|
|
|
2024-11-30 23:44:04 +00:00
|
|
|
if (messageInput) {
|
|
|
|
|
selectTemplate();
|
|
|
|
|
}
|
2024-11-21 06:46:51 +00:00
|
|
|
});
|
2024-10-18 21:55:39 +00:00
|
|
|
|
|
|
|
|
onDestroy(() => {
|
2024-11-21 06:46:51 +00:00
|
|
|
if (editor) {
|
|
|
|
|
editor.destroy();
|
|
|
|
|
}
|
2024-10-18 21:55:39 +00:00
|
|
|
});
|
2024-11-21 06:46:51 +00:00
|
|
|
|
2025-05-04 08:02:10 +00:00
|
|
|
$: if (value !== null && editor) {
|
2025-05-03 18:53:23 +00:00
|
|
|
onValueChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const onValueChange = () => {
|
|
|
|
|
if (!editor) return;
|
|
|
|
|
|
|
|
|
|
if (json) {
|
|
|
|
|
if (JSON.stringify(value) !== JSON.stringify(editor.getJSON())) {
|
|
|
|
|
editor.commands.setContent(value);
|
|
|
|
|
selectTemplate();
|
|
|
|
|
}
|
2025-02-14 06:37:01 +00:00
|
|
|
} else {
|
2025-05-03 19:05:25 +00:00
|
|
|
if (raw) {
|
|
|
|
|
if (value !== editor.getHTML()) {
|
|
|
|
|
editor.commands.setContent(value);
|
|
|
|
|
selectTemplate();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
value !==
|
|
|
|
|
turndownService
|
|
|
|
|
.turndown(
|
|
|
|
|
(preserveBreaks
|
|
|
|
|
? editor.getHTML().replace(/<p><\/p>/g, '<br/>')
|
|
|
|
|
: editor.getHTML()
|
|
|
|
|
).replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
|
|
|
|
)
|
|
|
|
|
.replace(/\u00a0/g, ' ')
|
|
|
|
|
) {
|
|
|
|
|
preserveBreaks
|
|
|
|
|
? editor.commands.setContent(value)
|
|
|
|
|
: editor.commands.setContent(
|
|
|
|
|
marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
|
|
|
|
|
breaks: false
|
|
|
|
|
})
|
|
|
|
|
); // Update editor content
|
|
|
|
|
|
|
|
|
|
selectTemplate();
|
|
|
|
|
}
|
2025-05-03 18:53:23 +00:00
|
|
|
}
|
2025-02-14 06:37:01 +00:00
|
|
|
}
|
2025-05-03 18:53:23 +00:00
|
|
|
};
|
2024-10-18 21:55:39 +00:00
|
|
|
</script>
|
|
|
|
|
|
2024-11-21 06:46:51 +00:00
|
|
|
<div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />
|