@bhsd/codemirror-mediawiki 2.6.7 → 2.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/mw/preference.ts CHANGED
@@ -1,14 +1,25 @@
1
1
  import {rules} from 'wikiparser-node/dist/base';
2
2
  import {CodeMirror} from './base';
3
- import {msg, i18n, setObject, getObject} from './msg';
3
+ import {msg, parseMsg, i18n, setObject, getObject} from './msg';
4
4
  import {instances} from './textSelection';
5
+ import type {ApiEditPageParams, ApiQueryRevisionsParams} from 'types-mediawiki/api_params';
5
6
 
6
7
  const storageKey = 'codemirror-mediawiki-addons',
7
8
  wikilintKey = 'codemirror-mediawiki-wikilint',
8
- codeKeys = ['ESLint', 'Stylelint'] as const;
9
+ codeKeys = ['ESLint', 'Stylelint'] as const,
10
+ user = mw.config.get('wgUserName'),
11
+ userPage = user && `User:${user}/codemirror-mediawiki.json`;
9
12
 
10
13
  declare type codeKey = typeof codeKeys[number];
11
14
 
15
+ declare interface Preferences {
16
+ addons?: string[];
17
+ indent?: string;
18
+ wikilint?: Record<Rule, RuleState>;
19
+ ESLint?: unknown;
20
+ Stylelint?: unknown;
21
+ }
22
+
12
23
  export const indentKey = 'codemirror-mediawiki-indent',
13
24
  prefs = new Set<string>(getObject(storageKey) as string[] | null),
14
25
  wikilintConfig = (getObject(wikilintKey) || {}) as Record<Rule, RuleState | undefined>,
@@ -30,12 +41,35 @@ const enum RuleState {
30
41
 
31
42
  const wikilintWidgets = new Map<Rule, OO.ui.DropdownInputWidget>();
32
43
 
44
+ /**
45
+ * 处理Api请求错误
46
+ * @param code 错误代码
47
+ * @param e 错误信息
48
+ */
49
+ const apiErr = (code: string, e: any): void => { // eslint-disable-line @typescript-eslint/no-explicit-any
50
+ const message = code === 'http' || code === 'okay-but-empty'
51
+ ? `MediaWiki API request failed: ${code}`
52
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
53
+ : $('<ul>', {html: (e.errors as {html: string}[]).map(({html}) => $('<li>', {html}))});
54
+ void mw.notify(message as string | HTMLElement[], {type: 'error', autoHideSeconds: 'long'});
55
+ };
56
+
57
+ const api = (async () => {
58
+ await mw.loader.using('mediawiki');
59
+ return new mw.Api({parameters: {errorformat: 'html', formatversion: '2'}});
60
+ })();
61
+
33
62
  /**
34
63
  * 打开设置对话框
35
64
  * @param editors CodeMirror实例
36
65
  */
37
66
  export const openPreference = async (editors: (CodeMirror | undefined)[]): Promise<void> => {
38
- await mw.loader.using(['oojs-ui-windows', 'oojs-ui-widgets', 'oojs-ui.styles.icons-content']);
67
+ await mw.loader.using([
68
+ 'oojs-ui-windows',
69
+ 'oojs-ui-widgets',
70
+ 'oojs-ui.styles.icons-content',
71
+ 'mediawiki.jqueryMsg',
72
+ ]);
39
73
  if (dialog) {
40
74
  widget.setValue([...prefs] as unknown as string);
41
75
  indentWidget.setValue(indent);
@@ -71,8 +105,9 @@ export const openPreference = async (editors: (CodeMirror | undefined)[]): Promi
71
105
  .filter(k => k !== 'addon-indent' && k.startsWith('addon-') && !k.endsWith('-mac'))
72
106
  .map(k => ({
73
107
  data: k.slice(6),
74
- label: $($.parseHTML(msg(k))),
75
- disabled: k === 'addon-wikiEditor' && !mw.loader.getState('ext.wikiEditor'),
108
+ label: parseMsg(k),
109
+ disabled: k === 'addon-wikiEditor' && !mw.loader.getState('ext.wikiEditor')
110
+ || k === 'addon-save' && !user,
76
111
  })),
77
112
  ],
78
113
  value: [...prefs] as unknown as string,
@@ -146,5 +181,63 @@ export const openPreference = async (editors: (CodeMirror | undefined)[]): Promi
146
181
  }
147
182
  setObject(storageKey, value);
148
183
  localStorage.setItem(indentKey, indent);
184
+
185
+ if (prefs.has('save')) {
186
+ const params: ApiEditPageParams = {
187
+ action: 'edit',
188
+ title: userPage,
189
+ text: JSON.stringify({
190
+ addons: [...prefs],
191
+ indent,
192
+ wikilint: wikilintConfig,
193
+ ESLint: codeConfigs.get('ESLint'),
194
+ Stylelint: codeConfigs.get('Stylelint'),
195
+ } as Preferences),
196
+ summary: msg('save-summary'),
197
+ };
198
+ // eslint-disable-next-line promise/prefer-await-to-then
199
+ (await api).postWithToken('csrf', params as Record<string, string>).then(
200
+ () => {
201
+ void mw.notify(parseMsg('save-success'), {type: 'success'});
202
+ },
203
+ apiErr,
204
+ );
205
+ }
149
206
  }
150
207
  };
208
+
209
+ (async () => {
210
+ const params: ApiQueryRevisionsParams = {
211
+ action: 'query',
212
+ prop: 'revisions',
213
+ titles: userPage,
214
+ rvprop: 'content',
215
+ rvlimit: 1,
216
+ };
217
+ (await api).get(params as Record<string, string>).then( // eslint-disable-line promise/prefer-await-to-then
218
+ res => {
219
+ const {query: {pages: [page]}} = res as MediaWikiResponse;
220
+ if (page?.revisions) {
221
+ const json: Preferences = JSON.parse(page.revisions[0]!.content);
222
+ for (const key of codeKeys) {
223
+ if (json[key]) {
224
+ codeConfigs.set(key, json[key]);
225
+ }
226
+ }
227
+ if (json.wikilint) {
228
+ Object.assign(wikilintConfig, json.wikilint);
229
+ }
230
+ if (json.addons) {
231
+ prefs.clear();
232
+ for (const option of json.addons) {
233
+ prefs.add(option);
234
+ }
235
+ }
236
+ if (json.indent) {
237
+ localStorage.setItem(indentKey, json.indent);
238
+ }
239
+ }
240
+ },
241
+ apiErr,
242
+ );
243
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.6.7",
3
+ "version": "2.6.9",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -43,23 +43,26 @@
43
43
  "engines": {
44
44
  "node": "20.9.0"
45
45
  },
46
- "devDependencies": {
47
- "@codemirror/autocomplete": "^6.11.1",
48
- "@codemirror/commands": "^6.3.2",
49
- "@codemirror/language": "^6.9.3",
46
+ "dependencies": {
47
+ "@codemirror/autocomplete": "^6.12.0",
48
+ "@codemirror/commands": "^6.3.3",
49
+ "@codemirror/language": "^6.10.1",
50
50
  "@codemirror/legacy-modes": "^6.3.3",
51
- "@codemirror/lint": "^6.4.2",
52
- "@codemirror/search": "^6.5.4",
53
- "@codemirror/state": "^6.3.3",
54
- "@codemirror/view": "^6.22.2",
55
- "@lezer/common": "^1.1.2",
51
+ "@codemirror/lint": "^6.5.0",
52
+ "@codemirror/search": "^6.5.6",
53
+ "@codemirror/state": "^6.4.1",
54
+ "@codemirror/view": "^6.24.1",
56
55
  "@lezer/highlight": "^1.2.0",
56
+ "wikiparser-node": "^1.5.3"
57
+ },
58
+ "devDependencies": {
59
+ "@lezer/common": "^1.1.2",
57
60
  "@stylistic/eslint-plugin": "^1.5.4",
58
61
  "@stylistic/stylelint-plugin": "^2.0.0",
59
62
  "@types/jquery": "^3.5.29",
60
63
  "@types/oojs-ui": "^0.47.6",
61
- "@typescript-eslint/eslint-plugin": "^6.19.1",
62
- "@typescript-eslint/parser": "^6.19.1",
64
+ "@typescript-eslint/eslint-plugin": "^7.1.0",
65
+ "@typescript-eslint/parser": "^7.1.0",
63
66
  "esbuild": "^0.19.12",
64
67
  "eslint": "^8.56.0",
65
68
  "eslint-plugin-es-x": "^7.5.0",
@@ -68,12 +71,11 @@
68
71
  "eslint-plugin-json-es": "^1.5.7",
69
72
  "eslint-plugin-promise": "^6.1.1",
70
73
  "eslint-plugin-regexp": "^2.2.0",
71
- "eslint-plugin-unicorn": "^50.0.1",
74
+ "eslint-plugin-unicorn": "^51.0.1",
72
75
  "http-server": "^14.1.0",
73
76
  "stylelint": "^16.1.0",
74
77
  "stylelint-config-recommended": "^14.0.0",
75
78
  "types-mediawiki": "^1.4.0",
76
- "typescript": "^5.3.3",
77
- "wikiparser-node": "^1.5.0"
79
+ "typescript": "^5.3.3"
78
80
  }
79
81
  }