fix editor

This commit is contained in:
AceDataCloud committed 2021-09-06 00:54:49 +08:00
1 parent d9d2c59295
commit ac450b0e04
12 files changed
+132 -61

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
<template>
<el-alert :title="content" v-if="show" type="success" center :closable="false" show-icon class="alert-succcess-info">
</el-alert>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'SuccessInfo',
props: {
show: {
type: Boolean,
default: false
},
content: {
type: String,
default: ''
}
},
watch: {
show(val, oldVal) {
if (val) {
setTimeout(() => {
this.$emit('update:show', false);
}, 2000);
}
}
}
});
</script>
<style lang="scss">
.alert-succcess-info {
position: fixed;
top: 110px;
left: 50%;
transform: translate(-50%, 0);
z-index: 1000;
width: 10%;
}
</style>
-1
View File
@@ -25,7 +25,6 @@ export default defineComponent({
},
computed: {
active() {
console.log('this.$route', this.$route);
return this.$route.matched[0].path;
}
},
+2 -1
View File
@@ -1,4 +1,5 @@
import TopHeader from './TopHeader.vue';
import Breadcrumb from './Breadcrumb.vue';
import Loading from './Loading.vue';
export { TopHeader, Breadcrumb, Loading };
import SuccessInfo from './SuccessInfo.vue';
export { TopHeader, Breadcrumb, Loading, SuccessInfo };
+5 -1
View File
@@ -1,7 +1,11 @@
import nav from './nav';
import editor from './editor';
import message from './message';
import text from './text';
export default {
nav,
editor
editor,
message,
text
};
+3
View File
@@ -0,0 +1,3 @@
export default {
saved: '保存成功',
};
+3
View File
@@ -0,0 +1,3 @@
export default {
notitle: '无标题'
};
-1
View File
@@ -40,7 +40,6 @@ export default defineComponent({
name: 'LayoutContent',
computed: {
showSidebar(): boolean {
console.log('router', this.$route.name);
return !this.isDetailRoute();
},
showFullHeight(): boolean {
-39
View File
@@ -1,39 +0,0 @@
<template>
<el-container>
<el-header>
<top-header />
</el-header>
<el-container>
<el-main>
<router-view />
</el-main>
</el-container>
</el-container>
</template>
<script lang="ts">
import Sidebar from '@/components/content/Sidebar.vue';
import { TopHeader } from '@/components/common/index';
export default {
components: {
TopHeader,
Sidebar
},
name: 'LayoutEditor'
};
</script>
<style lang="scss">
#app {
height: initial;
}
</style>
<style lang="scss" scoped>
.el-main {
display: initial;
flex: initial;
padding: 0;
margin: 0;
width: 100%;
}
</style>
+2 -2
View File
@@ -1,4 +1,4 @@
import LayoutHome from '@/layouts/Home.vue';
import LayoutEditor from '@/layouts/Content.vue';
import LayoutContent from '@/layouts/Content.vue';
export { LayoutHome, LayoutEditor };
export { LayoutHome, LayoutContent };
+25 -1
View File
@@ -38,6 +38,7 @@ import { renderDomByMd } from './ts/wysiwyg/renderDomByMd';
class Vditor extends VditorMethod {
public readonly version: string;
public initialized = false;
public vditor: IVditor;
/**
@@ -95,6 +96,24 @@ class Vditor extends VditorMethod {
}
}
public getTitle(defaultValue: string = '') {
const markdown = getMarkdown(this.vditor);
const result = /^\s?#\s{0,1}([^\n]*)\n/g.exec(markdown);
if (result && result.length > 1) {
return result[1] || defaultValue;
}
return defaultValue;
}
public getContent(defaultValue: string = '') {
const markdown = getMarkdown(this.vditor);
const result = /^(?:\s?#\s{0,1}[^\n]*\n?)?([\s\S]*)$/g.exec(markdown);
if (result && result.length > 1) {
return result[1] || defaultValue;
}
return defaultValue;
}
/** 获取 Markdown 内容 */
public getValue() {
return getMarkdown(this.vditor);
@@ -255,7 +274,9 @@ class Vditor extends VditorMethod {
}
/** 设置编辑器内容 */
public setValue(markdown: string, clearStack = false) {
public setValue({ title, content }: { title?: string; content?: string }, clearStack = false) {
const markdown = `# ${title || ''}\n${content || ''}`;
if (this.vditor.currentMode === 'sv') {
this.vditor.sv.element.innerHTML = this.vditor.lute.SpinVditorSVDOM(markdown);
processSVAfterRender(this.vditor, {
@@ -456,9 +477,12 @@ class Vditor extends VditorMethod {
initUI(this.vditor);
this.initialized = true;
if (mergedOptions.after) {
mergedOptions.after();
}
if (mergedOptions.icon) {
// 防止初始化 2 个编辑器时加载 2 次
addScriptSync(`${mergedOptions.cdn}/dist/js/icons/${mergedOptions.icon}.js`, 'vditorIconScript');
+44 -9
View File
@@ -3,6 +3,7 @@
<el-col :span="16" :offset="4">
<loading :loading="loading" />
<div class="px-4 pt-4">
<success-info v-model:show="succeed" :content="$t('content.message.saved')"></success-info>
<div id="vditor" class="vditor" />
</div>
</el-col>
@@ -11,7 +12,7 @@
<script lang="ts">
import Vditor from '@/libs/vditor/index';
import { Breadcrumb, Loading } from '@/components/common/index';
import { Breadcrumb, Loading, SuccessInfo } from '@/components/common/index';
import { defaultOptions } from '@/settings/editor';
import '@/libs/vditor/assets/scss/index.scss';
import { defineComponent } from 'vue';
@@ -23,35 +24,50 @@ interface IData {
vditor: null | Vditor;
item: null | IArticle;
loading: boolean;
succeed: boolean;
}
export default defineComponent({
components: {
Breadcrumb,
Loading
Loading,
SuccessInfo
},
data(): IData {
return {
id: this.$route.params.id,
vditor: null,
item: null,
loading: false
loading: false,
succeed: false
};
},
async mounted() {
this.loading = true;
ArticleService.get(this.id).then(({ data: data }: { data: IArticleDetailResponse }): void => {
console.log('data', data);
this.item = data;
this.loading = false;
if (this.vditor && this.item) {
this.vditor.setValue(this.item.content);
if (this.vditor?.initialized && this.item) {
this.vditor.setValue(this.item);
}
});
this.initVditor();
this.listenKeyboard();
},
computed: {},
methods: {
listenKeyboard() {
document.addEventListener(
'keydown',
(e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
this.onSave();
}
},
false
);
},
initVditor() {
const options = {
width: '100%',
@@ -61,11 +77,14 @@ export default defineComponent({
icon: 'ant',
debugger: false,
after: () => {
if (this.vditor?.getValue) {
if (this.vditor?.getValue()) {
return;
}
if (this.item?.content) {
this.vditor?.setValue(this.item.content);
if (!this.item.title) {
this.item.title = this.$t('content.text.notitle');
}
this.vditor?.setValue(this.item);
}
}
};
@@ -73,7 +92,23 @@ export default defineComponent({
...defaultOptions,
...options
});
// this.vditor.focus();
},
onSave() {
const title = this.vditor?.getTitle(this.$t('content.text.notitle'));
const content = this.vditor?.getContent();
ArticleService.update(this.id, {
title: title,
content: content
}).then(({ data: data }: { data: IArticleDetailResponse }): void => {
this.item = data;
if (this.vditor && this.item) {
if (!this.item.title) {
this.item.title = this.$t('content.text.notitle');
}
this.vditor.setValue(this.item);
}
this.succeed = true;
});
}
}
});
+6 -6
View File
@@ -1,10 +1,10 @@
export interface IArticle {
id: number;
title: string;
cover: string;
summary: string;
content: string;
updated_at: string;
id?: number;
title?: string | undefined;
cover?: string | undefined;
summary?: string | undefined;
content?: string | undefined;
updated_at?: string | undefined;
}
export interface IArticleListResponse {