2020-01-07 22:21:05 +05:00
|
|
|
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",
|
2020-01-09 00:06:25 +05:00
|
|
|
top: y + "px",
|
2020-01-07 22:21:05 +05:00
|
|
|
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();
|
2020-01-09 00:06:25 +05:00
|
|
|
if (initText) {
|
|
|
|
input.value = initText;
|
|
|
|
handleSubmit();
|
|
|
|
return;
|
|
|
|
}
|
2020-01-07 22:21:05 +05:00
|
|
|
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();
|
|
|
|
}
|