This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
748 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { IS_NON_DIMENSIONAL } from "../constants"; | ||
import options from "../options"; | ||
|
||
export function createNode(nodeName: string, isSvg: boolean): HTMLElement { | ||
const node: any = isSvg | ||
? document.createElementNS("http://www.w3.org/2000/svg", nodeName) | ||
: document.createElement(nodeName); | ||
node.normalizedNodeName = nodeName; | ||
return node; | ||
} | ||
|
||
export function removeNode(node: HTMLElement) { | ||
const parentNode = node.parentNode; | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
} | ||
|
||
export function setAccessor( | ||
node: HTMLElement, | ||
name: string, | ||
old: any, | ||
value: any, | ||
isSvg: boolean, | ||
) { | ||
if (name === "className") { | ||
name = "class"; | ||
} | ||
if ("ref" === name) { | ||
if (old) { | ||
old(null); | ||
} | ||
if (value) { | ||
value(node); | ||
} | ||
} else if ("class" === name) { | ||
node.className = value || ""; | ||
} else if ("style" === name) { | ||
if (!value || typeof value === "string" || typeof old === "string") { | ||
node.style.cssText = value || ""; | ||
} | ||
if (value && typeof value === "object") { | ||
if (typeof old !== "string") { | ||
for (const i in old) { | ||
if (!(i in value)) { | ||
node.style[i] = ""; | ||
} | ||
} | ||
} | ||
for (const i in value) { | ||
node.style[i] = typeof value[i] === "number" | ||
&& IS_NON_DIMENSIONAL.test(i) === false ? (value[i] + "px") : value[i]; | ||
} | ||
} | ||
} else if ("dangerouslySetInnerHTML" === name) { | ||
if (value) { | ||
node.innerHTML = value._html || ""; | ||
} | ||
} else if (name[0] === "o" && name[1] === "n") { | ||
const oldName = name; | ||
name = name.replace(/Capture$/, ""); | ||
const useCapture = oldName !== oldName; | ||
name = name.toLowerCase().substring(2); | ||
if (value) { | ||
if (!old) { | ||
node.addEventListener(name, eventProxy, useCapture); | ||
} | ||
} else { | ||
node.removeEventListener(name, eventProxy, useCapture); | ||
} | ||
const n: any = node; | ||
if (!n._listeners) { | ||
n._listeners = {}; | ||
} | ||
n._listeners[name] = value; | ||
} else if (name !== "list" && name !== "type" && !isSvg && name in node) { | ||
setProperty(node, name, value == null ? "" : value); | ||
if (value == null || value === false) { | ||
node.removeAttribute(name); | ||
} | ||
} else { | ||
const ns = isSvg && (name !== (name = name.replace(/^xlink\:?/, ""))); | ||
// null || undefined || void 0 || false | ||
if (value == null || value === false) { | ||
if (ns) { | ||
node.removeAttributeNS( | ||
"http://www.w3.org/1999/xlink", | ||
name.toLowerCase(), | ||
); | ||
} else { | ||
node.removeAttribute(name); | ||
} | ||
} else if (typeof value !== "function") { | ||
if (ns) { | ||
node.setAttributeNS( | ||
"http://www.w3.org/1999/xlink", | ||
name.toLowerCase(), | ||
value, | ||
); | ||
} else { | ||
node.setAttribute(name, value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function setProperty(node, name, value) { | ||
try { | ||
node[name] = value; | ||
} catch (e) { } | ||
} | ||
|
||
function eventProxy(e) { | ||
return this._listeners[e.type](options.event && options.event(e) || e); | ||
} |
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,30 @@ | ||
import { Component } from "./component"; | ||
import options from "./options"; | ||
import { defer } from "./util"; | ||
import { renderComponent } from "./vdom/component"; | ||
|
||
let items: Component[] = []; | ||
|
||
export function enqueueRender(component: Component) { | ||
if (!component._dirty) { | ||
component._dirty = true; | ||
const len = items.push(component); | ||
if (len === 1) { | ||
const deferFun = options.debounceRendering || defer; | ||
deferFun(rerender); | ||
} | ||
} | ||
} | ||
|
||
export function rerender() { | ||
let p: Component; | ||
const list = items; | ||
items = []; | ||
p = list.pop(); | ||
while (p) { | ||
if (p._dirty) { | ||
renderComponent(p); | ||
} | ||
p = list.pop(); | ||
} | ||
} |
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,5 @@ | ||
import { diff } from "./vdom/diff"; | ||
|
||
export function render(vnode, parent, merge) { | ||
return diff(merge, vnode, {}, false, parent, false); | ||
} |
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,5 @@ | ||
declare const Promise: any; | ||
|
||
export const defer = typeof Promise === "function" | ||
? Promise.resolve().then | ||
: setTimeout; |
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,42 @@ | ||
import { Component } from "../component"; | ||
|
||
const components: { | ||
[name: string]: Component[]; | ||
} = {}; | ||
|
||
declare class Function { | ||
|
||
} | ||
|
||
export function collectComponent(component: Component) { | ||
const constructor: any = component.constructor; | ||
const name = constructor.name; | ||
const list = components[name] || []; | ||
list.push(component); | ||
} | ||
|
||
export function createComponent(Ctor: any, props: any, context: any) { | ||
const list = components[Ctor.name]; | ||
let inst: Component; | ||
if (Ctor.prototype && Ctor.prototype.render) { | ||
inst = new Ctor(props, context); | ||
Component.call(inst, props, context); | ||
} else { | ||
inst = new Component(props, context); | ||
inst.constructor = Ctor; | ||
inst.render = function doRender(tprops: any, state: any, tcontext: any) { | ||
return this.constructor(tprops, tcontext); | ||
}; | ||
} | ||
if (list) { | ||
for (let i = list.length; i-- ; ) { | ||
const item = list[i]; | ||
if (item.constructor === Ctor) { | ||
inst.nextBase = item.nextBase; | ||
list.splice(i, 1); | ||
break; | ||
} | ||
} | ||
} | ||
return inst; | ||
} |
Oops, something went wrong.