From bd43391c6d8c423ee20270b535d177d89d06f711 Mon Sep 17 00:00:00 2001 From: chris <kokosias@yahoo.gr> Date: Wed, 27 Sep 2023 13:24:31 +0300 Subject: [PATCH] break into paragraphs --- editors/demo/src/Editoria/Editoria.js | 6 +-- editors/demo/src/Editoria/config/config.js | 39 ++++++++++++-- .../src/AiService/InsertTextBelowSelection.js | 11 ++-- .../src/AiService/ReplaceSelectedText.js | 52 +++++++++++++++---- .../src/AiService/components/AskAIOverlay.js | 4 +- 5 files changed, 91 insertions(+), 21 deletions(-) diff --git a/editors/demo/src/Editoria/Editoria.js b/editors/demo/src/Editoria/Editoria.js index 3a72be1e7..9bda4b113 100644 --- a/editors/demo/src/Editoria/Editoria.js +++ b/editors/demo/src/Editoria/Editoria.js @@ -53,9 +53,9 @@ const Editoria = () => { value={demo} // readonly layout={layout} - // onChange={debounce(source => { - // console.log(JSON.stringify(source)); - // }, 200)} + onChange={debounce(source => { + console.log(JSON.stringify(source)); + }, 200)} user={user} scrollMargin={200} scrollThreshold={200} diff --git a/editors/demo/src/Editoria/config/config.js b/editors/demo/src/Editoria/config/config.js index aa5d9607b..81440af16 100644 --- a/editors/demo/src/Editoria/config/config.js +++ b/editors/demo/src/Editoria/config/config.js @@ -64,6 +64,41 @@ import CharactersList from './CharactersList'; // console.log(title); // }; +const text = `In the shroud of redundant noise, consectetur, +A symphony sweeter than summer is discovered, +In the hearty paws and gentle eyes of a dog, our protector, +A tangible form of love, in fur is covered. + +Against every day's chaos, turmoil, and upset, +A wagging tail carries, perhaps, the simplest antidote, +A soothing balm to a soul, in life's whirling roulette, +In each bark and paw, an unwritten heartfelt note. + +Faithful are they, standing unwavering by our side, +Through the laughter, the struggles, and silent cries, +In their hearts, a world where compassion and loyalty reside, +Reflecting the truest emotions in their warm, gleaming eyes. + +From the grandeur of a golden retriever's gallop, +To the charming chortle of a chihuahua's cheer, +Each breed, each dog, narrates a touching tale, +A saga of love, in their worlds, so clear. + +They are guardian angels sporting leathered noses, +Draped in a cloak spun with devotion and trust, +They chase not just toys, but away our woes, +Their love, a precious gem, untouched by worldly rust. + +In every dog, there lies a universe obscure, +A cosmos of gentleness veiled by playful veneer, +Their paw prints leave indelible marks, so pure, +Within us, they awaken a love, incredibly dear. + +So here's to these creatures, adorable, and fierce, +The ones who teach us about love unfeigned, +In the echoing cacophony of this life, so diverse, +Semper fidelis - forever faithful, their love remains.`; + async function DummyPromise(userInput) { return new Promise((resolve, reject) => { setTimeout(() => { @@ -71,9 +106,7 @@ async function DummyPromise(userInput) { if (userInput === 'reject') { reject('Your request could not be processed for now'); } else { - resolve( - 'He made significant contributions to theoretical physics, including achievements in quantum mechanics', - ); + resolve(text); } }, 4150); }); diff --git a/wax-prosemirror-services/src/AiService/InsertTextBelowSelection.js b/wax-prosemirror-services/src/AiService/InsertTextBelowSelection.js index ac3e907c7..5d14eea5b 100644 --- a/wax-prosemirror-services/src/AiService/InsertTextBelowSelection.js +++ b/wax-prosemirror-services/src/AiService/InsertTextBelowSelection.js @@ -1,13 +1,14 @@ import { TextSelection } from 'prosemirror-state'; -export const insertTextBelowSelection = (view, transformedText) => { - let state = view.state; - let tr = state.tr; + +const insertTextBelowSelection = (view, transformedText) => { + let { state } = view; + let { tr } = state; const { to } = tr.selection; // Check if 'to' is within the document size if (to > state.doc.content.size) { - console.error("Position out of range"); + console.error('Position out of range'); return; } @@ -37,3 +38,5 @@ export const insertTextBelowSelection = (view, transformedText) => { // Dispatch the final transaction to update the state view.dispatch(tr); }; + +export default insertTextBelowSelection; diff --git a/wax-prosemirror-services/src/AiService/ReplaceSelectedText.js b/wax-prosemirror-services/src/AiService/ReplaceSelectedText.js index b0b5bb5ee..315f96ea7 100644 --- a/wax-prosemirror-services/src/AiService/ReplaceSelectedText.js +++ b/wax-prosemirror-services/src/AiService/ReplaceSelectedText.js @@ -1,13 +1,22 @@ -import { TextSelection } from 'prosemirror-state'; -export const replaceSelectedText = (view, transformedText) => { - let state = view.state; - let tr = state.tr; +import { DOMParser } from 'prosemirror-model'; +import { ReplaceStep, ReplaceAroundStep } from 'prosemirror-transform'; +import { Selection, TextSelection } from 'prosemirror-state'; + +const elementFromString = string => { + const wrappedValue = `<body>${string}</body>`; + + return new window.DOMParser().parseFromString(wrappedValue, 'text/html').body; +}; + +const replaceSelectedText = (view, transformedText) => { + let { state } = view; + let { tr } = state; const { from, to } = tr.selection; // Check if 'from' and 'to' are within the document size if (from > state.doc.content.size || to > state.doc.content.size) { - console.error("Position out of range"); + console.error('Position out of range'); return; } @@ -19,11 +28,33 @@ export const replaceSelectedText = (view, transformedText) => { // Fetch the most recent state again state = view.state; - // Create a new text node with the transformed text - const newText = state.schema.text(transformedText); + const paragraphNodes = []; + + if (transformedText.includes('\n\n')) { + transformedText.split('\n\n').forEach(element => { + paragraphNodes.push( + state.schema.nodes.paragraph.create( + { + preserveWhitespace: 'full', + }, + state.schema.text(element), + ), + ); + }); + } + + const newText = state.schema.nodes.paragraph.create( + { + preserveWhitespace: 'full', + }, + state.schema.text(transformedText), + ); + + const finalReplacementText = + paragraphNodes.length !== 0 ? paragraphNodes : newText; // Replace the selected text with the new text - tr = tr.replaceWith(from, from, newText); // Note: 'to' is replaced with 'from' + tr = tr.replaceWith(from, from, finalReplacementText); // Note: 'to' is replaced with 'from' // Dispatch the transaction to update the state view.dispatch(tr); @@ -33,9 +64,12 @@ export const replaceSelectedText = (view, transformedText) => { // Update the selection to the end of the new text const newTo = from + transformedText.length; - const newSelection = TextSelection.create(state.doc, newTo, newTo); + const newSelection = TextSelection.create(state.doc, newTo + 2, newTo + 2); tr = state.tr.setSelection(newSelection); // Dispatch the final transaction to update the state view.dispatch(tr); + view.focus(); }; + +export default replaceSelectedText; diff --git a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js index e33fa0a72..bf3564361 100644 --- a/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js +++ b/wax-prosemirror-services/src/AiService/components/AskAIOverlay.js @@ -2,8 +2,8 @@ import React, { useRef, useLayoutEffect, useContext, useState } from 'react'; import styled from 'styled-components'; import { WaxContext, icons } from 'wax-prosemirror-core'; -import { replaceSelectedText } from '../ReplaceSelectedText'; -import { insertTextBelowSelection } from '../InsertTextBelowSelection'; +import replaceSelectedText from '../ReplaceSelectedText'; +import insertTextBelowSelection from '../InsertTextBelowSelection'; const Wrapper = styled.div` display: flex; -- GitLab