Skip to content

Commit

Permalink
#16 added mentio-require-leading-space option
Browse files Browse the repository at this point in the history
When typing in book Chinese, it doesn't make sense to require spaces
before trigger characters because space isn't used.  Made it an option
instead.
  • Loading branch information
Jeff Collins committed Aug 24, 2014
1 parent ec580f3 commit d400b8b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 39 deletions.
48 changes: 30 additions & 18 deletions dist/mentio.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ angular.module('mentio', [])
select: '&mentioSelect',
items: '=mentioItems',
typedTerm: '=mentioTypedTerm',
requireLeadingSpace: '=mentioRequireLeadingSpace',
ngModel: '='
},
controller: function($scope, $timeout, $attrs) {
Expand Down Expand Up @@ -65,7 +66,7 @@ angular.module('mentio', [])
item: item
});
mentioUtil.replaceTriggerText($scope.targetElement, $scope.targetElementPath,
$scope.targetElementSelectedOffset, $scope.triggerCharSet, text);
$scope.targetElementSelectedOffset, $scope.triggerCharSet, text, $scope.requireLeadingSpace);
$scope.setTriggerText('');
angular.element($scope.targetElement).triggerHandler('change');
if ($scope.isContentEditable()) {
Expand Down Expand Up @@ -254,7 +255,7 @@ angular.module('mentio', [])
scope.replacingMacro = false;
}

var mentionInfo = mentioUtil.getTriggerInfo(scope.triggerCharSet);
var mentionInfo = mentioUtil.getTriggerInfo(scope.triggerCharSet, scope.requireLeadingSpace);

if (mentionInfo !== undefined) {
/** save selection info about the target control for later re-selection */
Expand Down Expand Up @@ -390,7 +391,7 @@ angular.module('mentio', [])
if (scope.isVisible()) {
var triggerCharSet = [];
triggerCharSet.push(scope.triggerChar);
mentioUtil.popUnderMention(triggerCharSet, element);
mentioUtil.popUnderMention(triggerCharSet, element, scope.requireLeadingSpace);
}
}
);
Expand All @@ -412,7 +413,7 @@ angular.module('mentio', [])
if (visible) {
var triggerCharSet = [];
triggerCharSet.push(scope.triggerChar);
mentioUtil.popUnderMention(triggerCharSet, element);
mentioUtil.popUnderMention(triggerCharSet, element, scope.requireLeadingSpace);
}
});

Expand Down Expand Up @@ -485,9 +486,9 @@ angular.module('mentio')
.factory('mentioUtil', function ($window, $location, $anchorScroll, $timeout) {

// public
function popUnderMention (triggerCharSet, selectionEl) {
function popUnderMention (triggerCharSet, selectionEl, requireLeadingSpace) {
var coordinates;
var mentionInfo = getTriggerInfo(triggerCharSet);
var mentionInfo = getTriggerInfo(triggerCharSet, requireLeadingSpace);

if (mentionInfo !== undefined) {

Expand Down Expand Up @@ -682,10 +683,10 @@ angular.module('mentio')
}

// public
function replaceTriggerText (targetElement, path, offset, triggerCharSet, text) {
function replaceTriggerText (targetElement, path, offset, triggerCharSet, text, requireLeadingSpace) {
resetSelection(targetElement, path, offset);

var mentionInfo = getTriggerInfo(triggerCharSet);
var mentionInfo = getTriggerInfo(triggerCharSet, requireLeadingSpace);

if (mentionInfo !== undefined) {
if (selectedElementIsTextAreaOrInput()) {
Expand Down Expand Up @@ -813,7 +814,7 @@ angular.module('mentio')
}

// public
function getTriggerInfo (triggerCharSet) {
function getTriggerInfo (triggerCharSet, requireLeadingSpace) {
var selected, path, offset;
if (selectedElementIsTextAreaOrInput()) {
selected = document.activeElement;
Expand All @@ -828,24 +829,35 @@ angular.module('mentio')
}
var effectiveRange = getTextPrecedingCurrentSelection();
if (effectiveRange !== undefined && effectiveRange !== null) {
var mostRecentAtSymbol = -1;
var mostRecentTriggerCharPos = -1;
var triggerChar;
triggerCharSet.forEach(function(c) {
var idx = effectiveRange.lastIndexOf(c);
if (idx > mostRecentAtSymbol) {
mostRecentAtSymbol = idx;
if (idx > mostRecentTriggerCharPos) {
mostRecentTriggerCharPos = idx;
triggerChar = c;
}
});
if (mostRecentAtSymbol === 0 || /[\xA0\s]/g.test(
effectiveRange.substring(mostRecentAtSymbol - 1, mostRecentAtSymbol))) {
var currentTriggerSnippet = effectiveRange.substring(mostRecentAtSymbol + 1,
if (mostRecentTriggerCharPos >= 0 &&
(
mostRecentTriggerCharPos === 0 ||
!requireLeadingSpace ||
/[\xA0\s]/g.test
(
effectiveRange.substring(
mostRecentTriggerCharPos - 1,
mostRecentTriggerCharPos)
)
)
)
{
var currentTriggerSnippet = effectiveRange.substring(mostRecentTriggerCharPos + 1,
effectiveRange.length);

triggerChar = effectiveRange.substring(mostRecentAtSymbol, mostRecentAtSymbol+1);
triggerChar = effectiveRange.substring(mostRecentTriggerCharPos, mostRecentTriggerCharPos+1);
if (!(/[\xA0\s]/g.test(currentTriggerSnippet))) {
return {
mentionPosition: mostRecentAtSymbol,
mentionPosition: mostRecentTriggerCharPos,
mentionText: currentTriggerSnippet,
mentionSelectedElement: selected,
mentionSelectedPath: path,
Expand Down Expand Up @@ -1047,4 +1059,4 @@ angular.module('mentio')
};
});

angular.module("mentio").run(["$templateCache", function($templateCache) {$templateCache.put("mentio-menu.tpl.html","<style>\n.scrollable-menu {\n height: auto;\n max-height: 300px;\n overflow: auto;\n}\n</style>\n<ul class=\"dropdown-menu scrollable-menu\" style=\"display:block\">\n <li mentio-menu-item=\"item\" ng-repeat=\"item in items track by $index\">\n <a class=\"text-primary\" ng-bind-html=\"item.label | mentioHighlight:triggerText:\'menu-highlighted\' | unsafe\"></a>\n </li>\n</ul>");}]);
angular.module("mentio").run(["$templateCache", function($templateCache) {$templateCache.put("mentio-menu.tpl.html","<style>\n.scrollable-menu {\n height: auto;\n max-height: 300px;\n overflow: auto;\n}\n\n.menu-highlighted {\n font-weight: bold;\n}\n</style>\n<ul class=\"dropdown-menu scrollable-menu\" style=\"display:block\">\n <li mentio-menu-item=\"item\" ng-repeat=\"item in items track by $index\">\n <a class=\"text-primary\" ng-bind-html=\"item.label | mentioHighlight:typedTerm:\'menu-highlighted\' | unsafe\"></a>\n </li>\n</ul>");}]);
Loading

0 comments on commit d400b8b

Please sign in to comment.