RichTextInput.svelte 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 structured block (like codeBlock or list)
  140. const { state } = view;
  141. const { $head } = state.selection;
  142. // Recursive function to check ancestors for specific node types
  143. function isInside(nodeTypes: string[]): boolean {
  144. let currentNode = $head;
  145. while (currentNode) {
  146. if (nodeTypes.includes(currentNode.parent.type.name)) {
  147. return true;
  148. }
  149. if (!currentNode.depth) break; // Stop if we reach the top
  150. currentNode = state.doc.resolve(currentNode.before()); // Move to the parent node
  151. }
  152. return false;
  153. }
  154. const isInCodeBlock = isInside(['codeBlock']);
  155. const isInList = isInside(['listItem', 'bulletList', 'orderedList']);
  156. const isInHeading = isInside(['heading']);
  157. if (isInCodeBlock || isInList || isInHeading) {
  158. // Let ProseMirror handle the normal Enter behavior
  159. return false;
  160. }
  161. }
  162. // Handle shift + Enter for a line break
  163. if (shiftEnter) {
  164. if (event.key === 'Enter' && event.shiftKey) {
  165. editor.commands.setHardBreak(); // Insert a hard break
  166. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
  167. event.preventDefault();
  168. return true;
  169. }
  170. if (event.key === 'Enter') {
  171. eventDispatch('enter', { event });
  172. event.preventDefault();
  173. return true;
  174. }
  175. }
  176. if (event.key === 'Enter') {
  177. eventDispatch('enter', { event });
  178. event.preventDefault();
  179. return true;
  180. }
  181. }
  182. eventDispatch('keydown', { event });
  183. return false;
  184. },
  185. paste: (view, event) => {
  186. if (event.clipboardData) {
  187. // Extract plain text from clipboard and paste it without formatting
  188. const plainText = event.clipboardData.getData('text/plain');
  189. if (plainText) {
  190. if (largeTextAsFile) {
  191. if (plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
  192. // Dispatch paste event to parent component
  193. eventDispatch('paste', { event });
  194. event.preventDefault();
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. // Check if the pasted content contains image files
  201. const hasImageFile = Array.from(event.clipboardData.files).some((file) =>
  202. file.type.startsWith('image/')
  203. );
  204. // Check for image in dataTransfer items (for cases where files are not available)
  205. const hasImageItem = Array.from(event.clipboardData.items).some((item) =>
  206. item.type.startsWith('image/')
  207. );
  208. if (hasImageFile) {
  209. // If there's an image, dispatch the event to the parent
  210. eventDispatch('paste', { event });
  211. event.preventDefault();
  212. return true;
  213. }
  214. if (hasImageItem) {
  215. // If there's an image item, dispatch the event to the parent
  216. eventDispatch('paste', { event });
  217. event.preventDefault();
  218. return true;
  219. }
  220. }
  221. // For all other cases (text, formatted text, etc.), let ProseMirror handle it
  222. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor after pasting
  223. return false;
  224. }
  225. }
  226. }
  227. });
  228. selectTemplate();
  229. });
  230. onDestroy(() => {
  231. if (editor) {
  232. editor.destroy();
  233. }
  234. });
  235. // Update the editor content if the external `value` changes
  236. $: if (editor && value !== turndownService.turndown(editor.getHTML())) {
  237. editor.commands.setContent(marked.parse(value)); // Update editor content
  238. selectTemplate();
  239. }
  240. </script>
  241. <div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />