Skip to content

Commit

Permalink
Fixes for allow spaces (#162)
Browse files Browse the repository at this point in the history
* hide Tribute menu if allow spaces = false and inserting a space while menu is open

* add positionMenu?: boolean
  • Loading branch information
fishgrind authored and mrsweaters committed Nov 13, 2018
1 parent bfa3ba7 commit 8b42cc9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
27 changes: 22 additions & 5 deletions dist/tribute.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tribute.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Tribute.js
Expand Up @@ -31,6 +31,7 @@ class Tribute {
this.allowSpaces = allowSpaces
this.replaceTextSuffix = replaceTextSuffix
this.positionMenu = positionMenu
this.hasTrailingSpace = false;

if (values) {
this.collection = [{
Expand Down
20 changes: 18 additions & 2 deletions src/TributeEvents.js
Expand Up @@ -104,6 +104,13 @@ class TributeEvents {

if (event.keyCode === 27) return

if (!instance.tribute.allowSpaces && instance.tribute.hasTrailingSpace) {
instance.tribute.hasTrailingSpace = false;
instance.commandEvent = true;
instance.callbacks()["space"](event, this);
return
}

if (!instance.tribute.isActive) {
let keyCode = instance.getKeyCode(instance, this, event)

Expand Down Expand Up @@ -142,7 +149,7 @@ class TributeEvents {
getKeyCode(instance, el, event) {
let char
let tribute = instance.tribute
let info = tribute.range.getTriggerInfo(false, false, true, tribute.allowSpaces)
let info = tribute.range.getTriggerInfo(false, tribute.hasTrailingSpace, true, tribute.allowSpaces)

if (info) {
return info.mentionTriggerChar.charCodeAt(0)
Expand All @@ -153,7 +160,7 @@ class TributeEvents {

updateSelection(el) {
this.tribute.current.element = el
let info = this.tribute.range.getTriggerInfo(false, false, true, this.tribute.allowSpaces)
let info = this.tribute.range.getTriggerInfo(false, this.tribute.hasTrailingSpace, true, this.tribute.allowSpaces)

if (info) {
this.tribute.current.selectedPath = info.mentionSelectedPath
Expand Down Expand Up @@ -198,6 +205,15 @@ class TributeEvents {
// choose first match
this.callbacks().enter(e, el)
},
space: (e, el) => {
if (this.tribute.isActive && !this.tribute.allowSpaces) {
e.stopPropagation();
setTimeout(() => {
this.tribute.hideMenu();
this.tribute.isActive = false;
}, 0);
}
},
up: (e, el) => {
// navigate up ul
if (this.tribute.isActive) {
Expand Down
4 changes: 3 additions & 1 deletion src/TributeRange.js
Expand Up @@ -22,7 +22,7 @@ class TributeRange {
let context = this.tribute.current,
coordinates

let info = this.getTriggerInfo(false, false, true, this.tribute.allowSpaces)
let info = this.getTriggerInfo(false, this.tribute.hasTrailingSpace, true, this.tribute.allowSpaces)

if (typeof info !== 'undefined') {

Expand Down Expand Up @@ -333,6 +333,8 @@ class TributeRange {

let regex = allowSpaces ? /[^\S ]/g : /[\xA0\s]/g;

this.tribute.hasTrailingSpace = regex.test(currentTriggerSnippet);

if (!leadingSpace && (menuAlreadyActive || !(regex.test(currentTriggerSnippet)))) {
return {
mentionPosition: mostRecentTriggerCharPos,
Expand Down
3 changes: 3 additions & 0 deletions tributejs.d.ts
Expand Up @@ -50,6 +50,9 @@ export type TributeCollection<T extends {}> = {
// optionally specify a custom suffix for the replace text
// (defaults to empty space if undefined)
replaceTextSuffix?: string

//specify whether the menu should be positioned
positionMenu?: boolean
}

export type TributeOptions<T> = TributeCollection<T> | {
Expand Down

0 comments on commit 8b42cc9

Please sign in to comment.