-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewModelFactory.ts
227 lines (196 loc) · 7.1 KB
/
ViewModelFactory.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
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
221
222
223
224
225
226
227
import { MicroPluginContainerInterface } from '@base/MicroPluginContainer'
import { ServiceFactory, ServiceFactoryInterface } from '@factories/ServiceFactory'
import { MarkdownPage, MarkdownPageInterface } from '@models/MarkdownPage'
import { MarkdownPost, MarkdownPostInterface } from '@models/MarkdownPost'
import { FrontmatterServiceInterface } from '@services/FrontmatterService'
import { ErrorViewModel } from '@views/ErrorViewModel'
import { MicroPluginSettingsViewModel } from '@views/MicroPluginSettingsViewModel'
import { PublishPageViewModel } from '@views/PublishPageViewModel'
import { PublishPostViewModel } from '@views/PublishPostViewModel'
import { TagSuggestionDelegate, TagSuggestionViewModel } from '@views/TagSuggestionViewModel'
import { UpdatePageViewModel } from '@views/UpdatePageViewModel'
import { UpdatePostViewModel } from '@views/UpdatePostViewModel'
import { MarkdownView } from 'obsidian'
export interface ViewModelFactoryInterface {
// Builds either the `PublishPostViewModel`, for publishing a note
// to Micro.blog, or the `UpdatePostViewModel`, to update a post.
makeSubmitPostViewModel(
markdownView: MarkdownView
): PublishPostViewModel | UpdatePostViewModel
// Builds either the `PublishPageViewModel`, for publishing a page
// to Micro.blog, or the `UpdatePageViewModel`, to update a page.
makeSubmitPageViewModel(
markdownView: MarkdownView
): PublishPageViewModel | UpdatePageViewModel
// Builds the `MicroPluginSettingsViewModel`, used by the plugin
// Settings.
makeMicroPluginSettingsViewModel(): MicroPluginSettingsViewModel
// Builds the `TagSuggestionViewModel`.
makeTagSuggestionViewModel(
excluding: Array<string>,
delegate?: TagSuggestionDelegate
): TagSuggestionViewModel
// Builds the Empty Post Error View Model.
makeEmptyPostErrorViewModel(): ErrorViewModel
// Builds the Empty Page Error View Model.
makeEmptyPageErrorViewModel(): ErrorViewModel
}
/*
* View Model Factory builds all the View Models in the plugin.
* It simplifies building View Models since all the resolved dependencies
* are already available via the factory.
*/
export class ViewModelFactory implements ViewModelFactoryInterface {
// Properties
private container: MicroPluginContainerInterface
private serviceFactory: ServiceFactoryInterface
// Life cycle
constructor(
container: MicroPluginContainerInterface
) {
this.container = container
this.serviceFactory = new ServiceFactory(container)
}
// Public
public makeSubmitPostViewModel(
markdownView: MarkdownView
): PublishPostViewModel | UpdatePostViewModel {
const frontmatterService = this.serviceFactory
.makeFrontmatterService(markdownView.file)
const post = new MarkdownPost(
frontmatterService,
markdownView
)
if (post.url && post.url.length > 0) {
return this.makeUpdatePostViewModel(
post.url,
post.title,
post.content
)
} else {
return this.makePublishPostViewModel(
post,
frontmatterService
)
}
}
public makeSubmitPageViewModel(
markdownView: MarkdownView
): PublishPageViewModel | UpdatePageViewModel {
const frontmatterService = this.serviceFactory
.makeFrontmatterService(markdownView.file)
const page = new MarkdownPage(
frontmatterService,
markdownView
)
if (page.url && page.url.length > 0) {
return this.makeUpdatePageViewModel(
page.url,
page.title,
page.content
)
} else {
return this.makePublishPageViewModel(
page,
frontmatterService
)
}
}
public makeMicroPluginSettingsViewModel(): MicroPluginSettingsViewModel {
return new MicroPluginSettingsViewModel(
this.container.plugin,
this.container.settings,
this.container.networkClient,
this.container.networkRequestFactory
)
}
public makeTagSuggestionViewModel(
excluding: Array<string>,
delegate?: TagSuggestionDelegate,
): TagSuggestionViewModel {
const suggestions = this.container.settings.tagSuggestions
.filter(element =>
!excluding.includes(element)
)
const viewModel = new TagSuggestionViewModel(
suggestions
)
viewModel.delegate = delegate
return viewModel
}
public makeEmptyPostErrorViewModel(): ErrorViewModel {
return new ErrorViewModel(
'Oops',
'Micro.blog does not support blank posts. Please write something before trying again.'
)
}
public makeEmptyPageErrorViewModel(): ErrorViewModel {
return new ErrorViewModel(
'Oops',
'Micro.blog does not support blank page. Please write something before trying again.'
)
}
// Private
private makePublishPostViewModel(
post: MarkdownPostInterface,
frontmatterService: FrontmatterServiceInterface
): PublishPostViewModel {
return new PublishPostViewModel(
post.title,
post.content,
post.tags || this.container.settings.defaultTags,
this.container.settings.postVisibility,
this.container.settings.blogs,
this.container.settings.selectedBlogID,
this.container.networkClient,
frontmatterService,
this.container.networkRequestFactory,
this
)
}
private makeUpdatePostViewModel(
url: string,
title: string,
content: string
): UpdatePostViewModel {
return new UpdatePostViewModel(
url,
title,
content,
this.container.settings.blogs,
this.container.settings.selectedBlogID,
this.container.networkClient,
this.container.networkRequestFactory
)
}
private makePublishPageViewModel(
page: MarkdownPageInterface,
frontmatterService: FrontmatterServiceInterface
): PublishPageViewModel {
return new PublishPageViewModel(
page.title,
page.content,
this.container.settings.blogs,
this.container.settings.selectedBlogID,
this.container.settings.includePagesInNavigation,
this.container.networkClient,
frontmatterService,
this.container.networkRequestFactory
)
}
private makeUpdatePageViewModel(
url: string,
title: string,
content: string
): UpdatePageViewModel {
return new UpdatePageViewModel(
url,
title,
content,
this.container.settings.blogs,
this.container.settings.selectedBlogID,
this.container.networkClient,
this.container.networkRequestFactory
)
}
}