@bhsd/codemirror-mediawiki 2.1.1 → 2.1.3

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/README.md CHANGED
@@ -10,11 +10,13 @@
10
10
  - [textarea](#textarea)
11
11
  - [lang](#lang)
12
12
  - [view](#view)
13
+ - [visible](#visible)
13
14
  - [getLinter](#getlinter)
14
15
  - [lint](#lint)
15
16
  - [prefer](#prefer)
16
17
  - [setIndent](#setindent)
17
18
  - [setLanguage](#setlanguage)
19
+ - [toggle](#toggle)
18
20
  - [update](#update)
19
21
 
20
22
  </details>
@@ -31,14 +33,14 @@ You can download the code via CDN, for example:
31
33
 
32
34
  ```js
33
35
  // static import
34
- import { CodeMirror6 } from 'https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.0.8/dist/main.min.js';
36
+ import {CodeMirror6} from 'https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki/dist/main.min.js';
35
37
  ```
36
38
 
37
39
  or
38
40
 
39
41
  ```js
40
42
  // dynamic import
41
- const { CodeMirror6 } = await import( 'https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.0.8/dist/main.min.js' );
43
+ const {CodeMirror6} = await import('https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki/dist/main.min.js');
42
44
  ```
43
45
 
44
46
  ## constructor
@@ -51,7 +53,13 @@ const { CodeMirror6 } = await import( 'https://cdn.jsdelivr.net/npm/@bhsd/codemi
51
53
  **param**: `unknown` the optional language configuration
52
54
 
53
55
  ```js
54
- const cm = new CodeMirror6( textarea, 'css' );
56
+ const cm = new CodeMirror6(textarea); // plain text
57
+ const cm = new CodeMirror6(textarea, 'mediawiki', mwConfig);
58
+ const cm = new CodeMirror6(textarea, 'html', mwConfig); // mixed MediaWiki-HTML
59
+ const cm = new CodeMirror6(textarea, 'css');
60
+ const cm = new CodeMirror6(textarea, 'javascript');
61
+ const cm = new CodeMirror6(textarea, 'json');
62
+ const cm = new CodeMirror6(textarea, 'lua');
55
63
  ```
56
64
 
57
65
  </details>
@@ -62,7 +70,7 @@ const cm = new CodeMirror6( textarea, 'css' );
62
70
  <summary>Expand</summary>
63
71
 
64
72
  **type**: `HTMLTextAreaElement`
65
- The textarea element replaced by CodeMirror.
73
+ The textarea element replaced by CodeMirror, read-only.
66
74
 
67
75
  </details>
68
76
 
@@ -74,7 +82,7 @@ The textarea element replaced by CodeMirror.
74
82
  *version added: 2.0.13*
75
83
 
76
84
  **type**: `string`
77
- The current language mode.
85
+ The current language mode, read-only.
78
86
 
79
87
  </details>
80
88
 
@@ -84,7 +92,19 @@ The current language mode.
84
92
  <summary>Expand</summary>
85
93
 
86
94
  **type**: [`EditorView`](https://codemirror.net/6/docs/ref/#view.EditorView)
87
- The CodeMirror EditorView instance.
95
+ The CodeMirror EditorView instance, read-only.
96
+
97
+ </details>
98
+
99
+ ## visible
100
+
101
+ <details>
102
+ <summary>Expand</summary>
103
+
104
+ *version added: 2.1.3*
105
+
106
+ **type**: `boolean`
107
+ Whether the editor is visible, read-only.
88
108
 
89
109
  </details>
90
110
 
@@ -93,11 +113,19 @@ The CodeMirror EditorView instance.
93
113
  <details>
94
114
  <summary>Expand</summary>
95
115
 
96
- *version added: 2.0.13*
116
+ *version added: 2.1.3*
97
117
 
118
+ **param**: `Record<string, any>` the optional linter configuration
98
119
  **returns**: `Promise<(doc: Text) => Diagnostic[] | Promise<Diagnostic[]>>`
99
120
  Get the default linting function, which can be used as the argument of [`lint`](#lint).
100
121
 
122
+ ```js
123
+ const linter = await cm.getLinter(); // default linter configuration
124
+ const linterMediawiki = await cm.getLinter({include}); // wikilint configuration
125
+ const linterJavaScript = await cm.getLinter({env, parserOptions, rules}); // ESLint configuration
126
+ const linterCSS = await cm.getLinter({rules}); // Stylelint configuration
127
+ ```
128
+
101
129
  </details>
102
130
 
103
131
  ## lint
@@ -109,7 +137,7 @@ Get the default linting function, which can be used as the argument of [`lint`](
109
137
  Set the linting function.
110
138
 
111
139
  ```js
112
- cm.lint( ( doc ) => [
140
+ cm.lint(doc => [
113
141
  /**
114
142
  * @type {Diagnostic}
115
143
  * @see https://codemirror.net/docs/ref/#lint.Diagnostic
@@ -120,7 +148,7 @@ cm.lint( ( doc ) => [
120
148
  message: 'error message',
121
149
  severity: 'error',
122
150
  },
123
- ] );
151
+ ]);
124
152
  ```
125
153
 
126
154
  </details>
@@ -136,14 +164,14 @@ cm.lint( ( doc ) => [
136
164
  Set the preferred CodeMirror extensions.
137
165
 
138
166
  ```js
139
- cm.prefer( [
167
+ cm.prefer([
140
168
  'bracketMatching',
141
169
  'closeBrackets',
142
170
  'highlightActiveLine',
143
171
  'highlightSpecialChars',
144
172
  'highlightWhitespace',
145
173
  'highlightTrailingWhitespace',
146
- ] );
174
+ ]);
147
175
  ```
148
176
 
149
177
  </details>
@@ -159,7 +187,8 @@ cm.prefer( [
159
187
  Set the indentation string.
160
188
 
161
189
  ```js
162
- cm.setIndent( ' '.repeat( 2 ) );
190
+ cm.setIndent(' '.repeat(2));
191
+ cm.setIndent('\t');
163
192
  ```
164
193
 
165
194
  </details>
@@ -174,20 +203,39 @@ cm.setIndent( ' '.repeat( 2 ) );
174
203
  Set the language mode.
175
204
 
176
205
  ```js
177
- cm.setLanguage( 'css' );
206
+ cm.setLanguage('mediawiki', mwConfig);
207
+ cm.setLanguage('html', mwConfig); // mixed MediaWiki-HTML
208
+ cm.setLanguage('css');
209
+ cm.setLanguage('javascript');
210
+ cm.setLanguage('json');
211
+ cm.setLanguage('lua');
178
212
  ```
179
213
 
180
214
  </details>
181
215
 
182
- ## update
216
+ ## toggle
183
217
 
184
218
  <details>
185
219
  <summary>Expand</summary>
186
220
 
187
- Refresh linting immediately.
221
+ *version added: 2.1.3*
222
+
223
+ **param**: `boolean` whether to show the editor, optional
224
+ Switch between the CodeMirror editor and the native textarea.
188
225
 
189
226
  ```js
190
- cm.update();
227
+ cm.toggle();
228
+ cm.toggle(true); // show CodeMirror
229
+ cm.toggle(false); // hide CodeMirror
191
230
  ```
192
231
 
193
232
  </details>
233
+
234
+ ## update
235
+
236
+ <details>
237
+ <summary>Expand</summary>
238
+
239
+ Refresh linting immediately.
240
+
241
+ </details>
@@ -5,9 +5,10 @@ export type { MwConfig } from './mediawiki';
5
5
  export type LintSource = (doc: Text) => Diagnostic[] | Promise<Diagnostic[]>;
6
6
  export declare class CodeMirror6 {
7
7
  #private;
8
- lang: string;
9
8
  get textarea(): HTMLTextAreaElement;
10
9
  get view(): EditorView;
10
+ get lang(): string;
11
+ get visible(): boolean;
11
12
  /**
12
13
  * @param textarea 文本框
13
14
  * @param lang 语言
@@ -27,10 +28,21 @@ export declare class CodeMirror6 {
27
28
  lint(lintSource?: LintSource): void;
28
29
  /** 立即更新语法检查 */
29
30
  update(): void;
30
- /** 添加扩展 */
31
+ /**
32
+ * 添加扩展
33
+ * @param names 扩展名
34
+ */
31
35
  prefer(names: readonly string[]): void;
32
- /** 设置缩进 */
36
+ /**
37
+ * 设置缩进
38
+ * @param indent 缩进字符串
39
+ */
33
40
  setIndent(indent: string): void;
34
41
  /** 获取默认linter */
35
- getLinter(): Promise<LintSource>;
42
+ getLinter(opt?: Record<string, unknown>): Promise<LintSource | undefined>;
43
+ /**
44
+ * 在编辑器和文本框之间切换
45
+ * @param show 是否显示编辑器
46
+ */
47
+ toggle(show?: boolean): void;
36
48
  }
package/dist/config.d.ts CHANGED
@@ -6,16 +6,11 @@
6
6
  import { Tag } from '@lezer/highlight';
7
7
  /**
8
8
  * Configuration for the MediaWiki highlighting mode for CodeMirror.
9
- * This is a separate class mainly to keep static configuration out of
10
- * the logic in CodeMirrorModeMediaWiki.
11
9
  */
12
10
  export declare const modeConfig: {
13
11
  /**
14
12
  * All HTML/XML tags permitted in MediaWiki Core.
15
13
  *
16
- * Extensions should use the CodeMirrorTagModes extension attribute to register tags
17
- * instead of adding them here.
18
- *
19
14
  * @see https://www.mediawiki.org/wiki/Extension:CodeMirror#Extension_integration
20
15
  */
21
16
  permittedHtmlTags: Set<string>;
@@ -25,14 +20,9 @@ export declare const modeConfig: {
25
20
  implicitlyClosedHtmlTags: Set<string>;
26
21
  /**
27
22
  * Mapping of MediaWiki-esque token identifiers to a standardized lezer highlighting tag.
28
- * Values are one of the default highlighting tags. The idea is to use as many default tags as
29
- * possible so that theming (such as dark mode) can be applied with minimal effort. The
30
- * semantic meaning of the tag may not really match how it is used, but as per CodeMirror docs,
31
- * this is fine. It's still better to make use of the standard tags in some way.
23
+ * Values are one of the default highlighting tags.
32
24
  *
33
25
  * Once we allow use of other themes, we may want to tweak these values for aesthetic reasons.
34
- * The values here can freely be changed. The actual CSS class used is defined further down
35
- * in highlightStyle().
36
26
  *
37
27
  * @see https://lezer.codemirror.net/docs/ref/#highlight.tags
38
28
  * @internal
@@ -130,7 +120,6 @@ export declare const modeConfig: {
130
120
  };
131
121
  /**
132
122
  * These are custom tokens (a.k.a. tags) that aren't mapped to any of the standardized tags.
133
- * Make sure these are also defined in tags() above.
134
123
  *
135
124
  * TODO: pass parent Tags in Tag.define() where appropriate for better theming.
136
125
  *