/
artalk.ts
131 lines (108 loc) · 3.27 KB
/
artalk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import './style/main.scss'
import type { ArtalkConfig, EventPayloadMap, ArtalkPlugin, ContextApi } from '@/types'
import type { EventHandler } from './lib/event-manager'
import Context from './context'
import { handelCustomConf, convertApiOptions } from './config'
import Services from './service'
import * as Stat from './plugins/stat'
import { Api } from './api'
import type { TInjectedServices } from './service'
import { GlobalPlugins, load } from './load'
/**
* Artalk
*
* @see https://artalk.js.org
*/
export default class Artalk {
public ctx!: ContextApi
constructor(conf: Partial<ArtalkConfig>) {
// Init Config
const handledConf = handelCustomConf(conf, true)
// Init Context
this.ctx = new Context(handledConf)
// Init Services
Object.entries(Services).forEach(([name, initService]) => {
const obj = initService(this.ctx)
obj && this.ctx.inject(name as keyof TInjectedServices, obj) // auto inject deps to ctx
})
if (import.meta.env.DEV && import.meta.env.VITEST) {
global.devLoadArtalk = () => load(this.ctx)
} else {
load(this.ctx)
}
}
/** Get the config of Artalk */
public getConf() {
return this.ctx.getConf()
}
/** Get the root element of Artalk */
public getEl() {
return this.ctx.$root
}
/** Update config of Artalk */
public update(conf: Partial<ArtalkConfig>) {
this.ctx.updateConf(conf)
return this
}
/** Reload comment list of Artalk */
public reload() {
this.ctx.reload()
}
/** Destroy instance of Artalk */
public destroy() {
this.ctx.trigger('unmounted')
while (this.ctx.$root.firstChild) {
this.ctx.$root.removeChild(this.ctx.$root.firstChild)
}
}
/** Add an event listener */
public on<K extends keyof EventPayloadMap>(name: K, handler: EventHandler<EventPayloadMap[K]>) {
this.ctx.on(name, handler)
}
/** Remove an event listener */
public off<K extends keyof EventPayloadMap>(name: K, handler: EventHandler<EventPayloadMap[K]>) {
this.ctx.off(name, handler)
}
/** Trigger an event */
public trigger<K extends keyof EventPayloadMap>(name: K, payload?: EventPayloadMap[K]) {
this.ctx.trigger(name, payload)
}
/** Set dark mode */
public setDarkMode(darkMode: boolean) {
this.ctx.setDarkMode(darkMode)
}
// ===========================
// Static Members
// ===========================
/** Init Artalk */
public static init(conf: Partial<ArtalkConfig>): Artalk {
return new Artalk(conf)
}
/** Use plugin, the plugin will be used when Artalk.init */
public static use(plugin: ArtalkPlugin) {
if (GlobalPlugins.includes(plugin)) return
GlobalPlugins.push(plugin)
}
/** Load count widget */
public static loadCountWidget(c: Partial<ArtalkConfig>) {
const conf = handelCustomConf(c, true)
Stat.initCountWidget({
getApi: () => new Api(convertApiOptions(conf)),
siteName: conf.site,
countEl: conf.countEl,
pvEl: conf.pvEl,
pvAdd: false,
})
}
// ===========================
// Deprecated
// ===========================
/** @deprecated Please use `getEl()` instead */
public get $root() {
return this.ctx.$root
}
/** @description Please use `getConf()` instead */
public get conf() {
return this.ctx.getConf()
}
}