@bhsd/codemirror-mediawiki 2.3.3 → 2.3.5

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,16 +1,13 @@
1
- const {vendor, userAgent, maxTouchPoints, platform} = navigator,
2
- modKey = vendor.includes('Apple Computer') && (userAgent.includes('Mobile/') || maxTouchPoints > 2)
3
- || platform.includes('Mac')
4
- ? 'metaKey'
5
- : 'ctrlKey';
1
+ import {isMac} from './msg';
6
2
 
7
- export const pageSelector = '.cm-mw-template-name, .cm-mw-link-pagename';
3
+ const modKey = isMac ? 'metaKey' : 'ctrlKey',
4
+ pageSelector = '.cm-mw-template-name, .cm-mw-link-pagename';
8
5
 
9
6
  /**
10
7
  * 点击时在新页面打开链接、模板等
11
8
  * @param e 点击事件
12
9
  */
13
- export const openLinks = function(this: HTMLElement, e: JQuery.ClickEvent): void {
10
+ const handler = function(this: HTMLElement, e: JQuery.ClickEvent): void {
14
11
  if (!e[modKey]) {
15
12
  return;
16
13
  }
@@ -37,3 +34,17 @@ export const openLinks = function(this: HTMLElement, e: JQuery.ClickEvent): void
37
34
  }
38
35
  open(new mw.Title(page, this.classList.contains('cm-mw-template-name') ? 10 : 0).getUrl(undefined), '_blank');
39
36
  };
37
+
38
+ /**
39
+ * 添加或移除打开链接的事件
40
+ * @param ele 编辑器DOM
41
+ * @param on 是否添加
42
+ */
43
+ export const openLinks = (ele: HTMLElement, on?: boolean): void => {
44
+ if (on) {
45
+ mw.loader.load('mediawiki.Title');
46
+ $(ele).on('click', pageSelector, handler).css('--codemirror-cursor', 'pointer');
47
+ } else if (on === false) {
48
+ $(ele).off('click', pageSelector, handler).css('--codemirror-cursor', '');
49
+ }
50
+ };
package/mw/preference.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import {msg} from './msg';
2
2
  import type {CodeMirror} from './base';
3
3
 
4
- export const storageKey = 'codemirror-mediawiki-addons',
5
- indentKey = 'codemirror-mediawiki-indent';
4
+ const storageKey = 'codemirror-mediawiki-addons';
5
+ export const indentKey = 'codemirror-mediawiki-indent',
6
+ prefs = new Set<string>(JSON.parse(localStorage.getItem(storageKey)!) as string[] | null);
6
7
 
7
8
  const options = [
8
9
  'allowMultipleSelections',
@@ -28,13 +29,12 @@ let dialog: OO.ui.MessageDialog | undefined,
28
29
 
29
30
  /**
30
31
  * 打开设置对话框
31
- * @param addons 预设的扩展
32
32
  * @param editors CodeMirror实例
33
33
  */
34
- export const openPreference = async (addons: Set<string>, editors: (CodeMirror | undefined)[]): Promise<void> => {
34
+ export const openPreference = async (editors: (CodeMirror | undefined)[]): Promise<void> => {
35
35
  await mw.loader.using(['oojs-ui-windows', 'oojs-ui.styles.icons-content']);
36
36
  if (dialog) {
37
- widget.setValue([...addons] as unknown as string);
37
+ widget.setValue([...prefs] as unknown as string);
38
38
  indentWidget.setValue(indent);
39
39
  } else {
40
40
  dialog = new OO.ui.MessageDialog();
@@ -43,13 +43,13 @@ export const openPreference = async (addons: Set<string>, editors: (CodeMirror |
43
43
  windowManager.addWindows([dialog]);
44
44
  widget = new OO.ui.CheckboxMultiselectInputWidget({
45
45
  options: options.map(option => ({data: option, label: $($.parseHTML(msg(`addon-${option}`)))})),
46
- value: [...addons] as unknown as string,
46
+ value: [...prefs] as unknown as string,
47
47
  });
48
48
  field = new OO.ui.FieldLayout(widget, {
49
49
  label: msg('label'),
50
50
  align: 'top',
51
51
  });
52
- indentWidget = new OO.ui.TextInputWidget({placeholder: '\\t'});
52
+ indentWidget = new OO.ui.TextInputWidget({value: indent, placeholder: '\\t'});
53
53
  indentField = new OO.ui.FieldLayout(indentWidget, {label: msg('addon-indent')});
54
54
  }
55
55
 
@@ -65,9 +65,9 @@ export const openPreference = async (addons: Set<string>, editors: (CodeMirror |
65
65
  if (typeof data === 'object' && data.action === 'accept') {
66
66
  const value = widget.getValue() as unknown as string[];
67
67
  indent = indentWidget.getValue(); // eslint-disable-line require-atomic-updates
68
- addons.clear();
68
+ prefs.clear();
69
69
  for (const option of value) {
70
- addons.add(option);
70
+ prefs.add(option);
71
71
  }
72
72
  for (const cm of editors) {
73
73
  cm?.prefer(value);
@@ -2,6 +2,10 @@ import type {CodeMirror} from './base';
2
2
 
3
3
  export const instances = new WeakMap<HTMLTextAreaElement, CodeMirror>();
4
4
 
5
+ /**
6
+ * 获取CodeMirror实例
7
+ * @param $ele textarea元素的jQuery对象
8
+ */
5
9
  const getInstance = ($ele: JQuery<HTMLTextAreaElement>): CodeMirror => instances.get($ele[0]!)!;
6
10
 
7
11
  function getCaretPosition(this: JQuery<HTMLTextAreaElement>, option: {startAndEnd: true}): [number, number];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.3.3",
3
+ "version": "2.3.5",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",