Skip to content
This repository was archived by the owner on Apr 7, 2020. It is now read-only.

Commit c58f2ea

Browse files
authored
decode pattern (#42)
* public renderElement method * delimiter and decode pattern * missing slash * decodePattern function to property * this.pattern for backward compatibility * remove ngAnnotate for example
1 parent a1e6502 commit c58f2ea

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ build/Release
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
2828
bower_components
29+
30+
#editors
31+
.idea

dist/mention.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
2323
var _this2 = this;
2424

2525
// Beginning of input or preceeded by spaces: @sometext
26-
this.pattern = this.pattern || /(?:\s+|^)@(\w+(?: \w+)?)$/;
26+
this.delimiter = '@';
27+
28+
// this.pattern is left for backward compatibility
29+
this.searchPattern = this.pattern || new RegExp("(?:\\s+|^)" + this.delimiter + "(\\w+(?: \\w+)?)$");
30+
31+
this.decodePattern = new RegExp(this.delimiter + "\[[\\s\\w]+:[0-9a-z-]+\]", "gi");
32+
2733
this.$element = $element;
2834
this.choices = [];
2935
this.mentions = [];
@@ -117,7 +123,7 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
117123
*
118124
* Get syntax-encoded HTML element
119125
*
120-
* @return {Element} HTML element
126+
* @return {Element} HTML element
121127
*/
122128
this.renderElement = function () {
123129
return $element.next();
@@ -145,7 +151,7 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
145151
this.decode = function () {
146152
var value = arguments.length <= 0 || arguments[0] === undefined ? ngModel.$modelValue : arguments[0];
147153

148-
return value ? value.replace(/@\[([\s\w]+):[0-9a-z-]+\]/gi, '$1') : '';
154+
return value ? value.replace(this.decodePattern, '$1') : '';
149155
};
150156

151157
/**
@@ -169,7 +175,7 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
169175
* @return {string} Syntax-encoded string version of choice
170176
*/
171177
this.encode = function (choice) {
172-
return '@[' + this.label(choice) + ':' + choice.id + ']';
178+
return this.delimiter + '[' + this.label(choice) + ':' + choice.id + ']';
173179
};
174180

175181
/**
@@ -188,7 +194,7 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
188194

189195
// TODO: come up with a better way to detect what to remove
190196
// TODO: consider alternative to using regex match
191-
text = text.substr(0, search.index + search[0].indexOf('@')) + this.label(mention) + ' ' + text.substr(search.index + search[0].length);
197+
text = text.substr(0, search.index + search[0].indexOf(this.delimiter)) + this.label(mention) + ' ' + text.substr(search.index + search[0].length);
192198
return text;
193199
};
194200

@@ -304,7 +310,8 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
304310
if ($element[0].selectionStart != $element[0].selectionEnd) return;
305311
var text = $element.val();
306312
// text to left of cursor ends with `@sometext`
307-
var match = _this2.pattern.exec(text.substr(0, $element[0].selectionStart));
313+
var match = _this2.searchPattern.exec(text.substr(0, $element[0].selectionStart));
314+
308315
if (match) {
309316
_this2.search(match);
310317
} else {
@@ -366,6 +373,7 @@ angular.module('ui.mention').controller('uiMention', ["$element", "$scope", "$at
366373

367374
// Autogrow is mandatory beacuse the textarea scrolls away from highlights
368375
$element.on('input', this.autogrow);
376+
369377
// Initialize autogrow height
370378
$timeout(this.autogrow, true);
371379
}]);

dist/mention.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/mentionController.es6.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ angular.module('ui.mention')
44
) {
55

66
// Beginning of input or preceeded by spaces: @sometext
7-
this.pattern = this.pattern || /(?:\s+|^)@(\w+(?: \w+)?)$/;
7+
this.delimiter = '@';
8+
9+
// this.pattern is left for backward compatibility
10+
this.searchPattern = this.pattern || new RegExp("(?:\\s+|^)" + this.delimiter + "(\\w+(?: \\w+)?)$");
11+
12+
this.decodePattern = new RegExp(this.delimiter + "\[[\\s\\w]+:[0-9a-z-]+\]", "gi");
13+
814
this.$element = $element;
915
this.choices = [];
1016
this.mentions = [];
@@ -93,7 +99,7 @@ angular.module('ui.mention')
9399
*
94100
* Get syntax-encoded HTML element
95101
*
96-
* @return {Element} HTML element
102+
* @return {Element} HTML element
97103
*/
98104
this.renderElement = () => {
99105
return $element.next();
@@ -119,7 +125,7 @@ angular.module('ui.mention')
119125
* @return {string} plaintext string with encoded labels used
120126
*/
121127
this.decode = function(value = ngModel.$modelValue) {
122-
return value ? value.replace(/@\[([\s\w]+):[0-9a-z-]+\]/gi, '$1') : '';
128+
return value ? value.replace(this.decodePattern, '$1') : '';
123129
};
124130

125131
/**
@@ -143,7 +149,7 @@ angular.module('ui.mention')
143149
* @return {string} Syntax-encoded string version of choice
144150
*/
145151
this.encode = function(choice) {
146-
return `@[${this.label(choice)}:${choice.id}]`;
152+
return `${this.delimiter}[${this.label(choice)}:${choice.id}]`;
147153
};
148154

149155
/**
@@ -159,7 +165,7 @@ angular.module('ui.mention')
159165
this.replace = function(mention, search = this.searching, text = ngModel.$viewValue) {
160166
// TODO: come up with a better way to detect what to remove
161167
// TODO: consider alternative to using regex match
162-
text = text.substr(0, search.index + search[0].indexOf('@')) +
168+
text = text.substr(0, search.index + search[0].indexOf(this.delimiter)) +
163169
this.label(mention) + ' ' +
164170
text.substr(search.index + search[0].length);
165171
return text;
@@ -277,7 +283,8 @@ angular.module('ui.mention')
277283
return;
278284
let text = $element.val();
279285
// text to left of cursor ends with `@sometext`
280-
let match = this.pattern.exec(text.substr(0, $element[0].selectionStart));
286+
let match = this.searchPattern.exec(text.substr(0, $element[0].selectionStart));
287+
281288
if (match) {
282289
this.search(match);
283290
} else {
@@ -316,8 +323,6 @@ angular.module('ui.mention')
316323
}
317324
});
318325

319-
320-
321326
this.onMouseup = (function(event) {
322327
if (event.target == $element[0])
323328
return
@@ -339,6 +344,7 @@ angular.module('ui.mention')
339344

340345
// Autogrow is mandatory beacuse the textarea scrolls away from highlights
341346
$element.on('input', this.autogrow);
347+
342348
// Initialize autogrow height
343349
$timeout(this.autogrow, true);
344350
});

test/uiMentionController.spec.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ describe('uiMention', () => {
3232
ctrlInstance = createController($element);
3333
});
3434

35-
it('exposes a pattern', () => {
36-
expect(ctrlInstance.pattern).to.eql(/(?:\s+|^)@(\w+(?: \w+)?)$/);
35+
it('exposes a delimiter', () => {
36+
expect(ctrlInstance.delimiter).to.eql('@');
37+
});
38+
39+
it('exposes a searchPattern', () => {
40+
expect(ctrlInstance.searchPattern).to.eql(/(?:\s+|^)@(\w+(?: \w+)?)$/);
41+
});
42+
43+
it('exposes a decodePattern', () => {
44+
expect(ctrlInstance.decodePattern).to.eql(/@[[\s\w]+:[0-9a-z-]+]/gi);
3745
});
3846

3947
it('exposes the given $element', () => {
@@ -399,7 +407,7 @@ describe('uiMention', () => {
399407

400408
it('searches if there is a match', () => {
401409
let spy = sinon.spy(ctrlInstance, 'search');
402-
ctrlInstance.pattern = /foo/;
410+
ctrlInstance.searchPattern = /foo/;
403411
$element.val('@foo');
404412
$element[0].selectionStart = $element[0].selectionEnd = 4;
405413

@@ -410,7 +418,7 @@ describe('uiMention', () => {
410418

411419
it('cancels if there is no match', () => {
412420
let spy = sinon.spy(ctrlInstance, 'cancel');
413-
ctrlInstance.pattern = /foo/;
421+
ctrlInstance.searchPattern = /foo/;
414422
$element.val('@bar');
415423
$element[0].selectionStart = $element[0].selectionEnd = 4;
416424

0 commit comments

Comments
 (0)