mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-12 04:15:25 +00:00
607 lines
17 KiB
Svelte
607 lines
17 KiB
Svelte
<script lang="ts">
|
|
import { marked } from 'marked';
|
|
import TurndownService from 'turndown';
|
|
import { gfm } from 'turndown-plugin-gfm';
|
|
const turndownService = new TurndownService({
|
|
codeBlockStyle: 'fenced',
|
|
headingStyle: 'atx'
|
|
});
|
|
turndownService.escape = (string) => string;
|
|
|
|
// Use turndown-plugin-gfm for proper GFM table support
|
|
turndownService.use(gfm);
|
|
|
|
import { onMount, onDestroy, tick } from 'svelte';
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
const eventDispatch = createEventDispatcher();
|
|
|
|
import { Fragment } from 'prosemirror-model';
|
|
import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
|
|
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
import { Editor } from '@tiptap/core';
|
|
|
|
import { AIAutocompletion } from './RichTextInput/AutoCompletion.js';
|
|
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';
|
|
|
|
import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
|
|
import Placeholder from '@tiptap/extension-placeholder';
|
|
import { all, createLowlight } from 'lowlight';
|
|
import StarterKit from '@tiptap/starter-kit';
|
|
import Highlight from '@tiptap/extension-highlight';
|
|
import Typography from '@tiptap/extension-typography';
|
|
|
|
import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
|
|
|
|
export let oncompositionstart = (e) => {};
|
|
export let oncompositionend = (e) => {};
|
|
export let onChange = (e) => {};
|
|
|
|
// create a lowlight instance with all languages loaded
|
|
const lowlight = createLowlight(all);
|
|
|
|
export let className = 'input-prose';
|
|
export let placeholder = 'Type here...';
|
|
|
|
export let id = '';
|
|
export let value = '';
|
|
export let html = '';
|
|
|
|
export let json = false;
|
|
export let raw = false;
|
|
export let editable = true;
|
|
|
|
export let preserveBreaks = false;
|
|
export let generateAutoCompletion: Function = async () => null;
|
|
export let autocomplete = false;
|
|
export let messageInput = false;
|
|
export let shiftEnter = false;
|
|
export let largeTextAsFile = false;
|
|
|
|
let element;
|
|
let editor;
|
|
|
|
const options = {
|
|
throwOnError: false
|
|
};
|
|
|
|
$: 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;
|
|
|
|
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) // 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');
|
|
const { state, view } = editor;
|
|
|
|
if (text.includes('\n')) {
|
|
// Multiple lines: make paragraphs
|
|
const { schema, tr } = state;
|
|
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 */);
|
|
|
|
// You probably want to move the cursor after the inserted content
|
|
// tr.setSelection(Selection.near(tr.doc.resolve(tr.selection.to)));
|
|
|
|
view.dispatch(tr);
|
|
} else if (text === '') {
|
|
// Empty: delete selection or paragraph
|
|
editor.commands.clearContent();
|
|
} else {
|
|
editor.commands.setContent(editor.state.schema.text(text));
|
|
}
|
|
|
|
selectNextTemplate(editor.view.state, editor.view.dispatch);
|
|
};
|
|
|
|
export const focus = () => {
|
|
if (editor) {
|
|
editor.view.focus();
|
|
// Scroll to the top of the editor
|
|
editor.view.dispatch(editor.view.state.tr.scrollIntoView());
|
|
}
|
|
};
|
|
|
|
// Function to find the next template in the document
|
|
function findNextTemplate(doc, from = 0) {
|
|
const patterns = [{ 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);
|
|
|
|
// Scroll to the selected template
|
|
dispatch(
|
|
tr.scrollIntoView().setMeta('preventScroll', true) // Prevent default scrolling behavior
|
|
);
|
|
}
|
|
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(async () => {
|
|
let content = value;
|
|
|
|
if (!json) {
|
|
if (preserveBreaks) {
|
|
turndownService.addRule('preserveBreaks', {
|
|
filter: 'br', // Target <br> elements
|
|
replacement: function (content) {
|
|
return '<br/>';
|
|
}
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
} else {
|
|
if (html && !content) {
|
|
content = html;
|
|
}
|
|
}
|
|
|
|
console.log('content', content);
|
|
|
|
editor = new Editor({
|
|
element: element,
|
|
extensions: [
|
|
StarterKit,
|
|
CodeBlockLowlight.configure({
|
|
lowlight
|
|
}),
|
|
Highlight,
|
|
Typography,
|
|
Placeholder.configure({ placeholder }),
|
|
Table.configure({ resizable: true }),
|
|
TableRow,
|
|
TableHeader,
|
|
TableCell,
|
|
...(autocomplete
|
|
? [
|
|
AIAutocompletion.configure({
|
|
generateCompletion: async (text) => {
|
|
if (text.trim().length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const suggestion = await generateAutoCompletion(text).catch(() => null);
|
|
if (!suggestion || suggestion.trim().length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return suggestion;
|
|
}
|
|
})
|
|
]
|
|
: [])
|
|
],
|
|
content: content,
|
|
autofocus: messageInput ? true : false,
|
|
onTransaction: () => {
|
|
// force re-render so `editor.isActive` works as expected
|
|
editor = editor;
|
|
|
|
html = editor.getHTML();
|
|
|
|
onChange({
|
|
html: editor.getHTML(),
|
|
json: editor.getJSON(),
|
|
md: turndownService
|
|
.turndown(
|
|
editor
|
|
.getHTML()
|
|
.replace(/<p><\/p>/g, '<br/>')
|
|
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
|
)
|
|
.replace(/\u00a0/g, ' ')
|
|
});
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
value = editor.getHTML();
|
|
}
|
|
}
|
|
},
|
|
editorProps: {
|
|
attributes: { id },
|
|
handleDOMEvents: {
|
|
compositionstart: (view, event) => {
|
|
oncompositionstart(event);
|
|
return false;
|
|
},
|
|
compositionend: (view, event) => {
|
|
oncompositionend(event);
|
|
return false;
|
|
},
|
|
focus: (view, event) => {
|
|
eventDispatch('focus', { event });
|
|
return false;
|
|
},
|
|
keyup: (view, event) => {
|
|
eventDispatch('keyup', { event });
|
|
return false;
|
|
},
|
|
keydown: (view, event) => {
|
|
if (messageInput) {
|
|
// Handle Tab Key
|
|
if (event.key === 'Tab') {
|
|
const handled = selectNextTemplate(view.state, view.dispatch);
|
|
if (handled) {
|
|
event.preventDefault();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (event.key === 'Enter') {
|
|
const isCtrlPressed = event.ctrlKey || event.metaKey; // metaKey is for Cmd key on Mac
|
|
if (event.shiftKey && !isCtrlPressed) {
|
|
editor.commands.enter(); // Insert a new line
|
|
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
|
|
}
|
|
return false;
|
|
}
|
|
|
|
const isInCodeBlock = isInside(['codeBlock']);
|
|
const isInList = isInside(['listItem', 'bulletList', 'orderedList']);
|
|
const isInHeading = isInside(['heading']);
|
|
|
|
if (isInCodeBlock || isInList || isInHeading) {
|
|
// Let ProseMirror handle the normal Enter behavior
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle shift + Enter for a line break
|
|
if (shiftEnter) {
|
|
if (event.key === 'Enter' && event.shiftKey && !event.ctrlKey && !event.metaKey) {
|
|
editor.commands.setHardBreak(); // Insert a hard break
|
|
view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (messageInput) {
|
|
selectTemplate();
|
|
}
|
|
});
|
|
|
|
onDestroy(() => {
|
|
if (editor) {
|
|
editor.destroy();
|
|
}
|
|
});
|
|
|
|
$: if (value !== null && editor) {
|
|
onValueChange();
|
|
}
|
|
|
|
const onValueChange = () => {
|
|
if (!editor) return;
|
|
|
|
if (json) {
|
|
if (JSON.stringify(value) !== JSON.stringify(editor.getJSON())) {
|
|
editor.commands.setContent(value);
|
|
selectTemplate();
|
|
}
|
|
} else {
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
};
|
|
</script>
|
|
|
|
<div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />
|