From 8ce1b881ab86cdac86ce0d686d60ebf523162616 Mon Sep 17 00:00:00 2001 From: Antal Orcsik Date: Fri, 16 Aug 2024 15:48:00 +0200 Subject: [PATCH] editor: fix last line issue --- src/ejs/editor.ejs | 3 ++- src/js/client/editor.js | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ejs/editor.ejs b/src/ejs/editor.ejs index 14d5554..9c6673b 100644 --- a/src/ejs/editor.ejs +++ b/src/ejs/editor.ejs @@ -37,7 +37,8 @@
-
<%= context.rawContent %>
+
<%= context.rawContent %> +
diff --git a/src/js/client/editor.js b/src/js/client/editor.js index 38eee46..c2d2cc7 100644 --- a/src/js/client/editor.js +++ b/src/js/client/editor.js @@ -3,6 +3,10 @@ import { calculateReadingTime, countWords } from "../server/shared"; import "../../css/editor.css"; +const getContent = () => { + return document.getElementById("content").innerText.replace(/\n$/s, ""); +}; + const contentScrollHandler = () => { const editorScrollValue = document.querySelector(".editor-content").scrollTop; const editorContentHeight = document.querySelector(".editor-content pre").offsetHeight; @@ -27,7 +31,7 @@ let updateStatusThrottle = null; const updateStatus = () => { if (updateStatusThrottle) window.clearTimeout(updateStatusThrottle); updateStatusThrottle = window.setTimeout(() => { - const markdownText = document.getElementById("content").innerText; + const markdownText = getContent(); const isLoading = document.querySelectorAll(".preview-container iframe").length; let status; @@ -102,7 +106,7 @@ const changeHandler = (event) => { readingTime: 0, }; - let markdownText = editableContent.innerText; + let markdownText = getContent(); const selection = getRange(editableContent); if (selection) { @@ -162,7 +166,7 @@ const changeHandler = (event) => { `${idx + 1}` + `${line}` + ""; - }).slice(0, -1).join("\n") + "\n"; + }).join("\n"); styleContent.style.paddingLeft = `${gutterSize + 1}ch`; editableContent.style.paddingLeft = `${gutterSize + 1}ch`; @@ -302,7 +306,7 @@ const changeHandler = (event) => { window.clearTimeout(changeThrottle); changeThrottle = window.setTimeout(() => { - let markdownText = document.getElementById("content").innerText; + let markdownText = getContent(); if (document.querySelector("form.editor-container input[name=content]").value !== markdownText) { document.querySelector("form.editor-container input[name=content]").value = markdownText;