homebrewery
homebrewery copied to clipboard
[Editor] Prevent unfolding code in Editor when adding a Snippet
This PR resolves #2135.
After investigating Gazook89's draft PR #2391, I was unable to automate the re-folding of previously folded code blocks in the Editor. However, on comparing the handleInject
function to the shortcuts for page and column breaks, I realized that the handleInject
function was operating in a very different fashion, causing the document to effectively be regenerated, losing the folded marks.
By moving this function out of the editor.jsx
to codeEditor.jsx
and using the same method (replaceSelection
) as the shortcut keys, folded code segments are no longer unfolded when a snippet is injected.
At this stage, I have included an optional overwrite
parameter to overwrite the existing selection, but the handleInject
function is currently called with this set to false
in order to mimic the previous functionality.
I think there is potentially value in storing fold information; however I think that would be a very different problem to solve than what this PR has been about.
Folded Preview Content
While you are in the folding functions, do you want to visit this issue #1907 concerning what appears in the folded 'preview' of the page? Displaying just the first header on the page would be ideal for me. Even if the first lines are not a header I think it should find the first header-- it's a better descriptor of "what" is on the page than just a random line of text or code. And if no header exists on the page, resort to just whatever is in the first line.
Folded Preview Styling
At the same time, I think it's a good idea to rethink the styling of the folded preview text-- bold bright blue text for the preview text draws a lot of visual attention to text that likely should be taking a backseat, since it is folded. Changing it to regular weight and gray would be my recommendation.
Again, separate PR might be needed though I think the above two points could be tackled together easily.
It's either/or, unfortunately. There's a post-replacement option which is currently set to around
, which selects the newly inserted text. Changing this to end
should place the cursor after the inserted snippet.
I can push a change using that option so we can determine what the preferred behavior is.
@calculuschild I've pushed a change to the post-insert selection now, so it uses the same parameter as inserting a page or column break. It's a bit different when inserting a snippet, as it doesn't (currently) use a hot key, so the user needs to click back into the Editor anyway. Give it a try and let me know what you think!
Yep this is good. I also just added cm.focus()
at the end which allows immediately typing again without needing a click (same as previous behavior).
Merging now! 💯 🎉