diff --git a/main.js b/main.js index 4d62ebe..8b81d71 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,15 @@ -import {BaseComponent, Application} from './new-angular.js'; +import {BaseComponent, Application, compileToFunction} from './new-angular.js'; import {setTimeoutPatch} from './new-zonejs/patch-settimeout.js'; class MyComponent extends BaseComponent { constructor() { super("MyComponent"); this.count = 0; - this.template = () => `

${this.count}

`; + this.template = () => compileToFunction(``, [() => { + setTimeout(() => { + this.count++; + }) + }, this.count]); } } @@ -13,8 +17,10 @@ const app = new Application(); const component = app.createComponent(new MyComponent()); +app.run(component); + // Change the state of the component and trigger-CD: -// app.run(component); +// component.count += 1; // app.tick(); /* ---- OR ---- */ @@ -22,4 +28,4 @@ const component = app.createComponent(new MyComponent()); setTimeoutPatch(); setTimeout(() => { component.count += 1; -}); \ No newline at end of file +}); diff --git a/new-angular.js b/new-angular.js index 98d0603..2aafce8 100644 --- a/new-angular.js +++ b/new-angular.js @@ -1,11 +1,16 @@ import { Zone } from './new-zonejs/index.js'; -import { setTimeoutPatch } from './new-zonejs/patch-settimeout.js'; class Renderer { // Render the template of a component render(component) { const container = document.getElementById("root"); - container.innerHTML = component.template(); + const template = component.template(); + const matchingEl = Array.from(container.children).find(child => child.nodeName === template.nodeName); + if (matchingEl) { + matchingEl.replaceWith(template); + return; + } + container.appendChild(template); } } @@ -29,7 +34,6 @@ class ChangeDetector { export class BaseComponent { constructor(name) { this.name = name; - this.template = () => ``; } // A method that will be called when the component is initialized @@ -48,7 +52,6 @@ export class Application { this.zone = new Zone(); this.renderer = new Renderer(); this.changeDetector = new ChangeDetector(this.zone, this.renderer); - this.zone.add({ task: () => this.tick() }); } @@ -70,4 +73,44 @@ export class Application { tick() { this.changeDetector.detectChanges(); } +} + +// This has a LOT of limitations, namely that it's bad +// It only handles one element + child text +// It only handles (eventName)="$1" and $1 is a templateArgs function syntax +export function compileToFunction(templateStr, templateArgs) { + const matchTemplateArgs = str => { + if (str[0] === '$') { + const index = Number(str.slice(1)); + return templateArgs[index - 1]; + } + return str; + } + + const openingTagStartIndex = templateStr.indexOf("<"); + const openingTagEndIndex = templateStr.indexOf(">", openingTagStartIndex); + const openingTagStr = templateStr.slice(openingTagStartIndex, openingTagEndIndex + 1); + + const [_, elName] = /^<([a-z0-9]+)/.exec(openingTagStr); + // [{name: "click", value: "$1"}] + const events = []; + + const eventMatches = openingTagStr.matchAll(/\(([a-z]+)\)="(.*?)"/g); + + for (const match of eventMatches) { + events.push({ name: match[1], value: matchTemplateArgs(match[2]) }); + } + + const closingTagStartIndex = templateStr.indexOf(``, openingTagEndIndex); + const textBetweenTags = matchTemplateArgs(templateStr.slice(openingTagEndIndex + 1, closingTagStartIndex)); + + const el = document.createElement(elName); + + for (const { name, value } of events) { + el.addEventListener(name, value); + } + + el.innerHTML = textBetweenTags; + + return el; } \ No newline at end of file diff --git a/new-zonejs/patch-settimeout.js b/new-zonejs/patch-settimeout.js index c1de7df..148022a 100644 --- a/new-zonejs/patch-settimeout.js +++ b/new-zonejs/patch-settimeout.js @@ -1,4 +1,4 @@ -import {currentZone} from './index.js'; +import { currentZone } from './index.js'; export const setTimeoutPatch = () => { const originalSetTimeout = setTimeout; @@ -11,4 +11,4 @@ export const setTimeoutPatch = () => { currentZone.run(); }, delay); }; -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index 1d3a01f..587ab0a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "main.js", "scripts": { - "start": "http-server", + "start": "http-server -c 1", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [],