wikiplus-highlight 2.8.0 → 2.12.1

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/matchtags.js CHANGED
@@ -10,7 +10,8 @@
10
10
  const {Pos, cmpPos} = CodeMirror;
11
11
 
12
12
  const tagStart = /<(\/?)([A-Z_a-z]\w*)/g,
13
- voidTags = ['br', 'wbr', 'hr', 'img'];
13
+ voidTags = ['br', 'wbr', 'hr', 'img'],
14
+ maxScanLines = 1000;
14
15
 
15
16
  class Iter {
16
17
  /**
@@ -18,9 +19,7 @@
18
19
  * @param {CodeMirror.Position} pos
19
20
  */
20
21
  constructor(cm, pos) {
21
- const {line, ch} = pos,
22
- /** @type {{state: {matchTags: {maxScanLines: number}}}} */
23
- {state: {matchTags: {maxScanLines = 1000}}} = cm;
22
+ const {line, ch} = pos;
24
23
  this.line = line;
25
24
  this.ch = ch;
26
25
  this.cm = cm;
@@ -138,7 +137,10 @@
138
137
  }
139
138
  }
140
139
 
141
- /** @param {string} tag */
140
+ /**
141
+ * @param {string} tag
142
+ * @returns {CodeMirror.matchingTag}
143
+ */
142
144
  findMatchingClose(tag) {
143
145
  const /** @type {string[]} */ stack = [];
144
146
  for (;;) {
@@ -176,7 +178,10 @@
176
178
  }
177
179
  }
178
180
 
179
- /** @param {string} tag */
181
+ /**
182
+ * @param {string|undefined} tag
183
+ * @returns {CodeMirror.matchingTag}
184
+ */
180
185
  findMatchingOpen(tag) {
181
186
  const /** @type {string[]} */ stack = [];
182
187
  for (;;) {
@@ -215,35 +220,43 @@
215
220
  }
216
221
  }
217
222
 
218
- CodeMirror.defineExtension('findMatchingTag', /** @param {CodeMirror.Position} pos */ function(pos) {
219
- let iter = new Iter(this, pos);
220
- if (!iter.isTag()) {
221
- return;
222
- }
223
- const end = iter.toTagEnd(),
224
- to = end && Pos(iter.line, iter.ch);
225
- const start = end && iter.toTagStart();
226
- if (!start || cmpPos(iter, pos) > 0) {
227
- return;
228
- }
229
- const tag = start[2].toLowerCase(),
230
- here = {from: Pos(iter.line, iter.ch), to, tag};
231
- if (end === 'selfClose' || voidTags.includes(tag)) {
232
- return {open: here, close: null, at: 'self'};
233
- }
223
+ CodeMirror.defineExtension(
224
+ 'findMatchingTag',
225
+ /**
226
+ * @param {CodeMirror.Position} pos
227
+ * @returns {CodeMirror.matchingTags}
228
+ */
229
+ function(pos) {
230
+ let iter = new Iter(this, pos);
231
+ if (!iter.isTag()) {
232
+ return;
233
+ }
234
+ const end = iter.toTagEnd(),
235
+ to = end && Pos(iter.line, iter.ch);
236
+ const start = end && iter.toTagStart();
237
+ if (!start || cmpPos(iter, pos) > 0) {
238
+ return;
239
+ }
240
+ const tag = start[2].toLowerCase(),
241
+ here = {from: Pos(iter.line, iter.ch), to, tag};
242
+ if (end === 'selfClose' || voidTags.includes(tag)) {
243
+ return {open: here, close: null, at: 'self'};
244
+ }
234
245
 
235
- if (start[1]) { // closing tag
236
- return {open: iter.findMatchingOpen(tag), close: here, at: 'close'};
237
- } // opening tag
238
- iter = new Iter(this, to);
239
- return {open: here, close: iter.findMatchingClose(tag), at: 'open'};
240
- });
246
+ if (start[1]) { // closing tag
247
+ return {open: iter.findMatchingOpen(tag), close: here, at: 'close'};
248
+ } // opening tag
249
+ iter = new Iter(this, to);
250
+ return {open: here, close: iter.findMatchingClose(tag), at: 'open'};
251
+ },
252
+ );
241
253
 
242
254
  CodeMirror.defineExtension(
243
255
  'findEnclosingTag',
244
256
  /**
245
257
  * @param {CodeMirror.Position} pos
246
258
  * @param {string} tag
259
+ * @returns {CodeMirror.matchingTags}
247
260
  */
248
261
  function(pos, tag) {
249
262
  const iter = new Iter(this, pos);
@@ -259,12 +272,7 @@
259
272
  },
260
273
  );
261
274
 
262
- /**
263
- * Used by addon/edit/closetag.js
264
- * @param {CodeMirror.Editor} cm
265
- * @param {CodeMirror.Position} pos
266
- * @param {string} name
267
- */
275
+ // Used by addon/edit/closetag.js
268
276
  CodeMirror.scanForClosingTag = function(cm, pos, name) {
269
277
  const iter = new Iter(cm, pos);
270
278
  return iter.findMatchingClose(name);
@@ -276,13 +284,12 @@
276
284
  clear(cm);
277
285
  }
278
286
  if (val) {
279
- cm.state.matchTags = typeof val === 'object' ? val : {};
280
287
  cm.on('cursorActivity', doMatchTags);
281
288
  doMatchTags(cm);
282
289
  }
283
290
  });
284
291
 
285
- /** @param {CodeMirror.Editor} cm */
292
+ /** @param {CodeMirror.EditorWithMatchingTags} cm */
286
293
  function clear(cm) {
287
294
  if (cm.state.tagHit) {
288
295
  cm.state.tagHit.clear();
@@ -294,22 +301,14 @@
294
301
  cm.state.tagOther = null;
295
302
  }
296
303
 
297
- /** @param {CodeMirror.Editor} cm */
304
+ /** @param {CodeMirror.EditorWithMatchingTags} cm */
298
305
  function doMatchTags(cm) {
299
306
  cm.operation(() => {
300
307
  clear(cm);
301
308
  if (cm.somethingSelected()) {
302
309
  return;
303
310
  }
304
-
305
- /**
306
- * @typedef {object} matchingTag
307
- * @property {string} at
308
- * @property {{from: CodeMirror.Position, to: CodeMirror.Position}} open
309
- * @property {{from: CodeMirror.Position, to: CodeMirror.Position}} close
310
- */
311
-
312
- const /** @type {matchingTag}} */ match = cm.findMatchingTag(cm.getCursor());
311
+ const match = cm.findMatchingTag(cm.getCursor());
313
312
  if (!match) {
314
313
  return;
315
314
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiplus-highlight",
3
- "version": "2.8.0",
3
+ "version": "2.12.1",
4
4
  "description": "A plugin for the MediaWiki front-end add-on \"Wikiplus\"",
5
5
  "main": "main.js",
6
6
  "scripts": {
@@ -23,6 +23,7 @@
23
23
  "homepage": "https://github.com/bhsd-harry/Wikiplus-highlight#readme",
24
24
  "devDependencies": {
25
25
  "eslint": "^8.8.0",
26
+ "types-mediawiki": "^1.2.0",
26
27
  "uglify-js": "^3.15.5"
27
28
  }
28
29
  }
package/search.js CHANGED
@@ -1,15 +1,12 @@
1
1
  /**
2
2
  * @author Bhsd <https://github.com/bhsd-harry>
3
- * @license: GPL-3.0
3
+ * @license GPL-3.0
4
4
  */
5
5
 
6
6
  (() => {
7
7
  'use strict';
8
8
 
9
- /**
10
- * @param {string} key
11
- * @returns {string}
12
- */
9
+ /** @param {string} key */
13
10
  const msg = key => mw.msg(`wphl-${key}`);
14
11
 
15
12
  // Prepare elements
@@ -46,7 +43,7 @@
46
43
  text: msg('addon-search'),
47
44
  });
48
45
 
49
- const /** @type {(str: string) => string} */ escapeRegExp = mw.util.escapeRegExp || mw.RegExp.escape;
46
+ const escapeRegExp = mw.util.escapeRegExp || mw.RegExp.escape;
50
47
  const /** @type {{token: (stream: CodeMirror.StringStream) => string}} */ overlay = {token: () => {}};
51
48
 
52
49
  /**
@@ -74,17 +71,9 @@
74
71
  $search.css('background-color', '').off('input', onInput);
75
72
  };
76
73
 
77
- /**
78
- * @typedef {object} SearchCursor
79
- * @property {() => boolean} findNext
80
- * @property {() => boolean} findPrevious
81
- * @property {() => CodeMirror.Position} from
82
- * @property {() => CodeMirror.Position} to
83
- */
84
-
85
74
  // keyboard event handler of $search
86
75
  let /** @type {string|RegExp} */ lastPtn,
87
- /** @type {SearchCursor} */ cursor;
76
+ /** @type {CodeMirror.SearchCursor} */ cursor;
88
77
  /**
89
78
  * @param {CodeMirror.Editor} cm
90
79
  * @param {boolean} dir
@@ -108,7 +97,7 @@
108
97
  cursor = cm.getSearchCursor(ptn, cm.getCursor(), {caseFold: true});
109
98
  }
110
99
  const method = dir ? 'findNext' : 'findPrevious';
111
- let result = cursor[method]();
100
+ let /** @type {boolean} */ result = cursor[method]();
112
101
  if (!result) {
113
102
  if (dir) {
114
103
  cursor = cm.getSearchCursor(ptn, {line: 0, ch: 0}, {caseFold: true});
@@ -147,22 +136,23 @@
147
136
  lastPtn = '';
148
137
  };
149
138
 
150
- /** @param {CodeMirror.Editor} doc */
151
139
  CodeMirror.commands.findForward = doc => {
152
140
  findNext(doc, true);
153
141
  };
154
- /** @param {CodeMirror.Editor} doc */
155
142
  CodeMirror.commands.findBackward = doc => {
156
143
  findNext(doc, false);
157
144
  };
158
145
 
159
- const /** @type {{name: string}} */ {name} = $.client.profile(),
146
+ const {name} = $.client.profile(),
160
147
  focus = name === 'safari'
161
148
  ? /** @param {CodeMirror.Editor} cm */ cm => {
162
149
  cm.focus();
163
150
  }
164
151
  : () => {};
165
152
  mw.hook('wiki-codemirror').add(/** @param {CodeMirror.Editor} cm */ cm => {
153
+ if (!cm.getOption('styleSelectedText')) {
154
+ return;
155
+ }
166
156
  const $textarea = $(cm.getWrapperElement()).prev('#Wikiplus-Quickedit');
167
157
  if ($textarea.length === 0) {
168
158
  return;