12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import { KEYS } from "../index";
- type TextWysiwygParams = {
- initText: string;
- x: number;
- y: number;
- strokeColor: string;
- font: string;
- onSubmit: (text: string) => void;
- };
- export function textWysiwyg({
- initText,
- x,
- y,
- strokeColor,
- font,
- onSubmit
- }: TextWysiwygParams) {
- const input = document.createElement("input");
- input.value = initText;
- Object.assign(input.style, {
- color: strokeColor,
- position: "absolute",
- top: y + "px",
- left: x + "px",
- transform: "translate(-50%, -50%)",
- boxShadow: "none",
- textAlign: "center",
- width: (window.innerWidth - x) * 2 + "px",
- font: font,
- border: "none",
- background: "transparent"
- });
- input.onkeydown = ev => {
- if (ev.key === KEYS.ESCAPE) {
- ev.preventDefault();
- if (initText) {
- input.value = initText;
- handleSubmit();
- return;
- }
- cleanup();
- return;
- }
- if (ev.key === KEYS.ENTER) {
- ev.preventDefault();
- handleSubmit();
- }
- };
- input.onblur = handleSubmit;
- function stopEvent(ev: Event) {
- ev.stopPropagation();
- }
- function handleSubmit() {
- if (input.value) {
- onSubmit(input.value);
- }
- cleanup();
- }
- function cleanup() {
- input.onblur = null;
- input.onkeydown = null;
- window.removeEventListener("wheel", stopEvent, true);
- document.body.removeChild(input);
- }
- window.addEventListener("wheel", stopEvent, true);
- document.body.appendChild(input);
- input.focus();
- input.select();
- }
|