Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Toggle feature not working for chat input formatting toolbars #435

Merged
merged 3 commits into from
Mar 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,34 @@ const ChatInputFormattingToolbar = ({ messageRef, inputRef }) => {
const initText = input.value.slice(0, selectionStart);
const selectedText = input.value.slice(selectionStart, selectionEnd);
const finalText = input.value.slice(selectionEnd, input.value.length);
if (
!document.execCommand ||
!document.execCommand(
'insertText',
false,
pattern.replace('{{text}}', selectedText)
)
) {

const startPattern = pattern.slice(0, pattern.indexOf('{{text}}'));
const endPattern = pattern.slice(
pattern.indexOf('{{text}}') + '{{text}}'.length
);

const startPatternFound = initText.endsWith(startPattern);
const endPatternFound = finalText.startsWith(endPattern);

if (startPatternFound && endPatternFound) {
// Text is already wrapped, so unwrap it
input.value =
initText + pattern.replace('{{text}}', selectedText) + finalText;
}
initText.slice(0, initText.length - startPattern.length) +
selectedText +
finalText.slice(endPattern.length);

input.selectionStart = selectionStart - startPattern.length;
input.selectionEnd = input.selectionStart + selectedText.length;
} else {
// Text is not wrapped, so wrap it
const wrappedText = startPattern + selectedText + endPattern;
if (!document.execCommand?.('insertText', false, wrappedText)) {
input.value = initText + wrappedText + finalText;
}

input.selectionStart = selectionStart + pattern.indexOf('{{text}}');
input.selectionEnd = input.selectionStart + selectedText.length;
input.selectionStart = selectionStart + startPattern.length;
input.selectionEnd = input.selectionStart + selectedText.length;
}
};

const popupStyle = {
Expand Down
Loading