它有两个显著的特点,一个是基于区块(block-styled)的编辑模式,另一个是可以输出干净的数据。
基于区块官网是这样解释的:
Editor.js工作区由单独的区块组成:段落、标题、图像、列表、引号等。它们中的每一个都是由Plugin提供的独立元素(或更复杂的结构)并由Editor'sCore连结。
对于控件本身也更易于实现,比如在文本“加粗”和“常规”来回切换,基于json的更改一个属性,总要比基于HTML反复添加和删除标记更简单吧?
在编辑区域,Editor.js提供了区块工具栏(BlockTools),内联工具栏(InlineTools)和区块编辑栏(BlockTunes)
他们分别通过加号+按钮,选中区块内容和菜单(六个点和尚按钮)来访问
每个区块都通过插件提供支持,官方提供了常用的插件,当然也可以自己写插件。
官方提供的插件如下图,在sample中,都以cdn方式引入了这些插件,也可以通过npm安装。
序列化后的数据如下图所示,
{"id":"VYsWoLL7yj","type":"image","data":{"url":"data:image/png;base64,...","caption":"codex2x.png","withBorder":false,"withBackground":false,"stretched":false}}列表支持有序和无序列表,列表支持嵌套
无序列表:
{"id":"i_cVQxn3Tb","type":"list","data":{"style":"unordered","items":[{"content":"香蕉","items":[]},{"content":"苹果","items":[]},{"content":"葡萄","items":[]}]}},有序列表:
{"id":"nOTdryosj2","type":"list","data":{"style":"ordered","items":[{"content":"洗手心","items":[]},{"content":"搓手背","items":[]},{"content":"洗指缝","items":[]}]}},嵌套列表:
不代表头:
{"id":"xPAQ6AkUiK","type":"paragraph","data":{"text":"不带表头"}},{"id":"_MMoOqlgXs","type":"table","data":{"withHeadings":false,"content":[["重要紧急","重要不紧急"],["吃饭睡觉","订生日蛋糕"],["不重要但紧急","不重要不紧急"],["上班前定好闹钟","总结这一周的工作"]]}},带表头:
yarnadd@editorjs/editorjs或
importEditorJSfrom'@editorjs/editorjs';consteditor=newEditorJS({/***IdofElementthatshouldcontainEditorinstance*/holder:'editorjs'});这是一个最小化的示例。你会发现没有那些默认的工具。因此需要在配置中指定工具。
可以通过传入配置对象创建编辑器实例。以下是示例
holder指定编辑器的容器元素。
window.editor=newwindow.EditorJS({/***WrapperofEditor*/holder:'editorjs',配置工具
配置完成后,区块工具栏将呈现一个较为完整的工具列表。
/***Toolslist*/tools:{paragraph:{config:{placeholder:"Entersomething"}},header:{class:Header,inlineToolbar:['link'],config:{placeholder:'Header'},shortcut:'CMD+SHIFT+H'},/***Orpassclassdirectlywithoutanyconfiguration*/image:ImageTool,list:{class:NestedList,inlineToolbar:true,shortcut:'CMD+SHIFT+L'},checklist:{class:Checklist,inlineToolbar:true,},quote:{class:Quote,inlineToolbar:true,config:{quotePlaceholder:'Enteraquote',captionPlaceholder:'Quote\'sauthor',},shortcut:'CMD+SHIFT+O'},marker:{class:Marker,shortcut:'CMD+SHIFT+M'},code:{class:CodeTool,shortcut:'CMD+SHIFT+C'},delimiter:Delimiter,inlineCode:{class:InlineCode,shortcut:'CMD+SHIFT+C'},linkTool:LinkTool,embed:Embed,table:{class:Table,inlineToolbar:true,shortcut:'CMD+ALT+T'},},/***InitialEditordata*/data:obj,onReady:function(){saveButton.click();},});本地化可以通过传入i18n配置对象来设置编辑器的本地化。以下是一个较为完整的中文化示例:
如通过重写.root样式选择器可以实现自定义的背景色,重写.ce-popover改变弹出框样式等。