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

Is it possible to pre-populate a text field with mentions? #47

Open
cmer opened this issue Jul 13, 2012 · 6 comments
Open

Is it possible to pre-populate a text field with mentions? #47

cmer opened this issue Jul 13, 2012 · 6 comments

Comments

@cmer
Copy link

cmer commented Jul 13, 2012

AFAIK this is not possible out of the box, but how should I proceed if I want to pre-populate a text field that contains mentions. The highlighting and the mentions should be preserved.

@SteelBRS
Copy link

+1

@SteelBRS
Copy link

This is actually an extremely important feature - in order to use the plugin as an editor you need to be able to set the marked up initial text

@LaundroMat
Copy link

Hi,

I'm not much of a JavaScript coder, but I stumbled on this while looking for something similar. I've hacked this together:

        update: function() {
            var messageText = getInputBoxValue();
            // Strip codes
            // add each mention to mentionsCollection
            // And update

            var mentionText = utils.htmlEncode(getInputBoxValue());
            var re = /@\[(.*?)\]\((.*?):(.*?)\)+/g; // Searches through @[value](type:id)

            var match;
            var newMentionText = mentionText;
            while ((match = re.exec(mentionText)) != null) {    // Find all matches in a string
                console.log(match);
                newMentionText = newMentionText.replace(match[0],match[1]);
                mentionsCollection.push({   // Btw: match[0] is the complete match
                    'id': match[3],
                    'type': match[2],
                    'value': match[1]
                });
            }
            elmInputBox.val(newMentionText);
            updateValues();
        },

Put it somewhere in the public methods of the javascript file. In your own code, you should call $('#name_of_your_textarea').mentionsInput("update");

Mind you that:

  • This only works with the standard templates for the mentions data (i.e. @[value](type:id)). If you have defined a seperate template, you should rework the regex. My JS knowledge is too limited to be able to translate the template to a regex
  • I've cobbled this together in about half an hour; don't shoot me if it doesn't work :/

(Both reasons are why I didn't fork and did a pull request: the above is far from tested and isn't ready for production due to the hardcoded template translation).

@SteelBRS
Copy link

Thanks mr. LaundroMat :-)

Also I had to write a different version of getInputBoxValue() - couldn't get jQuery textarea.val() to work:

function getInputBoxValue() {
  return $.trim(elmInputBox.val());
}

function getInputBoxValueRaw() {
  var childNodes = domInput.childNodes;
  if (childNodes.length > 0)
    return $.trim(childNodes[0].nodeValue);
  else
    return "";
}

@SteelBRS
Copy link

Doesn't work in IE, this was what I meant:

function getInputBoxValueRaw() {
  return $.trim(domInput.defaultValue);
}

(Works in IE9, FF14 & CHR21)

therabidbanana added a commit to dtime/jquery-mentions-input that referenced this issue Sep 17, 2012
@lysender
Copy link

This is already possible with "defaultValue" parameter (although ( still have issues with it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants