artalk 2.7.0 → 2.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.md +1 -1
  2. package/dist/Artalk.css +1 -1
  3. package/dist/Artalk.es.js +209 -188
  4. package/dist/Artalk.es.js.map +1 -0
  5. package/dist/Artalk.iife.js +2 -1
  6. package/dist/Artalk.iife.js.map +1 -0
  7. package/dist/Artalk.js +2 -1
  8. package/dist/Artalk.js.map +1 -0
  9. package/dist/ArtalkLite.css +1 -1
  10. package/dist/ArtalkLite.es.js +209 -188
  11. package/dist/ArtalkLite.es.js.map +1 -0
  12. package/dist/ArtalkLite.iife.js +2 -1
  13. package/dist/ArtalkLite.iife.js.map +1 -0
  14. package/dist/ArtalkLite.js +2 -1
  15. package/dist/ArtalkLite.js.map +1 -0
  16. package/dist/i18n/jp.js +1 -1
  17. package/dist/i18n/zh-TW.js +1 -1
  18. package/dist/src/api/_base.d.ts +7 -0
  19. package/dist/src/api/_options.d.ts +5 -0
  20. package/dist/src/artalk.d.ts +0 -2
  21. package/dist/src/components/checker/index.d.ts +2 -0
  22. package/dist/src/i18n/en.d.ts +4 -0
  23. package/dist/src/i18n/external.d.ts +4 -0
  24. package/dist/src/i18n/index.d.ts +12 -0
  25. package/dist/src/i18n/jp.d.ts +4 -0
  26. package/dist/src/i18n/zh-TW.d.ts +4 -0
  27. package/dist/src/lib/user.d.ts +8 -6
  28. package/dist/src/main.d.ts +3 -0
  29. package/dist/src/plugins/editor/_kit.d.ts +7 -1
  30. package/dist/src/service.d.ts +2 -8
  31. package/dist/types/event.d.ts +1 -1
  32. package/package.json +43 -17
  33. package/dist/src/components/dark-mode.d.ts +0 -2
  34. package/src/api/_base.ts +0 -18
  35. package/src/api/_options.ts +0 -25
  36. package/src/api/_request.ts +0 -104
  37. package/src/api/admin.ts +0 -18
  38. package/src/api/captcha.ts +0 -24
  39. package/src/api/comment.ts +0 -84
  40. package/src/api/index.ts +0 -29
  41. package/src/api/page.ts +0 -83
  42. package/src/api/site.ts +0 -51
  43. package/src/api/system.ts +0 -49
  44. package/src/api/upload.ts +0 -28
  45. package/src/api/user.ts +0 -132
  46. package/src/artalk.ts +0 -119
  47. package/src/comment/actions.ts +0 -83
  48. package/src/comment/comment.html +0 -18
  49. package/src/comment/comment.ts +0 -192
  50. package/src/comment/height-limit.ts +0 -100
  51. package/src/comment/index.ts +0 -3
  52. package/src/comment/render.ts +0 -150
  53. package/src/comment/renders/actions.ts +0 -114
  54. package/src/comment/renders/avatar.ts +0 -22
  55. package/src/comment/renders/content.ts +0 -39
  56. package/src/comment/renders/header.ts +0 -84
  57. package/src/comment/renders/index.ts +0 -19
  58. package/src/comment/renders/pending.ts +0 -12
  59. package/src/comment/renders/reply-at.ts +0 -16
  60. package/src/comment/renders/reply-to.ts +0 -24
  61. package/src/components/action-btn.ts +0 -170
  62. package/src/components/checker/admin.ts +0 -37
  63. package/src/components/checker/captcha-renders.ts +0 -69
  64. package/src/components/checker/captcha.ts +0 -26
  65. package/src/components/checker/index.ts +0 -192
  66. package/src/components/dark-mode.ts +0 -30
  67. package/src/components/dialog.ts +0 -62
  68. package/src/components/error-dialog.ts +0 -35
  69. package/src/components/pagination.ts +0 -153
  70. package/src/components/read-more-btn.ts +0 -92
  71. package/src/config.ts +0 -119
  72. package/src/context.ts +0 -146
  73. package/src/data.ts +0 -110
  74. package/src/defaults.ts +0 -61
  75. package/src/editor/editor.html +0 -18
  76. package/src/editor/editor.ts +0 -114
  77. package/src/editor/state.ts +0 -52
  78. package/src/editor/ui.ts +0 -36
  79. package/src/i18n/en.ts +0 -81
  80. package/src/i18n/external.ts +0 -10
  81. package/src/i18n/index.ts +0 -67
  82. package/src/i18n/jp.ts +0 -83
  83. package/src/i18n/zh-CN.ts +0 -85
  84. package/src/i18n/zh-TW.ts +0 -83
  85. package/src/layer/index.ts +0 -4
  86. package/src/layer/layer-manager.ts +0 -32
  87. package/src/layer/layer.ts +0 -39
  88. package/src/layer/scrollbar-helper.ts +0 -24
  89. package/src/layer/sidebar-layer.html +0 -8
  90. package/src/layer/sidebar-layer.ts +0 -155
  91. package/src/layer/wrap.ts +0 -77
  92. package/src/lib/component.ts +0 -12
  93. package/src/lib/detect.ts +0 -222
  94. package/src/lib/event-manager.ts +0 -47
  95. package/src/lib/highlight.ts +0 -5
  96. package/src/lib/marked-renderer.ts +0 -32
  97. package/src/lib/marked.ts +0 -72
  98. package/src/lib/sanitizer.ts +0 -57
  99. package/src/lib/ui.ts +0 -193
  100. package/src/lib/user.ts +0 -55
  101. package/src/lib/utils.ts +0 -236
  102. package/src/list/comment.ts +0 -20
  103. package/src/list/layout.ts +0 -120
  104. package/src/list/list.html +0 -20
  105. package/src/list/list.ts +0 -79
  106. package/src/list/nest.ts +0 -66
  107. package/src/list/page.ts +0 -97
  108. package/src/list/paginator/index.ts +0 -25
  109. package/src/list/paginator/read-more.ts +0 -67
  110. package/src/list/paginator/up-down.ts +0 -51
  111. package/src/main.ts +0 -5
  112. package/src/plugins/admin-only-elem.ts +0 -40
  113. package/src/plugins/conf-remoter.ts +0 -54
  114. package/src/plugins/dark-mode.ts +0 -12
  115. package/src/plugins/editor/_kit.ts +0 -57
  116. package/src/plugins/editor/_plug.ts +0 -60
  117. package/src/plugins/editor/_sample.ts +0 -8
  118. package/src/plugins/editor/closable.ts +0 -44
  119. package/src/plugins/editor/emoticons.scss +0 -72
  120. package/src/plugins/editor/emoticons.ts +0 -293
  121. package/src/plugins/editor/header-event.ts +0 -35
  122. package/src/plugins/editor/header-link.ts +0 -31
  123. package/src/plugins/editor/header-user.ts +0 -98
  124. package/src/plugins/editor/index.ts +0 -40
  125. package/src/plugins/editor/local-storage.ts +0 -36
  126. package/src/plugins/editor/mover.ts +0 -29
  127. package/src/plugins/editor/preview.scss +0 -7
  128. package/src/plugins/editor/preview.ts +0 -41
  129. package/src/plugins/editor/state-edit.ts +0 -116
  130. package/src/plugins/editor/state-reply.ts +0 -91
  131. package/src/plugins/editor/submit-add.ts +0 -33
  132. package/src/plugins/editor/submit-btn.ts +0 -25
  133. package/src/plugins/editor/submit.ts +0 -73
  134. package/src/plugins/editor/textarea.ts +0 -55
  135. package/src/plugins/editor/upload.ts +0 -145
  136. package/src/plugins/editor-kit.ts +0 -161
  137. package/src/plugins/index.ts +0 -16
  138. package/src/plugins/list/copyright.ts +0 -16
  139. package/src/plugins/list/count.ts +0 -31
  140. package/src/plugins/list/dropdown.ts +0 -95
  141. package/src/plugins/list/error-dialog.ts +0 -22
  142. package/src/plugins/list/fetch.ts +0 -64
  143. package/src/plugins/list/goto-first.ts +0 -23
  144. package/src/plugins/list/goto.ts +0 -72
  145. package/src/plugins/list/index.ts +0 -25
  146. package/src/plugins/list/loading.ts +0 -17
  147. package/src/plugins/list/no-comment.ts +0 -25
  148. package/src/plugins/list/reach-bottom.ts +0 -46
  149. package/src/plugins/list/sidebar-btn.ts +0 -39
  150. package/src/plugins/list/time-ticking.ts +0 -22
  151. package/src/plugins/list/unread-badge.ts +0 -26
  152. package/src/plugins/list/unread.ts +0 -38
  153. package/src/plugins/list/with-editor.ts +0 -59
  154. package/src/plugins/markdown.ts +0 -10
  155. package/src/plugins/stat.ts +0 -79
  156. package/src/plugins/version-check.ts +0 -37
  157. package/src/service.ts +0 -91
  158. package/src/style/_color.scss +0 -41
  159. package/src/style/_extend.scss +0 -20
  160. package/src/style/_variables.scss +0 -3
  161. package/src/style/comment.scss +0 -360
  162. package/src/style/editor.scss +0 -194
  163. package/src/style/list.scss +0 -212
  164. package/src/style/main.scss +0 -486
  165. package/src/style/pagination.scss +0 -58
  166. package/src/style/sidebar-layer.scss +0 -92
  167. package/types/config.ts +0 -153
  168. package/types/context.ts +0 -84
  169. package/types/data.ts +0 -271
  170. package/types/editor.ts +0 -95
  171. package/types/event.ts +0 -40
  172. package/types/global.d.ts +0 -1
  173. package/types/index.ts +0 -7
  174. package/types/plugin.ts +0 -3
  175. package/types/sidebar.ts +0 -3
@@ -1,40 +0,0 @@
1
- import type { ArtalkConfig } from '~/types'
2
- import type EditorPlug from './_plug'
3
- import LocalStorage from './local-storage'
4
- import Textarea from './textarea'
5
- import SubmitBtn from './submit-btn'
6
- import Submit from './submit'
7
- import StateReply from './state-reply'
8
- import StateEdit from './state-edit'
9
- import Closable from './closable'
10
- import HeaderEvent from './header-event'
11
- import HeaderUser from './header-user'
12
- import HeaderLink from './header-link'
13
- import Mover from './mover'
14
- import Emoticons from './emoticons'
15
- import Upload from './upload'
16
- import Preview from './preview'
17
-
18
- /** The default enabled plugs */
19
- export const ENABLED_PLUGS: (typeof EditorPlug)[] = [
20
- // Core
21
- LocalStorage,
22
- HeaderEvent, HeaderUser, HeaderLink,
23
- Textarea,
24
- Submit, SubmitBtn,
25
- Mover, StateReply, StateEdit,
26
- Closable,
27
-
28
- // Extensions
29
- Emoticons, Upload, Preview
30
- ]
31
-
32
- /** Get the name list of disabled plugs */
33
- export function getDisabledPlugByConf(conf: ArtalkConfig): (typeof EditorPlug)[] {
34
- return [
35
- {k: Upload, v: conf.imgUpload},
36
- {k: Emoticons, v: conf.emoticons},
37
- {k: Preview, v: conf.preview},
38
- {k: Mover, v: conf.editorTravel},
39
- ].filter(n => !n.v).flatMap(n => n.k)
40
- }
@@ -1,36 +0,0 @@
1
- import $t from '@/i18n'
2
- import type PlugKit from './_kit'
3
- import EditorPlug from './_plug'
4
-
5
- const LocalStorageKey = 'ArtalkContent'
6
-
7
- export default class LocalStorage extends EditorPlug {
8
- constructor(kit: PlugKit) {
9
- super(kit)
10
-
11
- const onContentUpdated = () => {
12
- this.save()
13
- }
14
-
15
- this.kit.useMounted(() => {
16
- // load editor content from localStorage when init
17
- const localContent = window.localStorage.getItem(LocalStorageKey) || ''
18
- if (localContent.trim() !== '') {
19
- this.kit.useEditor().showNotify($t('restoredMsg'), 'i')
20
- this.kit.useEditor().setContent(localContent)
21
- }
22
-
23
- // bind event
24
- this.kit.useEvents().on('content-updated', onContentUpdated)
25
- })
26
-
27
- this.kit.useUnmounted(() => {
28
- this.kit.useEvents().off('content-updated', onContentUpdated)
29
- })
30
- }
31
-
32
- // Save editor content to localStorage
33
- public save() {
34
- window.localStorage.setItem(LocalStorageKey, this.kit.useEditor().getContentRaw().trim())
35
- }
36
- }
@@ -1,29 +0,0 @@
1
- import * as Utils from '@/lib/utils'
2
- import EditorPlug from './_plug'
3
-
4
- export default class Mover extends EditorPlug {
5
- private isMoved = false
6
-
7
- move(afterEl: HTMLElement) {
8
- if (this.isMoved) return
9
- this.isMoved = true
10
-
11
- const editorEl = this.kit.useUI().$el
12
-
13
- editorEl.after(Utils.createElement('<div class="atk-editor-travel-placeholder"></div>'))
14
-
15
- const $travelPlace = Utils.createElement('<div></div>')
16
- afterEl.after($travelPlace)
17
- $travelPlace.replaceWith(editorEl)
18
-
19
- editorEl.classList.add('atk-fade-in') // 添加渐入动画
20
- editorEl.classList.add('editor-traveling')
21
- }
22
-
23
- back() {
24
- if (!this.isMoved) return
25
- this.isMoved = false
26
- this.kit.useGlobalCtx().$root.querySelector('.atk-editor-travel-placeholder')?.replaceWith(this.kit.useUI().$el)
27
- this.kit.useUI().$el.classList.remove('editor-traveling')
28
- }
29
- }
@@ -1,7 +0,0 @@
1
- .atk-editor-plug-preview {
2
- overflow-y: scroll;
3
- overflow-x: hidden;
4
- height: 100%;
5
- width: 100%;
6
- padding: 10px calc(10px + 5px);
7
- }
@@ -1,41 +0,0 @@
1
- import './preview.scss'
2
-
3
- import * as marked from '@/lib/marked'
4
- import $t from '@/i18n'
5
- import EditorPlug from './_plug'
6
- import type PlugKit from './_kit'
7
-
8
- export default class Preview extends EditorPlug {
9
- private isPlugPanelShow = false
10
-
11
- constructor(kit: PlugKit) {
12
- super(kit)
13
-
14
- this.kit.useMounted(() => {
15
- this.usePanel(`<div class="atk-editor-plug-preview"></div>`)
16
-
17
- // initialize plug button
18
- let btnText = $t('preview')
19
- if (marked.getInstance()) btnText += ` <i title="Markdown is supported"><svg class="markdown" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M14.85 3H1.15C.52 3 0 3.52 0 4.15v7.69C0 12.48.52 13 1.15 13h13.69c.64 0 1.15-.52 1.15-1.15v-7.7C16 3.52 15.48 3 14.85 3zM9 11H7V8L5.5 9.92 4 8v3H2V5h2l1.5 2L7 5h2v6zm2.99.5L9.5 8H11V5h2v3h1.5l-2.51 3.5z"></path></svg></i>`
20
- this.useBtn(btnText)
21
- })
22
- this.kit.useUnmounted(() => {})
23
-
24
- // function to update content
25
- this.kit.useEvents().on('content-updated', (content) => {
26
- this.isPlugPanelShow && this.updateContent()
27
- })
28
-
29
- this.usePanelShow(() => {
30
- this.isPlugPanelShow = true
31
- this.updateContent()
32
- })
33
- this.usePanelHide(() => {
34
- this.isPlugPanelShow = false
35
- })
36
- }
37
-
38
- updateContent() {
39
- this.$panel!.innerHTML = this.kit.useEditor().getContentMarked()
40
- }
41
- }
@@ -1,116 +0,0 @@
1
- import type { CommentData } from '~/types'
2
- import $t from '@/i18n'
3
- import * as Utils from '@/lib/utils'
4
- import User from '@/lib/user'
5
- import type PlugKit from './_kit'
6
- import EditorPlug from './_plug'
7
- import Submit from './submit'
8
-
9
- export default class StateEdit extends EditorPlug {
10
- private comment?: CommentData
11
-
12
- constructor(kit: PlugKit) {
13
- super(kit)
14
-
15
- // add effect when state switch to `edit`
16
- this.useEditorStateEffect('edit', (comment) => {
17
- this.edit(comment)
18
-
19
- return () => {
20
- this.cancelEdit()
21
- }
22
- })
23
-
24
- // register submit preset
25
- this.kit.useMounted(() => {
26
- const submitPlug = this.kit.useDeps(Submit)
27
- if (!submitPlug) throw Error("SubmitPlug not initialized")
28
-
29
- submitPlug.registerCustom({
30
- activeCond: () => !!this.comment, // active this custom submit when edit mode
31
- req: async () => {
32
- const saveData = {
33
- content: this.kit.useEditor().getContentFinal(),
34
- nick: this.kit.useUI().$nick.value,
35
- email: this.kit.useUI().$email.value,
36
- link: this.kit.useUI().$link.value,
37
- }
38
- const nComment = await this.kit.useApi().comment.commentEdit({
39
- ...this.comment, ...saveData
40
- })
41
- return nComment
42
- },
43
- post: (nComment: CommentData) => {
44
- this.kit.useGlobalCtx().getData().updateComment(nComment)
45
- }
46
- })
47
- })
48
- }
49
-
50
- private edit(comment: CommentData) {
51
- const ui = this.kit.useUI()
52
- if (!ui.$editCancelBtn) {
53
- const $btn = Utils.createElement(
54
- `<div class="atk-send-reply">` +
55
- `${$t('editCancel')} ` +
56
- `<span class="atk-cancel">×</span>` +
57
- `</div>`
58
- )
59
- $btn.onclick = () => {
60
- this.kit.useEditor().resetState()
61
- }
62
- ui.$textareaWrap.append($btn)
63
- ui.$editCancelBtn = $btn
64
- }
65
- this.comment = comment
66
-
67
- ui.$header.style.display = 'none' // TODO support modify header information
68
-
69
- ui.$nick.value = comment.nick || ''
70
- ui.$email.value = comment.email || ''
71
- ui.$link.value = comment.link || ''
72
-
73
- this.kit.useEditor().setContent(comment.content)
74
- ui.$textarea.focus()
75
-
76
- this.updateSubmitBtnText($t('save'))
77
- }
78
-
79
- private cancelEdit() {
80
- if (!this.comment) return
81
-
82
- const ui = this.kit.useUI()
83
-
84
- if (ui.$editCancelBtn) {
85
- ui.$editCancelBtn.remove()
86
- ui.$editCancelBtn = undefined
87
- }
88
-
89
- this.comment = undefined
90
-
91
- const { nick, email, link } = User.data
92
- ui.$nick.value = nick
93
- ui.$email.value = email
94
- ui.$link.value = link
95
-
96
- this.kit.useEditor().setContent('')
97
- this.restoreSubmitBtnText()
98
-
99
- ui.$header.style.display = '' // TODO support modify header information
100
- }
101
-
102
- // -------------------------------------------------------------------
103
- // Submit Btn Text Modifier
104
- // -------------------------------------------------------------------
105
-
106
- private originalSubmitBtnText = 'Send'
107
-
108
- private updateSubmitBtnText(text: string) {
109
- this.originalSubmitBtnText = this.kit.useUI().$submitBtn.innerText
110
- this.kit.useUI().$submitBtn.innerText = text
111
- }
112
-
113
- private restoreSubmitBtnText() {
114
- this.kit.useUI().$submitBtn.innerText = this.originalSubmitBtnText
115
- }
116
- }
@@ -1,91 +0,0 @@
1
- import type { CommentData } from '~/types'
2
- import * as Utils from '@/lib/utils'
3
- import * as Ui from '@/lib/ui'
4
- import $t from '@/i18n'
5
- import EditorPlug from './_plug'
6
- import type PlugKit from './_kit'
7
- import Submit from './submit'
8
- import SubmitAddPreset from './submit-add'
9
-
10
- export default class StateReply extends EditorPlug {
11
- private comment?: CommentData
12
-
13
- constructor(kit: PlugKit) {
14
- super(kit)
15
-
16
- // add effect when state switch to `reply`
17
- this.useEditorStateEffect('reply', (commentData) => {
18
- this.setReply(commentData)
19
-
20
- return () => {
21
- this.cancelReply()
22
- }
23
- })
24
-
25
- // register submit preset
26
- this.kit.useEvents().on('mounted', () => {
27
- const submitPlug = this.kit.useDeps(Submit)
28
- if (!submitPlug) throw Error("SubmitPlug not initialized")
29
-
30
- const defaultPreset = new SubmitAddPreset(this.kit)
31
-
32
- submitPlug.registerCustom({
33
- activeCond: () => !!this.comment, // active this custom submit when reply mode
34
- req: async () => {
35
- if (!this.comment) throw new Error('reply comment cannot be empty')
36
-
37
- const nComment = await this.kit.useApi().comment.add({
38
- ...defaultPreset.getSubmitAddParams(),
39
- rid: this.comment.id,
40
- page_key: this.comment.page_key,
41
- page_title: undefined,
42
- site_name: this.comment.site_name
43
- })
44
-
45
- return nComment
46
- },
47
- post: (nComment: CommentData) => {
48
- // open another page when reply comment is not the same pageKey
49
- const conf = this.kit.useConf()
50
- if (nComment.page_key !== conf.pageKey) {
51
- window.open(`${nComment.page_url}#atk-comment-${nComment.id}`)
52
- }
53
-
54
- defaultPreset.postSubmitAdd(nComment)
55
- }
56
- })
57
- })
58
- }
59
-
60
- private setReply(commentData: CommentData) {
61
- const ui = this.kit.useUI()
62
- if (!ui.$sendReply) {
63
- ui.$sendReply = Utils.createElement(
64
- `<div class="atk-send-reply">` +
65
- `${$t('reply')} ` +
66
- `<span class="atk-text"></span><span class="atk-cancel">×</span>` +
67
- `</div>`
68
- )
69
- ui.$sendReply.querySelector<HTMLElement>('.atk-text')!.innerText = `@${commentData.nick}`
70
- ui.$sendReply.addEventListener('click', () => {
71
- this.kit.useEditor().resetState()
72
- })
73
- ui.$textareaWrap.append(ui.$sendReply)
74
- }
75
-
76
- this.comment = commentData
77
-
78
- ui.$textarea.focus()
79
- }
80
-
81
- private cancelReply() {
82
- if (!this.comment) return
83
-
84
- const ui = this.kit.useUI()
85
- if (ui.$sendReply) {
86
- ui.$sendReply.remove()
87
- ui.$sendReply = undefined
88
- }
89
- this.comment = undefined
90
- }
91
- }
@@ -1,33 +0,0 @@
1
- import type { CommentData } from '~/types'
2
- import User from '@/lib/user'
3
- import type PlugKit from './_kit'
4
-
5
- export default class SubmitAddPreset {
6
- constructor(private kit: PlugKit) {}
7
-
8
- async reqAdd() {
9
- const nComment = await this.kit.useApi().comment.add({
10
- ...this.getSubmitAddParams()
11
- })
12
- return nComment
13
- }
14
-
15
- getSubmitAddParams() {
16
- const { nick, email, link } = User.data
17
- const conf = this.kit.useConf()
18
-
19
- return {
20
- content: this.kit.useEditor().getContentFinal(),
21
- nick, email, link,
22
- rid: 0,
23
- page_key: conf.pageKey,
24
- page_title: conf.pageTitle,
25
- site_name: conf.site
26
- }
27
- }
28
-
29
- postSubmitAdd(commentNew: CommentData) {
30
- // insert the new comment to list
31
- this.kit.useGlobalCtx().getData().insertComment(commentNew)
32
- }
33
- }
@@ -1,25 +0,0 @@
1
- import $t from '@/i18n'
2
- import EditorPlug from './_plug'
3
- import type PlugKit from './_kit'
4
-
5
- export default class SubmitBtn extends EditorPlug {
6
- constructor(kit: PlugKit) {
7
- super(kit)
8
-
9
- const onClick = () => {
10
- this.kit.useEditor().submit()
11
- }
12
-
13
- this.kit.useMounted(() => {
14
- // apply the submit button text from user custom config
15
- this.kit.useUI().$submitBtn.innerText = this.kit.useConf().sendBtn || $t('send')
16
-
17
- // bind the event when click the submit button
18
- this.kit.useUI().$submitBtn.addEventListener('click', onClick)
19
- })
20
-
21
- this.kit.useUnmounted(() => {
22
- this.kit.useUI().$submitBtn.removeEventListener('click', onClick)
23
- })
24
- }
25
- }
@@ -1,73 +0,0 @@
1
- import type { CommentData } from '~/types'
2
- import $t from '@/i18n'
3
- import EditorPlug from './_plug'
4
- import type PlugKit from './_kit'
5
- import SubmitAddPreset from './submit-add'
6
-
7
- interface CustomSubmit {
8
- activeCond: () => void
9
- pre?: () => void
10
- req?: () => Promise<CommentData>
11
- post?: (nComment: CommentData) => void
12
- }
13
-
14
- export default class Submit extends EditorPlug {
15
- private customs: CustomSubmit[] = []
16
- private defaultPreset: SubmitAddPreset
17
-
18
- constructor(kit: PlugKit) {
19
- super(kit)
20
-
21
- this.defaultPreset = new SubmitAddPreset(this.kit)
22
-
23
- const onEditorSubmit = () => this.do()
24
-
25
- this.kit.useMounted(() => {
26
- // invoke `do()` when event `editor-submit` is triggered
27
- this.kit.useGlobalCtx().on('editor-submit', onEditorSubmit)
28
- })
29
- this.kit.useUnmounted(() => {
30
- this.kit.useGlobalCtx().off('editor-submit', onEditorSubmit)
31
- })
32
- }
33
-
34
- registerCustom(c: CustomSubmit) {
35
- this.customs.push(c)
36
- }
37
-
38
- private async do() {
39
- if (this.kit.useEditor().getContentFinal().trim() === '') {
40
- this.kit.useEditor().focus()
41
- return
42
- }
43
-
44
- const custom = this.customs.find(o => o.activeCond())
45
-
46
- this.kit.useEditor().showLoading()
47
-
48
- try {
49
- // pre submit
50
- if (custom?.pre) custom.pre()
51
-
52
- let nComment: CommentData
53
-
54
- // submit request
55
- if (custom?.req) nComment = await custom.req()
56
- else nComment = await this.defaultPreset.reqAdd()
57
-
58
- // post submit
59
- if (custom?.post) custom.post(nComment)
60
- else this.defaultPreset.postSubmitAdd(nComment)
61
- } catch (err: any) {
62
- // submit error
63
- console.error(err)
64
- this.kit.useEditor().showNotify(`${$t('commentFail')},${err.msg || String(err)}`, 'e')
65
- return
66
- } finally {
67
- this.kit.useEditor().hideLoading()
68
- }
69
-
70
- this.kit.useEditor().reset() // 复原编辑器
71
- this.kit.useGlobalCtx().trigger('editor-submitted')
72
- }
73
- }
@@ -1,55 +0,0 @@
1
- import $t from '@/i18n'
2
- import EditorPlug from './_plug'
3
- import type PlugKit from './_kit'
4
-
5
- export default class Textarea extends EditorPlug {
6
- constructor(kit: PlugKit) {
7
- super(kit)
8
-
9
- const onKeydown = (e: KeyboardEvent) => this.onKeydown(e)
10
- const onInput = () => this.onInput()
11
-
12
- this.kit.useMounted(() => {
13
- // 占位符
14
- this.kit.useUI().$textarea.placeholder = this.kit.useConf().placeholder || $t('placeholder')
15
-
16
- // bind the event
17
- this.kit.useUI().$textarea.addEventListener('keydown', onKeydown)
18
- this.kit.useUI().$textarea.addEventListener('input', onInput)
19
- })
20
-
21
- this.kit.useUnmounted(() => {
22
- // unmount the event
23
- this.kit.useUI().$textarea.removeEventListener('keydown', onKeydown)
24
- this.kit.useUI().$textarea.removeEventListener('input', onInput)
25
- })
26
-
27
- this.kit.useEvents().on('content-updated', () => {
28
- // delay 80ms to prevent invalid execution
29
- window.setTimeout(() => {
30
- this.adaptiveHeightByContent()
31
- }, 80)
32
- })
33
- }
34
-
35
- // 按下 Tab 输入内容,而不是失去焦距
36
- private onKeydown(e: KeyboardEvent) {
37
- const keyCode = e.keyCode || e.which
38
-
39
- if (keyCode === 9) {
40
- e.preventDefault()
41
- this.kit.useEditor().insertContent('\t')
42
- }
43
- }
44
-
45
- private onInput() {
46
- this.kit.useEvents().trigger('content-updated', this.kit.useEditor().getContentRaw())
47
- }
48
-
49
- // Resize the textarea height by content
50
- public adaptiveHeightByContent() {
51
- const diff = this.kit.useUI().$textarea.offsetHeight - this.kit.useUI().$textarea.clientHeight
52
- this.kit.useUI().$textarea.style.height = '0px' // it's a magic. 若不加此行,内容减少,高度回不去
53
- this.kit.useUI().$textarea.style.height = `${this.kit.useUI().$textarea.scrollHeight + diff}px`
54
- }
55
- }