@bhsd/codemirror-mediawiki 2.0.15 → 2.1.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.
@@ -12,6 +12,7 @@ export interface MwConfig {
12
12
  doubleUnderscore: [Record<string, unknown>, Record<string, unknown>];
13
13
  variants?: string[];
14
14
  img?: Record<string, string>;
15
+ nsid: Record<string, number>;
15
16
  }
16
17
  /**
17
18
  * Gets a LanguageSupport instance for the MediaWiki mode.
package/mediawiki.css CHANGED
@@ -38,7 +38,6 @@
38
38
  .cm-mw-skipformatting {
39
39
  background-color: #adf;
40
40
  }
41
- .cm-mw-indenting,
42
41
  .cm-mw-list {
43
42
  color: #08f;
44
43
  font-weight: bold;
@@ -105,12 +104,6 @@
105
104
  .cm-mw-extlink,
106
105
  .cm-mw-free-extlink {
107
106
  color: #36c;
108
- }
109
- .cm-mw-link-pagename:not(.cm-mw-strong),
110
- .cm-mw-link-bracket,
111
- .cm-mw-link-delimiter,
112
- .cm-mw-extlink:not(.cm-mw-strong),
113
- .cm-mw-free-extlink:not(.cm-mw-strong) {
114
107
  font-weight: normal;
115
108
  }
116
109
  .cm-mw-link,
package/mw/dist/base.js CHANGED
@@ -1,16 +1,16 @@
1
- import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.0.15/dist/main.min.js';
1
+ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.1.1/dist/main.min.js';
2
2
  (() => {
3
3
  var _a;
4
- mw.loader.load('https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.0.15/mediawiki.min.css', 'text/css');
4
+ mw.loader.load('https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.1.1/mediawiki.min.css', 'text/css');
5
5
  const instances = new WeakMap();
6
6
  $.valHooks['textarea'] = {
7
7
  get(elem) {
8
8
  const cm = instances.get(elem);
9
- return cm ? cm.view.state.doc.toString() : elem.value;
9
+ return (cm === null || cm === void 0 ? void 0 : cm.visible) ? cm.view.state.doc.toString() : elem.value;
10
10
  },
11
11
  set(elem, value) {
12
12
  const cm = instances.get(elem);
13
- if (cm) {
13
+ if (cm === null || cm === void 0 ? void 0 : cm.visible) {
14
14
  cm.view.dispatch({
15
15
  changes: { from: 0, to: cm.view.state.doc.length, insert: value },
16
16
  });
@@ -78,7 +78,10 @@ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror
78
78
  }
79
79
  const isIPE = config && Object.values(config.functionSynonyms[0]).includes(true);
80
80
  if (config && config.img && config.variants && !isIPE) {
81
- return config;
81
+ return {
82
+ ...config,
83
+ nsid: mw.config.get('wgNamespaceIds'),
84
+ };
82
85
  }
83
86
  const { query: { general: { variants }, magicwords, extensiontags, functionhooks, variables }, } = await new mw.Api().get({
84
87
  meta: 'siteinfo',
@@ -126,6 +129,7 @@ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror
126
129
  }
127
130
  config.img = getConfig(getAliases(magicwords.filter(({ name }) => name.startsWith('img_'))));
128
131
  config.variants = variants ? variants.map(({ code }) => code) : [];
132
+ config.nsid = mw.config.get('wgNamespaceIds');
129
133
  mw.config.set('extCodeMirrorConfig', config);
130
134
  ALL_SETTINGS_CACHE[SITE_ID] = { config: config, time: Date.now() };
131
135
  localStorage.setItem('InPageEditMwConfig', JSON.stringify(ALL_SETTINGS_CACHE));
@@ -135,20 +139,32 @@ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror
135
139
  class CodeMirror extends CodeMirror6 {
136
140
  constructor(textarea, lang, config) {
137
141
  super(textarea, lang, config);
142
+ this.visible = true;
138
143
  instances.set(textarea, this);
139
144
  if (mw.loader.getState('jquery.textSelection') === 'ready') {
140
145
  $(textarea).data('jquery.textSelection', textSelection);
141
146
  }
142
147
  }
148
+ toggle(show = !this.visible) {
149
+ if (show && !this.visible) {
150
+ this.view.dispatch({
151
+ changes: { from: 0, to: this.view.state.doc.length, insert: this.textarea.value },
152
+ });
153
+ }
154
+ this.visible = show;
155
+ this.view.dom.style.setProperty('display', show ? '' : 'none', 'important');
156
+ this.textarea.style.display = show ? 'none' : '';
157
+ $(this.textarea).data('jquery.textSelection', show && textSelection);
158
+ }
143
159
  async defaultLint(on) {
144
160
  if (!on) {
145
- super.lint();
161
+ this.lint();
146
162
  return;
147
163
  }
148
164
  const { lang } = this;
149
165
  if (!(lang in linters)) {
150
166
  linters[lang] = await this.getLinter();
151
- if (this.lang === 'mediawiki') {
167
+ if (this.lang === 'mediawiki' || this.lang === 'html') {
152
168
  const mwConfig = await getMwConfig(), config = {
153
169
  ...await wikiparse.getConfig(),
154
170
  ext: Object.keys(mwConfig.tags),
@@ -156,7 +172,7 @@ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror
156
172
  nsid: mw.config.get('wgNamespaceIds'),
157
173
  doubleUnderscore: mwConfig.doubleUnderscore.map(obj => Object.keys(obj).map(s => s.slice(2, -2))),
158
174
  variants: mwConfig.variants,
159
- protocol: mwConfig.urlProtocols,
175
+ protocol: mwConfig.urlProtocols.replace(/\\:/gu, ':'),
160
176
  };
161
177
  [config.parserFunction[0]] = mwConfig.functionSynonyms;
162
178
  config.parserFunction[1] = [
@@ -169,23 +185,49 @@ import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror
169
185
  wikiparse.setConfig(config);
170
186
  }
171
187
  }
172
- super.lint(linters[lang]);
188
+ this.lint(linters[lang]);
173
189
  }
174
190
  static async fromTextArea(textarea, lang) {
175
- const cm = new CodeMirror(textarea, lang === 'mediawiki' ? undefined : lang);
176
- if (lang === 'mediawiki') {
191
+ const isWiki = lang === 'mediawiki' || lang === 'html', cm = new CodeMirror(textarea, isWiki ? undefined : lang);
192
+ if (isWiki) {
177
193
  const config = await getMwConfig();
178
- cm.setLanguage('mediawiki', config);
194
+ cm.setLanguage(lang, config);
179
195
  }
180
196
  return cm;
181
197
  }
182
198
  }
183
- $(document.body).click(async (e) => {
199
+ document.body.addEventListener('click', e => {
184
200
  if (e.target instanceof HTMLTextAreaElement && (e.ctrlKey || e.metaKey) && !instances.has(e.target)) {
185
201
  e.preventDefault();
186
- await mw.loader.using('oojs-ui-windows');
187
- const lang = await OO.ui.prompt('Language:') || undefined, cm = await CodeMirror.fromTextArea(e.target, lang === null || lang === void 0 ? void 0 : lang.toLowerCase());
188
- void cm.defaultLint(true);
202
+ (async () => {
203
+ var _a;
204
+ const { wgAction, wgNamespaceNumber, wgPageContentModel } = mw.config.get();
205
+ let lang;
206
+ if (wgAction !== 'edit' && wgAction !== 'submit') {
207
+ await mw.loader.using('oojs-ui-windows');
208
+ lang = (_a = (await OO.ui.prompt('Language:') || undefined)) === null || _a === void 0 ? void 0 : _a.toLowerCase();
209
+ }
210
+ else {
211
+ switch (wgPageContentModel) {
212
+ case 'css':
213
+ case 'sanitized-css':
214
+ lang = 'css';
215
+ break;
216
+ case 'javascript':
217
+ case 'json':
218
+ lang = 'javascript';
219
+ break;
220
+ case 'Scribunto':
221
+ lang = 'lua';
222
+ break;
223
+ case 'wikitext':
224
+ lang = wgNamespaceNumber === 274 ? 'html' : 'mediawiki';
225
+ break;
226
+ }
227
+ }
228
+ const cm = await CodeMirror.fromTextArea(e.target, lang);
229
+ void cm.defaultLint(true);
230
+ })();
189
231
  }
190
232
  });
191
233
  Object.assign(window, { CodeMirror });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/codemirror-mediawiki",
3
- "version": "2.0.15",
3
+ "version": "2.1.1",
4
4
  "description": "Modified CodeMirror mode based on wikimedia/mediawiki-extensions-CodeMirror",
5
5
  "keywords": [
6
6
  "mediawiki",