git ssb

0+

mixmix / patch-suggest



Tree:
📄.gitignore
📄README.md
📁about
📁channel
📁emoji
📄index.js
📄package.json
📁styles
README.md

Patch-suggest

A module for easy suggest-mentions of people / channels / emoji in patch-* family apps.

Provides suggestions and styles in a format consumed by suggest-box

You'll need to understand depject (a module for a different way of managing dependency injection), and for the example below, depnest - a lazy way to write nested objects quickly.

Example

const nest = require('depnest')
const { h } = require('mutant')
const suggestBox = require('suggest-box')

exports.gives = nest('app.page.somePage')

exports.needs = nest({
  'about.async.suggest': 'first',
  'channel.async.suggest': 'first',
  'emoji.async.suggest': 'first',  // NOTE emoji suggestion is synchronous
})

exports.create = (api) => {
  return nest('app.page.somePage', page)

  function page (location) {

    var textArea = h('textarea')

    var getUserSuggestions = api.about.async.suggest()
    var getChannelSuggestions = api.channel.async.suggest()
    var getEmojiSuggestions = api.emoji.async.suggest()

    suggestBox(
      textArea,
      (inputText, cb) => {
        const char = inputText[0]
        const wordFragment = inputText.slice(1)

        if (char === '@') cb(null, getUserSuggestions(wordFragment))
        if (char === '#') cb(null, getChannelSuggestions(wordFragment))
        if (char === ':') cb(null, getEmojiSuggestions(wordFragment))
      },
      {cls: 'PatchSuggest'}
    )

    //...
  }
}

API

Each of these depject methods is called to initialize it (starts pre-loading data in the pbackground) and returns a "suggester". The suggester functions return "suggestion" objects which are compatible with the suggest-box module, but you can use them for whatever!

api.about.async.suggest() => suggester(word, extraIds)

suggester returns suggestions for users based on your followers, following, and people who've recently said things.

extraIds (optional) you can add an Array (or MutantArray) of FeedIds which you would like included in the suggestions. This is a great way to add the context (i.e. the people in the conversation) of a the current page you're in to the suggestions.

api.channel.async.suggest() => suggester(word)

suggester returns suggestions for channels

api.emoji.async.suggest() => suggester(word)

suggester returns suggestions for emojis

Styling

To use the styles provided by patch-suggest (recommended), supply the options argument to suggest-box {cls: 'PatchSuggest} (see example above)

Built with git-ssb-web