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(2d): add line counter for CodeBlock #802

Merged

Conversation

SleeklyCoding
Copy link
Contributor

I think it would be useful to add a feature that allows for retrieving the current count of CodeBlock's lines.

line-count

e.g. I'm using this for my custom 'CodeEditor' component that has numbering on the left side.

line-count-code-editor

private createSideNumbers = (n: number): string => Array.from({ length: n }, (_, i) => i + 1).join('\n');
...
<Txt
     ...
      text={() => this.createSideNumbers(this.codeBlock().lineCount())}/>
...

BTW: When getting this value it has some delay. It could be probably improved in some way, but I'm not sure how.

@aarthificial
Copy link
Contributor

BTW: When getting this value it has some delay. It could be probably improved in some way, but I'm not sure how.

This happens because during tweening, a temporary structure stored in this.diffed is used instead of this.parsed();
You could account for that by doing the following:

  1. When tweenCode() is called, calculate and store the number of lines in the current source code and the new one.
  2. In lineCount, check if this.progress() != null, and if so, use the progress to interpolate between these two line counts.

@SleeklyCoding
Copy link
Contributor Author

@aarthificial I applied your suggestions and looks good in my opinion.

improved-line-count

Copy link
Contributor

@aarthificial aarthificial left a comment

Choose a reason for hiding this comment

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

Looks good! Needs just two little fixes

packages/2d/src/components/CodeBlock.ts Outdated Show resolved Hide resolved
packages/2d/src/components/CodeBlock.ts Outdated Show resolved Hide resolved
@aarthificial aarthificial merged commit c3f9676 into motion-canvas:main Sep 17, 2023
9 checks passed
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.

None yet

2 participants