iMove 基于 X6 + form-render 背后的思考

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今天是请到阿里的狼叔给大家分享最近他们团队内部开源项目IMove基于数据可视化的一些思考。本文详细阐述了iMove的可视化编排是如何实现的,以及iMove基于 X6+form-render的思考,整体内容详细且丰富,建议先收藏再看。在今年的5月28-30日举办的 "},{"type":"link","attrs":{"href":"https:\/\/qcon.infoq.cn\/2021\/beijing\/?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"QCon全球软件开发大会(北京站)"}]},{"type":"text","text":"上我们也设置了“低代码探索与实践“专题,目前议题征集中,欢迎大家来QCon分享。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近,我们的项目 "},{"type":"link","attrs":{"href":"https:\/\/github.com\/imgcook\/imove?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 在 "},{"type":"codeinline","content":[{"type":"text","text":"github"}]},{"type":"text","text":" 上的 "},{"type":"codeinline","content":[{"type":"text","text":"star"}]},{"type":"text","text":" 数增长较快,想必是有值得大家肯定的地方的。设计这款工具的初衷是为了提高开发者的开发效率,通过面向业务逻辑的可视化编排提高逻辑元件(如 "},{"type":"codeinline","content":[{"type":"text","text":"ui"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"api"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"function"}]},{"type":"text","text":")的复用能力。我们在双11业务中投入使用 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 进行开发,不仅提高了开发的速度,还积累了许多的逻辑元件,如下图所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ac\/acf9bdb9dc02fdeadcf7605e3fd333b1.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家可能会很好奇,我们按照一直习惯的前端开发模式写代码不好吗?根据产品给的需求文档,根据UI划分一个个的组件,再一起把UI和逻辑实现了不是大功告成吗?为什么要额外引入流程图的绘制,会不会增加工作量?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文会讲2个要点"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 是如何进行开发的,我们为什么要打破以往的开发模式。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"相比于绘制流程图, "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 更吸引人的是它可以将流程图编译成业务项目中可实际运行的代码。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"imove的可视化编排是如何实现的?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 的核心就是基于 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 协议实现的。"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有节点:利用 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 的可视化界面,便于复用和编排。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有指向边:即流程可视化,简单直观,边上还可以带参数。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有 "},{"type":"codeinline","content":[{"type":"text","text":"function"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"schema2form"}]},{"type":"text","text":",支持函数定义,这是面向开发者的。支持 "},{"type":"codeinline","content":[{"type":"text","text":"form"}]},{"type":"text","text":",让每个函数都可以配置入参,这部分是基于阿里开源的 "},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 实现的。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"整个项目难度不大,基于 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 进一步整合,将写法规范化,将编排工具化,这样克制的设计使得 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 具备小而美的特点,便于开发使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"基于imove的开发方式"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"绘制完流程图"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据你的业务逻辑绘制好流程图,这里的节点包括开始节点(圆形)、分支节点(菱形)和行为节点(矩形)。如下图所示,可以绘制多条执行链,每条执行链都从一个开始节点出发。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/c8\/c834dfc26dc01f314b16775ccdd90f8d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"完成每个节点的函数编写"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"依次双击节点,打开代码编辑框,为每个节点编写代码。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/7e\/7e02c342f94e68816c3e54aa5d9ce10b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前端js代码实现包含了同步逻辑和异步逻辑,大多数可以使用同步逻辑实现,但是涉及到接口请求、定时任务等场景时就需要使用到异步逻辑。因此,在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 中我们支持书写同步代码和异步代码,并在编译中考虑了这两种情况。具体写法如下所示。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 同步代码\nexport default function(ctx) {\n const data = ctx.getPipe();\n return doSomething(data);\n}\n\n\/\/ 异步代码\n\/\/ 写法1 使用promise\nexport default function(ctx) {\n return new Promise(resolve => {\n setTimeout(() => resolve(), 2000);\n });\n}\n\/\/ 写法2 使用async await\nexport default async function(ctx) {\n const data = await fetchData();\n return data;\n}"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"在项目中使用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上步骤只涉及到绘制流程图、编写节点代码,但是我们需要把这些js逻辑代码加入到自己的项目中,才能实现一个完整的项目。为了方便把这些编写的 "},{"type":"codeinline","content":[{"type":"text","text":"js"}]},{"type":"text","text":" 逻辑加入到项目中,我们可以选择以下两种方式引入:1)将 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 编写的代码在线打包,将打包文件引入到项目中;2)直接在本地启动开发模式 "},{"type":"codeinline","content":[{"type":"text","text":"imove -d"}]},{"type":"text","text":",可以结合项目进行实时调试,边在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 修改边看项目效果。以下介绍两种引入方式的具体步骤:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"(1)本地打包出码"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"点击页面右上方的\"导出\"按钮后,可以在弹窗内选择“导出代码”,此时流程图编译后的代码将以"},{"type":"codeinline","content":[{"type":"text","text":"zip"}]},{"type":"text","text":"包的形式下载到本地,你可以解压后再引入项目中使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/17\/170862645f14c0ef220e1dc795aa54b6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"引入后使用方法如下:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"通过 "},{"type":"codeinline","content":[{"type":"text","text":"logic.on"}]},{"type":"text","text":" 方法监听事件,事件名和参数与流程图中节点代码的 "},{"type":"text","marks":[{"type":"strong"}],"text":"ctx.emit"},{"type":"text","text":" 相对应"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"通过 "},{"type":"codeinline","content":[{"type":"text","text":"logic.invoke"}]},{"type":"text","text":" 方法调用逻辑,事件名与流程图中的开始节点的 "},{"type":"text","marks":[{"type":"strong"}],"text":"逻辑触发名称"},{"type":"text","text":" 相对应,否则会调用失败"}]}]}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"import React, {useEffect} from 'react';\nimport logic from '.\/logic';\n\nconst App = () => {\n \/\/ 引入方法\n useEffect(() => {\n \/\/ 事件监听——在节点代码中,通过`ctx.emit('a')`执行a事件,以下是监听此事件的函数\n logic.on('a', (data) => {\n \n });\n \/\/ 执行一条流程——触发执行“开始节点”逻辑触发名称为b的那条流程\n logic.invoke('b');\n }, []);\n \n return
xxx\n};\n\nexport default App;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"2)本地启动开发模式"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"安装 "},{"type":"codeinline","content":[{"type":"text","text":"@imove\/cli"}]},{"type":"text","text":" "}]}]}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"$ npm install -g @imove\/cli"}]},{"type":"numberedlist","attrs":{"start":2,"normalizeStart":2},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"进入项目根目录, "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 初始化"}]}]}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"$ cd yourProject \n$ imove --init # 或 imove -i"}]},{"type":"numberedlist","attrs":{"start":3,"normalizeStart":3},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"本地启动开发模式"}]}]}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"$ imove --dev # 或 imove -d"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本地启动成功之后,可以看到原来的页面右上角会显示连接成功。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/d5\/d5ab92015cfe49edd960e2bb154fe72f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此时页面上触发“保存快捷键 Ctrl + S”时,就可以看到当前项目的 "},{"type":"codeinline","content":[{"type":"text","text":"src"}]},{"type":"text","text":" 目录下会多出一个 "},{"type":"codeinline","content":[{"type":"text","text":"logic"}]},{"type":"text","text":" 目录,这就是 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 编译生成的代码,此时你只要在你的组件中调用它即可。调用的方法仍然如"},{"type":"text","marks":[{"type":"strong"}],"text":"本地打包出码"},{"type":"text","text":"中演示的一致。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"为什么需要使用流程编排"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"了解了基于 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 的开发方式,接下来具体讨论一下为什么需要这么做。试想一下,你有没有遇到过以下场景:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"UI"}]},{"type":"text","text":" 经常发生变化,但是我想复用以往实现过的逻辑,却不知道代码在哪里,又要重新写一遍"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"产品的需求文档都是文字,我们只好在脑中构思逻辑,边写边想还容易遗漏逻辑,只好写完了代码反复检查"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"以前做的项目很久没做了,但是最近我想改,但是项目对我来说如此陌生,不知道代码是什么意思,无法快速入手"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"我要接手一个老项目,但是里面的代码逻辑很复杂,又没有什么注释,不知道如何是好"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"我是个新人,在做新业务时可能有很多不太清楚的实现逻辑,如关注店铺、判断登录、发送埋点……,没有什么文档参考,师兄又太忙,问起来很花时间"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"实现某个逻辑a时,想参考下以前别人实现的代码,但是大串大串的业务逻辑耦合在一起,不知道哪一部分才是逻辑a,于是开始自己钻研……"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上这些种种问题,其实是前端开发中或多或少会遇到的痛点。其实,我们可以采用逻辑编排的开发方式,把业务中的一个个功能点按照逻辑顺序组织起来,不仅能够及时检查逻辑漏洞,还能沉淀非常多的业务逻辑,达到参考和复用的效果。以下是总结的采用可视化编排方式进行编程的好处:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/23\/23abaaeec91997098b7c9d90b250cf25.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(1)需求可视化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"把需求按照流程图的方式展示出来,可视化的流程对于新人以及非开发同学来说是非常容易理解的,对了解业务有很大的帮助。特别是在判别条件非常多、业务身份非常多、业务流程冗长而复杂的场景中,能梳理好全部的需求逻辑,能更好地检查代码是否考虑了全部的需求场景。这使得交付、交流、接手会更容易。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2)逻辑复用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因为代码是针对于节点粒度的,在做需求时,可以参考和复用已有的节点代码(如判断登录、关注店铺等等),不仅对新人上手非常友好,也节约了反复编写重复代码的时间,能缩短开发周期。随着逻辑节点的不断沉淀,这种优势也会越发明显。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(3)提高代码规范"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在现有的开发方式下,随着需求不断迭代,可能有会有越来越多的业务逻辑被分散在各个模块中,很可能会违反职责单一、高内聚低耦合的编程原则。而通过逻辑编排的方式开发,每次只针对一个节点编写代码,能保证各个能力职责单一,最后通过聚合的方式串联起整个业务逻辑。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在思考一下,使用到逻辑编排后,上述列举出的种种问题,是不是会迎刃而解呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实逻辑编排不是一种新出现的思想,其实像我们生活中,都有逻辑编排的影子。它只是按照行为逻辑,把一个个单一的业务行为(如限流、限购、加购等等)有序编织为一个完整流程,聚合成一条具有特定业务含义的执行链,每一个完整的流程都会有一个触发点。如下图,就是在业务场景中整理的流程图:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/14\/149361ea45f70a84c99ef93989e30362.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,在 "},{"type":"codeinline","content":[{"type":"text","text":"coding"}]},{"type":"text","text":" 中引入逻辑编排的概念是有价值的,我们也正在尝试着使用新的开发模式去提高生产效率。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"基于x6的流程编排"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在上文中详细展示了为什么需要使用到流程编排,下面详细介绍下流程图绘制的原理。 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 底层采用了蚂蚁团队提供的 "},{"type":"link","attrs":{"href":"https:\/\/X6.antv.vision\/zh\/?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"antv-X6 图编辑引擎"}]},{"type":"text","text":" ,从而实现了流程图绘制的能力。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为什么要选用 "},{"type":"codeinline","content":[{"type":"text","text":"antv-x6"}]},{"type":"text","text":" 作为底层绘图引擎呢?因为它基本能覆盖到流程图绘制的全部需求,内置了图编辑场景的常规交互和设计,能帮助我们快速创建画布、节点和边,能够做到开箱即用,使用是非常方便的。它开放了丰富的定制能力给到开发者,只需简单的配置就能实现想要的效果,其不仅仅支持流程图,还支持 "},{"type":"codeinline","content":[{"type":"text","text":"DAG"}]},{"type":"text","text":" 图、 "},{"type":"codeinline","content":[{"type":"text","text":"ER"}]},{"type":"text","text":" 图、组织架构图等等。在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 中我们仅仅使用到了流程图绘制的能力,其实 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 还有更多的能力值得大家去使用去探索。下面结合 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 框架的实现,详细介绍下我们实现流程图绘制的具体思路,如何从 0 到 1 实现可用的较为完备的流程图绘制能力。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"画布设计"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 创建画布非常容易,实例化一个 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 暴露的 "},{"type":"codeinline","content":[{"type":"text","text":"Graph"}]},{"type":"text","text":" 对象即可。以下是画布的具体配置项,包括了一下特性。"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"节点是否可旋转"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"节点是否可调整大小"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"跨画布的复制\/剪切\/粘贴"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"节点连线规则配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"画布背景配置(支持颜色\/图片\/水印等)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"网格配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","text":"点选\/框选配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","text":"对齐线配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","text":"键盘快捷键配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":10,"align":null,"origin":null},"content":[{"type":"text","text":"撤销\/重做能力"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":11,"align":null,"origin":null},"content":[{"type":"text","text":"画布滚动、平移、居中、缩放等能力"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":12,"align":null,"origin":null},"content":[{"type":"text","text":"鼠标滚轮缩放配置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":13,"align":null,"origin":null},"content":[{"type":"text","text":"……"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"具体的代码实现如下,注释了各类能力对应的API文档,大家有兴趣可以试试~"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"import {Graph} from '@antv\/X6';\nconst flowChart = new Graph({\n \/\/ 渲染指定 dom 节点\n container: document.getElementById('flowChart'),\n \/\/ 节点是否可旋转\n rotating: false,\n \/\/ 节点是否可调整大小\n resizing: true,\n \/\/ 剪切板,支持跨画布的复制\/粘贴(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/clipboard)\n clipboard: {\n enabled: true,\n useLocalStorage: true,\n },\n \/\/ 节点连线规则配置(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/api\/graph\/interaction#connecting)\n connecting: {\n snap: true,\n dangling: true,\n highlight: true,\n anchor: 'center',\n connectionPoint: 'anchor',\n router: { name: 'manhattan' }\n },\n \/\/ 画布背景,支持颜色\/图片\/水印等(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/background)\n background: {\n color: '#f8f9fa',\n },\n \/\/ 网格配置(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/grid)\n grid: {\n visible: true,\n },\n \/\/ 点选\/框选配置(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/selection)\n selecting: {\n enabled: true,\n multiple: true,\n rubberband: true,\n movable: true,\n strict: true,\n showNodeSelectionBox: true\n },\n \/\/ 对齐线配置,辅助移动节点排版(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/snapline)\n snapline: {\n enabled: true,\n clean: 100,\n },\n \/\/ 撤销\/重做能力(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/history)\n history: {\n enabled: true,\n },\n \/\/ 使画布具备滚动、平移、居中、缩放等能力(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/scroller)\n scroller: {\n enabled: true,\n },\n \/\/ 鼠标滚轮缩放(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/mousewheel)\n mousewheel: {\n enabled: true,\n minScale: MIN_ZOOM,\n maxScale: MAX_ZOOM,\n modifiers: ['ctrl', 'meta'],\n },\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完成了画布的开发,接下来在主界面中引入即可。这里我们发现还是存在很多问题:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"无法使用键盘快捷键怎么办,比如复制粘贴保存撤销?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想监听一些画布方法怎么办,例如双击节点直接打开编辑框、右键节点打开菜单、右键画布打开另一个菜单?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想实现小地图怎么办?"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"想把画布信息导出并储存怎么办?"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"……"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"仅仅依赖于以上 "},{"type":"codeinline","content":[{"type":"text","text":"new Graph"}]},{"type":"text","text":" 配置的信息是远远不够的,还需要一步步完善起来。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"快捷键设置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据 "},{"type":"link","attrs":{"href":"https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/keyboard#graphbindkey?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"键盘快捷键 Keyboard"}]},{"type":"text","text":" 介绍,"},{"type":"codeinline","content":[{"type":"text","text":"Graph"}]},{"type":"text","text":" 实例会暴露一个 "},{"type":"codeinline","content":[{"type":"text","text":"bindKey"}]},{"type":"text","text":" 方法,我们可以用它来绑定快捷键。以使用频率最高的 "},{"type":"codeinline","content":[{"type":"text","text":"ctrl + c"}]},{"type":"text","text":" \/ "},{"type":"codeinline","content":[{"type":"text","text":"ctrl + v"}]},{"type":"text","text":" 举例,这里定义了一系列的键盘快捷键和handler监听函数,如 "},{"type":"codeinline","content":[{"type":"text","text":"ctrl + c"}]},{"type":"text","text":"时获取当前选中的元素,利用 "},{"type":"codeinline","content":[{"type":"text","text":"copy()"}]},{"type":"text","text":" 方法完成复制操作,"},{"type":"codeinline","content":[{"type":"text","text":"ctrl + v"}]},{"type":"text","text":" 时直接使用 "},{"type":"codeinline","content":[{"type":"text","text":"paste({offset:xx})"}]},{"type":"text","text":" 方法完成粘贴操作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首选需要在 "},{"type":"codeinline","content":[{"type":"text","text":"new Graph"}]},{"type":"text","text":" 的参数中加入 "},{"type":"codeinline","content":[{"type":"text","text":"keyboard"}]},{"type":"text","text":" 配置项,这里的 "},{"type":"codeinline","content":[{"type":"text","text":"global"}]},{"type":"text","text":" 代表是否为全局键盘事件,设置为 "},{"type":"codeinline","content":[{"type":"text","text":"true"}]},{"type":"text","text":" 时绑定在 "},{"type":"codeinline","content":[{"type":"text","text":"Document"}]},{"type":"text","text":" 上,否则绑定在画布容器上。在这里我们设置为 "},{"type":"codeinline","content":[{"type":"text","text":"false"}]},{"type":"text","text":",只在画布获得焦点才触发键盘事件。"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const flowChart = new Graph({\n \/\/ 键盘快捷键能力(详细文档:https:\/\/X6.antv.vision\/zh\/docs\/tutorial\/basic\/keyboard)\n keyboard: {\n enabled: true,\n global: false,\n }\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下来要配置支持的快捷键,如复制、粘贴、保存、撤销、缩放、全选……需要指定对应的按键以及监听函数。感兴趣的朋友可以看下"},{"type":"link","attrs":{"href":"https:\/\/github.com\/imgcook\/imove\/blob\/master\/packages\/core\/src\/common\/shortcuts.ts?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"shortcuts.ts"}]},{"type":"text","text":"。"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"import { Cell, Edge, Graph, Node } from '@antv\/X6';\ninterface Shortcut {\n keys: string | string[];\n handler: (flowChart: Graph) => void;\n}\nconst shortcuts: { [key: string]: Shortcut } = {\n \/\/ 复制\n copy: {\n keys: 'meta + c',\n handler(flowChart: Graph) {\n const cells = flowChart.getSelectedCells();\n if (cells.length > 0) {\n flowChart.copy(cells);\n message.success('复制成功');\n }\n return false;\n },\n },\n \/\/ 粘贴\n paste: {\n keys: 'meta + v',\n handler(flowChart: Graph) {\n if (!flowChart.isClipboardEmpty()) {\n const cells = flowChart.paste({ offset: 32 });\n flowChart.cleanSelection();\n flowChart.select(cells);\n }\n return false;\n },\n },\n \/\/ many funcions can be defined here\n save: {}, \/\/ 保存\n undo: {}, \/\/ 撤销\n redo: {}, \/\/ 重做\n zoomIn: {}, \/\/ 放大\n zoomOut: {}, \/\/ 缩小\n delete: {}, \/\/ 删除\n selectAll: {}, \/\/ 全选\n bold: {}, \/\/ 加粗\n italic: {}, \/\/ 斜体\n underline: {}, \/\/ 下划线\n bringToTop: {}, \/\/ 置于顶层\n bringToBack: {} \/\/ 置于底层\n};\nexport default shortcuts;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们也可以实现快捷键或滚轮放大、缩小的能力,需要自定义每次缩放尺度的改变量,根据当前的缩放尺度去增减这个改变量即可。为了避免用户无限放大或无限缩小带来不好的视觉体验,最好定义一个最大和最小的缩放尺度。"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const shortcuts: { [key: string]: Shortcut } = {\n zoomIn: {\n keys: 'meta + shift + +',\n handler(flowChart: Graph) {\n const nextZoom = (flowChart.zoom() + ZOOM_STEP).toPrecision(2);\n flowChart.zoomTo(Number(nextZoom), { maxScale: MAX_ZOOM });\n return false;\n },\n },\n zoomOut: {\n keys: 'meta + shift + -',\n handler(flowChart: Graph) {\n const nextZoom = (flowChart.zoom() - ZOOM_STEP).toPrecision(2);\n flowChart.zoomTo(Number(nextZoom), { minScale: MIN_ZOOM });\n return false;\n },\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最后我们把所有的快捷键绑定在 "},{"type":"codeinline","content":[{"type":"text","text":"Graph"}]},{"type":"text","text":" 上,即可完成全部快捷点的配置。"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"shortcuts.forEach(shortcur => {\n const { key, handler } = shortcut;\n graph.bindKey(key, () => handler(graph));\n});"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"画布方法注册"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在当前画布上,我们需要监听一系列常用的方法,如双击节点、画布右键、节点右键等等。在imove中,完成了以下的操作:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"双击节点打开代码编辑框,想提高交互体验"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"右键节点打开菜单栏,支持节点复制、删除、编辑文本、置于顶层\/底层、编辑代码、执行代码"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"右键画布打开菜单栏,支持全选和粘贴"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ca\/cab784b0d0c8bc3181b8a5f0942be99f.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下是具体的实现方法:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const registerEvents = (flowChart: Graph): void => {\n \/\/ 监听节点双击事件,用于打开代码编辑界面\n flowChart.on('node:dblclick', () => {\n });\n \n \/\/ 监听画布右键菜单\n flowChart.on('blank:contextmenu', (args) => {\n });\n \n \/\/ 监听节点右键菜单\n flowChart.on('node:contextmenu', (args) => {\n });\n};"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"创建画布实例"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前已经完成了画布通用配置、快捷键设置、事件绑定,接下来实现一个 "},{"type":"codeinline","content":[{"type":"text","text":"createFlowChart"}]},{"type":"text","text":" 的工厂函数。在工厂函数中,我们创建了画布实例,并为其注册绑定的事件、注册快捷键、注册服务端存储。"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 注册快捷键\nconst registerShortcuts = (flowChart: Graph): void => {\n Object.values(shortcuts).forEach((shortcut) => {\n const { keys, handler } = shortcut;\n flowChart.bindKey(keys, () => handler(flowChart));\n });\n};\nconst createFlowChart = (container: HTMLDivElement, miniMapContainer: HTMLDivElement): Graph => {\n const flowChart = new Graph({\n \/\/ many configuration\n })\n \n registerEvents(flowChart); \/\/ 注册绑定事件\n registerShortcuts(flowChart); \/\/ 注册快捷键\n registerServerStorage(flowChart); \/\/ 注册服务端存储\n \n return flowChart;\n};\n\nexport default createFlowChart;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这样,画布的功能越来越完善了,已经支持绘制流程图了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"导出模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 中,需要支持绘制的流程图以 "},{"type":"codeinline","content":[{"type":"text","text":"DSL"}]},{"type":"text","text":" 、代码、流程图的导出,这样在真实业务开发中,就可以最大程度的复用节点和流程。为了实现这个功能,我们实现的相关函数如下。"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 导出DSL\nconst onExportDSL = () => {\n const dsl = JSON.stringify(flowChart.toJSON(), null, 2);\n const blob = new Blob([dsl], { type: 'text\/plain' });\n DataUri.downloadBlob(blob, 'imove.dsl.json');\n};\n\n\/\/ 导出代码\nconst onExportCode = () => {\n const zip = new JSZip();\n const dsl = flowChart.toJSON();\n const output = compileForProject(dsl);\n Helper.recursiveZip(zip, output);\n\n zip.generateAsync({ type: 'blob' }).then((blob) => {\n DataUri.downloadBlob(blob, 'logic.zip');\n });\n};\n\n\/\/ 导出流程图\nconst onExportFlowChart = () => {\n flowChart.toPNG((dataUri: string) => {\n DataUri.downloadDataUri(dataUri, 'flowChart.png');\n }, { padding: 50, ratio: '3.0' });\n};"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"节点设计"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 中,我们设计了以下三种节点类型:事件节点(开始节点)、行为节点、分支节点。节点负责处理具体的逻辑流程,以下是三种节点的具体描述:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"开始节点:逻辑起始,是所有流程的开始,可以是一次生命周期初始化\/一次点击"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"行为节点:逻辑执行,可以是一次网络请求\/一次改变状态\/发送埋点等"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"分支节点:逻辑路由,根据不同的逻辑执行结果跳转到不同的节点(注:一条逻辑流程必须以"},{"type":"text","marks":[{"type":"strong"}],"text":"开始节点"},{"type":"text","text":"为起始)"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/8b\/8bb955bbf2d6cc7226259232d51834e5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根据上述的规范描述,我们可以绘制出各种各样的逻辑流程图,例如 "},{"type":"text","marks":[{"type":"strong"}],"text":"进入首页"},{"type":"text","text":" 的流程图如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/55\/557bcea40796985d9df29644c262cc8a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"节点属性结构"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 的流程图节点需要承载了多种属性,如节点文字、代码、投放配置模型、投放配置数据、依赖包等等,但是在 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 中,节点的基本属性为 "},{"type":"codeinline","content":[{"type":"text","text":"id"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"shape"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"position"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"size"}]},{"type":"text","text":",主要包括与 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 图形展示相关的基本数据,简称为图形化数据。因此我们需要扩展节点属性,这些扩展的属性简称为自定义数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"图形化数据(主要包括与X6图形展示相关的基本数据)"}]}]}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"id: 32-bit 唯一标识符"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"shape: 形状"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"position: {x, y}: 横向\/纵向位移"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"size: {width, height}: 宽高大小"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"..."}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"2","normalizeStart":"2"},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"自定义数据(扩展的属性)"}]}]}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"type: 节点类型"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"label: 节点展示文案"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"code: 节点存储的代码"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"dependencies: js 代码的依赖包"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"trigger: 触发逻辑开始的事件名(开始节点才有)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ports: 节点出口配置(跳转逻辑,分支节点才有)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"configSchema: 投放配置模型"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"configData: 投放配置数据"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"version: 版本号"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"forkId: 复制来源"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"referId: 引用来源"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"定义完这些节点属性后,就可以实现节点信息的存储,不至于丢失信息。通常来说,信息配置使用频率最高的有:"}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"显示名称:更改节点名称"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"逻辑触发名称:开始节点类型专属配置,项目代码使用时根据这个值触发逻辑调用"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"投放配置 "},{"type":"codeinline","content":[{"type":"text","text":"schema"}]},{"type":"text","text":":修改投放配置的表单结构"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里编辑的每一条信息,都会保存在节点的属性里。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0f\/0f55f82351095dc55e6ca854636154f3.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"节点拖拽"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"单纯有画布是不够的,我们还需支持在画布上添加节点:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/35\/35da8171a3b7b7b928acb8185e7aa772.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以使用 addNode 和 addEdge 方法来动态添加节点和边:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 添加起点\nconst source = graph.addNode({\n id: 'node1',\n x: 40,\n y: 40,\n width: 80,\n height: 40,\n label: 'Hello',\n});\n\/\/ 添加终点\nconst target = graph.addNode({\n id: 'node2',\n x: 160,\n y: 180,\n width: 80,\n height: 40,\n label: 'World',\n});\n\/\/ 连线\ngraph.addEdge({ source, target });"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然而这种方式并不能达到通过拖拽来生成流程图的要求,不过也不用担心, "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 已经考虑到了这点,封装了 "},{"type":"codeinline","content":[{"type":"text","text":"Dnd"}]},{"type":"text","text":" 类(drag and drop)来解决这个问题。代码如下:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"import { Addon, Graph } from '@antv\/X6';\nconst { Dnd } = Addon;\n\/\/ 创建主画布\nconst graph = new Graph({\n id: document.getElementById('flowchart'),\n grid: true,\n snapline: { enabled: true }\n});\n\/\/ 创建 Dnd 实例\nconst dnd = new Dnd({\n target: graph,\n scaled: false,\n animation: true\n});\n\/\/ 创建侧边栏\nconst sideBar = new Graph({\n id: document.getElementById('sideBar'),\n interacting: false\n});\n\/\/ 侧边栏添加内置节点1\nsideBar.addNode({\n id: 'node1',\n x: 80,\n y: 80,\n width: 80,\n height: 40,\n label: 'Hello',\n});\n\/\/ 侧边栏添加内置节点2\nsideBar.addNode({\n id: 'node2',\n x: 80,\n y: 140,\n width: 80,\n height: 40,\n label: 'iMove',\n});\n\/\/ 监听 mousedown 事件,调用 dnd.start 处理拖拽\nsideBar.on(\"cell:mousedown\", (args) => {\n const { node, e } = args;\n dnd.start(node.clone(), e);\n});"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"节点样式设置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了不限制绘制流程图的体验, "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 工具栏提供了绘制流程图常用到的一些功能(例如修改字号、加粗、斜体、文字颜色、背景颜色、对齐等等),这主要也是得益于 "},{"type":"codeinline","content":[{"type":"text","text":"X6"}]},{"type":"text","text":" 提供了统一修改节点样式的方法。工具栏如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/54\/549e7bebac1a30f6a8e0098c44dced55.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用 "},{"type":"codeinline","content":[{"type":"text","text":"setAttrs"}]},{"type":"text","text":" 方法可以配置指定的样式:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 设置字号\ncell.setAttrs({ label: { fontSize: 14 } };\n\/\/ 设置字重\ncell.setAttrs({ label: { fontWeight: 'bold' } });\n\/\/ 设置斜体\ncell.setAttrs({ label: { fontStyle: 'italic' } });\n\/\/ 设置文字颜色\ncell.setAttrs({ label: { fill: 'red' } });\n\/\/ 设置背景颜色\ncell.setAttrs({ body: { fill: 'green' } });\n\/\/ …………"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"节点代码编写"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"每个节点的代码等价于一个 "},{"type":"codeinline","content":[{"type":"text","text":"js"}]},{"type":"text","text":" 模块,因此你不用担心全局变量的命名污染问题,甚至可以 "},{"type":"codeinline","content":[{"type":"text","text":"import"}]},{"type":"text","text":" 现有的 "},{"type":"codeinline","content":[{"type":"text","text":"npm"}]},{"type":"text","text":" 包,但最后必须 "},{"type":"codeinline","content":[{"type":"text","text":"export"}]},{"type":"text","text":" 出一个函数。需要注意的是,由于 "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 天生支持节点代码的异步调用,因此 "},{"type":"codeinline","content":[{"type":"text","text":"export"}]},{"type":"text","text":" 出的函数默认是一个 "},{"type":"codeinline","content":[{"type":"text","text":"promise"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/c1\/c1f1bd8580d1d907a2db5785d6ef76b8.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就以 "},{"type":"text","marks":[{"type":"strong"}],"text":"是否登录"},{"type":"text","text":" 这个分支节点为例,我们来看下节点代码应该如何编写:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export default async function() {\n return fetch('\/api\/isLogin')\n .then(res => res.json())\n .then(res => {\n const {success, data: {isLogin} = {}} = res;\n return success && isLogin;\n }).catch(err => {\n console.log('fetch \/api\/isLogin failed, the err is:', err);\n return false;\n });\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于该节点是分支节点,因此其 "},{"type":"codeinline","content":[{"type":"text","text":"boolean"}]},{"type":"text","text":" 返回值决定了整个流程的走向。如果是非分支节点,直接流向下一个连接的节点即可。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"节点间数据通信"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"完成节点代码编写之后,我们再来看下节点之间是如何进行数据通信的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 中,数据是以流(pipe)的形式从前往后进行流动的,也就是说前一个节点的返回值会是下一个节点的输入。不过也有一个例外,由于 "},{"type":"text","marks":[{"type":"strong"}],"text":"分支节点"},{"type":"text","text":" 的返回值会是 "},{"type":"codeinline","content":[{"type":"text","text":"boolean"}]},{"type":"text","text":" 类型,因此它的下游节点拿到的输入必将是一个 "},{"type":"codeinline","content":[{"type":"text","text":"boolean"}]},{"type":"text","text":" 类型值,从而造成数据流的中断。为此,我们进行了一定的改造,分支节点的作用只负责数据流的转发,就像一个开关一样,只决定数据流的走向,但不改变流向下游的数据——流入分支节点后一个节点的数据依然是分支节点前一个节点输出的数据。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,以下例子中“请求profile接口”和“返回数据“两个节点会成为数据流的上下游关系。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/03\/03d2e6868e9f178d1f5523b341555a78.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们再来看下他们之间是如何进行数据通信的:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"节点: 请求profile接口"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export default async function() {\n return fetch('\/api\/profile')\n .then(res => res.json())\n .then(res => {\n const {success, data} = res;\n return {success, data};\n }).catch(err => {\n console.log('fetch \/api\/isLogin failed, the err is:', err);\n return {success: false};\n });\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"节点: 接口成功"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"export default async function(ctx) {\n \/\/ 获取上游数据\n const pipe = ctx.getPipe() || {};\n return pipe.success;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"节点: 返回数据"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const processData = (data) => {\n \/\/ TODO: 数据加工处理\n return data;\n};\nexport default async function(ctx) {\n \/\/ 这里获取到的上游数据,不是\"接口成功\"这个分支节点的返回值,而是\"请求profile接口\"这个节点的返回值\n const pipe = ctx.getPipe() || {};\n \n \/\/ 触发updateUI这个方法更新界面,传入的值为profileData\n ctx.emit('updateUI', {profileData: processData(pipe.data)});\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上代码所述,每个下游节点可以调用 "},{"type":"codeinline","content":[{"type":"text","text":"ctx.getPipe"}]},{"type":"text","text":" 方法获取上游节点返回的数据流。另外,需要注意的是 "},{"type":"text","marks":[{"type":"strong"}],"text":"返回数据"},{"type":"text","text":" 节点的最后一行代码 "},{"type":"codeinline","content":[{"type":"text","text":"ctx.emit('updateUI', data)"}]},{"type":"text","text":" 需要和项目中的代码配合使用,项目中想监听这个事件,需要执行 "},{"type":"codeinline","content":[{"type":"text","text":"logic.on('updateUI',data=>{ \/\/handler })"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"边设计"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 中,边的作用被弱化,仅表示图形上的节点连接关系,主要控制流程的走向,使用过程中仅用于连线。因此我们没有额外设计边属性,只采用了X6默认的边的属性:id(唯一标识符)、shape(形状)、source(起点)、target(终点)。如下所示:"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"{\n \"id\": \"5d034984-e0d5-4636-a5ab-862f1270d9e0\",\n \"shape\": \"edge\",\n \"source\": {\n \"cell\": \"1b44f69a-1463-4f0e-b8fc-7de848517b4e\",\n \"port\": \"bottom\"\n },\n \"target\": {\n \"cell\": \"c18fa75c-2aad-40e9-b2d2-f3c408933d53\",\n \"port\": \"top\"\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了边属性结构描述外,我们还需要关注边连线实现、样式定制化和代码编译,下面会分别讲一下。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"边连线实现"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"边连线是通过在画布上绑定 "},{"type":"codeinline","content":[{"type":"text","text":"edge:connected"}]},{"type":"text","text":" 事件实现的:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"flowChart.on('edge:connected', (args) => {\n const edge = args.edge as Edge;\n const sourceNode = edge.getSourceNode() as Node;\n\n if (sourceNode && sourceNode.shape === 'imove-branch') {\n const portId = edge.getSourcePortId();\n\n if (portId === 'right' || portId === 'bottom') {\n edge.setLabelAt(0, sourceNode.getPortProp(portId, 'attrs\/text\/text'));\n sourceNode.setPortProp(portId, 'attrs\/text\/text', '');\n }\n }\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这些 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 都已经提供好了,开发和定制都是非常简单。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"边选中样式定制化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默认的边是没有选中高亮的样式的,这里我们可以直接在画布上绑定边选中的事件,改变边的样式即可:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"flowChart.on('edge:selected', (args) => {\n args.edge.attr('line\/stroke', '#feb663');\n args.edge.attr('line\/strokeWidth', '3px');\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果大家想按照自己的喜好来定制流程图上的边展示,可以非常简单的实现。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"基于流程图的代码编译"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"流程图画好了,我们如何实现完整流程的代码运行呢?其实,流程图对应是的一个 "},{"type":"codeinline","content":[{"type":"text","text":"JSON Schema"}]},{"type":"text","text":",这样我们就可以根据节点的连接顺序进行编译了:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0f\/0f5319d0161196937453e64356bfad41.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"流程图编译的 "},{"type":"codeinline","content":[{"type":"text","text":"Schema"}]},{"type":"text","text":" 如下所示:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"分支节点\n{\n \"shape\": \"imove-branch\",\n \"data\": {\n \"ports\": {\n \"right\": {\n \"condition\": \"true\"\n },\n \"bottom\": {\n \"condition\": \"false\"\n }\n },\n \"code\": \"export default async function(ctx) {\\n return true;\\n}\"\n },\n \"id\": \"a6da6684-96c8-4595-bec6-a94122b61e30\"\n}\n连线\n{\n \"shape\": \"edge\",\n \"id\": \"cbcbd0ea-4a2a-4d2a-8135-7b4b7d7ec50d\",\n \"source\": {\n \"cell\": \"de868d18-9ec0-4dac-abbe-5cb9e3c20e2f\",\n \"port\": \"right\"\n },\n \"target\": {\n \"cell\": \"a6da6684-96c8-4595-bec6-a94122b61e30\",\n \"port\": \"left\"\n }\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过流程图 "},{"type":"codeinline","content":[{"type":"text","text":"Schema"}]},{"type":"text","text":",可以解析到节点属性和节点关系,如开始节点、行为节点如何流向下一个节点,分支节点如何根据运行结果分别流向两条边。具体的编译过程如下所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/45\/45af56f0f6b708fe2865c22e909faf4c.png","alt":"image","title":"image","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先根据 "},{"type":"codeinline","content":[{"type":"text","text":"DSL"}]},{"type":"text","text":" 可以获取全部的边(Edge),边的属性上存储了 "},{"type":"codeinline","content":[{"type":"text","text":"source"}]},{"type":"text","text":" 节点和 "},{"type":"codeinline","content":[{"type":"text","text":"target"}]},{"type":"text","text":" 节点的 "},{"type":"codeinline","content":[{"type":"text","text":"id"}]},{"type":"text","text":" 和方向,即起始节点和终点节点的 "},{"type":"codeinline","content":[{"type":"text","text":"id"}]},{"type":"text","text":" 和方向,再根据节点的id可以找到全部的节点,于是可以串联起全部的节点和边。这里有几点要注意的是:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"节点(Node)和边(Edge)的 "},{"type":"codeinline","content":[{"type":"text","text":"id"}]},{"type":"text","text":" 永远都是唯一的,可以根据id找到对应的节点\/边。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"方向永远都是从边(Edge)的 "},{"type":"codeinline","content":[{"type":"text","text":"source"}]},{"type":"text","text":" 节点流向 "},{"type":"codeinline","content":[{"type":"text","text":"target"}]},{"type":"text","text":" 节点的,因此节点之间的流动关系也是固定的。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"判断节点有两个输出方向,需要根据节点输出的boolean值去判断走向。其中判断节点记录了两条边对应的方向和 "},{"type":"codeinline","content":[{"type":"text","text":"boolean"}]},{"type":"text","text":" 值,在编译时需要考虑。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照以上分析的思路,找到当前节点的下一个节点代码如下:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 找到下一个节点\nconst getNextNode = (curNode: Cell.Properties, dsl: DSL) => {\n const nodes = dsl.cells.filter((cell) => cell.shape !== 'edge');\n const edges = dsl.cells.filter((cell) => cell.shape === 'edge');\n const foundEdge = edges.find((edge) => edge.source.cell === curNode.id);\n \n if (foundEdge) {\n return nodes.find((node) => node.id === foundEdge.target.cell);\n }\n};"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"基于form-render的可视化搭建"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在电商领域,业务场景会时常涉及到营销表单的配置。因此代码中会暴露出一些字段供运营配置,这些字段需要以合适的表单形式呈现,因此涉及到表单 "},{"type":"codeinline","content":[{"type":"text","text":"Schema"}]},{"type":"text","text":" 结构的定义。 "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 在定义表单 "},{"type":"codeinline","content":[{"type":"text","text":"Schema"}]},{"type":"text","text":" 结构时,是可以使用可视化的方式设计表单结构的,这里得益于 "},{"type":"link","attrs":{"href":"https:\/\/x-render.gitee.io\/form-render\/","title":null,"type":null},"content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 这个开源项目的优秀设计,我们可以使用其提供的"},{"type":"codeinline","content":[{"type":"text","text":"fr-generator"}]},{"type":"text","text":"库,通过可视化拖拽修改的形式快速生成投放表单结构,方便后续进行数据投放。具体效果如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ef\/ef086bf8922417a96da6e74cd83b1974.png","alt":"image","title":"image","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"如何做到Schema to Form"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上提到的"},{"type":"codeinline","content":[{"type":"text","text":"fr-generator"}]},{"type":"text","text":"表单设计器是如何快速地进行表单搭建的呢?这不得不先说一下如何根据规范化 "},{"type":"codeinline","content":[{"type":"text","text":"Schema"}]},{"type":"text","text":" 转化为 "},{"type":"codeinline","content":[{"type":"text","text":"Form"}]},{"type":"text","text":"。知其然知其所以然,接下来我们看看 "},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 是如何实现这一步的:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/40\/400b8ac8d3f78c7e264ed59282460831.png","alt":"image","title":"image","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"入口是"},{"type":"codeinline","content":[{"type":"text","text":"AntdForm\/FusionForm"}]},{"type":"text","text":"组件(分别兼容"},{"type":"codeinline","content":[{"type":"text","text":"antd"}]},{"type":"text","text":"组件库和"},{"type":"codeinline","content":[{"type":"text","text":"fusion"}]},{"type":"text","text":"组件库)。其中传入"},{"type":"codeinline","content":[{"type":"text","text":"widgets"}]},{"type":"text","text":"参数包含了暴露的所有组件,如"},{"type":"codeinline","content":[{"type":"text","text":"checkbox"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"input"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"radio"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"select"}]},{"type":"text","text":"等,"},{"type":"codeinline","content":[{"type":"text","text":"mapping"}]},{"type":"text","text":"参数包含了"},{"type":"codeinline","content":[{"type":"text","text":"schema"}]},{"type":"text","text":"的"},{"type":"codeinline","content":[{"type":"text","text":"type"}]},{"type":"text","text":"字段与 "},{"type":"codeinline","content":[{"type":"text","text":"widgetName"}]},{"type":"text","text":"(组件名)的映射关系。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"AntdForm\/FusionForm"}]},{"type":"text","text":"组件是由"},{"type":"codeinline","content":[{"type":"text","text":"RenderField"}]},{"type":"text","text":"组件实现的,在这里组合了全部的 "},{"type":"codeinline","content":[{"type":"text","text":"Widget"}]},{"type":"text","text":" 生成了 "},{"type":"codeinline","content":[{"type":"text","text":"Field"}]},{"type":"text","text":" 组件(即纯展示的表单组件)。"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"RenderField"}]},{"type":"text","text":"是由"},{"type":"codeinline","content":[{"type":"text","text":"Field"}]},{"type":"text","text":"组件实现的,在这里将纯展示表单组件和"},{"type":"codeinline","content":[{"type":"text","text":"schema"}]},{"type":"text","text":"的属性结合在一起,转化为真正带有属性的表单组件。转化函数详见"},{"type":"link","attrs":{"href":"https:\/\/github.com\/alibaba\/form-render\/blob\/master\/packages\/form-render\/src\/base\/parser.js","title":null,"type":null},"content":[{"type":"text","text":"parser.js"}]},{"type":"text","text":",这里的转化函数是基于"},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":"设计的json规范实现的,使用"},{"type":"codeinline","content":[{"type":"text","text":"'ui:className'"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"'ui:hidden'"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"'ui:width'"}]},{"type":"text","text":"等一致的属性命名规范去承载样式数据。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接下来讨论下"},{"type":"codeinline","content":[{"type":"text","text":"fr-generator"}]},{"type":"text","text":"表单设计器的核心操作是什么。点击左侧表单组件,就可以将其加入到表格中:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/b4\/b45112b17ab829338104cc797e8e2fe6.png","alt":"image","title":"image","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以下的 "},{"type":"codeinline","content":[{"type":"text","text":"Element"}]},{"type":"text","text":" 组件即代表左侧每一个表单选项,点击左侧表单组件,会调用"},{"type":"codeinline","content":[{"type":"text","text":"handleElementClick"}]},{"type":"text","text":"方法,实际上是调用了 "},{"type":"codeinline","content":[{"type":"text","text":"addItem"}]},{"type":"text","text":" 方法:"}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const Element = ({ text, name, schema, icon }) => {\n \/\/ ......\n \n const { selected, flatten, onFlattenChange } = useStore();\n \n const handleElementClick = () => {\n const { newId, newFlatten } = addItem({ selected, name, schema, flatten });\n onFlattenChange(newFlatten);\n setGlobal({ selected: newId });\n };\n return (\n
\n \n \n );\n};"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"addItem"}]},{"type":"text","text":" 对应的代码实现如下,其实是改变了原有的 "},{"type":"codeinline","content":[{"type":"text","text":"JSON Schema"}]},{"type":"text","text":" 数据, "},{"type":"codeinline","content":[{"type":"text","text":"JSON Schema"}]},{"type":"text","text":" 改变了,中间渲染的表单就会跟随发生变化。核心步骤如下:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"获取画布中选中的节点(表单项)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"获取此节点的父节点children属性"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"找到此节点在children数组中的位置"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"在选中节点后插入新的表单项(其实是在操作父节点的children数组)"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"JSON Schema结构改变"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","text":"画布更新"}]}]}]},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"\/\/ 点击左侧菜单添加表单结构项\nexport const addItem = ({ selected, name, schema, flatten }) => {\n \/\/ ......\n let _name = name + '_' + nanoid(6);\n const idArr = selected.split('\/');\n idArr.pop();\n idArr.push(_name);\n newId = idArr.join('\/');\n const newFlatten = { ...flatten };\n \n try {\n \/\/ 拿到选中的节点\n const item = newFlatten[selected];\n \/\/ 拿到选中节点的父节点children属性\n const siblings = newFlatten[item.parent].children;\n \/\/ 找到选中节点在children数组中的位置\n const idx = siblings.findIndex(x => x === selected);\n \/\/ 将选中节点后插入新的表单项\n siblings.splice(idx + 1, 0, newId);\n const newItem = {\n parent: item.parent,\n schema: { ...schema, $id: newId },\n data: undefined,\n children: [],\n };\n newFlatten[newId] = newItem;\n } catch (error) {\n console.error(error);\n }\n return { newId, newFlatten };\n};"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"数据驱动带来的便利性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实,类似于 "},{"type":"codeinline","content":[{"type":"text","text":"form render"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"fomily"}]},{"type":"text","text":" 这样的库,可以通过简单的 "},{"type":"codeinline","content":[{"type":"text","text":"JSON Schema"}]},{"type":"text","text":" 生成表单,都是基于数据驱动的思想实现的。维护自身的一套 "},{"type":"codeinline","content":[{"type":"text","text":"schema"}]},{"type":"text","text":" 规范,按照规范解析 "},{"type":"codeinline","content":[{"type":"text","text":"schema"}]},{"type":"text","text":" 文件可以直接完成表单的渲染。数据驱动为何让开发者如此热衷? "},{"type":"codeinline","content":[{"type":"text","text":"react"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"vue"}]},{"type":"text","text":" 等流行框架的底层也是数据驱动的思维,解放了程序员繁琐重复的工作。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就拿 "},{"type":"codeinline","content":[{"type":"text","text":"vue"}]},{"type":"text","text":" 框架来说,以下数据驱动带来的便利性非常受人欢迎:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"模板渲染:根据模板生成 "},{"type":"codeinline","content":[{"type":"text","text":"AST"}]},{"type":"text","text":",最后根据 "},{"type":"codeinline","content":[{"type":"text","text":"AST"}]},{"type":"text","text":" 树填充数据生成真实 "},{"type":"codeinline","content":[{"type":"text","text":"DOM"}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"数据绑定:可以监听 交互输入\/http请求响应\/定时器触发 等行为,当数据发生变化时,做 "},{"type":"codeinline","content":[{"type":"text","text":"diff"}]},{"type":"text","text":" 操作,完成 "},{"type":"codeinline","content":[{"type":"text","text":"DOM"}]},{"type":"text","text":" 的更新"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"路由引擎:根据 host\/path\/params 等数据,解析对应页面"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在数据驱动的场景下,我们只需要完成两步:"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"将产品、业务、设计进行抽象化,将UI、交互抽象为数据"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"将数据用逻辑处理连接起来,通过数据去直接影响结果"}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"数据驱动的思想能够给前端带来很多的便利。以前开发时,我们处理页面元素就会处理 "},{"type":"codeinline","content":[{"type":"text","text":"DOM"}]},{"type":"text","text":",处理事件逻辑就会处理 "},{"type":"codeinline","content":[{"type":"text","text":"JavaScript"}]},{"type":"text","text":",处理样式就会处理 "},{"type":"codeinline","content":[{"type":"text","text":"CSS"}]},{"type":"text","text":"。切换为数据驱动的思想之后,我们可以把页面元素、事件逻辑、样式都视为数据,设计好数据与状态之间的转换关系,通过改变数据直接去改变状态。以上介绍的 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 都是可以通过已有的规范 "},{"type":"codeinline","content":[{"type":"text","text":"JSON"}]},{"type":"text","text":" 数据,直接生成对应的流程图和表单,其实数据和UI的转换关系已经隐藏在了框架的内部实现中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这篇文章主要讨论了 "},{"type":"codeinline","content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 基于"},{"type":"codeinline","content":[{"type":"text","text":"X6"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":"背后的思考以及相关的实现原理,主要是在已有开源库的基础上不断去完善,直到满足项目的需求,这样才能做到 "},{"type":"codeinline","content":[{"type":"text","text":"ROI"}]},{"type":"text","text":" 最大化,相互成就。 "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 做的比较好的是定位,继而将写法规范化,将编排工具化,这样克制的设计使得 "},{"type":"codeinline","content":[{"type":"text","text":"iMove"}]},{"type":"text","text":" 具备小而美的特点,便于开发使用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们在初次使用某个开源框架或库时可以根据官方文档提供的示例实现 "},{"type":"codeinline","content":[{"type":"text","text":"Demo"}]},{"type":"text","text":" 效果,由于每个人业务需求都不尽相同,可能需要进行不同的配置甚至进行二次开发才能实现。这时候,可以恰当地使用现有的 "},{"type":"codeinline","content":[{"type":"text","text":"API"}]},{"type":"text","text":" 能力去尽量实现需求,如果没有提供相应的能力,也可以看看有没有提供自定义插件、自定义函数或组件能满足需求。如果还是没有合适的方案,或许可以尝试一下自己实现?如果框架体积非常大但是你只需要使用到其中很小一部分,这时候可以考虑下看看对应的源码,学习下原理尝试自己实现,其实 "},{"type":"codeinline","content":[{"type":"text","text":"x6"}]},{"type":"text","text":" 和 "},{"type":"codeinline","content":[{"type":"text","text":"form-render"}]},{"type":"text","text":" 这种开源基础库在很多场景下都是非常实用的。未来 "},{"type":"link","attrs":{"href":"https:\/\/github.com\/imgcook\/imove?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"imove"}]},{"type":"text","text":" 还会持续迭代,喜欢的朋友们欢迎来踩踩哦~"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"活动推荐"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在今年的5月28-30日举办的 "},{"type":"link","attrs":{"href":"https:\/\/qcon.infoq.cn\/2021\/beijing\/?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"QCon全球软件开发大会(北京站)"}]},{"type":"text","text":"我们也设置了“低代码探索与实践“专题,目前议题征集中,欢迎大家来QCon分享。此外,QCon还设置有前端工程化、业务架构、大数据实时计算等技术专场,感兴趣的同学点击"},{"type":"link","attrs":{"href":"https:\/\/qcon.infoq.cn\/2021\/beijing\/?utm_source=web&utm_campaign=7&utm_medium=infoq&utm_content=langshu","title":"xxx","type":null},"content":[{"type":"text","text":"官网"}]},{"type":"text","text":"抢先了解吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章