Skip to content

Commit

Permalink
editor: fix last line issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aorcsik committed Aug 16, 2024
1 parent 2074441 commit 8ce1b88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/ejs/editor.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
<div class="editor-content">
<pre class="contentStylePre"><div id="contentStyle"></div></pre>
<pre class="contentLinesPre"><div id="contentLines"></div></pre>
<pre class="contentPre"><div id="content" contenteditable><%= context.rawContent %></div></pre>
<pre class="contentPre"><div id="content" contenteditable><%= context.rawContent %>
</div></pre>
</div>
<div class="editor-footer"></div>
</form>
Expand Down
12 changes: 8 additions & 4 deletions src/js/client/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -102,7 +106,7 @@ const changeHandler = (event) => {
readingTime: 0,
};

let markdownText = editableContent.innerText;
let markdownText = getContent();

const selection = getRange(editableContent);
if (selection) {
Expand Down Expand Up @@ -162,7 +166,7 @@ const changeHandler = (event) => {
`<span class='mdLineNumber' style='width: ${gutterSize}ch;'>${idx + 1}</span>` +
`<span class='mdLineContent'>${line}</span>` +
"</span>";
}).slice(0, -1).join("\n") + "\n";
}).join("\n");
styleContent.style.paddingLeft = `${gutterSize + 1}ch`;
editableContent.style.paddingLeft = `${gutterSize + 1}ch`;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 8ce1b88

Please sign in to comment.