@bhsd/codemirror-mediawiki 2.1.12 → 2.1.15

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/dist/base.js DELETED
@@ -1,274 +0,0 @@
1
- import { CodeMirror6 } from 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.1.12/dist/main.min.js';
2
- (() => {
3
- var _a;
4
- mw.loader.load('https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.1.12/mediawiki.min.css', 'text/css');
5
- mw.loader.addStyleTag('.wikiEditor-ui-toolbar{z-index:7}');
6
- const instances = new WeakMap();
7
- const getInstance = ($ele) => instances.get($ele[0]);
8
- $.valHooks['textarea'] = {
9
- get(elem) {
10
- const cm = instances.get(elem);
11
- return (cm === null || cm === void 0 ? void 0 : cm.visible) ? cm.view.state.doc.toString() : elem.value;
12
- },
13
- set(elem, value) {
14
- const cm = instances.get(elem);
15
- if (cm === null || cm === void 0 ? void 0 : cm.visible) {
16
- cm.setContent(value);
17
- }
18
- else {
19
- elem.value = value;
20
- }
21
- },
22
- };
23
- function getCaretPosition(option) {
24
- const { view: { state: { selection: { main } } } } = getInstance(this);
25
- return (option === null || option === void 0 ? void 0 : option.startAndEnd) ? [main.from, main.to] : main.head;
26
- }
27
- const textSelection = {
28
- getContents() {
29
- return getInstance(this).view.state.doc.toString();
30
- },
31
- setContents(content) {
32
- getInstance(this).setContent(content);
33
- return this;
34
- },
35
- getSelection() {
36
- const { view: { state } } = getInstance(this);
37
- return state.sliceDoc(state.selection.main.from, state.selection.main.to);
38
- },
39
- setSelection({ start, end }) {
40
- const { view } = getInstance(this);
41
- view.dispatch({
42
- selection: { anchor: start, head: end !== null && end !== void 0 ? end : start },
43
- });
44
- view.focus();
45
- return this;
46
- },
47
- replaceSelection(value) {
48
- const { view } = getInstance(this);
49
- view.dispatch(view.state.replaceSelection(value));
50
- return this;
51
- },
52
- getCaretPosition,
53
- scrollToCaretPosition() {
54
- getInstance(this).view.dispatch({ scrollIntoView: true });
55
- return this;
56
- },
57
- };
58
- const USING_LOCAL = mw.loader.getState('ext.CodeMirror') !== null, DATA_MODULE = mw.loader.getState('ext.CodeMirror.data') ? 'ext.CodeMirror.data' : 'ext.CodeMirror', ALL_SETTINGS_CACHE = (_a = JSON.parse(localStorage.getItem('InPageEditMwConfig'))) !== null && _a !== void 0 ? _a : {}, SITE_ID = `${mw.config.get('wgServerName')}${mw.config.get('wgScriptPath')}`, SITE_SETTINGS = ALL_SETTINGS_CACHE[SITE_ID], VALID = Number(SITE_SETTINGS === null || SITE_SETTINGS === void 0 ? void 0 : SITE_SETTINGS.time) > Date.now() - 86400 * 1000 * 30;
59
- const getConfig = (magicWords, rule, flip) => {
60
- const words = magicWords.filter(rule).filter(({ 'case-sensitive': i }) => i !== flip)
61
- .flatMap(({ aliases, name, 'case-sensitive': i }) => aliases.map(alias => ({
62
- alias: (i ? alias : alias.toLowerCase()).replace(/:$/u, ''),
63
- name,
64
- }))), obj = {};
65
- for (const { alias, name } of words) {
66
- obj[alias] = name;
67
- }
68
- return obj;
69
- };
70
- const getConfigPair = (magicWords, rule) => [true, false]
71
- .map(bool => getConfig(magicWords, rule, bool));
72
- const setConfig = (config) => {
73
- mw.config.set('extCodeMirrorConfig', config);
74
- };
75
- const getMwConfig = async () => {
76
- if (USING_LOCAL && !VALID) {
77
- await mw.loader.using(DATA_MODULE);
78
- }
79
- let config = mw.config.get('extCodeMirrorConfig');
80
- if (!config && VALID) {
81
- ({ config } = SITE_SETTINGS);
82
- setConfig(config);
83
- }
84
- const isIPE = config && Object.values(config.functionSynonyms[0]).includes(true);
85
- if ((config === null || config === void 0 ? void 0 : config.img) && config.variants && !isIPE) {
86
- return {
87
- ...config,
88
- nsid: mw.config.get('wgNamespaceIds'),
89
- };
90
- }
91
- const { query: { general: { variants }, magicwords, extensiontags, functionhooks, variables }, } = await new mw.Api().get({
92
- meta: 'siteinfo',
93
- siprop: [
94
- 'general',
95
- 'magicwords',
96
- ...config && !isIPE ? [] : ['extensiontags', 'functionhooks', 'variables'],
97
- ],
98
- formatversion: '2',
99
- });
100
- const others = new Set(['msg', 'raw', 'msgnw', 'subst', 'safesubst']);
101
- if (config && !isIPE) {
102
- const { functionSynonyms: [insensitive] } = config;
103
- if (!('subst' in insensitive)) {
104
- Object.assign(insensitive, getConfig(magicwords, ({ name }) => others.has(name)));
105
- }
106
- }
107
- else {
108
- config = {
109
- tagModes: {
110
- tab: 'text/mediawiki',
111
- indicator: 'text/mediawiki',
112
- poem: 'text/mediawiki',
113
- ref: 'text/mediawiki',
114
- option: 'text/mediawiki',
115
- combooption: 'text/mediawiki',
116
- tabs: 'text/mediawiki',
117
- poll: 'text/mediawiki',
118
- gallery: 'text/mediawiki',
119
- },
120
- tags: {},
121
- urlProtocols: mw.config.get('wgUrlProtocols'),
122
- };
123
- for (const tag of extensiontags) {
124
- config.tags[tag.slice(1, -1)] = true;
125
- }
126
- const functions = new Set([
127
- ...functionhooks,
128
- ...variables,
129
- ...others,
130
- ]);
131
- config.functionSynonyms = getConfigPair(magicwords, ({ name }) => functions.has(name));
132
- config.doubleUnderscore = getConfigPair(magicwords, ({ aliases }) => aliases.some(alias => /^__.+__$/u.test(alias)));
133
- }
134
- config.img = getConfig(magicwords, ({ name }) => name.startsWith('img_'));
135
- config.variants = variants ? variants.map(({ code }) => code) : [];
136
- config.nsid = mw.config.get('wgNamespaceIds');
137
- setConfig(config);
138
- ALL_SETTINGS_CACHE[SITE_ID] = { config: config, time: Date.now() };
139
- localStorage.setItem('InPageEditMwConfig', JSON.stringify(ALL_SETTINGS_CACHE));
140
- return config;
141
- };
142
- const linters = {};
143
- class CodeMirror extends CodeMirror6 {
144
- constructor(textarea, lang, config) {
145
- super(textarea, lang, config);
146
- instances.set(textarea, this);
147
- if (mw.loader.getState('jquery.textSelection') === 'ready') {
148
- $(textarea).data('jquery.textSelection', textSelection);
149
- }
150
- mw.hook('wiki-codemirror6').fire(this);
151
- }
152
- toggle(show = !this.visible) {
153
- super.toggle(show);
154
- $(this.textarea).data('jquery.textSelection', show && textSelection);
155
- }
156
- async getLinter(opt) {
157
- const linter = await super.getLinter(opt);
158
- linters[this.lang] = linter;
159
- return linter;
160
- }
161
- async defaultLint(on, optOrNs) {
162
- if (!on) {
163
- this.lint();
164
- return;
165
- }
166
- const { lang } = this;
167
- let opt;
168
- if (typeof optOrNs === 'number') {
169
- if (lang === 'mediawiki' && (optOrNs === 10 || optOrNs === 828 || optOrNs === 2)) {
170
- opt = { include: true };
171
- }
172
- else if (lang === 'javascript' && (optOrNs === 8 || optOrNs === 2300)) {
173
- opt = {
174
- env: { browser: true, es6: true },
175
- parserOptions: { ecmaVersion: 6 },
176
- };
177
- }
178
- }
179
- else {
180
- opt = optOrNs;
181
- }
182
- if (!(lang in linters)) {
183
- if (lang === 'mediawiki') {
184
- const i18n = mw.config.get('wgUserLanguage');
185
- if (['zh', 'zh-hans', 'zh-cn', 'zh-sg', 'zh-my'].includes(i18n)) {
186
- opt = { ...opt, i18n: 'zh-hans' };
187
- }
188
- else if (['zh-hant', 'zh-tw', 'zh-hk', 'zh-mo'].includes(i18n)) {
189
- opt = { ...opt, i18n: 'zh-hant' };
190
- }
191
- }
192
- await this.getLinter(opt);
193
- if (lang === 'mediawiki') {
194
- const mwConfig = await getMwConfig(), config = {
195
- ...await wikiparse.getConfig(),
196
- ext: Object.keys(mwConfig.tags),
197
- namespaces: mw.config.get('wgFormattedNamespaces'),
198
- nsid: mwConfig.nsid,
199
- doubleUnderscore: mwConfig.doubleUnderscore.map(obj => Object.keys(obj).map(s => s.slice(2, -2))),
200
- variants: mwConfig.variants,
201
- protocol: mwConfig.urlProtocols.replace(/\\:/gu, ':'),
202
- };
203
- [config.parserFunction[0]] = mwConfig.functionSynonyms;
204
- if (!USING_LOCAL) {
205
- for (const [key, val] of Object.entries(mwConfig.functionSynonyms[0])) {
206
- if (!key.startsWith('#')) {
207
- config.parserFunction[0][`#${key}`] = val;
208
- }
209
- }
210
- }
211
- config.parserFunction[1] = [
212
- ...Object.keys(mwConfig.functionSynonyms[1]),
213
- '=',
214
- ];
215
- for (const key of Object.keys(mwConfig.img)) {
216
- config.img[key] = mwConfig.img[key].slice(4);
217
- }
218
- wikiparse.setConfig(config);
219
- }
220
- }
221
- else if (opt) {
222
- await this.getLinter(opt);
223
- }
224
- if (linters[lang]) {
225
- this.lint(linters[lang]);
226
- }
227
- }
228
- static async fromTextArea(textarea, lang) {
229
- const isWiki = lang === 'mediawiki' || lang === 'html', cm = new CodeMirror(textarea, isWiki ? undefined : lang);
230
- if (isWiki) {
231
- const config = await getMwConfig();
232
- cm.setLanguage(lang, config);
233
- }
234
- return cm;
235
- }
236
- }
237
- document.body.addEventListener('click', e => {
238
- if (e.target instanceof HTMLTextAreaElement && e.shiftKey && !instances.has(e.target)) {
239
- e.preventDefault();
240
- (async () => {
241
- var _a;
242
- const { wgAction, wgNamespaceNumber, wgPageContentModel } = mw.config.get();
243
- let lang;
244
- if (wgAction !== 'edit' && wgAction !== 'submit') {
245
- await mw.loader.using('oojs-ui-windows');
246
- lang = (_a = (await OO.ui.prompt('Language:') || undefined)) === null || _a === void 0 ? void 0 : _a.toLowerCase();
247
- }
248
- else {
249
- switch (wgPageContentModel) {
250
- case 'css':
251
- case 'sanitized-css':
252
- lang = 'css';
253
- break;
254
- case 'javascript':
255
- lang = 'javascript';
256
- break;
257
- case 'json':
258
- lang = 'json';
259
- break;
260
- case 'Scribunto':
261
- lang = 'lua';
262
- break;
263
- case 'wikitext':
264
- lang = wgNamespaceNumber === 274 ? 'html' : 'mediawiki';
265
- break;
266
- }
267
- }
268
- const cm = await CodeMirror.fromTextArea(e.target, lang);
269
- void cm.defaultLint(true, wgNamespaceNumber);
270
- })();
271
- }
272
- });
273
- Object.assign(window, { CodeMirror6: CodeMirror });
274
- })();