下图为 enhancer-timeline 组件的配置页面html
通常配置页面分为 数据源配置 和 其余配置项, 上图中左边是数据源配置, 右边是其余配置项。
配置页面生成的 JSON 以下:node
{ "dataSourceId": 1, // 数据源 id, 经过这个id能获取到组件数据 "direction": "vertical", // 展现方向 "showCenter": true, // 居中显示 "icon":"fas fa-tag", // 节点图标 "iconSize": 16, // 图标大小 "titleFontSize": 16, // 标题字体大小 "contentFontSize": 14, // 内容字体大小 "footerFontSize": 12 // 底部字体大小 }
下图为 enhancer-timeline 组件的预览页面:git
这里咱们准备开发一个列表组件, 组件名叫 widget-dev-demo. 以列表的形式渲染从数据源获取到的数据, 该组件能够控制列表中的数据是不是单行渲染。 下图中左右两部分分别是不一样配置的展现github
全局安装组件开发工具 ewtool:npm
npm install ewtool -g --registry=https://registry.npm.taobao.org
打开命令行终端, 运行下面的命令:json
mkdir widget-dev-demo cd widget-dev-demo ewtool npm install --registry=https://registry.npm.taobao.org npm start
启动 widget-dev-demo 组件开发后, 会在默认浏览器中打开页面并显示以下:api
若是你的默认浏览器不是 Chrome, 建议换成 Chrome 来查看。
开发者须要将组件的不一样配置项渲染在这块区域。浏览器
从左到右分别是: 编辑器
预览组件, 保存配置, 另存为(将组件配置另存为一个场景), 查看配置。函数
若是在尚未点击 保存配置 按钮以前就点击了 预览组件 按钮, 会打开新页面显示以下:
会看到组件一直在加载中, 这是由于此时组件的配置还没保存过, 须要先点 保存配置 按钮。
widget-dev-demo 组件配置器的具体代码请参考 configurator, 下面介绍一下主要步骤:
const tplHTML = template({ locale: locale() }); $('body').html(tplHTML); // 初始化数据源配置器 this.dataSourceConfig = Enhancer.DatasourceManager.createConfigurator('dataSourceDom', { supportedTypes: ['rdb', 'http', 'static', 'jsonp'], dataSpecification: 'dataSpecification', // 组件数据格式说明 onSave: (source) => { this.profile.dataSourceId = source.id; } });
// 用保存过的数据源Id区加载数据源, 而后回填给数据源配置器 if (this.profile.dataSourceId) { Enhancer.DatasourceManager.getDatasource(this.profile.dataSourceId, (source) => { this.dataSourceConfig.setConfig(source); }); } // 回填 单行显示 配置项 if (this.profile.oneLine) { $('input[name=oneLine]').prop('checked', true); }
因为 widget-dev-demo 组件只有一个 数据源 配置项和一个 单行显示 的配置项, 所以 getProfile 函数只会返回这两个字段。
return { dataSourceId: this.profile.dataSourceId, oneLine: $('input[name=oneLine]').prop('checked') };
widget-dev-demo 组件的具体代码请参考widget, 主要代码以下:
// 获取组件所在的 html 容器 const $container = this.getContainer(); // 经过配置器里保存的数据源Id加载数据 this.getSourceData(profile.dataSourceId, (data) => { // 将 数据源数据(data.rows) 和 配置数据(profile.oneLine) 塞给 tpl 模版进行渲染 $container.html(tpl({ locale: locale(), data: data.rows, oneLine: profile.oneLine })); // 通知平台 组件已经初始化完成 this.trig('complete'); });
将下面的数据粘贴到数据源编辑器里。
{ "rows": [{ "content": "In locavore voluptate assumenda. Non raw denim sapiente aute small batch fap. Raw denim cliche lo-fi umami cray incididunt sunt before they sold out. Viral mollit flexitarian locavore, beard readymade eiusmod anim." }, { "content": "Lorem Ipsum has been the industry's standard dummy text eve. Lorem Ipsum has been the industry's standard dummy text eve. Lorem Ipsum has been the industry's standard dummy text eve. Lorem Ipsum has been the industry's standard dummy text eve" }, { "content": "Lorem Ipsum has been the industry's standard dummy text eve" }] }
到配置页面, 点击 预览 按钮后能够看到的效果以下:
以列表的形式渲染数据, 当宽度不够时文字自动换行显示。
到配置页面, 将 单行显示 复选框勾上, 而后点 保存 按钮, 而后再点 预览 按钮能够看到的效果以下:
以列表的形式渲染数据, 当宽度不够时超出部分会显示成省略号。
组件发布能够经过下面两种方式来发布, 任选其一便可
ewtool login // 用在 enhancer 平台上注册的用户名、密码进行登陆, 如已登陆过请忽略 ewtool release
运行下面的命令:
ewtool pack // 会将组件代码打包到 widget-dev-demo/release 下