Skip to content

Commit 98258bc

Browse files
committed
fix: No back button for modals on some mobile.
1 parent 401fbcb commit 98258bc

12 files changed

Lines changed: 324 additions & 270 deletions

main.js

Lines changed: 186 additions & 135 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"esbuild": "0.17.3",
3838
"eslint-plugin-node": "^11.1.0",
3939
"file-type-checker": "^1.0.8",
40-
"juice": "^9.1.0",
40+
"juice": "^10.0.0",
4141
"lodash-es": "^4.17.21",
4242
"markdown-it": "^13.0.1",
4343
"mathjax-full": "^3.2.2",

src/abstract-modal.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Modal, Platform } from 'obsidian';
2+
import WordpressPlugin from './main';
3+
import { TranslateKey } from './i18n';
4+
5+
export abstract class AbstractModal extends Modal {
6+
7+
protected constructor(
8+
protected readonly plugin: WordpressPlugin
9+
) {
10+
super(plugin.app);
11+
}
12+
13+
protected t(key: TranslateKey, vars?: Record<string, string>): string {
14+
return this.plugin.i18n.t(key, vars);
15+
}
16+
17+
protected createHeader(title: string): void {
18+
const { contentEl } = this;
19+
20+
const headerDiv = contentEl.createDiv();
21+
headerDiv.addClass('modal-header');
22+
headerDiv.createEl('h1', { text: title });
23+
if (Platform.isMobile) {
24+
const backButton = headerDiv.createEl('button', { text: this.t('common_back') });
25+
backButton.addEventListener('click', () => {
26+
this.close();
27+
});
28+
}
29+
}
30+
31+
}

src/i18n/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"ribbon_iconTitle": "WordPress Publish",
2424
"command_publish": "Publish current note",
2525
"command_publishWithDefault": "Publish current note with default options",
26+
"common_back": "Back",
2627
"confirmModal_title": "Confirmation",
2728
"confirmModal_cancel": "Cancel",
2829
"confirmModal_confirm": "Confirm",

src/i18n/zh-cn.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"ribbon_iconTitle": "发布到 WordPress",
2424
"command_publish": "发布当前笔记",
2525
"command_publishWithDefault": "使用默认参数发布当前笔记",
26+
"common_back": "返回",
2627
"confirmModal_title": "需要确认",
2728
"confirmModal_cancel": "取消",
2829
"confirmModal_confirm": "确认",

src/wp-login-modal.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Modal, Setting } from 'obsidian';
22
import WordpressPlugin from './main';
3-
import { TranslateKey } from './i18n';
43
import { WpProfile } from './wp-profile';
54
import { WordPressAuthParams } from './wp-client';
65
import { showError } from './utils';
6+
import { AbstractModal } from './abstract-modal';
77

88
export function openLoginModal(
99
plugin: WordpressPlugin,
@@ -30,30 +30,26 @@ export function openLoginModal(
3030
/**
3131
* WordPress login modal with username and password inputs.
3232
*/
33-
export class WpLoginModal extends Modal {
33+
export class WpLoginModal extends AbstractModal {
3434

3535
constructor(
36-
private readonly plugin: WordpressPlugin,
36+
readonly plugin: WordpressPlugin,
3737
private readonly profile: WpProfile,
3838
private readonly onSubmit: (auth: WordPressAuthParams, modal: Modal) => void
3939
) {
40-
super(plugin.app);
40+
super(plugin);
4141
}
4242

4343
onOpen() {
44-
const t = (key: TranslateKey, vars?: Record<string, string>): string => {
45-
return this.plugin.i18n.t(key, vars);
46-
};
47-
4844
const { contentEl } = this;
4945

50-
contentEl.createEl('h1', { text: t('loginModal_title') });
46+
this.createHeader(this.t('loginModal_title'));
5147

5248
let username = this.profile.username;
5349
let password = this.profile.password;
5450
new Setting(contentEl)
55-
.setName(t('loginModal_username'))
56-
.setDesc(t('loginModal_usernameDesc', { url: this.profile.endpoint }))
51+
.setName(this.t('loginModal_username'))
52+
.setDesc(this.t('loginModal_usernameDesc', { url: this.profile.endpoint }))
5753
.addText(text => {
5854
text
5955
.setValue(this.profile.username ?? '')
@@ -71,8 +67,8 @@ export class WpLoginModal extends Modal {
7167
}
7268
});
7369
new Setting(contentEl)
74-
.setName(t('loginModal_password'))
75-
.setDesc(t('loginModal_passwordDesc', { url: this.profile.endpoint }))
70+
.setName(this.t('loginModal_password'))
71+
.setDesc(this.t('loginModal_passwordDesc', { url: this.profile.endpoint }))
7672
.addText(text => {
7773
text
7874
.setValue(this.profile.password ?? '')
@@ -90,8 +86,8 @@ export class WpLoginModal extends Modal {
9086
}
9187
});
9288
// new Setting(contentEl)
93-
// .setName(t('loginModal_rememberUsername'))
94-
// .setDesc(t('loginModal_rememberUsernameDesc'))
89+
// .setName(this.t('loginModal_rememberUsername'))
90+
// .setDesc(this.t('loginModal_rememberUsernameDesc'))
9591
// .addToggle((toggle) =>
9692
// toggle
9793
// .setValue(this.profile.saveUsername)
@@ -106,8 +102,8 @@ export class WpLoginModal extends Modal {
106102
// }),
107103
// );
108104
// new Setting(contentEl)
109-
// .setName(t('loginModal_rememberPassword'))
110-
// .setDesc(t('loginModal_rememberPasswordDesc'))
105+
// .setName(this.t('loginModal_rememberPassword'))
106+
// .setDesc(this.t('loginModal_rememberPasswordDesc'))
111107
// .addToggle((toggle) =>
112108
// toggle
113109
// .setValue(this.profile.savePassword)
@@ -123,13 +119,13 @@ export class WpLoginModal extends Modal {
123119
// );
124120
new Setting(contentEl)
125121
.addButton(button => button
126-
.setButtonText(t('loginModal_loginButtonText'))
122+
.setButtonText(this.t('loginModal_loginButtonText'))
127123
.setCta()
128124
.onClick(() => {
129125
if (!username) {
130-
showError(t('error_noUsername'));
126+
showError(this.t('error_noUsername'));
131127
} else if (!password) {
132-
showError(t('error_noPassword'));
128+
showError(this.t('error_noPassword'));
133129
}
134130
if (username && password) {
135131
this.onSubmit({ username, password }, this);

src/wp-profile-chooser-modal.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { Modal, Setting } from 'obsidian';
21
import WordpressPlugin from './main';
32
import { WpProfile } from './wp-profile';
4-
import { TranslateKey } from './i18n';
53
import { rendererProfile } from './utils';
4+
import { AbstractModal } from './abstract-modal';
65

76

87
export function openProfileChooserModal(
@@ -19,24 +18,20 @@ export function openProfileChooserModal(
1918
/**
2019
* WordPress profiles chooser modal.
2120
*/
22-
class WpProfileChooserModal extends Modal {
21+
class WpProfileChooserModal extends AbstractModal {
2322

2423
private readonly profiles: WpProfile[];
2524

2625
constructor(
27-
private readonly plugin: WordpressPlugin,
26+
readonly plugin: WordpressPlugin,
2827
private readonly onChoose: (profile: WpProfile) => void
2928
) {
30-
super(plugin.app);
29+
super(plugin);
3130

3231
this.profiles = plugin.settings.profiles;
3332
}
3433

3534
onOpen() {
36-
const t = (key: TranslateKey, vars?: Record<string, string>): string => {
37-
return this.plugin.i18n.t(key, vars);
38-
};
39-
4035
const chooseProfile = (profile: WpProfile): void => {
4136
this.onChoose(profile);
4237
this.close();
@@ -52,12 +47,9 @@ class WpProfileChooserModal extends Modal {
5247
});
5348
}
5449

55-
const { contentEl } = this;
56-
57-
contentEl.createEl('h1', { text: t('profilesChooserModal_title') });
50+
this.createHeader(this.t('profilesChooserModal_title'));
5851

59-
new Setting(contentEl)
60-
.setName(t('profilesChooserModal_pickOne'));
52+
const { contentEl } = this;
6153
const content = contentEl.createEl('div');
6254
renderProfiles();
6355
}

src/wp-profile-manage-modal.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
import { Modal, Setting } from 'obsidian';
1+
import { Setting } from 'obsidian';
22
import WordpressPlugin from './main';
33
import { WpProfile } from './wp-profile';
4-
import { TranslateKey } from './i18n';
54
import { openProfileModal } from './wp-profile-modal';
65
import { isNil } from 'lodash-es';
76
import { rendererProfile } from './utils';
7+
import { AbstractModal } from './abstract-modal';
88

99

1010
/**
1111
* WordPress profiles manage modal.
1212
*/
13-
export class WpProfileManageModal extends Modal {
13+
export class WpProfileManageModal extends AbstractModal {
1414

1515
private readonly profiles: WpProfile[];
1616

1717
constructor(
18-
private readonly plugin: WordpressPlugin
18+
readonly plugin: WordpressPlugin
1919
) {
20-
super(plugin.app);
20+
super(plugin);
2121

2222
this.profiles = plugin.settings.profiles;
2323
}
2424

2525
onOpen() {
26-
const t = (key: TranslateKey, vars?: Record<string, string>): string => {
27-
return this.plugin.i18n.t(key, vars);
28-
};
29-
3026
const renderProfiles = (): void => {
3127
content.empty();
3228
this.profiles.forEach((profile, index) => {
3329
const setting = rendererProfile(profile, content);
3430
if (!profile.isDefault) {
3531
setting
3632
.addButton(button => button
37-
.setButtonText(t('profilesManageModal_setDefault'))
33+
.setButtonText(this.t('profilesManageModal_setDefault'))
3834
.onClick(() => {
3935
this.profiles.forEach(it => it.isDefault = false);
4036
profile.isDefault = true;
@@ -43,7 +39,7 @@ export class WpProfileManageModal extends Modal {
4339
}));
4440
}
4541
setting.addButton(button => button
46-
.setButtonText(t('profilesManageModal_showDetails'))
42+
.setButtonText(this.t('profilesManageModal_showDetails'))
4743
.onClick(async () => {
4844
const { profile: newProfile, atIndex } = await openProfileModal(
4945
this.plugin,
@@ -62,7 +58,7 @@ export class WpProfileManageModal extends Modal {
6258
}));
6359
setting.addExtraButton(button => button
6460
.setIcon('lucide-trash')
65-
.setTooltip(t('profilesManageModal_deleteTooltip'))
61+
.setTooltip(this.t('profilesManageModal_deleteTooltip'))
6662
.onClick(() => {
6763
this.profiles.splice(index, 1);
6864
if (profile.isDefault) {
@@ -76,15 +72,14 @@ export class WpProfileManageModal extends Modal {
7672
});
7773
}
7874

79-
const { contentEl } = this;
80-
81-
contentEl.createEl('h1', { text: t('profilesManageModal_title') });
75+
this.createHeader(this.t('profilesManageModal_title'));
8276

77+
const { contentEl } = this;
8378
new Setting(contentEl)
84-
.setName(t('profilesManageModal_create'))
85-
.setDesc(t('profilesManageModal_createDesc'))
79+
.setName(this.t('profilesManageModal_create'))
80+
.setDesc(this.t('profilesManageModal_createDesc'))
8681
.addButton(button => button
87-
.setButtonText(t('profilesManageModal_create'))
82+
.setButtonText(this.t('profilesManageModal_create'))
8883
.setCta()
8984
.onClick(async () => {
9085
const { profile } = await openProfileModal(

0 commit comments

Comments
 (0)