vue3中使用editor.js的詳細步驟記錄
第一步安裝依賴
npm i @editorjs/editorjs --save
第二步創(chuàng)建editor.vue插件
<template>
<div>
<div id="editorjs" :style="'width:' + props.width + 'px;height:' + props.height + 'px;'"></div>
</div>
</template>
<script setup>
// 標題(Header):用于設(shè)置文本的標題級別,支持多級標題。
// 段落(Paragraph):用于添加普通文本段落。
// 引用(Quote):用于添加引用文本塊。
// 列表(List):支持有序列表和無序列表。
// 圖像(Image):可以插入圖片,并指定圖片的來源、標題和描述等屬性。
// 插入鏈接(Link):可以插入超鏈接,指定鏈接的URL、標題和打開方式等。
// 視頻(Embed):可以插入媒體內(nèi)容的外部嵌入代碼或鏈接。
// 表格(Table):可以創(chuàng)建簡單的表格,指定表格行數(shù)和列數(shù)。
// 代碼(Code):用于插入代碼塊,支持多種編程語言的語法高亮。
// 內(nèi)容警告(Warning):用于突出顯示重要提示或警告。
// 勾選框(Checklist):用于創(chuàng)建待辦事項列表,可以勾選完成狀態(tài)。
// 分割線(Delimiter):用于在內(nèi)容中插入水平分割線。
import EditorJS from '@editorjs/editorjs';
import Header from '@editorjs/header';
import List from '@editorjs/list';
import Paragraph from '@editorjs/paragraph';
import Quote from '@editorjs/quote';
import Image from '@editorjs/image';
import Embed from '@editorjs/embed';
import Table from '@editorjs/table';
import Code from '@editorjs/code';
import Delimiter from '@editorjs/delimiter';
import zh from './i18n.json'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const store = useStore()
const { ctx, proxy } = getCurrentInstance()
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
modelValue: {
type: String,
default: "",
},
width: {
type: Number,
default: 500,
},
height: {
type: Number,
default: 500,
},
})
const editor = ref(null)
const saveEditor = () => {
editor.value.save().then((outputData) => {
console.log(outputData)
}).catch((error) => {
console.log(error)
});
}
const { data } = toRefs(reactive({
//定義數(shù)組和對象
data: ''
}))
onMounted(() => {
editor.value = new EditorJS({
holder: 'editorjs',//應該包含編輯器的元素Id
// autofocus: true,//自動獲取焦點
placeholder: '請輸入內(nèi)容',
i18n: {
messages: zh
},
logLevel: 'VERBOSE',//日志級別 VERBOSE顯示所有消息(默認) INFO顯示信息和調(diào)試消息 WARN只顯示警告消息 ERROR只顯示錯誤信息
readOnly: false,//只讀模式
tools: {//工具列表
header: {
class: Header,
inlineToolbar: ['link']
},
list: {
class: List,
inlineToolbar: true
},
paragraph: {
class: Paragraph,
inlineToolbar: true
},
quote: {
class: Quote,
inlineToolbar: true
},
// image: SimpleImage,
image: {
class: Image,
inlineToolbar: true
},
embed: {
class: Embed,
inlineToolbar: true
},
table: {
class: Table,
inlineToolbar: true
},
code: {
class: Code,
inlineToolbar: true
},
delimiter: {
class: Delimiter,
inlineToolbar: true
},
},
onReady: () => {
console.log('Editor.js is ready to work!')
},
})
console.log(editor.value);
})
// watch(propData,(newVal,oldVal)=>{})
defineExpose({ saveEditor })
</script>
<style scoped></style>創(chuàng)建i18n翻譯文件 i18n.json
{
"ui": {
"blockTunes": {
"toggler": {
"Click to tune": "點擊調(diào)整",
"or drag to move": "或者拖動移動"
}
},
"inlineToolbar": {
"converter": {
"Convert to": "轉(zhuǎn)換為"
}
},
"toolbar": {
"toolbox": {
"Add": "添加"
}
}
},
"toolNames": {
"Text": "文本",
"Heading": "標題",
"List": "列表",
"Warning": "警告",
"Checklist": "清單",
"Quote": "引用",
"Code": "代碼",
"Delimiter": "分隔符",
"Raw HTML": "原始HTML",
"Table": "表格",
"Link": "鏈接",
"Marker": "標記",
"Bold": "加粗",
"Italic": "斜體",
"Image": "圖片"
},
"tools": {
"warning": {
"Title": "標題",
"Message": "消息"
},
"link": {
"Add a link": "添加鏈接"
},
"stub": {
"The block can not be displayed correctly.": "該塊不能正確顯示"
}
},
"blockTunes": {
"delete": {
"Delete": "刪除"
},
"moveUp": {
"Move up": "向上移動"
},
"moveDown": {
"Move down": "向下移動"
}
}
}在頁面引入組件
<template>
<div class="editorBox">
<editorJs ref="editorRef" :width="700" :height="1000" v-model="data"></editorJs>
<el-button @click="save">保存</el-button>
</div>
</template>
<script setup>
import editorJs from '@/components/Editor/editorJs'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const store = useStore()
const { ctx, proxy } = getCurrentInstance()
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
propData: {
type: String,
default: '',
},
})
const { data } = toRefs(reactive({
//定義數(shù)組和對象
data: ''
}))
const editorRef = ref(null)//定義普通類型
function save() {
editorRef.value.saveEditor()
}
// onMounted(() => {})
// watch(propData,(newVal,oldVal)=>{})
</script>
<style scoped>
.editorBox {
padding: 40px;
width: 700px;
background-color: #fff;
margin: 50px auto auto;
box-shadow: 0 1px 4px rgba(0, 0, 0, .04), 0 4px 10px rgba(0, 0, 0, .08);
}
</style>這只是一個簡單的示例,具體配置請去官網(wǎng)Editor.js (editorjs.io)
Editor.js 在其核心設(shè)計中不直接包含一個傳統(tǒng)的“頂部工具欄”。它的設(shè)計理念是簡潔、無干擾的用戶體驗,焦點是在“塊”內(nèi)容上,而非傳統(tǒng)的富文本編輯器的樣式。因此,Editor.js 的默認設(shè)置下,不會顯示類似 Word 或其他傳統(tǒng)編輯器的頂部工具欄。
說一下我的心得:
官方文檔對中文及其不友好,不建議使用這個編輯器,除非你有大量時間來開發(fā)研究
Editor.js 是一個用于構(gòu)建具有完全可定制化塊結(jié)構(gòu)的現(xiàn)代編輯器的開源庫。它提供了一個簡潔、可擴展和易于使用的接口,使開發(fā)人員能夠創(chuàng)建擁有豐富內(nèi)容和互動性的編輯器。
以下是一些 Editor.js 的特點和優(yōu)勢:
塊結(jié)構(gòu):Editor.js 采用了塊結(jié)構(gòu)的概念,將內(nèi)容分解為可獨立操作和樣式化的不同塊,如段落、標題、圖像、列表等。這種結(jié)構(gòu)使得對內(nèi)容的編輯和擴展變得更加直觀和靈活。
嵌套塊:不僅可以創(chuàng)建單一塊的內(nèi)容,還可以在塊內(nèi)部創(chuàng)建嵌套結(jié)構(gòu),構(gòu)建復雜的內(nèi)容組合。例如,你可以在段落塊中嵌套標題、列表或引用塊等。
簡潔的用戶界面:Editor.js 的用戶界面被設(shè)計成簡潔、直觀且易于使用。用戶可以通過簡單的操作來創(chuàng)建、編輯和刪除塊,不需要關(guān)注復雜的標記語法或樣式細節(jié)。
開放的插件系統(tǒng):Editor.js 提供了一個強大的插件系統(tǒng),允許開發(fā)人員創(chuàng)建自定義塊類型和擴展編輯器功能。你可以根據(jù)需要編寫自己的塊插件,并將其集成到編輯器中,以滿足特定的編輯需求。
豐富的內(nèi)容保存:Editor.js 使用 JSON 格式保存編輯器的內(nèi)容。這種格式簡單易懂,便于存儲和傳輸,同時保留了塊結(jié)構(gòu)和樣式的信息。你可以將編輯器的內(nèi)容保存到數(shù)據(jù)庫中,然后在需要時重新加載。
可擴展的主題和樣式:Editor.js 的外觀和樣式可以根據(jù)你的品牌和設(shè)計需求進行定制。你可以創(chuàng)建自己的主題或使用現(xiàn)有的主題來改變編輯器的外觀。
多語言支持:Editor.js 支持多種語言,可以輕松地切換編輯器的顯示語言或自定義特定術(shù)語的翻譯。
總而言之,Editor.js 是一個功能強大、靈活且易于使用的編輯器庫,適用于構(gòu)建各種應用程序中的富文本編輯功能,如內(nèi)容管理系統(tǒng)、博客平臺、電子商務平臺等。它提供了現(xiàn)代化的編輯體驗,同時讓開發(fā)人員能夠自由定制編輯器以滿足個性化需求。
總結(jié)
到此這篇關(guān)于vue3中使用editor.js的文章就介紹到這了,更多相關(guān)vue3使用editor.js內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3新屬性之css中使用v-bind的方法(v-bind?in?css)
這篇文章主要介紹了Vue3新屬性css中使用v-bind(v-bind?in?css)的方法,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01
詳解如何在Vue2中實現(xiàn)useDraggable
這篇文章主要為大家詳細介紹了Vue2中實現(xiàn)useDraggable的相關(guān)知識,文中的示例代碼簡潔易懂,對我們深入了解vue有一定的幫助,需要的小伙伴可以參考下2023-12-12
vue-cli2,vue-cli3,vite?生產(chǎn)環(huán)境去掉console.log
console.log一般都是在開發(fā)環(huán)境下使用的,在生產(chǎn)環(huán)境下需要去除?,本文主要介紹了vue-cli2,vue-cli3,vite?生產(chǎn)環(huán)境去掉console.log,具有一定的參考價值,感興趣的可以了解一下2024-05-05
Vue?Router路由hash模式與history模式詳細介紹
Vue?Router是Vue.js官方的路由管理器。它和Vue.js的核心深度集成,讓構(gòu)建單頁面應用變得易如反掌。路由實際上就是可以理解為指向,就是我在頁面上點擊一個按鈕需要跳轉(zhuǎn)到對應的頁面,這就是路由跳轉(zhuǎn)2022-08-08
VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實現(xiàn)方法,需要的朋友參考下2018-02-02

