@bhsd/codemirror-mediawiki 3.2.0 → 3.3.0

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
@@ -25,6 +25,11 @@ Nonetheless, this repository also provides a customized version with additional
25
25
  - [lua](#lua)
26
26
  - [mediawiki](#mediawiki)
27
27
  - [vue](#vue)
28
+ - [Other languages](#other-languages)
29
+ - [Themes](#themes)
30
+ - [light](#light)
31
+ - [nord](#nord)
32
+ - [Other themes](#other-themes)
28
33
  - [Constructor](#constructor)
29
34
  - [Accessors](#accessors)
30
35
  - [dialect](#dialect)
@@ -46,6 +51,7 @@ Nonetheless, this repository also provides a customized version with additional
46
51
  - [setIndent](#setindent)
47
52
  - [setLanguage](#setlanguage)
48
53
  - [setLineWrapping](#setlinewrapping)
54
+ - [setTheme](#settheme)
49
55
  - [toggle](#toggle)
50
56
  - [update](#update)
51
57
  - [Static methods](#static-methods)
@@ -398,6 +404,41 @@ registerLanguageCore('python', python);
398
404
 
399
405
  </details>
400
406
 
407
+ # Themes
408
+
409
+ ## light
410
+
411
+ This is the default theme, which is a light theme.
412
+
413
+ ## nord
414
+
415
+ <details>
416
+ <summary>Expand</summary>
417
+
418
+ This is a dark theme created by [Takuya Matsuyama](https://www.npmjs.com/package/cm6-theme-nord) and [鬼影233](https://zh.moegirl.org.cn/User:%E9%AC%BC%E5%BD%B1233/Nord). You need to register this theme before using it:
419
+
420
+ ```js
421
+ import {registerTheme, nord} from '@bhsd/codemirror-mediawiki';
422
+ registerTheme('nord', nord);
423
+ ```
424
+
425
+ </details>
426
+
427
+ ## Other themes
428
+
429
+ <details>
430
+ <summary>Expand</summary>
431
+
432
+ You can also register other themes by importing the `registerTheme` function:
433
+
434
+ ```js
435
+ import {registerTheme} from '@bhsd/codemirror-mediawiki';
436
+ import {oneDark} from '@codemirror/theme-one-dark';
437
+ registerTheme('one-dark', oneDark);
438
+ ```
439
+
440
+ </details>
441
+
401
442
  # Constructor
402
443
 
403
444
  <details>
@@ -779,6 +820,24 @@ cm.setLineWrapping(true);
779
820
 
780
821
  </details>
781
822
 
823
+ ## setTheme
824
+
825
+ <details>
826
+ <summary>Expand</summary>
827
+
828
+ *version added: 3.3.0*
829
+
830
+ **param**: `string` the theme name
831
+ Set the theme of the editor. The default theme is `light`, other themes need to be registered using the `registerTheme` function first:
832
+
833
+ ```js
834
+ import {registerTheme, nord} from '@bhsd/codemirror-mediawiki';
835
+ registerTheme('nord', nord);
836
+ cm.setTheme('nord');
837
+ ```
838
+
839
+ </details>
840
+
782
841
  ## toggle
783
842
 
784
843
  <details>
@@ -28,6 +28,7 @@ export declare const avail: Record<string, Addon<any>>;
28
28
  export declare const linterRegistry: Record<string, LintSourceGetter>;
29
29
  export declare const menuRegistry: MenuItem[];
30
30
  export declare const destroyListeners: ((view: EditorView) => void)[];
31
+ export declare const themes: Record<string, Extension>;
31
32
  export declare const optionalFunctions: OptionalFunctions;
32
33
  /** CodeMirror 6 editor */
33
34
  export declare class CodeMirror6 {
@@ -130,6 +131,12 @@ export declare class CodeMirror6 {
130
131
  anchor: number;
131
132
  head: number;
132
133
  }): void;
134
+ /**
135
+ * Set the editor theme
136
+ * @param theme theme name
137
+ * @since 3.3.0
138
+ */
139
+ setTheme(theme: string): void;
133
140
  /**
134
141
  * Replace the current selection with the result of a function
135
142
  * @param view EditorView instance
@@ -4,6 +4,7 @@ import { syntaxHighlighting, defaultHighlightStyle, indentOnInput, indentUnit, e
4
4
  import { defaultKeymap, historyKeymap, history, redo, indentWithTab } from '@codemirror/commands';
5
5
  import { searchKeymap } from '@codemirror/search';
6
6
  import { linter, lintGutter, lintKeymap } from '@codemirror/lint';
7
+ import { light } from './theme';
7
8
  export const plain = () => EditorView.contentAttributes.of({ spellcheck: 'true' });
8
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
10
  export const languages = { plain };
@@ -12,6 +13,7 @@ export const avail = {};
12
13
  export const linterRegistry = {};
13
14
  export const menuRegistry = [];
14
15
  export const destroyListeners = [];
16
+ export const themes = { light };
15
17
  export const optionalFunctions = {
16
18
  statusBar() {
17
19
  return [];
@@ -37,6 +39,7 @@ export class CodeMirror6 {
37
39
  #extraKeys = new Compartment();
38
40
  #phrases = new Compartment();
39
41
  #lineWrapping = new Compartment();
42
+ #theme = new Compartment();
40
43
  #view;
41
44
  #lang;
42
45
  #visible = false;
@@ -95,6 +98,7 @@ export class CodeMirror6 {
95
98
  this.#extraKeys.of([]),
96
99
  this.#phrases.of(EditorState.phrases.of(phrases)),
97
100
  this.#lineWrapping.of(EditorView.lineWrapping),
101
+ this.#theme.of(light),
98
102
  syntaxHighlighting(defaultHighlightStyle),
99
103
  EditorView.contentAttributes.of({
100
104
  accesskey: accessKey,
@@ -411,6 +415,18 @@ export class CodeMirror6 {
411
415
  this.#view.dispatch({ effects });
412
416
  }
413
417
  }
418
+ /**
419
+ * Set the editor theme
420
+ * @param theme theme name
421
+ * @since 3.3.0
422
+ */
423
+ setTheme(theme) {
424
+ if (theme in themes) {
425
+ this.#view?.dispatch({
426
+ effects: this.#theme.reconfigure(themes[theme]),
427
+ });
428
+ }
429
+ }
414
430
  /**
415
431
  * Replace the current selection with the result of a function
416
432
  * @param view EditorView instance
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { CodeMirror6 } from './codemirror';
2
+ import type { Extension } from '@codemirror/state';
2
3
  import type { LanguageSupport } from '@codemirror/language';
3
4
  import type { MwConfig } from './token';
4
5
  import type { LintSourceGetter } from './lintsource';
@@ -100,3 +101,5 @@ export declare const registerLanguage: (name: string, lang: (config?: unknown) =
100
101
  * @param lintSource optional linter
101
102
  */
102
103
  export declare const registerLanguageCore: (name: string, lang: (config?: unknown) => LanguageSupport, lintSource?: LintSourceGetter) => void;
104
+ export declare const registerTheme: (name: string, theme: Extension) => void;
105
+ export { nord } from './theme';
package/dist/index.js CHANGED
@@ -28,7 +28,7 @@ import css from './css';
28
28
  import lua from './lua';
29
29
  import vue from './vue';
30
30
  import html from './html';
31
- import { CodeMirror6, avail, languages, linterRegistry, destroyListeners, plain, optionalFunctions } from './codemirror';
31
+ import { CodeMirror6, avail, languages, linterRegistry, destroyListeners, plain, optionalFunctions, themes, } from './codemirror';
32
32
  export { CodeMirror6 };
33
33
  /**
34
34
  * 注册通用扩展
@@ -105,7 +105,7 @@ export const registerColorPicker = () => {
105
105
  };
106
106
  /** 注册所有通用扩展(除`colorPicker`) */
107
107
  const registerExtensions = () => {
108
- highlightSpecialChars();
108
+ registerHighlightSpecialChars();
109
109
  registerHighlightActiveLine();
110
110
  registerHighlightWhitespace();
111
111
  registerHighlightTrailingWhitespace();
@@ -189,7 +189,7 @@ export const registerInlayHints = () => {
189
189
  export const registerColorPickerForMediaWiki = () => {
190
190
  registerLangExtension('mediawiki', 'colorPicker', [
191
191
  [makeColorPicker({ discoverColors }), colorPickerTheme],
192
- { marginLeft: '0.6ch' },
192
+ { marginLeft: '.6ch' },
193
193
  ]);
194
194
  };
195
195
  /** Register the `bracketMatching` extension for MediaWiki */
@@ -338,3 +338,7 @@ export const registerLanguageCore = (name, lang, lintSource) => {
338
338
  registerLintSource(name, lintSource);
339
339
  }
340
340
  };
341
+ export const registerTheme = (name, theme) => {
342
+ themes[name] = theme;
343
+ };
344
+ export { nord } from './theme';