From 4cf357f61a324c4fd69bb87fc2d62dd2c35299e7 Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Wed, 16 Aug 2017 00:30:34 -0400 Subject: [PATCH] Add code to extract hashtags from changeset comment --- modules/ui/changeset_editor.js | 12 ++++++++---- modules/ui/commit.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/modules/ui/changeset_editor.js b/modules/ui/changeset_editor.js index c05181e52..f04b47e3d 100644 --- a/modules/ui/changeset_editor.js +++ b/modules/ui/changeset_editor.js @@ -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() diff --git a/modules/ui/commit.js b/modules/ui/commit.js index db773a2bb..5b492a949 100644 --- a/modules/ui/commit.js +++ b/modules/ui/commit.js @@ -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 }); }