User:Diegodlh/Web2Cit/script.js

User:Diegodlh/Web2Cit/script.js

Update

← Previous revision Revision as of 17:53, 20 April 2026
Line 1: Line 1:
(function () {
(function () {
// https://gitlab.wikimedia.org/diegodlh/w2c-gadget/-/blob/c13176ed484a5bcaff437f669063de76a5744365/script.js
// https://gitlab.wikimedia.org/diegodlh/w2c-gadget/-/blob/38e2203f55ba3256426ba44f5fbb5f8a2043faa9/script.js
// Copyright (C) 2022 Diego de la Hera and contributors.
// Copyright (C) 2022 Diego de la Hera and contributors.
// This work is released under the terms of GPL-3.0 or any later version.
// This work is released under the terms of GPL-3.0 or any later version.
Line 21: Line 21:
}
}
this.search
this.search
this.added = false; // whether w2c citations added since last saving attempt
}
}


Line 56: Line 55:
// this.enable();
// this.enable();
// }
// }
const onSaveWorkflowChangePanel = (saveDialog) => {
if (this.added) {
let editSummary = saveDialog.editSummaryInput.getValue();
if (!editSummary.includes("#Web2Cit")) {
if (!editSummary.trim()) { // empty edit summary
editSummary = ve.msg("citoid-citoiddialog-title"); // "Add a citation"
}
// we need a space before #, otherwise Hashtags tool won't pick it (T262234)
editSummary = editSummary.trimEnd() + " [[meta:Web2Cit| #Web2Cit]]";
saveDialog.editSummaryInput.setValue(editSummary);
}
}
// reset added flag so that we don't update the summary again in case the
// user decided to manually remove our suggestion, unless a new citation
// is added again
this.added = false;
};
// see https://www.mediawiki.org/wiki/VisualEditor/Hooks
mw.hook("ve.newTarget").add((target) => {
// ve.saveDialog.stateChanged
target.on("saveWorkflowChangePanel", () => {
onSaveWorkflowChangePanel(target.saveDialog)
});
});
}
}


Line 191: Line 166:
if (web2cit) {
if (web2cit) {
// console.log("Web2Cit: Web2Cit citation inserted");
// console.log("Web2Cit: Web2Cit citation inserted");
this.added = true;
updateEditSummary();
// console.log('{ action: "insert" }');
// console.log('{ action: "insert" }');
mw.track(TRACK_TOPIC, 1, { action: "insert" });
mw.track(TRACK_TOPIC, 1, { action: "insert" });
Line 327: Line 302:
ve.ui.CitoidInspector.prototype.performLookup = this.unpatched.performLookup;
ve.ui.CitoidInspector.prototype.performLookup = this.unpatched.performLookup;
delete this.unpatched.performLookup;
delete this.unpatched.performLookup;
}
}

function updateEditSummary() {
const hashtag = "#Web2Cit";
const saveDialog = ve.init.target.saveDialog;
const initialEditSummary = (
ve.init.target.editSummaryValue || ve.init.target.initialEditSummary
);
let editSummaryValue = (
saveDialog ?
saveDialog.editSummaryInput.getValue() :
initialEditSummary
) || "";
if (!editSummaryValue.includes(hashtag)) {
const prefix = ve.msg("citoid-citoiddialog-title"); // "Add a citation"
if (!editSummaryValue.trim()) { // empty edit summary
editSummaryValue = prefix;
} else if (editSummaryValue == initialEditSummary) {
editSummaryValue += prefix;
}
// we need a space before hashtag, otherwise Hashtags tool won't pick it (T262234)
editSummaryValue = (
editSummaryValue.trimEnd() + ` [[meta:Web2Cit| ${hashtag}]]`
);

if (saveDialog) {
saveDialog.setEditSummary(editSummaryValue);
} else {
ve.init.target.editSummaryValue = editSummaryValue;
}
}
}
}
}