国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務器之家 - 編程語言 - JavaScript - React - 使用react從零封裝一個可實時預覽的Json編輯器

使用react從零封裝一個可實時預覽的Json編輯器

2022-01-12 23:27趣談前端徐小夕 React

文章將介紹如何使用react,開發一個自定義json編輯器組件.我們這里使用了jsoneditor這個第三方庫。

使用react從零封裝一個可實時預覽的Json編輯器

綠樹成蔭

做為一名前端開發人員,掌握vue/react/angular等框架已經是必不可少的技能了,我們都知道,vue或react等MVVM框架提倡組件化開發,這樣一方面可以提高組件復用性和可擴展性,另一方面也帶來了項目開發的靈活性和可維護,方便多人開發協作.接下來文章將介紹如何使用react,開發一個自定義json編輯器組件.我們這里使用了jsoneditor這個第三方庫,官方地址: jsoneditor 通過實現一個json在線編輯器,來學習如何一步步封裝自己的組件(不限于react,vue,原理類似)。

你將學到:

  • react組件封裝的基本思路
  • SOLID (面向對象設計)原則介紹
  • jsoneditor用法
  • 使用PropTypes做組件類型檢查

設計思路

在介紹組件設計思路之前,有必要介紹一下著名的SOLID原則。

SOLID(單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉)是由羅伯特·C·馬丁提出的面向對象編程和面向對象設計的五個基本原則。利用這些原則,程序員能更容易和高效的開發一個可維護和擴展的系統。SOLID被典型的應用在測試驅動開發上,并且是敏捷開發以及自適應軟件開發的基本原則的重要組成部分。

  • S 單一功能原則: 規定每個類都應該有一個單一的功能,并且該功能應該由這個類完全封裝起來。所有它的服務都應該嚴密的和該功能保持一致。
  • O 開閉原則: 規定“軟件中的對象(類,模塊,函數等等)應該對于擴展是開放的,但是對于修改是封閉的”,這意味著一個實體是允許在不改變它的源代碼的前提下變更它的行為。遵循這種原則的代碼在擴展時并不需要改變。
  • L 里氏替換原則: 派生類(子類)對象可以在程序中代替其基類(超類)對象,是對子類型的特別定義。
  • I 接口隔離原則: 指明應用或者對象應該不依賴于它不使用的方法。接口隔離原則(ISP)拆分非常龐大臃腫的接口成為更小的和更具體的接口,這樣應用或對象只需要知道它們感興趣的方法。這種縮小的接口也被稱為角色接口。接口隔離原則(ISP)的目的是系統去耦合,從而容易重構,更改和重新部署。接口隔離原則是在SOLID (面向對象設計)中五個面向對象設計(OOD)的原則之一,類似于在GRASP (面向對象設計)中的高內聚性。
  • D 依賴反轉原則: 是指一種特定的解耦 形式,使得高層次的模塊不依賴于低層次的模塊的實現細節,依賴關系被顛倒(反轉),從而使得低層次模塊依賴于高層次模塊的需求抽象。

掌握好這5個原則將有利于我們開發出更優秀的組件,請默默記住.接下來我們來看看json編輯器的設計思路。

使用react從零封裝一個可實時預覽的Json編輯器

如上所示, 和任何一個輸入框一樣, 參考antd組件設計方式并兼容antd的form表單, 我們提供了onChange方法.(具體細節下文會詳細介紹)

首先利用jsoneditor渲染的基本樣式以及API,我們能實現一個基本可用的json編輯器,然后通過對外暴露的json和onChange屬性進行數據雙向綁定, 通過onError來監控異常或者輸入的錯誤, 通過themeBgColor來修改默認的主題色,通過這幾個接口,我們便能完全掌握一個組件的運行情況.

正文

接下來我們就正式開始我們的正文.由于本文的組件是基于react實現的,但是用在vue,angular上,基本模式同樣適用.關鍵就是掌握好不同框架的生命周期.

在學習實現json編輯器組件之前,我們有必要了解一下jsoneditor這個第三方組件的用法與api.

jsoneditor的使用

安裝

我們先執行npm install安裝我們的組件

  1. npm install jsoneditor 

其次手動引入樣式文件

  1. <link href="jsoneditor/dist/jsoneditor.min.css" rel="stylesheet" type="text/css"

這樣,我們就能使用它的api了:

  1. <div id="jsoneditor" style="width: 400px; height: 400px;"></div> 
  2.  
  3. <script> 
  4.     // 創建編輯器 
  5.     var container = document.getElementById("jsoneditor"); 
  6.     var editor = new JSONEditor(container); 
  7.  
  8.     // 設置json數據 
  9.     function setJSON () { 
  10.         var json = { 
  11.             "Array": [1, 2, 3], 
  12.             "Boolean"true
  13.             "Null"null
  14.             "Number": 123, 
  15.             "Object": {"a""b""c""d"}, 
  16.             "String""Hello World" 
  17.         }; 
  18.         editor.set(json); 
  19.     } 
  20.  
  21.     // 獲取json數據 
  22.     function getJSON() { 
  23.         var json = editor.get(); 
  24.         alert(JSON.stringify(json, null, 2)); 
  25.     } 
  26. </script> 

所以你可能看到如下界面:

使用react從零封裝一個可實時預覽的Json編輯器

為了能實現實時預覽和編輯,光這樣還遠遠不夠,我們還需要進行額外的處理.我們需要用到jsoneditor其他的api和技巧.

結合react進行二次封裝

基于以上談論,我們很容易將編輯器封裝成react組件, 我們只需要在componentDidMount生命周期里初始化實例即可.react代碼可能是這樣的:

  1. import React, { PureComponent } from 'react' 
  2. import JSONEditor from 'jsoneditor' 
  3.  
  4. import 'jsoneditor/dist/jsoneditor.css' 
  5.  
  6. class JsonEditor extends PureComponent { 
  7.   initJsonEditor = () => { 
  8.     const options = { 
  9.       mode: 'code'
  10.       history: true
  11.       onChange: this.onChange, 
  12.       onValidationError: this.onError 
  13.     }; 
  14.  
  15.     this.jsoneditor = new JSONEditor(this.container, options) 
  16.     this.jsoneditor.set(this.props.value) 
  17.   } 
  18.  
  19.   componentDidMount () { 
  20.     this.initJsonEditor() 
  21.   } 
  22.  
  23.   componentWillUnmount () { 
  24.     if (this.jsoneditor) { 
  25.       this.jsoneditor.destroy() 
  26.     } 
  27.   } 
  28.  
  29.   render() { 
  30.     return <div className="jsoneditor-react-container" ref={elem => this.container = elem} /> 
  31.   } 
  32. export default JsonEditor 

至于options里的選項, 我們可以參考jsoneditor的API文檔,里面寫的很詳細, 通過以上代碼,我們便可以實現一個基本的react版的json編輯器組件.接下來我們來按照設計思路一步步實現可實時預覽的json編輯器組件.

實現預覽和編輯視圖

其實這一點很好實現,我們只需要實例化2個編輯器實例,一個用于預覽,一個用于編輯就好了.

  1. import React, { PureComponent } from 'react' 
  2. import JSONEditor from 'jsoneditor' 
  3. import 'jsoneditor/dist/jsoneditor.css' 
  4.  
  5. class JsonEditor extends PureComponent { 
  6.   onChange = () => { 
  7.     let value = this.jsoneditor.get() 
  8.     this.viewJsoneditor.set(value) 
  9.   } 
  10.  
  11.   initJsonEditor = () => { 
  12.     const options = { 
  13.       mode: 'code'
  14.       history: true 
  15.     }; 
  16.  
  17.     this.jsoneditor = new JSONEditor(this.container, options) 
  18.     this.jsoneditor.set(this.props.value) 
  19.   } 
  20.  
  21.   initViewJsonEditor = () => { 
  22.     const options = { 
  23.       mode: 'view' 
  24.     }; 
  25.  
  26.     this.viewJsoneditor = new JSONEditor(this.viewContainer, options) 
  27.     this.viewJsoneditor.set(this.props.value) 
  28.   } 
  29.  
  30.   componentDidMount () { 
  31.     this.initJsonEditor() 
  32.     this.initViewJsonEditor() 
  33.   } 
  34.  
  35.   componentDidUpdate() { 
  36.     if(this.jsoneditor) { 
  37.       this.jsoneditor.update(this.props.value) 
  38.       this.viewJsoneditor.update(this.props.value) 
  39.     } 
  40.   } 
  41.  
  42.   render() { 
  43.     return ( 
  44.       <div className="jsonEditWrap"
  45.         <div className="jsoneditor-react-container" ref={elem => this.container = elem} /> 
  46.         <div className="jsoneditor-react-container" ref={elem => this.viewContainer = elem} /> 
  47.       </div> 
  48.     ); 
  49.   } 
  50.  
  51. export default JsonEditor 

這樣,我們便能實現一個初步的可實時預覽的編輯器.可能效果長這樣:

使用react從零封裝一個可實時預覽的Json編輯器

接近于成熟版,但是還有很多細節要處理.

對外暴露屬性和方法以支持不同場景的需要

  1. import React, { PureComponent } from 'react' 
  2. import JSONEditor from 'jsoneditor' 
  3.  
  4. import 'jsoneditor/dist/jsoneditor.css' 
  5.  
  6. class JsonEditor extends PureComponent { 
  7.   // 監聽輸入值的變化 
  8.   onChange = () => { 
  9.     let value = this.jsoneditor.get() 
  10.     this.props.onChange && this.props.onChange(value) 
  11.     this.viewJsoneditor.set(value) 
  12.   } 
  13.   // 對外暴露獲取編輯器的json數據 
  14.   getJson = () => { 
  15.     this.props.getJson && this.props.getJson(this.jsoneditor.get()) 
  16.   } 
  17.   // 對外提交錯誤信息 
  18.   onError = (errArr) => { 
  19.     this.props.onError && this.props.onError(errArr) 
  20.   } 
  21.  
  22.   initJsonEditor = () => { 
  23.     const options = { 
  24.       mode: 'code'
  25.       history: true
  26.       onChange: this.onChange, 
  27.       onValidationError: this.onError 
  28.     }; 
  29.  
  30.     this.jsoneditor = new JSONEditor(this.container, options) 
  31.     this.jsoneditor.set(this.props.value) 
  32.   } 
  33.  
  34.   initViewJsonEditor = () => { 
  35.     const options = { 
  36.       mode: 'view' 
  37.     }; 
  38.  
  39.     this.viewJsoneditor = new JSONEditor(this.viewContainer, options) 
  40.     this.viewJsoneditor.set(this.props.value) 
  41.   } 
  42.  
  43.   componentDidMount () { 
  44.     this.initJsonEditor() 
  45.     this.initViewJsonEditor() 
  46.     // 設置主題色 
  47.     this.container.querySelector('.jsoneditor-menu').style.backgroundColor = this.props.themeBgColor 
  48.     this.container.querySelector('.jsoneditor').style.border = `thin solid ${this.props.themeBgColor}` 
  49.     this.viewContainer.querySelector('.jsoneditor-menu').style.backgroundColor = this.props.themeBgColor 
  50.     this.viewContainer.querySelector('.jsoneditor').style.border = `thin solid ${this.props.themeBgColor}` 
  51.   } 
  52.  
  53.   componentDidUpdate() { 
  54.     if(this.jsoneditor) { 
  55.       this.jsoneditor.update(this.props.json) 
  56.       this.viewJsoneditor.update(this.props.json) 
  57.     } 
  58.   } 
  59.  
  60.   render() { 
  61.     return ( 
  62.       <div className="jsonEditWrap"
  63.         <div className="jsoneditor-react-container" ref={elem => this.container = elem} /> 
  64.         <div className="jsoneditor-react-container" ref={elem => this.viewContainer = elem} /> 
  65.       </div> 
  66.     ); 
  67.   } 
  68.  
  69. export default JsonEditor 

通過以上的過程,我們已經完成一大半工作了,剩下的細節和優化工作,比如組件卸載時如何卸載實例, 對組件進行類型檢測等,我們繼續完成以上問題.

使用PropTypes進行類型檢測以及在組件卸載時清除實例 類型檢測時react內部支持的,安裝react的時候會自動幫我們安裝PropTypes,具體用法可參考官網地址propTypes文檔,其次我們會在react的componentWillUnmount生命周期中清除編輯器的實例以釋放內存.完整代碼如下:

  1. import React, { PureComponent } from 'react' 
  2. import JSONEditor from 'jsoneditor' 
  3. import PropTypes from 'prop-types' 
  4. import 'jsoneditor/dist/jsoneditor.css' 
  5.  
  6. /** 
  7.  * JsonEditor 
  8.  * @param {object} json 用于綁定的json數據 
  9.  * @param {func} onChange 變化時的回調 
  10.  * @param {func} getJson 為外部提供回去json的方法 
  11.  * @param {func} onError 為外部提供json格式錯誤的回調 
  12.  * @param {string} themeBgColor 為外部暴露修改主題色 
  13.  */ 
  14. class JsonEditor extends PureComponent { 
  15.   onChange = () => { 
  16.     let value = this.jsoneditor.get() 
  17.     this.props.onChange && this.props.onChange(value) 
  18.     this.viewJsoneditor.set(value) 
  19.   } 
  20.  
  21.   getJson = () => { 
  22.     this.props.getJson && this.props.getJson(this.jsoneditor.get()) 
  23.   } 
  24.  
  25.   onError = (errArr) => { 
  26.     this.props.onError && this.props.onError(errArr) 
  27.   } 
  28.  
  29.   initJsonEditor = () => { 
  30.     const options = { 
  31.       mode: 'code'
  32.       history: true
  33.       onChange: this.onChange, 
  34.       onValidationError: this.onError 
  35.     }; 
  36.  
  37.     this.jsoneditor = new JSONEditor(this.container, options) 
  38.     this.jsoneditor.set(this.props.value) 
  39.   } 
  40.  
  41.   initViewJsonEditor = () => { 
  42.     const options = { 
  43.       mode: 'view' 
  44.     }; 
  45.  
  46.     this.viewJsoneditor = new JSONEditor(this.viewContainer, options) 
  47.     this.viewJsoneditor.set(this.props.value) 
  48.   } 
  49.  
  50.   componentDidMount () { 
  51.     this.initJsonEditor() 
  52.     this.initViewJsonEditor() 
  53.     // 設置主題色 
  54.     this.container.querySelector('.jsoneditor-menu').style.backgroundColor = this.props.themeBgColor 
  55.     this.container.querySelector('.jsoneditor').style.border = `thin solid ${this.props.themeBgColor}` 
  56.     this.viewContainer.querySelector('.jsoneditor-menu').style.backgroundColor = this.props.themeBgColor 
  57.     this.viewContainer.querySelector('.jsoneditor').style.border = `thin solid ${this.props.themeBgColor}` 
  58.   } 
  59.  
  60.   componentWillUnmount () { 
  61.     if (this.jsoneditor) { 
  62.       this.jsoneditor.destroy() 
  63.       this.viewJsoneditor.destroy() 
  64.     } 
  65.   } 
  66.  
  67.   componentDidUpdate() { 
  68.     if(this.jsoneditor) { 
  69.       this.jsoneditor.update(this.props.json) 
  70.       this.viewJsoneditor.update(this.props.json) 
  71.     } 
  72.   } 
  73.  
  74.   render() { 
  75.     return ( 
  76.       <div className="jsonEditWrap"
  77.         <div className="jsoneditor-react-container" ref={elem => this.container = elem} /> 
  78.         <div className="jsoneditor-react-container" ref={elem => this.viewContainer = elem} /> 
  79.       </div> 
  80.     ); 
  81.   } 
  82.  
  83. JsonEditor.propTypes = { 
  84.   json: PropTypes.object, 
  85.   onChange: PropTypes.func, 
  86.   getJson: PropTypes.func, 
  87.   onError: PropTypes.func, 
  88.   themeBgColor: PropTypes.string 
  89.  
  90. export default JsonEditor 

由于組件嚴格遵守開閉原則,所以我們可以提供更加定制的功能在我們的json編輯器中,已實現不同項目的需求.對于組件開發的健壯性探討,除了使用propTypes外還可以基于typescript開發,這樣適合團隊開發組件庫或者復雜項目組件的追溯和查錯.最終效果如下:

使用react從零封裝一個可實時預覽的Json編輯器

筆者已經將實現過的組件發布到npm上了,大家如果感興趣可以直接用npm安裝后使用,方式如下:

  1. npm i @alex_xu/xui 
  2.  
  3. // 導入xui 
  4. import {  
  5.   Button, 
  6.   Skeleton, 
  7.   Empty, 
  8.   Progress, 
  9.   Tag, 
  10.   Switch, 
  11.   Drawer, 
  12.   Badge, 
  13.   Alert 
  14. from '@alex_xu/xui' 

該組件庫支持按需導入,我們只需要在項目里配置babel-plugin-import即可,具體配置如下:

  1. // .babelrc 
  2. "plugins": [ 
  3.   ["import", { "libraryName""@alex_xu/xui""style"true }] 

npm庫截圖如下:

使用react從零封裝一個可實時預覽的Json編輯器

好啦, 今天的分享就到這啦!

本文轉載自微信公眾號「趣談前端」

使用react從零封裝一個可實時預覽的Json編輯器

原文鏈接:https://mp.weixin.qq.com/s/DxcMtSBmzPCLLB_O-evx-g

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 最新国产一区二区 | 日韩欧美一级精品久久 | 国产一区二区三区成人 | 久久影院一区 | 久久久久久亚洲精品中文字幕 | 69久久久 | 不卡视频一区二区 | 国产区在线 | 国产麻豆一区二区三区 | 亚洲精品一二三区 | 中文字幕日本一区二区 | 激情久久婷婷 | 久久中文字幕在线 | 日韩中文字幕在线观看视频 | 国内精品久久久久久久影视红豆 | 日韩一区二区三区福利视频 | 看av网站 | 国产一区二区免费 | 精品一区二区视频 | 视频一区二区三区中文字幕 | 国产精品综合一区二区 | 欧美精品久久久久久久久老牛影院 | 久久嗨 | 欧美精品成人一区二区三区四区 | 中文字幕在线观看一区二区三区 | av影音资源 | 精品在线播放 | 亚洲精品久久久久久动漫 | 久久都是精品 | 国产精品久久久久久久久久新婚 | 91精品国产91久久综合桃花 | 伦理午夜电影免费观看 | 91精品久久久久久久久久 | 精品一区二区三区在线观看 | 欧美激情综合网 | 精品国产区一区二 | 日韩欧美一区二区三区 | 久久久久久综合 | 亚洲欧美视频在线播放 | 欧美精品在线一区二区三区 | 美日韩一区 |