Skip to content

Feature/new react sketch pad component#45

Merged
brandongregoryscott merged 16 commits into
rsm-hcd:masterfrom
KevinBusch:feature/new-react-sketch-pad-component
Aug 31, 2020
Merged

Feature/new react sketch pad component#45
brandongregoryscott merged 16 commits into
rsm-hcd:masterfrom
KevinBusch:feature/new-react-sketch-pad-component

Conversation

@KevinBusch
Copy link
Copy Markdown
Collaborator

#42
NOTE:

  • Related GitHub issue(s) linked in PR description
  • Destination branch merged, built and tested with your changes
  • Code formatted and follows best practices and patterns
  • Code builds cleanly (no additional warnings or errors)
  • Manually tested
  • Automated tests are passing
  • [-] No decreases in automated test coverage (will back fill tests after review)
  • [-] Documentation updated (readme, docs, comments, etc.)
  • Localization: No hard-coded error messages in code files (minimally in string constants)
  • New component and/ or properties? Make sure to update storybook

@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 24, 2020

Codecov Report

Merging #45 into master will decrease coverage by 22.79%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             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               
Impacted Files Coverage Δ
src/atoms/forms/canvas-sketch/canvas-sketch.ts 0.00% <0.00%> (ø)
.../atoms/forms/canvas-sketch/react-canvas-sketch.tsx 0.00% <0.00%> (ø)
...forms/canvas-sketch/tools/base-canvas-draw-tool.ts 0.00% <0.00%> (ø)
...toms/forms/canvas-sketch/tools/base-canvas-tool.ts 0.00% <0.00%> (ø)
...oms/forms/canvas-sketch/tools/image-canvas-tool.ts 0.00% <0.00%> (ø)
...forms/canvas-sketch/tools/line-canvas-draw-tool.ts 0.00% <0.00%> (ø)
...atoms/forms/canvas-sketch/tools/pan-canvas-tool.ts 0.00% <0.00%> (ø)
...rms/canvas-sketch/tools/pencil-canvas-draw-tool.ts 0.00% <0.00%> (ø)
.../atoms/forms/canvas-sketch/utils/position-utils.ts 0.00% <0.00%> (ø)
src/utilities/core-utils.ts 0.00% <0.00%> (ø)
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1c9c686...c02142e. Read the comment docs.

Copy link
Copy Markdown
Contributor

@brandongregoryscott brandongregoryscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer explicit non-null check over a truthy check

return;
}
this.selectedTool = selectedTool;
if (this.selectedTool) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here ☝️ Prefer explicit non-null check

this._clearCanvas(this._config.sketchCanvas, this._sketchContext);
}

private _clearBackgroundImageCanvas(): void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick - alphabetization of methods within a region

@@ -0,0 +1,397 @@
import { ImageSettings, ImageCanvasTool } from "./tools/image-canvas-tool";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I would recommend adding some JSDoc commentary around some of these methods and how they interact.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Will do

}

export interface ReactCanvasSketchProps {
backgroundImageUrl: string;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@KevinBusch
Copy link
Copy Markdown
Collaborator Author

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.

canvas-sketch.tsx isn't a component at all. It's actually canvas-sketch.ts and it's really a separate javascript library which is responsible for tracking to canvas elements (1 for a background image and 1 for drawing) provided to it, binding the selected tool's interactions with the drawing/sketch canvas, and bubbling drawing interactions back to the consumer.

react-canvas-sketch.tsx is responsible for rendering the two canvases and instantiating/maintining an instance of CanvasSketch using those two canvases within it's own state. The hooks are meant to trigger the various public API methods of CanvasSketch to do things like switch color, width, selected tool, etc...

I'll add this documentation when I get some time. I probably should have done this before passing it off.

@KevinBusch
Copy link
Copy Markdown
Collaborator Author

@brandongregoryscott At this point, what else should I change / add to get this to a point where it's approved and merged?

@brandongregoryscott
Copy link
Copy Markdown
Contributor

@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 brandongregoryscott merged commit fff7511 into rsm-hcd:master Aug 31, 2020
@KevinBusch
Copy link
Copy Markdown
Collaborator Author

@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.

@brandongregoryscott
Copy link
Copy Markdown
Contributor

@all-contributors add @KevinBusch for code, doc

@allcontributors
Copy link
Copy Markdown
Contributor

@brandongregoryscott

I've put up a pull request to add @KevinBusch! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants