Have correct enter behavior in for Editable and Codable

This commit is contained in:
Henrik Giesel 2021-06-17 22:02:06 +02:00
parent f923660fc6
commit 94c789a5bf
7 changed files with 45 additions and 27 deletions

View file

@ -41,6 +41,6 @@ export function saveNow(keepFocus: boolean): void {
saveField(currentField, "key"); saveField(currentField, "key");
} else { } else {
// triggers onBlur, which saves // triggers onBlur, which saves
currentField.blurEditable(); currentField.blur();
} }
} }

View file

@ -55,6 +55,13 @@ export class Codable extends HTMLTextAreaElement {
this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions); this.codeMirror = CodeMirror.fromTextArea(this, codeMirrorOptions);
} }
teardown(): string {
this.active = false;
this.codeMirror.toTextArea();
this.codeMirror = undefined;
return parseHTML(this.value);
}
focus(): void { focus(): void {
this.codeMirror.focus(); this.codeMirror.focus();
} }
@ -63,10 +70,7 @@ export class Codable extends HTMLTextAreaElement {
this.codeMirror.setCursor(this.codeMirror.lineCount(), 0); this.codeMirror.setCursor(this.codeMirror.lineCount(), 0);
} }
teardown(): string { enterBehavior(): void {
this.active = false; /* default */
this.codeMirror.toTextArea();
this.codeMirror = undefined;
return parseHTML(this.value);
} }
} }

View file

@ -1,7 +1,7 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { nodeIsInline, caretToEnd } from "./helpers"; import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers";
function containsInlineContent(field: Element): boolean { function containsInlineContent(field: Element): boolean {
if (field.childNodes.length === 0) { if (field.childNodes.length === 0) {
@ -40,4 +40,14 @@ export class Editable extends HTMLElement {
caretToEnd() { caretToEnd() {
caretToEnd(this); caretToEnd(this);
} }
enterBehavior(event: KeyboardEvent): void {
if (
!getBlockElement(this.getRootNode() as Document | ShadowRoot) !==
event.shiftKey
) {
event.preventDefault();
document.execCommand("insertLineBreak");
}
}
} }

View file

@ -130,14 +130,6 @@ export class EditingArea extends HTMLDivElement {
this.activeInput.blur(); this.activeInput.blur();
} }
/* legacy */
focusEditable(): void {
focus();
}
blurEditable(): void {
blur();
}
caretToEnd(): void { caretToEnd(): void {
this.activeInput.caretToEnd(); this.activeInput.caretToEnd();
} }
@ -146,6 +138,10 @@ export class EditingArea extends HTMLDivElement {
return document.activeElement === this; return document.activeElement === this;
} }
enterBehavior(event: KeyboardEvent): void {
this.activeInput.enterBehavior(event);
}
toggleHtmlEdit(): void { toggleHtmlEdit(): void {
const hadFocus = this.hasFocus(); const hadFocus = this.hasFocus();
@ -163,4 +159,17 @@ export class EditingArea extends HTMLDivElement {
this.caretToEnd(); this.caretToEnd();
} }
} }
/**
* @deprecated Use focus instead
*/
focusEditable(): void {
focus();
}
/**
* @deprecated Use blur instead
*/
blurEditable(): void {
blur();
}
} }

View file

@ -9,7 +9,7 @@ import { bridgeCommand } from "./lib";
export function onFocus(evt: FocusEvent): void { export function onFocus(evt: FocusEvent): void {
const currentField = evt.currentTarget as EditingArea; const currentField = evt.currentTarget as EditingArea;
currentField.focusEditable(); currentField.focus();
bridgeCommand(`focus:${currentField.ord}`); bridgeCommand(`focus:${currentField.ord}`);
enableButtons(); enableButtons();
} }

View file

@ -50,7 +50,7 @@ export function focusField(n: number): void {
const field = getEditorField(n); const field = getEditorField(n);
if (field) { if (field) {
field.editingArea.focusEditable(); field.editingArea.focus();
field.editingArea.caretToEnd(); field.editingArea.caretToEnd();
updateActiveButtons(new Event("manualfocus")); updateActiveButtons(new Event("manualfocus"));
} }
@ -61,7 +61,7 @@ export function focusIfField(x: number, y: number): boolean {
for (let i = 0; i < elements.length; i++) { for (let i = 0; i < elements.length; i++) {
const elem = elements[i] as EditingArea; const elem = elements[i] as EditingArea;
if (elem instanceof EditingArea) { if (elem instanceof EditingArea) {
elem.focusEditable(); elem.focus();
return true; return true;
} }
} }

View file

@ -7,7 +7,7 @@
import { updateActiveButtons } from "./toolbar"; import { updateActiveButtons } from "./toolbar";
import { EditingArea } from "./editingArea"; import { EditingArea } from "./editingArea";
import { nodeIsElement, getBlockElement } from "./helpers"; import { nodeIsElement } from "./helpers";
import { triggerChangeTimer } from "./changeTimer"; import { triggerChangeTimer } from "./changeTimer";
import { registerShortcut } from "lib/shortcuts"; import { registerShortcut } from "lib/shortcuts";
@ -22,17 +22,12 @@ export function onKey(evt: KeyboardEvent): void {
// esc clears focus, allowing dialog to close // esc clears focus, allowing dialog to close
if (evt.code === "Escape") { if (evt.code === "Escape") {
currentField.blurEditable(); return currentField.blur();
return;
} }
// prefer <br> instead of <div></div> // prefer <br> instead of <div></div>
if ( if (evt.code === "Enter") {
evt.code === "Enter" && return currentField.enterBehavior(evt);
!getBlockElement(currentField.shadowRoot!) !== evt.shiftKey
) {
evt.preventDefault();
document.execCommand("insertLineBreak");
} }
// // fix Ctrl+right/left handling in RTL fields // // fix Ctrl+right/left handling in RTL fields