2022-02-25 01:59:06 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import { getRange, getSelection } from "../lib/cross-browser";
|
|
|
|
import { on } from "../lib/events";
|
2022-09-10 10:46:59 +02:00
|
|
|
import { isArrowDown, isArrowLeft, isArrowRight, isArrowUp } from "../lib/keys";
|
|
|
|
import { singleCallback } from "../lib/typing";
|
2022-02-25 01:59:06 +01:00
|
|
|
import { HandlerList } from "./handler-list";
|
|
|
|
|
|
|
|
const nbsp = "\xa0";
|
|
|
|
|
|
|
|
export type SetupInputHandlerAction = (element: HTMLElement) => { destroy(): void };
|
|
|
|
|
|
|
|
export interface InputEventParams {
|
|
|
|
event: InputEvent;
|
|
|
|
}
|
2022-09-10 10:46:59 +02:00
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
export interface InsertTextParams {
|
|
|
|
event: InputEvent;
|
|
|
|
text: Text;
|
|
|
|
}
|
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
type SpecialKeyAction =
|
|
|
|
| "caretUp"
|
|
|
|
| "caretDown"
|
|
|
|
| "caretLeft"
|
|
|
|
| "caretRight"
|
|
|
|
| "enter"
|
|
|
|
| "tab";
|
|
|
|
|
|
|
|
export interface SpecialKeyParams {
|
|
|
|
event: KeyboardEvent;
|
|
|
|
action: SpecialKeyAction;
|
|
|
|
}
|
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
export interface InputHandlerAPI {
|
|
|
|
readonly beforeInput: HandlerList<InputEventParams>;
|
|
|
|
readonly insertText: HandlerList<InsertTextParams>;
|
2022-09-10 10:46:59 +02:00
|
|
|
readonly pointerDown: HandlerList<{ event: PointerEvent }>;
|
|
|
|
readonly specialKey: HandlerList<SpecialKeyParams>;
|
2022-02-25 01:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface that allows Svelte components to attach event listeners via triggers.
|
|
|
|
* They will be attached to the component(s) that install the manager.
|
|
|
|
* Prevents that too many event listeners are attached and allows for some
|
|
|
|
* coordination between them.
|
|
|
|
*/
|
|
|
|
function useInputHandler(): [InputHandlerAPI, SetupInputHandlerAction] {
|
|
|
|
const beforeInput = new HandlerList<InputEventParams>();
|
|
|
|
const insertText = new HandlerList<InsertTextParams>();
|
|
|
|
|
|
|
|
async function onBeforeInput(this: Element, event: InputEvent): Promise<void> {
|
|
|
|
const selection = getSelection(this)!;
|
|
|
|
const range = getRange(selection);
|
|
|
|
|
|
|
|
await beforeInput.dispatch({ event });
|
|
|
|
|
|
|
|
if (!range || event.inputType !== "insertText" || insertText.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const content = !event.data || event.data === " " ? nbsp : event.data;
|
|
|
|
const text = new Text(content);
|
|
|
|
|
|
|
|
range.deleteContents();
|
|
|
|
range.insertNode(text);
|
|
|
|
range.selectNode(text);
|
|
|
|
range.collapse(false);
|
|
|
|
|
|
|
|
await insertText.dispatch({ event, text });
|
|
|
|
|
|
|
|
range.commonAncestorContainer.normalize();
|
|
|
|
}
|
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
const pointerDown = new HandlerList<{ event: PointerEvent }>();
|
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
function clearInsertText(): void {
|
|
|
|
insertText.clear();
|
|
|
|
}
|
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
function onPointerDown(event: PointerEvent): void {
|
|
|
|
pointerDown.dispatch({ event });
|
|
|
|
clearInsertText();
|
|
|
|
}
|
|
|
|
|
|
|
|
const specialKey = new HandlerList<SpecialKeyParams>();
|
|
|
|
|
|
|
|
async function onKeyDown(this: Element, event: KeyboardEvent): Promise<void> {
|
|
|
|
if (isArrowDown(event)) {
|
|
|
|
specialKey.dispatch({ event, action: "caretDown" });
|
|
|
|
} else if (isArrowUp(event)) {
|
|
|
|
specialKey.dispatch({ event, action: "caretUp" });
|
|
|
|
} else if (isArrowRight(event)) {
|
|
|
|
specialKey.dispatch({ event, action: "caretRight" });
|
|
|
|
} else if (isArrowLeft(event)) {
|
|
|
|
specialKey.dispatch({ event, action: "caretLeft" });
|
|
|
|
} else if (event.code === "Enter" || event.code === "NumpadEnter") {
|
|
|
|
specialKey.dispatch({ event, action: "enter" });
|
|
|
|
} else if (event.code === "Tab") {
|
|
|
|
specialKey.dispatch({ event, action: "tab" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 01:59:06 +01:00
|
|
|
function setupHandler(element: HTMLElement): { destroy(): void } {
|
2022-09-10 10:46:59 +02:00
|
|
|
const destroy = singleCallback(
|
|
|
|
on(element, "beforeinput", onBeforeInput),
|
|
|
|
on(element, "blur", clearInsertText),
|
|
|
|
on(element, "pointerdown", onPointerDown),
|
|
|
|
on(element, "keydown", onKeyDown),
|
|
|
|
on(document, "selectionchange", clearInsertText),
|
|
|
|
);
|
|
|
|
|
|
|
|
return { destroy };
|
2022-02-25 01:59:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
beforeInput,
|
|
|
|
insertText,
|
2022-09-10 10:46:59 +02:00
|
|
|
specialKey,
|
|
|
|
pointerDown,
|
2022-02-25 01:59:06 +01:00
|
|
|
},
|
|
|
|
setupHandler,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useInputHandler;
|