mirror of
https://github.com/ankitects/anki.git
synced 2025-11-17 01:57:12 -05:00
Strip trailing newline if inline elements logic
This commit is contained in:
parent
322788133b
commit
335267d42e
1 changed files with 22 additions and 4 deletions
|
|
@ -109,6 +109,10 @@ function nodeIsElement(node: Node): node is Element {
|
||||||
return node.nodeType === Node.ELEMENT_NODE;
|
return node.nodeType === Node.ELEMENT_NODE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function nodeIsText(node: Node): node is Text {
|
||||||
|
return node.nodeType === Node.TEXT_NODE;
|
||||||
|
}
|
||||||
|
|
||||||
function inListItem(): boolean {
|
function inListItem(): boolean {
|
||||||
const anchor = window.getSelection().anchorNode;
|
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 {
|
function saveField(type: "blur" | "key"): void {
|
||||||
clearChangeTimer();
|
clearChangeTimer();
|
||||||
if (!currentField) {
|
if (!currentField) {
|
||||||
// no field has been focused yet
|
// no field has been focused yet
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// type is either 'blur' or 'key'
|
|
||||||
pycmd(
|
const fieldText = stripTrailingLinebreakIfInlineElements(currentField);
|
||||||
`${type}:${currentFieldOrdinal()}:${currentNoteId}:${currentField.innerHTML}`
|
pycmd(`${type}:${currentFieldOrdinal()}:${currentNoteId}:${fieldText}`);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function currentFieldOrdinal(): string {
|
function currentFieldOrdinal(): string {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue