open-webui/src/lib/components/common/RichTextInput.svelte

1161 lines
32 KiB
Svelte
Raw Normal View History

2024-10-18 21:55:39 +00:00
<script lang="ts">
2024-11-21 06:46:51 +00:00
import { marked } from 'marked';
2025-10-21 21:59:52 +00:00
import DOMPurify from 'dompurify';
2025-07-08 23:43:41 +00:00
marked.use({
breaks: true,
gfm: true,
renderer: {
list(body, ordered, start) {
const isTaskList = body.includes('data-checked=');
if (isTaskList) {
return `<ul data-type="taskList">${body}</ul>`;
}
const type = ordered ? 'ol' : 'ul';
const startatt = ordered && start !== 1 ? ` start="${start}"` : '';
return `<${type}${startatt}>${body}</${type}>`;
},
listitem(text, task, checked) {
if (task) {
const checkedAttr = checked ? 'true' : 'false';
return `<li data-type="taskItem" data-checked="${checkedAttr}">${text}</li>`;
}
return `<li>${text}</li>`;
}
}
});
2025-07-07 17:43:28 +00:00
2024-11-21 06:46:51 +00:00
import TurndownService from 'turndown';
2025-08-04 13:27:28 +00:00
import { gfm } from '@joplin/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;
2025-08-04 13:27:28 +00:00
// Use turndown-plugin-gfm for proper GFM table support
turndownService.use(gfm);
2025-08-04 13:27:28 +00:00
// Add custom table header rule before using GFM plugin
turndownService.addRule('tableHeaders', {
filter: 'th',
replacement: function (content, node) {
return content;
}
});
// Add custom table rule to handle headers properly
turndownService.addRule('tables', {
filter: 'table',
replacement: function (content, node) {
// Extract rows
const rows = Array.from(node.querySelectorAll('tr'));
if (rows.length === 0) return content;
let markdown = '\n';
rows.forEach((row, rowIndex) => {
const cells = Array.from(row.querySelectorAll('th, td'));
const cellContents = cells.map((cell) => {
// Get the text content and clean it up
let cellContent = turndownService.turndown(cell.innerHTML).trim();
// Remove extra paragraph tags that might be added
cellContent = cellContent.replace(/^\n+|\n+$/g, '');
return cellContent;
});
// Add the row
markdown += '| ' + cellContents.join(' | ') + ' |\n';
// Add separator after first row (which should be headers)
if (rowIndex === 0) {
const separator = cells.map(() => '---').join(' | ');
markdown += '| ' + separator + ' |\n';
}
});
return markdown + '\n';
}
});
2025-07-08 19:57:34 +00:00
turndownService.addRule('taskListItems', {
filter: (node) =>
node.nodeName === 'LI' &&
(node.getAttribute('data-checked') === 'true' ||
node.getAttribute('data-checked') === 'false'),
replacement: function (content, node) {
const checked = node.getAttribute('data-checked') === 'true';
content = content.replace(/^\s+/, '');
return `- [${checked ? 'x' : ' '}] ${content}\n`;
}
});
2025-09-14 22:08:31 +00:00
// Convert TipTap mention spans -> <@id>
turndownService.addRule('mentions', {
filter: (node) => node.nodeName === 'SPAN' && node.getAttribute('data-type') === 'mention',
replacement: (_content, node: HTMLElement) => {
const id = node.getAttribute('data-id') || '';
// TipTap stores the trigger char in data-mention-suggestion-char (usually "@")
const ch = node.getAttribute('data-mention-suggestion-char') || '@';
// Emit <@id> style, e.g. <@llama3.2:latest>
return `<${ch}${id}>`;
}
});
2025-07-08 23:11:51 +00:00
import { onMount, onDestroy, tick, getContext } from 'svelte';
2024-10-19 05:40:15 +00:00
import { createEventDispatcher } from 'svelte';
2025-07-08 23:11:51 +00:00
const i18n = getContext('i18n');
2024-10-19 05:40:15 +00:00
const eventDispatch = createEventDispatcher();
2024-10-18 21:55:39 +00:00
2025-07-07 17:43:28 +00:00
import { Fragment, DOMParser } from 'prosemirror-model';
import { EditorState, Plugin, PluginKey, TextSelection, Selection } from 'prosemirror-state';
2025-07-19 12:35:03 +00:00
import { Decoration, DecorationSet } from 'prosemirror-view';
2025-09-14 22:08:31 +00:00
import { Editor, Extension, mergeAttributes } from '@tiptap/core';
2024-11-21 06:46:51 +00:00
2024-11-29 07:22:53 +00:00
import { AIAutocompletion } from './RichTextInput/AutoCompletion.js';
import StarterKit from '@tiptap/starter-kit';
2025-07-08 23:11:51 +00:00
2025-07-17 10:43:42 +00:00
// Bubble and Floating menus are currently fixed to v2 due to styling issues in v3
// TODO: Update to v3 when styling issues are resolved
2025-07-08 23:11:51 +00:00
import BubbleMenu from '@tiptap/extension-bubble-menu';
import FloatingMenu from '@tiptap/extension-floating-menu';
2025-07-17 10:43:42 +00:00
import { TableKit } from '@tiptap/extension-table';
import { ListKit } from '@tiptap/extension-list';
import { Placeholder, CharacterCount } from '@tiptap/extensions';
2025-07-17 13:36:06 +00:00
import Image from './RichTextInput/Image/index.js';
// import TiptapImage from '@tiptap/extension-image';
import FileHandler from '@tiptap/extension-file-handler';
2025-07-17 10:43:42 +00:00
import Typography from '@tiptap/extension-typography';
import Highlight from '@tiptap/extension-highlight';
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
import Mention from '@tiptap/extension-mention';
2025-09-12 16:31:57 +00:00
import FormattingButtons from './RichTextInput/FormattingButtons.svelte';
2024-11-21 06:56:26 +00:00
import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
import { createLowlight } from 'lowlight';
import hljs from 'highlight.js';
import type { SocketIOCollaborationProvider } from './RichTextInput/Collaboration';
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
2025-09-24 11:49:39 +00:00
const lowlight = createLowlight(
hljs.listLanguages().reduce(
(obj, lang) => {
obj[lang] = () => hljs.getLanguage(lang);
return obj;
},
{} as Record<string, any>
)
);
2024-11-21 06:56:26 +00:00
export let editor: Editor | null = null;
2025-07-09 09:38:54 +00:00
2025-07-11 08:15:13 +00:00
export let socket = null;
export let user = null;
2025-07-17 13:36:06 +00:00
export let files = [];
2025-07-11 08:15:13 +00:00
export let documentId = '';
2025-12-10 02:50:27 +00:00
export let className = 'input-prose min-h-fit';
2025-09-24 15:09:59 +00:00
export let placeholder = $i18n.t('Type here...');
2025-09-16 16:13:15 +00:00
let _placeholder = placeholder;
$: if (placeholder !== _placeholder) {
setPlaceholder();
}
const setPlaceholder = () => {
_placeholder = placeholder;
if (editor) {
editor?.view.dispatch(editor.state.tr);
}
};
2025-09-12 16:54:34 +00:00
export let richText = true;
2025-09-23 00:02:37 +00:00
export let dragHandle = false;
2025-07-09 19:22:59 +00:00
export let link = false;
2025-07-17 13:36:06 +00:00
export let image = false;
export let fileHandler = false;
2025-09-12 16:31:57 +00:00
export let suggestions = null;
2025-07-18 11:58:06 +00:00
export let onFileDrop = (currentEditor, files, pos) => {
files.forEach((file) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
currentEditor
.chain()
.insertContentAt(pos, {
type: 'image',
attrs: {
src: fileReader.result
}
})
.focus()
.run();
};
});
};
export let onFilePaste = (currentEditor, files, htmlContent) => {
files.forEach((file) => {
if (htmlContent) {
// if there is htmlContent, stop manual insertion & let other extensions handle insertion via inputRule
// you could extract the pasted file from this url string and upload it to a server for example
console.log(htmlContent); // eslint-disable-line no-console
return false;
}
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
currentEditor
.chain()
.insertContentAt(currentEditor.state.selection.anchor, {
type: 'image',
attrs: {
src: fileReader.result
}
})
.focus()
.run();
};
});
};
2025-07-19 12:35:03 +00:00
export let onSelectionUpdate = (e) => {};
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-07-11 08:15:13 +00:00
export let collaboration = false;
2025-02-14 06:37:01 +00:00
2025-08-06 08:21:18 +00:00
export let showFormattingToolbar = true;
2025-07-08 23:11:51 +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;
2025-07-07 17:43:28 +00:00
export let insertPromptAsRichText = false;
2025-07-14 10:37:28 +00:00
export let floatingMenuPlacement = 'bottom-start';
2024-10-18 21:55:39 +00:00
2025-07-11 19:59:48 +00:00
let content = null;
let htmlValue = '';
let jsonValue = '';
let mdValue = '';
let provider: SocketIOCollaborationProvider | null = null;
2025-07-11 19:59:48 +00:00
let floatingMenuElement: Element | null = null;
let bubbleMenuElement: Element | null = null;
let element: Element | null = null;
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);
}
export const getWordAtDocPos = () => {
if (!editor) return '';
const { state } = editor.view;
const pos = state.selection.from;
const doc = state.doc;
const resolvedPos = doc.resolve(pos);
const textBlock = resolvedPos.parent;
const paraStart = resolvedPos.start();
const text = textBlock.textContent;
const offset = resolvedPos.parentOffset;
let wordStart = offset,
wordEnd = offset;
while (wordStart > 0 && !/\s/.test(text[wordStart - 1])) wordStart--;
while (wordEnd < text.length && !/\s/.test(text[wordEnd])) wordEnd++;
const word = text.slice(wordStart, wordEnd);
return word;
};
// Returns {start, end} of the word at pos
function getWordBoundsAtPos(doc, pos) {
const resolvedPos = doc.resolve(pos);
const textBlock = resolvedPos.parent;
const paraStart = resolvedPos.start();
const text = textBlock.textContent;
const offset = resolvedPos.parentOffset;
let wordStart = offset,
wordEnd = offset;
while (wordStart > 0 && !/\s/.test(text[wordStart - 1])) wordStart--;
while (wordEnd < text.length && !/\s/.test(text[wordEnd])) wordEnd++;
return {
start: paraStart + wordStart,
end: paraStart + wordEnd
};
}
export const replaceCommandWithText = async (text) => {
const { state, dispatch } = editor.view;
const { selection } = state;
const pos = selection.from;
// Get the plain text of this document
// const docText = state.doc.textBetween(0, state.doc.content.size, '\n', '\n');
// Find the word boundaries at cursor
const { start, end } = getWordBoundsAtPos(state.doc, pos);
let tr = state.tr;
2025-07-07 17:43:28 +00:00
if (insertPromptAsRichText) {
2025-10-21 21:59:52 +00:00
const htmlContent = DOMPurify.sanitize(
marked
.parse(text, {
breaks: true,
gfm: true
})
.trim()
);
2025-07-07 17:43:28 +00:00
// Create a temporary div to parse HTML
const tempDiv = document.createElement('div');
tempDiv.innerHTML = htmlContent;
// Convert HTML to ProseMirror nodes
const fragment = DOMParser.fromSchema(state.schema).parse(tempDiv);
// Extract just the content, not the wrapper paragraphs
const content = fragment.content;
let nodesToInsert = [];
content.forEach((node) => {
if (node.type.name === 'paragraph') {
// If it's a paragraph, extract its content
nodesToInsert.push(...node.content.content);
} else {
nodesToInsert.push(node);
}
});
tr = tr.replaceWith(start, end, nodesToInsert);
// Calculate new position
const newPos = start + nodesToInsert.reduce((sum, node) => sum + node.nodeSize, 0);
tr = tr.setSelection(Selection.near(tr.doc.resolve(newPos)));
} else {
2025-07-07 17:43:28 +00:00
if (text.includes('\n')) {
// Split the text into lines and create a <p> node for each line
const lines = text.split('\n');
const nodes = lines.map(
(line, index) =>
index === 0
? state.schema.text(line ? line : []) // First line is plain text
: state.schema.nodes.paragraph.create({}, line ? state.schema.text(line) : undefined) // Subsequent lines are paragraphs
);
// Build and dispatch the transaction to replace the word at cursor
tr = tr.replaceWith(start, end, nodes);
let newSelectionPos;
// +1 because the insert happens at start, so last para starts at (start + sum of all previous nodes' sizes)
let lastPos = start;
for (let i = 0; i < nodes.length; i++) {
lastPos += nodes[i].nodeSize;
}
// Place cursor inside the last paragraph at its end
newSelectionPos = lastPos;
tr = tr.setSelection(TextSelection.near(tr.doc.resolve(newSelectionPos)));
} else {
tr = tr.replaceWith(
start,
end, // replace this range
text !== '' ? state.schema.text(text) : []
);
tr = tr.setSelection(
state.selection.constructor.near(tr.doc.resolve(start + text.length + 1))
);
}
}
dispatch(tr);
await tick();
// selectNextTemplate(state, dispatch);
};
export const setText = (text: string) => {
if (!editor) return;
text = text.replaceAll('\n\n', '\n');
2025-07-18 13:49:24 +00:00
// reset the editor content
editor.commands.clearContent();
const { state, view } = editor;
2025-07-06 13:32:03 +00:00
const { schema, tr } = state;
if (text.includes('\n')) {
// Multiple lines: make paragraphs
const lines = text.split('\n');
// Map each line to a paragraph node (empty lines -> empty paragraph)
const nodes = lines.map((line) =>
schema.nodes.paragraph.create({}, line ? schema.text(line) : undefined)
);
// Create a document fragment containing all parsed paragraphs
const fragment = Fragment.fromArray(nodes);
// Replace current selection with these paragraphs
tr.replaceSelectionWith(fragment, false /* don't select new */);
view.dispatch(tr);
} else if (text === '') {
2025-07-06 13:32:03 +00:00
// Empty: replace with empty paragraph using tr
2025-07-06 17:40:53 +00:00
editor.commands.clearContent();
} else {
2025-07-06 13:32:03 +00:00
// Single line: create paragraph with text
const paragraph = schema.nodes.paragraph.create({}, schema.text(text));
tr.replaceSelectionWith(paragraph, false);
view.dispatch(tr);
}
selectNextTemplate(editor.view.state, editor.view.dispatch);
2025-07-06 13:32:03 +00:00
focus();
};
2025-07-07 17:22:07 +00:00
export const insertContent = (content) => {
if (!editor) return;
const { state, view } = editor;
const { schema, tr } = state;
2025-07-08 23:39:57 +00:00
// If content is a string, convert it to a ProseMirror node
const htmlContent = marked.parse(content);
2025-07-07 17:22:07 +00:00
// insert the HTML content at the current selection
editor.commands.insertContent(htmlContent);
focus();
};
export const replaceVariables = (variables) => {
if (!editor) return;
const { state, view } = editor;
const { doc } = state;
// Create a transaction to replace variables
let tr = state.tr;
let offset = 0; // Track position changes due to text length differences
// Collect all replacements first to avoid position conflicts
const replacements = [];
doc.descendants((node, pos) => {
if (node.isText && node.text) {
const text = node.text;
const replacedText = text.replace(/{{\s*([^|}]+)(?:\|[^}]*)?\s*}}/g, (match, varName) => {
const trimmedVarName = varName.trim();
return variables.hasOwnProperty(trimmedVarName)
? String(variables[trimmedVarName])
: match;
});
if (replacedText !== text) {
replacements.push({
from: pos,
to: pos + text.length,
text: replacedText
});
}
}
});
// Apply replacements in reverse order to maintain correct positions
replacements.reverse().forEach(({ from, to, text }) => {
tr = tr.replaceWith(from, to, text !== '' ? state.schema.text(text) : []);
});
// Only dispatch if there are changes
if (replacements.length > 0) {
view.dispatch(tr);
}
};
2025-07-04 19:40:31 +00:00
export const focus = () => {
if (editor) {
2025-09-24 11:49:39 +00:00
try {
editor.view?.focus();
// Scroll to the current selection
editor.view?.dispatch(editor.view.state.tr.scrollIntoView());
} catch (e) {
// sometimes focusing throws an error, ignore
console.warn('Error focusing editor', e);
}
2025-07-04 19:40:31 +00:00
}
};
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) {
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);
2025-07-03 17:28:09 +00:00
// Scroll to the selected template
dispatch(
tr.scrollIntoView().setMeta('preventScroll', true) // Prevent default scrolling behavior
);
2024-10-19 07:23:59 +00:00
}
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) {
2025-07-06 13:32:03 +00:00
editor.commands.focus('end');
2024-11-21 06:46:51 +00:00
}
}, 0);
}
};
2024-10-18 21:55:39 +00:00
2025-07-19 12:35:03 +00:00
const SelectionDecoration = Extension.create({
name: 'selectionDecoration',
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('selection'),
props: {
decorations: (state) => {
const { selection } = state;
const { focused } = this.editor;
if (focused || selection.empty) {
return null;
}
return DecorationSet.create(state.doc, [
Decoration.inline(selection.from, selection.to, {
class: 'editor-selection'
})
]);
}
}
})
];
}
});
2025-09-23 00:02:37 +00:00
import { listDragHandlePlugin } from './RichTextInput/listDragHandlePlugin.js';
const ListItemDragHandle = Extension.create({
name: 'listItemDragHandle',
addProseMirrorPlugins() {
return [
listDragHandlePlugin({
itemTypeNames: ['listItem', 'taskItem'],
getEditor: () => this.editor
})
];
}
});
2024-11-22 06:20:57 +00:00
onMount(async () => {
2025-07-11 19:59:48 +00:00
content = value;
2025-02-14 06:37:01 +00:00
2025-07-06 13:32:03 +00:00
if (json) {
if (!content) {
content = html ? html : null;
}
} else {
2025-05-03 18:53:23 +00:00
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-02-14 06:37:01 +00:00
}
2024-11-22 06:20:57 +00:00
if (collaboration && documentId && socket && user) {
const { SocketIOCollaborationProvider } = await import('./RichTextInput/Collaboration');
provider = new SocketIOCollaborationProvider(documentId, socket, user, content);
2025-07-11 08:15:13 +00:00
}
2024-11-21 06:46:51 +00:00
editor = new Editor({
element: element,
2024-11-21 06:56:26 +00:00
extensions: [
2025-07-17 13:36:06 +00:00
StarterKit.configure({
link: link
}),
2025-09-23 00:02:37 +00:00
...(dragHandle ? [ListItemDragHandle] : []),
2025-09-24 15:09:59 +00:00
Placeholder.configure({ placeholder: () => _placeholder, showOnlyWhenEditable: false }),
2025-07-19 12:35:03 +00:00
SelectionDecoration,
2025-07-17 10:43:42 +00:00
2025-09-12 16:54:34 +00:00
...(richText
? [
CodeBlockLowlight.configure({
lowlight
}),
Typography,
TableKit.configure({
table: { resizable: true }
}),
ListKit.configure({
taskItem: {
nested: true
}
})
]
: []),
2025-09-12 16:31:57 +00:00
...(suggestions
? [
Mention.configure({
HTMLAttributes: { class: 'mention' },
suggestions: suggestions
})
]
: []),
2025-08-08 00:10:58 +00:00
2025-07-09 18:33:36 +00:00
CharacterCount.configure({}),
2025-07-17 13:36:06 +00:00
...(image ? [Image] : []),
...(fileHandler
? [
FileHandler.configure({
2025-07-18 11:58:06 +00:00
onDrop: onFileDrop,
onPaste: onFilePaste
2025-07-17 13:36:06 +00:00
})
]
: []),
...(autocomplete
2024-11-29 07:26:09 +00:00
? [
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
}
})
]
2025-07-08 23:11:51 +00:00
: []),
2025-09-12 16:54:34 +00:00
...(richText && showFormattingToolbar
2025-07-08 23:11:51 +00:00
? [
BubbleMenu.configure({
element: bubbleMenuElement,
tippyOptions: {
duration: 100,
arrow: false,
placement: 'top',
theme: 'transparent',
offset: [0, 2]
}
}),
FloatingMenu.configure({
element: floatingMenuElement,
tippyOptions: {
duration: 100,
arrow: false,
2025-07-14 10:37:28 +00:00
placement: floatingMenuPlacement,
2025-07-08 23:11:51 +00:00
theme: 'transparent',
2025-07-09 19:22:59 +00:00
offset: [-12, 4]
2025-07-08 23:11:51 +00:00
}
})
]
2025-07-11 08:15:13 +00:00
: []),
...(collaboration && provider ? [provider.getEditorExtension()] : [])
2024-11-21 06:56:26 +00:00
],
2025-07-11 19:59:48 +00:00
content: collaboration ? undefined : 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-07-17 10:43:42 +00:00
if (!editor) return;
2024-11-21 06:46:51 +00:00
2025-07-11 19:59:48 +00:00
htmlValue = editor.getHTML();
jsonValue = editor.getJSON();
2024-12-11 22:07:25 +00:00
2025-10-01 17:39:49 +00:00
if (richText) {
2025-10-06 04:29:48 +00:00
mdValue = turndownService
.turndown(
htmlValue
.replace(/<p><\/p>/g, '<br/>')
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
)
.replace(/\u00a0/g, ' ');
2025-10-01 17:39:49 +00:00
} else {
2025-10-06 04:29:48 +00:00
mdValue = turndownService
.turndown(
htmlValue
// Replace empty paragraphs with line breaks
.replace(/<p><\/p>/g, '<br/>')
// Replace multiple spaces with non-breaking spaces
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
// Replace tabs with non-breaking spaces (preserve indentation)
.replace(/\t/g, '\u00a0\u00a0\u00a0\u00a0') // 1 tab = 4 spaces
)
// Convert non-breaking spaces back to regular spaces for markdown
.replace(/\u00a0/g, ' ');
2025-10-01 17:39:49 +00:00
}
2024-11-24 07:57:05 +00:00
2025-10-06 04:29:48 +00:00
onChange({
html: htmlValue,
json: jsonValue,
md: mdValue
});
2025-05-03 18:53:23 +00:00
if (json) {
2025-07-06 13:32:03 +00:00
value = jsonValue;
2025-05-03 18:53:23 +00:00
} else {
2025-07-06 13:32:03 +00:00
if (raw) {
value = htmlValue;
} else {
2025-05-03 18:53:23 +00:00
if (!preserveBreaks) {
2025-07-06 13:32:03 +00:00
mdValue = mdValue.replace(/<br\/>/g, '');
2025-05-03 18:53:23 +00:00
}
2025-07-06 13:32:03 +00:00
if (value !== mdValue) {
value = mdValue;
2025-05-03 18:53:23 +00:00
// 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
}
2024-11-24 07:57:05 +00:00
}
2024-11-21 06:46:51 +00:00
}
},
editorProps: {
attributes: { id },
2025-09-12 16:54:34 +00:00
handlePaste: (view, event) => {
// Force plain-text pasting when richText === false
if (!richText) {
// swallow HTML completely
event.preventDefault();
const { state, dispatch } = view;
2025-09-15 21:25:04 +00:00
const plainText = (event.clipboardData?.getData('text/plain') ?? '').replace(
/\r\n/g,
'\n'
);
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);
dispatch(state.tr.replaceSelectionWith(fragment, false).scrollIntoView());
2025-09-12 16:54:34 +00:00
return true; // handled
}
return false;
},
2024-11-21 06:46:51 +00:00
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) {
2025-07-12 17:54:05 +00:00
// 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
}
return false;
}
2024-11-30 23:44:04 +00:00
// Handle Tab Key
if (event.key === 'Tab') {
2025-07-12 17:54:05 +00:00
const isInCodeBlock = isInside(['codeBlock']);
if (isInCodeBlock) {
// Handle tab in code block - insert tab character or spaces
const tabChar = '\t'; // or ' ' for 4 spaces
editor.commands.insertContent(tabChar);
2024-11-30 23:44:04 +00:00
event.preventDefault();
2025-07-12 17:54:05 +00:00
return true; // Prevent further propagation
} else {
const handled = selectNextTemplate(view.state, view.dispatch);
if (handled) {
event.preventDefault();
return true;
}
2024-11-30 23:44:04 +00:00
}
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
2025-09-12 16:31:57 +00:00
const { state } = view;
const { $from } = state.selection;
const lineStart = $from.before($from.depth);
const lineEnd = $from.after($from.depth);
const lineText = state.doc.textBetween(lineStart, lineEnd, '\n', '\0').trim();
2025-05-28 10:27:34 +00:00
if (event.shiftKey && !isCtrlPressed) {
2025-08-18 15:43:18 +00:00
if (lineText.startsWith('```')) {
// Fix GitHub issue #16337: prevent backtick removal for lines starting with ```
return false; // Let ProseMirror handle the Enter key normally
}
editor.commands.enter(); // Insert a new line
2025-05-27 22:10:54 +00:00
view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
event.preventDefault();
return true;
} else {
const isInCodeBlock = isInside(['codeBlock']);
2025-07-08 20:05:59 +00:00
const isInList = isInside(['listItem', 'bulletList', 'orderedList', 'taskList']);
2025-05-27 22:10:54 +00:00
const isInHeading = isInside(['heading']);
2024-11-21 07:14:06 +00:00
2025-09-12 16:31:57 +00:00
console.log({ isInCodeBlock, isInList, isInHeading });
2025-05-27 22:10:54 +00:00
if (isInCodeBlock || isInList || isInHeading) {
// Let ProseMirror handle the normal Enter behavior
return false;
}
2025-09-12 16:31:57 +00:00
const suggestionsElement = document.getElementById('suggestions-container');
if (lineText.startsWith('#') && suggestionsElement) {
console.log('Letting heading suggestion handle Enter key');
return true;
}
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) {
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
}
// 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;
2024-11-21 06:46:51 +00:00
}
// 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
// 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/')
);
// 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-07-16 11:01:03 +00:00
const hasFile = Array.from(event.clipboardData.files).length > 0;
if (hasImageFile || hasImageItem || hasFile) {
2024-11-21 06:46:51 +00:00
eventDispatch('paste', { event });
event.preventDefault();
return true;
}
2024-10-19 06:54:35 +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;
2025-09-28 22:10:11 +00:00
},
copy: (view, event: ClipboardEvent) => {
if (!event.clipboardData) return false;
if (richText) return false; // Let ProseMirror handle normal copy in rich text mode
2025-09-30 03:45:38 +00:00
const { state } = view;
const { from, to } = state.selection;
2025-09-28 22:10:11 +00:00
2025-09-30 03:45:38 +00:00
// Only take the selected text & HTML, not the full doc
const plain = state.doc.textBetween(from, to, '\n');
const slice = state.doc.cut(from, to);
const html = editor.schema ? editor.getHTML(slice) : editor.getHTML(); // depending on your editor API
event.clipboardData.setData('text/plain', plain);
2025-09-28 22:10:11 +00:00
event.clipboardData.setData('text/html', html);
event.preventDefault();
return true;
2024-10-19 21:56:30 +00:00
}
2024-10-19 05:40:15 +00:00
}
2025-07-17 13:36:06 +00:00
},
onBeforeCreate: ({ editor }) => {
if (files) {
editor.storage.files = files;
}
2025-07-19 12:35:03 +00:00
},
2025-09-12 16:54:34 +00:00
onSelectionUpdate: onSelectionUpdate,
enableInputRules: richText,
enablePasteRules: richText
2024-10-18 21:55:39 +00:00
});
2024-10-19 07:23:59 +00:00
provider?.setEditor(editor, () => ({ md: mdValue, html: htmlValue, json: jsonValue }));
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(() => {
2025-07-11 19:59:48 +00:00
if (provider) {
provider.destroy();
2025-07-11 08:15:13 +00:00
}
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-07-11 19:59:48 +00:00
$: if (value !== null && editor && !collaboration) {
2025-05-03 18:53:23 +00:00
onValueChange();
}
const onValueChange = () => {
if (!editor) return;
2025-07-06 13:32:03 +00:00
const jsonValue = editor.getJSON();
const htmlValue = editor.getHTML();
let mdValue = turndownService
.turndown(
(preserveBreaks ? htmlValue.replace(/<p><\/p>/g, '<br/>') : htmlValue).replace(
/ {2,}/g,
(m) => m.replace(/ /g, '\u00a0')
)
)
.replace(/\u00a0/g, ' ');
if (value === '') {
editor.commands.clearContent(); // Clear content if value is empty
selectTemplate();
return;
}
2025-05-03 18:53:23 +00:00
if (json) {
2025-07-06 13:32:03 +00:00
if (JSON.stringify(value) !== JSON.stringify(jsonValue)) {
2025-05-03 18:53:23 +00:00
editor.commands.setContent(value);
selectTemplate();
}
2025-02-14 06:37:01 +00:00
} else {
2025-05-03 19:05:25 +00:00
if (raw) {
2025-07-06 13:32:03 +00:00
if (value !== htmlValue) {
2025-05-03 19:05:25 +00:00
editor.commands.setContent(value);
selectTemplate();
}
} else {
2025-07-06 13:32:03 +00:00
if (value !== mdValue) {
editor.commands.setContent(
preserveBreaks
? value
: marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
2025-05-03 19:05:25 +00:00
breaks: false
})
2025-07-06 13:32:03 +00:00
);
2025-05-03 19:05:25 +00:00
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>
2025-09-12 17:43:05 +00:00
{#if richText && showFormattingToolbar}
2025-09-23 03:28:19 +00:00
<div bind:this={bubbleMenuElement} id="bubble-menu" class="p-0 {editor ? '' : 'hidden'}">
2025-07-08 23:11:51 +00:00
<FormattingButtons {editor} />
</div>
2025-09-23 03:28:19 +00:00
<div bind:this={floatingMenuElement} id="floating-menu" class="p-0 {editor ? '' : 'hidden'}">
2025-07-08 23:11:51 +00:00
<FormattingButtons {editor} />
</div>
{/if}
2025-09-24 15:09:59 +00:00
<div
bind:this={element}
2025-12-10 02:50:27 +00:00
class="relative w-full min-w-full h-full {className} {!editable ? 'cursor-not-allowed' : ''}"
2025-09-24 15:09:59 +00:00
/>