From 9dcc99887843d562b16419611d715e36abfaea58 Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Sat, 23 Jan 2021 20:28:37 -0500
Subject: [PATCH] Fixing markdown spaces. Fixes #101 Fixes #126

---
 src/shared/components/markdown-textarea.tsx | 30 +++++++++++++--------
 1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/src/shared/components/markdown-textarea.tsx b/src/shared/components/markdown-textarea.tsx
index 23551bc..21af02c 100644
--- a/src/shared/components/markdown-textarea.tsx
+++ b/src/shared/components/markdown-textarea.tsx
@@ -420,13 +420,13 @@ export class MarkdownTextArea extends Component<
       i.state.content = `${i.state.content.substring(
         0,
         start
-      )} [${selectedText}]() ${i.state.content.substring(end)}`;
+      )}[${selectedText}]()${i.state.content.substring(end)}`;
       textarea.focus();
-      setTimeout(() => (textarea.selectionEnd = end + 4), 10);
+      setTimeout(() => (textarea.selectionEnd = end + 5), 10);
     } else {
       i.state.content += '[]()';
       textarea.focus();
-      setTimeout(() => (textarea.selectionEnd -= 1), 10);
+      setTimeout(() => (textarea.selectionEnd -= 0), 10);
     }
     i.setState(i.state);
   }
@@ -435,7 +435,15 @@ export class MarkdownTextArea extends Component<
     this.simpleSurroundBeforeAfter(chars, chars);
   }
 
-  simpleSurroundBeforeAfter(beforeChars: string, afterChars: string) {
+  simpleBeginningofLine(chars: string) {
+    this.simpleSurroundBeforeAfter(`${chars} `, '', '');
+  }
+
+  simpleSurroundBeforeAfter(
+    beforeChars: string,
+    afterChars: string,
+    emptyChars: string = '___'
+  ) {
     if (!this.state.content) {
       this.state.content = '';
     }
@@ -447,12 +455,12 @@ export class MarkdownTextArea extends Component<
       let selectedText = this.state.content.substring(start, end);
       this.state.content = `${this.state.content.substring(
         0,
-        start - 1
-      )} ${beforeChars}${selectedText}${afterChars} ${this.state.content.substring(
-        end + 1
+        start
+      )}${beforeChars}${selectedText}${afterChars}${this.state.content.substring(
+        end
       )}`;
     } else {
-      this.state.content += `${beforeChars}___${afterChars}`;
+      this.state.content += `${beforeChars}${emptyChars}${afterChars}`;
     }
     this.setState(this.state);
     setTimeout(() => {
@@ -482,17 +490,17 @@ export class MarkdownTextArea extends Component<
 
   handleInsertList(i: MarkdownTextArea, event: any) {
     event.preventDefault();
-    i.simpleInsert('-');
+    i.simpleBeginningofLine('-');
   }
 
   handleInsertQuote(i: MarkdownTextArea, event: any) {
     event.preventDefault();
-    i.simpleInsert('>');
+    i.simpleBeginningofLine('>');
   }
 
   handleInsertHeader(i: MarkdownTextArea, event: any) {
     event.preventDefault();
-    i.simpleInsert('#');
+    i.simpleBeginningofLine('#');
   }
 
   handleInsertSubscript(i: MarkdownTextArea, event: any) {
-- 
2.44.1