RichTextInput.svelte 8.3 KB

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