-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Gadget-wikieditor-highlight-cm6 (#393)
Update MediaWiki:Gadget-wikieditor-highlight.js using CodeMirror 6 Beta version for techedit
- Loading branch information
1 parent
4823bbd
commit b0e4dc8
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/gadgets/wikieditor-highlight-cm6/MediaWiki:Gadget-wikieditor-highlight-cm6.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
// 本页面大部分内容均直接或间接修改自[[MW:Extension:CodeMirror]] | ||
(async () => { | ||
if (!["edit", "submit"].includes(mw.config.get("wgAction")) || mw.config.get("wgPageContentModel") !== "wikitext") { | ||
return; | ||
} | ||
await $.ready; | ||
|
||
const localObjectStorage = new LocalObjectStorage("wikieditor-highlight"); | ||
let cm, state = localObjectStorage.getItem("wikieditor-codemirror", false); | ||
const $textarea = $("#wpTextbox1"); | ||
const isAdvanced = ["loading", "loaded", "executing", "ready"].includes(mw.loader.getState("ext.wikiEditor")); | ||
const lang = mw.config.get("wgNamespaceNumber") === 274 ? "html" : "mediawiki"; | ||
const init = () => new Promise((resolve) => { | ||
const script = document.createElement("script"); | ||
script.addEventListener("load", async () => { | ||
cm = await CodeMirror.fromTextArea($textarea[0], lang); | ||
cm.defaultLint(true); | ||
cm.prefer([ | ||
"highlightSpecialChars", | ||
"highlightActiveLine", | ||
"highlightWhitespace", | ||
"bracketMatching", | ||
"closeBrackets", | ||
]); | ||
resolve(); | ||
}); | ||
script.type = "module"; | ||
script.src = "https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki@2.1.2/mw/dist/base.min.js"; | ||
document.head.appendChild(script); | ||
}); | ||
if (!isAdvanced) { | ||
init(); | ||
return; | ||
} else if (state === null || !isAdvanced) { | ||
state = true; | ||
} | ||
const btn = new OO.ui.ButtonWidget({ | ||
classes: ["tool"], icon: "highlight", framed: false, title: "代码高亮开关", | ||
}).on("click", async () => { | ||
if (cm) { | ||
cm.toggle(); | ||
} else { | ||
await init(); | ||
} | ||
btn.$element.toggleClass("tool-active"); | ||
state = !state; | ||
localObjectStorage.setItem("wikieditor-codemirror", state); | ||
}); | ||
const group = $("#wikiEditor-section-main > .group-insert")[0]; | ||
$textarea.on("wikiEditor-toolbar-doneInitialSections", () => { | ||
btn.$element.appendTo("#wikiEditor-section-main > .group-insert"); | ||
}); | ||
if (group && !group.contains(btn.$element[0])) { | ||
$textarea.trigger("wikiEditor-toolbar-doneInitialSections"); | ||
} | ||
if (state) { | ||
await mw.loader.using("ext.wikiEditor"); | ||
await init(); | ||
btn.$element.addClass("tool-active"); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
ResourceLoader: true | ||
|
||
hidden: false | ||
|
||
default: false | ||
|
||
supportsUrlLoad: false | ||
|
||
targets: [] | ||
|
||
skins: [] | ||
|
||
actions: [] | ||
|
||
type: general | ||
|
||
package: false | ||
|
||
rights: | ||
- techedit | ||
|
||
peers: [] | ||
|
||
dependencies: | ||
- oojs-ui-core | ||
- ext.gadget.libPolyfill | ||
- ext.gadget.LocalObjectStorage | ||
|
||
_sites: | ||
- zh | ||
|
||
_section: editing | ||
|
||
_files: | ||
- MediaWiki:Gadget-wikieditor-highlight-cm6.js |