From c8055090ca912afb9e2bec0714c736c2e941ea4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20=C3=96zbey?= Date: Tue, 13 Jul 2021 14:14:15 +0300 Subject: [PATCH] improve selected text stylization --- .../core/js/src/common/utils/styleSelectedText.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/framework/core/js/src/common/utils/styleSelectedText.ts b/framework/core/js/src/common/utils/styleSelectedText.ts index fc42c7c4b..c206327b8 100644 --- a/framework/core/js/src/common/utils/styleSelectedText.ts +++ b/framework/core/js/src/common/utils/styleSelectedText.ts @@ -140,7 +140,7 @@ function blockStyle(textarea: HTMLTextAreaElement, arg: StyleArgs): SelectionRan let selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd); let prefixToUse = isMultipleLines(selectedText) && blockPrefix.length > 0 ? `${blockPrefix}\n` : prefix; - let suffixToUse = isMultipleLines(selectedText) && blockSuffix.length > 0 ? `\n${blockSuffix}` : suffix; + let suffixToUse = isMultipleLines(selectedText) && blockSuffix.length > 0 ? `\n${blockSuffix}` : prefixToUse === prefix ? suffix : ''; if (prefixSpace) { const beforeSelection = textarea.value[textarea.selectionStart - 1]; @@ -198,19 +198,21 @@ function blockStyle(textarea: HTMLTextAreaElement, arg: StyleArgs): SelectionRan } function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) { - const { prefix, suffix, surroundWithNewlines } = arg; + const { prefix, suffix, blockPrefix, blockSuffix, surroundWithNewlines } = arg; let text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd); let selectionStart = textarea.selectionStart; let selectionEnd = textarea.selectionEnd; const lines = text.split('\n'); - const undoStyle = lines.every((line) => line.startsWith(prefix) && line.endsWith(suffix)); + let prefixToUse = blockPrefix.length > 0 ? blockPrefix : prefix; + let suffixToUse = blockSuffix.length > 0 ? blockSuffix : prefixToUse == prefix ? suffix : ''; + const undoStyle = lines.every((line) => line.startsWith(prefixToUse) && line.endsWith(suffixToUse)); if (undoStyle) { - text = lines.map((line) => line.slice(prefix.length, line.length - suffix.length)).join('\n'); + text = lines.map((line) => line.slice(prefixToUse.length, line.length - suffixToUse.length)).join('\n'); selectionEnd = selectionStart + text.length; } else { - text = lines.map((line) => prefix + line + suffix).join('\n'); - if (surroundWithNewlines) { + text = lines.map((line) => prefixToUse + line + suffixToUse).join('\n'); + if (surroundWithNewlines || suffixToUse === '') { const { newlinesToAppend, newlinesToPrepend } = newlinesToSurroundSelectedText(textarea); selectionStart += newlinesToAppend.length; selectionEnd = selectionStart + text.length;