RichTextInput.svelte 6.5 KB

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