-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ProjectPenrose/develop
v0.4.0
- Loading branch information
Showing
54 changed files
with
776 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ParadoxElement, ParadoxVirtualElement } from "../types"; | ||
export default function buildVirtualDOM(vTree: ParadoxElement | ParadoxElement[]): ParadoxVirtualElement[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const createElement_1 = __importDefault(require("./createElement")); | ||
function buildVirtualDOM(vTree) { | ||
let vDOM = []; | ||
if (!Array.isArray(vTree)) | ||
vTree = [vTree]; | ||
for (const elementObj of vTree) { | ||
let elementObject = elementObj; | ||
if (typeof elementObj === "function") | ||
elementObject = elementObj(); | ||
if (typeof elementObject === "string") { | ||
vDOM.push(elementObject); | ||
continue; | ||
} | ||
for (const [key, value] of Object.entries(elementObject)) { | ||
let { attrs = {}, events = {}, children = [] } = value; | ||
if (children.length) { | ||
children = buildVirtualDOM(children); | ||
} | ||
vDOM.push((0, createElement_1.default)(key, { attrs, events, children })); | ||
} | ||
} | ||
return vDOM; | ||
} | ||
exports.default = buildVirtualDOM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ParadoxElement, ParadoxVirtualElement } from '../types'; | ||
export default function createElement(tagName: string, options?: ParadoxElement): ParadoxVirtualElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function createElement(tagName, options = { children: [], events: {}, attrs: {} }) { | ||
if (!tagName) | ||
throw new Error("tagName is required"); | ||
let children = []; | ||
let events = {}; | ||
let attrs = {}; | ||
if (typeof options === 'object' && !Array.isArray(options) && 'children' in options) { | ||
children = options.children || []; | ||
events = options.events || {}; | ||
attrs = options.attrs || {}; | ||
} | ||
return { | ||
tagName, | ||
attrs, | ||
children, | ||
events, | ||
}; | ||
} | ||
exports.default = createElement; | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ParadoxElement } from "../types"; | ||
export default function createVirtualDOM(treeFunc: Function): ParadoxElement | ParadoxElement[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function createVirtualDOM(treeFunc) { | ||
return treeFunc(); | ||
} | ||
exports.default = createVirtualDOM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ParadoxVirtualElement, Patch } from "../types"; | ||
export default function diff(originalOldTree: ParadoxVirtualElement[], originalNewTree: ParadoxVirtualElement[]): Patch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const render_1 = __importDefault(require("./render")); | ||
function zip(xs, ys) { | ||
const zipped = []; | ||
for (let i = 0; i < Math.min(xs.length, ys.length); i++) { | ||
zipped.push([xs[i], ys[i]]); | ||
} | ||
return zipped; | ||
} | ||
function diffAttrs(oldAttrs, newAttrs) { | ||
const patches = []; | ||
for (const [key, value] of Object.entries(newAttrs)) { | ||
patches.push(node => { | ||
node.setAttribute(key, value); | ||
return node; | ||
}); | ||
} | ||
for (const key of Object.keys(oldAttrs)) { | ||
if (!(key in newAttrs)) { | ||
patches.push(node => { | ||
node.removeAttribute(key); | ||
return node; | ||
}); | ||
} | ||
} | ||
return (node) => { | ||
for (const patch of patches) { | ||
patch(node); | ||
} | ||
}; | ||
} | ||
function diffChildren(oldChildren, newChildren) { | ||
const patches = []; | ||
for (const [oldChild, newChild] of zip(oldChildren, newChildren)) { | ||
patches.push(diff(oldChild, newChild)); | ||
} | ||
const additionalPatches = []; | ||
for (const additionalChild of newChildren.slice(oldChildren.length)) { | ||
additionalPatches.push(node => { | ||
node.appendChild((0, render_1.default)(additionalChild)); | ||
return node; | ||
}); | ||
} | ||
return (parent) => { | ||
for (const [patch, child] of zip(patches, parent.childNodes)) { | ||
patch(child); | ||
} | ||
for (const patch of additionalPatches) { | ||
patch(parent); | ||
} | ||
return parent; | ||
}; | ||
} | ||
function diff(originalOldTree, originalNewTree) { | ||
const oldTree = originalOldTree[0]; | ||
const newTree = originalNewTree[0]; | ||
if (!newTree) { | ||
return (node) => { | ||
node.remove(); | ||
return undefined; | ||
}; | ||
} | ||
if (typeof oldTree === "string" || typeof newTree === "string") { | ||
if (oldTree !== newTree) { | ||
return (node) => { | ||
const newNode = (0, render_1.default)(newTree); | ||
node.replaceWith(newNode); | ||
return newNode; | ||
}; | ||
} | ||
else { | ||
return (node) => undefined; | ||
} | ||
} | ||
if (oldTree.tagName !== newTree.tagName) { | ||
return (node) => { | ||
const newNode = (0, render_1.default)(newTree); | ||
node.replaceWith(newNode); | ||
return newTree; | ||
}; | ||
} | ||
const patchAttr = diffAttrs(oldTree.attrs, newTree.attrs); | ||
const patchChildren = diffChildren(oldTree.children, newTree.children); | ||
return (node) => { | ||
patchAttr(node); | ||
patchChildren(node); | ||
return node; | ||
}; | ||
} | ||
exports.default = diff; | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default function mount(vnode: HTMLElement, target: HTMLElement): HTMLElement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function mount(vnode, target) { | ||
target.replaceWith(vnode); | ||
return vnode; | ||
} | ||
exports.default = mount; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { ParadoxVirtualElement } from "../types"; | ||
export default function render(vnode: ParadoxVirtualElement): HTMLElement | Text; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function renderEle(vnode) { | ||
let tagName = ""; | ||
let attrs = {}; | ||
let children = []; | ||
let events = {}; | ||
if (typeof vnode === "object") { | ||
tagName = vnode.tagName; | ||
attrs = vnode.attrs; | ||
children = vnode.children; | ||
events = vnode.events || {}; | ||
} | ||
const element = document.createElement(tagName); | ||
for (const [key, value] of Object.entries(attrs)) { | ||
element.setAttribute(key, value.toString()); | ||
} | ||
for (const child of children) { | ||
const $child = render(child); | ||
element.appendChild($child); | ||
} | ||
for (const [key, value] of Object.entries(events)) { | ||
if (Array.isArray(value)) { | ||
for (const event of value) { | ||
element.addEventListener(key, event); | ||
} | ||
continue; | ||
} | ||
else { | ||
element.addEventListener(key, value); | ||
} | ||
} | ||
return element; | ||
} | ||
; | ||
function render(vnode) { | ||
if (typeof vnode === "string") { | ||
return document.createTextNode(vnode); | ||
} | ||
return renderEle(vnode); | ||
} | ||
exports.default = render; | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { ParadoxVirtualElement } from '../types'; | ||
export declare let targetNodeCache: HTMLElement; | ||
export declare function setTargetNodeCache(targetNode: HTMLElement): void; | ||
export default function renderVirtualDOM(vDOM: ParadoxVirtualElement[], targetNode: HTMLElement): void; |
Oops, something went wrong.