Commit b80d488274b40b56a3d6fc8eba453a3c1fa0ac29
readme + improved channels
mix irving committed on 9/20/2017, 7:02:42 AMParent: 43f3765c702a25773717997c0eb377f228deca32
Files changed
channel/async/suggest.js | changed |
emoji/async/suggest.js | changed |
styles/mcss.js | changed |
README.md | added |
channel/async/suggest.js | ||
---|---|---|
@@ -20,14 +20,20 @@ | ||
20 | 20 … | function suggestedChannels () { |
21 | 21 … | loadSuggestions() |
22 | 22 … | return function (word) { |
23 | 23 … | if (!word) { |
24 | - return suggestions().slice(0, 100) | |
24 … | + return suggestions() | |
25 … | + .sort(() => Math.random() > 0.5 ? +1 : -1) //shuffle | |
26 … | + .slice(0, 100) | |
25 | 27 … | } |
26 | 28 … | |
27 | 29 … | word = word.toLowerCase() |
28 | 30 … | return suggestions() |
29 | 31 … | .filter(item => ~item.title.toLowerCase().indexOf(word)) |
32 … | + .sort((a, b) => { | |
33 … | + // if word fragment occurs earlier in name, bump up | |
34 … | + return a.title.indexOf(word) < b.title.indexOf(word) ? -1 : +1 | |
35 … | + }) | |
30 | 36 … | } |
31 | 37 … | } |
32 | 38 … | |
33 | 39 … | function loadSuggestions () { |
emoji/async/suggest.js | ||
---|---|---|
@@ -12,13 +12,13 @@ | ||
12 | 12 … | var suggestions = null |
13 | 13 … | var subscribed = null |
14 | 14 … | |
15 | 15 … | return nest('emoji.async.suggest', suggestedEmoji) |
16 | - | |
17 | - function suggestedEmoji (word) { | |
16 … | + | |
17 … | + function suggestedEmoji () { | |
18 | 18 … | return function (word) { |
19 | 19 … | // when no emoji typed, list some default ones |
20 | - if (word.length === 0) { | |
20 … | + if (!word) { | |
21 | 21 … | return api.emoji.sync.names() |
22 | 22 … | .sort(() => Math.random() > 0.5 ? +1 : -1) //shuffle |
23 | 23 … | .slice(0, 10) |
24 | 24 … | .map(toSuggestion) |
styles/mcss.js | ||
---|---|---|
@@ -2,115 +2,113 @@ | ||
2 | 2 … | |
3 | 3 … | exports.gives = nest('styles.mcss') |
4 | 4 … | |
5 | 5 … | const suggestBox = ` |
6 | -body { | |
7 | - div.suggest-box { | |
8 | - overflow-y: auto | |
9 | - background-color: #fff | |
6 … | +PatchSuggest { | |
7 … | + overflow-y: auto | |
8 … | + background-color: #fff | |
10 | 9 … | |
11 | - width: max-content | |
12 | - min-width: 20rem | |
13 | - max-width: 35rem | |
10 … | + width: max-content | |
11 … | + min-width: 20rem | |
12 … | + max-width: 35rem | |
14 | 13 … | |
15 | - max-height: 70vh | |
14 … | + max-height: 70vh | |
16 | 15 … | |
17 | - padding: .2rem .5rem | |
18 | - border: 1px gainsboro solid | |
19 | - margin-top: .35rem | |
16 … | + padding: .2rem .5rem | |
17 … | + border: 1px gainsboro solid | |
18 … | + margin-top: .35rem | |
20 | 19 … | |
21 | - ul { | |
22 | - list-style-type: none | |
23 | - padding: 0 | |
20 … | + ul { | |
21 … | + list-style-type: none | |
22 … | + padding: 0 | |
24 | 23 … | |
25 | - li { | |
26 | - display: flex | |
27 | - align-items: center | |
24 … | + li { | |
25 … | + display: flex | |
26 … | + align-items: center | |
28 | 27 … | |
29 | - padding-right: .2rem | |
30 | - margin-bottom: .2rem | |
28 … | + padding-right: .2rem | |
29 … | + margin-bottom: .2rem | |
31 | 30 … | |
32 | - img { | |
33 | - height: 36px | |
34 | - width: 36px | |
35 | - min-width: 36px | |
36 | - padding: .2rem | |
37 | - } | |
31 … | + img { | |
32 … | + height: 36px | |
33 … | + width: 36px | |
34 … | + min-width: 36px | |
35 … | + padding: .2rem | |
36 … | + } | |
38 | 37 … | |
39 | - strong { | |
40 | - font-weight: 300 | |
41 | - min-width: 7rem | |
42 | - margin-left: .5rem | |
43 | - margin-right: .5rem | |
38 … | + strong { | |
39 … | + font-weight: 300 | |
40 … | + min-width: 7rem | |
41 … | + margin-left: .5rem | |
42 … | + margin-right: .5rem | |
44 | 43 … | |
45 | - span.subtle { | |
46 | - color: #aaa | |
47 | - } | |
44 … | + span.subtle { | |
45 … | + color: #aaa | |
48 | 46 … | } |
47 … | + } | |
49 | 48 … | |
50 | - small { | |
51 | - flex-grow: 1 | |
49 … | + small { | |
50 … | + flex-grow: 1 | |
52 | 51 … | |
53 | - margin-left: .5rem | |
54 | - padding-right: .2rem | |
55 | - font-size: 1rem | |
52 … | + margin-left: .5rem | |
53 … | + padding-right: .2rem | |
54 … | + font-size: 1rem | |
56 | 55 … | |
57 | - display: flex | |
58 | - justify-content: flex-end | |
56 … | + display: flex | |
57 … | + justify-content: flex-end | |
59 | 58 … | |
60 | - // profile only | |
61 | - div.aliases { | |
62 | - flex-grow: 1 | |
59 … | + // profile only | |
60 … | + div.aliases { | |
61 … | + flex-grow: 1 | |
63 | 62 … | |
64 | - font-size: .8rem | |
65 | - color: #666 | |
66 | - margin-right: .5rem | |
63 … | + font-size: .8rem | |
64 … | + color: #666 | |
65 … | + margin-right: .5rem | |
67 | 66 … | |
68 | - display: flex | |
69 | - flex-wrap: wrap | |
67 … | + display: flex | |
68 … | + flex-wrap: wrap | |
70 | 69 … | |
71 | - div.alias { | |
72 | - margin-right: .4rem | |
73 | - -bold { | |
74 | - font-weight: 600 | |
75 | - } | |
70 … | + div.alias { | |
71 … | + margin-right: .4rem | |
72 … | + -bold { | |
73 … | + font-weight: 600 | |
76 | 74 … | } |
77 | 75 … | } |
76 … | + } | |
78 | 77 … | |
79 | - // profile only | |
80 | - div.key { | |
81 | - align-self: flex-end | |
78 … | + // profile only | |
79 … | + div.key { | |
80 … | + align-self: flex-end | |
82 | 81 … | |
83 | - margin: auto 0 | |
82 … | + margin: auto 0 | |
84 | 83 … | |
85 | - font-family: monospace | |
86 | - font-size: .8rem | |
87 | - min-width: 5rem | |
88 | - } | |
84 … | + font-family: monospace | |
85 … | + font-size: .8rem | |
86 … | + min-width: 5rem | |
87 … | + } | |
89 | 88 … | |
90 | - // emoji only | |
91 | - div.emoji { | |
92 | - flex-grow: 1 | |
93 | - } | |
89 … | + // emoji only | |
90 … | + div.emoji { | |
91 … | + flex-grow: 1 | |
92 … | + } | |
94 | 93 … | |
95 | - } | |
96 | 94 … | } |
95 … | + } | |
97 | 96 … | |
98 | - li.selected { | |
99 | - color: #fff | |
100 | - background: #0caaf9 | |
97 … | + li.selected { | |
98 … | + color: #fff | |
99 … | + background: #0caaf9 | |
101 | 100 … | |
102 | - img {} | |
103 | - strong {} | |
104 | - small { | |
105 | - div.aliases { | |
106 | - color: #eee | |
107 | - } | |
101 … | + img {} | |
102 … | + strong {} | |
103 … | + small { | |
104 … | + div.aliases { | |
105 … | + color: #eee | |
108 | 106 … | } |
109 | 107 … | } |
110 | 108 … | } |
111 | - } | |
112 | -} | |
109 … | + } | |
110 … | +} | |
113 | 111 … | ` |
114 | 112 … | |
115 | 113 … | exports.create = (api) => { |
116 | 114 … | return nest('styles.mcss', mcss) |
README.md | ||
---|---|---|
@@ -1,0 +1,58 @@ | ||
1 … | +# Patch-suggest | |
2 … | + | |
3 … | +A module for easy suggest-mentions of people / channels / emoji in patch-* family apps. | |
4 … | + | |
5 … | +Provides suggestions and styles in a format consumed by [`suggest-box`](https://github.com/pfrazee/suggest-box) | |
6 … | + | |
7 … | +You'll need to understand [depject](https://github.com/depject/depject) (a module for a different way of managing dependency injection), and for the example below, [depnest](https://github.com/depject/depnest) - a lazy way to write nested objects quickly. | |
8 … | + | |
9 … | + | |
10 … | +## Example | |
11 … | + | |
12 … | + | |
13 … | +```js | |
14 … | +const nest = require('depnest') | |
15 … | +const { h } = require('mutant') | |
16 … | +const suggestBox = require('suggest-box') | |
17 … | + | |
18 … | +exports.gives = nest('app.page.somePage') | |
19 … | + | |
20 … | +exports.needs = nest({ | |
21 … | + 'about.async.suggest': 'first', | |
22 … | + 'channel.async.suggest': 'first', | |
23 … | + 'emoji.async.suggest': 'first', // NOTE emoji suggestion is synchronous | |
24 … | +}) | |
25 … | + | |
26 … | +exports.create = (api) => { | |
27 … | + return nest('app.page.somePage', page) | |
28 … | + | |
29 … | + function page (location) { | |
30 … | + | |
31 … | + var textArea = h('textarea') | |
32 … | + | |
33 … | + var getProfileSuggestions = api.about.async.suggest() | |
34 … | + var getChannelSuggestions = api.channel.async.suggest() | |
35 … | + var getEmojiSuggestions = api.emoji.async.suggest() | |
36 … | + | |
37 … | + suggestBox( | |
38 … | + textArea, | |
39 … | + (inputText, cb) => { | |
40 … | + const char = inputText[0] | |
41 … | + const wordFragment = inputText.slice(1) | |
42 … | + | |
43 … | + if (char === '@') cb(null, getProfileSuggestions(wordFragment)) | |
44 … | + if (char === '#') cb(null, getChannelSuggestions(wordFragment)) | |
45 … | + if (char === ':') cb(null, getEmojiSuggestions(wordFragment)) | |
46 … | + }, | |
47 … | + {cls: 'PatchSuggest'} | |
48 … | + ) | |
49 … | + | |
50 … | + //... | |
51 … | + } | |
52 … | +} | |
53 … | +``` | |
54 … | + | |
55 … | +## Styling | |
56 … | + | |
57 … | +To use the styles provided by patch-suggest (recommended), supply the options argument to suggest-box `{cls: 'PatchSuggest}` (see example above) | |
58 … | + |
Built with git-ssb-web