富文本編輯器之——CKEditor

CKEditor4

1. 在index.html中直接引入,注意這裏引入的是 CKEditor 4,

<script src="https://cdn.ckeditor.com/4.12.1/standard/ckeditor.js"></script>

2. 在vue.config.js文件中配置

module.exports = {
  configureWebpack: {
    externals: {
      'CKEDITOR': 'window.CKEDITOR'
    }
  }
}

3. 在組件中使用

<template>
    <div class="ckeditor-container">
        <el-row type="flex" justify="center">
            <el-col :span="8" style="text-align:center">
                <h1>CKEditor Demo</h1>
            </el-col>
        </el-row>
        <el-row type="flex" justify="center">
            <el-col :span="16" >
                <textarea name="lmtEditor" id="lmtEditor"></textarea>
            </el-col>
        </el-row>
        <el-row type="flex" justify="center" style="padding-top:26px;">
            <el-col :span="3" style="text-align:center;">
               <el-button type="warning"  size="large" @click="cleanEditor">重置</el-button>
            </el-col>
            <el-col :span="1"></el-col>
            <el-col :span="3">
               <el-button type="primary"  size="large" @click="copyData">複製</el-button>
            </el-col>
            <el-col :span="1"></el-col>
            <el-col :span="3" style="text-align:center;">
                <el-button type="info"  size="large" @click="saveEditor">保存</el-button>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import CKEDITOR from 'CKEDITOR'
import Vue from 'vue'
import VueClipborad from 'vue-clipboard2'
Vue.use(VueClipborad)
export default {
  name: 'cke',
  data () {
    return {
      editorData: ''
    }
  },
  mounted () {
    this.initCKEditor()
  },
  methods: {
    initCKEditor () {
      CKEDITOR.replace('lmtEditor', {
        langguage: 'zh-ch',
        filebrowserWindowWidth: '800',
        filebrowserWindowHeight: '600',
        height: 600,
        format_tags: 'p;h1;h2;h3;h4;h5;h6;pre;address;div'
      })
    },
    cleanEditor () {
      CKEDITOR.instances.lmtEditor.setData('')
    },
    saveEditor () {
      this.editorData = CKEDITOR.instances.lmtEditor.getData()
    },
    copyData () {
      this.editorData = CKEDITOR.instances.lmtEditor.getData()
      this.$copyText(this.editorData).then(e => {
        alert('複製成功')
      }, e => {
        alert('複製失敗')
        console.log(e, '失敗原因')
      })
    }
 
  }
}
</script>
<style lang="scss">
.ckeditor-container{
    color: red;
}
</style> 

CKEditor效果如圖:
在這裏插入圖片描述
注意:
項目中安裝了Elementvue-clipboard2

安裝並使用 vue-clipboard2vue 剪切板功能, 將文本複製到剪切板需要它 )

1). 安裝

 yarn add vue-clipboard2 或者npm install --save vue-clipboard2 

2). 在main.js中引入:

import VueClipboard from "vue-clipboard2";
import Vue from "vue";
Vue.use(VueClipboard);

3). 使用:

let container = this.$refs.container 
this.$copyText("Text to copy", container) 

參考:https://github.com/Inndy/vue-clipboard2

對於CKEditor4
要檢索編輯器數據,請調用編輯器實例的CKEDITOR.editor.getData方法。對於ID爲的編輯器實例editor1,它將類似於以下內容:

<script>
    var data = CKEDITOR.instances.editor1.getData();
</script>
  1. 定義一個textarea,並定義好id名爲如’lmtEditor’, CKEDITOR.replace('lmtEditor',{寫初始化內容,如language:zh-cn})
  2. CKEDITOR.instances.lmtEditor.setData('')可設置富文本編輯器裏的內容爲空,即清除,
  3. CKEDITOR.instances.lmtEditor.getData()可獲取編輯器裏面的內容

參考官網:https://ckeditor.com/docs/ckeditor4/latest/guide/dev_savedata.html#retrieving-data-from-ckeditor

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章