Skip to content

Commit bcf6cd3

Browse files
authored
How to use templates with functions (#83)
1 parent f20619f commit bcf6cd3

32 files changed

+2389
-11
lines changed

.github/mcl_config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"aliveStatusCodes": [200, 206, 403]
3+
}

.github/workflows/action.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ jobs:
77
name: All Markdown links are valid
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@master
11-
- uses: gaurav-nelson/github-action-markdown-link-check@1.0.13
10+
- uses: actions/checkout@v4
11+
- uses: gaurav-nelson/github-action-markdown-link-check@v1
12+
with:
13+
config-file: '.github/mcl_config.json'
1214

1315
check-snippet-compilation:
1416
name: All code snippets compile
1517
runs-on: ubuntu-latest
16-
1718
steps:
18-
- uses: actions/checkout@v2
19-
19+
- uses: actions/checkout@v4
2020
- name: Check that all snippets compile
2121
run: |
2222
pip install pipenv

animation/templates/package-lock.json renamed to animation/templates_how_functions/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {
2+
CodeModification,
3+
} from '@motion-canvas/2d/lib/components/CodeBlock';
4+
5+
const store = (...args: [TemplateStringsArray, ...any]) => args;
6+
7+
function to_string([strings, ...values]: [TemplateStringsArray, ...any]): string {
8+
return strings.reduce((acc, str, i) => {
9+
const value = values[i] ?? '';
10+
return acc + str + value;
11+
}, '');
12+
}
13+
14+
function append(
15+
template_1: TemplateStringsArray, args_1: string[],
16+
template_2: TemplateStringsArray, args_2: string[]): [TemplateStringsArray, ...string[]] {
17+
let new_template = Array.from<string>(template_1);
18+
let new_args: Array<string> = args_1;
19+
new_template[template_1.length - 1] += template_2[0]
20+
new_template = new_template.concat(template_2.slice(1))
21+
new_args = new_args.concat(args_2)
22+
return [new_template as unknown as TemplateStringsArray, ...new_args];
23+
}
24+
25+
const isCodeModification = (code: any): code is CodeModification => (code as CodeModification).from !== undefined;
26+
27+
function simplify(
28+
args_in: [TemplateStringsArray, ...any]): [TemplateStringsArray, ...string[]] {
29+
const template = args_in[0]
30+
const args = args_in.slice(1)
31+
if (args.every((arg) => (typeof (arg) == "string") || isCodeModification(arg))) {
32+
return args_in
33+
}
34+
let new_template: Array<any> = [];
35+
let new_args: Array<any> = [];
36+
let concatenate = false;
37+
for (let i = 0; i <= args.length; i++) {
38+
if (concatenate) {
39+
concatenate = false;
40+
new_template[new_template.length - 1] += template[i];
41+
} else {
42+
new_template.push(template[i]);
43+
}
44+
if (i == args.length) { break; }
45+
if (typeof (args[i]) == "string" || isCodeModification(args[i])) {
46+
new_args.push(args[i])
47+
continue;
48+
}
49+
let simplified_arg = simplify(args[i]);
50+
let simplified_template = simplified_arg[0]
51+
let simplified_args = simplified_arg.slice(1)
52+
const appended = append(
53+
new_template as unknown as TemplateStringsArray,
54+
new_args,
55+
simplified_template,
56+
simplified_args as string[]);
57+
new_template = Array.from<string>(appended[0])
58+
new_args = appended.slice(1)
59+
concatenate = true;
60+
}
61+
return [new_template as unknown as TemplateStringsArray, ...new_args]
62+
}
63+
64+
export {store, simplify, to_string};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {makeProject} from '@motion-canvas/core';
2+
3+
import template_print from './scenes/template_print?scene';
4+
import three_types from './scenes/three_types?scene';
5+
import mixing_params from './scenes/mixing_params?scene';
6+
7+
import './global.css'; // <- import the css
8+
9+
export default makeProject({
10+
scenes: [template_print],
11+
});

0 commit comments

Comments
 (0)