1.0.6 • Published 5 years ago

f-editor v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

f-editor

一个基于vue的markdown编辑器和渲染器 由mavon-editor 2.7.5版本修改而成。支持服务端渲染。拆分成两个组件:fEditor和fRender。

开始使用

Es6

npm install f-editor  --save // or
yarn add f-editor 

index.js

// 全局注册
    import Vue from 'vue'
    import fMd from 'f-editor'
    import 'f-editor/dist/f-editor.css'
    // use
    Vue.use(fMd)
    new Vue({
        'el': '#app',
        data() {
            return { value: '' }
        }
    })

index.vue

<div>
  <f-editor v-model="value"></f-editor>
  <f-render :value="value"/>
</div>

browser

<div id="app">
  <f-editor v-model="value"></f-editor>
  <f-render :value="value"/>
</div>
<script src="path/to/vue.js"></script>
<script src="path/to/f-editor.umd.js"></script>
<script>
  Vue.use(window['f-editor'])
  new Vue({
    el: '#app',
    data() {
        return { value: '' }
    }
  })
</script>

在nuxt中使用

本插件支持SSR 1. 在工程目录plugins 下新建f-editor.js

import Vue from 'vue'
import fEditor from 'f-editor'
import 'f-editor/dist/f-editor.css'
// use
Vue.use(fEditor)
  1. nuxt.config.js中添加plugins配置
module.exports = {
  // ...
  plugins: [
    // ...
    { src: '@/plugins/f-editor', ssr: true }
  ],
}
  1. 在页面中使用
<template>
  <div>
      <f-editor v-model="content"/>
  </div>
</template>
<script>
export default {
  data() {
    return {
      content: "#### how to use f-editor in nuxt.js"
    };
  }
};
</script>

API文档

props

name 名称type 类型default默认值applicable component适用组件describe 描述
valueStringfEditor, fRender初始值
useTocBooleantruefEditor, fRender是否使用ftoc
tocOptionObject{}fEditor, fRender前提是设置useToc为true,目录设置,参考ftoc,注意tocCallback已被占用,如有需要,请监听toc事件
forbiddenHashBooleantruefEditor, fRender使用禁用hash跳转,避免触发vue路由切换动画
maxLengthNumber, nulltruefEditor输入长度限制
languageStringzh-CNfEditor语言选择,暂支持 zh-CN: 中文简体 , en: 英文 , fr: 法语, pt-BR: 葡萄牙语, ru: 俄语, de: 德语, ja: 日语
fontSizeString14pxfEditor编辑区域文字大小
scrollStyleBooleantruefEditor, fRender开启滚动条样式(暂时仅支持chrome)
boxShadowBooleantruefEditor开启边框阴影
subfieldBooleantruefEditortrue: 双栏(编辑预览同屏), false: 单栏(编辑预览分屏)
defaultOpenStringfEditoredit: 默认展示编辑区域 , preview: 默认展示预览区域 , 其他 = edit
placeholderString开始编辑...fEditor输入框为空时默认提示文本
editableBooleantruefEditor是否允许编辑
codeStyleStringcode-githubfEditor, fRendermarkdown样式: 默认github, 可选配色方案
toolbarsFlagBooleantruefEditor工具栏是否显示
navigationBooleanfalsefEditor默认展示目录
shortCutBooleantruefEditor是否启用快捷键
autofocusBooleantruefEditor自动聚焦到文本框
ishljsBooleantruefEditor, fRender代码高亮
imageFilterfunctionnullfEditor图片过滤函数,参数为一个File Object,要求返回一个Boolean, true表示文件合法,false表示文件不合法
imageClickfunctionnullfEditor, fRender图片点击事件,默认为预览,可覆盖
tabSizeNumber\tfEditortab转化为几个空格,默认为\t
toolbarsObject如下例fEditor工具栏
sToolbarsObject如下例fEditor小屏幕时的工具栏
/*
    默认工具栏按钮全部开启, 传入自定义对象
    例如: {
         bold: true, // 粗体
         italic: true,// 斜体
         header: true,// 标题
    }
    此时, 仅仅显示此三个功能键
 */
sToolbars:{
  underline: true,
  strikethrough: true,
  imagelink: true,
  table: true,
  undo: true,
  save: true,
  preview: true,
  navigation: true
},
toolbars: {
  bold: true, // 粗体
  italic: true, // 斜体
  header: true, // 标题
  underline: true, // 下划线
  strikethrough: true, // 中划线
  mark: true, // 标记
  superscript: true, // 上角标
  subscript: true, // 下角标
  quote: true, // 引用
  ol: true, // 有序列表
  ul: true, // 无序列表
  link: true, // 链接
  imagelink: true, // 图片链接
  code: true, // code
  table: true, // 表格
  fullscreen: true, // 全屏编辑
  readmodel: true, // 沉浸式阅读
  htmlcode: true, // 展示html源码
  help: true, // 帮助
  undo: true, // 上一步
  redo: true, // 下一步
  trash: true, // 清空
  save: true, // 保存(触发events中的save事件)
  navigation: true, // 导航目录
  alignleft: true, // 左对齐
  aligncenter: true, // 居中
  alignright: true, // 右对齐
  subfield: true, // 单双栏模式
  preview: true, // 预览
  }

events 事件绑定

name 方法名params 参数applicable component适用组件describe 描述
tocString: tocHtmlfEditor, fRender生成目录时触发,参数为生成目录的html,参数修改参考tocOption选项
inputString: valuefEditor编辑区内容发生改变时触发,参数为编辑区内容
renderString: valuefEditor, fRendermarkdown渲染时触发, 参数为渲染结果
saveString: value , String: renderfEditorctrl + s 的回调事件(保存按键,同样触发该回调)
fullScreenBoolean: status , String: valuefEditor切换全屏编辑的回调事件(boolean: 全屏开启状态)
readModelBoolean: status , String: valuefEditor切换沉浸式阅读的回调事件(boolean: 阅读开启状态)
htmlCodeBoolean: status , String: valuefEditor查看html源码的回调事件(boolean: 源码开启状态)
subfieldToggleBoolean: status , String: valuefEditor切换单双栏编辑的回调事件(boolean: 双栏开启状态)
previewToggleBoolean: status , String: valuefEditor切换预览编辑的回调事件(boolean: 预览开启状态)
helpToggleBoolean: status , String: valuefEditor查看帮助的回调事件(boolean: 帮助开启状态)
navigationToggleBoolean: status , String: valuefEditor切换导航目录的回调事件(boolean: 导航开启状态)
imgAddString: filename, File: imgfile, VueInstance: $vmfEditor图片文件添加回调事件(filename: 写在md中的文件名, File: File Object, $vm 编辑器实例)

slot插槽

只适用于fEditor

name 插槽名称describe 描述
left-toolbar-before左侧工具栏前方
left-toolbar-after左侧工具栏后方
right-toolbar-before右侧工具栏前方
right-toolbar-after右侧工具栏后方

快捷键

只适用于fEditor

keykeycode功能
F8119开启/关闭导航
F9120预览/编辑切换
F10121开启/关闭全屏
F11122开启/关闭阅读模式
F12123单栏/双栏切换
TAB9缩进
CTRL + S17 + 83触发保存
CTRL + D17 + 68删除选中行
CTRL + Z17 + 90上一步
CTRL + Y17 + 89下一步
CTRL + BreakSpace17 + 8清空编辑
CTRL + B17 + 66加粗
CTRL + I17 + 73斜体
CTRL + H17 + 72# 标题
CTRL + 117 + 97 or 49# 标题
CTRL + 217 + 98 or 50## 标题
CTRL + 317 + 99 or 51### 标题
CTRL + 417 + 100 or 52#### 标题
CTRL + 517 + 101 or 53##### 标题
CTRL + 617 + 102 or 54###### 标题
CTRL + U17 + 85++下划线++
CTRL + M17 + 77==标记==
CTRL + Q17 + 81> 引用
CTRL + O17 + 791. 有序列表
CTRL + L17 + 76链接()
CTRL + ALT + S17 + 18 + 83^上角标^
CTRL + ALT + U17 + 18 + 85- 无序列表
CTRL + ALT + C17 + 18 + 67` 代码块
CTRL + ALT + L17 + 18 + 76!图片链接()
CTRL + ALT + T17 + 18 + 84表格
CTRL + SHIFT + S17 + 16 + 83~下角标~
CTRL + SHIFT + D17 + 16 + 68中划线
CTRL + SHIFT + C17 + 16 + 67居中
CTRL + SHIFT + L17 + 16 + 76居左
CTRL + SHIFT + R17 + 16 + 82居右
SHIFT + TAB16 + 9取消缩进

图片上传

<template>
  <f-editor @imgAdd="handleImgAdd" v-model="content"></f-editor>
</template>
<script>
  export default {
    data() {
      return {
        content: ''
      }
    },
    methods: {
      handleImgAdd(pos, file, vm) {
        vm.$img2Url(pos, file.miniurl) // 直接上传base64
      }
    }
  }
</script>

常见问题

  1. 图片上传问题 如果要使用图片功能,必须要处理imgAdd事件,在获取到图片url时,要调用vm.$img2Url(pos, url)将图片地址插入文档中。
  2. 目录中文 要在目录中使用中文,并想作为hash跳转,可使用以下方法
    <template>
      <f-editor :toc-option="tocOption"></f-editor>
    </template>
    <script>
      // 需要先安装uslug依赖
      import uslug from 'uslug'
       export default{
         data() {
           return {
             tocOption: {
               slugify: uslug
             },
           }
         }
       }
    </script>
  3. hash跳转触发vue路由切换动画 可以设置forbiddenHashtrue,原理是使用scrollIntoView
  4. 获取文档目录,请监听toc事件

Markdown 语法拓展

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago