animotionjs / animotion

🪄 Create beautiful presentations with Svelte
https://animotion.pages.dev
MIT License
1.3k stars 38 forks source link

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

Closed jazimabbas closed 1 month ago

jazimabbas commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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

mattcroat commented 1 month ago

Yeah it seems to work but the formatting needs work.

jazimabbas commented 1 month ago

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 commented 1 month ago

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:

https://github.com/user-attachments/assets/638c536a-d822-4fed-8ab1-866106e1a31c

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

jazimabbas commented 1 month ago

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

mattcroat commented 1 month ago

Closed https://github.com/animotionjs/animotion/commit/27982ad42e107a6d84083b93e79fdc4a9e1a4ea5.