2020-01-09 02:00:59 +04:00
|
|
|
import { KEYS } from "../keys";
|
2020-01-07 22:21:05 +05:00
|
|
|
|
|
|
|
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) {
|
2020-01-09 02:04:53 +05:00
|
|
|
// Using contenteditable here as it has dynamic width.
|
|
|
|
// But this solution has an issue — it allows to paste
|
|
|
|
// multiline text, which is not currently supported
|
|
|
|
const editable = document.createElement("div");
|
|
|
|
editable.contentEditable = "plaintext-only";
|
|
|
|
editable.tabIndex = 0;
|
|
|
|
editable.innerText = initText;
|
|
|
|
editable.dataset.type = "wysiwyg";
|
|
|
|
|
|
|
|
Object.assign(editable.style, {
|
2020-01-07 22:21:05 +05:00
|
|
|
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%)",
|
|
|
|
textAlign: "center",
|
2020-01-09 02:04:53 +05:00
|
|
|
display: "inline-block",
|
2020-01-07 22:21:05 +05:00
|
|
|
font: font,
|
2020-01-09 02:04:53 +05:00
|
|
|
padding: "4px",
|
|
|
|
outline: "transparent",
|
|
|
|
whiteSpace: "nowrap"
|
2020-01-07 22:21:05 +05:00
|
|
|
});
|
|
|
|
|
2020-01-09 02:04:53 +05:00
|
|
|
editable.onkeydown = ev => {
|
2020-01-07 22:21:05 +05:00
|
|
|
if (ev.key === KEYS.ESCAPE) {
|
|
|
|
ev.preventDefault();
|
2020-01-09 00:06:25 +05:00
|
|
|
if (initText) {
|
2020-01-09 02:04:53 +05:00
|
|
|
editable.innerText = initText;
|
2020-01-09 00:06:25 +05:00
|
|
|
handleSubmit();
|
|
|
|
return;
|
|
|
|
}
|
2020-01-07 22:21:05 +05:00
|
|
|
cleanup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ev.key === KEYS.ENTER) {
|
|
|
|
ev.preventDefault();
|
|
|
|
handleSubmit();
|
|
|
|
}
|
|
|
|
};
|
2020-01-09 02:04:53 +05:00
|
|
|
editable.onblur = handleSubmit;
|
2020-01-07 22:21:05 +05:00
|
|
|
|
|
|
|
function stopEvent(ev: Event) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSubmit() {
|
2020-01-09 02:04:53 +05:00
|
|
|
if (editable.innerText) {
|
|
|
|
onSubmit(editable.innerText);
|
2020-01-07 22:21:05 +05:00
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup() {
|
2020-01-09 02:04:53 +05:00
|
|
|
editable.onblur = null;
|
|
|
|
editable.onkeydown = null;
|
2020-01-07 22:21:05 +05:00
|
|
|
window.removeEventListener("wheel", stopEvent, true);
|
2020-01-09 02:04:53 +05:00
|
|
|
document.body.removeChild(editable);
|
2020-01-07 22:21:05 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener("wheel", stopEvent, true);
|
2020-01-09 02:04:53 +05:00
|
|
|
document.body.appendChild(editable);
|
|
|
|
editable.focus();
|
|
|
|
const selection = window.getSelection();
|
|
|
|
if (selection) {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(editable);
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
2020-01-07 22:21:05 +05:00
|
|
|
}
|