RichTextInput.svelte 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. <script lang="ts">
  2. import { marked } from 'marked';
  3. marked.use({
  4. breaks: true,
  5. gfm: true,
  6. renderer: {
  7. list(body, ordered, start) {
  8. const isTaskList = body.includes('data-checked=');
  9. if (isTaskList) {
  10. return `<ul data-type="taskList">${body}</ul>`;
  11. }
  12. const type = ordered ? 'ol' : 'ul';
  13. const startatt = ordered && start !== 1 ? ` start="${start}"` : '';
  14. return `<${type}${startatt}>${body}</${type}>`;
  15. },
  16. listitem(text, task, checked) {
  17. if (task) {
  18. const checkedAttr = checked ? 'true' : 'false';
  19. return `<li data-type="taskItem" data-checked="${checkedAttr}">${text}</li>`;
  20. }
  21. return `<li>${text}</li>`;
  22. }
  23. }
  24. });
  25. import TurndownService from 'turndown';
  26. import { gfm } from 'turndown-plugin-gfm';
  27. const turndownService = new TurndownService({
  28. codeBlockStyle: 'fenced',
  29. headingStyle: 'atx'
  30. });
  31. turndownService.escape = (string) => string;
  32. // Use turndown-plugin-gfm for proper GFM table support
  33. turndownService.use(gfm);
  34. turndownService.addRule('taskListItems', {
  35. filter: (node) =>
  36. node.nodeName === 'LI' &&
  37. (node.getAttribute('data-checked') === 'true' ||
  38. node.getAttribute('data-checked') === 'false'),
  39. replacement: function (content, node) {
  40. const checked = node.getAttribute('data-checked') === 'true';
  41. content = content.replace(/^\s+/, '');
  42. return `- [${checked ? 'x' : ' '}] ${content}\n`;
  43. }
  44. });
  45. import { onMount, onDestroy, tick, getContext } from 'svelte';
  46. import { createEventDispatcher } from 'svelte';
  47. const i18n = getContext('i18n');
  48. const eventDispatch = createEventDispatcher();
  49. import { Fragment, DOMParser } from 'prosemirror-model';
  50. import { EditorState, Plugin, PluginKey, TextSelection, Selection } from 'prosemirror-state';
  51. import { Decoration, DecorationSet } from 'prosemirror-view';
  52. import { Editor, Extension } from '@tiptap/core';
  53. // Yjs imports
  54. import * as Y from 'yjs';
  55. import {
  56. ySyncPlugin,
  57. yCursorPlugin,
  58. yUndoPlugin,
  59. undo,
  60. redo,
  61. prosemirrorJSONToYDoc,
  62. yDocToProsemirrorJSON
  63. } from 'y-prosemirror';
  64. import { keymap } from 'prosemirror-keymap';
  65. import { AIAutocompletion } from './RichTextInput/AutoCompletion.js';
  66. import StarterKit from '@tiptap/starter-kit';
  67. // Bubble and Floating menus are currently fixed to v2 due to styling issues in v3
  68. // TODO: Update to v3 when styling issues are resolved
  69. import BubbleMenu from '@tiptap/extension-bubble-menu';
  70. import FloatingMenu from '@tiptap/extension-floating-menu';
  71. import { TableKit } from '@tiptap/extension-table';
  72. import { ListKit } from '@tiptap/extension-list';
  73. import { Placeholder, CharacterCount } from '@tiptap/extensions';
  74. import Image from './RichTextInput/Image/index.js';
  75. // import TiptapImage from '@tiptap/extension-image';
  76. import FileHandler from '@tiptap/extension-file-handler';
  77. import Typography from '@tiptap/extension-typography';
  78. import Highlight from '@tiptap/extension-highlight';
  79. import CodeBlockLowlight from '@tiptap/extension-code-block-lowlight';
  80. import { all, createLowlight } from 'lowlight';
  81. import { PASTED_TEXT_CHARACTER_LIMIT } from '$lib/constants';
  82. import FormattingButtons from './RichTextInput/FormattingButtons.svelte';
  83. import { duration } from 'dayjs';
  84. export let oncompositionstart = (e) => {};
  85. export let oncompositionend = (e) => {};
  86. export let onChange = (e) => {};
  87. // create a lowlight instance with all languages loaded
  88. const lowlight = createLowlight(all);
  89. export let editor = null;
  90. export let socket = null;
  91. export let user = null;
  92. export let files = [];
  93. export let documentId = '';
  94. export let className = 'input-prose';
  95. export let placeholder = 'Type here...';
  96. export let link = false;
  97. export let image = false;
  98. export let fileHandler = false;
  99. export let onFileDrop = (currentEditor, files, pos) => {
  100. files.forEach((file) => {
  101. const fileReader = new FileReader();
  102. fileReader.readAsDataURL(file);
  103. fileReader.onload = () => {
  104. currentEditor
  105. .chain()
  106. .insertContentAt(pos, {
  107. type: 'image',
  108. attrs: {
  109. src: fileReader.result
  110. }
  111. })
  112. .focus()
  113. .run();
  114. };
  115. });
  116. };
  117. export let onFilePaste = (currentEditor, files, htmlContent) => {
  118. files.forEach((file) => {
  119. if (htmlContent) {
  120. // if there is htmlContent, stop manual insertion & let other extensions handle insertion via inputRule
  121. // you could extract the pasted file from this url string and upload it to a server for example
  122. console.log(htmlContent); // eslint-disable-line no-console
  123. return false;
  124. }
  125. const fileReader = new FileReader();
  126. fileReader.readAsDataURL(file);
  127. fileReader.onload = () => {
  128. currentEditor
  129. .chain()
  130. .insertContentAt(currentEditor.state.selection.anchor, {
  131. type: 'image',
  132. attrs: {
  133. src: fileReader.result
  134. }
  135. })
  136. .focus()
  137. .run();
  138. };
  139. });
  140. };
  141. export let getWordAtDocPosCallback = (func) => {};
  142. export let onSelectionUpdate = (e) => {};
  143. export let id = '';
  144. export let value = '';
  145. export let html = '';
  146. export let json = false;
  147. export let raw = false;
  148. export let editable = true;
  149. export let collaboration = false;
  150. export let showFormattingButtons = true;
  151. export let preserveBreaks = false;
  152. export let generateAutoCompletion: Function = async () => null;
  153. export let autocomplete = false;
  154. export let messageInput = false;
  155. export let shiftEnter = false;
  156. export let largeTextAsFile = false;
  157. export let insertPromptAsRichText = false;
  158. export let floatingMenuPlacement = 'bottom-start';
  159. let content = null;
  160. let htmlValue = '';
  161. let jsonValue = '';
  162. let mdValue = '';
  163. let lastSelectionBookmark = null;
  164. // Yjs setup
  165. let ydoc = null;
  166. let yXmlFragment = null;
  167. let awareness = null;
  168. const getEditorInstance = async () => {
  169. return new Promise((resolve) => {
  170. setTimeout(() => {
  171. resolve(editor);
  172. }, 0);
  173. });
  174. };
  175. // Custom Yjs Socket.IO provider
  176. class SocketIOProvider {
  177. constructor(doc, documentId, socket, user) {
  178. this.doc = doc;
  179. this.documentId = documentId;
  180. this.socket = socket;
  181. this.user = user;
  182. this.isConnected = false;
  183. this.synced = false;
  184. this.setupEventListeners();
  185. }
  186. generateUserColor() {
  187. const colors = [
  188. '#FF6B6B',
  189. '#4ECDC4',
  190. '#45B7D1',
  191. '#96CEB4',
  192. '#FFEAA7',
  193. '#DDA0DD',
  194. '#98D8C8',
  195. '#F7DC6F',
  196. '#BB8FCE',
  197. '#85C1E9'
  198. ];
  199. return colors[Math.floor(Math.random() * colors.length)];
  200. }
  201. joinDocument() {
  202. const userColor = this.generateUserColor();
  203. this.socket.emit('ydoc:document:join', {
  204. document_id: this.documentId,
  205. user_id: this.user?.id,
  206. user_name: this.user?.name,
  207. user_color: userColor
  208. });
  209. // Set user awareness info
  210. if (awareness && this.user) {
  211. awareness.setLocalStateField('user', {
  212. name: `${this.user.name}`,
  213. color: userColor,
  214. id: this.socket.id
  215. });
  216. }
  217. }
  218. setupEventListeners() {
  219. // Listen for document updates from server
  220. this.socket.on('ydoc:document:update', (data) => {
  221. if (data.document_id === this.documentId && data.socket_id !== this.socket.id) {
  222. try {
  223. const update = new Uint8Array(data.update);
  224. Y.applyUpdate(this.doc, update);
  225. } catch (error) {
  226. console.error('Error applying Yjs update:', error);
  227. }
  228. }
  229. });
  230. // Listen for document state from server
  231. this.socket.on('ydoc:document:state', async (data) => {
  232. if (data.document_id === this.documentId) {
  233. try {
  234. if (data.state) {
  235. const state = new Uint8Array(data.state);
  236. if (state.length === 2 && state[0] === 0 && state[1] === 0) {
  237. // Empty state, check if we have content to initialize
  238. // check if editor empty as well
  239. // const editor = await getEditorInstance();
  240. const isEmptyEditor = !editor || editor.getText().trim() === '';
  241. if (isEmptyEditor) {
  242. if (content && (data?.sessions ?? ['']).length === 1) {
  243. const editorYdoc = prosemirrorJSONToYDoc(editor.schema, content);
  244. if (editorYdoc) {
  245. Y.applyUpdate(this.doc, Y.encodeStateAsUpdate(editorYdoc));
  246. }
  247. }
  248. } else {
  249. // If the editor already has content, we don't need to send an empty state
  250. if (this.doc.getXmlFragment('prosemirror').length > 0) {
  251. this.socket.emit('ydoc:document:update', {
  252. document_id: this.documentId,
  253. user_id: this.user?.id,
  254. socket_id: this.socket.id,
  255. update: Y.encodeStateAsUpdate(this.doc)
  256. });
  257. } else {
  258. console.warn('Yjs document is empty, not sending state.');
  259. }
  260. }
  261. } else {
  262. Y.applyUpdate(this.doc, state, 'server');
  263. }
  264. }
  265. this.synced = true;
  266. } catch (error) {
  267. console.error('Error applying Yjs state:', error);
  268. this.synced = false;
  269. this.socket.emit('ydoc:document:state', {
  270. document_id: this.documentId
  271. });
  272. }
  273. }
  274. });
  275. // Listen for awareness updates
  276. this.socket.on('ydoc:awareness:update', (data) => {
  277. if (data.document_id === this.documentId && awareness) {
  278. try {
  279. const awarenessUpdate = new Uint8Array(data.update);
  280. awareness.applyUpdate(awarenessUpdate, 'server');
  281. } catch (error) {
  282. console.error('Error applying awareness update:', error);
  283. }
  284. }
  285. });
  286. // Handle connection events
  287. this.socket.on('connect', this.onConnect);
  288. this.socket.on('disconnect', this.onDisconnect);
  289. // Listen for document updates from Yjs
  290. this.doc.on('update', async (update, origin) => {
  291. if (origin !== 'server' && this.isConnected) {
  292. await tick(); // Ensure the DOM is updated before sending
  293. this.socket.emit('ydoc:document:update', {
  294. document_id: this.documentId,
  295. user_id: this.user?.id,
  296. socket_id: this.socket.id,
  297. update: Array.from(update),
  298. data: {
  299. content: {
  300. md: mdValue,
  301. html: htmlValue,
  302. json: jsonValue
  303. }
  304. }
  305. });
  306. }
  307. });
  308. // Listen for awareness updates from Yjs
  309. if (awareness) {
  310. awareness.on('change', ({ added, updated, removed }, origin) => {
  311. if (origin !== 'server' && this.isConnected) {
  312. const changedClients = added.concat(updated).concat(removed);
  313. const awarenessUpdate = awareness.encodeUpdate(changedClients);
  314. this.socket.emit('ydoc:awareness:update', {
  315. document_id: this.documentId,
  316. user_id: this.socket.id,
  317. update: Array.from(awarenessUpdate)
  318. });
  319. }
  320. });
  321. }
  322. if (this.socket.connected) {
  323. this.isConnected = true;
  324. this.joinDocument();
  325. }
  326. }
  327. onConnect = () => {
  328. this.isConnected = true;
  329. this.joinDocument();
  330. };
  331. onDisconnect = () => {
  332. this.isConnected = false;
  333. this.synced = false;
  334. };
  335. destroy() {
  336. this.socket.off('ydoc:document:update');
  337. this.socket.off('ydoc:document:state');
  338. this.socket.off('ydoc:awareness:update');
  339. this.socket.off('connect', this.onConnect);
  340. this.socket.off('disconnect', this.onDisconnect);
  341. if (this.isConnected) {
  342. this.socket.emit('ydoc:document:leave', {
  343. document_id: this.documentId,
  344. user_id: this.user?.id
  345. });
  346. }
  347. }
  348. }
  349. let provider = null;
  350. // Simple awareness implementation
  351. class SimpleAwareness {
  352. constructor(yDoc) {
  353. // Yjs awareness expects clientID (not clientId) property
  354. this.clientID = yDoc ? yDoc.clientID : Math.floor(Math.random() * 0xffffffff);
  355. // Map from clientID (number) to state (object)
  356. this._states = new Map(); // _states, not states; will make getStates() for compat
  357. this._updateHandlers = [];
  358. this._localState = {};
  359. // As in Yjs Awareness, add our local state to the states map from the start:
  360. this._states.set(this.clientID, this._localState);
  361. }
  362. on(event, handler) {
  363. if (event === 'change') this._updateHandlers.push(handler);
  364. }
  365. off(event, handler) {
  366. if (event === 'change') {
  367. const i = this._updateHandlers.indexOf(handler);
  368. if (i !== -1) this._updateHandlers.splice(i, 1);
  369. }
  370. }
  371. getLocalState() {
  372. return this._states.get(this.clientID) || null;
  373. }
  374. getStates() {
  375. // Yjs returns a Map (clientID->state)
  376. return this._states;
  377. }
  378. setLocalStateField(field, value) {
  379. let localState = this._states.get(this.clientID);
  380. if (!localState) {
  381. localState = {};
  382. this._states.set(this.clientID, localState);
  383. }
  384. localState[field] = value;
  385. // After updating, fire 'update' event to all handlers
  386. for (const cb of this._updateHandlers) {
  387. // Follows Yjs Awareness ({ added, updated, removed }, origin)
  388. cb({ added: [], updated: [this.clientID], removed: [] }, 'local');
  389. }
  390. }
  391. applyUpdate(update, origin) {
  392. // Very simple: Accepts a serialized JSON state for now as Uint8Array
  393. try {
  394. const str = new TextDecoder().decode(update);
  395. const obj = JSON.parse(str);
  396. // Should be a plain object: { clientID: state, ... }
  397. for (const [k, v] of Object.entries(obj)) {
  398. this._states.set(+k, v);
  399. }
  400. for (const cb of this._updateHandlers) {
  401. cb({ added: [], updated: Array.from(Object.keys(obj)).map(Number), removed: [] }, origin);
  402. }
  403. } catch (e) {
  404. console.warn('SimpleAwareness: Could not decode update:', e);
  405. }
  406. }
  407. encodeUpdate(clients) {
  408. // Encodes the states for the given clientIDs as Uint8Array (JSON)
  409. const obj = {};
  410. for (const id of clients || Array.from(this._states.keys())) {
  411. const st = this._states.get(id);
  412. if (st) obj[id] = st;
  413. }
  414. const json = JSON.stringify(obj);
  415. return new TextEncoder().encode(json);
  416. }
  417. }
  418. // Yjs collaboration extension
  419. const YjsCollaboration = Extension.create({
  420. name: 'yjsCollaboration',
  421. addProseMirrorPlugins() {
  422. if (!collaboration || !yXmlFragment) return [];
  423. const plugins = [
  424. ySyncPlugin(yXmlFragment),
  425. yUndoPlugin(),
  426. keymap({
  427. 'Mod-z': undo,
  428. 'Mod-y': redo,
  429. 'Mod-Shift-z': redo
  430. })
  431. ];
  432. if (awareness) {
  433. plugins.push(yCursorPlugin(awareness));
  434. }
  435. return plugins;
  436. }
  437. });
  438. function initializeCollaboration() {
  439. if (!collaboration) return;
  440. // Create Yjs document
  441. ydoc = new Y.Doc();
  442. yXmlFragment = ydoc.getXmlFragment('prosemirror');
  443. awareness = new SimpleAwareness(ydoc);
  444. // Create custom Socket.IO provider
  445. provider = new SocketIOProvider(ydoc, documentId, socket, user);
  446. }
  447. let floatingMenuElement = null;
  448. let bubbleMenuElement = null;
  449. let element;
  450. const options = {
  451. throwOnError: false
  452. };
  453. $: if (editor) {
  454. editor.setOptions({
  455. editable: editable
  456. });
  457. }
  458. $: if (value === null && html !== null && editor) {
  459. editor.commands.setContent(html);
  460. }
  461. export const getWordAtDocPos = () => {
  462. if (!editor) return '';
  463. const { state } = editor.view;
  464. const pos = state.selection.from;
  465. const doc = state.doc;
  466. const resolvedPos = doc.resolve(pos);
  467. const textBlock = resolvedPos.parent;
  468. const paraStart = resolvedPos.start();
  469. const text = textBlock.textContent;
  470. const offset = resolvedPos.parentOffset;
  471. let wordStart = offset,
  472. wordEnd = offset;
  473. while (wordStart > 0 && !/\s/.test(text[wordStart - 1])) wordStart--;
  474. while (wordEnd < text.length && !/\s/.test(text[wordEnd])) wordEnd++;
  475. const word = text.slice(wordStart, wordEnd);
  476. return word;
  477. };
  478. // Returns {start, end} of the word at pos
  479. function getWordBoundsAtPos(doc, pos) {
  480. const resolvedPos = doc.resolve(pos);
  481. const textBlock = resolvedPos.parent;
  482. const paraStart = resolvedPos.start();
  483. const text = textBlock.textContent;
  484. const offset = resolvedPos.parentOffset;
  485. let wordStart = offset,
  486. wordEnd = offset;
  487. while (wordStart > 0 && !/\s/.test(text[wordStart - 1])) wordStart--;
  488. while (wordEnd < text.length && !/\s/.test(text[wordEnd])) wordEnd++;
  489. return {
  490. start: paraStart + wordStart,
  491. end: paraStart + wordEnd
  492. };
  493. }
  494. export const replaceCommandWithText = async (text) => {
  495. const { state, dispatch } = editor.view;
  496. const { selection } = state;
  497. const pos = selection.from;
  498. // Get the plain text of this document
  499. // const docText = state.doc.textBetween(0, state.doc.content.size, '\n', '\n');
  500. // Find the word boundaries at cursor
  501. const { start, end } = getWordBoundsAtPos(state.doc, pos);
  502. let tr = state.tr;
  503. if (insertPromptAsRichText) {
  504. const htmlContent = marked
  505. .parse(text, {
  506. breaks: true,
  507. gfm: true
  508. })
  509. .trim();
  510. // Create a temporary div to parse HTML
  511. const tempDiv = document.createElement('div');
  512. tempDiv.innerHTML = htmlContent;
  513. // Convert HTML to ProseMirror nodes
  514. const fragment = DOMParser.fromSchema(state.schema).parse(tempDiv);
  515. // Extract just the content, not the wrapper paragraphs
  516. const content = fragment.content;
  517. let nodesToInsert = [];
  518. content.forEach((node) => {
  519. if (node.type.name === 'paragraph') {
  520. // If it's a paragraph, extract its content
  521. nodesToInsert.push(...node.content.content);
  522. } else {
  523. nodesToInsert.push(node);
  524. }
  525. });
  526. tr = tr.replaceWith(start, end, nodesToInsert);
  527. // Calculate new position
  528. const newPos = start + nodesToInsert.reduce((sum, node) => sum + node.nodeSize, 0);
  529. tr = tr.setSelection(Selection.near(tr.doc.resolve(newPos)));
  530. } else {
  531. if (text.includes('\n')) {
  532. // Split the text into lines and create a <p> node for each line
  533. const lines = text.split('\n');
  534. const nodes = lines.map(
  535. (line, index) =>
  536. index === 0
  537. ? state.schema.text(line ? line : []) // First line is plain text
  538. : state.schema.nodes.paragraph.create({}, line ? state.schema.text(line) : undefined) // Subsequent lines are paragraphs
  539. );
  540. // Build and dispatch the transaction to replace the word at cursor
  541. tr = tr.replaceWith(start, end, nodes);
  542. let newSelectionPos;
  543. // +1 because the insert happens at start, so last para starts at (start + sum of all previous nodes' sizes)
  544. let lastPos = start;
  545. for (let i = 0; i < nodes.length; i++) {
  546. lastPos += nodes[i].nodeSize;
  547. }
  548. // Place cursor inside the last paragraph at its end
  549. newSelectionPos = lastPos;
  550. tr = tr.setSelection(TextSelection.near(tr.doc.resolve(newSelectionPos)));
  551. } else {
  552. tr = tr.replaceWith(
  553. start,
  554. end, // replace this range
  555. text !== '' ? state.schema.text(text) : []
  556. );
  557. tr = tr.setSelection(
  558. state.selection.constructor.near(tr.doc.resolve(start + text.length + 1))
  559. );
  560. }
  561. }
  562. dispatch(tr);
  563. await tick();
  564. // selectNextTemplate(state, dispatch);
  565. };
  566. export const setText = (text: string) => {
  567. if (!editor) return;
  568. text = text.replaceAll('\n\n', '\n');
  569. // reset the editor content
  570. editor.commands.clearContent();
  571. const { state, view } = editor;
  572. const { schema, tr } = state;
  573. if (text.includes('\n')) {
  574. // Multiple lines: make paragraphs
  575. const lines = text.split('\n');
  576. // Map each line to a paragraph node (empty lines -> empty paragraph)
  577. const nodes = lines.map((line) =>
  578. schema.nodes.paragraph.create({}, line ? schema.text(line) : undefined)
  579. );
  580. // Create a document fragment containing all parsed paragraphs
  581. const fragment = Fragment.fromArray(nodes);
  582. // Replace current selection with these paragraphs
  583. tr.replaceSelectionWith(fragment, false /* don't select new */);
  584. view.dispatch(tr);
  585. } else if (text === '') {
  586. // Empty: replace with empty paragraph using tr
  587. editor.commands.clearContent();
  588. } else {
  589. // Single line: create paragraph with text
  590. const paragraph = schema.nodes.paragraph.create({}, schema.text(text));
  591. tr.replaceSelectionWith(paragraph, false);
  592. view.dispatch(tr);
  593. }
  594. selectNextTemplate(editor.view.state, editor.view.dispatch);
  595. focus();
  596. };
  597. export const insertContent = (content) => {
  598. if (!editor) return;
  599. const { state, view } = editor;
  600. const { schema, tr } = state;
  601. // If content is a string, convert it to a ProseMirror node
  602. const htmlContent = marked.parse(content);
  603. // insert the HTML content at the current selection
  604. editor.commands.insertContent(htmlContent);
  605. focus();
  606. };
  607. export const replaceVariables = (variables) => {
  608. if (!editor) return;
  609. const { state, view } = editor;
  610. const { doc } = state;
  611. // Create a transaction to replace variables
  612. let tr = state.tr;
  613. let offset = 0; // Track position changes due to text length differences
  614. // Collect all replacements first to avoid position conflicts
  615. const replacements = [];
  616. doc.descendants((node, pos) => {
  617. if (node.isText && node.text) {
  618. const text = node.text;
  619. const replacedText = text.replace(/{{\s*([^|}]+)(?:\|[^}]*)?\s*}}/g, (match, varName) => {
  620. const trimmedVarName = varName.trim();
  621. return variables.hasOwnProperty(trimmedVarName)
  622. ? String(variables[trimmedVarName])
  623. : match;
  624. });
  625. if (replacedText !== text) {
  626. replacements.push({
  627. from: pos,
  628. to: pos + text.length,
  629. text: replacedText
  630. });
  631. }
  632. }
  633. });
  634. // Apply replacements in reverse order to maintain correct positions
  635. replacements.reverse().forEach(({ from, to, text }) => {
  636. tr = tr.replaceWith(from, to, text !== '' ? state.schema.text(text) : []);
  637. });
  638. // Only dispatch if there are changes
  639. if (replacements.length > 0) {
  640. view.dispatch(tr);
  641. }
  642. };
  643. export const focus = () => {
  644. if (editor) {
  645. editor.view.focus();
  646. // Scroll to the current selection
  647. editor.view.dispatch(editor.view.state.tr.scrollIntoView());
  648. }
  649. };
  650. // Function to find the next template in the document
  651. function findNextTemplate(doc, from = 0) {
  652. const patterns = [{ start: '{{', end: '}}' }];
  653. let result = null;
  654. doc.nodesBetween(from, doc.content.size, (node, pos) => {
  655. if (result) return false; // Stop if we've found a match
  656. if (node.isText) {
  657. const text = node.text;
  658. let index = Math.max(0, from - pos);
  659. while (index < text.length) {
  660. for (const pattern of patterns) {
  661. if (text.startsWith(pattern.start, index)) {
  662. const endIndex = text.indexOf(pattern.end, index + pattern.start.length);
  663. if (endIndex !== -1) {
  664. result = {
  665. from: pos + index,
  666. to: pos + endIndex + pattern.end.length
  667. };
  668. return false; // Stop searching
  669. }
  670. }
  671. }
  672. index++;
  673. }
  674. }
  675. });
  676. return result;
  677. }
  678. // Function to select the next template in the document
  679. function selectNextTemplate(state, dispatch) {
  680. const { doc, selection } = state;
  681. const from = selection.to;
  682. let template = findNextTemplate(doc, from);
  683. if (!template) {
  684. // If not found, search from the beginning
  685. template = findNextTemplate(doc, 0);
  686. }
  687. if (template) {
  688. if (dispatch) {
  689. const tr = state.tr.setSelection(TextSelection.create(doc, template.from, template.to));
  690. dispatch(tr);
  691. // Scroll to the selected template
  692. dispatch(
  693. tr.scrollIntoView().setMeta('preventScroll', true) // Prevent default scrolling behavior
  694. );
  695. }
  696. return true;
  697. }
  698. return false;
  699. }
  700. export const setContent = (content) => {
  701. editor.commands.setContent(content);
  702. };
  703. const selectTemplate = () => {
  704. if (value !== '') {
  705. // After updating the state, try to find and select the next template
  706. setTimeout(() => {
  707. const templateFound = selectNextTemplate(editor.view.state, editor.view.dispatch);
  708. if (!templateFound) {
  709. editor.commands.focus('end');
  710. }
  711. }, 0);
  712. }
  713. };
  714. const SelectionDecoration = Extension.create({
  715. name: 'selectionDecoration',
  716. addProseMirrorPlugins() {
  717. return [
  718. new Plugin({
  719. key: new PluginKey('selection'),
  720. props: {
  721. decorations: (state) => {
  722. const { selection } = state;
  723. const { focused } = this.editor;
  724. if (focused || selection.empty) {
  725. return null;
  726. }
  727. return DecorationSet.create(state.doc, [
  728. Decoration.inline(selection.from, selection.to, {
  729. class: 'editor-selection'
  730. })
  731. ]);
  732. }
  733. }
  734. })
  735. ];
  736. }
  737. });
  738. onMount(async () => {
  739. content = value;
  740. if (json) {
  741. if (!content) {
  742. content = html ? html : null;
  743. }
  744. } else {
  745. if (preserveBreaks) {
  746. turndownService.addRule('preserveBreaks', {
  747. filter: 'br', // Target <br> elements
  748. replacement: function (content) {
  749. return '<br/>';
  750. }
  751. });
  752. }
  753. if (!raw) {
  754. async function tryParse(value, attempts = 3, interval = 100) {
  755. try {
  756. // Try parsing the value
  757. return marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
  758. breaks: false
  759. });
  760. } catch (error) {
  761. // If no attempts remain, fallback to plain text
  762. if (attempts <= 1) {
  763. return value;
  764. }
  765. // Wait for the interval, then retry
  766. await new Promise((resolve) => setTimeout(resolve, interval));
  767. return tryParse(value, attempts - 1, interval); // Recursive call
  768. }
  769. }
  770. // Usage example
  771. content = await tryParse(value);
  772. }
  773. }
  774. console.log('content', content);
  775. if (collaboration) {
  776. initializeCollaboration();
  777. }
  778. console.log(bubbleMenuElement, floatingMenuElement);
  779. editor = new Editor({
  780. element: element,
  781. extensions: [
  782. StarterKit.configure({
  783. link: link
  784. }),
  785. Placeholder.configure({ placeholder }),
  786. SelectionDecoration,
  787. CodeBlockLowlight.configure({
  788. lowlight
  789. }),
  790. Highlight,
  791. Typography,
  792. TableKit.configure({
  793. table: { resizable: true }
  794. }),
  795. ListKit.configure({
  796. taskItem: {
  797. nested: true
  798. }
  799. }),
  800. CharacterCount.configure({}),
  801. ...(image ? [Image] : []),
  802. ...(fileHandler
  803. ? [
  804. FileHandler.configure({
  805. onDrop: onFileDrop,
  806. onPaste: onFilePaste
  807. })
  808. ]
  809. : []),
  810. ...(autocomplete
  811. ? [
  812. AIAutocompletion.configure({
  813. generateCompletion: async (text) => {
  814. if (text.trim().length === 0) {
  815. return null;
  816. }
  817. const suggestion = await generateAutoCompletion(text).catch(() => null);
  818. if (!suggestion || suggestion.trim().length === 0) {
  819. return null;
  820. }
  821. return suggestion;
  822. }
  823. })
  824. ]
  825. : []),
  826. ...(showFormattingButtons
  827. ? [
  828. BubbleMenu.configure({
  829. element: bubbleMenuElement,
  830. tippyOptions: {
  831. duration: 100,
  832. arrow: false,
  833. placement: 'top',
  834. theme: 'transparent',
  835. offset: [0, 2]
  836. }
  837. }),
  838. FloatingMenu.configure({
  839. element: floatingMenuElement,
  840. tippyOptions: {
  841. duration: 100,
  842. arrow: false,
  843. placement: floatingMenuPlacement,
  844. theme: 'transparent',
  845. offset: [-12, 4]
  846. }
  847. })
  848. ]
  849. : []),
  850. ...(collaboration ? [YjsCollaboration] : [])
  851. ],
  852. content: collaboration ? undefined : content,
  853. autofocus: messageInput ? true : false,
  854. onTransaction: () => {
  855. // force re-render so `editor.isActive` works as expected
  856. editor = editor;
  857. if (!editor) return;
  858. htmlValue = editor.getHTML();
  859. jsonValue = editor.getJSON();
  860. mdValue = turndownService
  861. .turndown(
  862. htmlValue
  863. .replace(/<p><\/p>/g, '<br/>')
  864. .replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
  865. )
  866. .replace(/\u00a0/g, ' ');
  867. onChange({
  868. html: htmlValue,
  869. json: jsonValue,
  870. md: mdValue
  871. });
  872. if (json) {
  873. value = jsonValue;
  874. } else {
  875. if (raw) {
  876. value = htmlValue;
  877. } else {
  878. if (!preserveBreaks) {
  879. mdValue = mdValue.replace(/<br\/>/g, '');
  880. }
  881. if (value !== mdValue) {
  882. value = mdValue;
  883. // check if the node is paragraph as well
  884. if (editor.isActive('paragraph')) {
  885. if (value === '') {
  886. editor.commands.clearContent();
  887. }
  888. }
  889. }
  890. }
  891. }
  892. },
  893. editorProps: {
  894. attributes: { id },
  895. handleDOMEvents: {
  896. compositionstart: (view, event) => {
  897. oncompositionstart(event);
  898. return false;
  899. },
  900. compositionend: (view, event) => {
  901. oncompositionend(event);
  902. return false;
  903. },
  904. focus: (view, event) => {
  905. eventDispatch('focus', { event });
  906. return false;
  907. },
  908. keyup: (view, event) => {
  909. eventDispatch('keyup', { event });
  910. return false;
  911. },
  912. keydown: (view, event) => {
  913. if (messageInput) {
  914. // Check if the current selection is inside a structured block (like codeBlock or list)
  915. const { state } = view;
  916. const { $head } = state.selection;
  917. // Recursive function to check ancestors for specific node types
  918. function isInside(nodeTypes: string[]): boolean {
  919. let currentNode = $head;
  920. while (currentNode) {
  921. if (nodeTypes.includes(currentNode.parent.type.name)) {
  922. return true;
  923. }
  924. if (!currentNode.depth) break; // Stop if we reach the top
  925. currentNode = state.doc.resolve(currentNode.before()); // Move to the parent node
  926. }
  927. return false;
  928. }
  929. // Handle Tab Key
  930. if (event.key === 'Tab') {
  931. const isInCodeBlock = isInside(['codeBlock']);
  932. if (isInCodeBlock) {
  933. // Handle tab in code block - insert tab character or spaces
  934. const tabChar = '\t'; // or ' ' for 4 spaces
  935. editor.commands.insertContent(tabChar);
  936. event.preventDefault();
  937. return true; // Prevent further propagation
  938. } else {
  939. const handled = selectNextTemplate(view.state, view.dispatch);
  940. if (handled) {
  941. event.preventDefault();
  942. return true;
  943. }
  944. }
  945. }
  946. if (event.key === 'Enter') {
  947. const isCtrlPressed = event.ctrlKey || event.metaKey; // metaKey is for Cmd key on Mac
  948. if (event.shiftKey && !isCtrlPressed) {
  949. editor.commands.enter(); // Insert a new line
  950. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
  951. event.preventDefault();
  952. return true;
  953. } else {
  954. const isInCodeBlock = isInside(['codeBlock']);
  955. const isInList = isInside(['listItem', 'bulletList', 'orderedList', 'taskList']);
  956. const isInHeading = isInside(['heading']);
  957. if (isInCodeBlock || isInList || isInHeading) {
  958. // Let ProseMirror handle the normal Enter behavior
  959. return false;
  960. }
  961. }
  962. }
  963. // Handle shift + Enter for a line break
  964. if (shiftEnter) {
  965. if (event.key === 'Enter' && event.shiftKey && !event.ctrlKey && !event.metaKey) {
  966. editor.commands.setHardBreak(); // Insert a hard break
  967. view.dispatch(view.state.tr.scrollIntoView()); // Move viewport to the cursor
  968. event.preventDefault();
  969. return true;
  970. }
  971. }
  972. }
  973. eventDispatch('keydown', { event });
  974. return false;
  975. },
  976. paste: (view, event) => {
  977. if (event.clipboardData) {
  978. const plainText = event.clipboardData.getData('text/plain');
  979. if (plainText) {
  980. if (largeTextAsFile && plainText.length > PASTED_TEXT_CHARACTER_LIMIT) {
  981. // Delegate handling of large text pastes to the parent component.
  982. eventDispatch('paste', { event });
  983. event.preventDefault();
  984. return true;
  985. }
  986. // Workaround for mobile WebViews that strip line breaks when pasting from
  987. // clipboard suggestions (e.g., Gboard clipboard history).
  988. const isMobile = /Android|iPhone|iPad|iPod|Windows Phone/i.test(
  989. navigator.userAgent
  990. );
  991. const isWebView =
  992. typeof window !== 'undefined' &&
  993. (/wv/i.test(navigator.userAgent) || // Standard Android WebView flag
  994. (navigator.userAgent.includes('Android') &&
  995. !navigator.userAgent.includes('Chrome')) || // Other generic Android WebViews
  996. (navigator.userAgent.includes('Safari') &&
  997. !navigator.userAgent.includes('Version'))); // iOS WebView (in-app browsers)
  998. if (isMobile && isWebView && plainText.includes('\n')) {
  999. // Manually deconstruct the pasted text and insert it with hard breaks
  1000. // to preserve the multi-line formatting.
  1001. const { state, dispatch } = view;
  1002. const { from, to } = state.selection;
  1003. const lines = plainText.split('\n');
  1004. const nodes = [];
  1005. lines.forEach((line, index) => {
  1006. if (index > 0) {
  1007. nodes.push(state.schema.nodes.hardBreak.create());
  1008. }
  1009. if (line.length > 0) {
  1010. nodes.push(state.schema.text(line));
  1011. }
  1012. });
  1013. const fragment = Fragment.fromArray(nodes);
  1014. const tr = state.tr.replaceWith(from, to, fragment);
  1015. dispatch(tr.scrollIntoView());
  1016. event.preventDefault();
  1017. return true;
  1018. }
  1019. // Let ProseMirror handle normal text paste in non-problematic environments.
  1020. return false;
  1021. }
  1022. // Delegate image paste handling to the parent component.
  1023. const hasImageFile = Array.from(event.clipboardData.files).some((file) =>
  1024. file.type.startsWith('image/')
  1025. );
  1026. // Fallback for cases where an image is in dataTransfer.items but not clipboardData.files.
  1027. const hasImageItem = Array.from(event.clipboardData.items).some((item) =>
  1028. item.type.startsWith('image/')
  1029. );
  1030. const hasFile = Array.from(event.clipboardData.files).length > 0;
  1031. if (hasImageFile || hasImageItem || hasFile) {
  1032. eventDispatch('paste', { event });
  1033. event.preventDefault();
  1034. return true;
  1035. }
  1036. }
  1037. // For all other cases, let ProseMirror perform its default paste behavior.
  1038. view.dispatch(view.state.tr.scrollIntoView());
  1039. return false;
  1040. }
  1041. }
  1042. },
  1043. onBeforeCreate: ({ editor }) => {
  1044. if (files) {
  1045. editor.storage.files = files;
  1046. }
  1047. },
  1048. onSelectionUpdate: onSelectionUpdate
  1049. });
  1050. if (messageInput) {
  1051. selectTemplate();
  1052. }
  1053. getWordAtDocPosCallback(getWordAtDocPos);
  1054. });
  1055. onDestroy(() => {
  1056. if (provider) {
  1057. provider.destroy();
  1058. }
  1059. if (editor) {
  1060. editor.destroy();
  1061. }
  1062. });
  1063. $: if (value !== null && editor && !collaboration) {
  1064. onValueChange();
  1065. }
  1066. const onValueChange = () => {
  1067. if (!editor) return;
  1068. const jsonValue = editor.getJSON();
  1069. const htmlValue = editor.getHTML();
  1070. let mdValue = turndownService
  1071. .turndown(
  1072. (preserveBreaks ? htmlValue.replace(/<p><\/p>/g, '<br/>') : htmlValue).replace(
  1073. / {2,}/g,
  1074. (m) => m.replace(/ /g, '\u00a0')
  1075. )
  1076. )
  1077. .replace(/\u00a0/g, ' ');
  1078. if (value === '') {
  1079. editor.commands.clearContent(); // Clear content if value is empty
  1080. selectTemplate();
  1081. return;
  1082. }
  1083. if (json) {
  1084. if (JSON.stringify(value) !== JSON.stringify(jsonValue)) {
  1085. editor.commands.setContent(value);
  1086. selectTemplate();
  1087. }
  1088. } else {
  1089. if (raw) {
  1090. if (value !== htmlValue) {
  1091. editor.commands.setContent(value);
  1092. selectTemplate();
  1093. }
  1094. } else {
  1095. if (value !== mdValue) {
  1096. editor.commands.setContent(
  1097. preserveBreaks
  1098. ? value
  1099. : marked.parse(value.replaceAll(`\n<br/>`, `<br/>`), {
  1100. breaks: false
  1101. })
  1102. );
  1103. selectTemplate();
  1104. }
  1105. }
  1106. }
  1107. };
  1108. </script>
  1109. {#if showFormattingButtons}
  1110. <div bind:this={bubbleMenuElement} id="bubble-menu" class="p-0">
  1111. <FormattingButtons {editor} />
  1112. </div>
  1113. <div bind:this={floatingMenuElement} id="floating-menu" class="p-0">
  1114. <FormattingButtons {editor} />
  1115. </div>
  1116. {/if}
  1117. <div bind:this={element} class="relative w-full min-w-full h-full min-h-fit {className}" />