diff --git a/src/app/view/write/components/publish-form/publish-form.component.html b/src/app/view/write/components/publish-form/publish-form.component.html index 6ede325..b79cf34 100644 --- a/src/app/view/write/components/publish-form/publish-form.component.html +++ b/src/app/view/write/components/publish-form/publish-form.component.html @@ -1,6 +1,6 @@
- + 发布为 @@ -20,37 +20,7 @@ - - - 文章标签 - - - {{t}} - - New Tag - - -
未选择或添加标签 -
- - - - {{ tag.name }} - - - - -
-
文章分类 @@ -83,6 +53,40 @@ + + + 文章标签 + + + {{t}} +
+ + + +
+ + 未选择或添加标签 + + + {{ tag.name }} + + + + +
+
+ diff --git a/src/app/view/write/components/publish-form/publish-form.component.less b/src/app/view/write/components/publish-form/publish-form.component.less index 56366df..a2899de 100644 --- a/src/app/view/write/components/publish-form/publish-form.component.less +++ b/src/app/view/write/components/publish-form/publish-form.component.less @@ -1,4 +1,3 @@ .errTip { color: #f5222d; - line-height: 30px; -} \ No newline at end of file +} diff --git a/src/app/view/write/components/publish-form/publish-form.component.ts b/src/app/view/write/components/publish-form/publish-form.component.ts index b2c4cbd..a86098a 100644 --- a/src/app/view/write/components/publish-form/publish-form.component.ts +++ b/src/app/view/write/components/publish-form/publish-form.component.ts @@ -1,6 +1,7 @@ import {Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core'; import {FormBuilder, FormGroup, Validators} from '@angular/forms'; import {Category, Tag} from '../../../../class/Tag'; +import {ColorList} from '../../../../utils/color' @Component({ selector: 'c-publish-form', @@ -12,17 +13,28 @@ export class PublishFormComponent implements OnInit { constructor(private fb: FormBuilder) { } - @ViewChild('inputElement', {static: false}) tagInputElement: ElementRef; + @ViewChild('inputElement', {static: true}) tagInputElement: ElementRef; @Input() tagNacList: { name: string, size: number }[]; @Input() categoryList: Category[]; @Input() primaryData: { id: number, type: boolean, tags: string[], category: string, url?: string }; - @Output() submitEvent = new EventEmitter<{ id: number, type: boolean, tags: string[], category: string, url?: string }>(); + @Output() submitEvent = new EventEmitter<{ + id: number, + type: boolean, + tags: string[], + category: string, + isUpdate: boolean, + url?: string + }>(); formGroup: FormGroup; tagTmpList: string[] = []; tagInputVisible: boolean = false; tagListTouched: boolean = false; + editTagText: string = '新增'; + + color: string[] = []; + randomColor = () => this.color = ColorList.map(c => c.bgColor).sort(() => Math.floor(Math.random() * 2)); ngOnInit(): void { this.formGroup = this.fb.group({ @@ -41,15 +53,18 @@ export class PublishFormComponent implements OnInit { this.formGroup.get('category').setValue(this.primaryData.category); this.formGroup.get('url').setValue(this.primaryData.url); } + this.randomColor(); } publishArticle() { + console.log(this.formGroup.value) const formData = { - id: this.formGroup.value.isUpdate ? this.primaryData.id : null, + id: this.formGroup.value.isUpdate ? this.primaryData?.id : null, type: this.formGroup.value.type, tags: this.formGroup.value.tagList, category: this.formGroup.value.category, - url: this.formGroup.value.type ? null : this.formGroup.value.url + url: this.formGroup.value.type ? null : this.formGroup.value.url, + isUpdate: this.formGroup.value.isUpdate }; this.submitEvent.emit(formData); } @@ -58,6 +73,7 @@ export class PublishFormComponent implements OnInit { showTagInput() { this.tagInputVisible = true; setTimeout(() => { + this.tagInputElement.nativeElement.focus(); }, 10); } @@ -69,9 +85,10 @@ export class PublishFormComponent implements OnInit { } // 输入框输入完成 - handleTagInputConfirm() { + handleTagInputConfirm($event: { value: string; originalValue: string; changed: boolean }) { this.tagListTouched = true; - const tmpTag = this.formGroup.get('tags').value; + // const tmpTag = this.formGroup.get('tags').value; + const tmpTag = $event.value; if (tmpTag && this.tagTmpList.indexOf(tmpTag) === -1) { this.tagTmpList = [...this.tagTmpList, tmpTag]; } @@ -79,12 +96,13 @@ export class PublishFormComponent implements OnInit { this.formGroup.get('tagList').setValue(this.tagTmpList.length ? this.tagTmpList : null); this.formGroup.get('tagList').updateValueAndValidity(); this.tagInputVisible = false; + this.editTagText = '新增' } // 点击tag切换 tagChange($event: boolean, name: string) { this.tagListTouched = true; - if (this.tagTmpList.indexOf(name) > -1) { + if (this.tagTmpList?.indexOf(name) > -1) { this.tagTmpList = this.tagTmpList.filter(v => v !== name); } else { this.tagTmpList.push(name); @@ -99,6 +117,7 @@ export class PublishFormComponent implements OnInit { } // 点选了文章类型 + articleTypeChanged() { this.formGroup.get(`url`).clearValidators(); const type = this.formGroup.get(`type`).value; diff --git a/src/app/view/write/write.component.ts b/src/app/view/write/write.component.ts index 305dba7..9bf6dc7 100644 --- a/src/app/view/write/write.component.ts +++ b/src/app/view/write/write.component.ts @@ -35,7 +35,7 @@ export class WriteComponent implements OnInit { userInfo: User; categoryList: Tag[]; tagNacList: { name: string, size: number }[]; - primaryData = {}; + primaryData = null; // 发布新文章时,文章相同会被拦回 此处判断一下 title: string; @@ -44,6 +44,8 @@ export class WriteComponent implements OnInit { // 同步属性内容 syncModel(str): void { this.article.mdContent = str; + // 文章 暂存 + localStorage.setItem('tmpArticle', JSON.stringify(this.article)); } @@ -109,12 +111,13 @@ export class WriteComponent implements OnInit { /** * 文章数据提交 */ - publishArticle(e: { id: number, type: boolean, tags: string[], category: string, url?: string }) { + publishArticle(e: { id: number; type: boolean; tags: string[]; category: string; isUpdate: boolean; url?: string }) { this.article.type = e.type; this.article.tags = e.tags; this.article.category = e.category; this.article.url = e.url; this.article.id = e.id; + this.isUpdate = e.isUpdate this.modalVisible = false; @@ -123,11 +126,8 @@ export class WriteComponent implements OnInit { // return; // } - // 文章 暂存 - localStorage.setItem('tmpArticle', JSON.stringify(this.article)); this.article.url = this.article.type ? null : this.article.url; - if (!this.isUpdate) { // 非文章更新 @@ -149,6 +149,7 @@ export class WriteComponent implements OnInit { if (err.code === 3020) { this.message.error('你没有发布文章的权限,文章替你暂存在本地'); } + this.message.error(err.msg); } }); @@ -163,13 +164,13 @@ export class WriteComponent implements OnInit { this.router.navigateByUrl('article/' + data.result.id); }, 2500); }, - error: e => { - if (e.code === 3010) { + error: err => { + if (err.code === 3010) { this.router.navigateByUrl('login'); - } else if (e.code === 3020) { + } else if (err.code === 3020) { this.message.error('你没有更新文章的权限'); } else { - this.message.error('失败,原因:' + e.msg); + this.message.error('失败,原因:' + err.msg); } } }); diff --git a/src/app/view/write/write.module.ts b/src/app/view/write/write.module.ts index ac65ed3..87d5bb0 100644 --- a/src/app/view/write/write.module.ts +++ b/src/app/view/write/write.module.ts @@ -15,6 +15,7 @@ import { NzSelectModule, NzTagModule } from 'ng-zorro-antd'; +import {EditableTagModule} from '../admin/components/editable-tag/editable-tag.module'; const routes: Route[] = [ {path: '**', component: WriteComponent} @@ -38,6 +39,7 @@ const routes: Route[] = [ NzSelectModule, NzCardModule, NzDividerModule, + EditableTagModule ] }) export class WriteModule {