Feature/new react sketch pad component#45
Conversation
…Code.JavaScript.React.Components into feature/new-react-sketch-pad-component
Codecov Report
@@ Coverage Diff @@
## master #45 +/- ##
===========================================
- Coverage 41.64% 18.84% -22.80%
===========================================
Files 41 51 +10
Lines 401 886 +485
Branches 117 202 +85
===========================================
Hits 167 167
- Misses 224 709 +485
Partials 10 10
Continue to review full report at Codecov.
|
brandongregoryscott
left a comment
There was a problem hiding this comment.
Still looking through this, but I left some initial feedback. I might be missing some context around how these components interact, but the relationship between react-canvas-sketch.tsx and canvas-sketch.tsx wasn't very easy to follow as a reader.
| this._redrawSketch(); | ||
| } | ||
|
|
||
| public redrawBackgroundImageUsing(backgroundImageUrl: string): void { |
There was a problem hiding this comment.
Nitpick - alphabetization of methods within a region
| return; | ||
| } | ||
| if (newCurrentObjectIndex < -1) { | ||
| console.error(`Cannot set new object index below -1. -1 should be used when nothing is to be drawn`); |
There was a problem hiding this comment.
Should these errors be shown in a production environment? What might cause data to be set in this state?
If they should only be shown (or will only be useful) in a development environment, we do have an EnvironmentUtils to check for a dev environment & run a function only in development.
There was a problem hiding this comment.
Nice to know about the EnvironmentUtils, thanks!
I think these errors should show. These indicate improper use of this library. In this example, the consumer was attempting to set the index of a stack (array of pen/line strokes) below -1 which, to this library in an illegitimate value. -1 represents that nothing in the stack is selected and therefore nothing should be drawn. [0-n] represents where in the stack the pen/line strokes should be drawn.
| return; | ||
| } | ||
|
|
||
| if (this.selectedTool) { |
There was a problem hiding this comment.
Prefer explicit non-null check over a truthy check
| return; | ||
| } | ||
| this.selectedTool = selectedTool; | ||
| if (this.selectedTool) { |
There was a problem hiding this comment.
Same here ☝️ Prefer explicit non-null check
| this._clearCanvas(this._config.sketchCanvas, this._sketchContext); | ||
| } | ||
|
|
||
| private _clearBackgroundImageCanvas(): void { |
There was a problem hiding this comment.
Nitpick - alphabetization of methods within a region
| @@ -0,0 +1,397 @@ | |||
| import { ImageSettings, ImageCanvasTool } from "./tools/image-canvas-tool"; | |||
There was a problem hiding this comment.
Overall, I would recommend adding some JSDoc commentary around some of these methods and how they interact.
| } | ||
|
|
||
| export interface ReactCanvasSketchProps { | ||
| backgroundImageUrl: string; |
There was a problem hiding this comment.
Would recommend adding some JSDoc commentary around these props (same goes for the component above)
| // set up effects | ||
| useEffect(() => { | ||
| console.log("useEffect: initializing"); | ||
| if (!isInitialized) { |
There was a problem hiding this comment.
Would recommend short circuiting if isInitialized is true
| return; | ||
| } | ||
| canvasSketch.redrawSketchAt(props.value.objects, props.value.currentObjectIndex); | ||
| }, [props.redrawIncrement, canvasSketch, props.value.objects, props.value.currentObjectIndex]); |
There was a problem hiding this comment.
Not really sure what this useEffect is accomplishing vs. the one right above it - might be some intricacies in the dependency list that I'm missing, but could use some additional explanation
There was a problem hiding this comment.
This effect hook is triggered anytime the renderer of <ReactCanvasSketch/> passes in a different redrawIncrement integer where as the other effect hook doesn't have a dependency on redrawIncrement. This is a way I've allowed high level components control their rendered subordinates by way of props instead of tracking refs which I avoid as much as possible.
| props.onAddedStroke, | ||
| props.toolColor, | ||
| props.toolWidth, | ||
| props.value.currentObjectIndex, |
There was a problem hiding this comment.
Just a note, we've experienced some weirdness with putting deeply nested properties in a dependency list for a hook. If it's working for you, great - just something that I have avoided due to some unexpected behavior.
There was a problem hiding this comment.
Honestly I wasn't planning on adding all of these dependencies but Typescript is configured to complain about it because they're all being used inside the hook. In it's current implementation, all of these dependencies are necessary for configuring the CanvasSketch object at initialization, although I suppose setSelectedTool(), setToolColor(), setToolWidth(), and setOnAddedToolStroke() are probably not needed here since they're already being done in different hooks. I'll remove them and validate that. I'm all ears if you have any suggestions besides that.
There was a problem hiding this comment.
You could probably listen for props and then destructure the properties you care about inside of the hook - or, destructure the properties you care about from outside the hook, and then add those in the dependency list instead.
Take a look at this thread: facebook/react#16265
There was a problem hiding this comment.
@brandongregoryscott That thread was helpful, thanks! I updated the code since it wasn't optimized for initialization of the canvas sketch so I got around it that way. Nice to know to destructure in advance though according to best practive.
I'll add this documentation when I get some time. I probably should have done this before passing it off. |
…to call it when redraw incrementer changes
…every time effectively clearing out the canvas
|
@brandongregoryscott At this point, what else should I change / add to get this to a point where it's approved and merged? |
|
@KevinBusch Took a look at your changes since the last time I looked - thanks for adding all the commentary and cleaning up. I think there are few missed recommended refactors (mostly around nesting/short-circuiting), but I think this is good to go for now 👍 |
|
@brandongregoryscott I think I updated all the places you commented on including the nesting/short-circuiting suggested changes. I'd be happy to update wherever I missed. Let me know. |
|
@KevinBusch A few of the missed places I'm seeing: Not a big deal, but would probably help a reader follow the logic paths. Feel free to PR it in if you have time 👍 |
|
@all-contributors add @KevinBusch for code, doc |
|
I've put up a pull request to add @KevinBusch! 🎉 |
#42
NOTE: