RichTextInput.svelte 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <script lang="ts">
  2. import { marked } from 'marked';
  3. import TurndownService from 'turndown';
  4. const turndownService = new TurndownService({
  5. codeBlockStyle: 'fenced',
  6. headingStyle: 'atx'
  7. });
  8. turndownService.escape = (string) => string;
  9. import { onMount, onDestroy } from 'svelte';
  10. import { createEventDispatcher } from 'svelte';
  11. const eventDispatch = createEventDispatcher();
  12. import { EditorState, Plugin, PluginKey, TextSelection } from 'prosemirror-state';
  13. import { Decoration, DecorationSet } from 'prosemirror-view';
  14. import { Editor } from '@tiptap/core';
  15. import { AIAutocompletion } from './RichTextInput/AutoCompletion.js';
  16. import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
  17. import Placeholder from '@tiptap/extension-placeholder';
  18. import Highlight from '@tiptap/extension-highlight';
  19. import Typography from '@tiptap/extension-typography';
  20. import StarterKit from '@tiptap/starter-kit';
  21. import { all, createLowlight } from 'lowlight';
  22. import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
  23. export let oncompositionstart = (e) => {};
  24. export let oncompositionend = (e) => {};
  25. // create a lowlight instance with all languages loaded
  26. const lowlight = createLowlight(all);
  27. export let className = 'input-prose';
  28. export let placeholder = 'Type here...';
  29. export let value = '';
  30. export let id = '';
  31. export let raw = false;
  32. export let preserveBreaks = false;
  33. export let generateAutoCompletion: Function = async () => null;
  34. export let autocomplete = false;
  35. export let messageInput = false;
  36. export let shiftEnter = false;
  37. export let largeTextAsFile = false;
  38. let element;
  39. let editor;
  40. const options = {
  41. throwOnError: false
  42. };
  43. // Function to find the next template in the document
  44. function findNextTemplate(doc, from = 0) {
  45. const patterns = [{ start: '{{', end: '}}' }];
  46. let result = null;
  47. doc.nodesBetween(from, doc.content.size, (node, pos) => {
  48. if (result) return false; // Stop if we've found a match
  49. if (node.isText) {
  50. const text = node.text;
  51. let index = Math.max(0, from - pos);
  52. while (index < text.length) {
  53. for (const pattern of patterns) {
  54. if (text.startsWith(pattern.start, index)) {
  55. const endIndex = text.indexOf(pattern.end, index + pattern.start.length);
  56. if (endIndex !== -1) {
  57. result = {
  58. from: pos + index,
  59. to: pos + endIndex + pattern.end.length
  60. };
  61. return false; // Stop searching
  62. }
  63. }
  64. }
  65. index++;
  66. }
  67. }
  68. });
  69. return result;
  70. }
  71. // Function to select the next template in the document
  72. function selectNextTemplate(state, dispatch) {
  73. const { doc, selection } = state;
  74. const from = selection.to;
  75. let template = findNextTemplate(doc, from);
  76. if (!template) {
  77. // If not found, search from the beginning
  78. template = findNextTemplate(doc, 0);
  79. }
  80. if (template) {
  81. if (dispatch) {
  82. const tr = state.tr.setSelection(TextSelection.create(doc, template.from, template.to));
  83. dispatch(tr);
  84. }
  85. return true;
  86. }
  87. return false;
  88. }
  89. export const setContent = (content) => {
  90. editor.commands.setContent(content);
  91. };
  92. const selectTemplate = () => {
  93. if (value !== '') {
  94. // After updating the state, try to find and select the next template
  95. setTimeout(() => {
  96. const templateFound = selectNextTemplate(editor.view.state, editor.view.dispatch);
  97. if (!templateFound) {
  98. // If no template found, set cursor at the end
  99. const endPos = editor.view.state.doc.content.size;
  100. editor.view.dispatch(
  101. editor.view.state.tr.setSelection(TextSelection.create(editor.view.state.doc, endPos))
  102. );
  103. }
  104. }, 0);
  105. }
  106. };
  107. onMount(async () => {
  108. console.log(value);
  109. if (preserveBreaks) {
  110. turndownService.addRule('preserveBreaks', {
  111. filter: 'br', // Target <br> elements
  112. replacement: function (content) {
  113. return '<br/>';
  114. }
  115. });
  116. }
  117. let content = value;
  118. if (!raw) {
  119. async function tryParse(value, attempts = 3, interval = 100) {
  120. try {
  121. // Try parsing the value
  122. return marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
  123. breaks: false
  124. });
  125. } catch (error) {
  126. // If no attempts remain, fallback to plain text
  127. if (attempts <= 1) {
  128. return value;
  129. }
  130. // Wait for the interval, then retry
  131. await new Promise((resolve) => setTimeout(resolve, interval));
  132. return tryParse(value, attempts - 1, interval); // Recursive call
  133. }
  134. }
  135. // Usage example
  136. content = await tryParse(value);
  137. }
  138. editor = new Editor({
  139. element: element,
  140. extensions: [
  141. StarterKit,
  142. CodeBlockLowlight.configure({
  143. lowlight
  144. }),
  145. Highlight,
  146. Typography,
  147. Placeholder.configure({ placeholder }),
  148. ...(autocomplete
  149. ? [
  150. AIAutocompletion.configure({
  151. generateCompletion: async (text) => {
  152. if (text.trim().length === 0) {
  153. return null;
  154. }
  155. const suggestion = await generateAutoCompletion(text).catch(() => null);
  156. if (!suggestion || suggestion.trim().length === 0) {
  157. return null;
  158. }
  159. return suggestion;
  160. }
  161. })
  162. ]
  163. : [])
  164. ],
  165. content: content,
  166. autofocus: messageInput ? true : false,
  167. onTransaction: () => {
  168. // force re-render so `editor.isActive` works as expected
  169. editor = editor;
  170. if (!raw) {
  171. let newValue = turndownService
  172. .turndown(
  173. editor
  174. .getHTML()
  175. .replace(/<p><\/p>/g, '<br/>')
  176. .replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
  177. )
  178. .replace(/\u00a0/g, ' ');
  179. if (!preserveBreaks) {
  180. newValue = newValue.replace(/<br\/>/g, '');
  181. }
  182. if (value !== newValue) {
  183. value = newValue;
  184. // check if the node is paragraph as well
  185. if (editor.isActive('paragraph')) {
  186. if (value === '') {
  187. editor.commands.clearContent();
  188. }
  189. }
  190. }
  191. } else {
  192. value = editor.getHTML();
  193. }
  194. },
  195. editorProps: {
  196. attributes: { id },
  197. handleDOMEvents: {
  198. compositionstart: (view, event) => {
  199. oncompositionstart(event);
  200. return false;
  201. },
  202. compositionend: (view, event) => {
  203. oncompositionend(event);
  204. return false;
  205. },
  206. focus: (view, event) => {
  207. eventDispatch('focus', { event });
  208. return false;
  209. },
  210. keyup: (view, event) => {
  211. eventDispatch('keyup', { event });
  212. return false;
  213. },
  214. keydown: (view, event) => {
  215. if (messageInput) {
  216. // Handle Tab Key
  217. if (event.key === 'Tab') {
  218. const handled = selectNextTemplate(view.state, view.dispatch);
  219. if (handled) {
  220. event.preventDefault();
  221. return true;
  222. }
  223. }
  224. if (event.key === 'Enter') {
  225. // Check if the current selection is inside a structured block (like codeBlock or list)
  226. const { state } = view;
  227. const { $head } = state.selection;
  228. // Recursive function to check ancestors for specific node types
  229. function isInside(nodeTypes: string[]): boolean {
  230. let currentNode = $head;
  231. while (currentNode) {
  232. if (nodeTypes.includes(currentNode.parent.type.name)) {
  233. return true;
  234. }
  235. if (!currentNode.depth) break; // Stop if we reach the top
  236. currentNode = state.doc.resolve(currentNode.before()); // Move to the parent node
  237. }
  238. return false;
  239. }
  240. const isInCodeBlock = isInside(['codeBlock']);
  241. const isInList = isInside(['listItem', 'bulletList', 'orderedList']);
  242. const isInHeading = isInside(['heading']);
  243. if (isInCodeBlock || isInList || isInHeading) {
  244. // Let ProseMirror handle the normal Enter behavior
  245. return false;
  246. }
  247. }
  248. // Handle shift + Enter for a line break
  249. if (shiftEnter) {
  250. if (event.key === 'Enter' && event.shiftKey && !event.ctrlKey && !event.metaKey) {
  251. editor.commands.setHardBreak(); // Insert a hard break
  252. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
  253. event.preventDefault();
  254. return true;
  255. }
  256. }
  257. }
  258. eventDispatch('keydown', { event });
  259. return false;
  260. },
  261. paste: (view, event) => {
  262. if (event.clipboardData) {
  263. // Extract plain text from clipboard and paste it without formatting
  264. const plainText = event.clipboardData.getData('text/plain');
  265. if (plainText) {
  266. if (largeTextAsFile) {
  267. if (plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
  268. // Dispatch paste event to parent component
  269. eventDispatch('paste', { event });
  270. event.preventDefault();
  271. return true;
  272. }
  273. }
  274. return false;
  275. }
  276. // Check if the pasted content contains image files
  277. const hasImageFile = Array.from(event.clipboardData.files).some((file) =>
  278. file.type.startsWith('image/')
  279. );
  280. // Check for image in dataTransfer items (for cases where files are not available)
  281. const hasImageItem = Array.from(event.clipboardData.items).some((item) =>
  282. item.type.startsWith('image/')
  283. );
  284. if (hasImageFile) {
  285. // If there's an image, dispatch the event to the parent
  286. eventDispatch('paste', { event });
  287. event.preventDefault();
  288. return true;
  289. }
  290. if (hasImageItem) {
  291. // If there's an image item, dispatch the event to the parent
  292. eventDispatch('paste', { event });
  293. event.preventDefault();
  294. return true;
  295. }
  296. }
  297. // For all other cases (text, formatted text, etc.), let ProseMirror handle it
  298. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor after pasting
  299. return false;
  300. }
  301. }
  302. }
  303. });
  304. if (messageInput) {
  305. selectTemplate();
  306. }
  307. });
  308. onDestroy(() => {
  309. if (editor) {
  310. editor.destroy();
  311. }
  312. });
  313. // Update the editor content if the external `value` changes
  314. $: if (
  315. editor &&
  316. (raw
  317. ? value !== editor.getHTML()
  318. : value !==
  319. turndownService
  320. .turndown(
  321. (preserveBreaks
  322. ? editor.getHTML().replace(/<p><\/p>/g, '<br/>')
  323. : editor.getHTML()
  324. ).replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
  325. )
  326. .replace(/\u00a0/g, ' '))
  327. ) {
  328. if (raw) {
  329. editor.commands.setContent(value);
  330. } else {
  331. preserveBreaks
  332. ? editor.commands.setContent(value)
  333. : editor.commands.setContent(
  334. marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
  335. breaks: false
  336. })
  337. ); // Update editor content
  338. }
  339. selectTemplate();
  340. }
  341. </script>
  342. <div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />