Git Product home page Git Product logo

jsonml-to-react-component's Introduction

1.jsonml-to-react-component

首先判断是否满足converters中第一个判断条件,如果满足条件我们才会通过第二个函数进行jsonml处理

'use strict';

const React = require('react');
const JsonML = require('jsonml.js/lib/utils');
const utils = require('./utils');

let cid = 0;
module.exports = function toReactComponent(jsonml, converters = []) {
  //默认的jsonml的converters
  const defaultConverters = [
    [(node) => JsonML.getTagName(node) === 'style', (node, index) => {
      const tagName = JsonML.getTagName(node);
      const attrs = JsonML.getAttributes(node);
      const styles = JsonML.getChildren(node)[0];
      return React.createElement(tagName, utils.assign({
        key: index,
        //这个key就是每一个react组件必须有的key
        dangerouslySetInnerHTML: {
          __html: styles,
          //创建style标签,同时将style中的css/less内容作为创建的React的style的Element的内容
        },
      }, attrs));
    }],
    [(node) => typeof node === 'string', (node) => node],
    //如果这个node是"string"那么原样返回
    [() => true, (node, index) => {
      const attrs = utils.assign({ key: index }, JsonML.getAttributes(node));
      //其他的node都是做这样的处理
      if (attrs.class) {
        attrs.className = attrs.class;
        delete attrs.class;
      }
      //将class属性转化为className
      if (attrs.style) {
        attrs.style = utils.toStyleObject(attrs.style);
      }
      //将style的string转化为字符串对象,而不是字符串
      const tagName = JsonML.getTagName(node);
      return React.createElement(
        tagName,
        attrs,
        utils.isStandalone(tagName) ?
          undefined :
          //tagName === 'hr' || tagName === 'br' || tagName === 'img'
          JsonML.getChildren(node).map(innerToReactComponent)
          //对每一个子元素都会进行处理的~~
      );
    }],
  ];

  const mergeConverters = converters.concat(defaultConverters);
// exports.cond = function cond(data, conds, index) {
//   const pair = conds.filter((converter) => {
//     return converter[0](data);
//   })[0];
//   return pair[1](data, index);
// };
// The first function is a prediction, and the second function is a 
// processor which take JsonML node and return React Component.
// 首先根据converters[0]也就是第一个函数进行筛选,筛选通过了才会进行jsonml的处理操作
  function innerToReactComponent(jsonml, index) {
    return utils.cond(jsonml, mergeConverters, index);
  }
  return utils.cond(jsonml, mergeConverters, cid++);
};

2.converters的顺序问题

exports.cond = function cond(data, conds, index) {
  const pair = conds.filter((converter) => {
    return converter[0](data);
    //如果返回true,那么表示这个converter应该是可以处理的,满足被处理的条件
  })[0];
  return pair[1](data, index);
};

对converters进行筛选,我们的defaultConverters在最后面,如果前面有converters 已经满足条件,那么后续的converter不再进行处理

jsonml-to-react-component's People

Contributors

benjycui avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.