Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to include (or not) a new page to the blog navigation #15

Merged
merged 1 commit into from Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/factories/ViewModelFactory.ts
Expand Up @@ -202,6 +202,7 @@ export class ViewModelFactory implements ViewModelFactoryInterface {
page.content,
this.container.settings.blogs,
this.container.settings.selectedBlogID,
this.container.settings.includePagesInNavigation,
this.container.networkClient,
frontmatterService,
this.container.networkRequestFactory
Expand Down
9 changes: 6 additions & 3 deletions src/networking/NetworkRequestFactory.ts
Expand Up @@ -19,7 +19,8 @@ export interface NetworkRequestFactoryInterface {
makePublishPageRequest(
title: string,
content: string,
blogID: string
blogID: string,
navigation: boolean
): NetworkRequest

// Builds the configuration request, `NetworkRequest` used to "log in"
Expand Down Expand Up @@ -91,13 +92,15 @@ export class NetworkRequestFactory implements NetworkRequestFactoryInterface {
makePublishPageRequest(
title: string,
content: string,
blogID: string
blogID: string,
navigation: boolean
): NetworkRequest {
const parameters = new URLSearchParams([
['h', 'entry'],
['name', title],
['content', content],
['mp-channel', 'pages']
['mp-channel', 'pages'],
['mp-navigation', navigation.toString()]
])

if (blogID.length > 0 && blogID != 'default') {
Expand Down
7 changes: 6 additions & 1 deletion src/stores/StoredSettings.ts
Expand Up @@ -23,6 +23,10 @@ export interface StoredSettings {
// List of tag suggestions for new posts,
// retrieved from Micro.blog for the selected blog.
tagSuggestions: Array<string>

// Boolean indicating if pages should be added
// to the blog navigation.
includePagesInNavigation: boolean
}

// Default values for the plugin.
Expand All @@ -32,5 +36,6 @@ export const defaultSettings: StoredSettings = {
postVisibility: 'draft',
blogs: {},
selectedBlogID: 'default',
tagSuggestions: []
tagSuggestions: [],
includePagesInNavigation: false
}
21 changes: 19 additions & 2 deletions src/views/MicroPluginSettingsView.ts
Expand Up @@ -129,10 +129,11 @@ export class MicroPluginSettingsView extends PluginSettingTab implements MicroPl
const { containerEl } = this

containerEl.empty()
containerEl.createEl('h2', { text: 'Blog' })

new Setting(containerEl)
.setName('Blog')
.setDesc('Default blog for new posts.')
.setDesc('Default blog for new posts and pages.')
.addDropdown(dropDown => dropDown
.addOptions(this.viewModel.blogs)
.setValue(this.viewModel.selectedBlogID)
Expand All @@ -151,6 +152,8 @@ export class MicroPluginSettingsView extends PluginSettingTab implements MicroPl
})
)

containerEl.createEl('h2', { text: 'Posts' })

new Setting(containerEl)
.setName('Categories')
.setDesc('Default list of categories for new posts.')
Expand All @@ -163,7 +166,7 @@ export class MicroPluginSettingsView extends PluginSettingTab implements MicroPl
)

new Setting(containerEl)
.setName('Post visibility')
.setName('Visibility')
.setDesc('Default visibility for new posts.')
.addDropdown(dropDown => dropDown
.addOption('draft', 'Draft')
Expand All @@ -174,6 +177,20 @@ export class MicroPluginSettingsView extends PluginSettingTab implements MicroPl
})
)

containerEl.createEl('h2', { text: 'Pages' })

new Setting(containerEl)
.setName('Navigation')
.setDesc('Default setting to determine whether new pages should be included in the blog\'s navigation automatically.')
.addToggle(toggle => toggle
.setValue(this.viewModel.includePagesInNavigation)
.onChange(value => {
this.viewModel.includePagesInNavigation = value
})
)

containerEl.createEl('h2', { text: 'Misc.' })

new Setting(this.containerEl)
.setName('Sponsor')
.setDesc('Enjoying this plugin? Show your appreciation with a cup of coffee! 😊☕')
Expand Down
9 changes: 9 additions & 0 deletions src/views/MicroPluginSettingsViewModel.ts
Expand Up @@ -118,6 +118,15 @@ export class MicroPluginSettingsViewModel {
this.plugin.saveSettings()
}

public get includePagesInNavigation(): boolean {
return this.settings.includePagesInNavigation
}

public set includePagesInNavigation(value: boolean) {
this.settings.includePagesInNavigation = value
this.plugin.saveSettings()
}

public async validate() {
console.log('Logging in')

Expand Down
10 changes: 10 additions & 0 deletions src/views/PublishPageView.ts
Expand Up @@ -70,6 +70,16 @@ export class PublishPageView extends Modal implements PublishPageViewModelDelega
)
}

new Setting(contentEl)
.setName('Navigation')
.setDesc('Override the default setting to determine whether new pages should be included in the blog\'s navigation automatically for this page.')
.addToggle(toggle => toggle
.setValue(this.viewModel.includeInNavigation)
.onChange(value => {
this.viewModel.includeInNavigation = value
})
)

new Setting(contentEl)
.addButton(button => button
.setButtonText('Publish')
Expand Down
14 changes: 13 additions & 1 deletion src/views/PublishPageViewModel.ts
Expand Up @@ -37,6 +37,7 @@ export class PublishPageViewModel {
private titleWrappedValue: string
private content: string
private selectedBlogIDWrappedValue: string
private includeInNavigationWrappedValue: boolean
private networkClient: NetworkClientInterface
private frontmatterService: FrontmatterServiceInterface
private networkRequestFactory: NetworkRequestFactoryInterface
Expand All @@ -49,6 +50,7 @@ export class PublishPageViewModel {
content: string,
blogs: Record<string, string>,
selectedBlogID: string,
includeInNavigation: boolean,
networkClient: NetworkClientInterface,
frontmatterService: FrontmatterServiceInterface,
networkRequestFactory: NetworkRequestFactoryInterface
Expand All @@ -57,6 +59,7 @@ export class PublishPageViewModel {
this.content = content
this.blogs = blogs
this.selectedBlogIDWrappedValue = selectedBlogID
this.includeInNavigationWrappedValue = includeInNavigation
this.isSubmitting = false
this.networkClient = networkClient
this.frontmatterService = frontmatterService
Expand Down Expand Up @@ -85,6 +88,14 @@ export class PublishPageViewModel {
this.selectedBlogIDWrappedValue = value
}

public get includeInNavigation(): boolean {
return this.includeInNavigationWrappedValue
}

public set includeInNavigation(value: boolean) {
this.includeInNavigationWrappedValue = value
}

public clearTitle() {
this.title = ''
this.delegate?.publishDidClearTitle()
Expand Down Expand Up @@ -112,7 +123,8 @@ export class PublishPageViewModel {
const response = this.networkRequestFactory.makePublishPageRequest(
this.title,
this.content,
this.selectedBlogID
this.selectedBlogID,
this.includeInNavigation
)

const result = await this.networkClient.run<PublishResponse>(
Expand Down