2020-01-09 02:00:59 +04:00
|
|
|
import { KEYS } from "../keys";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { selectNode } from "../utils";
|
2020-01-07 22:21:05 +05:00
|
|
|
|
|
|
|
type TextWysiwygParams = {
|
|
|
|
initText: string;
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
strokeColor: string;
|
|
|
|
font: string;
|
2020-01-25 18:58:57 +01:00
|
|
|
opacity: number;
|
2020-01-07 22:21:05 +05:00
|
|
|
onSubmit: (text: string) => void;
|
2020-01-25 18:45:23 +01:00
|
|
|
onCancel: () => void;
|
2020-01-07 22:21:05 +05:00
|
|
|
};
|
|
|
|
|
2020-01-19 20:52:19 +01:00
|
|
|
// When WYSIWYG text ends with white spaces, the text gets vertically misaligned
|
|
|
|
// in order to fix this issue, we remove those white spaces
|
|
|
|
function trimText(text: string) {
|
|
|
|
return text.trim();
|
|
|
|
}
|
|
|
|
|
2020-01-07 22:21:05 +05:00
|
|
|
export function textWysiwyg({
|
|
|
|
initText,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
strokeColor,
|
|
|
|
font,
|
2020-01-25 18:58:57 +01:00
|
|
|
opacity,
|
2020-01-24 12:04:54 +02:00
|
|
|
onSubmit,
|
2020-01-25 18:45:23 +01:00
|
|
|
onCancel,
|
2020-01-07 22:21:05 +05:00
|
|
|
}: 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");
|
2020-01-10 15:50:13 +01:00
|
|
|
editable.contentEditable = "true";
|
2020-01-09 02:04:53 +05:00
|
|
|
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-25 18:58:57 +01:00
|
|
|
opacity: opacity / 100,
|
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%)",
|
2020-01-24 10:35:51 -08:00
|
|
|
textAlign: "left",
|
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",
|
2020-01-10 15:50:13 +01:00
|
|
|
whiteSpace: "nowrap",
|
2020-01-24 12:04:54 +02:00
|
|
|
minHeight: "1em",
|
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;
|
|
|
|
}
|
2020-01-24 10:35:51 -08:00
|
|
|
if (ev.key === KEYS.ENTER && !ev.shiftKey) {
|
2020-01-07 22:21:05 +05:00
|
|
|
ev.preventDefault();
|
2020-01-19 07:13:44 +09:00
|
|
|
if (ev.isComposing || ev.keyCode === 229) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-07 22:21:05 +05:00
|
|
|
handleSubmit();
|
|
|
|
}
|
|
|
|
};
|
2020-01-09 02:04:53 +05:00
|
|
|
editable.onblur = handleSubmit;
|
2020-01-10 15:50:13 +01:00
|
|
|
// override paste to disallow non-textual data, and replace newlines
|
|
|
|
editable.onpaste = ev => {
|
|
|
|
ev.preventDefault();
|
|
|
|
try {
|
|
|
|
const text = ev.clipboardData!.getData("text").replace(/\n+/g, " ");
|
|
|
|
editable.textContent = text;
|
|
|
|
} catch {}
|
|
|
|
};
|
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) {
|
2020-01-19 20:52:19 +01:00
|
|
|
onSubmit(trimText(editable.innerText));
|
2020-01-25 18:45:23 +01:00
|
|
|
} else {
|
|
|
|
onCancel();
|
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-10 15:50:13 +01:00
|
|
|
editable.onpaste = 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();
|
2020-01-15 20:42:02 +05:00
|
|
|
selectNode(editable);
|
2020-01-07 22:21:05 +05:00
|
|
|
}
|