@bhsd/codemirror-mediawiki 2.4.5 → 2.5.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/mw/openLinks.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import {isMac} from './msg';
2
+ import type {SyntaxNode} from '@lezer/common';
2
3
  import type {CodeMirror} from './base';
3
4
 
4
5
  declare type MouseEventListener = (e: MouseEvent) => void;
@@ -7,6 +8,16 @@ const modKey = isMac ? 'metaKey' : 'ctrlKey',
7
8
  regex = /-template-name|-link-pagename/u,
8
9
  handlers = new WeakMap<CodeMirror, MouseEventListener>();
9
10
 
11
+ /**
12
+ * 获取节点的名称
13
+ * @param node 语法树节点
14
+ */
15
+ function getName(node: SyntaxNode): string;
16
+ function getName(node: null): undefined;
17
+ function getName(node: SyntaxNode | null): string | undefined {
18
+ return node?.name.replace(/_+/gu, ' ').trim();
19
+ }
20
+
10
21
  /**
11
22
  * 点击时在新页面打开链接、模板等
12
23
  * @param cm
@@ -26,13 +37,13 @@ const getHandler = (cm: CodeMirror): MouseEventListener => {
26
37
  return;
27
38
  }
28
39
  e.preventDefault();
29
- const {name} = node;
40
+ const name = getName(node);
30
41
  let prev = node,
31
42
  next = node;
32
- while (prev.prevSibling?.name === name) {
43
+ while (getName(prev.prevSibling!) === name) {
33
44
  prev = prev.prevSibling!;
34
45
  }
35
- while (next.nextSibling?.name === name) {
46
+ while (getName(next.nextSibling!) === name) {
36
47
  next = next.nextSibling!;
37
48
  }
38
49
  let page = view.state.sliceDoc(prev.from, next.to).trim();
@@ -8,16 +8,6 @@ export const instances = new WeakMap<HTMLTextAreaElement, CodeMirror>();
8
8
  */
9
9
  const getInstance = ($ele: JQuery<HTMLTextAreaElement>): CodeMirror => instances.get($ele[0]!)!;
10
10
 
11
- function getCaretPosition(this: JQuery<HTMLTextAreaElement>, option: {startAndEnd: true}): [number, number];
12
- function getCaretPosition(this: JQuery<HTMLTextAreaElement>, option?: {startAndEnd?: false}): number;
13
- function getCaretPosition(
14
- this: JQuery<HTMLTextAreaElement>,
15
- option?: {startAndEnd?: boolean},
16
- ): [number, number] | number {
17
- const {view: {state: {selection: {main}}}} = getInstance(this);
18
- return option?.startAndEnd ? [main.from, main.to] : main.head;
19
- }
20
-
21
11
  /**
22
12
  * jQuery.textSelection overrides for CodeMirror.
23
13
  * See jQuery.textSelection.js for method documentation
@@ -46,11 +36,16 @@ export const textSelection = {
46
36
  return this;
47
37
  },
48
38
  replaceSelection(this: JQuery<HTMLTextAreaElement>, value: string): JQuery<HTMLTextAreaElement> {
49
- const {view} = getInstance(this);
39
+ const {view} = getInstance(this),
40
+ {state: {selection: {main: {from, to}}}} = view;
41
+ view.dispatch({selection: {anchor: from, head: to}});
50
42
  view.dispatch(view.state.replaceSelection(value));
51
43
  return this;
52
44
  },
53
- getCaretPosition,
45
+ getCaretPosition(this: JQuery<HTMLTextAreaElement>, option?: {startAndEnd?: boolean}): [number, number] | number {
46
+ const {view: {state: {selection: {main}}}} = getInstance(this);
47
+ return option?.startAndEnd ? [main.from, main.to] : main.head;
48
+ },
54
49
  scrollToCaretPosition(this: JQuery<HTMLTextAreaElement>): JQuery<HTMLTextAreaElement> {
55
50
  getInstance(this).view.dispatch({scrollIntoView: true});
56
51
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.4.5",
3
+ "version": "2.5.0",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",