Skip to content

Commit 155dc03

Browse files
authored
feat: add an option to ignore empty body updates (#60)
Workaround for a vscode quirk (see #23).
2 parents bb5eaeb + e77a759 commit 155dc03

8 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎ext/options.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ label {
5656
&[for="enableNotifications"]::before {
5757
content: "__MSG_options_enable_notifications__";
5858
}
59+
&[for="ignoreEmptyBodyUpdates"]::before {
60+
content: "__MSG_options_ignore_empty_body_updates__";
61+
}
5962
}
6063
.progress {
6164
&.saved::before {

‎ext/options.html‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
type="checkbox"
3030
>
3131
</label>
32+
<label for="ignoreEmptyBodyUpdates"></label>
33+
<label>
34+
<input
35+
id="ignoreEmptyBodyUpdates"
36+
name="ignoreEmptyBodyUpdates"
37+
type="checkbox"
38+
>
39+
</label>
3240
<span></span>
3341
<span
3442
><input type="reset"></span

‎locales.toml‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,37 @@ vi = "Hiển thị thông báo"
252252
zh = "显示通知"
253253
zh-tw = "顯示通知"
254254

255+
[options_ignore_empty_body_updates]
256+
# Description of the option item for ignoring empty body updates sent by the text editor.
257+
ar = "تجاهل تحديثات النص الفارغ (كحل مؤقت)"
258+
be = "Ігнараваць пустыя абнаўленні тэксту (для абыходу праблемы)"
259+
ca = "Ignora les actualitzacions de cos buit (per a una solució alternativa)"
260+
cs = "Ignorovat aktualizace s prázdným textem (obejití problému)"
261+
de = "Leere Textaktualisierungen ignorieren (Workaround)"
262+
el = "Αγνόηση κενών ενημερώσεων σώματος (για παρακάμψη)"
263+
en = "Ignore empty body updates (for workaround)"
264+
es = "Ignorar actualizaciones de cuerpo vacío (solución alternativa)"
265+
fi = "Ohita tyhjät runkopäivitykset (kiertotapa)"
266+
fr = "Ignorer les mises à jour de corps vides (contournement)"
267+
hu = "Üres törzsfrissítések figyelmen kívül hagyása (megkerülő megoldás)"
268+
id = "Abaikan pembaruan isi kosong (solusi sementara)"
269+
it = "Ignora gli aggiornamenti del corpo vuoti (per aggirare un problema)"
270+
ja = "(バグ回避用)空の本文更新を無視する"
271+
ko = "빈 본문 업데이트 무시 (우회 방법)"
272+
nl = "Lege hoofdtekstupdates negeren (workaround)"
273+
pl = "Ignoruj puste aktualizacje treści (obejście)"
274+
pt = "Ignorar atualizações de corpo vazio (solução alternativa)"
275+
pt-br = "Ignorar atualizações de corpo vazio (solução alternativa)"
276+
ro = "Ignoră actualizările corpului gol (soluție provizorie)"
277+
ru = "Игнорировать пустые обновления текста (обходное решение)"
278+
sk = "Ignorovať prázdne aktualizácie tela (obchádzka)"
279+
th = "ไม่สนใจการอัปเดตเนื้อหาที่ว่างเปล่า (วิธีแก้ปัญหาชั่วคราว)"
280+
tr = "Boş gövde güncellemelerini yoksay (geçici çözüm)"
281+
uk = "Ігнорувати порожні оновлення тексту (обхідний шлях)"
282+
vi = "Bỏ qua cập nhật nội dung trống (cách khắc phục tạm thời)"
283+
zh = "忽略空正文更新(临时解决方法)"
284+
zh-tw = "忽略空白內文更新(臨時解決方法)"
285+
255286
[options_saved]
256287
# Message shown when the options has been saved successfully.
257288
ar = "تم الحفظ"

‎src/ghosttext-adaptor/api.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export type StoredOptions = {
8383
serverPort: number
8484
/** Shows notifications when connecting or disconnecting from GhostText if true */
8585
enableNotifications: boolean
86+
/** Ignore all empty body updates sent by the server if true */
87+
ignoreEmptyBodyUpdates: boolean
8688
}
8789

8890
/** Manages access to options */

‎src/ghosttext-adaptor/client_options_loader.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { IManifestInfo, IStoredOptionsLoader, StoredOptions } from "./api"
44
export const DEFAULT_STORED_OPTIONS: StoredOptions = {
55
serverPort: 4001,
66
enableNotifications: true,
7+
ignoreEmptyBodyUpdates: false,
78
} as const
89

910
export class ClientOptionsLoader implements IClientOptionsLoader {
@@ -15,7 +16,7 @@ export class ClientOptionsLoader implements IClientOptionsLoader {
1516
) {}
1617

1718
async load(): Promise<ClientOptions> {
18-
let { serverPort, enableNotifications } = await this.storedOptionsLoader.load()
19+
let { serverPort, enableNotifications, ignoreEmptyBodyUpdates } = await this.storedOptionsLoader.load()
1920
const extensionId = this.manifestInfo.getId()
2021
let serverUrl = new URL(`http://localhost:${serverPort}/`)
2122
let { host } = new URL(`extension://${extensionId}.localhost`)
@@ -24,6 +25,7 @@ export class ClientOptionsLoader implements IClientOptionsLoader {
2425
serverUrl,
2526
clientHostName: host,
2627
enableNotifications,
28+
ignoreEmptyBodyUpdates,
2729
}
2830
}
2931
}
@@ -38,6 +40,7 @@ if (import.meta.vitest) {
3840
Promise.resolve({
3941
serverPort: 1234,
4042
enableNotifications: true,
43+
ignoreEmptyBodyUpdates: true,
4144
}),
4245
} satisfies IStoredOptionsLoader
4346
const manifestInfo = {
@@ -49,6 +52,7 @@ if (import.meta.vitest) {
4952
serverUrl: new URL("http://localhost:1234/"),
5053
clientHostName: "test.extension.localhost",
5154
enableNotifications: true,
55+
ignoreEmptyBodyUpdates: true,
5256
})
5357
})
5458
})

‎src/ghosttext-adaptor/email_editor.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class EmailEditor implements IClientEditor, IStatusIndicator {
5151
}
5252

5353
async applyChange(change: ExternalEdit): Promise<void> {
54+
if (this.options.ignoreEmptyBodyUpdates && change.body === "") {
55+
return
56+
}
57+
5458
// TODO Should re-add HTML tags like `<p></p>` if stripped
5559
this.port.send(change)
5660
}
@@ -86,3 +90,51 @@ function messageForStatus(status: ClientStatus): MessageId {
8690
return "src_client_status_error"
8791
}
8892
}
93+
94+
if (import.meta.vitest) {
95+
const { describe, expect, it } = import.meta.vitest
96+
97+
describe(EmailEditor, () => {
98+
it("should not apply an empty body when ignoreEmptyBodyUpdates is enabled", async () => {
99+
const sent: unknown[] = []
100+
const sut = makeSut({ ignoreEmptyBodyUpdates: true, sent })
101+
102+
await sut.applyChange({ body: "" })
103+
104+
expect(sent).to.be.empty
105+
})
106+
107+
it("should apply an empty body when ignoreEmptyBodyUpdates is disabled", async () => {
108+
const sent: unknown[] = []
109+
const sut = makeSut({ ignoreEmptyBodyUpdates: false, sent })
110+
111+
const change: ExternalEdit = { body: "" }
112+
await sut.applyChange(change)
113+
114+
expect(sent).to.deep.equal([change])
115+
})
116+
117+
it("should apply a non-empty body even when ignoreEmptyBodyUpdates is enabled", async () => {
118+
const sent: unknown[] = []
119+
const sut = makeSut({ ignoreEmptyBodyUpdates: true, sent })
120+
121+
const change: ExternalEdit = { body: "hello" }
122+
await sut.applyChange(change)
123+
124+
expect(sent).to.deep.equal([change])
125+
})
126+
})
127+
128+
function makeSut(opts: { ignoreEmptyBodyUpdates: boolean; sent: unknown[] }): EmailEditor {
129+
const port = {
130+
send: (m: never) => opts.sent.push(m),
131+
} as unknown as IGhostServerPort
132+
const options = {
133+
clientHostName: "test.extension.localhost",
134+
serverUrl: new URL("http://localhost:4001/"),
135+
enableNotifications: true,
136+
ignoreEmptyBodyUpdates: opts.ignoreEmptyBodyUpdates,
137+
} as ClientOptions
138+
return new EmailEditor({} as INotificationTray, {} as IComposeWindow, port, options)
139+
}
140+
}

‎src/ghosttext-runner/api.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export type ClientOptions = {
3434
readonly serverUrl: URL
3535
/** Show notifications on connect/disconnect events if true */
3636
readonly enableNotifications: boolean
37+
/** Ignore empty body updates sent by the server before closing */
38+
/** Drop empty body updates from the server, e.g. the empty update VS Code sends on close */
39+
readonly ignoreEmptyBodyUpdates: boolean
3740
}
3841

3942
/**

‎src/test/testbench/ghostbird.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ function makeMockedThunderbird(
9999
Promise.resolve({
100100
serverPort: 12345,
101101
enableNotifications: false,
102+
ignoreEmptyBodyUpdates: false,
102103
} as StoredOptions),
103104
},
104105
manifestInfo: {

0 commit comments

Comments
 (0)