实现虚拟Dom
tic.final.js
import {render, Component, createElement} from './toy-react.js'
class Square extends Component {
render() {
return (
<button className="square" onClick={this.props.onClick}>
{this.props.value}
</button>
);
}
}
class Board extends Component {
renderSquare(i) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends Component {
constructor(props) {
super(props);
this.state = {
history: [
{
squares: Array(9).fill(null)
}
],
stepNumber: 0,
xIsNext: true
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([
{
squares: squares
}
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext
});
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0
});
}
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={i => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
// ========================================
let game = <Game />
console.log(game);
// render(game, document.getElementById("root"));
function calculateWinner(squares) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
toy-react.js
const RENDER_TO_DOM = Symbol('render to dom')
export class Component {
constructor() {
this.props = Object.create(null);
this.children = [];
this._root = null;
this._range = null;
}
setAttribute(name, value) {
this.props[name] = value
}
appendChild(component) {
this.children.push(component)
}
get vdom() {
return this.render().vdom;
}
[RENDER_TO_DOM](range) {
this._range = range
this.render()[RENDER_TO_DOM](range)
}
rerender() {
// this._range.deleteContents()
// this[RENDER_TO_DOM](this._range)
// 先插入再删除
let oldRange = this._range;
let range = document.createRange();
range.setStart(oldRange.startContainer, oldRange.startOffset)
range.setEnd(oldRange.startContainer, oldRange.startOffset)
this[RENDER_TO_DOM](range)
oldRange.setStart(range.endContainer, range.endOffset)
oldRange.deleteContents()
}
setState (newState) {
// console.log(newState)
// 没有state 或者 不是对象, 短路
if (this.state == null || typeof this.state != "object") {
this.setState = newState
this.rerender()
return;
}
// 深拷贝
let merge = (oldState, newState) => {
for (let p in newState) {
if (oldState[p] == null || typeof oldState[p] != "object") {
oldState[p] = newState[p]
} else {
merge(oldState[p], newState[p])
}
}
}
merge(this.state, newState)
this.rerender()
}
// get root() {
// if (!this._root) {
// this._root = this.render().root
// }
// return this._root
// }
}
class ElementWrapper extends Component {
constructor(type) {
super(type)
this.type = type
this.root = document.createElement(type)
}
// setAttribute(name, value) {
// // 过滤事件, 如onClick
// if (name.match(/^on([\s\S]+)$/)) {
// // console.log(RegExp.$1)
// // 绑定事件, Click转click
// this.root.addEventListener(RegExp.$1.replace(/^[\s\S]/, c => c.toLowerCase()), value)
// } else if (name == 'className'){
// this.setAttribute('class', value)
// } else {
// this.root.setAttribute(name, value)
// }
// this.root.setAttribute(name, value)
// }
// appendChild(component) {
// // this.root.appendChild(component.root)
// let range = document.createRange()
// range.setStart(this.root, this.root.childNodes.length)
// range.setEnd(this.root, this.root.childNodes.length)
// component[RENDER_TO_DOM](range)
// }
get vdom() {
this.children = this.children.map(child => child.vdom);
return this
}
[RENDER_TO_DOM](range){
range.deleteContents()
range.insertNode(this.root)
}
}
class TextWrapper extends Component{
constructor(content) {
super(content)
this.content = content
this.root = document.createTextNode(content)
}
get vdom() {
return this;
}
[RENDER_TO_DOM](range){
range.deleteContents()
range.insertNode(this.root)
}
}
export function createElement(tagName, attributes, ...rest) {
let element
if (typeof tagName == 'string') {
element = new ElementWrapper(tagName)
} else {
element = new tagName
}
if (typeof attributes === 'object' && attributes instanceof Object) {
for (const key in attributes) {
element.setAttribute(key, attributes[key])
}
}
let insertChildren = (children) => {
// console.log(children)
for(const child of children) {
if (typeof child == 'string') {
child = new TextWrapper(child)
}
if (child === null) {
continue;
}
if ((typeof child == 'object') && (child instanceof Array)) {
insertChildren(child)
} else {
element.appendChild(child)
}
}
}
insertChildren(rest)
return element
}
export function render(component, parentElement) {
// parentElement.appendChild(component.root)
let range = document.createRange()
range.setStart(parentElement, 0)
range.setEnd(parentElement, parentElement.childNodes.length)
component[RENDER_TO_DOM](range)
}