Repository: iview/iview-editor
Branch: master
Commit: 3c0086c14094
Files: 22
Total size: 859.5 KB
Directory structure:
gitextract_t5d0fv7w/
├── .babelrc
├── .gitignore
├── LICENSE
├── README.md
├── build/
│ └── build-dev.js
├── dist/
│ ├── iview-editor.css
│ └── iview-editor.js
├── index.html
├── package.json
├── src/
│ ├── index.js
│ └── lib/
│ ├── components/
│ │ ├── editor.vue
│ │ ├── md.vue
│ │ └── upload.vue
│ ├── style/
│ │ └── iview-editor.less
│ └── util/
│ ├── index.js
│ ├── insertText.js
│ └── pinyin/
│ ├── pinyinUtil.js
│ ├── pinyin_dict_firstletter.js
│ ├── pinyin_dict_notone.js
│ ├── pinyin_dict_polyphone.js
│ └── pinyin_dict_withtone.js
└── webpack.config.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .babelrc
================================================
{
"presets": ["env", "stage-3"],
"comments": false
}
================================================
FILE: .gitignore
================================================
.DS_Store
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2018 iView
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# iView Editor
[http://editor.iviewui.com](http://editor.iviewui.com)
> iView Editor 是基于 iView 的 markdown 编辑器,支持上传图片(可集成七牛等服务),支持全屏实时编辑预览。
## 如何使用
### 通过 npm 安装
```
npm install iview-editor --save
```
在 webpack 中使用插件,部分代码省略:
``` js
import Vue from 'vue';
import iView from 'iview';
import iEditor from 'iview-editor';
// 使用 css
import 'iview/dist/styles/iview.css';
import 'iview-editor/dist/i-editor.css';
// 使用插件
Vue.use(iView);
Vue.use(iEditor);
```
在 .vue 中使用:
``` html
```
### 通过 CDN 使用
``` html
iView Editor
```
================================================
FILE: build/build-dev.js
================================================
const gulp = require('gulp');
const less = require('gulp-less');
const cssmin = require('gulp-clean-css');
const rename = require('gulp-rename');
gulp.task('compile-css', () => {
return gulp.src(['../src/lib/style/iview-editor.less'])
.pipe(less())
.pipe(cssmin())
.pipe(gulp.dest('../dist/'));
});
gulp.task('auto', () => {
gulp.watch('../src/**/*.less', ['compile-css']);
});
gulp.task('default', ['compile-css', 'auto']);
================================================
FILE: dist/iview-editor.css
================================================
.hljs{display:block;overflow-x:auto;color:#525252;padding:15px;-webkit-text-size-adjust:none}.hljs-doctype{color:#999}.css .hljs-selector-tag,.hljs-tag{color:#3e76f6}.hljs-attr,.hljs-attribute{color:#e96900}.hljs-value{color:#42b983}.hljs-keyword{color:#e96900}.hljs-string{color:#42b983}.hljs-comment{color:#b3b3b3}.hljs-operator .hljs-comment{color:#525252}.hljs-regexp{color:#af7dff}.hljs-built_in{color:#2db7f5}.css .hljs-class,.css .hljs-selector-class{color:#e96900}.css .hljs-number,.javascript .hljs-number{color:#fc1e70}.css .hljs-attribute{color:#af7dff}.css .hljs-important{color:red}.actionscript .hljs-literal,.javascript .hljs-literal{color:#fc1e70}.i-editor{padding-bottom:12px}.i-editor-md{font-size:14px;color:#515a6e}.i-editor-md h1,.i-editor-md h2,.i-editor-md h3,.i-editor-md h4,.i-editor-md h5,.i-editor-md h6{font-weight:700;margin:1em 0;color:#17233d}.i-editor-md hr{height:.05em;border:0;color:#ccc;background-color:#ccc}.i-editor-md blockquote{margin:1em 0;border-left:4px solid #ddd;padding:0 1em;color:#666}.i-editor-md pre{font-size:14px;padding:16px;overflow:auto;line-height:1.45;background-color:#f6f8fa;border-radius:3px}.i-editor-md code{padding:.2em .4em;margin:0;font-size:85%;background-color:rgba(27,31,35,.05);border-radius:3px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace}.i-editor-md pre>code{border:0;margin:0;padding:0;background-color:#f6f8fa;font-size:100%}.i-editor-md a,.i-editor-md a:visited{color:#4183c4;background-color:inherit;text-decoration:none}.i-editor-md img{max-width:100%}.i-editor-md div,.i-editor-md p{line-height:1.7em}.i-editor-md ol,.i-editor-md ul{padding-left:2em}.i-editor-md table{display:block;width:100%;overflow:hidden;border-spacing:0;border-collapse:collapse}.i-editor-md table tr{background-color:#fff;border-top:1px solid #c6cbd1}.i-editor-md table tr:nth-child(2n){background-color:#f6f8fa}.i-editor-md table td,.i-editor-md table th{padding:6px 13px;border:1px solid #dfe2e5}.i-editor-md table th{font-weight:600}.i-editor-md blockquote,.i-editor-md dl,.i-editor-md ol,.i-editor-md p,.i-editor-md pre,.i-editor-md table,.i-editor-md ul{margin-top:0;margin-bottom:.72em}.i-editor-md-cover{width:100%}.i-editor-md-cover img{max-width:100%}.i-editor-tabs{padding-top:5px;padding-bottom:10px;background:#fff}.i-editor-tabs .ivu-tabs-bar{margin-bottom:0;border-bottom:1px solid #eeeff2}.i-editor-md{margin-top:6px}.i-editor-md-tip div{margin-bottom:6px;font-size:14px}.i-editor-md-tip code{padding:.2em .4em;margin:0;font-size:85%;background-color:rgba(27,31,35,.05);border-radius:3px;font-family:SFMono-Regular,Consolas,"Liberation Mono",Menlo,Courier,monospace}.i-editor-md-tip blockquote{margin:1em 0;border-left:4px solid #ddd;padding:0 1em;color:#666}.i-editor-wrapper{padding:0 12px 4px}.i-editor-wrapper .ivu-upload-drag{border:none;overflow:visible}.i-editor-wrapper .ivu-upload-dragOver .ivu-input{border:1px dashed #2d8cf0}.i-editor-fullscreen{min-height:100%}.i-editor-fullscreen .ivu-modal{top:0;height:100%}.i-editor-fullscreen .ivu-modal-content{min-height:100%;border-radius:0}.i-editor-fullscreen-header{width:100%;height:24px;position:fixed;top:0;left:0;border-bottom:1px solid #e8eaec;z-index:1;background:#fff}.i-editor-fullscreen-header p{display:inline-block;float:left;height:24px;line-height:24px;padding-left:16px}.i-editor-fullscreen-header-tip{float:right;margin-right:30px}.i-editor-fullscreen-header-tip .ivu-upload{display:inline-block}.i-editor-fullscreen-main{margin-top:30px}.i-editor-fullscreen-main .ivu-upload-drag{border:none}.i-editor-fullscreen-main .ivu-input{border:none;resize:none}.i-editor-fullscreen-tail{width:1px;height:100%;background:#e8eaec;position:fixed;top:0;left:50%}.i-editor-upload{height:36px;line-height:36px;padding-right:12px}.i-editor-upload .ivu-upload{display:inline-block}.i-editor-upload-item{color:inherit}.i-editor-upload-item i{font-size:16px;vertical-align:middle}.i-editor-upload-cover{background-size:cover;background-position:center}
================================================
FILE: dist/iview-editor.js
================================================
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("iviewEditor",[],t):"object"==typeof exports?exports.iviewEditor=t():e.iviewEditor=t()}("undefined"!=typeof self?self:this,function(){return function(e){function t(Z){if(Y[Z])return Y[Z].exports;var n=Y[Z]={i:Z,l:!1,exports:{}};return e[Z].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var Y={};return t.m=e,t.c=Y,t.d=function(e,Y,Z){t.o(e,Y)||Object.defineProperty(e,Y,{configurable:!1,enumerable:!0,get:Z})},t.n=function(e){var Y=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(Y,"a",Y),Y},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="/dist/",t(t.s=4)}([function(e,t){e.exports=function(e,t,Y,Z,n,L){var X,J=e=e||{},S=typeof e.default;"object"!==S&&"function"!==S||(X=e,J=e.default);var r="function"==typeof J?J.options:J;t&&(r.render=t.render,r.staticRenderFns=t.staticRenderFns,r._compiled=!0),Y&&(r.functional=!0),n&&(r._scopeId=n);var i;if(L?(i=function(e){e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext,e||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),Z&&Z.call(this,e),e&&e._registeredComponents&&e._registeredComponents.add(L)},r._ssrRegister=i):Z&&(i=Z),i){var s=r.functional,o=s?r.render:r.beforeCreate;s?(r._injectStyles=i,r.render=function(e,t){return i.call(t),o(e,t)}):r.beforeCreate=o?[].concat(o,i):[i]}return{esModule:X,exports:J,options:r}}},function(e,t,Y){"use strict";function Z(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var n=Y(6),L=Z(n),X=Y(7),J=Z(X),S=Y(9),r=Z(S);t.default={name:"iEditor",components:{Upload:J.default,Markdown:r.default},props:{affix:Boolean,offsetTop:Number,placeholder:String,autosize:Object,writeName:{type:String,default:"内容"},changeScroll:Boolean,cover:String,showSummary:{type:Boolean,default:!1},config:{type:Object,default:function(){return{action:"/",maxSize:5120}}},value:{type:String,default:""},beforeUpload:{type:Function,default:function(){return!0}},imgUrl:{type:Function,default:function(e){return e}},highlight:{type:Function,default:function(e){return e}},paste:{type:Boolean,default:!1}},data:function(){return{tabType:"write",content:this.value,showMdTip:!1,showDiff:!1,showDiffEditor:!1,summary:""}},watch:{showDiff:function(e){var t=this;this.$nextTick(function(){t.showDiffEditor=e})},value:function(e){this.content=e},content:function(e){this.$emit("input",e)}},computed:{},methods:{handleChangeTab:function(e){var t=this;"write"===e?this.$nextTick(function(){t.$refs.content.focus()}):"preview"===e?this.changeScroll&&this.$nextTick(function(){var e=t.$refs.markdown.$el;window.scrollTo(0,e.offsetTop)}):"summary"===e&&this.$nextTick(function(){t.$refs.summary.focus()})},handleUploadSuccess:function(e){var t=this,Y=this.imgUrl(e),Z="",n=this.$refs.content.$refs.textarea;(0,L.default)(n,Z),this.$nextTick(function(){t.content=n.value,t.$refs.content.focus()})},handleImportMd:function(e){var t=this;""!==this.content?this.$Modal.confirm({title:"导入确认",content:"导入后将覆盖您已经输入的内容,是否继续导入?",onOk:function(){t.content=e}}):this.content=e},focus:function(){this.$refs.content&&this.$refs.content.focus()}}}},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default={props:{type:{type:String,default:"select"},styles:{type:Number,default:0},cover:String,info:{type:Boolean,default:!1},showBtn:Boolean,config:Object,beforeUpload:Function,paste:Boolean},data:function(){return{uploadForm:{},uploadPercent:0,uploadStatus:0,noUploadAccess:!1}},computed:{},methods:{beforeUploadFile:function(e){if(6===this.styles){if("text/markdown"!==e.type)return this.$Notice.warning({title:"文件格式不正确",desc:"您上传的文件 "+e.name+" 格式不符合要求,请上传 .md 格式的文件。",duration:6}),!1;var t=new FileReader;t.readAsText(e);var Y=this;return t.onload=function(){Y.$emit("on-success",this.result)},!1}return this.beforeUpload(e)},handleSuccess:function(e){this.uploadStatus=0,3===this.styles&&100===this.uploadPercent&&this.showUploadProgress&&(this.showUploadProgress(),this.showUploadProgress=null),this.$emit("on-success",e)},handleError:function(){this.uploadStatus=0,this.$emit("on-error"),3===this.styles&&this.showUploadProgress&&(this.showUploadProgress(),this.showUploadProgress=null)},handleFormatError:function(e){this.$Notice.warning({title:"文件格式不正确",desc:"您上传的文件 "+e.name+" 格式不符合要求,请上传 jpg、png、gif 格式的文件。",duration:6})},handleMaxSize:function(e){this.$Notice.warning({title:"文件过大",desc:"您上传的文件 "+e.name+" 体积过大,请上传不超过 "+this.config.maxSize/1024+" 的文件。",duration:6})},handleProgress:function(e){var t=this;if(this.$emit("on-progress",e),this.uploadStatus=1,this.uploadPercent=parseInt(e.percent),3===this.styles){if(this.noUploadAccess)return;this.showUploadProgress||(this.showUploadProgress=this.$Message.loading({duration:0,render:function(e){return e("span","图片上传中("+t.uploadPercent+"%)")}}))}}}}},function(e,t,Y){"use strict";function Z(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0});var n=Y(10),L=Z(n),X=Y(12),J=Z(X);t.default={props:{content:String,highlight:Function},data:function(){return{html:""}},watch:{content:function(){this.renderMd()}},methods:{renderMd:function(){var e=new L.default.Renderer;e.heading=function(e,t){var Y=J.default.getFirstLetter(e);return Y=Y.replace(/\s/g,"_").replace(/\?|?|,/g,""),"'+e+" "};var t=this;this.html=(0,L.default)(this.content,{breaks:!0,headerIds:!1,highlight:function(e){return t.highlight(e)},renderer:e})}},mounted:function(){this.renderMd()}}},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var Z=Y(5),n=function(e){return e&&e.__esModule?e:{default:e}}(Z),L={iEditor:n.default},X=function(e){arguments.length>1&&void 0!==arguments[1]&&arguments[1];e.component("i-editor",n.default)};"undefined"!=typeof window&&window.Vue&&X(window.Vue),t.default=Object.assign(L,{install:X})},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var Z=Y(1),n=Y.n(Z);for(var L in Z)"default"!==L&&function(e){Y.d(t,e,function(){return Z[e]})}(L);var X=Y(15),J=Y(0),S=J(n.a,X.a,!1,null,null,null);t.default=S.exports},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t){if(document.selection){document.selection.createRange().text=t}else if("number"==typeof e.selectionStart&&"number"==typeof e.selectionEnd){var Y=e.selectionStart,Z=e.selectionEnd,n=Y,L=e.value;e.value=L.substring(0,Y)+t+L.substring(Z,L.length),n+=t.length,e.selectionStart=e.selectionEnd=n}else e.value+=t}},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var Z=Y(2),n=Y.n(Z);for(var L in Z)"default"!==L&&function(e){Y.d(t,e,function(){return Z[e]})}(L);var X=Y(8),J=Y(0),S=J(n.a,X.a,!1,null,null,null);t.default=S.exports},function(e,t,Y){"use strict";var Z=function(){var e=this,t=e.$createElement,Y=e._self._c||t;return Y("Upload",{attrs:{type:e.type,paste:e.paste,action:e.config.action,"show-upload-list":!1,data:e.config.uploadForm,format:["jpg","jpeg","png","gif"],"on-format-error":e.handleFormatError,"max-size":e.config.maxSize,"on-exceeded-size":e.handleMaxSize,"before-upload":e.beforeUploadFile,"on-success":e.handleSuccess,"on-error":e.handleError,"on-progress":e.handleProgress}},[e._t("default",[1===e.styles?Y("Button",{staticClass:"i-editor-upload-item",attrs:{type:"text",size:"small",disabled:1===e.uploadStatus}},[Y("Tooltip",{attrs:{content:"上传图片",transfer:""}},[Y("Icon",{attrs:{type:"ios-image"}})],1),e._v(" "),Y("span",{directives:[{name:"show",rawName:"v-show",value:e.uploadStatus,expression:"uploadStatus"}]},[e._v("("+e._s(e.uploadPercent)+"%)")])],1):e._e(),e._v(" "),6===e.styles?Y("Button",{staticClass:"i-editor-upload-item",attrs:{type:"text",size:"small"}},[Y("Tooltip",{attrs:{content:"导入 Markdown 文件",transfer:""}},[Y("Icon",{attrs:{type:"md-document"}})],1)],1):e._e()])],2)},n=[],L={render:Z,staticRenderFns:n};t.a=L},function(e,t,Y){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var Z=Y(3),n=Y.n(Z);for(var L in Z)"default"!==L&&function(e){Y.d(t,e,function(){return Z[e]})}(L);var X=Y(14),J=Y(0),S=J(n.a,X.a,!1,null,null,null);t.default=S.exports},function(e,t,Y){(function(t){!function(t){"use strict";function Y(e){this.tokens=[],this.tokens.links={},this.options=e||C.defaults,this.rules=Q.normal,this.options.gfm&&(this.options.tables?this.rules=Q.tables:this.rules=Q.gfm)}function Z(e,t){if(this.options=t||C.defaults,this.links=e,this.rules=a.normal,this.renderer=this.options.renderer||new n,this.renderer.options=this.options,!this.links)throw new Error("Tokens array requires a `links` property.");this.options.gfm?this.options.breaks?this.rules=a.breaks:this.rules=a.gfm:this.options.pedantic&&(this.rules=a.pedantic)}function n(e){this.options=e||{}}function L(){}function X(e){this.tokens=[],this.token=null,this.options=e||C.defaults,this.options.renderer=this.options.renderer||new n,this.renderer=this.options.renderer,this.renderer.options=this.options}function J(e,t){return e.replace(t?/&/g:/&(?!#?\w+;)/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")}function S(e){return e.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/gi,function(e,t){return t=t.toLowerCase(),"colon"===t?":":"#"===t.charAt(0)?"x"===t.charAt(1)?String.fromCharCode(parseInt(t.substring(2),16)):String.fromCharCode(+t.substring(1)):""})}function r(e,t){return e=e.source,t=t||"",{replace:function(t,Y){return Y=Y.source||Y,Y=Y.replace(/(^|[^\[])\^/g,"$1"),e=e.replace(t,Y),this},getRegex:function(){return new RegExp(e,t)}}}function i(e,t){return T[" "+e]||(/^[^:]+:\/*[^\/]*$/.test(e)?T[" "+e]=e+"/":T[" "+e]=e.replace(/[^\/]*$/,"")),e=T[" "+e],"//"===t.slice(0,2)?e.replace(/:[\s\S]*/,":")+t:"/"===t.charAt(0)?e.replace(/(:\/*[^\/]*)[\s\S]*/,"$1")+t:e+t}function s(){}function o(e){for(var t,Y,Z=1;ZAn error occurred:"+J(e.message+"",!0)+" ";throw e}}var Q={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:s,hr:/^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:s,blockquote:/^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:/^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,def:/^ {0,3}\[(label)\]: *\n? *([^\s>]+)>?(?:(?: +\n? *| *\n *)(title))? *(?:\n+|$)/,table:s,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,paragraph:/^([^\n]+(?:\n?(?!hr|heading|lheading| {0,3}>|tag)[^\n]+)+)/,text:/^[^\n]+/};Q._label=/(?:\\[\[\]]|[^\[\]])+/,Q._title=/(?:"(?:\\"|[^"]|"[^"\n]*")*"|'\n?(?:[^'\n]+\n?)*'|\([^()]*\))/,Q.def=r(Q.def).replace("label",Q._label).replace("title",Q._title).getRegex(),Q.bullet=/(?:[*+-]|\d+\.)/,Q.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,Q.item=r(Q.item,"gm").replace(/bull/g,Q.bullet).getRegex(),Q.list=r(Q.list).replace(/bull/g,Q.bullet).replace("hr","\\n+(?=\\1?(?:(?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$))").replace("def","\\n+(?="+Q.def.source+")").getRegex(),Q._tag="(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:|[^\\w\\s@]*@)\\b",Q.html=r(Q.html).replace("comment",//).replace("closed",/<(tag)[\s\S]+?<\/\1>/).replace("closing",/ \s]*)*?\/?>/).replace(/tag/g,Q._tag).getRegex(),Q.paragraph=r(Q.paragraph).replace("hr",Q.hr).replace("heading",Q.heading).replace("lheading",Q.lheading).replace("tag","<"+Q._tag).getRegex(),Q.blockquote=r(Q.blockquote).replace("paragraph",Q.paragraph).getRegex(),Q.normal=o({},Q),Q.gfm=o({},Q.normal,{fences:/^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\n? *\1 *(?:\n+|$)/,paragraph:/^/,heading:/^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/}),Q.gfm.paragraph=r(Q.paragraph).replace("(?!","(?!"+Q.gfm.fences.source.replace("\\1","\\2")+"|"+Q.list.source.replace("\\1","\\3")+"|").getRegex(),Q.tables=o({},Q.gfm,{nptable:/^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,table:/^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/}),Y.rules=Q,Y.lex=function(e,t){return new Y(t).lex(e)},Y.prototype.lex=function(e){return e=e.replace(/\r\n|\r/g,"\n").replace(/\t/g," ").replace(/\u00a0/g," ").replace(/\u2424/g,"\n"),this.token(e,!0)},Y.prototype.token=function(e,t){e=e.replace(/^ +$/gm,"");for(var Y,Z,n,L,X,J,S,r,i,s,o;e;)if((n=this.rules.newline.exec(e))&&(e=e.substring(n[0].length),n[0].length>1&&this.tokens.push({type:"space"})),n=this.rules.code.exec(e))e=e.substring(n[0].length),n=n[0].replace(/^ {4}/gm,""),this.tokens.push({type:"code",text:this.options.pedantic?n:n.replace(/\n+$/,"")});else if(n=this.rules.fences.exec(e))e=e.substring(n[0].length),this.tokens.push({type:"code",lang:n[2],text:n[3]||""});else if(n=this.rules.heading.exec(e))e=e.substring(n[0].length),this.tokens.push({type:"heading",depth:n[1].length,text:n[2]});else if(t&&(n=this.rules.nptable.exec(e))){for(e=e.substring(n[0].length),J={type:"table",header:n[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:n[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:n[3].replace(/\n$/,"").split("\n")},r=0;r ?/gm,""),this.token(n,t),this.tokens.push({type:"blockquote_end"});else if(n=this.rules.list.exec(e)){for(e=e.substring(n[0].length),L=n[2],o=L.length>1,this.tokens.push({type:"list_start",ordered:o,start:o?+L:""}),n=n[0].match(this.rules.item),Y=!1,s=n.length,r=0;r1&&X.length>1||(e=n.slice(r+1).join("\n")+e,r=s-1)),Z=Y||/\n\n(?!\s*$)/.test(J),r!==s-1&&(Y="\n"===J.charAt(J.length-1),Z||(Z=Y)),this.tokens.push({type:Z?"loose_item_start":"list_item_start"}),this.token(J,!1),this.tokens.push({type:"list_item_end"});this.tokens.push({type:"list_end"})}else if(n=this.rules.html.exec(e))e=e.substring(n[0].length),this.tokens.push({type:this.options.sanitize?"paragraph":"html",pre:!this.options.sanitizer&&("pre"===n[1]||"script"===n[1]||"style"===n[1]),text:n[0]});else if(t&&(n=this.rules.def.exec(e)))e=e.substring(n[0].length),n[3]&&(n[3]=n[3].substring(1,n[3].length-1)),i=n[1].toLowerCase(),this.tokens.links[i]||(this.tokens.links[i]={href:n[2],title:n[3]});else if(t&&(n=this.rules.table.exec(e))){for(e=e.substring(n[0].length),J={type:"table",header:n[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:n[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:n[3].replace(/(?: *\| *)?\n$/,"").split("\n")},r=0;r])/,autolink:/^<(scheme:[^\s\x00-\x1f<>]*|email)>/,url:s,tag:/^|^<\/?[a-zA-Z0-9\-]+(?:"[^"]*"|'[^']*'|\s[^<'">\/\s]*)*?\/?>/,link:/^!?\[(inside)\]\(href\)/,reflink:/^!?\[(inside)\]\s*\[([^\]]*)\]/,nolink:/^!?\[((?:\[[^\[\]]*\]|\\[\[\]]|[^\[\]])*)\]/,strong:/^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,em:/^_([^\s_](?:[^_]|__)+?[^\s_])_\b|^\*((?:\*\*|[^*])+?)\*(?!\*)/,code:/^(`+)\s*([\s\S]*?[^`]?)\s*\1(?!`)/,br:/^ {2,}\n(?!\s*$)/,del:s,text:/^[\s\S]+?(?=[\\?(?:\s+['"]([\s\S]*?)['"])?\s*/,a.link=r(a.link).replace("inside",a._inside).replace("href",a._href).getRegex(),a.reflink=r(a.reflink).replace("inside",a._inside).getRegex(),a.normal=o({},a),a.pedantic=o({},a.normal,{strong:/^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,em:/^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/}),a.gfm=o({},a.normal,{escape:r(a.escape).replace("])","~|])").getRegex(),url:r(/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/).replace("email",a._email).getRegex(),_backpedal:/(?:[^?!.,:;*_~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_~)]+(?!$))+/,del:/^~~(?=\S)([\s\S]*?\S)~~/,text:r(a.text).replace("]|","~]|").replace("|","|https?://|ftp://|www\\.|[a-zA-Z0-9.!#$%&'*+/=?^_`{\\|}~-]+@|").getRegex()}),a.breaks=o({},a.gfm,{br:r(a.br).replace("{2,}","*").getRegex(),text:r(a.gfm.text).replace("{2,}","*").getRegex()}),Z.rules=a,Z.output=function(e,t,Y){return new Z(t,Y).output(e)},Z.prototype.output=function(e){for(var t,Y,Z,n,L="";e;)if(n=this.rules.escape.exec(e))e=e.substring(n[0].length),L+=n[1];else if(n=this.rules.autolink.exec(e))e=e.substring(n[0].length),"@"===n[2]?(Y=J(this.mangle(n[1])),Z="mailto:"+Y):(Y=J(n[1]),Z=Y),L+=this.renderer.link(Z,null,Y);else if(this.inLink||!(n=this.rules.url.exec(e))){if(n=this.rules.tag.exec(e))!this.inLink&&/^/i.test(n[0])&&(this.inLink=!1),e=e.substring(n[0].length),L+=this.options.sanitize?this.options.sanitizer?this.options.sanitizer(n[0]):J(n[0]):n[0];else if(n=this.rules.link.exec(e))e=e.substring(n[0].length),this.inLink=!0,L+=this.outputLink(n,{href:n[2],title:n[3]}),this.inLink=!1;else if((n=this.rules.reflink.exec(e))||(n=this.rules.nolink.exec(e))){if(e=e.substring(n[0].length),t=(n[2]||n[1]).replace(/\s+/g," "),!(t=this.links[t.toLowerCase()])||!t.href){L+=n[0].charAt(0),e=n[0].substring(1)+e;continue}this.inLink=!0,L+=this.outputLink(n,t),this.inLink=!1}else if(n=this.rules.strong.exec(e))e=e.substring(n[0].length),L+=this.renderer.strong(this.output(n[2]||n[1]));else if(n=this.rules.em.exec(e))e=e.substring(n[0].length),L+=this.renderer.em(this.output(n[2]||n[1]));else if(n=this.rules.code.exec(e))e=e.substring(n[0].length),L+=this.renderer.codespan(J(n[2].trim(),!0));else if(n=this.rules.br.exec(e))e=e.substring(n[0].length),L+=this.renderer.br();else if(n=this.rules.del.exec(e))e=e.substring(n[0].length),L+=this.renderer.del(this.output(n[1]));else if(n=this.rules.text.exec(e))e=e.substring(n[0].length),L+=this.renderer.text(J(this.smartypants(n[0])));else if(e)throw new Error("Infinite loop on byte: "+e.charCodeAt(0))}else n[0]=this.rules._backpedal.exec(n[0])[0],e=e.substring(n[0].length),"@"===n[2]?(Y=J(n[0]),Z="mailto:"+Y):(Y=J(n[0]),Z="www."===n[1]?"http://"+Y:Y),L+=this.renderer.link(Z,null,Y);return L},Z.prototype.outputLink=function(e,t){var Y=J(t.href),Z=t.title?J(t.title):null;return"!"!==e[0].charAt(0)?this.renderer.link(Y,Z,this.output(e[1])):this.renderer.image(Y,Z,J(e[1]))},Z.prototype.smartypants=function(e){return this.options.smartypants?e.replace(/---/g,"—").replace(/--/g,"–").replace(/(^|[-\u2014\/(\[{"\s])'/g,"$1‘").replace(/'/g,"’").replace(/(^|[-\u2014\/(\[{\u2018\s])"/g,"$1“").replace(/"/g,"”").replace(/\.{3}/g,"…"):e},Z.prototype.mangle=function(e){if(!this.options.mangle)return e;for(var t,Y="",Z=e.length,n=0;n.5&&(t="x"+t.toString(16)),Y+=""+t+";";return Y},n.prototype.code=function(e,t,Y){if(this.options.highlight){var Z=this.options.highlight(e,t);null!=Z&&Z!==e&&(Y=!0,e=Z)}return t?''+(Y?e:J(e,!0))+"\n \n":""+(Y?e:J(e,!0))+"\n "},n.prototype.blockquote=function(e){return"\n"+e+" \n"},n.prototype.html=function(e){return e},n.prototype.heading=function(e,t,Y){return"\n"},n.prototype.hr=function(){return this.options.xhtml?" \n":" \n"},n.prototype.list=function(e,t,Y){var Z=t?"ol":"ul";return"<"+Z+(t&&1!==Y?' start="'+Y+'"':"")+">\n"+e+""+Z+">\n"},n.prototype.listitem=function(e){return""+e+" \n"},n.prototype.paragraph=function(e){return""+e+"
\n"},n.prototype.table=function(e,t){return"\n"},n.prototype.tablerow=function(e){return"\n"+e+" \n"},n.prototype.tablecell=function(e,t){var Y=t.header?"th":"td";return(t.align?"<"+Y+' style="text-align:'+t.align+'">':"<"+Y+">")+e+""+Y+">\n"},n.prototype.strong=function(e){return""+e+" "},n.prototype.em=function(e){return""+e+" "},n.prototype.codespan=function(e){return""+e+""},n.prototype.br=function(){return this.options.xhtml?" ":" "},n.prototype.del=function(e){return""+e+""},n.prototype.link=function(e,t,Y){if(this.options.sanitize){try{var Z=decodeURIComponent(S(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(e){return Y}if(0===Z.indexOf("javascript:")||0===Z.indexOf("vbscript:")||0===Z.indexOf("data:"))return Y}this.options.baseUrl&&!H.test(e)&&(e=i(this.options.baseUrl,e));var n='"+Y+" "},n.prototype.image=function(e,t,Y){this.options.baseUrl&&!H.test(e)&&(e=i(this.options.baseUrl,e));var Z=' ":">"},n.prototype.text=function(e){return e},L.prototype.strong=L.prototype.em=L.prototype.codespan=L.prototype.del=L.prototype.text=function(e){return e},L.prototype.link=L.prototype.image=function(e,t,Y){return""+Y},L.prototype.br=function(){return""},X.parse=function(e,t){return new X(t).parse(e)},X.prototype.parse=function(e){this.inline=new Z(e.links,this.options),this.inlineText=new Z(e.links,o({},this.options,{renderer:new L})),this.tokens=e.reverse();for(var t="";this.next();)t+=this.tok();return t},X.prototype.next=function(){return this.token=this.tokens.pop()},X.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},X.prototype.parseText=function(){for(var e=this.token.text;"text"===this.peek().type;)e+="\n"+this.next().text;return this.inline.output(e)},X.prototype.tok=function(){switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,S(this.inlineText.output(this.token.text)));case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var e,t,Y,Z,n="",L="";for(Y="",e=0;e=19968&&X<=40869&&(J=i.firstletter.all.charAt(X-19968),t&&(J=i.firstletter.polyphone[X]||J)),Y.push(J)}return t?Z(Y,"",""):Y.join("")}var S=this.getPinyin(e," ",!1,t);S=S instanceof Array?S:[S];for(var Y=[],n=0;n 引用内容")])]),e._v(" "),Y("i-col",{attrs:{span:"14"}},[Y("div",[Y("strong",[e._v("结果")])]),e._v(" "),Y("div",[e._v("H1")]),e._v(" "),Y("div",[e._v("H2")]),e._v(" "),Y("div",[Y("strong",[e._v("粗体")])]),e._v(" "),Y("div",[Y("i",[e._v("斜体")])]),e._v(" "),Y("div",[Y("a",{attrs:{href:"javascript:void(0)"}},[e._v("链接")])]),e._v(" "),Y("div",[Y("code",[e._v("Inline Code")])]),e._v(" "),Y("div",[Y("code",[e._v("Code")])]),e._v(" "),Y("div",[e._v("图片")]),e._v(" "),Y("div",[Y("ul",[Y("li",[e._v("无序列表")])])]),e._v(" "),Y("div",[Y("ol",[Y("li",[e._v("有序列表")])])]),e._v(" "),Y("div",[Y("blockquote",[e._v("引用内容")])])])],1),e._v(" "),Y("a",{attrs:{href:"http://wowubuntu.com/markdown/",target:"_blank"}},[e._v("更多语法")])],1),e._v(" "),Y("Modal",{attrs:{closable:!1,"mask-closable":!1,width:"100","class-name":"i-editor-fullscreen","footer-hide":"","transition-names":["",""]},model:{value:e.showDiff,callback:function(t){e.showDiff=t},expression:"showDiff"}},[e.showDiff?Y("div",{staticClass:"i-editor-fullscreen-container"},[Y("div",{staticClass:"i-editor-fullscreen-header",attrs:{slot:"header"},slot:"header"},[Y("p",[e._v("全屏编辑")]),e._v(" "),Y("div",{staticClass:"i-editor-fullscreen-header-tip"},[Y("Upload",{attrs:{config:e.config,"before-upload":e.beforeUpload,styles:1},on:{"on-success":e.handleUploadSuccess}}),e._v(" "),Y("Button",{staticClass:"i-editor-item",attrs:{type:"text",size:"small"},on:{click:function(t){e.showDiff=!1}}},[Y("Tooltip",{attrs:{content:"退出全屏",transfer:""}},[Y("Icon",{attrs:{type:"md-contract"}})],1)],1)],1)]),e._v(" "),Y("div",{staticClass:"i-editor-fullscreen-main"},[Y("row",{attrs:{gutter:32}},[Y("i-col",{attrs:{span:"12"}},[e.showDiffEditor?Y("Upload",{attrs:{paste:e.paste,config:e.config,"before-upload":e.beforeUpload,type:"drag",styles:3},on:{"on-success":e.handleUploadSuccess},nativeOn:{click:function(e){e.preventDefault(),e.stopPropagation()}}},[Y("Input",{ref:"content",attrs:{placeholder:e.placeholder,type:"textarea",autosize:e.autosize},model:{value:e.content,callback:function(t){e.content=t},expression:"content"}})],1):e._e()],1),e._v(" "),Y("i-col",{attrs:{span:"12"}},[Y("Markdown",{attrs:{content:e.content,highlight:e.highlight}})],1)],1)],1),e._v(" "),Y("div",{staticClass:"i-editor-fullscreen-tail"})]):e._e()])],1)},n=[],L={render:Z,staticRenderFns:n};t.a=L}])});
//# sourceMappingURL=iview-editor.js.map
================================================
FILE: index.html
================================================
iView Editor
================================================
FILE: package.json
================================================
{
"name": "iview-editor",
"version": "1.1.1",
"description": "markdown editor powered by iView UI",
"private": false,
"main": "./dist/iview-editor.js",
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
"dev-style": "gulp --gulpfile build/build-dev.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iview/iview-editor.git"
},
"author": "Aresn",
"license": "MIT",
"bugs": {
"url": "https://github.com/iview/iview-editor/issues"
},
"homepage": "https://github.com/iview/iview-editor#readme",
"dependencies": {
"iview": "^2.11.0",
"vue": "^2.5.16",
"highlightjs": "^9.10.0",
"marked": "^0.3.19"
},
"devDependencies": {
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"extract-text-webpack-plugin": "^3.0.2",
"cross-env": "^5.0.5",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"style-loader": "^0.19.1",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.9.1",
"gulp": "^3.9.1",
"gulp-clean-css": "^3.9.3",
"gulp-less": "^4.0.0",
"gulp-rename": "^1.2.2",
"gulp-util": "^3.0.8"
},
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}
================================================
FILE: src/index.js
================================================
import iEditor from './lib/components/editor.vue';
const iviewEditor = {
iEditor
};
const install = function(Vue, opts = {}) {
Vue.component('i-editor', iEditor);
};
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default Object.assign(iviewEditor, { install });
================================================
FILE: src/lib/components/editor.vue
================================================
Markdown
# 标题
## 标题
**粗体**
*斜体*
[描述](http://)
`code`
```code```

- item
1. item
> 引用内容
结果
H1
H2
粗体
斜体
Inline Code
Code
图片
更多语法
================================================
FILE: src/lib/components/md.vue
================================================
================================================
FILE: src/lib/components/upload.vue
================================================
({{ uploadPercent }}%)
================================================
FILE: src/lib/style/iview-editor.less
================================================
.hljs {
display: block;
overflow-x: auto;
color: #525252;
padding: 15px;
-webkit-text-size-adjust: none;
}
.hljs-doctype{
color: #999;
}
.hljs-tag, .css .hljs-selector-tag{
color: #3e76f6;
}
.hljs-attribute, .hljs-attr {
color: #e96900;
}
.hljs-value{
color: #42b983;
}
.hljs-keyword{
color: #e96900;
}
.hljs-string{
color: #42b983;
}
.hljs-comment{
color: #b3b3b3;
}
.hljs-operator .hljs-comment{
color: #525252;
}
.hljs-regexp{
color: #af7dff;
}
.hljs-built_in{
color: #2db7f5;
}
.css .hljs-class, .css .hljs-selector-class{
color: #e96900;
}
.css .hljs-number, .javascript .hljs-number{
color: #fc1e70;
}
.css .hljs-attribute{
color: #af7dff;
}
.css .hljs-important{
color: #f00;
}
.javascript .hljs-literal, .actionscript .hljs-literal{
color: #fc1e70;
}
@common-padding: 12px;
.i-editor{
&-md{
font-size: 14px;
color: #515a6e;
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
margin: 1em 0;
color: #17233d;
}
hr {
height: 0.05em;
border: 0;
color: #CCCCCC;
background-color: #CCCCCC;
}
blockquote{
margin: 1em 0;
border-left: 4px solid #ddd;
padding: 0 1em;
color: #666;
}
pre {
font-size: 14px;
padding: 16px;
overflow: auto;
line-height: 1.45;
background-color: #f6f8fa;
border-radius: 3px;
}
code {
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
background-color: rgba(27,31,35,0.05);
border-radius: 3px;
font-family: "SFMono-Regular",Consolas,"Liberation Mono",Menlo,Courier,monospace;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
background-color: #f6f8fa;
font-size: 100%;
}
a, a:visited {
color: #4183C4;
background-color: inherit;
text-decoration: none;
}
img {
max-width: 100%;
}
p, div{
line-height: 1.7em;
}
ul, ol{
padding-left: 2em;
}
table{
display: block;
width: 100%;
overflow: hidden;
border-spacing: 0;
border-collapse: collapse;
tr{
background-color: #fff;
border-top: 1px solid #c6cbd1;
&:nth-child(2n) {
background-color: #f6f8fa;
}
}
th, td{
padding: 6px 13px;
border: 1px solid #dfe2e5;
}
th{
font-weight: 600;
}
}
p, blockquote, ul, ol, dl, table, pre{
margin-top: 0;
margin-bottom: .72em;
}
&-cover{
width: 100%;
img {
max-width: 100%;
}
}
}
padding-bottom: @common-padding;
&-tabs{
padding-top: 5px;
padding-bottom: 10px;
background: #fff;
.ivu-tabs-bar{
margin-bottom: 0;
border-bottom: 1px solid #eeeff2;
}
}
&-md{
margin-top: 6px;
&-tip{
div{
margin-bottom: 6px;
font-size: 14px;
}
code{
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
background-color: rgba(27,31,35,0.05);
border-radius: 3px;
font-family: "SFMono-Regular",Consolas,"Liberation Mono",Menlo,Courier,monospace;
}
blockquote{
margin: 1em 0;
border-left: 4px solid #ddd;
padding: 0 1em;
color: #666;
}
}
}
&-wrapper{
padding: 0 @common-padding 4px;
.ivu-upload-drag{
border: none;
overflow: visible;
}
.ivu-upload-dragOver .ivu-input{
border: 1px dashed #2d8cf0;
}
}
&-fullscreen{
min-height: 100%;
.ivu-modal{
top: 0;
height: 100%;
}
.ivu-modal-content{
min-height: 100%;
border-radius: 0;
}
&-header{
width: 100%;
height: 24px;
position: fixed;
top: 0;
left: 0;
border-bottom: 1px solid #e8eaec;
z-index: 1;
background: #fff;
p{
display: inline-block;
float: left;
height: 24px;
line-height: 24px;
padding-left: 16px;
}
&-tip{
float: right;
margin-right: 30px;
.ivu-upload{
display: inline-block;
}
}
}
&-main{
margin-top: 30px;
.ivu-upload-drag{
border: none;
}
.ivu-input{
border: none;
resize: none;
}
}
&-tail{
width: 1px;
height: 100%;
background: #e8eaec;
position: fixed;
top: 0;
left: 50%;
}
}
&-upload{
height: 36px;
line-height: 36px;
padding-right: 12px;
.ivu-upload{
display: inline-block;
}
&-item{
color: inherit;
i{
font-size: 16px;
vertical-align: middle;
}
}
&-cover{
background-size: cover;
background-position: center;
}
}
}
================================================
FILE: src/lib/util/index.js
================================================
let util = {};
export default util;
================================================
FILE: src/lib/util/insertText.js
================================================
/**
* 在指定光标位置插入内容
* @param obj
* @param str
*/
export default function (obj, str) {
if (document.selection) {
const sel = document.selection.createRange();
sel.text = str;
} else if ( typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number' ) {
let startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
================================================
FILE: src/lib/util/pinyin/pinyinUtil.js
================================================
import pinyin_dict_firstletter from './pinyin_dict_firstletter';
var toneMap =
{
"ā": "a1",
"á": "a2",
"ǎ": "a3",
"à": "a4",
"ō": "o1",
"ó": "o2",
"ǒ": "o3",
"ò": "o4",
"ē": "e1",
"é": "e2",
"ě": "e3",
"è": "e4",
"ī": "i1",
"í": "i2",
"ǐ": "i3",
"ì": "i4",
"ū": "u1",
"ú": "u2",
"ǔ": "u3",
"ù": "u4",
"ü": "v0",
"ǖ": "v1",
"ǘ": "v2",
"ǚ": "v3",
"ǜ": "v4",
"ń": "n2",
"ň": "n3",
"": "m2"
};
var dict = {}; // 存储所有字典数据
var pinyinUtil =
{
/**
* 解析各种字典文件,所需的字典文件必须在本JS之前导入
*/
parseDict: function()
{
// 如果导入了 pinyin_dict_firstletter.js
if(pinyin_dict_firstletter)
{
dict.firstletter = pinyin_dict_firstletter;
}
// 如果导入了 pinyin_dict_notone.js
if(window.pinyin_dict_notone)
{
dict.notone = {};
dict.py2hz = pinyin_dict_notone; // 拼音转汉字
for(var i in pinyin_dict_notone)
{
var temp = pinyin_dict_notone[i];
for(var j=0, len=temp.length; j 汉字
if(window.pinyin_dict_notone)
{
// 对于拼音转汉字,我们优先使用pinyin_dict_notone字典文件
// 因为这个字典文件不包含生僻字,且已按照汉字使用频率排序
dict.py2hz = pinyin_dict_notone; // 拼音转汉字
}
else
{
// 将字典文件解析成拼音->汉字的结构
// 与先分割后逐个去掉声调相比,先一次性全部去掉声调然后再分割速度至少快了3倍,前者大约需要120毫秒,后者大约只需要30毫秒(Chrome下)
var notone = pinyinUtil.removeTone(pinyin_dict_withtone).split(',');
var py2hz = {}, py, hz;
for(var i=0, len = notone.length; i= 19968 && unicode <= 40869)
{
ch = dict.firstletter.all.charAt(unicode-19968);
if(polyphone) ch = dict.firstletter.polyphone[unicode] || ch;
}
result.push(ch);
}
if(!polyphone) return result.join(''); // 如果不用管多音字,直接将数组拼接成字符串
else return handlePolyphone(result, '', ''); // 处理多音字,此时的result类似于:['D', 'ZC', 'F']
}
else
{
var py = this.getPinyin(str, ' ', false, polyphone);
py = py instanceof Array ? py : [py];
var result = [];
for(var i=0; i