MediaWiki:Gadget-PreviewWithVariant2017.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
/**
* SPDX-License-Identifier: MIT
* _addText: '{{Gadget Header|license=MIT|attribution=Diskdance, et. al.}}'
*
* @base {@link https://zh.wikipedia.org/wiki/MediaWiki:Gadget-PreviewWithVariant2017.js}
* @base {@link https://zh.wikipedia.org/wiki/MediaWiki:Gadget-PreviewWithVariant2017.css}
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/PreviewWithVariant2017}
* @license MIT {@link https://zh.wikipedia.org/wiki/MediaWiki:Gadget-PreviewWithVariant2017.js}
*/
/**
* Copyright Diskdance
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* +------------------------------------------------------------+
* | === WARNING: GLOBAL GADGET FILE === |
* +------------------------------------------------------------+
* | All changes should be made in the repository, |
* | otherwise they will be lost. |
* +------------------------------------------------------------+
* | Changes to this page may affect many users. |
* | Please discuss changes by opening an issue before editing. |
* +------------------------------------------------------------+
*/
/* <nowiki> */
(() => {
"use strict";
// dist/PreviewWithVariant2017/PreviewWithVariant2017.js
function _createForOfIteratorHelper(r, e) {
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (!t) {
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
t && (r = t);
var n = 0, F = function() {
};
return { s: F, n: function() {
return n >= r.length ? { done: true } : { done: false, value: r[n++] };
}, e: function(r2) {
throw r2;
}, f: F };
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var o, a = true, u = false;
return { s: function() {
t = t.call(r);
}, n: function() {
var r2 = t.next();
return a = r2.done, r2;
}, e: function(r2) {
u = true, o = r2;
}, f: function() {
try {
a || null == t.return || t.return();
} finally {
if (u) throw o;
}
} };
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
//! src/PreviewWithVariant2017/options.json
var configKey = "gadget-PreviewWithVariant2017__Initialized";
var className = "pwv-2017-variant";
//! src/PreviewWithVariant2017/modules/constant.ts
var DATA = [{
var: "zh",
htmlLang: "zh",
msg: "pwv-2017-zh"
}, {
var: "zh-hans",
htmlLang: "zh-Hans",
msg: "pwv-2017-zh-hans"
}, {
var: "zh-hant",
htmlLang: "zh-Hant",
msg: "pwv-2017-zh-hant"
}, {
var: "zh-cn",
htmlLang: "zh-Hans-CN",
msg: "pwv-2017-zh-cn"
}, {
var: "zh-hk",
htmlLang: "zh-Hant-HK",
msg: "pwv-2017-zh-hk"
}, {
var: "zh-mo",
htmlLang: "zh-Hant-MO",
msg: "pwv-2017-zh-mo"
}, {
var: "zh-my",
htmlLang: "zh-Hans-MY",
msg: "pwv-2017-zh-my"
}, {
var: "zh-sg",
htmlLang: "zh-Hans-SG",
msg: "pwv-2017-zh-sg"
}, {
var: "zh-tw",
htmlLang: "zh-Hant-TW",
msg: "pwv-2017-zh-tw"
}];
//! src/PreviewWithVariant2017/modules/messages.ts
var PWV2017messages = () => {
mw.messages.set({
"pwv-2017-caption": window.wgULS("选择语言变体", "選擇語言變體"),
"pwv-2017-zh": window.wgULS("不转换", "不轉換"),
"pwv-2017-zh-hans": "简体",
"pwv-2017-zh-hant": "繁體",
"pwv-2017-zh-cn": "中国大陆简体",
"pwv-2017-zh-hk": "中國香港繁體",
"pwv-2017-zh-mo": "中國澳門繁體",
"pwv-2017-zh-my": "马来西亚简体",
"pwv-2017-zh-sg": "新加坡简体",
"pwv-2017-zh-tw": "中國臺灣繁體"
});
};
//! src/PreviewWithVariant2017/modules/processVisualEditor.js
mw.config.set(configKey, false);
PWV2017messages();
var PendingStackLayout = function PendingStackLayout2(config) {
PendingStackLayout2.super.call(this, config);
OO.ui.mixin.PendingElement.call(this);
this.$element.addClass("pwv-2017-pendingStackLayout");
};
OO.inheritClass(PendingStackLayout, OO.ui.StackLayout);
OO.mixinClass(PendingStackLayout, OO.ui.mixin.PendingElement);
var processVisualEditor = () => {
const {
skin,
wgUserLanguage,
wgUserVariant
} = mw.config.get();
let variant, target, dialog, dropdown, stackLayout, panelLayouts, windowManager, errorDialog;
const constructDocument = (title, wikitext, categories) => {
const $result = $("<div>").addClass("mw-body mw-body-content");
if (skin === "vector") {
$result.addClass("vector-body");
}
$result.append(
$("<h1>").addClass("firstHeading").html(title),
// Classes used here:
// * mw-content-ltr
// * mw-content-rtl
$("<div>").addClass("mw-content-".concat(mw.config.get("wgVisualEditor").pageLanguageDir)).attr("lang", DATA.find((item) => {
return item.var === variant;
}).htmlLang).html(wikitext),
$.parseHTML(categories)
);
mw.hook("wikipage.content").fire($result);
ve.targetLinksToNewWindow($result[0]);
return $result;
};
const fetchPreview = () => {
const deferred = $.Deferred();
target.getContentApi().post({
action: "parse",
disableeditsection: true,
errorformat: "html",
errorlang: wgUserLanguage,
errorsuselocal: true,
formatversion: 2,
prop: "text|indicators|displaytitle|categorieshtml|parsewarningshtml",
pst: true,
preview: true,
title: target.getPageName(),
text: target.getDocToSave(),
uselang: wgUserLanguage,
variant
}).then((response) => {
void deferred.resolve(constructDocument(response.parse.displaytitle, response.parse.text, response.parse.categorieshtml));
}, (error, detail) => {
void deferred.reject(detail);
});
return deferred;
};
const changeVariant = function changeVariant2(val) {
dialog.previewPanel.$element[0].focus();
variant = val;
const targetPanel = stackLayout.findItemFromData(variant);
if (targetPanel.$element.children().length) {
stackLayout.setItem(targetPanel);
} else {
stackLayout.pushPending();
dropdown.setDisabled(true);
void fetchPreview().then(($previewContainer) => {
targetPanel.$element.append($previewContainer);
stackLayout.setItem(targetPanel);
}, (error) => {
windowManager.openWindow(errorDialog, {
title: OO.ui.msg("ooui-dialog-process-error"),
// eslint-disable-next-line no-undef
message: ve.init.target.getContentApi().getErrorMessage(error),
actions: [{
action: "reject",
label: OO.ui.deferMsg("ooui-dialog-message-reject"),
flags: "safe"
}, {
action: "retry",
label: OO.ui.deferMsg("ooui-dialog-process-retry"),
flags: ["primary", "progressive"]
}]
}).closed.then((data) => {
if (data && data.action === "retry") {
changeVariant2(variant);
} else {
dropdown.setValue(stackLayout.getCurrentItem().getData());
}
});
}).always(() => {
stackLayout.popPending();
dropdown.setDisabled(false);
});
}
};
const previewWithVariant = () => {
const currentPanel = stackLayout.getCurrentItem();
if (currentPanel.$element.children().length) {
dialog.swapPanel("preview");
dialog.previewPanel.$element.prepend(dropdown.$element);
} else {
target.emit("savePreview");
dialog.pushPending();
void fetchPreview().then(($previewContent) => {
target.getSurface().getModel().getDocument().once("transact", () => {
var _iterator = _createForOfIteratorHelper(panelLayouts), _step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done; ) {
const item = _step.value;
item.$element.empty();
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
});
dialog.swapPanel("preview");
currentPanel.$element.append($previewContent);
stackLayout.setItem(stackLayout.findItemFromData(variant));
const $body = $("body");
if (!$body.find(".".concat(className)).length) {
dialog.previewPanel.$element.prepend(dropdown.$element);
}
}, (error) => {
dialog.showErrors(
// eslint-disable-next-line no-undef
new OO.ui.Error(ve.init.target.getContentApi().getErrorMessage(error), {
recoverable: true
})
);
}).always(() => {
dialog.popPending();
});
}
};
const init = () => {
variant = wgUserVariant;
({
target
} = ve.init);
dialog = target.saveDialog;
dropdown = new OO.ui.DropdownInputWidget({
$overlay: dialog.$overlay,
classes: [className],
options: [{
optgroup: mw.msg("pwv-2017-caption")
}, ...DATA.map((item) => {
return {
data: item.var,
label: mw.msg(item.msg)
};
})],
value: variant
});
dropdown.on("change", changeVariant);
panelLayouts = DATA.map((item) => {
return new OO.ui.PanelLayout({
expanded: false,
data: item.var
});
});
stackLayout = new PendingStackLayout({
expanded: false,
items: panelLayouts
});
stackLayout.setItem(stackLayout.findItemFromData(variant));
dialog.previewPanel.$element.append(stackLayout.$element);
errorDialog = new OO.ui.MessageDialog();
windowManager = new OO.ui.WindowManager();
windowManager.addWindows([errorDialog]);
const $body = $("body");
$body.append(windowManager.$element);
const handlerToRemove = "onSaveDialogPreview";
dialog.off("preview", handlerToRemove, target).on("preview", previewWithVariant);
mw.hook("ve.activationComplete").add(() => {
if (mw.config.get(configKey)) {
mw.config.set(configKey, false);
}
});
};
if (!mw.config.get(configKey)) {
init();
mw.config.set(configKey, true);
}
};
//! src/PreviewWithVariant2017/PreviewWithVariant2017.ts
(function previewWithVariants2017() {
mw.hook("ve.saveDialog.stateChanged").add(() => {
processVisualEditor();
});
})();
})();