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

How to reuse existing template literay when updating code in 'do action' #35

Closed
jazimabbas opened this issue Aug 8, 2024 · 7 comments
Closed

Comments

@jazimabbas
Copy link

Is there any way to pass the dynamic expression e.g.

<Action 
  do={() => {
     const newCode = ``;
     code.update`async function animate() {
       // New code here ...
       ${newCode}
     }`;
  }}
/>

I don't wanna repeat the same code again and again. I want to reuse the existing code because I have many lines of code. So how can we achieve this ? Any thoughts on this ?

@mattcroat
Copy link
Collaborator

mattcroat commented Aug 11, 2024

I thought about it but turns out it's more complicated because tagged templates accept strings and expressions as arguments:

function update(strings, ...expressions) {
  // ...
}

So we could just concatenate everything but in this example where you have expressions inside:

async function animate() {
  ${newCode}
 }

The output would be this because expressions are added after the strings:

async function animate() {

 }
 
 // new code...

You could use a function but I would have to think about it more.

@mattcroat
Copy link
Collaborator

The strings and expressions array seem to match so we could merge them together. 🤔

@mattcroat
Copy link
Collaborator

Yeah it seems to work but the formatting needs work.

@jazimabbas
Copy link
Author

I think we should make it as a normal function and call it instead passing template strings. If we do so, user has to do code formatting by himself.

@mattcroat
Copy link
Collaborator

mattcroat commented Aug 14, 2024

This is what I meant by using a function:

<script>
  const template = () => `
    async function animate() {
      ${expression}
    }
  `
</script>

I prefer the tagged template experience and you only have to check for expressions and merge them plus the formatting should be respected by the autoIndent option:

template-expression.mp4

I'm working on other improvements so I can add them together and update the docs.

@jazimabbas
Copy link
Author

Sounds good. Looking forward to it. BTW, thanks for quick reply.

@mattcroat
Copy link
Collaborator

mattcroat commented Aug 17, 2024

Closed 27982ad.

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

No branches or pull requests

2 participants