Skip to content

Commit 967c324

Browse files
Copilotcyanzhong
andauthored
Add extension menu items (#21)
* Initial plan * Add extension menu items: Proofread Now, Ignore All, Version, Check Release Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * Move globals.d.ts from src to types directory Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * Wrap menu items under a parent "Proofread" menu with SF Symbol icon Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * Move repoUrl to const.ts; remove addMenuItems wrapper, call buildMenuItem directly in main.ts Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * nit-picking * Remove unnecessary stale document check from proofreadNow Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * Revert accidental lock file changes Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> * Replace sliceString(0) with toString() in menu.ts and extension.ts Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cyanzhong <6745066+cyanzhong@users.noreply.github.com> Co-authored-by: Ying Zhong <0x00eeee@gmail.com>
1 parent 319d0ff commit 967c324

8 files changed

Lines changed: 85 additions & 2 deletions

File tree

main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { MarkEdit } from 'markedit-api';
22
import { proofreadingExtension } from './src/extension';
3+
import { buildMenuItem } from './src/menu';
34

45
MarkEdit.addExtension(proofreadingExtension());
6+
MarkEdit.addMainMenuItem(buildMenuItem());

src/const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const repoUrl = 'https://github.com/MarkEdit-app/MarkEdit-proofreading';

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const lintScheduler = ViewPlugin.fromClass(class {
3939

4040
async runLint() {
4141
const doc = this.view.state.doc;
42-
const text = doc.sliceString(0);
42+
const text = doc.toString();
4343
const lints = await lint(text);
4444

4545
if (this.view.state.doc !== doc) {

src/menu.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { MarkEdit } from 'markedit-api';
2+
import type { MenuItem } from 'markedit-api';
3+
import { setDiagnosticsEffect, lintToDiagnostic } from './decoration';
4+
import { lint } from './lint';
5+
import { repoUrl } from './const';
6+
7+
export function buildMenuItem(): MenuItem {
8+
return {
9+
title: 'Proofread',
10+
icon: 'text.badge.checkmark',
11+
children: [
12+
{
13+
title: 'Proofread Now',
14+
action: proofreadNow,
15+
},
16+
{
17+
title: 'Ignore All',
18+
action: ignoreAll,
19+
},
20+
{ separator: true },
21+
{
22+
title: `Version ${__PKG_VERSION__}`,
23+
action: () => open(`${repoUrl}/releases/tag/v${__PKG_VERSION__}`),
24+
},
25+
{
26+
title: 'Check Release (GitHub)',
27+
action: () => open(`${repoUrl}/releases`),
28+
},
29+
],
30+
};
31+
}
32+
33+
async function proofreadNow() {
34+
const view = MarkEdit.editorView;
35+
const text = view.state.doc.toString();
36+
const lints = await lint(text);
37+
view.dispatch({ effects: setDiagnosticsEffect.of(lints.map(lintToDiagnostic)) });
38+
}
39+
40+
function ignoreAll() {
41+
MarkEdit.editorView.dispatch({ effects: setDiagnosticsEffect.of([]) });
42+
}

tests/menu.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { buildMenuItem } from '../src/menu';
3+
4+
describe('buildMenuItem', () => {
5+
it('returns the expected menu structure', () => {
6+
const item = buildMenuItem();
7+
8+
expect(item.title).toBe('Proofread');
9+
expect(item.icon).toBe('text.badge.checkmark');
10+
11+
const children = item.children!;
12+
expect(children).toHaveLength(5);
13+
14+
expect(children[0].title).toBe('Proofread Now');
15+
expect(typeof children[0].action).toBe('function');
16+
17+
expect(children[1].title).toBe('Ignore All');
18+
expect(typeof children[1].action).toBe('function');
19+
20+
expect(children[2].separator).toBe(true);
21+
22+
expect(children[3].title).toMatch(/^Version \S+/);
23+
expect(typeof children[3].action).toBe('function');
24+
25+
expect(children[4].title).toBe('Check Release (GitHub)');
26+
expect(typeof children[4].action).toBe('function');
27+
});
28+
});

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"strictNullChecks": true,
1010
"importHelpers": true,
1111
"noEmit": true,
12-
"skipLibCheck": true
12+
"skipLibCheck": true,
13+
"allowSyntheticDefaultImports": true,
14+
"resolveJsonModule": true
1315
}
1416
}

types/globals.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Package version read from the `package.json` file.
3+
*/
4+
declare const __PKG_VERSION__: string;

vite.config.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { defineConfig, mergeConfig } from 'vite';
22
import { viteSingleFile } from 'vite-plugin-singlefile';
33
import { defaultViteConfig } from 'markedit-vite';
4+
import mainPackage from './package.json' with { type: 'json' };
45

56
export default defineConfig(mergeConfig(defaultViteConfig(), {
7+
define: {
8+
__PKG_VERSION__: JSON.stringify(mainPackage.version),
9+
},
610
plugins: [viteSingleFile()],
711
}));

0 commit comments

Comments
 (0)