-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
Release EnvironmentsThis Environment is provided by Release, learn more! 🔧Environment Status : https://app.release.com/public/Processing%20Foundation/env-861652405e |
There was a problem hiding this 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.
client/utils/previewEntry.js
Outdated
@@ -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]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
useEffect(() => { | ||
const unsubscribe = listen(handleMessageEvent); | ||
return function cleanup() { | ||
unsubscribe(); | ||
}; | ||
}); | ||
listen(handleMessageEvent); | ||
}, [handleMessageEvent]); | ||
|
There was a problem hiding this comment.
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.
dispatchConsoleEvent={(event) => | ||
dispatch(ConsoleActions.dispatchConsoleEvent(event)) | ||
} |
There was a problem hiding this comment.
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
.
return () => { | ||
if (cmInstance.current) { | ||
cmInstance.current = null; | ||
} | ||
}; |
There was a problem hiding this comment.
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.
cmInstance.current.on('keydown', handleEnterKey); | ||
} | ||
|
||
return () => { | ||
if (cmInstance.current) { | ||
cmInstance.current.off('keydown', handleEnterKey); | ||
} | ||
}; | ||
}, [commandHistory, dispatchConsoleEvent]); |
There was a problem hiding this comment.
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.
cm.setValue(commandHistory[newCursor] || ''); | ||
const cursorPos = cm.getDoc().getLine(0).length - 1; | ||
cm.getDoc().setCursor({ line: 0, ch: cursorPos }); |
There was a problem hiding this comment.
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]); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ''; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
ConsoleInput.propTypes = { | ||
theme: PropTypes.string.isRequired, | ||
dispatchConsoleEvent: PropTypes.func.isRequired, | ||
fontSize: PropTypes.number.isRequired | ||
}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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`; |
There was a problem hiding this comment.
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 div
s 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried it out!
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! |
}); | ||
|
||
dispatchConsoleEvent(consoleEvent); |
There was a problem hiding this comment.
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. (:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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! 🎉
Related Issues:
Console.jsx
)Changes:
consoleStyles.js
ConsoleInput.jsx
into individual ones to handle keyboard inputs for Enter, ArrowUp, and ArrowDownpreviewEntry.js
to prevent "_report is undefined" warningsI have verified that this pull request:
npm run lint
)npm run test
)develop
branch.Fixes #123