Visualize Your Ideas With Code
refactor: bootstrap function
aarthificiala3e3e1426483
diff --git a/package-lock.json b/package-lock.jsonindex 43ebd5ec..18549133 100644--- a/package-lock.json+++ b/package-lock.json@@ -30,9 +30,9 @@"mc-serve": "tools/serve.mjs"},"devDependencies": {- "@types/dom-webcodecs": "^0.1.4","@types/node-sass": "^4.11.2","@types/three": "^0.138.0",+ "@types/webpack-env": "^1.17.0","css-loader": "^6.7.1","node-loader": "^2.0.0","node-sass": "^7.0.1",@@ -262,12 +262,6 @@"@types/node": "*"}},- "node_modules/@types/dom-webcodecs": {- "version": "0.1.4",- "resolved": "https://registry.npmjs.org/@types/dom-webcodecs/-/dom-webcodecs-0.1.4.tgz",- "integrity": "sha512-dc+xSUnCaCdi/hExZArnLhiavS3E1Rdpp2+zCI6TcmJvz4qgDPBbpvCM7DsQhwRXIIpVMHO6c3s+t+JyCSqYBA==",- "dev": true- },"node_modules/@types/eslint": {"version": "8.4.1","resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.4.1.tgz",@@ -400,6 +394,12 @@"integrity": "sha512-D8AoV7h2kbCfrv/DcebHOFh1WDwyus3HdooBkAwcBikXArdqnsQ38PQ85JCunnvun160oA9jz53GszF3zch3tg==","dev": true},+ "node_modules/@types/webpack-env": {+ "version": "1.17.0",+ "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.17.0.tgz",+ "integrity": "sha512-eHSaNYEyxRA5IAG0Ym/yCyf86niZUIF/TpWKofQI/CVfh5HsMEUyfE2kwFxha4ow0s5g0LfISQxpDKjbRDrizw==",+ "dev": true+ },"node_modules/@types/wicg-file-system-access": {"version": "2020.9.5","resolved": "https://registry.npmjs.org/@types/wicg-file-system-access/-/wicg-file-system-access-2020.9.5.tgz",@@ -6543,12 +6543,6 @@"@types/node": "*"}},- "@types/dom-webcodecs": {- "version": "0.1.4",- "resolved": "https://registry.npmjs.org/@types/dom-webcodecs/-/dom-webcodecs-0.1.4.tgz",- "integrity": "sha512-dc+xSUnCaCdi/hExZArnLhiavS3E1Rdpp2+zCI6TcmJvz4qgDPBbpvCM7DsQhwRXIIpVMHO6c3s+t+JyCSqYBA==",- "dev": true- },"@types/eslint": {"version": "8.4.1","resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.4.1.tgz",@@ -6681,6 +6675,12 @@"integrity": "sha512-D8AoV7h2kbCfrv/DcebHOFh1WDwyus3HdooBkAwcBikXArdqnsQ38PQ85JCunnvun160oA9jz53GszF3zch3tg==","dev": true},+ "@types/webpack-env": {+ "version": "1.17.0",+ "resolved": "https://registry.npmjs.org/@types/webpack-env/-/webpack-env-1.17.0.tgz",+ "integrity": "sha512-eHSaNYEyxRA5IAG0Ym/yCyf86niZUIF/TpWKofQI/CVfh5HsMEUyfE2kwFxha4ow0s5g0LfISQxpDKjbRDrizw==",+ "dev": true+ },"@types/wicg-file-system-access": {"version": "2020.9.5","resolved": "https://registry.npmjs.org/@types/wicg-file-system-access/-/wicg-file-system-access-2020.9.5.tgz",diff --git a/package.json b/package.jsonindex 37023286..9ee500e4 100644--- a/package.json+++ b/package.json@@ -39,6 +39,7 @@"devDependencies": {"@types/node-sass": "^4.11.2","@types/three": "^0.138.0",+ "@types/webpack-env": "^1.17.0","css-loader": "^6.7.1","node-loader": "^2.0.0","node-sass": "^7.0.1",diff --git a/src/Project.ts b/src/Project.tsindex eeac1735..df2c3022 100644--- a/src/Project.ts+++ b/src/Project.ts@@ -8,17 +8,17 @@ import {Konva} from 'konva/lib/Global';import {Thread, ThreadsCallback} from './threading';import {Scene, SceneRunner} from './Scene';import {SimpleEventDispatcher} from 'strongly-typed-events';-import {Origin} from './types';+import {Origin, Size} from './types';Konva.autoDrawEnabled = false;-export enum ProjectSize {- FullHD,+export const ProjectSize = {+ FullHD: {width: 1920, height: 1080},}-const Sizes: Record<ProjectSize, [number, number]> = {- [ProjectSize.FullHD]: [1920, 1080],-};+interface ProjectConfig extends Partial<StageConfig> {+ scenes: SceneRunner[],+}export class Project extends Stage {public get ScenesChanged() {@@ -51,21 +51,19 @@ export class Project extends Stage {private currentThread: Thread = null;public constructor(- scenes: SceneRunner[],- size: ProjectSize = ProjectSize.FullHD,- config: Partial<StageConfig> = {},+ config: ProjectConfig,) {+ const {scenes, ...rest} = config;super({- width: Sizes[size][0],- height: Sizes[size][1],listening: false,- container: null,- ...config,+ container: 'konva',+ ...ProjectSize.FullHD,+ ...rest,});this.center = {- x: Sizes[size][0] / 2,- y: Sizes[size][1] / 2,+ x: this.width() / 2,+ y: this.height() / 2,};this.background = new Rect({diff --git a/src/bootstrap.ts b/src/bootstrap.tsnew file mode 100644index 00000000..4d1f537b--- /dev/null+++ b/src/bootstrap.ts@@ -0,0 +1,29 @@+import type {Size, Waveform} from './types';+import type {SceneRunner} from './Scene';+import {Project, ProjectSize} from './Project';+import {Player} from './player/Player';+import {hot} from './hot';++interface BootstrapConfig {+ name: string;+ size?: Size;+ scenes: SceneRunner[];+ audio?: {+ meta: Waveform;+ src: string;+ };+}++export function bootstrap(config: BootstrapConfig) {+ const project = new Project({+ name: config.name,+ scenes: config.scenes,+ ...(config.size ?? ProjectSize.FullHD),+ });+ const player = new Player(project, config.audio);+ (<any>window).player = player;++ //@ts-ignore+ const parent = require.cache[module.parents[0]];+ hot(player, parent);+}diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsxindex c2e8abb9..16a50f92 100644--- a/src/components/ColorPicker.tsx+++ b/src/components/ColorPicker.tsx@@ -21,7 +21,7 @@ const colorRangeConfig: RangeConfig = {height: 60,range: [0, 255],precision: 0,- margin: 10,+ margin: [10, 40],value: 0,};@@ -58,7 +58,7 @@ export class ColorPicker extends Surface {ref={[this, 'r']}{...colorRangeConfig}label={'R:'}- margin={[40, 10, 10]}+ margin={[40, 40, 10]}/><Range ref={[this, 'g']} {...colorRangeConfig} label={'G:'} /><Range ref={[this, 'b']} {...colorRangeConfig} label={'B:'} />@@ -66,7 +66,7 @@ export class ColorPicker extends Surface {ref={[this, 'a']}{...colorRangeConfig}label={'A:'}- margin={[10, 10, 40]}+ margin={[10, 40, 40]}/></LinearLayout>,);diff --git a/src/components/Range.ts b/src/components/Range.tsindex 75c20ee4..c9fb6f69 100644--- a/src/components/Range.ts+++ b/src/components/Range.ts@@ -93,4 +93,17 @@ export class Range extends Shape {ctx.fillText(label, position.x - 60, 10);}}++ public _hitFunc(context: Context) {+ const size = this.getSize();+ const position = {+ x: size.width / -2,+ y: size.height / -2,+ };++ context.beginPath();+ context.rect(position.x, position.y, size.width, size.height);+ context.closePath();+ context.fillShape(this);+ }}diff --git a/src/hot.ts b/src/hot.tsindex 79ee784c..2cc6bfaa 100644--- a/src/hot.ts+++ b/src/hot.ts@@ -1,10 +1,9 @@import {Player} from './player/Player';-export function hot(player: Player, root: any) {+export function hot(player: Player, root: NodeModule) {const update = async (modules: string[]) => {const runners = [];for (const module of modules) {- // @ts-ignoreconst runner = __webpack_require__(module).default;if (// FIXME Find out a better way to detect runner factories.@@ -20,15 +19,33 @@ export function hot(player: Player, root: any) {player.project.reload(runners);player.reload();};+ const updateAudio = async (modules: string[]) => {+ let src = null;+ let meta = null;+ for (const module of modules) {+ const audio = __webpack_require__(module);+ if (typeof audio === 'object') {+ meta = audio.default;+ } else {+ src = audio;+ }+ }++ if (src && meta) {+ player.reloadAudio(src, meta);+ }+ };++ const scenePaths = __webpack_require__.c[root.id].children.filter(+ (name: string) => name.match(/\.scene\.[jt]sx?/),+ );- //@ts-ignore- const scenePaths = require.cache[root.id].children.filter(name =>- //@ts-ignore- name.match(/\.scene\.[jt]sx?/),+ const audioPaths = __webpack_require__.c[root.id].children.filter(+ (name: string) => name.match(/\.wav/),);- //@ts-ignoreroot.hot.accept(scenePaths, update);- //@ts-ignoremodule.hot.accept(scenePaths, update);+ root.hot.accept(audioPaths, updateAudio);+ module.hot.accept(audioPaths, updateAudio);}diff --git a/src/player/Player.ts b/src/player/Player.tsindex ab162e21..05c086ff 100644--- a/src/player/Player.ts+++ b/src/player/Player.ts@@ -143,7 +143,6 @@ export class Player {src: string;meta: Waveform;},- public readonly labels?: Record<string, number>,) {this.startTime = performance.now();@@ -172,6 +171,13 @@ export class Player {}}+ public reloadAudio(src: string, meta: Waveform) {+ this.audioElement.src = src;+ this.audio.src = src;+ // FIXME UI should be notified about this change+ this.audio.meta = meta;+ }+public requestNextFrame(): void {this.commands.seek = this.frame + 1;}diff --git a/src/transitions/slideTransition.ts b/src/transitions/slideTransition.tsindex bece2739..0543ba8a 100644--- a/src/transitions/slideTransition.ts+++ b/src/transitions/slideTransition.ts@@ -10,7 +10,7 @@ export function slideTransition(direction: Direction = Direction.Top,): SceneTransition {return function* (next, previous): ThreadGenerator {- const project = (yield PROJECT) as Project;+ const project: Project = yield PROJECT;const position = originPosition(direction,project.width(),diff --git a/src/types/Spacing.ts b/src/types/Spacing.tsindex 532a2b08..e4acc2bd 100644--- a/src/types/Spacing.ts+++ b/src/types/Spacing.ts@@ -95,10 +95,10 @@ export class Spacing implements ISpacing {public scale(scale: Vector2d): Spacing {return new Spacing([- this.left * scale.x,- this.right * scale.x,this.top * scale.y,+ this.right * scale.x,this.bottom * scale.y,+ this.left * scale.x,]);}}diff --git a/tools/loaders/wav-loader.js b/tools/loaders/wav-loader.jsindex f6c6830e..5e56c6b6 100644--- a/tools/loaders/wav-loader.js+++ b/tools/loaders/wav-loader.js@@ -11,7 +11,7 @@ function loader() {exec(`..\\bin\\audiowaveform.exe -i ${source} -o ${destination}`,{cwd: __dirname},- async (error, stdout) => {+ (error, stdout) => {if (error) {callback(error);return;diff --git a/tsconfig.json b/tsconfig.jsonindex 8c584a0c..d75377db 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -15,9 +15,7 @@"jsx": "react-jsx","jsxImportSource": "@motion-canvas/core","paths": {- "@motion-canvas/core/jsx-runtime*": [- "jsx-runtime.ts"- ]+ "@motion-canvas/core/jsx-runtime*": ["jsx-runtime.ts"]}}}diff --git a/tsconfig.project.json b/tsconfig.project.jsonindex ab4c2535..29c1086b 100644--- a/tsconfig.project.json+++ b/tsconfig.project.json@@ -14,6 +14,7 @@"MC/*": ["../../core/src/*"],"@motion-canvas/core/*": ["../../core/src/*"],"*": ["../../core/node_modules/*", "../../core/node_modules/@types/*"]- }+ },+ "typeRoots": ["../../core/node_modules/@types", "./node_modules/@types"]}}