Skip to content
Snippets Groups Projects
Commit 40dc5d44 authored by Samuel Jugnet's avatar Samuel Jugnet
Browse files

non working text keydown handling

parent fc7e81b2
Branches
No related tags found
1 merge request!10Draft: MigratingAnnotationCreation to MUI5.
Pipeline #1703 failed
...@@ -23,7 +23,7 @@ class TextNode extends React.Component { ...@@ -23,7 +23,7 @@ class TextNode extends React.Component {
} }
handleClick = () => { handleClick = () => {
this.props.onShapeClick(this.props.id); this.props.onShapeClick(this.props._id);
}; };
...@@ -32,14 +32,70 @@ class TextNode extends React.Component { ...@@ -32,14 +32,70 @@ class TextNode extends React.Component {
if (this.trRef.current) { if (this.trRef.current) {
this.trRef.current.nodes([this.shapeRef.current]); this.trRef.current.nodes([this.shapeRef.current]);
this.trRef.current.getLayer().batchDraw(); this.trRef.current.getLayer().batchDraw();
// add event listener for key down
// this.shapeRef.current.addEventListener('keydown', this.handleKeyDown);
}
}
handleKeyDown = (e) => {
// if (e.keyCode === 13) {
// this.shapeRef.current.blur();
// }
//type text content
console.log('Key down:', e.evt.key);
//update text content
if (e.evt.key === 'Backspace') {
const { shapes } = this.state;
const { selectedShapeId } = this.state;
// update text of selected shape
const newShapes = shapes.map((shape) => {
if (shape.id === selectedShapeId) {
shape.text = shape.text.slice(0, -1);
}
return shape;
}
);
this.setState({ shapes: newShapes });
}else{
const { shapes } = this.state;
const { selectedShapeId } = this.state;
// update text of selected shape
const newShapes = shapes.map((shape) => {
if (shape.id === selectedShapeId) {
shape.text = shape.text + e.evt.key;
} }
return shape;
}
);
this.setState({ shapes: newShapes });
}
} }
render() { render() {
const { activeTool } = this.props; const { activeTool } = this.props;
console.log("selectedId", this.props.selectedShapeId); console.log("selectedId", this.props._id, window.selectedShapeId);
const isSelected = this.props.id === this.props.selectedShapeId; const isSelected = this.props._id === window.selectedShapeId;
console.log('is selected', this.props._id,isSelected);
return ( return (
...@@ -51,11 +107,14 @@ class TextNode extends React.Component { ...@@ -51,11 +107,14 @@ class TextNode extends React.Component {
fontSize={this.props.fontSize} fontSize={this.props.fontSize}
fill={this.props.fill} fill={this.props.fill}
text={this.props.text} text={this.props.text}
id={this.props._id}
//dragable if tool is cursor or edit //dragable if tool is cursor or edit
draggable={activeTool === 'cursor' || activeTool === 'edit'} draggable={activeTool === 'cursor' || activeTool === 'edit'}
onClick={this.handleClick} onClick={this.handleClick}
onKeyDown={this.handleKeyDown}
// onClick={activeTool === 'cursor' ? null : null} // onClick={activeTool === 'cursor' ? null : null}
// onDblClick={acveTool === 'edit' ? this.handleClick : null}ti // onDblClick={acveTool === 'edit' ? this.handleClick : null}ti
...@@ -87,6 +146,9 @@ class Rectangle extends React.Component { ...@@ -87,6 +146,9 @@ class Rectangle extends React.Component {
this.shapeRef = React.createRef(); this.shapeRef = React.createRef();
this.trRef = React.createRef(); this.trRef = React.createRef();
} }
...@@ -99,16 +161,17 @@ class Rectangle extends React.Component { ...@@ -99,16 +161,17 @@ class Rectangle extends React.Component {
} }
handleClick = () => { handleClick = () => {
this.props.onShapeClick(this.props.id); this.props.onShapeClick(this.props._id);
}; };
render() { render() {
const { activeTool } = this.props; const { activeTool } = this.props;
console.log('active tool ===>', activeTool); console.log("selectedId", this.props._id, window.selectedShapeId);
console.log("selectedId", this.props.selectedShapeId);
const isSelected = this.props.id === this.props.selectedShapeId;
const isSelected = this.props._id === window.selectedShapeId;
console.log('is selected', this.props._id,isSelected);
return ( return (
<React.Fragment> <React.Fragment>
...@@ -122,6 +185,7 @@ class Rectangle extends React.Component { ...@@ -122,6 +185,7 @@ class Rectangle extends React.Component {
fill={this.props.fill || 'red'} fill={this.props.fill || 'red'}
stroke={this.props.stroke || 'black'} stroke={this.props.stroke || 'black'}
strokeWidth={this.props.strokeWidth || 1} strokeWidth={this.props.strokeWidth || 1}
id={this.props._id}
draggable={activeTool === 'cursor' || activeTool === 'edit'} draggable={activeTool === 'cursor' || activeTool === 'edit'}
onClick={this.handleClick} onClick={this.handleClick}
...@@ -136,7 +200,7 @@ class Rectangle extends React.Component { ...@@ -136,7 +200,7 @@ class Rectangle extends React.Component {
<Transformer ref={this.trRef} <Transformer ref={this.trRef}
visible={activeTool === 'edit'} visible={activeTool === 'edit' && isSelected}
/> />
...@@ -153,25 +217,67 @@ class ParentComponent extends React.Component { ...@@ -153,25 +217,67 @@ class ParentComponent extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
selectedShapeId: this.props.selectedShapeId,
currentShape: null,
};
} }
handleShapeClick = (id) => { handleShapeClick = (id) => {
console.log('shape clicked', id); console.log('shape clicked', id);
window.selectedShapeId = id;
this.setState({ selectedShapeId: id }); this.setState({ selectedShapeId: id });
// this.setState({ selectedShapeId: id });
}; };
//handle key down
// handleKeyPress = (e) => {
// console.log('key press', e);
// if (e.key === 'Backspace') {
// const { shapes } = this.state;
// const { selectedShapeId } = this.state;
// const newShapes = shapes.filter((shape) => shape.id !== selectedShapeId);
// this.setState({ shapes: newShapes });
// }
// if (e.key === 'Escape') {
// this.setState({ currentShape: null });
// window.removeEventListener('keydown', this.handleKeyPress);
// }
// if (e.key === 'Enter') {
// this.setState({ currentShape: null });
// window.removeEventListener('keydown', this.handleKeyPress);
// }
// if (e.key === 'Delete') {
// const { shapes } = this.state;
// const { selectedShapeId } = this.state;
// const newShapes = shapes.filter((shape) => shape.id !== selectedShapeId);
// this.setState({ shapes: newShapes });
// }
// }
render() { render() {
const { shapes, selectedShapeId const { shapes
} = this.props; } = this.props;
console.log("selectedId", this.props.selectedShapeId); console.log('shapes', this.props.shapes);
return ( return (
<Layer> <Layer>
{shapes.map((shape, i) => { {shapes.map((shape, i) => {
console.log('shape', shape);
switch (shape.type) { switch (shape.type) {
case 'rectangle': case 'rectangle':
...@@ -179,7 +285,7 @@ class ParentComponent extends React.Component { ...@@ -179,7 +285,7 @@ class ParentComponent extends React.Component {
return ( return (
<Rectangle <Rectangle
selectedShapeId={this.props.selectedShapeId}
activeTool={this.props.activeTool} activeTool={this.props.activeTool}
_id={shape.id} _id={shape.id}
key={i} key={i}
...@@ -204,7 +310,7 @@ class ParentComponent extends React.Component { ...@@ -204,7 +310,7 @@ class ParentComponent extends React.Component {
<TextNode <TextNode
activeTool={this.props.activeTool} activeTool={this.props.activeTool}
selectedShapeId={this.props.selectedShapeId}
_id={shape.id} _id={shape.id}
key={i} key={i}
x={shape.x} x={shape.x}
...@@ -366,7 +472,24 @@ class AnnotationDrawing extends Component { ...@@ -366,7 +472,24 @@ class AnnotationDrawing extends Component {
console.log('draw konvas', this.props); console.log('draw konvas', this.props);
const { shapes, newShape, currentShape } = this.state; const { shapes, newShape } = this.state;
// update selected shape with new props if any
// for (let shape of shapes) {
// if (shape.id === window.selectedShapeId) {
// shape.strokeColor = this.props.strokeColor;
// shape.strokeWidth = this.props.strokeWidth;
// shape.fill = this.props.fillColor;
// // update shape in state
// this.setState({ shapes: [...shapes] });
// }
// }
...@@ -389,8 +512,9 @@ class AnnotationDrawing extends Component { ...@@ -389,8 +512,9 @@ class AnnotationDrawing extends Component {
<ParentComponent shapes={shapes} <ParentComponent shapes={shapes}
selectedShapeId
activeTool={this.props.activeTool} activeTool={this.props.activeTool}
selectedShapeId={this.state.selectedShapeId}
/> />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment