mirror of
https://github.com/discourse/discourse.git
synced 2025-04-30 23:54:37 +08:00
Extract category hashtag autocomplete into a resuable function.
This commit is contained in:
parent
9cf57e3ca4
commit
90100378b8
@ -3,7 +3,9 @@ import loadScript from 'discourse/lib/load-script';
|
|||||||
import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators';
|
import { default as computed, on, observes } from 'ember-addons/ember-computed-decorators';
|
||||||
import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
|
import { showSelector } from "discourse/lib/emoji/emoji-toolbar";
|
||||||
import Category from 'discourse/models/category';
|
import Category from 'discourse/models/category';
|
||||||
import { SEPARATOR as categoryHashtagSeparator } from 'discourse/lib/category-hashtags';
|
import { SEPARATOR as categoryHashtagSeparator,
|
||||||
|
categoryHashtagTriggerRule
|
||||||
|
} from 'discourse/lib/category-hashtags';
|
||||||
|
|
||||||
// Our head can be a static string or a function that returns a string
|
// Our head can be a static string or a function that returns a string
|
||||||
// based on input (like for numbered lists).
|
// based on input (like for numbered lists).
|
||||||
@ -262,25 +264,7 @@ export default Ember.Component.extend({
|
|||||||
return Category.search(term);
|
return Category.search(term);
|
||||||
},
|
},
|
||||||
triggerRule(textarea, opts) {
|
triggerRule(textarea, opts) {
|
||||||
const result = Discourse.Utilities.caretRowCol(textarea);
|
return categoryHashtagTriggerRule(textarea, opts);
|
||||||
const row = result.rowNum;
|
|
||||||
var col = result.colNum;
|
|
||||||
var line = textarea.value.split("\n")[row - 1];
|
|
||||||
|
|
||||||
if (opts && opts.backSpace) {
|
|
||||||
col = col - 1;
|
|
||||||
line = line.slice(0, line.length - 1);
|
|
||||||
|
|
||||||
// Don't trigger autocomplete when backspacing into a `#category |` => `#category|`
|
|
||||||
if (/^#{1}\w+/.test(line)) return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (col < 6) {
|
|
||||||
// Don't trigger autocomplete when ATX-style headers are used
|
|
||||||
return (line.slice(0, col) !== "#".repeat(col));
|
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -3,3 +3,25 @@ export const SEPARATOR = ":";
|
|||||||
export function replaceSpan($elem, categorySlug, categoryLink) {
|
export function replaceSpan($elem, categorySlug, categoryLink) {
|
||||||
$elem.replaceWith(`<a href="${categoryLink}" class="hashtag">#<span>${categorySlug}</span></a>`);
|
$elem.replaceWith(`<a href="${categoryLink}" class="hashtag">#<span>${categorySlug}</span></a>`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function categoryHashtagTriggerRule(textarea, opts) {
|
||||||
|
const result = Discourse.Utilities.caretRowCol(textarea);
|
||||||
|
const row = result.rowNum;
|
||||||
|
var col = result.colNum;
|
||||||
|
var line = textarea.value.split("\n")[row - 1];
|
||||||
|
|
||||||
|
if (opts && opts.backSpace) {
|
||||||
|
col = col - 1;
|
||||||
|
line = line.slice(0, line.length - 1);
|
||||||
|
|
||||||
|
// Don't trigger autocomplete when backspacing into a `#category |` => `#category|`
|
||||||
|
if (/^#{1}\w+/.test(line)) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (col < 6) {
|
||||||
|
// Don't trigger autocomplete when ATX-style headers are used
|
||||||
|
return (line.slice(0, col) !== "#".repeat(col));
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user