sandpack icon indicating copy to clipboard operation
sandpack copied to clipboard

feat: Add `initialCursor` to code editor when setting the active file

Open mattphillips opened this issue 3 years ago • 2 comments

What kind of change does this pull request introduce?

This PR closes https://github.com/codesandbox/sandpack/issues/558

What is the current behavior?

Currently sandpack has a setActiveFile function that can only open the file without putting the cursor at a given position.

What is the new behavior?

This PR adds an optional cursor (line / column numbers) to the setActiveFile function. If a cursor position is set as part of this function then the CodeMirror state has a selection at the given cursor and will scroll it into view and focus the editor.

This PR also introduces a change to SandpackTests. I've rewritten the FormattedErrors component to use JSX instead of relying of string manipulation and dangerously rendering the string. The reason to do this is so that we can safely attach an onClick to the failing matcher error which will open the test file at the correct position to fix the matcher.

What steps did you take to test this? This is required before we can merge, make sure to test the flow you've updated.

See attached demo of the cursor change in action using SandpackTests errors.

https://user-images.githubusercontent.com/5610087/188724977-099ca5d5-b5cc-43a0-b45d-9cd9420a6968.mov

Checklist

  • [ ] Documentation;
  • [x] Storybook (if applicable);
  • [ ] Tests;
  • [ ] Ready to be merged;

Todo

  • I'm going to hold off updating the docs until @danilowoz has had a chance to look this over and he's happy with everything I'll get the docs updated too.

mattphillips avatar Sep 06 '22 19:09 mattphillips

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

codesandbox[bot] avatar Sep 06 '22 19:09 codesandbox[bot]

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 29f6105be94afcf27c4b363a49afd43e9620947b:

Sandbox Source
Sandpack Configuration

codesandbox-ci[bot] avatar Sep 06 '22 19:09 codesandbox-ci[bot]

Hey @danilowoz 👋 have you had a chance to look at this? Anything you need me to change or do?

mattphillips avatar Sep 29 '22 10:09 mattphillips

Hey, I'm going to close this, as it has been a while. Feel free to reopen it anytime Thanks!

danilowoz avatar Apr 06 '23 21:04 danilowoz