Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WuiText #12

Open
ronkorving opened this issue Mar 24, 2014 · 0 comments
Open

WuiText #12

ronkorving opened this issue Mar 24, 2014 · 0 comments

Comments

@ronkorving
Copy link

What follows is a suggested draft implementation for WuiText. The idea is that text nodes become wrapped, just like DOM elements do. The wrapper object is very thin, but would allow us to attach logic to all text. An example of this (and implemented below) is to allow text to be a tome, in which case the binding to it (allowing changes to automatically be reflected on-screen without any code required) could be automatic.

WuiDom's setText function would instantiate WuiText out of the content passed to it. At the same time, when people want to instantiate their own text objects, they can do this and use the already existing APIs to deal with text the way they already deal with DOM elements. That is: append, insert, etc. The reason why this would work so easily is because most of those DOM APIs operate on Nodes rather than elements.

WuiText.js:

var handlers;

function WuiText(content) {
    this.rootElement = document.createTextNode('');
    this.set(content);
}

WuiText.addContentHandler = function (fn) {
    if (handlers) {
        handlers.push(fn);
    } else {
        handlers = [fn];
    }
};

WuiText.prototype.set = function (content) {
    if (content === undefined || content === null) {
        content = '';
    }

    if (handlers) {
        for (var i = 0; i < handlers.length; i += 1) {
            content = handlers[i].call(this, content);
        }
    }

    this.rootElement.nodeValue = content;
};

module.exports = WuiText;

Registering a tome text handler:

WuiText.addContentHandler(function (content) {
    var that = this;

    // hot path when change happens on a tome

    if (this.sourceTome === content) {
        return content.toString();
    }

    // slow path when creating the event listener for the first time

    if (Tome.isTome(content)) {
        if (this.sourceTome) {
            // a different tome is being set, so cleanup the previous one

            this.sourceTome.removeListener(this.tomeListener);
        }

        this.sourceTome = content;

        this.tomeListener = function () {
            that.set(this.value);
        };

        content.on('readable', this.tomeListener);

        return content.toString();
    }

    // not a tome

    return content;
});

Final goal:

title.setText('Welcome ', player.name, '!');

// player.name is a tome that might change
// the code above has now created 3 text nodes, the 2nd of which auto-updates with the tome.

Poke @micky2be @bjornstar

@micky2be micky2be changed the title [rfc] WuiText [RFC] WuiText Jul 10, 2014
@micky2be micky2be added the RFC label Apr 10, 2015
@micky2be micky2be changed the title [RFC] WuiText WuiText Apr 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants