1- import { App , Modal , Notice } from 'obsidian' ;
1+ import { Modal , Notice } from 'obsidian' ;
22import WordpressPlugin from './main' ;
3- import { WpLoginModal } from './wp-login-modal' ;
3+ import { openLoginModal } from './wp-login-modal' ;
44import {
55 WordPressAuthParams ,
66 WordPressClient ,
@@ -9,15 +9,17 @@ import {
99 WordPressPostParams ,
1010 WordPressPublishParams
1111} from './wp-client' ;
12- import { WpPublishModal } from './wp-publish-modal' ;
12+ import { openPublishModal } from './wp-publish-modal' ;
1313import { Term } from './wp-api' ;
14- import { ERROR_NOTICE_TIMEOUT } from './consts' ;
14+ import { ERROR_NOTICE_TIMEOUT , WP_DEFAULT_PROFILE_NAME } from './consts' ;
1515import matter from 'gray-matter' ;
1616import yaml from 'js-yaml' ;
17- import { isPromiseFulfilledResult , openWithBrowser , SafeAny } from './utils' ;
18- import { PostPublishedModal } from './post-published-modal' ;
17+ import { doClientPublish , isPromiseFulfilledResult , openWithBrowser , SafeAny } from './utils' ;
18+ import { openPostPublishedModal } from './post-published-modal' ;
1919import { WpProfile } from './wp-profile' ;
2020import { AppState } from './app-state' ;
21+ import { ConfirmCode , openConfirmModal } from './confirm-modal' ;
22+ import { isNil } from 'lodash-es' ;
2123
2224
2325const matterOptions = {
@@ -36,7 +38,6 @@ const matterOptions = {
3638export abstract class AbstractWordPressClient implements WordPressClient {
3739
3840 protected constructor (
39- protected readonly app : App ,
4041 protected readonly plugin : WordpressPlugin ,
4142 protected readonly profile : WpProfile
4243 ) { }
@@ -75,19 +76,52 @@ export abstract class AbstractWordPressClient implements WordPressClient {
7576 } ) ;
7677 }
7778
78- const { activeEditor } = this . app . workspace ;
79+ const { activeEditor } = app . workspace ;
7980 if ( activeEditor && activeEditor . file ) {
80- const publishToWordPress = async (
81- username : string | null ,
82- password : string | null ,
83- loginModal ?: Modal
84- ) => {
81+ ( async ( ) => {
82+ let username = null ;
83+ let password = null ;
84+ let loginModal ;
85+ if ( this . openLoginModal ( ) ) {
86+ if ( this . profile . username && this . profile . password ) {
87+ // saved username and password found
88+ username = this . profile . username ;
89+ password = this . profile . password ;
90+ } else {
91+ const loginModalReturns = await openLoginModal ( this . plugin , this . profile ) ;
92+ username = loginModalReturns . username ;
93+ password = loginModalReturns . password ;
94+ loginModal = loginModalReturns . loginModal ;
95+ }
96+ }
97+ // start publishing...
8598 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
8699 const noteTitle = activeEditor . file ! . basename ;
87100 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
88- const rawContent = await this . app . vault . read ( activeEditor . file ! ) ;
101+ const rawContent = await app . vault . read ( activeEditor . file ! ) ;
89102 const { content, data : matterData } = matter ( rawContent , matterOptions ) ;
90103
104+ if ( ! isNil ( matterData . profileName )
105+ && matterData . profileName . length > 0
106+ && matterData . profileName !== this . profile . name
107+ ) {
108+ const confirm = await openConfirmModal ( {
109+ message : this . plugin . i18n . t ( 'error_profileNotMatch' ) ,
110+ cancelText : this . plugin . i18n . t ( 'profileNotMatch_useOld' , {
111+ profileName : matterData . profileName
112+ } ) ,
113+ confirmText : this . plugin . i18n . t ( 'profileNotMatch_useNew' , {
114+ profileName : this . profile . name
115+ } )
116+ } , this . plugin ) ;
117+ if ( confirm . code === ConfirmCode . Cancel ) {
118+ doClientPublish ( this . plugin , matterData . profileName ) ;
119+ return Promise . resolve ( ) ;
120+ } else {
121+ delete matterData . postId ;
122+ matterData . categories = this . profile . lastSelectedCategories ?? [ 1 ] ;
123+ }
124+ }
91125 const validateUserResult = await this . validateUser ( { username, password } ) ;
92126 if ( validateUserResult . code === WordPressClientReturnCode . OK ) {
93127 if ( defaultPostParams ) {
@@ -106,53 +140,22 @@ export abstract class AbstractWordPressClient implements WordPressClient {
106140 password
107141 } ) ;
108142 const selectedCategories = matterData . categories as number [ ] ?? this . profile . lastSelectedCategories ;
109- new WpPublishModal (
110- this . app ,
111- this . plugin ,
112- categories ,
113- selectedCategories ,
114- async ( postParams , publishModal ) => {
115- const params = this . readFromFrontMatter ( noteTitle , matterData , postParams ) ;
116- params . content = content ;
117- const result = await this . doPublish ( {
118- username,
119- password,
120- postParams : params ,
121- matterData
122- } , loginModal , publishModal ) ;
123- resolve ( result ) ;
124- }
125- ) . open ( ) ;
143+ const { postParams, publishModal } = await openPublishModal ( this . plugin , categories , selectedCategories ) ;
144+ const params = this . readFromFrontMatter ( noteTitle , matterData , postParams ) ;
145+ params . content = content ;
146+ const result = await this . doPublish ( {
147+ username,
148+ password,
149+ postParams : params ,
150+ matterData
151+ } , loginModal , publishModal ) ;
152+ resolve ( result ) ;
126153 }
127154 } else {
128155 const invalidUsernameOrPassword = this . plugin . i18n . t ( 'error_invalidUser' ) ;
129156 new Notice ( invalidUsernameOrPassword , ERROR_NOTICE_TIMEOUT ) ;
130157 }
131- } ;
132-
133- if ( this . openLoginModal ( ) ) {
134- if ( this . profile . username && this . profile . password ) {
135- // saved username and password found
136- publishToWordPress ( this . profile . username , this . profile . password ) . then ( ( ) => {
137- // make compiler happy
138- } ) ;
139- } else {
140- new WpLoginModal (
141- this . app ,
142- this . plugin ,
143- this . profile ,
144- ( username , password , loginModal ) => {
145- publishToWordPress ( username , password , loginModal ) . then ( ( ) => {
146- // make compiler happy
147- } ) ;
148- }
149- ) . open ( ) ;
150- }
151- } else {
152- publishToWordPress ( null , null ) . then ( ( ) => {
153- // make compiler happy
154- } ) ;
155- }
158+ } ) ( ) ;
156159 } else {
157160 const error = 'There is no editor or file found. Nothing will be published.' ;
158161 console . warn ( error ) ;
@@ -204,6 +207,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
204207 const postId = ( result . data as SafeAny ) . postId ;
205208 if ( postId ) {
206209 // save post id to front-matter
210+ matterData . profileName = this . profile . name ;
207211 matterData . postId = postId ;
208212 matterData . categories = postParams . categories ;
209213 const modified = matter . stringify ( postParams . content , matterData , matterOptions ) ;
@@ -215,13 +219,13 @@ export abstract class AbstractWordPressClient implements WordPressClient {
215219 }
216220
217221 if ( this . plugin . settings . showWordPressEditConfirm ) {
218- new PostPublishedModal ( this . app , this . plugin , ( modal : Modal ) => {
219- openWithBrowser ( `${ this . profile . endpoint } /wp-admin/post.php` , {
220- action : 'edit' ,
221- post : postId
222+ openPostPublishedModal ( this . plugin )
223+ . then ( ( ) => {
224+ openWithBrowser ( `${ this . profile . endpoint } /wp-admin/post.php` , {
225+ action : 'edit' ,
226+ post : postId
227+ } ) ;
222228 } ) ;
223- modal . close ( ) ;
224- } ) . open ( ) ;
225229 }
226230 }
227231 }
@@ -260,6 +264,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
260264 if ( matterData . postId ) {
261265 postParams . postId = matterData . postId ;
262266 }
267+ postParams . profileName = matterData . profileName ?? WP_DEFAULT_PROFILE_NAME ;
263268 if ( matterData . categories ) {
264269 postParams . categories = matterData . categories as number [ ] ?? this . profile . lastSelectedCategories ;
265270 }
@@ -270,7 +275,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
270275 }
271276
272277 private updateFrontMatter ( value : string ) : void {
273- const { activeEditor } = this . app . workspace ;
278+ const { activeEditor } = app . workspace ;
274279 if ( activeEditor ) {
275280 const editor = activeEditor . editor ;
276281 if ( editor ) {
0 commit comments