1
- import { App , Modal , Notice } from 'obsidian' ;
1
+ import { Modal , Notice } from 'obsidian' ;
2
2
import WordpressPlugin from './main' ;
3
- import { WpLoginModal } from './wp-login-modal' ;
3
+ import { openLoginModal } from './wp-login-modal' ;
4
4
import {
5
5
WordPressAuthParams ,
6
6
WordPressClient ,
@@ -9,15 +9,17 @@ import {
9
9
WordPressPostParams ,
10
10
WordPressPublishParams
11
11
} from './wp-client' ;
12
- import { WpPublishModal } from './wp-publish-modal' ;
12
+ import { openPublishModal } from './wp-publish-modal' ;
13
13
import { Term } from './wp-api' ;
14
- import { ERROR_NOTICE_TIMEOUT } from './consts' ;
14
+ import { ERROR_NOTICE_TIMEOUT , WP_DEFAULT_PROFILE_NAME } from './consts' ;
15
15
import matter from 'gray-matter' ;
16
16
import 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' ;
19
19
import { WpProfile } from './wp-profile' ;
20
20
import { AppState } from './app-state' ;
21
+ import { ConfirmCode , openConfirmModal } from './confirm-modal' ;
22
+ import { isNil } from 'lodash-es' ;
21
23
22
24
23
25
const matterOptions = {
@@ -36,7 +38,6 @@ const matterOptions = {
36
38
export abstract class AbstractWordPressClient implements WordPressClient {
37
39
38
40
protected constructor (
39
- protected readonly app : App ,
40
41
protected readonly plugin : WordpressPlugin ,
41
42
protected readonly profile : WpProfile
42
43
) { }
@@ -75,19 +76,52 @@ export abstract class AbstractWordPressClient implements WordPressClient {
75
76
} ) ;
76
77
}
77
78
78
- const { activeEditor } = this . app . workspace ;
79
+ const { activeEditor } = app . workspace ;
79
80
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...
85
98
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86
99
const noteTitle = activeEditor . file ! . basename ;
87
100
// 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 ! ) ;
89
102
const { content, data : matterData } = matter ( rawContent , matterOptions ) ;
90
103
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
+ }
91
125
const validateUserResult = await this . validateUser ( { username, password } ) ;
92
126
if ( validateUserResult . code === WordPressClientReturnCode . OK ) {
93
127
if ( defaultPostParams ) {
@@ -106,53 +140,22 @@ export abstract class AbstractWordPressClient implements WordPressClient {
106
140
password
107
141
} ) ;
108
142
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 ) ;
126
153
}
127
154
} else {
128
155
const invalidUsernameOrPassword = this . plugin . i18n . t ( 'error_invalidUser' ) ;
129
156
new Notice ( invalidUsernameOrPassword , ERROR_NOTICE_TIMEOUT ) ;
130
157
}
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
+ } ) ( ) ;
156
159
} else {
157
160
const error = 'There is no editor or file found. Nothing will be published.' ;
158
161
console . warn ( error ) ;
@@ -204,6 +207,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
204
207
const postId = ( result . data as SafeAny ) . postId ;
205
208
if ( postId ) {
206
209
// save post id to front-matter
210
+ matterData . profileName = this . profile . name ;
207
211
matterData . postId = postId ;
208
212
matterData . categories = postParams . categories ;
209
213
const modified = matter . stringify ( postParams . content , matterData , matterOptions ) ;
@@ -215,13 +219,13 @@ export abstract class AbstractWordPressClient implements WordPressClient {
215
219
}
216
220
217
221
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
+ } ) ;
222
228
} ) ;
223
- modal . close ( ) ;
224
- } ) . open ( ) ;
225
229
}
226
230
}
227
231
}
@@ -260,6 +264,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
260
264
if ( matterData . postId ) {
261
265
postParams . postId = matterData . postId ;
262
266
}
267
+ postParams . profileName = matterData . profileName ?? WP_DEFAULT_PROFILE_NAME ;
263
268
if ( matterData . categories ) {
264
269
postParams . categories = matterData . categories as number [ ] ?? this . profile . lastSelectedCategories ;
265
270
}
@@ -270,7 +275,7 @@ export abstract class AbstractWordPressClient implements WordPressClient {
270
275
}
271
276
272
277
private updateFrontMatter ( value : string ) : void {
273
- const { activeEditor } = this . app . workspace ;
278
+ const { activeEditor } = app . workspace ;
274
279
if ( activeEditor ) {
275
280
const editor = activeEditor . editor ;
276
281
if ( editor ) {
0 commit comments