Skip to content

Commit a09d998

Browse files
committed
Enhanced type instructions & Add status bar prompt function
1 parent 1bb921a commit a09d998

File tree

12 files changed

+535
-505
lines changed

12 files changed

+535
-505
lines changed

src/export_command/cite_function/web.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*--------------------------------------------------------------------------------------------*/
55
// Part of the content comes from https://github.com/jasonwilliams/mediawiki-support/blob/master/src/webCitation.ts under license Apache-2.0
66

7-
import fetch, { Response } from "node-fetch";
7+
import * as vscode from "vscode";
88
import * as cheerio from "cheerio";
9+
import fetch, { Response } from "node-fetch";
910
import { DateTime } from "luxon";
10-
import * as vscode from "vscode";
1111
import { ArchiveConvert, ArchiveResult } from "../../interface_definition/archiveInterface";
1212

1313
export async function addWebCite(): Promise<void> {
@@ -20,14 +20,14 @@ export async function addWebCite(): Promise<void> {
2020
});
2121
if (!url) { return undefined; }
2222

23-
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Parsing the URL...");
23+
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Parsing...");
2424
try {
2525
const citeInfo: WebCiteInfo = new WebCiteInfo(url);
2626
await citeInfo.buildInfo();
2727
const result: string = citeInfo.toString(config.get("webCiteFormat") ?? "");
2828

29-
const editor = vscode.window.activeTextEditor;
30-
const selection = editor?.selection;
29+
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
30+
const selection: vscode.Selection | undefined = editor?.selection;
3131

3232
if (selection) {
3333
editor?.edit((editorBuilder) => {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as vscode from 'vscode';
2+
import MWBot from 'mwbot';
3+
import { getBot } from '../wikimedia_function/bot';
4+
import { Action, alterNativeValues, Prop, RvProp } from '../wikimedia_function/args';
5+
import { getPageCode } from '../wikimedia_function/core';
6+
import { IParameters, isRemoteBot, parseArgs } from './uri';
7+
8+
export async function editPage(query: string): Promise<void> {
9+
// vscode-insiders://rowewilsonfrederiskholme.wikitext/PullPage?Title=1
10+
const pars: IParameters = parseArgs(query);
11+
12+
const tbot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
13+
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
14+
}) : await getBot();
15+
16+
const title: string | undefined = pars['Title'];
17+
18+
if (!title || !tbot) {
19+
vscode.window.showErrorMessage(`${!title ? 'title ' : ''}${!tbot ? 'tbot ' : ''}is undefined or empty.`);
20+
return undefined;
21+
}
22+
23+
const args: { [Key: string]: string | undefined } = {
24+
'action': Action.query,
25+
'prop': Prop.reVisions,
26+
'rvprop': alterNativeValues(RvProp.content, RvProp.ids),
27+
'rvslots': "*",
28+
'titles': title
29+
};
30+
console.log(args);
31+
console.log(tbot);
32+
getPageCode(args, tbot);
33+
}

src/export_command/uri_function/uri.ts

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import * as MWBot from 'mwbot';
8-
import { Action, alterNativeValues, Prop, ContextModel, RvProp } from '../wikimedia_function/args';
9-
import { getView } from '../wikimedia_function/view';
10-
import { getPageCode } from '../wikimedia_function/core';
11-
import { getBot } from '../wikimedia_function/bot';
12-
import { getHost } from '../host_function/host';
7+
import { editPage } from './editPage';
8+
import { viewPage } from './viewPage';
9+
import { writeLine } from './writeLine';
1310

1411
export function baseUriProcess(uri: vscode.Uri): void {
1512
// vscode://rowewilsonfrederiskholme.wikitext/WriteLine?你好,世界!
1613
switch (uri.path) {
1714
case "/WriteLine":
1815
case "/Write":
19-
write(uri.query);
16+
writeLine(uri.query);
2017
break;
2118
case "/Edit":
2219
case "/EditPage":
@@ -32,79 +29,11 @@ export function baseUriProcess(uri: vscode.Uri): void {
3229
}
3330
}
3431

35-
async function editPage(query: string): Promise<void> {
36-
// vscode-insiders://rowewilsonfrederiskholme.wikitext/PullPage?Title=1
37-
const pars: IParameters = parseArgs(query);
38-
39-
const tbot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
40-
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
41-
}) : await getBot();
42-
43-
const title: string | undefined = pars['Title'];
44-
45-
if (!title || !tbot) {
46-
vscode.window.showErrorMessage(`${!title ? 'title ' : ''}${!tbot ? 'tbot ' : ''}is undefined or empty.`);
47-
return undefined;
48-
}
49-
50-
const args: any = {
51-
'action': Action.query,
52-
'prop': Prop.reVisions,
53-
'rvprop': alterNativeValues(RvProp.content, RvProp.ids),
54-
'rvslots': "*",
55-
'titles': title
56-
};
57-
console.log(args);
58-
console.log(tbot);
59-
getPageCode(args, tbot);
60-
}
61-
62-
function write(query: string): void {
63-
vscode.window.showInformationMessage(query);
64-
}
65-
66-
async function viewPage(query: string): Promise<void> {
67-
function setArgs(par: string, defaultValue: string | undefined = undefined) {
68-
args[par.toLowerCase()] = pars[par] ?? defaultValue;
69-
}
70-
71-
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("wikitext");
72-
const pars: IParameters = parseArgs(query);
73-
74-
const tbot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
75-
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
76-
}) : await getBot();
77-
78-
if (!tbot) {
79-
vscode.window.showErrorMessage(`${!tbot ? 'tbot ' : ''}is undefined or empty.`);
80-
return undefined;
81-
}
82-
83-
// TODO: getHost()
84-
const baseHref: string = isRemoteBot(pars) ? pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"] : config.get("transferProtocol") + (await getHost() || '') + config.get("articlePath");
85-
86-
// args value
87-
const args: any = { 'action': Action.parse };
88-
setArgs('Prop', alterNativeValues(
89-
Prop.text,
90-
Prop.displayTitle,
91-
Prop.categoriesHTML,
92-
(config.get("getCss") ? Prop.headHTML : undefined)
93-
));
94-
let undefParNames = ['Text', 'Title', 'Summary', 'RevID', 'Page',
95-
'PageID', 'OldID', 'Redirects', 'OnlyPST', 'Section',
96-
'SectionTitle', 'UseSkin', 'ContentFormat', 'ContentModel'];
97-
undefParNames.forEach((value: string): void => setArgs(value));
98-
setArgs("PST", "true");
99-
100-
getView("pageViewer", "WikiViewer", args, tbot, baseHref);
101-
}
102-
103-
interface IParameters {
32+
export interface IParameters {
10433
[Key: string]: string;
10534
}
10635

107-
function isRemoteBot(pars: IParameters): boolean {
36+
export function isRemoteBot(pars: IParameters): boolean {
10837
return !!(pars['RemoteBot'] || pars['SiteHost']);
10938
}
11039

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as vscode from 'vscode';
2+
import MWBot from 'mwbot';
3+
import { getBot } from '../wikimedia_function/bot';
4+
import { Action, alterNativeValues, Prop } from '../wikimedia_function/args';
5+
import { getView } from '../wikimedia_function/view';
6+
import { IParameters, isRemoteBot, parseArgs } from './uri';
7+
import { getHost } from '../host_function/host';
8+
9+
export async function viewPage(query: string): Promise<void> {
10+
function setArgs(par: string, defaultValue: string | undefined = undefined) {
11+
args[par.toLowerCase()] = pars[par] ?? defaultValue;
12+
}
13+
14+
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("wikitext");
15+
const pars: IParameters = parseArgs(query);
16+
17+
const tbot: MWBot | undefined = isRemoteBot(pars) ? new MWBot({
18+
apiUrl: pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"]
19+
}) : await getBot();
20+
21+
if (!tbot) {
22+
vscode.window.showErrorMessage(`${!tbot ? 'tbot ' : ''}is undefined or empty.`);
23+
return undefined;
24+
}
25+
26+
// TODO: getHost()
27+
const baseHref: string = isRemoteBot(pars) ? pars["TransferProtocol"] + pars["SiteHost"] + pars["APIPath"] : config.get("transferProtocol") + (await getHost() || '') + config.get("articlePath");
28+
29+
// args value
30+
const args: { [Key: string]: string | undefined } = { 'action': Action.parse };
31+
setArgs('Prop', alterNativeValues(
32+
Prop.text,
33+
Prop.displayTitle,
34+
Prop.categoriesHTML,
35+
(config.get("getCss") ? Prop.headHTML : undefined)
36+
));
37+
let undefParNames = ['Text', 'Title', 'Summary', 'RevID', 'Page',
38+
'PageID', 'OldID', 'Redirects', 'OnlyPST', 'Section',
39+
'SectionTitle', 'UseSkin', 'ContentFormat', 'ContentModel'];
40+
undefParNames.forEach((value: string): void => setArgs(value));
41+
setArgs("PST", "true");
42+
43+
getView("pageViewer", "WikiViewer", args, tbot, baseHref);
44+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as vscode from 'vscode';
2+
export function writeLine(query: string): void {
3+
vscode.window.showInformationMessage(query);
4+
}

src/export_command/wikimedia_function/bot.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as MWBot from 'mwbot';
6+
import MWBot from 'mwbot';
77
import * as vscode from 'vscode';
88
import { getHost } from '../host_function/host';
99

@@ -28,7 +28,7 @@ export async function login(): Promise<void> {
2828
bot = new MWBot({
2929
apiUrl: config.get("transferProtocol") + host + config.get("apiPath")
3030
});
31-
31+
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Login...");
3232
try {
3333
const response = await bot?.login(userInfo);
3434
console.log(response);
@@ -39,19 +39,31 @@ export async function login(): Promise<void> {
3939
console.log(error);
4040
vscode.window.showErrorMessage(error.message);
4141
}
42+
finally {
43+
barMessage.dispose();
44+
}
4245
}
4346

4447
export async function logout(): Promise<void> {
4548
await bot?.getEditToken();
46-
const result = await bot?.request({
47-
'action': 'logout',
48-
'token': bot.editToken
49-
});
50-
// it will be {} if success
51-
console.log(result);
52-
// clear bot
53-
bot = undefined;
54-
vscode.window.showInformationMessage('result: "Success"');
49+
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Logout...");
50+
try {
51+
const result = await bot?.request({
52+
'action': 'logout',
53+
'token': bot.editToken
54+
});
55+
// it will be {} if success
56+
console.log(result);
57+
// clear bot
58+
bot = undefined;
59+
vscode.window.showInformationMessage('result: "Success"');
60+
}
61+
catch (error: any) {
62+
vscode.window.showErrorMessage(error.message);
63+
}
64+
finally {
65+
barMessage.dispose();
66+
}
5567
}
5668

5769
export async function getBot(): Promise<MWBot | undefined> {

src/export_command/wikimedia_function/core.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7-
import * as MWBot from 'mwbot';
7+
import type Bluebird from 'bluebird';
8+
import type MWBot from 'mwbot';
89
import { Action, Prop, RvProp, alterNativeValues } from './args';
910
import { ReadPageConvert, ReadPageResult, Main, Revision, Jump } from '../../interface_definition/readPageInterface';
1011
import { OldTokensConvert, OldTokensResult } from '../../interface_definition/oldTokensInterface';
@@ -17,8 +18,8 @@ import { TokensConvert, TokensResult } from '../../interface_definition/tokensIn
1718
export async function postPage(): Promise<void> {
1819
async function getEditToken(bot: MWBot): Promise<string> {
1920
console.log("try get token.");
20-
let args: any;
21-
let result: any;
21+
let args: { [Key: string]: string | undefined };
22+
let result: Bluebird<any>;
2223
let token: string | undefined;
2324
let errors: any[] = [undefined, undefined];
2425

@@ -91,6 +92,7 @@ export async function postPage(): Promise<void> {
9192
placeHolder: " // Edit via Wikitext Extension for VSCode"
9293
}) + " // Edit via Wikitext Extension for VSCode";
9394

95+
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Posting...");
9496
try {
9597
bot.editToken = await getEditToken(bot);
9698
await bot.edit(wikiTitle, contentInfo.content, wikiSummary).then(response => {
@@ -110,6 +112,9 @@ export async function postPage(): Promise<void> {
110112
console.log(error);
111113
vscode.window.showErrorMessage(`Error:${error.name}, ${error.message}. Your Token: ${bot?.editToken}`);
112114
}
115+
finally {
116+
barMessage.dispose();
117+
}
113118
}
114119

115120
/**
@@ -130,7 +135,7 @@ export async function pullPage(): Promise<void> {
130135
// if title is null or empty, do nothing
131136
if (!title) { return undefined; }
132137

133-
const args: any = {
138+
const args: { [Key: string]: string | undefined } = {
134139
'action': Action.query,
135140
'prop': Prop.reVisions,
136141
'rvprop': alterNativeValues(RvProp.content, RvProp.ids),
@@ -144,7 +149,8 @@ export async function pullPage(): Promise<void> {
144149
getPageCode(args, tbot);
145150
}
146151

147-
export async function getPageCode(args: any, tbot: MWBot) {
152+
export async function getPageCode(args: { [Key: string]: string | undefined }, tbot: MWBot): Promise<void> {
153+
const barMessage: vscode.Disposable = vscode.window.setStatusBarMessage("Wikitext: Getting code...");
148154
try {
149155
// get request result
150156
const result = await tbot.request(args);
@@ -173,14 +179,6 @@ export async function getPageCode(args: any, tbot: MWBot) {
173179

174180
const content: Main | Revision | undefined = revision?.slots?.main || revision;
175181

176-
const normalized: Jump | undefined = re.query?.normalized?.[0];
177-
const redirects: Jump | undefined = re.query?.redirects?.[0];
178-
vscode.window.showInformationMessage(
179-
`Opened page "${page.title}" with Model ${content?.contentmodel}. ` +
180-
`Normalized: ${normalized ? `${normalized.from} => ${normalized.to}` : undefined}. ` +
181-
`Redirect: ${redirects ? `${redirects.from} => ${redirects.to}` : undefined}`
182-
);
183-
184182
const infoHead: string =
185183
`<%-- [PAGE_INFO]
186184
Comment=#Please do not remove this struct. It's record contains some important informations of edit. This struct will be removed automatically after you push edits.#
@@ -196,10 +194,22 @@ ${InfoType.contentFormat}=#${content?.contentformat}#
196194
content: infoHead + "\r\r" + content?.["*"]
197195
});
198196
vscode.window.showTextDocument(textDocument);
197+
198+
const normalized: Jump | undefined = re.query?.normalized?.[0];
199+
const redirects: Jump | undefined = re.query?.redirects?.[0];
200+
201+
vscode.window.showInformationMessage(
202+
`Opened page "${page.title}" with Model ${content?.contentmodel}. ` +
203+
`Normalized: ${normalized ? `${normalized.from} => ${normalized.to}` : undefined}. ` +
204+
`Redirect: ${redirects ? `${redirects.from} => ${redirects.to}` : undefined}`
205+
);
199206
}
200207
catch (error: any) {
201208
vscode.window.showErrorMessage(`${error.code}! ${error.info}`);
202209
}
210+
finally {
211+
barMessage.dispose();
212+
}
203213
}
204214

205215
// TODO: uploadFile, deletedPage

0 commit comments

Comments
 (0)