From 3309545f8874fa96d5b34b7d4d815258b49063a7 Mon Sep 17 00:00:00 2001 From: bhsd <2545473905@qq.com> Date: Wed, 17 Jan 2024 14:27:07 +0800 Subject: [PATCH 1/4] feat(cm6.js): CodeMirror 6 support --- src/plugins/code-mirror/cm6.js | 91 ++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/plugins/code-mirror/cm6.js diff --git a/src/plugins/code-mirror/cm6.js b/src/plugins/code-mirror/cm6.js new file mode 100644 index 0000000..37a97f5 --- /dev/null +++ b/src/plugins/code-mirror/cm6.js @@ -0,0 +1,91 @@ +/* eslint-env browser, jquery */ +/* eslint indent: [2, 2], semi: [2, "never"], operator-linebreak: 2, strict: 0 */ +/* global mw, InPageEdit, CodeMirror6 */ +/** + * @name code-mirror 语法高亮编辑器 + * @author Bhsd + * @author 机智的小鱼君 + */ +mw.hook('InPageEdit').add(() => + (async () => { + // Constants + const CM_CDN = 'https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki' + + mw.loader.addStyleTag('.in-page-edit .cm-editor{min-height:400px!important}') + + await Promise.all([ + mw.loader.using('mediawiki.Title'), + window.CodeMirror6 || import(`${CM_CDN}/mw/dist/base.min.js`), + ]) + + /** + * 检查页面语言类型 + * @param page Page title + */ + function getPageMode(page) { + const {namespace, title} = page + const isSubject = namespace % 2 === 0 + if (/\.css$/i.test(title) && isSubject) { + return 'css' + } else if (/\.js$/i.test(title) && isSubject) { + return 'javascript' + } else if (/\.json$/i.test(title) && isSubject) { + return 'json' + } else if (namespace === 828 && !title.endsWith('/doc')) { + return 'lua' + } else if (namespace === 274 && !title.endsWith('/doc')) { + return 'html' + } + return 'mediawiki' + } + + /** + * 渲染编辑器 + * @param {JQuery} target 目标编辑框 + * @param page Page title + */ + async function renderEditor(target, page) { + if (target.length) { + // 防止抑郁 + const clearDiv = '
' + target.before(clearDiv) + target.after(clearDiv) + + const mode = getPageMode(page) + + const wikiEditor = InPageEdit.preference + .get('plugins') + .some(i => /wiki-editor/.test(i)) + + if (wikiEditor) { + await new Promise(resolve => { + target.on('wikiEditor-toolbar-doneInitialSections', resolve) + }) + } + + const cm = await CodeMirror6.fromTextArea(target[0], mode) + cm.prefer([ + 'highlightSpecialChars', + 'highlightActiveLine', + 'highlightWhitespace', + 'bracketMatching', + 'closeBrackets', + ]) + + cm.defaultLint(true, mode === 'mediawiki' ? {include: page.namespace === 10} : undefined) + return cm + } + } + + /** + * 为 quickEdit 钩子添加函数 + */ + mw.hook('InPageEdit.quickEdit').add(({ $editArea, $modalTitle }) => + (async () => { + const page = new mw.Title($modalTitle.find('.editPage').text()) + const cm = await renderEditor($editArea, page) + mw.hook('InPageEdit.quickEdit.codemirror6').fire({ $editArea, cm }) + })(), + ) + })(), +) From 3a9813663cc0bd2a8ed5c30bc2436f8087bf5523 Mon Sep 17 00:00:00 2001 From: bhsd <2545473905@qq.com> Date: Wed, 17 Jan 2024 17:03:43 +0800 Subject: [PATCH 2/4] Update index.json --- src/index.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.json b/src/index.json index 7a188d6..7141fc9 100644 --- a/src/index.json +++ b/src/index.json @@ -19,8 +19,13 @@ "author": "dragon-fish" }, "code-mirror/script.js": { - "name": "[BETA] Code Mirror", - "description": "Syntaxhighlight editor for InPageEdit. Currently supported languages: Wikitext, CSS, JavaScript, JSON, Lua.", + "name": "CodeMirror 5", + "description": "Syntaxhighlight editor for InPageEdit. Currently supported languages: Wikitext, CSS, JavaScript, JSON, Lua. In conflict with CodeMirror 6.", + "author": "InPageEdit" + }, + "code-mirror/cm6.js": { + "name": "[BETA] CodeMirror 6", + "description": "Syntaxhighlight editor for InPageEdit. Currently supported languages: Wikitext, CSS, JavaScript, JSON, Lua. In conflict with CodeMirror 5.", "author": "InPageEdit" }, "color-preview.js": { From 0d42912a5dfd7c16005c6f81cacf3a1d84c23fcf Mon Sep 17 00:00:00 2001 From: bhsd <2545473905@qq.com> Date: Wed, 17 Jan 2024 17:18:22 +0800 Subject: [PATCH 3/4] fix(cm6.js): ext --- src/plugins/code-mirror/cm6.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/plugins/code-mirror/cm6.js b/src/plugins/code-mirror/cm6.js index 37a97f5..ebddc4e 100644 --- a/src/plugins/code-mirror/cm6.js +++ b/src/plugins/code-mirror/cm6.js @@ -24,12 +24,13 @@ mw.hook('InPageEdit').add(() => */ function getPageMode(page) { const {namespace, title} = page + const ext = page.ext.toLowerCase() const isSubject = namespace % 2 === 0 - if (/\.css$/i.test(title) && isSubject) { + if (ext === 'css' && isSubject) { return 'css' - } else if (/\.js$/i.test(title) && isSubject) { + } else if (ext === 'js' && isSubject) { return 'javascript' - } else if (/\.json$/i.test(title) && isSubject) { + } else if (ext === 'json' && isSubject) { return 'json' } else if (namespace === 828 && !title.endsWith('/doc')) { return 'lua' From 2153964c2e7af8e56ff3aa41212183d4b9815440 Mon Sep 17 00:00:00 2001 From: bhsd <2545473905@qq.com> Date: Wed, 17 Jan 2024 17:23:10 +0800 Subject: [PATCH 4/4] fix(cm6.js): ext --- src/plugins/code-mirror/cm6.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/code-mirror/cm6.js b/src/plugins/code-mirror/cm6.js index ebddc4e..dbccd12 100644 --- a/src/plugins/code-mirror/cm6.js +++ b/src/plugins/code-mirror/cm6.js @@ -24,7 +24,7 @@ mw.hook('InPageEdit').add(() => */ function getPageMode(page) { const {namespace, title} = page - const ext = page.ext.toLowerCase() + const ext = page.ext?.toLowerCase() const isSubject = namespace % 2 === 0 if (ext === 'css' && isSubject) { return 'css'