Skip to content

Commit 7a19a5f

Browse files
committed
feat: Now you can publish private or scheduled posts.
1 parent ab2ac41 commit 7a19a5f

12 files changed

+3421
-29
lines changed

main.js

+3,232-20
Large diffs are not rendered by default.

package-lock.json

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"esbuild": "0.17.3",
3838
"eslint-plugin-node": "^11.1.0",
3939
"file-type-checker": "^1.0.8",
40+
"imask": "^7.2.1",
4041
"juice": "^10.0.0",
4142
"lodash-es": "^4.17.21",
4243
"markdown-it": "^13.0.1",

src/i18n/en.json

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"settings_defaultPostStatusDesc": "Post status which will be published to WordPress.",
5151
"settings_defaultPostStatusDraft": "Draft",
5252
"settings_defaultPostStatusPublish": "Publish",
53+
"settings_defaultPostStatusPrivate": "Private",
5354
"settings_rememberLastSelectedCategories": "Remember last selected categories",
5455
"settings_rememberLastSelectedCategoriesDesc": "Remember last selected post categories of this site.",
5556
"settings_showWordPressEditPageModal": "Show WordPress edit confirmation",
@@ -94,6 +95,10 @@
9495
"publishModal_postStatus": "Post Status",
9596
"publishModal_postStatusDraft": "Draft",
9697
"publishModal_postStatusPublish": "Publish",
98+
"publishModal_postStatusPrivate": "Private",
99+
"publishModal_postStatusFuture": "Future",
100+
"publishModal_postDateTime": "Post Date",
101+
"publishModal_postDateTimeDesc": "With format YYYY-MM-DD or YYYY-MM-DD HH:MM:SS",
97102
"publishModal_commentStatus": "Comment Status",
98103
"publishModal_commentStatusOpen": "Open",
99104
"publishModal_commentStatusClosed": "Closed",

src/i18n/zh-cn.json

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"settings_defaultPostStatusDesc": "发布到 WordPress 的文章默认状态",
5151
"settings_defaultPostStatusDraft": "草稿",
5252
"settings_defaultPostStatusPublish": "正式发布",
53+
"settings_defaultPostStatusPrivate": "私有",
5354
"settings_rememberLastSelectedCategories": "记住上次选择的分类",
5455
"settings_rememberLastSelectedCategoriesDesc": "记住该站点上次发布时选择的分类",
5556
"settings_showWordPressEditPageModal": "显示 WordPress 编辑确认框",
@@ -94,6 +95,10 @@
9495
"publishModal_postStatus": "文章状态",
9596
"publishModal_postStatusDraft": "草稿",
9697
"publishModal_postStatusPublish": "正式发布",
98+
"publishModal_postStatusPrivate": "私有",
99+
"publishModal_postStatusFuture": "定时",
100+
"publishModal_postDateTime": "时间",
101+
"publishModal_postDateTimeDesc": "格式 YYYY-MM-DD 或 YYYY-MM-DD HH:MM:SS",
97102
"publishModal_commentStatus": "评论状态",
98103
"publishModal_commentStatusOpen": "开启",
99104
"publishModal_commentStatusClosed": "关闭",

src/settings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class WordpressSettingTab extends PluginSettingTab {
8383
dropdown
8484
.addOption(PostStatus.Draft, t('settings_defaultPostStatusDraft'))
8585
.addOption(PostStatus.Publish, t('settings_defaultPostStatusPublish'))
86-
// .addOption(PostStatus.Future, 'future')
86+
.addOption(PostStatus.Private, t('settings_defaultPostStatusPrivate'))
8787
.setValue(this.plugin.settings.defaultPostStatus)
8888
.onChange(async (value) => {
8989
this.plugin.settings.defaultPostStatus = value as PostStatus;

src/wp-api.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export const enum PostStatus {
22
Draft = 'draft',
33
Publish = 'publish',
4-
// Future = 'future'
4+
Private = 'private',
5+
Future = 'future'
56
}
67

78
export const enum CommentStatus {

src/wp-client.ts

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ export interface WordPressPostParams {
6969
* WordPress profile name.
7070
*/
7171
profileName?: string;
72+
73+
datetime?: Date;
7274
}
7375

7476
export interface WordPressPublishParams extends WordPressAuthParams {

src/wp-publish-modal.ts

+95-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ import { toNumber } from 'lodash-es';
66
import { MatterData } from './types';
77
import { ConfirmCode, openConfirmModal } from './confirm-modal';
88
import { AbstractModal } from './abstract-modal';
9+
import IMask, { DynamicMaskType, InputMask } from 'imask';
10+
import { SafeAny } from './utils';
11+
import { format, parse } from 'date-fns';
912

1013

1114
/**
1215
* WordPress publish modal.
1316
*/
1417
export class WpPublishModal extends AbstractModal {
1518

19+
private dateInputMask: InputMask<DynamicMaskType> | null = null;
20+
1621
constructor(
1722
readonly plugin: WordpressPlugin,
1823
private readonly categories: {
@@ -46,6 +51,9 @@ export class WpPublishModal extends AbstractModal {
4651
onClose() {
4752
const { contentEl } = this;
4853
contentEl.empty();
54+
if (this.dateInputMask) {
55+
this.dateInputMask.destroy();
56+
}
4957
}
5058

5159
private display(params: WordPressPostParams): void {
@@ -61,19 +69,103 @@ export class WpPublishModal extends AbstractModal {
6169
dropdown
6270
.addOption(PostStatus.Draft, this.t('publishModal_postStatusDraft'))
6371
.addOption(PostStatus.Publish, this.t('publishModal_postStatusPublish'))
64-
// .addOption(PostStatus.Future, 'future')
65-
.setValue(this.plugin.settings.defaultPostStatus)
72+
.addOption(PostStatus.Private, this.t('publishModal_postStatusPrivate'))
73+
.addOption(PostStatus.Future, this.t('publishModal_postStatusFuture'))
74+
.setValue(params.status)
6675
.onChange((value) => {
6776
params.status = value as PostStatus;
77+
this.display(params);
6878
});
6979
});
80+
81+
if (params.status === PostStatus.Future) {
82+
new Setting(contentEl)
83+
.setName(this.t('publishModal_postDateTime'))
84+
.setDesc(this.t('publishModal_postDateTimeDesc'))
85+
.addText(text => {
86+
const dateFormat = 'yyyy-MM-dd';
87+
const dateTimeFormat = 'yyyy-MM-dd HH:mm:ss';
88+
const dateBlocks = {
89+
yyyy: {
90+
mask: IMask.MaskedRange,
91+
from: 1970,
92+
to: 9999,
93+
},
94+
MM: {
95+
mask: IMask.MaskedRange,
96+
from: 1,
97+
to: 12,
98+
},
99+
dd: {
100+
mask: IMask.MaskedRange,
101+
from: 1,
102+
to: 31,
103+
},
104+
};
105+
const dateMask = {
106+
mask: Date,
107+
lazy: false,
108+
overwrite: true,
109+
};
110+
if (this.dateInputMask) {
111+
this.dateInputMask.destroy();
112+
}
113+
this.dateInputMask = IMask(text.inputEl, [
114+
{
115+
...dateMask,
116+
pattern: dateFormat,
117+
blocks: dateBlocks,
118+
format: (date: SafeAny) => format(date, dateFormat),
119+
parse: (str: string) => parse(str, dateFormat, new Date())
120+
},
121+
{
122+
...dateMask,
123+
pattern: dateTimeFormat,
124+
blocks: {
125+
...dateBlocks,
126+
HH: {
127+
mask: IMask.MaskedRange,
128+
from: 0,
129+
to: 23,
130+
},
131+
mm: {
132+
mask: IMask.MaskedRange,
133+
from: 0,
134+
to: 59,
135+
},
136+
ss: {
137+
mask: IMask.MaskedRange,
138+
from: 0,
139+
to: 59,
140+
},
141+
},
142+
format: (date: SafeAny) => format(date, dateTimeFormat),
143+
parse: (str: string) => parse(str, dateTimeFormat, new Date())
144+
}
145+
]);
146+
147+
this.dateInputMask.on('accept', () => {
148+
if (this.dateInputMask) {
149+
if (this.dateInputMask.masked.isComplete) {
150+
text.inputEl.style.borderColor = '';
151+
params.datetime = this.dateInputMask.typedValue;
152+
} else {
153+
text.inputEl.style.borderColor = 'red';
154+
}
155+
}
156+
});
157+
});
158+
} else {
159+
delete params.datetime;
160+
}
161+
70162
new Setting(contentEl)
71163
.setName(this.t('publishModal_commentStatus'))
72164
.addDropdown((dropdown) => {
73165
dropdown
74166
.addOption(CommentStatus.Open, this.t('publishModal_commentStatusOpen'))
75167
.addOption(CommentStatus.Closed, this.t('publishModal_commentStatusClosed'))
76-
.setValue(this.plugin.settings.defaultCommentStatus)
168+
.setValue(params.commentStatus)
77169
.onChange((value) => {
78170
params.commentStatus = value as CommentStatus;
79171
});

src/wp-rest-client.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import {
88
} from './wp-client';
99
import { AbstractWordPressClient } from './abstract-wp-client';
1010
import WordpressPlugin from './main';
11-
import { PostType, Term } from './wp-api';
11+
import { PostStatus, PostType, Term } from './wp-api';
1212
import { RestClient } from './rest-client';
1313
import { isArray, isFunction, isNumber, isObject, isString, template } from 'lodash-es';
1414
import { SafeAny } from './utils';
1515
import { WpProfile } from './wp-profile';
1616
import { FormItemNameMapper, FormItems, Media } from './types';
17+
import { formatISO } from 'date-fns';
1718

1819

1920
interface WpRestEndpoint {
@@ -65,6 +66,10 @@ export class WpRestClient extends AbstractWordPressClient {
6566
} else {
6667
url = getUrl(this.context.endpoints?.newPost, 'wp-json/wp/v2/posts');
6768
}
69+
const extra: Record<string, string> = {};
70+
if (postParams.status === PostStatus.Future) {
71+
extra.date = formatISO(postParams.datetime ?? new Date());
72+
}
6873
const resp: SafeAny = await this.client.httpPost(
6974
url,
7075
{
@@ -73,7 +78,8 @@ export class WpRestClient extends AbstractWordPressClient {
7378
status: postParams.status,
7479
comment_status: postParams.commentStatus,
7580
categories: postParams.categories,
76-
tags: postParams.tags ?? []
81+
tags: postParams.tags ?? [],
82+
...extra
7783
},
7884
{
7985
headers: this.context.getHeaders(certificate)

src/wp-xml-rpc-client.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from './wp-client';
1010
import { XmlRpcClient } from './xmlrpc-client';
1111
import { AbstractWordPressClient } from './abstract-wp-client';
12-
import { PostType, PostTypeConst, Term } from './wp-api';
12+
import { PostStatus, PostType, PostTypeConst, Term } from './wp-api';
1313
import { SafeAny, showError } from './utils';
1414
import { WpProfile } from './wp-profile';
1515
import { Media } from './types';
@@ -69,6 +69,12 @@ export class WpXmlRpcClient extends AbstractWordPressClient {
6969
}
7070
};
7171
}
72+
if (postParams.status === PostStatus.Future) {
73+
publishContent = {
74+
...publishContent,
75+
post_date: postParams.datetime ?? new Date()
76+
};
77+
}
7278
let publishPromise;
7379
if (postParams.postId) {
7480
publishPromise = this.client.methodCall('wp.editPost', [

src/xmlrpc-client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class XmlRpcClient {
103103
value.appendChild(boolean);
104104
} else if (isDate(data)) {
105105
const date = doc.createElement('dateTime.iso8601');
106-
date.appendText(format(data as Date, 'yyyyMMddTHH:mm:ss'));
106+
date.appendText(format(data as Date, "yyyyMMdd'T'HH:mm:ss"));
107107
value.appendChild(date);
108108
} else if (isArray(data)) {
109109
const array = doc.createElement('array');

0 commit comments

Comments
 (0)