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/ |
// 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 (!editSummary.includes("#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"); |
||
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 ? |
|||
| ⚫ | |||
initialEditSummary |
|||
| ⚫ | |||
if (!editSummaryValue.includes(hashtag)) { |
|||
| ⚫ | |||
| ⚫ | |||
editSummaryValue = prefix; |
|||
} else if (editSummaryValue == initialEditSummary) { |
|||
editSummaryValue += prefix; |
|||
| ⚫ | |||
| ⚫ | |||
editSummaryValue = ( |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
saveDialog.setEditSummary(editSummaryValue); |
|||
} else { |
|||
ve.init.target.editSummaryValue = editSummaryValue; |
|||
| ⚫ | |||
} |
} |
||
} |
} |
||