Show warning if post is empty

This fixes #10.
This commit is contained in:
Otavio Cordeiro 2023-05-31 12:03:36 +02:00
parent e2ebff9c5d
commit f424cf8a8d
No known key found for this signature in database
4 changed files with 88 additions and 7 deletions

View File

@ -1,3 +1,4 @@
import { ErrorView } from '@views/ErrorView'
import { Notice, Plugin } from 'obsidian'
import { MicroPluginContainerInterface, MicroPluginContainer } from '@base/MicroPluginContainer'
import { MicroPluginSettingsView } from '@views/MicroPluginSettingsView'
@ -32,12 +33,18 @@ export default class MicroPlugin extends Plugin {
id: 'microblog-publish-command',
name: 'Post to Micro.blog',
editorCallback: (editor, markdownView) => {
new PublishView(
this.viewModelFactory.makePublishViewModel(
markdownView.file.basename,
editor.getValue()
)
).open()
if (editor.getValue().trim().length == 0) {
new ErrorView(
this.viewModelFactory.makeEmptyPostErrorViewModel()
).open()
} else {
new PublishView(
this.viewModelFactory.makePublishViewModel(
markdownView.file.basename,
editor.getValue()
)
).open()
}
}
})

View File

@ -1,6 +1,7 @@
import { MicroPluginSettingsViewModel } from '@views/MicroPluginSettingsViewModel'
import { PublishViewModel } from '@views/PublishViewModel'
import { TagSuggestionViewModel, TagSuggestionDelegate } from '@views/TagSuggestionViewModel'
import { ErrorViewModel } from '@views/ErrorViewModel'
import { MicroPluginContainerInterface } from '@base/MicroPluginContainer'
export interface ViewModelFactoryInterface {
@ -21,6 +22,9 @@ export interface ViewModelFactoryInterface {
excluding: Array<string>,
delegate?: TagSuggestionDelegate
): TagSuggestionViewModel
// Builds the Empty Post Error View Model.
makeEmptyPostErrorViewModel(): ErrorViewModel
}
/*
@ -87,4 +91,11 @@ export class ViewModelFactory implements ViewModelFactoryInterface {
return viewModel
}
}
public makeEmptyPostErrorViewModel(): ErrorViewModel {
return new ErrorViewModel(
"Oops",
"Micro.blog doesn't support blank posts. Write something first and try again."
)
}
}

42
src/views/ErrorView.ts Normal file
View File

@ -0,0 +1,42 @@
import { Modal } from 'obsidian'
import { ErrorViewModel } from '@views/ErrorViewModel'
/*
* Error View subclasses Modal.
*/
export class ErrorView extends Modal {
// Properties
private viewModel: ErrorViewModel
// Life cycle
constructor(
viewModel: ErrorViewModel
) {
super(app)
this.viewModel = viewModel
}
// Public
public onOpen() {
super.onOpen()
const { contentEl } = this
contentEl.empty()
contentEl.createEl('h2', { text: this.viewModel.title })
contentEl.createEl('p', { text: this.viewModel.message })
}
public onClose() {
super.onClose()
const { contentEl } = this
contentEl.empty()
}
}

View File

@ -0,0 +1,21 @@
/*
* This view model drives the content and interactions with the
* error view.
*/
export class ErrorViewModel {
// Properties
readonly title: string
readonly message: string
// Life cycle
constructor(
title: string,
message: string
) {
this.title = title
this.message = message
}
}