@snap/camera-kit
Version:
Camera Kit Web
89 lines • 3.26 kB
JavaScript
import { isState, forActions } from "@snap/state-management";
import { Injectable } from "../dependency-injection/Injectable";
import { TypedCustomEvent } from "../events/TypedCustomEvent";
import { TypedEventTarget } from "../events/TypedEventTarget";
import { lensStateFactory } from "./lensState";
export class LensKeyboard {
constructor(lensState) {
this.lensState = lensState;
this.active = false;
this.element = document.createElement("textarea");
this.element.addEventListener("keypress", (event) => {
if (event.code === "Enter" && !event.shiftKey) {
event.preventDefault();
this.handleReply(this.element.value);
}
});
this.events = new TypedEventTarget();
this.handleReply = () => { };
this.uriHandler = {
uri: "app://textInput/requestKeyboard",
handleRequest: (_request, reply) => {
this.element.autofocus = true;
this.handleReply = (text) => {
const opt = {
text: text,
start: text.length,
end: text.length,
done: true,
shouldNotify: true,
};
const output = new TextEncoder().encode(JSON.stringify(opt));
reply({
code: 200,
description: "",
contentType: "application/json",
data: output,
});
};
this.active = true;
this.updateStatus();
this.element.focus();
},
};
lensState.events.pipe(forActions("turnedOff")).subscribe(() => {
this.dismiss();
});
}
dismiss() {
if (this.active) {
this.active = false;
this.element.value = "";
this.updateStatus();
}
}
getElement() {
return this.element;
}
sendInputToLens(text) {
this.element.value = text;
this.handleReply(text);
}
addEventListener(type, callback, options) {
this.events.addEventListener(type, callback, options);
}
removeEventListener(type, callback) {
this.events.removeEventListener(type, callback);
}
toPublicInterface() {
return {
addEventListener: this.addEventListener.bind(this),
removeEventListener: this.removeEventListener.bind(this),
getElement: this.getElement.bind(this),
sendInputToLens: this.sendInputToLens.bind(this),
dismiss: this.dismiss.bind(this),
};
}
updateStatus() {
const state = this.lensState.getState();
if (isState(state, "noLensApplied"))
return;
this.events.dispatchEvent(new TypedCustomEvent("active", {
element: this.element,
active: this.active,
lens: state.data,
}));
}
}
export const lensKeyboardFactory = Injectable("lensKeyboard", [lensStateFactory.token], (lensState) => new LensKeyboard(lensState));
//# sourceMappingURL=LensKeyboard.js.map