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

feat: add files via file tree #314

Merged
merged 16 commits into from
Sep 13, 2024

Conversation

AriPerkkio
Copy link
Member

@AriPerkkio AriPerkkio commented Sep 4, 2024

BREAKING CHANGES

  • Projects that are using @tutorialkit/react package directly, without TutorialKit need to update <FileTree> component's props.files type

Description

Adds new metadata option that can be used to enable file and folder creation in the file tree. By default this feature is disabled.

---
type: lesson
editor:
  fileTree:
    allowEdits: true
---
tk-filetree-example.webm

Copy link

stackblitz bot commented Sep 4, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@AriPerkkio
Copy link
Member Author

Early draft PR, mostly so that 81eac67 can be used if #208 is started before this is finalized.

@AriPerkkio AriPerkkio force-pushed the feat/add-files-via-filetree branch from 88e4815 to a3944c3 Compare September 9, 2024 16:09
@AriPerkkio AriPerkkio force-pushed the feat/add-files-via-filetree branch 2 times, most recently from 187d285 to 589629a Compare September 10, 2024 15:07
@AriPerkkio AriPerkkio force-pushed the feat/add-files-via-filetree branch from abebc6f to 380126f Compare September 11, 2024 06:20
@AriPerkkio AriPerkkio force-pushed the feat/add-files-via-filetree branch 2 times, most recently from 2a4a839 to a9219d0 Compare September 11, 2024 07:19

interface FileChangeEvent {
type: 'FILE' | 'FOLDER';
method: 'ADD' | 'REMOVE' | 'RENAME';
Copy link
Member Author

Choose a reason for hiding this comment

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

Added the REMOVE and RENAME here even though they are not yet implemented. This kind of API allows us to extend the functionality later without causing breaking changes.

@AriPerkkio AriPerkkio force-pushed the feat/add-files-via-filetree branch from a9219d0 to 2fe5950 Compare September 11, 2024 07:27
@AriPerkkio AriPerkkio marked this pull request as ready for review September 11, 2024 07:27
@AriPerkkio AriPerkkio requested a review from Nemikolh September 11, 2024 07:27
Copy link
Member

@Nemikolh Nemikolh left a comment

Choose a reason for hiding this comment

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

Did a first pass, good work on this!! 💪


// or configure file tree with options
z.strictObject({
allowEdits: z
Copy link
Member

Choose a reason for hiding this comment

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

I think we want something more fine grained where you specify in which scope you want to allow files and folder to be added.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good! Though this is something that could be implemented after this first PR without breaking the API too, so that true would mean "allow everything".

But I can also take a look at this once I've addressed other review findings. 👍

packages/runtime/src/store/index.ts Outdated Show resolved Hide resolved
packages/runtime/src/store/index.ts Show resolved Hide resolved
packages/runtime/src/store/editor.ts Outdated Show resolved Hide resolved
packages/react/src/core/FileTree.tsx Outdated Show resolved Hide resolved
packages/react/src/core/FileTree.tsx Show resolved Hide resolved
packages/react/src/core/FileTree.tsx Outdated Show resolved Hide resolved
packages/react/src/core/FileTree.tsx Outdated Show resolved Hide resolved
packages/react/src/core/ContextMenu.tsx Outdated Show resolved Hide resolved
@AriPerkkio
Copy link
Member Author

@Nemikolh ready for review round 2! 🔔

Added support for identifying files and folders from each other - we no longer require files to have extensions.

However there's still an edge case that causes issues. As we are using filepath as map key here, there's no way for us to have file and folder with same name in the tree.

export type EditorDocuments = Record<string, EditorDocument>;

So for example this will fail:

test('empty directories are removed when new content is added', () => {
  const store = new EditorStore();
  store.setDocuments({
    example: 'should not be confused as directory',
  });

  store.addFileOrFolder({ path: 'example', type: 'FOLDER' });
  store.addFileOrFolder({ path: 'example/another', type: 'FOLDER' });

  expect(store.files.get().map(toFilename)).toMatchInlineSnapshot(`
    [
      "example/another",
      "example",
    ]
  `);
});

Is this really an issue that users could run into? Maybe not.

@Nemikolh
Copy link
Member

@AriPerkkio I'm not up to date with every file systems that exists out there, but AFAIK, in most of them you cannot have a file and a folder named the same. If you think about it, that make sense with the Linux open function and its equivalent in node.

In our case, what matter is what the WebContainer fs does and it does not allow multiple files given a single path. So this is completely fine 👍

@AriPerkkio
Copy link
Member Author

Oh that's right! It does make sense, when you think from the file system's perspective! I guess I was looking at this too much from FileTree's visual representation and didn't realize it's not possible on fs. 💯

Copy link
Member

@Nemikolh Nemikolh left a comment

Choose a reason for hiding this comment

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

Second pass done! I hope I didn't miss anything. I'll probably do another pass tomorrow 🙏

packages/react/src/core/FileTree.tsx Outdated Show resolved Hide resolved
packages/types/src/entities/index.ts Outdated Show resolved Hide resolved
packages/runtime/src/store/index.ts Outdated Show resolved Hide resolved
packages/runtime/src/store/editor.ts Outdated Show resolved Hide resolved
packages/runtime/src/store/editor.ts Outdated Show resolved Hide resolved
packages/runtime/src/store/editor.spec.ts Outdated Show resolved Hide resolved
packages/react/src/core/ContextMenu.tsx Outdated Show resolved Hide resolved
position?: 'before' | 'after';

/** Localized texts for menu. */
i18n?: Pick<I18n, 'fileTreeCreateFileText' | 'fileTreeCreateFolderText'>;
Copy link
Member

Choose a reason for hiding this comment

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

Oh this is a cool way to only require what we use, I wonder if we should use Pick for i18n in other places. I suppose this can be done later. Curious to hear your thoughts on this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep we should! I saw that there's quite much duplication in couple of places. When we can derive props from their orignal source, we should always do it.

Copy link
Member

@Nemikolh Nemikolh left a comment

Choose a reason for hiding this comment

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

Incredible job on this @AriPerkkio! 🥳 🥳

I'm starting to think we could merge this PR first, and then change the API in a follow-up PR to be:

type GlobPattern = string;

interface P {
  allowEdits?: boolean | GlobPattern | GlobPattern[]
}

That way it wouldn't be a breaking change, we would still support the boolean version with the default value being false.

I got a feeling that some will still want to use true anyway.

What I think we should do in the follow-up PR is to clarify in the docs that only allowing certain patterns (or even a single path) should be preferred.

I would even reference the talk from Rich Harris: FullStack Documentation as a suggestion to why this should be preferred.

@Nemikolh Nemikolh merged commit 7782bdc into stackblitz:main Sep 13, 2024
10 checks passed
@AriPerkkio AriPerkkio deleted the feat/add-files-via-filetree branch September 13, 2024 09:53
@AriPerkkio
Copy link
Member Author

I agree on all points! I'll continue on that next.

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

Successfully merging this pull request may close these issues.

Allow the user to add files
2 participants