当前位置:   article > 正文

封装 Ace Editor Vue 组件,以及了解发布 npm 包

npm ace

封装的组件,源代码点这里传送

只关注 npm 发包,可以点这里传送

只关注如何给项目增加 badge,可以点这里传送

封装给 vue 用的 ace editor

Ace Editor: https://github.com/ajaxorg/ace

封装依赖包 brace: https://github.com/thlorenz/brace

源码:

  1. const ace = require('brace'),
  2. defaultConfig = { // Editor 默认配置
  3. lang: 'json', // 语言
  4. theme: 'xcode', // 编辑器主题
  5. options: { // Ace Editor 配置
  6. useSoftTabs: true, // 默认 2 个空格缩进
  7. tabSize: 2
  8. }
  9. };
  10. let vm;
  11. module.exports = {
  12. template: '<div :style="{height: height, width: width}"></div>',
  13. computed: {
  14. editorConfig() { // 这里主要是防止 props `config` 中只配置了一部分,无法使用默认值
  15. return Object.assign({}, defaultConfig, this.config);
  16. }
  17. },
  18. props: {
  19. value: {
  20. type: String,
  21. required: true
  22. },
  23. height: true,
  24. width: true,
  25. fontSize: {
  26. type: Number,
  27. default: 12
  28. },
  29. config: {
  30. type: Object,
  31. default: function () {
  32. return defaultConfig
  33. }
  34. }
  35. },
  36. data() {
  37. return {
  38. editor: null
  39. }
  40. },
  41. beforeDestroy: function () {
  42. this.editor.destroy();
  43. this.editor.container.remove();
  44. },
  45. mounted() {
  46. let vm = this,
  47. {
  48. lang,
  49. theme
  50. } = this.editorConfig,
  51. editor;
  52. lang === 'html' && require('brace/ext/emmet');
  53. require('brace/ext/language_tools');
  54. require('brace/mode/' + lang);
  55. require('brace/theme/' + theme);
  56. require('brace/snippets/' + lang);
  57. this.editor = editor = ace.edit(this.$el);
  58. editor.setTheme('ace/theme/' + theme);
  59. editor.setOption("enableEmmet", true);
  60. editor.getSession().setMode('ace/mode/' + lang);
  61. editor.$blockScrolling = Infinity;
  62. editor.setFontSize(this.fontSize);
  63. editor.setValue(this.value);
  64. this.editorConfig.options && editor.setOptions(this.editorConfig.options);
  65. this.$emit('init', editor);
  66. editor.on('change', _ => {
  67. vm.$emit('input', editor.getValue());
  68. });
  69. }
  70. }
  71. 复制代码

npmjs.org注册

npm 发包

  1. 有一个源码项目
  2. 官网注册账号
  3. 执行下面的命令(注意设置要 ignore 的文件,以及完善 package.json 中的信息)
  1. npm adduser
  2. # 用户名 密码
  3. npm publish
  4. # 完成
  5. 复制代码

使用的话就 npm install xxx

badge

从这里获取:http://shields.io/ , 它能提供build(集成、测试等信息)、version(版本)、自定义的徽章,比如:

上面的代码如下:

  1. # 自定义
  2. ![](https://img.shields.io/badge/vue-%3E2.0-brightgreen.svg)
  3. # npm 版本
  4. ![](https://img.shields.io/npm/v/ace-editor-on-vue.svg)
  5. # npm 下载数
  6. ![](https://img.shields.io/npm/dt/ace-editor-on-vue.svg)
  7. # 项目根目录有 LICENSE 文件
  8. ![](https://img.shields.io/github/license/NoName4Me/ace-editor-on-vue.svg)
  9. 复制代码
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/415412
推荐阅读