Jump to content

MediaWiki:Gadget-TciTest.js

Find traditional instrumental music
Revision as of 10:55, 11 April 2025 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// 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);
Cookies help us deliver our services. By using The Traditional Tune Archive services, you agree to our use of cookies.