Giter Site home page Giter Site logo

pbrooker / jquery-mentions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ivirabyan/jquery-mentions

0.0 2.0 0.0 163 KB

Adds mentioning support to your text fields.

Home Page: http://ivirabyan.github.io/jquery-mentions/

License: Other

HTML 6.33% CoffeeScript 90.66% CSS 3.02%

jquery-mentions's Introduction

jquery-mentions

Adds mentioning support to your text fields.

It's a more robust alternative to podio's jquery.mentionsInput plugin.

Live example: http://ivirabyan.github.io/jquery-mentions/

Advantages over jquery.mentionsInput:

  • Many issues are solved
  • Input styles are copied to highlighter automatically
  • Support for both textarea and input tags
  • Support for contenteditable (and as a result support for some WYSIWYG editors)
  • Hidden-input with markuped text created automatically
  • Uses jQuery UI autocomplete

Solved issues of jquery.mentionsInput:

  • Adding spaces before mention shows overlay
  • Scrolling to bottom inside textarea moves text, but not highlights
  • Typing email adress activates mentions
  • Inserting same mentions multiple times
  • Prepopulate input with text

Usage

For this plugin to work you need to include jQuery Autocomplete to your page.

var data = [
    {value: 'alex', uid: 'user:1'},
    {value: 'andrew', uid: 'user:2'},
    {value: 'angry birds', uid: 'game:5'},
    {value: 'assault', uid: 'game:3'}
];

$('textarea').mentionsInput({source: data});

Remote datasource (ajax):

$('textarea').mentionsInput({source: 'http://example.com/users.json'})

The url is given a query paremeter term, like http://example.com/users.json?term=Foo and must return a json list of matched values (like the above).

Calling a method:

$('textarea').mentionsInput('getValue');
$('textarea').mentionsInput('setValue', 'Hello, @[Alex](user:1)');

Getting value: $('textarea').mentionsInput('getValue') -> Hello, @[Alex](user:1) $('textarea').mentionsInput('getRawValue') -> Hello, Alex

Don't use textarea value directly, because it contains special characters, used by plugin internally. Always use methods.

WYSIWYG editors

To create WYSIWYG editor on your site, usually you create <textarea> tag, and then your editor replaces it with editor's visual representation, including element with contenteditable="true" attribute. So, to make mentionsInput plugin work, you need to apply the plugin to element with contenteditable="true". If you apply the plugin to your <textarea>, it'll not work. For example:

    <textarea id="content"></textarea>
    <script>
        $('#content').myEditor();
        // Now your editor is initialized, find element with contenteditable.
        // For particular plugin you may find a better way to get such an element,
        // maybe even write your own plugin.
        var elEditable = $('[contenteditable=true]');
        elEditable.mentionsInput({...});
    </script>

Options

source

Data source for the autocomplete. See jQuery Autocomplete API for available values.

Source data is an array of objects with uid and value properties: [{uid: '123', value: 'Alex'}, ...]. If you want to display an icon in dropdown list, you can add an image property to objects in the array.

suffix

String to add to selected mention when it is inserted in text. Can be usefull if you wish to automatically insert a space after mention. For that case: $textarea.mentionsInput({suffix: ' '}) Note: only supported for textarea and input. Contenteditable does not support this option yet.

trigger

Char which trigger autocomplete, default value is '@'

widget

Name of the autocomplete widget to use. May be useful when you want to somehow customize appearance of autocomplete widget, for example add headers to items list. You must inherit from widget, used internally (ui.areacomplete when you use textarea, and ui.editablecomplete when you use div with contenteditable=true).

autocomplete

Options to pass to jQuery Autocomplete widget. Default is {delay: 0, autoFocus: true}.

Methods

getValue()

Returns marked up value.

getRawValue()

Returns value without any markup

setValue(value)

Takes marked up value as an argument. For example 'Hey, @[alex](user:1)'. You can also represent mentions as objects, instead of manually marking them up: $textarea.mentionsInput('setValue', 'Hey, ', {name: 'alex', uid: 'user:1'})

getMentions()

Returns an array of all mentions contained within the text, like this:

[
  {name: 'alex', uid: 'user:1'},
  {name: 'andrew', uid: 'user:2'}
]

clear()

Clears value. Note that you must use this method insted of manually clearing value of the input

destroy()

Destroys current instance of the plugin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.