Clear selection if no element is clicked
This commit is contained in:
parent
874934e585
commit
61bdedaecf
81
src/index.js
81
src/index.js
@ -327,6 +327,7 @@ class App extends React.Component {
|
|||||||
onMouseDown={e => {
|
onMouseDown={e => {
|
||||||
const x = e.clientX - e.target.offsetLeft;
|
const x = e.clientX - e.target.offsetLeft;
|
||||||
const y = e.clientY - e.target.offsetTop;
|
const y = e.clientY - e.target.offsetTop;
|
||||||
|
const element = newElement(this.state.elementType, x, y);
|
||||||
let isDraggingElements = false;
|
let isDraggingElements = false;
|
||||||
const cursorStyle = document.documentElement.style.cursor;
|
const cursorStyle = document.documentElement.style.cursor;
|
||||||
if (this.state.elementType === "selection") {
|
if (this.state.elementType === "selection") {
|
||||||
@ -341,41 +342,39 @@ class App extends React.Component {
|
|||||||
if (isDraggingElements) {
|
if (isDraggingElements) {
|
||||||
document.documentElement.style.cursor = "move";
|
document.documentElement.style.cursor = "move";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.state.elementType === "text") {
|
||||||
|
const text = prompt("What text do you want?");
|
||||||
|
if (text === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
element.text = text;
|
||||||
|
element.font = "20px Virgil";
|
||||||
|
const font = context.font;
|
||||||
|
context.font = element.font;
|
||||||
|
element.measure = context.measureText(element.text);
|
||||||
|
context.font = font;
|
||||||
|
const height =
|
||||||
|
element.measure.actualBoundingBoxAscent +
|
||||||
|
element.measure.actualBoundingBoxDescent;
|
||||||
|
// Center the text
|
||||||
|
element.x -= element.measure.width / 2;
|
||||||
|
element.y -= element.measure.actualBoundingBoxAscent;
|
||||||
|
element.width = element.measure.width;
|
||||||
|
element.height = height;
|
||||||
|
}
|
||||||
|
|
||||||
|
generateDraw(element);
|
||||||
|
elements.push(element);
|
||||||
|
if (this.state.elementType === "text") {
|
||||||
|
this.setState({
|
||||||
|
draggingElement: null,
|
||||||
|
elementType: "selection"
|
||||||
|
});
|
||||||
|
element.isSelected = true;
|
||||||
} else {
|
} else {
|
||||||
const element = newElement(this.state.elementType, x, y);
|
this.setState({ draggingElement: element });
|
||||||
|
|
||||||
if (this.state.elementType === "text") {
|
|
||||||
const text = prompt("What text do you want?");
|
|
||||||
if (text === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
element.text = text;
|
|
||||||
element.font = "20px Virgil";
|
|
||||||
const font = context.font;
|
|
||||||
context.font = element.font;
|
|
||||||
element.measure = context.measureText(element.text);
|
|
||||||
context.font = font;
|
|
||||||
const height =
|
|
||||||
element.measure.actualBoundingBoxAscent +
|
|
||||||
element.measure.actualBoundingBoxDescent;
|
|
||||||
// Center the text
|
|
||||||
element.x -= element.measure.width / 2;
|
|
||||||
element.y -= element.measure.actualBoundingBoxAscent;
|
|
||||||
element.width = element.measure.width;
|
|
||||||
element.height = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
generateDraw(element);
|
|
||||||
elements.push(element);
|
|
||||||
if (this.state.elementType === "text") {
|
|
||||||
this.setState({
|
|
||||||
draggingElement: null,
|
|
||||||
elementType: "selection"
|
|
||||||
});
|
|
||||||
element.isSelected = true;
|
|
||||||
} else {
|
|
||||||
this.setState({ draggingElement: element });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let lastX = x;
|
let lastX = x;
|
||||||
@ -417,22 +416,30 @@ class App extends React.Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onMouseUp = e => {
|
const onMouseUp = e => {
|
||||||
|
const { draggingElement, elementType } = this.state
|
||||||
|
|
||||||
window.removeEventListener("mousemove", onMouseMove);
|
window.removeEventListener("mousemove", onMouseMove);
|
||||||
window.removeEventListener("mouseup", onMouseUp);
|
window.removeEventListener("mouseup", onMouseUp);
|
||||||
|
|
||||||
document.documentElement.style.cursor = cursorStyle;
|
document.documentElement.style.cursor = cursorStyle;
|
||||||
|
|
||||||
const draggingElement = this.state.draggingElement;
|
// if no element is clicked, clear the selection and redraw
|
||||||
if (draggingElement === null) {
|
if (draggingElement === null) {
|
||||||
return;
|
clearSelection()
|
||||||
|
drawScene();
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (this.state.elementType === "selection") {
|
|
||||||
|
if (elementType === "selection") {
|
||||||
if (isDraggingElements) {
|
if (isDraggingElements) {
|
||||||
isDraggingElements = false;
|
isDraggingElements = false;
|
||||||
}
|
}
|
||||||
|
elements.pop()
|
||||||
setSelection(draggingElement);
|
setSelection(draggingElement);
|
||||||
} else {
|
} else {
|
||||||
draggingElement.isSelected = true;
|
draggingElement.isSelected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
draggingElement: null,
|
draggingElement: null,
|
||||||
elementType: "selection"
|
elementType: "selection"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user