wikiplus-highlight 3.0.7 → 3.1.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.
Files changed (3) hide show
  1. package/README.md +3 -3
  2. package/dist/main.js +87 -58
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Wikiplus-highlight
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/wikiplus-highlight.svg)](https://www.npmjs.com/package/wikiplus-highlight)
4
- [![CodeQL](https://github.com/bhsd-harry/Wikiplus-highlight/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/bhsd-harry/Wikiplus-highlight/actions/workflows/github-code-scanning/codeql)
4
+ [![CodeQL](https://github.com/bhsd-harry/Wikiplus-highlight/actions/workflows/codeql.yml/badge.svg)](https://github.com/bhsd-harry/Wikiplus-highlight/actions/workflows/codeql.yml)
5
5
 
6
6
  **Wikiplus-highlight** 是由 Bhsd 编写的一款 [Wikiplus](https://github.com/Wikiplus/Wikiplus) 语法高亮插件,它主要基于 MediaWiki 内置的 [JavaScript 模块](https://www.mediawiki.org/wiki/ResourceLoader/Core_modules)和 [CodeMirror](https://codemirror.net/) 编写。
7
7
 
@@ -9,13 +9,13 @@
9
9
 
10
10
  在*个人 JS 页*添加以下代码:
11
11
 
12
- ```javascript
12
+ ```js
13
13
  mw.loader.load('//cdn.jsdelivr.net/npm/wikiplus-highlight');
14
14
  ```
15
15
 
16
16
 
17
17
 
18
- ```javascript
18
+ ```js
19
19
  mw.loader.load('//unpkg.com/wikiplus-highlight');
20
20
  ```
21
21
 
package/dist/main.js CHANGED
@@ -9,21 +9,23 @@
9
9
  if (wphl === null || wphl === void 0 ? void 0 : wphl.version) {
10
10
  return;
11
11
  }
12
- const version = '3.0.7';
13
- libs.wphl = { version, cmVersion: '2.9', ...wphl }; // 开始加载
12
+ const version = '3.1.0';
13
+ libs.wphl = { version, cmVersion: '2.9', monacoVersion: '0.2.2', ...wphl }; // 开始加载
14
14
  // 路径
15
- const CDN = '//testingcf.jsdelivr.net', MW_CDN = `npm/@bhsd/codemirror-mediawiki@${libs.wphl.cmVersion || 'latest'}/dist/mw.min.js`, REPO_CDN = 'npm/wikiplus-highlight';
16
- const /** 加载CodeMirror 6 */ init = 'CodeMirror6' in window
17
- ? Promise.resolve()
18
- : new Promise(resolve => {
19
- const script = document.createElement('script');
20
- script.addEventListener('load', () => {
21
- resolve();
22
- });
23
- script.type = 'module';
24
- script.src = `${CDN}/${MW_CDN}`;
25
- document.head.appendChild(script);
26
- });
15
+ const CDN = '//testingcf.jsdelivr.net', MW_CDN = `npm/@bhsd/codemirror-mediawiki@${libs.wphl.cmVersion || 'latest'}/dist/mw.min.js`, MONACO_CDN = `npm/monaco-wiki@${libs.wphl.monacoVersion || 'latest'}/dist/mw.min.js`, REPO_CDN = 'npm/wikiplus-highlight';
16
+ const useMonaco = wphl === null || wphl === void 0 ? void 0 : wphl.useMonaco;
17
+ if (useMonaco) {
18
+ window.MonacoWikiEditor || (window.MonacoWikiEditor = (async () => {
19
+ await $.ajax(`${CDN}/${MONACO_CDN}`, { dataType: 'script', scriptAttrs: { type: 'module' } });
20
+ return MonacoWikiEditor;
21
+ })());
22
+ }
23
+ else {
24
+ window.CodeMirror6 || (window.CodeMirror6 = (async () => {
25
+ await $.ajax(`${CDN}/${MW_CDN}`, { dataType: 'script', scriptAttrs: { type: 'module' } });
26
+ return CodeMirror6;
27
+ })());
28
+ }
27
29
  const { wgPageName: page, wgNamespaceNumber: ns, wgPageContentModel: contentmodel, } = mw.config.get();
28
30
  const CONTENTMODELS = {
29
31
  'sanitized-css': 'css',
@@ -36,8 +38,8 @@
36
38
  828: 'lua',
37
39
  274: 'html',
38
40
  };
39
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/unbound-method
40
- const getObject = mw.storage.getObject || ((key) => JSON.parse(String(localStorage.getItem(key))));
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ const getObject = (key) => JSON.parse(String(localStorage.getItem(key)));
41
43
  /**
42
44
  * 检查页面语言类型
43
45
  * @param value 页面内容
@@ -91,57 +93,84 @@
91
93
  }
92
94
  return [ns === 274 ? 'html' : 'lua'];
93
95
  };
96
+ const submit = /** 提交编辑 */ () => {
97
+ document.getElementById('Wikiplus-Quickedit-Submit').dispatchEvent(new MouseEvent('click'));
98
+ return true;
99
+ }, submitMinor = /** 提交小编辑 */ () => {
100
+ document.querySelector('#Wikiplus-Quickedit-MinorEdit').checked = true;
101
+ return submit();
102
+ }, escapeEdit = /** 按下Esc键退出编辑 */ () => {
103
+ document.getElementById('Wikiplus-Quickedit-Back').dispatchEvent(new MouseEvent('click'));
104
+ return true;
105
+ };
94
106
  /**
95
107
  * 渲染编辑器
96
108
  * @param $target 目标编辑框
97
109
  * @param setting 是否是Wikiplus设置(使用json语法)
98
110
  */
99
111
  const renderEditor = async ($target, setting) => {
100
- await init;
101
- const cm = await CodeMirror6.fromTextArea($target[0], ...setting ? ['json'] : await getPageMode($target.val()));
102
- cm.view.dom.id = 'Wikiplus-CodeMirror';
103
- document.querySelector('#Wikiplus-Quickedit-Jump > a').href = '#Wikiplus-CodeMirror';
104
- if (!setting) { // 普通Wikiplus编辑区
105
- const settings = getObject('Wikiplus_Settings'), escToExitQuickEdit = settings && (settings['esc_to_exit_quickedit'] || settings['escToExitQuickEdit']), submit = /** 提交编辑 */ () => {
106
- document.getElementById('Wikiplus-Quickedit-Submit').dispatchEvent(new MouseEvent('click'));
107
- return true;
108
- }, submitMinor = /** 提交小编辑 */ () => {
109
- document.querySelector('#Wikiplus-Quickedit-MinorEdit').checked = true;
110
- return submit();
111
- }, escapeEdit = /** 按下Esc键退出编辑 */ () => {
112
- document.getElementById('Wikiplus-Quickedit-Back').dispatchEvent(new MouseEvent('click'));
113
- return true;
114
- };
115
- cm.extraKeys([
116
- { key: 'Mod-S', run: submit },
117
- { key: 'Shift-Mod-S', run: submitMinor },
118
- ...escToExitQuickEdit === true || escToExitQuickEdit === 'true'
119
- ? [{ key: 'Esc', run: escapeEdit }]
120
- : [],
121
- ]);
112
+ const settings = getObject('Wikiplus_Settings'), escToExitQuickEdit = settings && (settings['esc_to_exit_quickedit'] || settings['escToExitQuickEdit']);
113
+ if (useMonaco) {
114
+ const lang = setting ? 'json' : (await getPageMode($target.val()))[0], { editor } = await (await MonacoWikiEditor).fromTextArea($target[0], lang);
115
+ editor.getDomNode().id = 'Wikiplus-CodeMirror';
116
+ if (!setting) { // 普通Wikiplus编辑区
117
+ editor.onKeyUp(e => {
118
+ if (e.keyCode === 49 && (e.ctrlKey || e.metaKey)) {
119
+ e.preventDefault();
120
+ if (e.shiftKey) {
121
+ submitMinor();
122
+ }
123
+ else {
124
+ submit();
125
+ }
126
+ }
127
+ else if (e.keyCode === 9
128
+ && (escToExitQuickEdit === true || escToExitQuickEdit === 'true')) {
129
+ e.preventDefault();
130
+ escapeEdit();
131
+ }
132
+ });
133
+ }
122
134
  }
123
- /** @todo 以下过渡代码添加于2024-02-07,将于一段时间后弃用 */
124
- const oldKey = 'Wikiplus-highlight-addons', oldPrefs = getObject(oldKey), mapping = {
125
- activeLine: 'highlightActiveLine',
126
- trailingspace: 'highlightTrailingWhitespace',
127
- matchBrackets: 'bracketMatching',
128
- closeBrackets: 'closeBrackets',
129
- matchTags: 'tagMatching',
130
- fold: 'codeFolding',
131
- wikiEditor: 'wikiEditor',
132
- escape: 'escape',
133
- contextmenu: 'openLinks',
134
- lint: 'lint',
135
- };
136
- localStorage.removeItem(oldKey);
137
- if (oldPrefs) {
138
- const obj = {};
139
- for (const k of oldPrefs) {
140
- if (k in mapping) {
141
- obj[mapping[k]] = true;
135
+ else {
136
+ const cm = await (await CodeMirror6).fromTextArea($target[0], ...setting ? ['json'] : await getPageMode($target.val()));
137
+ cm.view.dom.id = 'Wikiplus-CodeMirror';
138
+ if (!setting) { // 普通Wikiplus编辑区
139
+ cm.extraKeys([
140
+ { key: 'Mod-S', run: submit },
141
+ { key: 'Shift-Mod-S', run: submitMinor },
142
+ ...escToExitQuickEdit === true || escToExitQuickEdit === 'true'
143
+ ? [{ key: 'Esc', run: escapeEdit }]
144
+ : [],
145
+ ]);
146
+ }
147
+ /** @todo 以下过渡代码添加于2024-02-07,将于一段时间后弃用 */
148
+ const oldKey = 'Wikiplus-highlight-addons', oldPrefs = getObject(oldKey), mapping = {
149
+ activeLine: 'highlightActiveLine',
150
+ trailingspace: 'highlightTrailingWhitespace',
151
+ matchBrackets: 'bracketMatching',
152
+ closeBrackets: 'closeBrackets',
153
+ matchTags: 'tagMatching',
154
+ fold: 'codeFolding',
155
+ wikiEditor: 'wikiEditor',
156
+ escape: 'escape',
157
+ contextmenu: 'openLinks',
158
+ lint: 'lint',
159
+ };
160
+ localStorage.removeItem(oldKey);
161
+ if (oldPrefs) {
162
+ const obj = {};
163
+ for (const k of oldPrefs) {
164
+ if (k in mapping) {
165
+ obj[mapping[k]] = true;
166
+ }
142
167
  }
168
+ cm.prefer(obj);
143
169
  }
144
- cm.prefer(obj);
170
+ }
171
+ const jump = document.querySelector('#Wikiplus-Quickedit-Jump > a');
172
+ if (jump) {
173
+ jump.href = '#Wikiplus-CodeMirror';
145
174
  }
146
175
  };
147
176
  // 监视 Wikiplus 编辑框
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikiplus-highlight",
3
- "version": "3.0.7",
3
+ "version": "3.1.0",
4
4
  "description": "A plugin for the MediaWiki front-end add-on \"Wikiplus\"",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -44,6 +44,7 @@
44
44
  "eslint-plugin-promise": "^6.1.1",
45
45
  "eslint-plugin-regexp": "^2.2.0",
46
46
  "eslint-plugin-unicorn": "^51.0.1",
47
+ "monaco-wiki": "^0.2.0",
47
48
  "stylelint": "^16.1.0",
48
49
  "stylelint-config-recommended": "^14.0.0",
49
50
  "types-mediawiki": "^1.4.0",