Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Console Components #3276

Merged
merged 11 commits into from
Nov 18, 2024
Merged

Update Console Components #3276

merged 11 commits into from
Nov 18, 2024

Conversation

raclim
Copy link
Collaborator

@raclim raclim commented Nov 13, 2024

Related Issues:

Changes:

  • moves the styling for the Console into a separate utility file, consoleStyles.js
  • separates hook in ConsoleInput.jsx into individual ones to handle keyboard inputs for Enter, ArrowUp, and ArrowDown
  • removes named import in previewEntry.js to prevent "_report is undefined" warnings

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #123

Copy link

release-com bot commented Nov 13, 2024

Release Environments

This Environment is provided by Release, learn more!
To see the status of the Environment click on Environment Status below.

🔧Environment Status : https://app.release.com/public/Processing%20Foundation/env-861652405e

Copy link
Collaborator

@lindapaiste lindapaiste left a comment

Choose a reason for hiding this comment

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

previewEntry.js needs to be fixed.

ConsoleInput.jsx looks pretty solid but could be cleaner. I'll see if I can write a PR with what I have in mind.

@@ -175,6 +176,6 @@ if (_report) {
paths.forEach((path) => {
newMessage = newMessage.replaceAll(path, window.objectPaths[path]);
});
_report.apply(window.p5, [newMessage, method, color]);
p5Call.apply(window.p5, [newMessage, method, color]);
Copy link
Collaborator

@lindapaiste lindapaiste Nov 13, 2024

Choose a reason for hiding this comment

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

Previously the apply was on window.p5._report (a function). Now it is on window.p5 (an object). So that's a mistake. I would keep a _report variable but define it inside the if (p5Call) branch.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Circling back to this, perhaps the only change that we need in this file is from const { _report } = window.p5; to const _report = window.p5?._report;.

However I do have questions as to why window.p5 is undefined in the first place. I'll leave some more info on the issue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for looking further into this!! I actually didn't realize there were so many issues about this already 😂

I think for now, only changing window.p5 to window.p5?.report makes sense to me.

I'll change this PR as progress towards the issue, since it seems like we need to change where previewScripts is loading, but I'm not sure where the best placement might be for it yet.

Comment on lines 40 to 43
useEffect(() => {
const unsubscribe = listen(handleMessageEvent);
return function cleanup() {
unsubscribe();
};
});
listen(handleMessageEvent);
}, [handleMessageEvent]);

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would keep the unsubscribe on cleanup behavior.

Comment on lines 97 to 99
dispatchConsoleEvent={(event) =>
dispatch(ConsoleActions.dispatchConsoleEvent(event))
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO it's better to do the dispatching directly from ConsoleInput.

Comment on lines 29 to 33
return () => {
if (cmInstance.current) {
cmInstance.current = null;
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably don't need a cleanup here if all that it's doing is clearing a local variable. If there's a destroy or cleanup function on the CM instance we would want to call that though.

Comment on lines 73 to 81
cmInstance.current.on('keydown', handleEnterKey);
}

return () => {
if (cmInstance.current) {
cmInstance.current.off('keydown', handleEnterKey);
}
};
}, [commandHistory, dispatchConsoleEvent]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

The off and on is good. That will prevent us having multiple event handlers.

Having dispatchConsoleEvent as a dependency means that this effect will re-run on every render of Console because the function is declared inline and is not memoized. You could memoize it (with useCallback) but I would instead handle the dispatching here instead of passing a function prop.

Comment on lines +93 to +95
cm.setValue(commandHistory[newCursor] || '');
const cursorPos = cm.getDoc().getLine(0).length - 1;
cm.getDoc().setCursor({ line: 0, ch: cursorPos });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good change ✔️

cmInstance.current.getWrapperElement().style['font-size'] = `${fontSize}px`;
cmInstance.current.refresh();
}, [theme, fontSize]);
}, [commandCursor, commandHistory]);
Copy link
Collaborator

@lindapaiste lindapaiste Nov 13, 2024

Choose a reason for hiding this comment

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

We def need these as dependencies if we want to use those variables, but this does mean that we are changing the handler very frequently. The goal with having both values in the same state is that you can access both values from within a prevState callback. So the the effect doesn't need to re-run on every change of the variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I think this was what I started to become unsure about as I was working on it 😅

style
);
default:
return '';
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is supposed to return an object, right? So return ''; doesn't make sense.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes!! I'm thinking of setting the default to the light theme instead?

Comment on lines 154 to 157
ConsoleInput.propTypes = {
theme: PropTypes.string.isRequired,
dispatchConsoleEvent: PropTypes.func.isRequired,
fontSize: PropTypes.number.isRequired
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

theme and fontSize are both selected from Redux. so those can be selected inside the ConsoleInput rather than selected by the parent and passed down.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Although now I see that the parent still needs to select them regardless because it uses them for the ConsoleFeed style.

Comment on lines 28 to 36
cmInstance.current.getWrapperElement().style['font-size'] = `${fontSize}px`;
}, []);

useEffect(() => {
if (cmInstance.current) {
cmInstance.current.setOption('theme', `p5-${theme}`);
cmInstance.current.getWrapperElement().style[
'font-size'
] = `${fontSize}px`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can add a style property to one of the divs that we control instead of manipulating the element.

<div ref={codemirrorContainer} className="console__editor" style={{ fontSize: `${fontSize}px` }} />

Where it is being set currently is a direct child of that div but it has the same effect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just tried it out!

@raclim
Copy link
Collaborator Author

raclim commented Nov 18, 2024

I'm going to merge this in since I noted in the original issue that I would get something out for it soon, but am definitely open to following up with further changes here!

@raclim raclim merged commit 7104c7f into develop Nov 18, 2024
4 checks passed
});

dispatchConsoleEvent(consoleEvent);

Choose a reason for hiding this comment

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

Wouldn't you need to wrap this in dispatch = useDispatch(), dispatch(...), like the reducers used in Console.jsx? Asking since, currently, while Up arrow does work, the console no longer echoes the inputted commands. (:

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes!! Thank you so much for catching that 😂 I thought I had it in at some point! I'll try to have the update out for this at some point early next week.

Choose a reason for hiding this comment

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

The update works, thanks a ton! 🎉

@raclim raclim deleted the update-console branch November 27, 2024 21:53
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.

Console history not accessible with up arrow
3 participants