-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathtreeDataProvider.ts
More file actions
220 lines (201 loc) · 7.51 KB
/
treeDataProvider.ts
File metadata and controls
220 lines (201 loc) · 7.51 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import type { ThemeName } from '@md/shared/configs'
import * as vscode from 'vscode'
import { colorOptions, fontFamilyOptions, fontSizeOptions, themeOptions } from './styleChoices'
export class MarkdownTreeDataProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
private _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | undefined> = new vscode.EventEmitter<vscode.TreeItem | undefined>()
readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | undefined> = this._onDidChangeTreeData.event
private currentFontSize: string
private currentTheme: ThemeName
private currentPrimaryColor: string
private currentFontFamily: string
private countStatus: boolean
private isMacCodeBlock: boolean
private citeStatus: boolean
private context: vscode.ExtensionContext
constructor(context: vscode.ExtensionContext) {
this.context = context
this.currentFontSize = this.context.workspaceState.get(`markdownPreview.fontSize`, fontSizeOptions[0].value)
this.currentTheme = this.context.workspaceState.get(`markdownPreview.theme`, themeOptions[0].value)
this.currentPrimaryColor = this.context.workspaceState.get(`markdownPreview.primaryColor`, colorOptions[0].value)
this.currentFontFamily = this.context.workspaceState.get(`markdownPreview.fontFamily`, fontFamilyOptions[0].value)
this.countStatus = this.context.workspaceState.get(`markdownPreview.countStatus`, false)
this.isMacCodeBlock = this.context.workspaceState.get(`markdownPreview.isMacCodeBlock`, false)
this.citeStatus = this.context.workspaceState.get(`markdownPreview.citeStatus`, false)
}
getTreeItem(element: vscode.TreeItem): vscode.TreeItem {
return element
}
updateCountStatus(status: boolean): void {
this.countStatus = status
this.context.workspaceState.update(`markdownPreview.countStatus`, status)
this._onDidChangeTreeData.fire(undefined)
}
updateMacCodeBlock(status: boolean): void {
this.isMacCodeBlock = status
this.context.workspaceState.update(`markdownPreview.isMacCodeBlock`, status)
this._onDidChangeTreeData.fire(undefined)
}
updateCiteStatus(status: boolean): void {
this.citeStatus = status
this.context.workspaceState.update(`markdownPreview.citeStatus`, status)
this._onDidChangeTreeData.fire(undefined)
}
getCurrentMacCodeBlock(): boolean {
return this.isMacCodeBlock
}
getCurrentCountStatus(): boolean {
return this.countStatus
}
getCurrentCiteStatus(): boolean {
return this.citeStatus
}
getChildren(element?: vscode.TreeItem): Thenable<vscode.TreeItem[]> {
if (!element) {
return Promise.resolve([
new vscode.TreeItem(`字号`, vscode.TreeItemCollapsibleState.Expanded),
new vscode.TreeItem(`字体`, vscode.TreeItemCollapsibleState.Expanded),
new vscode.TreeItem(`主题`, vscode.TreeItemCollapsibleState.Expanded),
new vscode.TreeItem(`主题色`, vscode.TreeItemCollapsibleState.Expanded),
new vscode.TreeItem(`计数状态`, vscode.TreeItemCollapsibleState.None),
new vscode.TreeItem(`Mac代码块`, vscode.TreeItemCollapsibleState.None),
new vscode.TreeItem(`微信外链转引用`, vscode.TreeItemCollapsibleState.None),
].map((item) => {
if (item.label === `计数状态`) {
item.command = {
command: `markdown.toggleCountStatus`,
title: `Toggle Count Status`,
arguments: [],
}
if (this.countStatus) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
}
else if (item.label === `Mac代码块`) {
item.command = {
command: `markdown.toggleMacCodeBlock`,
title: `Toggle Mac Code Block`,
arguments: [],
}
if (this.isMacCodeBlock) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
}
else if (item.label === `微信外链转引用`) {
item.command = {
command: `markdown.toggleCiteStatus`,
title: `Toggle Cite Status`,
arguments: [],
}
if (this.citeStatus) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
}
return item
}))
}
else if (element.label === `字号`) {
return Promise.resolve(fontSizeOptions.map((option) => {
const size = option.value
const label = option.label
const desc = option.desc
const item = new vscode.TreeItem(`${label} ${desc}`)
item.command = {
command: `markdown.setFontSize`,
title: `Set Font Size`,
arguments: [size],
}
if (size === this.currentFontSize) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
return item
}))
}
else if (element.label === `字体`) {
return Promise.resolve(fontFamilyOptions.map((option) => {
const font = option.value
const label = option.label
const desc = option.desc
const item = new vscode.TreeItem(`${label} ${desc}`)
item.command = {
command: `markdown.setFontFamily`,
title: `Set Font Family`,
arguments: [font],
}
if (font === this.currentFontFamily) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
return item
}))
}
else if (element.label === `主题`) {
return Promise.resolve(themeOptions.map((option) => {
const theme = option.value
const label = option.label
const desc = option.desc
const item = new vscode.TreeItem(`${label} ${desc}`)
item.command = {
command: `markdown.setTheme`,
title: `Set Theme`,
arguments: [theme],
}
if (theme === this.currentTheme) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
return item
}))
}
else if (element.label === `主题色`) {
return Promise.resolve(colorOptions.map((option) => {
const color = option.value
const label = option.label
const desc = option.desc
const item = new vscode.TreeItem(`${label} ${desc}`)
item.command = {
command: `markdown.setPrimaryColor`,
title: `Set Primary Color`,
arguments: [color],
}
if (color === this.currentPrimaryColor) {
item.iconPath = new vscode.ThemeIcon(`check`)
}
return item
}))
}
return Promise.resolve([])
}
updateFontSize(size: string) {
this.currentFontSize = size
this.context.workspaceState.update(`markdownPreview.fontSize`, size)
this._onDidChangeTreeData.fire(undefined)
}
updateTheme(theme: ThemeName) {
this.currentTheme = theme
this.context.workspaceState.update(`markdownPreview.theme`, theme)
this._onDidChangeTreeData.fire(undefined)
}
updatePrimaryColor(color: string) {
this.currentPrimaryColor = color
this.context.workspaceState.update(`markdownPreview.primaryColor`, color)
this._onDidChangeTreeData.fire(undefined)
}
updateFontFamily(font: string) {
this.currentFontFamily = font
this.context.workspaceState.update(`markdownPreview.fontFamily`, font)
this._onDidChangeTreeData.fire(undefined)
}
getCurrentFontSize() {
return this.currentFontSize
}
getCurrentFontSizeNumber() {
return Number(this.currentFontSize.replace(`px`, ``))
}
getCurrentTheme(): ThemeName {
return this.currentTheme
}
getCurrentPrimaryColor() {
return this.currentPrimaryColor
}
getCurrentFontFamily() {
return this.currentFontFamily
}
}