-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(editor): refactor editor plugin manager (#609)
* refactor(editor): refactor editor plugin manager * update * modify editorTravelBack to editorResetUI
- Loading branch information
Showing
33 changed files
with
1,006 additions
and
938 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Editor from '../editor' | ||
import EditorPlug from '../editor-plug' | ||
|
||
export default class SamplePlug extends EditorPlug { | ||
constructor(editor: Editor) { | ||
super(editor) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import User from '@/lib/user' | ||
import * as Utils from '@/lib/utils' | ||
import Editor from '../editor' | ||
import EditorPlug from '../editor-plug' | ||
|
||
export default class ClosablePlug extends EditorPlug { | ||
constructor(editor: Editor) { | ||
super(editor) | ||
} | ||
|
||
close() { | ||
if (!this.editor.getUI().$textareaWrap.querySelector('.atk-comment-closed')) | ||
this.editor.getUI().$textareaWrap.prepend(Utils.createElement(`<div class="atk-comment-closed">${this.editor.$t('onlyAdminCanReply')}</div>`)) | ||
|
||
if (!User.data.isAdmin) { | ||
this.editor.getUI().$textarea.style.display = 'none' | ||
this.editor.getPlugs()?.closePlugPanel() | ||
this.editor.getUI().$bottom.style.display = 'none' | ||
} else { | ||
// 管理员一直打开评论 | ||
this.editor.getUI().$textarea.style.display = '' | ||
this.editor.getUI().$bottom.style.display = '' | ||
} | ||
} | ||
|
||
open() { | ||
this.editor.getUI().$textareaWrap.querySelector('.atk-comment-closed')?.remove() | ||
this.editor.getUI().$textarea.style.display = '' | ||
this.editor.getUI().$bottom.style.display = '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import type { CommentData } from '~/types/artalk-data' | ||
import * as Utils from '../../lib/utils' | ||
import Editor from '../editor' | ||
import User from '../../lib/user' | ||
import EditorPlug from '../editor-plug' | ||
import SubmitPlug from './submit-plug' | ||
import MoverPlug from './mover-plug' | ||
|
||
export default class EditPlug extends EditorPlug { | ||
private comment?: CommentData | ||
|
||
getComment() { | ||
return this.comment | ||
} | ||
|
||
getIsEditMode() { | ||
return !!this.comment | ||
} | ||
|
||
constructor(editor: Editor) { | ||
super(editor) | ||
|
||
this.kit.useMounted(() => { | ||
const submitPlug = this.editor.getPlugs()?.get(SubmitPlug) | ||
if (!submitPlug) throw Error("SubmitPlug not initialized") | ||
|
||
submitPlug.registerCustom({ | ||
activeCond: () => !!this.comment, // active this custom submit when edit mode | ||
req: async () => { | ||
const saveData = { | ||
content: this.editor.getContentFinal(), | ||
nick: this.editor.getUI().$nick.value, | ||
email: this.editor.getUI().$email.value, | ||
link: this.editor.getUI().$link.value, | ||
} | ||
const nComment = await this.editor.ctx.getApi().comment.commentEdit({ | ||
...this.comment, ...saveData | ||
}) | ||
return nComment | ||
}, | ||
post: (nComment: CommentData) => { | ||
this.editor.ctx.updateComment(nComment) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
edit(comment: CommentData, $comment: HTMLElement) { | ||
this.cancelEdit() | ||
this.editor.cancelReply() | ||
|
||
const ui = this.editor.getUI() | ||
if (!ui.$editCancelBtn) { | ||
const $btn = Utils.createElement( | ||
`<div class="atk-send-reply">` + | ||
`${this.editor.$t('editCancel')} ` + | ||
`<span class="atk-cancel">×</span>` + | ||
`</div>` | ||
) | ||
$btn.onclick = () => { | ||
this.cancelEdit() | ||
} | ||
ui.$textareaWrap.append($btn) | ||
ui.$editCancelBtn = $btn | ||
} | ||
this.comment = comment | ||
|
||
ui.$header.style.display = 'none' // TODO support modify header information | ||
|
||
this.editor.getPlugs()?.get(MoverPlug)?.move($comment) | ||
|
||
ui.$nick.value = comment.nick || '' | ||
ui.$email.value = comment.email || '' | ||
ui.$link.value = comment.link || '' | ||
|
||
this.editor.setContent(comment.content) | ||
ui.$textarea.focus() | ||
|
||
this.updateSubmitBtnText(this.editor.$t('save')) | ||
} | ||
|
||
cancelEdit() { | ||
if (!this.comment) return | ||
|
||
const ui = this.editor.getUI() | ||
|
||
if (ui.$editCancelBtn) { | ||
ui.$editCancelBtn.remove() | ||
ui.$editCancelBtn = undefined | ||
} | ||
|
||
this.comment = undefined | ||
this.editor.getPlugs()?.get(MoverPlug)?.back() | ||
|
||
const { nick, email, link } = User.data | ||
ui.$nick.value = nick | ||
ui.$email.value = email | ||
ui.$link.value = link | ||
|
||
this.editor.setContent('') | ||
this.restoreSubmitBtnText() | ||
|
||
ui.$header.style.display = '' // TODO support modify header information | ||
} | ||
|
||
// ------------------------------------------------------------------- | ||
// Submit Btn Text Modifier | ||
// ------------------------------------------------------------------- | ||
|
||
private originalSubmitBtnText = 'Send' | ||
|
||
private updateSubmitBtnText(text: string) { | ||
this.originalSubmitBtnText = this.editor.getUI().$submitBtn.innerText | ||
this.editor.getUI().$submitBtn.innerText = text | ||
} | ||
|
||
private restoreSubmitBtnText() { | ||
this.editor.getUI().$submitBtn.innerText = this.originalSubmitBtnText | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import User from '@/lib/user' | ||
import Editor from '../editor' | ||
import EditorPlug from '../editor-plug' | ||
import EditPlug from './edit-plug' | ||
|
||
export default class HeaderPlug extends EditorPlug { | ||
private get $inputs() { | ||
return this.editor.getHeaderInputEls() | ||
} | ||
|
||
constructor(editor: Editor) { | ||
super(editor) | ||
|
||
const inputEventFns: {[name: string]: () => void} = {} | ||
|
||
// the input event | ||
const onInput = ($input: HTMLInputElement, key: string) => () => { | ||
if (editor.getPlugs()?.get(EditPlug)?.getIsEditMode()) return // 评论编辑模式,不修改个人信息 | ||
|
||
User.update({ [key]: $input.value.trim() }) | ||
|
||
// trigger header input event | ||
editor.getPlugs()?.triggerHeaderInputEvt(key, $input) | ||
} | ||
|
||
this.kit.useMounted(() => { | ||
// set placeholder and sync header input value | ||
Object.entries(this.$inputs).forEach(([key, $input]) => { | ||
$input.placeholder = `${editor.$t(key as any)}` | ||
$input.value = User.data[key] || '' | ||
}) | ||
|
||
// bind the event | ||
Object.entries(this.$inputs).forEach(([key, $input]) => { | ||
$input.addEventListener('input', inputEventFns[key] = onInput($input, key)) | ||
}) | ||
}) | ||
|
||
this.kit.useUnmounted(() => { | ||
// unmount the event | ||
Object.entries(this.$inputs).forEach(([key, $input]) => { | ||
$input.removeEventListener('input', inputEventFns[key]) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Editor from '../editor' | ||
import EditorPlug from '../editor-plug' | ||
|
||
const LocalStorageKey = 'ArtalkContent' | ||
|
||
export default class LocalStoragePlug extends EditorPlug { | ||
constructor(editor: Editor) { | ||
super(editor) | ||
|
||
this.kit.useMounted(() => { | ||
// load editor content from localStorage when init | ||
const localContent = window.localStorage.getItem(LocalStorageKey) || '' | ||
if (localContent.trim() !== '') { | ||
editor.showNotify(editor.$t('restoredMsg'), 'i') | ||
editor.setContent(localContent) | ||
} | ||
}) | ||
|
||
this.kit.useUnmounted(() => { | ||
}) | ||
|
||
this.kit.useContentUpdated(() => { | ||
this.save() | ||
}) | ||
} | ||
|
||
// Save editor content to localStorage | ||
public save() { | ||
window.localStorage.setItem(LocalStorageKey, this.editor.getContentRaw().trim()) | ||
} | ||
} |
Oops, something went wrong.