MediaWiki:Gadget-TciTest.js: Difference between revisions
Appearance
No edit summary Tag: Manual revert |
No edit summary |
||
Line 1: | Line 1: | ||
// Gadget-TciTest.js β | // Gadget-TciTest.js β DEBUG REGEXP MATCHES | ||
(function ($, mw, OO) { | (function ($, mw, OO) { | ||
"use strict"; | "use strict"; | ||
mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function () { | mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function () { | ||
function | function TCICalculatorDialog(config) { | ||
TCICalculatorDialog.super.call(this, config); | |||
} | } | ||
OO.inheritClass( | OO.inheritClass(TCICalculatorDialog, OO.ui.ProcessDialog); | ||
TCICalculatorDialog.static.name = 'tciCalculatorDialog'; | |||
TCICalculatorDialog.static.title = 'TCI Calculator (Test Mode)'; | |||
TCICalculatorDialog.static.actions = [ | |||
{ action: 'cancel', label: 'Cancel', flags: 'safe' }, | { action: 'cancel', label: 'Cancel', flags: 'safe' }, | ||
{ action: 'calculate', label: 'Calculate TCI', flags: ['primary', 'progressive'] } | { action: 'calculate', label: 'Calculate TCI', flags: ['primary', 'progressive'] } | ||
]; | ]; | ||
const | const tokenRegex = /((=|\^|_)?[a-gA-GzZ][',/]*)(\d*\/?\d*)/g; | ||
function parseABC(abcText) { | |||
console.log("π₯ ABC from textarea:\n", abcText); | |||
const matches = [...abcText.matchAll(tokenRegex)].map(m => m[0]); | |||
console.log("π Raw matches from ABC:", matches); | |||
const lines = abcText.split(/\r?\n/); | const lines = abcText.split(/\r?\n/); | ||
const keyLine = lines.find(line => line.startsWith('K:')) || 'K:D'; | const keyLine = lines.find(line => line.startsWith('K:')) || 'K:D'; | ||
const lengthLine = lines.find(line => line.startsWith('L:')) || 'L:1/8'; | const lengthLine = lines.find(line => line.startsWith('L:')) || 'L:1/8'; | ||
const [lNum, lDen] = lengthLine.split(':')[1].split('/').map(Number); | const [lNum, lDen] = lengthLine.split(':')[1].split('/').map(Number); | ||
const beatDuration = lNum / lDen; | const beatDuration = lNum / lDen; | ||
const tokenRegex = /((=|\^|_)?[a-gA-GzZ][',/]*)(\d*\/?\d*)/g; | const tokenRegex = /((=|\^|_)?[a-gA-GzZ][',/]*)(\d*\/?\d*)/g; | ||
let match; | let match; | ||
let | let beat = 0; | ||
let result = []; | |||
while ((match = tokenRegex.exec(abcText)) !== null && beat < 8) { | |||
while ((match = tokenRegex.exec(abcText)) !== null && | |||
const [full, note, , durStr] = match; | const [full, note, , durStr] = match; | ||
const plain = note.replace(/\d.*$/, ''); | const plain = note.replace(/\d.*$/, ''); | ||
const dur = durStr ? (durStr.includes('/') ? eval(durStr) : parseFloat(durStr)) : beatDuration; | |||
console.log(`NOTE: ${plain}, duration: ${dur}`); | console.log(`NOTE: ${plain}, duration: ${dur}`); | ||
result.push(`${plain}`); | |||
beat++; | |||
} | } | ||
return | return result; | ||
} | } | ||
TCICalculatorDialog.prototype.initialize = function () { | |||
TCICalculatorDialog.super.prototype.initialize.call(this); | |||
this.content = new OO.ui.PanelLayout({ padded: true, expanded: | |||
this.content = new OO.ui.PanelLayout({ padded: true, expanded: true, scrollable: true }); | |||
this.$body.append(this.content.$element); | this.$body.append(this.content.$element); | ||
this.abcInput = new OO.ui.MultilineTextInputWidget({ placeholder: 'Paste ABC notation here...', autosize: true, rows: 10 | this.abcInput = new OO.ui.MultilineTextInputWidget({ | ||
placeholder: 'Paste ABC notation here...', | |||
autosize: true, | |||
rows: 10 | |||
}); | }); | ||
Line 151: | Line 62: | ||
this.content.$element.append( | this.content.$element.append( | ||
new OO.ui.FieldsetLayout({ | new OO.ui.FieldsetLayout({ | ||
label: 'TCI Calculator | label: 'TCI Test Calculator', | ||
items: [ | items: [ | ||
new OO.ui.FieldLayout(this.abcInput, { label: 'ABC Notation', align: 'top' }), | new OO.ui.FieldLayout(this.abcInput, { label: 'ABC Notation', align: 'top' }), | ||
new OO.ui.FieldLayout(this.resultOutput, { label: 'Extracted Notes', align: 'top' }) | |||
new OO.ui.FieldLayout(this.resultOutput, { label: ' | |||
] | ] | ||
}).$element | }).$element | ||
Line 161: | Line 71: | ||
}; | }; | ||
TCICalculatorDialog.prototype.getActionProcess = function (action) { | |||
if (action === 'calculate') { | if (action === 'calculate') { | ||
return new OO.ui.Process(() => { | return new OO.ui.Process(() => { | ||
const abc = this.abcInput.getValue(); | const abc = this.abcInput.getValue(); | ||
const | const parsed = parseABC(abc); | ||
this.resultOutput.setValue(parsed.join(' ')); | |||
this.resultOutput.setValue( | |||
}); | }); | ||
} else if (action === 'cancel') { | } else if (action === 'cancel') { | ||
return new OO.ui.Process(() => this.close({ action: action })); | return new OO.ui.Process(() => this.close({ action: action })); | ||
} | } | ||
return | return TCICalculatorDialog.super.prototype.getActionProcess.call(this, action); | ||
}; | }; | ||
function | function openTCICalculatorDialog() { | ||
const windowManager = new OO.ui.WindowManager(); | const windowManager = new OO.ui.WindowManager(); | ||
$(document.body).append(windowManager.$element); | $(document.body).append(windowManager.$element); | ||
const dialog = new | const dialog = new TCICalculatorDialog(); | ||
windowManager.addWindows([dialog]); | windowManager.addWindows([dialog]); | ||
windowManager.openWindow(dialog); | windowManager.openWindow(dialog); | ||
} | } | ||
mw.util.addPortletLink('p-cactions', '#', 'πΌ TCI Test', 'ca-tcitest', 'Open TCI test'); | mw.util.addPortletLink('p-cactions', '#', 'πΌ TCI (Test Mode)', 'ca-tcitest', 'Open TCI test dialog'); | ||
$(document).on('click', '#ca-tcitest', function (e) { | $(document).on('click', '#ca-tcitest', function (e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
openTCICalculatorDialog(); | |||
}); | }); | ||
}); | }); | ||
})(jQuery, mediaWiki, OO); | })(jQuery, mediaWiki, OO); |
Latest revision as of 10:55, 11 April 2025
// Gadget-TciTest.js β DEBUG REGEXP MATCHES
(function ($, mw, OO) {
"use strict";
mw.loader.using(["oojs-ui-core", "oojs-ui-widgets", "oojs-ui-windows"]).done(function () {
function TCICalculatorDialog(config) {
TCICalculatorDialog.super.call(this, config);
}
OO.inheritClass(TCICalculatorDialog, OO.ui.ProcessDialog);
TCICalculatorDialog.static.name = 'tciCalculatorDialog';
TCICalculatorDialog.static.title = 'TCI Calculator (Test Mode)';
TCICalculatorDialog.static.actions = [
{ action: 'cancel', label: 'Cancel', flags: 'safe' },
{ action: 'calculate', label: 'Calculate TCI', flags: ['primary', 'progressive'] }
];
const tokenRegex = /((=|\^|_)?[a-gA-GzZ][',/]*)(\d*\/?\d*)/g;
function parseABC(abcText) {
console.log("π₯ ABC from textarea:\n", abcText);
const matches = [...abcText.matchAll(tokenRegex)].map(m => m[0]);
console.log("π Raw matches from ABC:", matches);
const lines = abcText.split(/\r?\n/);
const keyLine = lines.find(line => line.startsWith('K:')) || 'K:D';
const lengthLine = lines.find(line => line.startsWith('L:')) || 'L:1/8';
const [lNum, lDen] = lengthLine.split(':')[1].split('/').map(Number);
const beatDuration = lNum / lDen;
const tokenRegex = /((=|\^|_)?[a-gA-GzZ][',/]*)(\d*\/?\d*)/g;
let match;
let beat = 0;
let result = [];
while ((match = tokenRegex.exec(abcText)) !== null && beat < 8) {
const [full, note, , durStr] = match;
const plain = note.replace(/\d.*$/, '');
const dur = durStr ? (durStr.includes('/') ? eval(durStr) : parseFloat(durStr)) : beatDuration;
console.log(`NOTE: ${plain}, duration: ${dur}`);
result.push(`${plain}`);
beat++;
}
return result;
}
TCICalculatorDialog.prototype.initialize = function () {
TCICalculatorDialog.super.prototype.initialize.call(this);
this.content = new OO.ui.PanelLayout({ padded: true, expanded: true, scrollable: true });
this.$body.append(this.content.$element);
this.abcInput = new OO.ui.MultilineTextInputWidget({
placeholder: 'Paste ABC notation here...',
autosize: true,
rows: 10
});
this.resultOutput = new OO.ui.MultilineTextInputWidget({ readOnly: true, autosize: true, rows: 4 });
this.content.$element.append(
new OO.ui.FieldsetLayout({
label: 'TCI Test Calculator',
items: [
new OO.ui.FieldLayout(this.abcInput, { label: 'ABC Notation', align: 'top' }),
new OO.ui.FieldLayout(this.resultOutput, { label: 'Extracted Notes', align: 'top' })
]
}).$element
);
};
TCICalculatorDialog.prototype.getActionProcess = function (action) {
if (action === 'calculate') {
return new OO.ui.Process(() => {
const abc = this.abcInput.getValue();
const parsed = parseABC(abc);
this.resultOutput.setValue(parsed.join(' '));
});
} else if (action === 'cancel') {
return new OO.ui.Process(() => this.close({ action: action }));
}
return TCICalculatorDialog.super.prototype.getActionProcess.call(this, action);
};
function openTCICalculatorDialog() {
const windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
const dialog = new TCICalculatorDialog();
windowManager.addWindows([dialog]);
windowManager.openWindow(dialog);
}
mw.util.addPortletLink('p-cactions', '#', 'πΌ TCI (Test Mode)', 'ca-tcitest', 'Open TCI test dialog');
$(document).on('click', '#ca-tcitest', function (e) {
e.preventDefault();
openTCICalculatorDialog();
});
});
})(jQuery, mediaWiki, OO);