@bhsd/codemirror-mediawiki 2.7.5 → 2.7.7

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.
@@ -1,5 +1,4 @@
1
- import type {SelectionRange} from '@codemirror/state';
2
- import type {CodeMirror} from './base';
1
+ import {CodeMirror} from './base';
3
2
 
4
3
  export const instances = new WeakMap<HTMLTextAreaElement, CodeMirror>();
5
4
 
@@ -65,30 +64,32 @@ export const textSelection = {
65
64
  }: EncapsulateOptions): JQuery<HTMLTextAreaElement> {
66
65
  const {view} = getInstance(this),
67
66
  {state} = view;
68
- const handleOwnline = (from: number, to: number, text: string): string => {
69
- /* eslint-disable no-param-reassign */
70
- if (from > 0 && !/[\n\r]/u.test(state.sliceDoc(from - 1, from))) {
71
- text = `\n${text}`;
72
- pre += '\n';
73
- }
74
- if (!/[\n\r]/u.test(state.sliceDoc(to, to + 1))) {
75
- text += '\n';
76
- post += '\n';
67
+ const handleOwnline = (from: number, to: number, text: string): [string, number, number] => {
68
+ let start = 0,
69
+ end = 0;
70
+ if (ownline) {
71
+ if (from > 0 && !/[\n\r]/u.test(state.sliceDoc(from - 1, from))) {
72
+ text = `\n${text}`; // eslint-disable-line no-param-reassign
73
+ start = 1;
74
+ }
75
+ if (!/[\n\r]/u.test(state.sliceDoc(to, to + 1))) {
76
+ text += '\n'; // eslint-disable-line no-param-reassign
77
+ end = 1;
78
+ }
77
79
  }
78
- return text;
79
- /* eslint-enable no-param-reassign */
80
+ return [text, start, end];
80
81
  };
81
82
  if (ownline && replace && !pre && !post && selectionStart === undefined && /^\s*=.*=\s*$/u.test(peri)) {
82
83
  // 单独处理改变标题层级
83
84
  const {selection: {main: {from, to}}} = state,
84
- insertText = handleOwnline(from, to, peri);
85
+ [insertText] = handleOwnline(from, to, peri);
85
86
  view.dispatch({
86
87
  changes: {from, to, insert: insertText},
87
88
  selection: {anchor: from + insertText.length},
88
89
  });
89
90
  return this;
90
91
  }
91
- view.dispatch(state.changeByRange(({from, to}) => {
92
+ CodeMirror.replaceSelections(view, (_, {from, to}) => {
92
93
  if (selectionStart !== undefined) {
93
94
  /* eslint-disable no-param-reassign */
94
95
  from = selectionStart;
@@ -96,21 +97,17 @@ export const textSelection = {
96
97
  /* eslint-enable no-param-reassign */
97
98
  }
98
99
  const isSample = selectPeri && from === to,
99
- selText = replace || from === to ? peri : state.sliceDoc(from, to);
100
- let insertText = splitlines
101
- ? selText.split('\n').map(line => `${pre}${line}${post}`).join('\n')
102
- : `${pre}${selText}${post}`;
103
- if (ownline) {
104
- insertText = handleOwnline(from, to, insertText);
105
- }
106
- const head = from + insertText.length;
107
- return {
108
- changes: {from, to, insert: insertText},
109
- range: (isSample
110
- ? {anchor: from + pre.length, head: head - post.length}
111
- : {anchor: head, head}) as SelectionRange,
112
- };
113
- }));
100
+ selText = replace || from === to ? peri : state.sliceDoc(from, to),
101
+ [insertText, start, end] = handleOwnline(
102
+ from,
103
+ to,
104
+ splitlines
105
+ ? selText.split('\n').map(line => `${pre}${line}${post}`).join('\n')
106
+ : `${pre}${selText}${post}`,
107
+ ),
108
+ head = from + insertText.length;
109
+ return isSample ? [insertText, from + pre.length + start, head - post.length - end] : [insertText, head];
110
+ });
114
111
  return this;
115
112
  },
116
113
  getCaretPosition(this: JQuery<HTMLTextAreaElement>, option?: {startAndEnd?: boolean}): [number, number] | number {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.7.5",
3
+ "version": "2.7.7",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",