More actions
Make ES5 compatible |
Don't wait for ready |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'ext.visualEditor.desktopArticleTarget.init', 'ext.cite.visualEditor'], function () { | mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'ext.visualEditor.desktopArticleTarget.init', 'ext.cite.visualEditor'], function () { | ||
// Wait for VisualEditor to be ready | // Wait for VisualEditor to be ready | ||
if (!mw.config.get('wgVisualEditorConfig')) return; | // if (!mw.config.get('wgVisualEditorConfig')) return; | ||
const apiBase = 'https://wingsoffire.wiki/find_paragraph'; | const apiBase = 'https://wingsoffire.wiki/find_paragraph'; | ||
| Line 151: | Line 151: | ||
function interceptCiteBookTemplate() { | function interceptCiteBookTemplate() { | ||
ve | var originalStaticOptions = ve.ui.MWCitationDialog.static.getTemplateOptionsStatic; | ||
ve.ui.MWCitationDialog.static.getTemplateOptionsStatic = function () { | |||
var templates = originalStaticOptions.apply(this, arguments); | |||
var citeBook = templates.find(function (t) { | |||
return t.template && t.template.getName() === 'Cite book'; | |||
}); | |||
if (citeBook && !citeBook._paragraphFinderPatched) { | if (citeBook && !citeBook._paragraphFinderPatched) { | ||
citeBook._paragraphFinderPatched = true; | citeBook._paragraphFinderPatched = true; | ||
var originalAction = citeBook.action; | |||
citeBook.action = () | citeBook.action = function () { | ||
showParagraphFinderDialog((data) | showParagraphFinderDialog(function (data) { | ||
// Call original Cite book dialog | // Call original Cite book action to open dialog | ||
originalAction.call(citeBook); | originalAction.call(citeBook); | ||
// | // Fill in fields once dialog is ready | ||
setTimeout(() | setTimeout(function () { | ||
var dialog = ve.ui.windowFactory.getOpenedWindows()[0].dialog; | |||
dialog.bookWidget.setValue(data.book); | if (dialog && dialog.bookWidget) { | ||
dialog.bookWidget.setValue(data.book); | |||
dialog.chapterWidget.setValue(data.chapter); | |||
dialog.paragraphWidget.setValue(data.paragraph.toString()); | |||
dialog.quoteWidget.setValue(data.quote); | |||
} | |||
}, 500); | }, 500); | ||
}); | }); | ||
| Line 185: | Line 189: | ||
}; | }; | ||
} | } | ||
interceptCiteBookTemplate(); | |||
// Hook into VE once it loads | // Hook into VE once it loads | ||
mw.hook('ve.activationComplete').add(interceptCiteBookTemplate); | // mw.hook('ve.activationComplete').add(interceptCiteBookTemplate); | ||
}); | }); | ||
Latest revision as of 07:17, 22 July 2025
mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'ext.visualEditor.desktopArticleTarget.init', 'ext.cite.visualEditor'], function () {
// Wait for VisualEditor to be ready
// if (!mw.config.get('wgVisualEditorConfig')) return;
const apiBase = 'https://wingsoffire.wiki/find_paragraph';
function showParagraphFinderDialog(onComplete) {
const windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
const dialog = new OO.ui.ProcessDialog({
size: 'medium',
title: 'Find Paragraph Automatically'
});
dialog.getBodyHeight = function () {
return 250;
};
dialog.initialize = function () {
OO.ui.ProcessDialog.prototype.initialize.apply(this, arguments);
const dialogInstance = this;
// UI Elements
this.bookSelect = new OO.ui.DropdownInputWidget({
label: 'Select book',
options: [{ data: '', label: 'Loading…' }],
required: true
});
this.chapterSelect = new OO.ui.DropdownInputWidget({
label: 'Select chapter',
options: [{ data: '', label: 'Choose a book first' }],
required: true
});
this.quoteInput = new OO.ui.MultilineTextInputWidget({
placeholder: 'Paste the full paragraph you want to cite',
autosize: true,
rows: 4
});
this.errorLabel = new OO.ui.LabelWidget({
label: '',
classes: ['error'],
invisibleLabel: true
});
this.panel = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
this.panel.$element.append(
new OO.ui.LabelWidget({ label: 'Book:' }).$element,
this.bookSelect.$element,
new OO.ui.LabelWidget({ label: 'Chapter:' }).$element,
this.chapterSelect.$element,
new OO.ui.LabelWidget({ label: 'Quote:' }).$element,
this.quoteInput.$element,
this.errorLabel.$element
);
this.$body.append(this.panel.$element);
// Load book list
fetch(apiBase + '/books')
.then((r) => r.json())
.then((data) => {
const opts = data.available_books.map((book) => ({
data: book,
label: book.replace(/_/g, ' ')
}));
this.bookSelect.setOptions(opts);
});
// Load chapter list when book changes
this.bookSelect.on('change', (book) => {
this.chapterSelect.setOptions([{ data: '', label: 'Loading…' }]);
fetch(`${apiBase}/books/${encodeURIComponent(book)}/chapters`)
.then((r) => r.json())
.then((data) => {
const opts = data.available_chapters.map((ch) => ({
data: ch,
label: ch
}));
this.chapterSelect.setOptions(opts);
});
});
};
dialog.getActionProcess = function (action) {
if (action === 'find') {
const book = this.bookSelect.getValue();
const chapter = this.chapterSelect.getValue();
const quote = this.quoteInput.getValue();
if (!book || !chapter || !quote.trim()) {
this.errorLabel.setLabel('Please fill in all fields.');
return new OO.ui.Process(() => { });
}
this.errorLabel.setLabel('');
return new OO.ui.Process(() => {
return fetch(apiBase, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
book_name: book,
chapter: chapter,
paragraph: quote
})
})
.then(function (response) {
if (!response.ok) {
return response.json().then(function (err) {
throw new Error(err.reason || 'No match found');
});
}
return response.json();
})
.then(function (result) {
onComplete({
book: book,
chapter: chapter,
paragraph: result.index,
quote: result.match
});
windowManager.closeWindow(dialog);
})
.catch(function (err) {
dialog.errorLabel.setLabel(err.message);
});
});
}
return new OO.ui.Process(() => windowManager.closeWindow(dialog));
};
dialog.getActions = function () {
return [
{ action: 'find', label: 'Find Paragraph', flags: ['primary', 'progressive'] },
{ action: 'cancel', label: 'Cancel', flags: ['safe', 'close'] }
];
};
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);
}
function interceptCiteBookTemplate() {
var originalStaticOptions = ve.ui.MWCitationDialog.static.getTemplateOptionsStatic;
ve.ui.MWCitationDialog.static.getTemplateOptionsStatic = function () {
var templates = originalStaticOptions.apply(this, arguments);
var citeBook = templates.find(function (t) {
return t.template && t.template.getName() === 'Cite book';
});
if (citeBook && !citeBook._paragraphFinderPatched) {
citeBook._paragraphFinderPatched = true;
var originalAction = citeBook.action;
citeBook.action = function () {
showParagraphFinderDialog(function (data) {
// Call original Cite book action to open dialog
originalAction.call(citeBook);
// Fill in fields once dialog is ready
setTimeout(function () {
var dialog = ve.ui.windowFactory.getOpenedWindows()[0].dialog;
if (dialog && dialog.bookWidget) {
dialog.bookWidget.setValue(data.book);
dialog.chapterWidget.setValue(data.chapter);
dialog.paragraphWidget.setValue(data.paragraph.toString());
dialog.quoteWidget.setValue(data.quote);
}
}, 500);
});
};
}
return templates;
};
}
interceptCiteBookTemplate();
// Hook into VE once it loads
// mw.hook('ve.activationComplete').add(interceptCiteBookTemplate);
});