Fixed cleaning handlers after cleanup

This commit is contained in:
hazam 2020-01-07 20:23:33 +05:00
parent ae982e9298
commit bf8a288490
3 changed files with 95 additions and 19 deletions

View file

@ -9,3 +9,4 @@ export { handlerRectangles } from "./handlerRectangles";
export { hitTest } from "./collision"; export { hitTest } from "./collision";
export { resizeTest } from "./resizeTest"; export { resizeTest } from "./resizeTest";
export { isTextElement } from "./typeChecks"; export { isTextElement } from "./typeChecks";
export { textWysiwyg } from "./textWysiwyg";

View file

@ -0,0 +1,60 @@
import { KEYS } from "../index";
export function textWysiwyg(
x: number,
y: number,
strokeColor: string,
onSubmit: (text: string) => void
) {
const input = document.createElement("input");
Object.assign(input.style, {
color: strokeColor,
position: "absolute",
top: y - 8 + "px",
left: x + "px",
transform: "translate(-50%, -50%)",
boxShadow: "none",
textAlign: "center",
width: (window.innerWidth - x) * 2 + "px",
fontSize: "20px",
fontFamily: "Virgil",
border: "none",
background: "transparent"
});
input.onkeydown = ev => {
if (ev.key === KEYS.ESCAPE) {
ev.preventDefault();
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();
}

View file

@ -4,7 +4,7 @@ import rough from "roughjs/bin/wrappers/rough";
import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex"; import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex";
import { randomSeed } from "./random"; import { randomSeed } from "./random";
import { newElement, resizeTest, isTextElement } from "./element"; import { newElement, resizeTest, isTextElement, textWysiwyg } from "./element";
import { import {
clearSelection, clearSelection,
getSelectedIndices, getSelectedIndices,
@ -50,11 +50,12 @@ const DEFAULT_PROJECT_NAME = `excalidraw-${getDateTime()}`;
const CANVAS_WINDOW_OFFSET_LEFT = 250; const CANVAS_WINDOW_OFFSET_LEFT = 250;
const CANVAS_WINDOW_OFFSET_TOP = 0; const CANVAS_WINDOW_OFFSET_TOP = 0;
const KEYS = { export const KEYS = {
ARROW_LEFT: "ArrowLeft", ARROW_LEFT: "ArrowLeft",
ARROW_RIGHT: "ArrowRight", ARROW_RIGHT: "ArrowRight",
ARROW_DOWN: "ArrowDown", ARROW_DOWN: "ArrowDown",
ARROW_UP: "ArrowUp", ARROW_UP: "ArrowUp",
ENTER: "Enter",
ESCAPE: "Escape", ESCAPE: "Escape",
DELETE: "Delete", DELETE: "Delete",
BACKSPACE: "Backspace" BACKSPACE: "Backspace"
@ -79,9 +80,8 @@ function resetCursor() {
document.documentElement.style.cursor = ""; document.documentElement.style.cursor = "";
} }
function addTextElement(element: ExcalidrawTextElement) { function addTextElement(element: ExcalidrawTextElement, text: string) {
resetCursor(); resetCursor();
const text = prompt("What text do you want?");
if (text === null || text === "") { if (text === null || text === "") {
return false; return false;
} }
@ -688,9 +688,22 @@ class App extends React.Component<{}, AppState> {
} }
if (isTextElement(element)) { if (isTextElement(element)) {
if (!addTextElement(element)) { textWysiwyg(
return; e.clientX,
} e.clientY,
this.state.currentItemStrokeColor,
text => {
addTextElement(element, text);
elements.push(element);
element.isSelected = true;
this.setState({
draggingElement: null,
elementType: "selection"
});
}
);
return;
} }
elements.push(element); elements.push(element);
@ -920,19 +933,21 @@ class App extends React.Component<{}, AppState> {
100 100
); );
if (!addTextElement(element as ExcalidrawTextElement)) { textWysiwyg(
return; e.clientX,
} e.clientY,
this.state.currentItemStrokeColor,
text => {
addTextElement(element as ExcalidrawTextElement, text);
elements.push(element);
element.isSelected = true;
elements.push(element); this.setState({
draggingElement: null,
this.setState({ elementType: "selection"
draggingElement: null, });
elementType: "selection" }
}); );
element.isSelected = true;
this.forceUpdate();
}} }}
/> />
</div> </div>