-
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.
Update Gadget:wikieditor-highlight with CodeMirror 6 (#392)
* Update MediaWiki:Gadget-wikieditor-highlight.js using CodeMirror 6 * fix(MediaWiki:Gadget-wikieditor-highlight.js): dynamic import * feat(Gadget-wikieditor-highlight): Update definition.yaml * Delete MediaWiki:Gadget-wikieditor-highlight.css * feat(wikieditor-highlight): default addons * fix(libCachedCode): ES module * refactor(Gadget-wikieditor-highlight.js): localStorage fallback * Update MediaWiki:Gadget-wikieditor-highlight.js * fix(wikieditor-highlight): avoid reusing global CodeMirror * delete temporary gadget
- Loading branch information
1 parent
95d6cf0
commit cf11a65
Showing
5 changed files
with
35 additions
and
252 deletions.
There are no files selected for viewing
71 changes: 0 additions & 71 deletions
71
src/gadgets/wikieditor-highlight-cm6/MediaWiki:Gadget-wikieditor-highlight-cm6.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
src/gadgets/wikieditor-highlight/MediaWiki:Gadget-wikieditor-highlight.css
This file was deleted.
Oops, something went wrong.
158 changes: 35 additions & 123 deletions
158
src/gadgets/wikieditor-highlight/MediaWiki:Gadget-wikieditor-highlight.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 |
---|---|---|
@@ -1,159 +1,71 @@ | ||
"use strict"; | ||
// 本页面大部分内容均直接或间接修改自[[MW:Extension:CodeMirror]] | ||
(async () => { | ||
await $.ready; | ||
if (!["edit", "submit"].includes(mw.config.get("wgAction")) || mw.config.get("wgPageContentModel") !== "wikitext") { | ||
return; | ||
} | ||
await $.ready; | ||
|
||
const localObjectStorage = new LocalObjectStorage("wikieditor-highlight"); | ||
let cm, $doc, state = localObjectStorage.getItem("wikieditor-codemirror", false); | ||
let cm, state = localObjectStorage.getItem("wikieditor-codemirror", true); | ||
const $textarea = $("#wpTextbox1"); | ||
const isAdvanced = ["loading", "loaded", "executing", "ready"].includes(mw.loader.getState("ext.wikiEditor")); | ||
const ns = mw.config.get("wgNamespaceNumber"); | ||
const lang = ns === 274 ? "html" : "mediawiki"; | ||
const init = async () => { | ||
const $search = $(".group-search a"); | ||
await libCachedCode.batchInjectCachedCode([ | ||
"https://npm.elemecdn.com/[email protected]/lib/codemirror.css", | ||
"https://npm.elemecdn.com/@bhsd/[email protected]/mediawiki.css", | ||
], "style"); | ||
await libCachedCode.batchInjectCachedCode([ | ||
"https://npm.elemecdn.com/[email protected]/lib/codemirror.js", | ||
// "https://npm.elemecdn.com/[email protected]/addon/edit/matchbrackets.js", | ||
// "https://npm.elemecdn.com/[email protected]/matchtags.js", | ||
], "script"); | ||
await libCachedCode.batchInjectCachedCode([ | ||
"https://npm.elemecdn.com/@bhsd/[email protected]/mediawiki.js", | ||
...ns === 274 ? [ | ||
"https://npm.elemecdn.com/[email protected]/mode/javascript/javascript.js", | ||
"https://npm.elemecdn.com/[email protected]/mode/css/css.js", | ||
] : [], | ||
], "script"); | ||
const mwConfig = JSON.parse(await libCachedCode.getCachedCode("https://npm.elemecdn.com/@bhsd/[email protected]/config.json")); | ||
if (ns === 274) { | ||
$.extend(mwConfig.tags, { | ||
script: true, style: true, | ||
}); | ||
$.extend(mwConfig.tagModes, { | ||
script: "javascript", style: "css", | ||
if (!window.CodeMirror6) { | ||
await new Promise((resolve) => { | ||
const script = document.createElement("script"); | ||
script.addEventListener("load", resolve); | ||
script.type = "module"; | ||
script.src = "https://testingcf.jsdelivr.net/npm/@bhsd/codemirror-mediawiki/mw/dist/base.min.js"; | ||
document.head.append(script); | ||
}); | ||
} | ||
if (isAdvanced) { | ||
cm = new CodeMirror($textarea.parent()[0], { | ||
mode: "text/mediawiki", mwConfig, | ||
lineWrapping: true, lineNumbers: true, readOnly: $textarea.prop("readonly"), | ||
matchBrackets: { | ||
bracketRegex: /[{}[\]]/, | ||
}, matchTags: true, | ||
}); | ||
} else { | ||
cm = CodeMirror.fromTextArea($textarea[0], { | ||
mode: "text/mediawiki", mwConfig, | ||
lineWrapping: true, lineNumbers: true, readOnly: $textarea.prop("readonly"), | ||
matchBrackets: { | ||
bracketRegex: /[{}[\]]/, | ||
}, matchTags: true, | ||
}); | ||
cm.setSize(null, $textarea.height()); | ||
cm = await window.CodeMirror6.fromTextArea($textarea[0], lang); | ||
cm.prefer([ | ||
"highlightSpecialChars", | ||
"highlightActiveLine", | ||
"highlightWhitespace", | ||
"bracketMatching", | ||
"closeBrackets", | ||
]); | ||
const [config] = await Promise.all([ | ||
libCachedCode.getCachedCode("https://testingcf.jsdelivr.net/npm/wikiparser-node/config/moegirl.json"), | ||
cm.defaultLint(true, {include: ns === 10}), | ||
]); | ||
try { | ||
window.wikiparse?.setConfig(JSON.parse(config)); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
mw.hook("wiki-codemirror").fire(cm); | ||
$doc = $(cm.getWrapperElement()); | ||
$.valHooks.textarea = { | ||
get: (ele_1) => ele_1 === $textarea[0] && state ? cm.getValue() : ele_1.value, | ||
set: (ele_3, val) => { | ||
ele_3 === $textarea[0] && state ? cm.setValue(val) : ele_3.value = val; | ||
}, | ||
}; | ||
if (mw.loader.getState("jquery.ui.resizable") === "ready") { | ||
$doc.resizable({ | ||
handles: "s", | ||
}); | ||
} | ||
if ($search.length === 0) { | ||
return; | ||
} | ||
cm.addKeyMap({ | ||
"Ctrl-F": () => { | ||
$search.trigger("click"); | ||
}, | ||
"Cmd-F": () => { | ||
$search.trigger("click"); | ||
}, | ||
}); | ||
}; | ||
if (state === null || !isAdvanced) { | ||
state = true; | ||
} | ||
if (!isAdvanced) { | ||
init(); | ||
return; | ||
} | ||
const $form = $(document.editform); | ||
const btn = new OO.ui.ButtonWidget({ | ||
classes: ["tool"], icon: "highlight", framed: false, title: "代码高亮开关", | ||
}).on("click", () => { | ||
}).on("click", async () => { | ||
if (cm) { | ||
$doc.toggle(); | ||
// eslint-disable-next-line no-use-before-define | ||
update(); | ||
cm.toggle(); | ||
} else { | ||
// eslint-disable-next-line no-use-before-define | ||
initAndUpdate(); | ||
await init(); | ||
} | ||
}); | ||
const fn = { | ||
getSelection: () => cm.getSelection(), | ||
setSelection: function (options) { | ||
cm.setSelection(cm.posFromIndex(options.start), cm.posFromIndex(options.end)); | ||
cm.focus(); | ||
return this; | ||
}, | ||
getCaretPosition: (options) => { | ||
const caretPos = cm.indexFromPos(cm.getCursor("from")), | ||
endPos = cm.indexFromPos(cm.getCursor("to")); | ||
if (options.startAndEnd) { | ||
return [caretPos, endPos]; | ||
} | ||
return caretPos; | ||
}, | ||
scrollToCaretPosition: function () { | ||
cm.scrollIntoView(); | ||
return this; | ||
}, | ||
}; | ||
const submit = () => { | ||
$textarea[0].value = cm.getValue(); | ||
}; | ||
const shared = () => { | ||
btn.$element.toggleClass("tool-active"); | ||
if (state) { | ||
cm.setValue($textarea[0].value); | ||
cm.setSize(null, $textarea.height()); | ||
} else { | ||
$textarea[0].value = cm.getValue(); | ||
} | ||
$textarea.toggle(); | ||
$form[state ? "on" : "off"]("submit", submit); | ||
if ($textarea.textSelection) { | ||
$textarea.textSelection(state ? "register" : "unregister", fn); | ||
} | ||
}; | ||
const update = () => { | ||
state = !state; | ||
localObjectStorage.setItem("wikieditor-codemirror", state); | ||
shared(); | ||
}; | ||
const initAndUpdate = () => { | ||
init().then(update); | ||
}; | ||
const group = $("#wikiEditor-section-main > .group-insert")[0]; | ||
}); | ||
$textarea.on("wikiEditor-toolbar-doneInitialSections", () => { | ||
btn.$element.appendTo("#wikiEditor-section-main > .group-insert"); | ||
}); | ||
const group = $("#wikiEditor-section-main > .group-insert")[0]; | ||
if (group && !group.contains(btn.$element[0])) { | ||
$textarea.trigger("wikiEditor-toolbar-doneInitialSections"); | ||
btn.$element.appendTo(group); | ||
} | ||
if (state) { | ||
mw.loader.using("ext.wikiEditor").then(init).then(shared); | ||
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