From 335267d42e1e0bd45096b516113535532389a843 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 26 Jan 2021 21:26:04 +0100 Subject: [PATCH] Strip trailing newline if inline elements logic --- qt/aqt/data/web/js/editor.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/qt/aqt/data/web/js/editor.ts b/qt/aqt/data/web/js/editor.ts index a22b699ed..6c505e715 100644 --- a/qt/aqt/data/web/js/editor.ts +++ b/qt/aqt/data/web/js/editor.ts @@ -109,6 +109,10 @@ function nodeIsElement(node: Node): node is Element { return node.nodeType === Node.ELEMENT_NODE; } +function nodeIsText(node: Node): node is Text { + return node.nodeType === Node.TEXT_NODE; +} + function inListItem(): boolean { const anchor = window.getSelection().anchorNode; @@ -273,16 +277,30 @@ function onBlur(): void { } } +function stripTrailingLinebreakIfInlineElements(field: HTMLDivElement) { + if ( + nodeIsElement(field.lastChild) && + field.lastChild.tagName === "BR" && ( + nodeIsText(field.lastChild.previousSibling) || + window.getComputedStyle(field.lastChild.previousSibling as Element).getPropertyValue("display").startsWith("inline") + ) + ) { + console.log('trimmd!', field.lastChild, field.lastChild.previousSibling) + return field.innerHTML.slice(0, -4); + } + + return field.innerHTML; +} + function saveField(type: "blur" | "key"): void { clearChangeTimer(); if (!currentField) { // no field has been focused yet return; } - // type is either 'blur' or 'key' - pycmd( - `${type}:${currentFieldOrdinal()}:${currentNoteId}:${currentField.innerHTML}` - ); + + const fieldText = stripTrailingLinebreakIfInlineElements(currentField); + pycmd(`${type}:${currentFieldOrdinal()}:${currentNoteId}:${fieldText}`); } function currentFieldOrdinal(): string {