Add code to extract hashtags from changeset comment

This commit is contained in:
Bryan Housel
2017-08-16 00:30:34 -04:00
parent df16568e1c
commit 4cf357f61a
2 changed files with 39 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ import { t } from '../util/locale';
import { svgIcon } from '../svg';
import { uiField } from './field';
import { uiFormFields } from './form_fields';
import { utilRebind } from '../util';
import { utilRebind, utilTriggerEvent } from '../util';
export function uiChangesetEditor(context) {
@@ -62,6 +62,10 @@ export function uiChangesetEditor(context) {
commentNode.select();
}
// trigger a 'blur' event so that comment field can be cleaned
// and checked for hashtags, even if retrieved from localstorage
utilTriggerEvent(commentField, 'blur');
var osm = context.connection();
if (osm) {
osm.userChangesets(function (err, changesets) {
@@ -84,10 +88,10 @@ export function uiChangesetEditor(context) {
}
}
// Add comment warning
var matches = tags.comment.match(/google/i);
// Add warning if comment mentions Google
var hasGoogle = tags.comment.match(/google/i);
var commentWarning = selection.select('.form-field-comment').selectAll('.comment-warning')
.data(matches ? [0] : []);
.data(hasGoogle ? [0] : []);
commentWarning.exit()
.transition()

View File

@@ -260,6 +260,30 @@ export function uiCommit(context) {
}
function findHashtags(tags) {
return _.unionBy(commentTags(), hashTags(), function (s) {
return s.toLowerCase();
});
// Extract hashtags from `comment`
function commentTags() {
return tags.comment.match(/#[^\s\#]+/g);
}
// Extract and clean hashtags from `hashtags`
function hashTags() {
var t = tags.hashtags || '';
return t
.split(/[,;\s]+/)
.map(function (s) {
if (s[0] !== '#') { s = '#' + s; } // prepend '#'
var matched = s.match(/#[^\s\#]+/g); // match valid hashtags
return matched && matched[0];
}).filter(Boolean); // exclude falsey
}
}
function isReviewRequested(tags) {
var rr = tags.review_requested;
if (rr === undefined) return false;
@@ -286,6 +310,13 @@ export function uiCommit(context) {
}
});
if (!onInput) {
var hashtags = findHashtags(tags);
if (hashtags.length) {
tags.hashtags = hashtags.join(';').substr(0, 255);
}
}
if (!_.isEqual(changeset.tags, tags)) {
changeset = changeset.update({ tags: tags });
}