Make different onPaste behavior for Editable and Codable

This commit is contained in:
Henrik Giesel 2021-06-17 23:12:15 +02:00
parent 94c789a5bf
commit 817dee1a1b
4 changed files with 23 additions and 12 deletions

View file

@ -70,7 +70,11 @@ export class Codable extends HTMLTextAreaElement {
this.codeMirror.setCursor(this.codeMirror.lineCount(), 0); this.codeMirror.setCursor(this.codeMirror.lineCount(), 0);
} }
enterBehavior(): void { onEnter(): void {
/* default */
}
onPaste(): void {
/* default */ /* default */
} }
} }

View file

@ -1,6 +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 { bridgeCommand } from "./lib";
import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers"; import { nodeIsInline, caretToEnd, getBlockElement } from "./helpers";
function containsInlineContent(field: Element): boolean { function containsInlineContent(field: Element): boolean {
@ -41,7 +42,7 @@ export class Editable extends HTMLElement {
caretToEnd(this); caretToEnd(this);
} }
enterBehavior(event: KeyboardEvent): void { onEnter(event: KeyboardEvent): void {
if ( if (
!getBlockElement(this.getRootNode() as Document | ShadowRoot) !== !getBlockElement(this.getRootNode() as Document | ShadowRoot) !==
event.shiftKey event.shiftKey
@ -50,4 +51,9 @@ export class Editable extends HTMLElement {
document.execCommand("insertLineBreak"); document.execCommand("insertLineBreak");
} }
} }
onPaste(event: ClipboardEvent): void {
bridgeCommand("paste");
event.preventDefault();
}
} }

View file

@ -13,11 +13,6 @@ import { bridgeCommand } from "./lib";
import { onInput, onKey, onKeyUp } from "./inputHandlers"; import { onInput, onKey, onKeyUp } from "./inputHandlers";
import { onFocus, onBlur } from "./focusHandlers"; import { onFocus, onBlur } from "./focusHandlers";
function onPaste(evt: ClipboardEvent): void {
bridgeCommand("paste");
evt.preventDefault();
}
function onCutOrCopy(): void { function onCutOrCopy(): void {
bridgeCommand("cutOrCopy"); bridgeCommand("cutOrCopy");
} }
@ -48,6 +43,8 @@ export class EditingArea extends HTMLDivElement {
is: "anki-codable", is: "anki-codable",
}) as Codable; }) as Codable;
this.shadowRoot!.appendChild(this.codable); this.shadowRoot!.appendChild(this.codable);
this.onPaste = this.onPaste.bind(this);
} }
get activeInput(): Editable | Codable { get activeInput(): Editable | Codable {
@ -72,7 +69,7 @@ export class EditingArea extends HTMLDivElement {
this.addEventListener("input", onInput); this.addEventListener("input", onInput);
this.addEventListener("focus", onFocus); this.addEventListener("focus", onFocus);
this.addEventListener("blur", onBlur); this.addEventListener("blur", onBlur);
this.addEventListener("paste", onPaste); this.addEventListener("paste", this.onPaste);
this.addEventListener("copy", onCutOrCopy); this.addEventListener("copy", onCutOrCopy);
this.addEventListener("oncut", onCutOrCopy); this.addEventListener("oncut", onCutOrCopy);
this.addEventListener("mouseup", updateActiveButtons); this.addEventListener("mouseup", updateActiveButtons);
@ -87,7 +84,7 @@ export class EditingArea extends HTMLDivElement {
this.removeEventListener("input", onInput); this.removeEventListener("input", onInput);
this.removeEventListener("focus", onFocus); this.removeEventListener("focus", onFocus);
this.removeEventListener("blur", onBlur); this.removeEventListener("blur", onBlur);
this.removeEventListener("paste", onPaste); this.removeEventListener("paste", this.onPaste);
this.removeEventListener("copy", onCutOrCopy); this.removeEventListener("copy", onCutOrCopy);
this.removeEventListener("oncut", onCutOrCopy); this.removeEventListener("oncut", onCutOrCopy);
this.removeEventListener("mouseup", updateActiveButtons); this.removeEventListener("mouseup", updateActiveButtons);
@ -138,8 +135,12 @@ export class EditingArea extends HTMLDivElement {
return document.activeElement === this; return document.activeElement === this;
} }
enterBehavior(event: KeyboardEvent): void { onEnter(event: KeyboardEvent): void {
this.activeInput.enterBehavior(event); this.activeInput.onEnter(event);
}
onPaste(event: ClipboardEvent): void {
this.activeInput.onPaste(event);
} }
toggleHtmlEdit(): void { toggleHtmlEdit(): void {

View file

@ -27,7 +27,7 @@ export function onKey(evt: KeyboardEvent): void {
// prefer <br> instead of <div></div> // prefer <br> instead of <div></div>
if (evt.code === "Enter") { if (evt.code === "Enter") {
return currentField.enterBehavior(evt); return currentField.onEnter(evt);
} }
// // fix Ctrl+right/left handling in RTL fields // // fix Ctrl+right/left handling in RTL fields