RichTextInput.svelte 7.2 KB

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