@bhsd/codemirror-mediawiki 2.7.4 → 2.7.6

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
@@ -2,6 +2,7 @@ import {rules} from 'wikiparser-node/dist/base';
2
2
  import {CodeMirror} from './base';
3
3
  import {msg, parseMsg, i18n, setObject, getObject} from './msg';
4
4
  import {instances} from './textSelection';
5
+ import type {LintError} from 'wikiparser-node';
5
6
  import type {ApiEditPageParams, ApiQueryRevisionsParams} from 'types-mediawiki/api_params';
6
7
 
7
8
  const storageKey = 'codemirror-mediawiki-addons',
@@ -15,9 +16,20 @@ declare type codeKey = typeof codeKeys[number];
15
16
  declare type Preferences = {
16
17
  addons?: string[];
17
18
  indent?: string;
18
- wikilint?: Record<Rule, RuleState>;
19
+ wikilint?: Record<LintError.Rule, RuleState>;
19
20
  } & Record<codeKey, unknown>;
20
21
 
22
+ declare interface MediaWikiPage {
23
+ readonly revisions?: {
24
+ readonly content: string;
25
+ }[];
26
+ }
27
+ declare interface MediaWikiResponse {
28
+ readonly query: {
29
+ readonly pages: MediaWikiPage[];
30
+ };
31
+ }
32
+
21
33
  const enum RuleState {
22
34
  off = '0',
23
35
  error = '1',
@@ -26,7 +38,7 @@ const enum RuleState {
26
38
 
27
39
  export const indentKey = 'codemirror-mediawiki-indent',
28
40
  prefs = new Set<string>(getObject(storageKey) as string[] | null),
29
- wikilintConfig = (getObject(wikilintKey) || {}) as Record<Rule, RuleState | undefined>,
41
+ wikilintConfig = (getObject(wikilintKey) || {}) as Record<LintError.Rule, RuleState | undefined>,
30
42
  codeConfigs = new Map(codeKeys.map(k => [k, getObject(`codemirror-mediawiki-${k}`)]));
31
43
 
32
44
  // OOUI组件
@@ -36,7 +48,7 @@ let dialog: OO.ui.MessageDialog | undefined,
36
48
  indentWidget: OO.ui.TextInputWidget,
37
49
  indent = localStorage.getItem(indentKey) || '';
38
50
  const widgets: Partial<Record<codeKey, OO.ui.MultilineTextInputWidget>> = {},
39
- wikilintWidgets = new Map<Rule, OO.ui.DropdownInputWidget>();
51
+ wikilintWidgets = new Map<LintError.Rule, OO.ui.DropdownInputWidget>();
40
52
 
41
53
  /**
42
54
  * 处理Api请求错误
@@ -1,4 +1,4 @@
1
- import type {CodeMirror} from './base';
1
+ import {CodeMirror} from './base';
2
2
 
3
3
  export const instances = new WeakMap<HTMLTextAreaElement, CodeMirror>();
4
4
 
@@ -8,6 +8,18 @@ export const instances = new WeakMap<HTMLTextAreaElement, CodeMirror>();
8
8
  */
9
9
  const getInstance = ($ele: JQuery<HTMLTextAreaElement>): CodeMirror => instances.get($ele[0]!)!;
10
10
 
11
+ declare interface EncapsulateOptions {
12
+ pre?: string;
13
+ peri?: string;
14
+ post?: string;
15
+ ownline?: boolean;
16
+ replace?: boolean;
17
+ selectPeri?: boolean;
18
+ splitlines?: boolean;
19
+ selectionStart?: number;
20
+ selectionEnd?: number;
21
+ }
22
+
11
23
  /**
12
24
  * jQuery.textSelection overrides for CodeMirror.
13
25
  * See jQuery.textSelection.js for method documentation
@@ -17,11 +29,7 @@ export const textSelection = {
17
29
  return getInstance(this).view.state.doc.toString();
18
30
  },
19
31
  setContents(this: JQuery<HTMLTextAreaElement>, content: string): JQuery<HTMLTextAreaElement> {
20
- const cm = getInstance(this),
21
- {view: {scrollDOM}} = cm,
22
- {scrollTop} = scrollDOM;
23
- cm.setContent(content);
24
- scrollDOM.scrollTop = scrollTop;
32
+ getInstance(this).setContent(content);
25
33
  return this;
26
34
  },
27
35
  getSelection(this: JQuery<HTMLTextAreaElement>): string {
@@ -40,10 +48,65 @@ export const textSelection = {
40
48
  },
41
49
  replaceSelection(this: JQuery<HTMLTextAreaElement>, value: string): JQuery<HTMLTextAreaElement> {
42
50
  const {view} = getInstance(this);
43
- view.dispatch({selection: view.state.selection.asSingle()});
44
51
  view.dispatch(view.state.replaceSelection(value));
45
52
  return this;
46
53
  },
54
+ encapsulateSelection(this: JQuery<HTMLTextAreaElement>, {
55
+ pre = '',
56
+ peri = '',
57
+ post = '',
58
+ ownline,
59
+ replace,
60
+ selectPeri = true,
61
+ splitlines,
62
+ selectionStart,
63
+ selectionEnd = selectionStart,
64
+ }: EncapsulateOptions): JQuery<HTMLTextAreaElement> {
65
+ const {view} = getInstance(this),
66
+ {state} = view;
67
+ const handleOwnline = (from: number, to: number, text: string): string => {
68
+ /* eslint-disable no-param-reassign */
69
+ if (from > 0 && !/[\n\r]/u.test(state.sliceDoc(from - 1, from))) {
70
+ text = `\n${text}`;
71
+ pre += '\n';
72
+ }
73
+ if (!/[\n\r]/u.test(state.sliceDoc(to, to + 1))) {
74
+ text += '\n';
75
+ post += '\n';
76
+ }
77
+ return text;
78
+ /* eslint-enable no-param-reassign */
79
+ };
80
+ if (ownline && replace && !pre && !post && selectionStart === undefined && /^\s*=.*=\s*$/u.test(peri)) {
81
+ // 单独处理改变标题层级
82
+ const {selection: {main: {from, to}}} = state,
83
+ insertText = handleOwnline(from, to, peri);
84
+ view.dispatch({
85
+ changes: {from, to, insert: insertText},
86
+ selection: {anchor: from + insertText.length},
87
+ });
88
+ return this;
89
+ }
90
+ CodeMirror.replaceSelections(view, (_, {from, to}) => {
91
+ if (selectionStart !== undefined) {
92
+ /* eslint-disable no-param-reassign */
93
+ from = selectionStart;
94
+ to = selectionEnd!;
95
+ /* eslint-enable no-param-reassign */
96
+ }
97
+ const isSample = selectPeri && from === to,
98
+ selText = replace || from === to ? peri : state.sliceDoc(from, to);
99
+ let insertText = splitlines
100
+ ? selText.split('\n').map(line => `${pre}${line}${post}`).join('\n')
101
+ : `${pre}${selText}${post}`;
102
+ if (ownline) {
103
+ insertText = handleOwnline(from, to, insertText);
104
+ }
105
+ const head = insertText.length;
106
+ return isSample ? [insertText, pre.length, head - post.length] : [insertText, head];
107
+ });
108
+ return this;
109
+ },
47
110
  getCaretPosition(this: JQuery<HTMLTextAreaElement>, option?: {startAndEnd?: boolean}): [number, number] | number {
48
111
  const {view: {state: {selection: {main: {from, to, head}}}}} = getInstance(this);
49
112
  return option?.startAndEnd ? [from, to] : head;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.7.4",
3
+ "version": "2.7.6",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",