Change Continue Macro to only fire on space

Twine Version: 2.3.13
Story Format: sugarcube 2.34.1

Hello,

wanted to ask if it is possible to change the Continue.js from ChapelR to accept for example only the space key? Messed around with the code for a day and got nowhere. (still suck at js. Know how to check if a key was pressed, but i don’t know how to pass this check into the continue function) ChapelRs Code can be found here. https://github.com/ChapelR/custom-macros-for-sugarcube-2/blob/master/scripts/continue.js

Here’s an updated version. Use the new space keyword, e.g. <<cont space>>...

(function () {
    // v1.0.1
    'use strict';

    // selectors to ignore
    var ignored = ['a', ':button', '*[role="button"]', '.continue-macro-ignore', '#ui-bar', '#ui-dialog'];

    var _i = 0;

    prehistory['%%continue-expiration'] = function () {
        _i = 0;
    };

    function ns () {
        // create unique namespace
        var namespace = '.' + Date.now().toString(36) + '-' + _i;
        _i++;
        return namespace;
    }

    function ignoreMe () {
        $(document).on('click.continue-macro keyup.continue-macro', ignored.join(', '), function (ev) { 
            ev.stopPropagation(); 
        });
    }

    function addIgnore () {
        if (State.length > 0) {
            return false;
        }
        var args = [].slice.call(arguments).flatten();
        ignored = ignored.concat(args);
        return true;
    }

    $(document).one(':passagerender', function () {
        // on first render, set up the ignore list
        ignoreMe();
    });

    // continue functions
    function cont (press, space, cb) {
        var namespace = ns();
        if (!cb || typeof cb !== 'function') {
            return;
        }
        var events = 'click.continue-macro' + namespace;
        if (press || space) {
            events = events + ' keyup.continue-macro' + namespace;
        }
        $(document).one(events, function (ev) {
            if (space && ev.type.includes('key') && ev.which !== 32) {
                cont(true, true, cb);
            } else {
                cb.call();
            }
            // expire all namespaced events
            $(document).off(namespace);
        });
    }

    function reset () {
        var args = [].slice.call(arguments).flatten();
        ignored = ignored.concat(args);
        $(document).off('.continue-macro');
        ignoreMe();
    }

    // macros

    // <<ignore selectors...>>
    Macro.add('ignore', {
        handler : function () {
            var check = addIgnore(this.args);
            if (!check) {
                return this.error('the <<ignore>> macro should only be run from StoryInit or equivalent.');
            }
        }
    });

    // <<cont [append] [press]>>Code<</cont>>
    Macro.add('cont', {
        tags : null,
        handler : function () {
            var append = this.args.includes('append'), // append keyword
                press = this.args.includesAny('key', 'keypress', 'press', 'button'), // keypress keyword
                space = this.args.includes('space'),
                wiki = this.payload[0].contents, // content to wikify
                $output; // output element (if needed)

            if (append) {
                // create output element, but only if needed (e.g. if appending content)
                $output = $(document.createElement('span'))
                    .addClass('macro-' + this.name)
                    .appendTo(this.output);
            }

            cont(press, space, this.createShadowWrapper( function () {
                // wikify 
                if (append && $output && $output instanceof $) {
                    $output.wiki(wiki);
                } else {
                    $.wiki(wiki);
                }
            }));
        }
    });

    // APIs

    setup.cont = cont;
    setup.cont.ignore = addIgnore;
    setup.cont.reset = reset;

    window.cont = window.cont || setup.cont;

}());
1 Like

Wow! This was fast. I somehow get it now that i see it, even though the whole code is still some kind of magic to me.

Works great. Thanks a lot!

1 Like