Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:新增子组件嵌套文本节点 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,150 changes: 7,150 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions src/core/diff.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { ShapeFlags } from "../utils/base.js";
import {
import {
hostSetElementText,
hostPatchProp,
hostInsert,
hostRemove
} from './dom.js';
hostRemove,
} from "./dom.js";
import { patch } from "./renderer";

function isSameVNodeType (n1, n2) {
function isSameVNodeType(n1, n2) {
return n1.type === n2.type && n1.key === n2.key;
};
}

function patchKeyedChildren(c1, c2, container) {
let i = 0;
Expand Down Expand Up @@ -167,7 +168,4 @@ function patchChildren(oldVnode, vnode, container) {
}
}

export {
patchProps,
patchChildren
}
export { patchProps, patchChildren };
14 changes: 9 additions & 5 deletions src/core/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ function hostCreateElement(type) {
}

function hostSetElementText(el, text) {
el.innerText = text;
if (el.nodeType === Node.TEXT_NODE) {
el.data = text;
} else {
el.innerText = text;
}
}

function hostPatchProp(el, key, preValue, nextValue) {
Expand Down Expand Up @@ -32,9 +36,9 @@ function hostPatchProp(el, key, preValue, nextValue) {
}
}

function hostInsert(child, parent, anchor=null) {
function hostInsert(child, parent, anchor = null) {
if (anchor) {
parent.insertBefore(child,anchor);
parent.insertBefore(child, anchor);
} else {
parent.appendChild(child);
}
Expand All @@ -52,5 +56,5 @@ export {
hostSetElementText,
hostPatchProp,
hostInsert,
hostRemove
}
hostRemove,
};
70 changes: 33 additions & 37 deletions src/core/renderer.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import { ShapeFlags } from "../utils/base.js";
import { queueJob } from "./scheduler";
import { createVNode } from './h';
import { createVNode } from "./h";
import { effect } from "./reactive";
import {
import {
hostCreateElement,
hostSetElementText,
hostPatchProp,
hostInsert,
} from './dom.js';
} from "./dom.js";

import {
patchProps,
patchChildren
} from './diff.js'
import { patchProps, patchChildren } from "./diff.js";

import {
beforeMount,
mounted
} from './lifecycle.js';
import { beforeMount, mounted } from "./lifecycle.js";

const render = (vnode, container) => {
patch(null, vnode, container);
Expand All @@ -32,6 +26,8 @@ function patch(oldVnode, vnode, container = null) {
default:
if (shapeFlag & ShapeFlags.ELEMENT) {
processElement(oldVnode, vnode, container);
} else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
processElement(oldVnode, vnode, container);
} else if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
processComponent(oldVnode, vnode, container);
}
Expand Down Expand Up @@ -61,22 +57,28 @@ function mountElement(vnode, container) {
const { shapeFlag, props } = vnode;
// 1. 先创建 element
// 基于可扩展的渲染 api
const el = (vnode.el = hostCreateElement(vnode.type));
let el;
if (typeof vnode.type === "symbol" && vnode.type.description === "Text") {
el = document.createTextNode(vnode.children);
vnode.el = el;
} else {
el = vnode.el = hostCreateElement(vnode.type);

// 支持单子组件和多子组件的创建
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children);
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
mountChildren(vnode.children, el);
// 支持单子组件和多子组件的创建
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children);
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
mountChildren(vnode.children, el);
}
}

// 处理 props
if (props) {
for (const key in props) {
const nextVal = props[key];
hostPatchProp(el, key, null, nextVal);
}
}

hostInsert(el, container);
}

Expand All @@ -102,9 +104,8 @@ function processComponent(n1, n2, container) {
}

function mountComponent(initialVNode, container) {
const instance = (initialVNode.component = createComponentInstance(
initialVNode
));
const instance = (initialVNode.component =
createComponentInstance(initialVNode));

setupComponent(instance);
setupRenderEffect(instance, container);
Expand All @@ -123,7 +124,6 @@ function createComponentInstance(vnode) {
}

function setupComponent(instance) {

initProps();
initSlots();

Expand All @@ -145,7 +145,6 @@ function setupStatefulComponent(instance) {
instance.render = render;
}


function setupRenderEffect(instance, container) {
/*
经过 sfc 后的代码应该是这样的
Expand Down Expand Up @@ -178,25 +177,22 @@ function setupRenderEffect(instance, container) {
{
scheduler: (effect) => {
// 把 effect 推到微任务的时候在执行
queueJob(effect)
queueJob(effect);
},
}
);
}

const createApp = (rootComponent, rootProps = null) => {
const app = {
mount(rootDom) {
// 此时 rootComponent = {setup, render}
const vnode = createVNode(rootComponent, rootProps);
app._container = rootDom;
render(vnode, rootDom);
},
};
return app;
const app = {
mount(rootDom) {
// 此时 rootComponent = {setup, render}
const vnode = createVNode(rootComponent, rootProps);
app._container = rootDom;
render(vnode, rootDom);
},
};
return app;
};

export {
createApp,
render
}
export { createApp, render, patch };
5 changes: 3 additions & 2 deletions uuzpack/example/demo/demo-reactive.uuz
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div class="abc" @click="addCount">
<div class="abc" @click="addCount" >
{{count.num}}
<div>123sss</div>
</div>
</template>

Expand All @@ -9,7 +10,7 @@ import { reactive } from '../uuz.esm.js';

export default {
setup() {
let count = reactive({ num: 10 })
let count = reactive({ num: 10,age:18 })

const addCount = () => {
count.num += 10;
Expand Down
10 changes: 5 additions & 5 deletions uuzpack/example/main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createApp } from './uuz.esm.js';
import './styles/reset.css'
import { createApp } from "./uuz.esm.js";
import "./styles/reset.css";

// demo-ref
// import App from './demo/demo-ref.js';

// demo-reactive
import App from './demo/demo-reactive.uuz';
import App from "./demo/demo-reactive.uuz";

// demo-computed
// import App from './demo/demo-computed.js';

const ele = document.querySelector("#app");

const ele = document.querySelector('#app');
createApp(App).mount(ele);
createApp(App).mount(ele);
46 changes: 31 additions & 15 deletions uuzpack/example/uuz.esm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const isArr = Array.isArray;
const isObject = val => val !== null && typeof val === 'object';
const Text = Symbol('Text');
const ShapeFlags = {
Expand All @@ -12,8 +13,10 @@ const getShapeFlag = type => {

const blockStack = [];
let currentBlock = null;
const EMPTY_ARR = [];

function createVNode(type, props = null, children = null, patchFlag = 0, dynamicProps = null, isBlockNode = false) {
// TODO: Block相关
const vnode = {
el: null,
component: null,
Expand All @@ -24,7 +27,7 @@ function createVNode(type, props = null, children = null, patchFlag = 0, dynamic
shapeFlag: getShapeFlag(type)
};

if (Array.isArray(children)) {
if (isArr(children)) {
vnode.shapeFlag |= ShapeFlags.ARRAY_CHILDREN;
} else if (typeof children === "string") {
vnode.shapeFlag |= ShapeFlags.TEXT_CHILDREN;
Expand Down Expand Up @@ -56,7 +59,7 @@ function createBlock(type, props, children, patchFlag, dynamicProps) {
return vnode;
}

function createTextVNode(text = ' ', flag = 0) {
function createTextVNode(text = '', flag = 0) {
return createVNode(Text, null, text, flag);
}

Expand Down Expand Up @@ -230,7 +233,11 @@ function hostCreateElement(type) {
}

function hostSetElementText(el, text) {
el.innerText = text;
if (el.nodeType === Node.TEXT_NODE) {
el.data = text;
} else {
el.innerText = text;
}
}

function hostPatchProp(el, key, preValue, nextValue) {
Expand Down Expand Up @@ -456,10 +463,10 @@ function mounted() {
}

const render = (vnode, container) => {
patch$1(null, vnode, container);
patch(null, vnode, container);
};

function patch$1(oldVnode, vnode, container = null) {
function patch(oldVnode, vnode, container = null) {
const {
type,
shapeFlag
Expand All @@ -473,6 +480,8 @@ function patch$1(oldVnode, vnode, container = null) {
default:
if (shapeFlag & ShapeFlags.ELEMENT) {
processElement(oldVnode, vnode, container);
} else if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
processElement(oldVnode, vnode, container);
} else if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
processComponent(oldVnode, vnode, container);
}
Expand Down Expand Up @@ -505,12 +514,19 @@ function mountElement(vnode, container) {
} = vnode; // 1. 先创建 element
// 基于可扩展的渲染 api

const el = vnode.el = hostCreateElement(vnode.type); // 支持单子组件和多子组件的创建
let el;

if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children);
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
mountChildren(vnode.children, el);
if (typeof vnode.type === "symbol" && vnode.type.description === "Text") {
el = document.createTextNode(vnode.children);
vnode.el = el;
} else {
el = vnode.el = hostCreateElement(vnode.type); // 支持单子组件和多子组件的创建

if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
hostSetElementText(el, vnode.children);
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
mountChildren(vnode.children, el);
}
} // 处理 props


Expand All @@ -530,7 +546,7 @@ function mountChildren(children, container) {
// 这里应该需要处理一下 vnodeChild
// 因为有可能不是 vnode 类型
console.log("mountChildren:", VNodeChild);
patch$1(null, VNodeChild, container);
patch(null, VNodeChild, container);
});
}

Expand Down Expand Up @@ -577,7 +593,7 @@ function setupStatefulComponent(instance) {
function setupRenderEffect(instance, container) {
/*
经过 sfc 后的代码应该是这样的
export function render(_ctx, _cache) {
export function render(_ctx, _cache) {
return (_openBlock(), _createBlock("div", {
class: "abc",
onClick: _ctx.addCount
Expand All @@ -588,15 +604,15 @@ function setupRenderEffect(instance, container) {
if (!instance.isMounted) {
const subTree = instance.subTree = instance.render(instance.proxy);
beforeMount();
patch$1(null, subTree, container);
patch(null, subTree, container);
mounted();
instance.isMounted = true;
} else {
const nextTree = instance.render(instance.proxy);
const prevTree = instance.subTree;
instance.subTree = nextTree;
beforeMount();
patch$1(prevTree, nextTree, prevTree.el);
patch(prevTree, nextTree, prevTree.el);
mounted();
}
}, {
Expand All @@ -620,4 +636,4 @@ const createApp = (rootComponent, rootProps = null) => {
return app;
};

export { computed, createApp, createBlock, createTextVNode, createVNode, effect, nextTick, openBlock, queueJob, reactive, ref, render, toDisplayString };
export { computed, createApp, createBlock, createTextVNode, createVNode, effect, nextTick, openBlock, patch, queueJob, reactive, ref, render, toDisplayString };
Loading