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-02-15 21:03:32 +01:00
|
|
|
zoom: 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-02-15 21:03:32 +01:00
|
|
|
zoom,
|
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-02-21 22:51:34 -05:00
|
|
|
try {
|
|
|
|
editable.contentEditable = "plaintext-only";
|
|
|
|
} catch {
|
|
|
|
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,
|
2020-02-01 06:07:08 +00:00
|
|
|
position: "fixed",
|
2020-01-25 18:58:57 +01:00
|
|
|
opacity: opacity / 100,
|
2020-02-02 20:04:35 +02:00
|
|
|
top: `${y}px`,
|
|
|
|
left: `${x}px`,
|
2020-02-15 21:03:32 +01:00
|
|
|
transform: `translate(-50%, -50%) scale(${zoom})`,
|
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",
|
2020-02-01 06:07:08 +00:00
|
|
|
// This needs to have "1px solid" otherwise the carret doesn't show up
|
|
|
|
// the first time on Safari and Chrome!
|
|
|
|
outline: "1px solid transparent",
|
2020-01-10 15:50:13 +01:00
|
|
|
whiteSpace: "nowrap",
|
2020-01-24 12:04:54 +02:00
|
|
|
minHeight: "1em",
|
2020-02-07 18:42:24 +01:00
|
|
|
backfaceVisibility: "hidden",
|
2020-01-07 22:21:05 +05:00
|
|
|
});
|
|
|
|
|
2020-02-07 18:42:24 +01:00
|
|
|
editable.onpaste = ev => {
|
|
|
|
try {
|
|
|
|
const selection = window.getSelection();
|
|
|
|
if (!selection?.rangeCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selection.deleteFromDocument();
|
|
|
|
|
|
|
|
const text = ev.clipboardData!.getData("text").replace(/\r\n?/g, "\n");
|
|
|
|
|
|
|
|
const span = document.createElement("span");
|
|
|
|
span.innerText = text;
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
range.insertNode(span);
|
|
|
|
|
|
|
|
// deselect
|
|
|
|
window.getSelection()!.removeAllRanges();
|
|
|
|
range.setStart(span, span.childNodes.length);
|
|
|
|
range.setEnd(span, span.childNodes.length);
|
|
|
|
selection.addRange(range);
|
|
|
|
|
|
|
|
ev.preventDefault();
|
2020-02-28 23:03:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2020-02-07 18:42:24 +01: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-03-12 18:04:56 +01:00
|
|
|
handleSubmit();
|
2020-01-07 22:21:05 +05:00
|
|
|
}
|
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-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-02-07 18:42:24 +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
|
|
|
}
|