Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple trigger support in comment component at any position 🦊 #1854

Open
wants to merge 18 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
482beff
fix: multiple trigger support in comment component at any position 🦊
CoderSerio May 24, 2024
881efad
chore: 将重复逻辑封装为函数
CoderSerio May 25, 2024
04035ad
chore(env): 适配`pnpm@9`及`Nodejs 20` (#1855)
fu050409 May 26, 2024
e2747b8
fix(select): 下拉没有内容的时候没有去除掉dom ul。 #1844 (#1851)
aolinpk May 26, 2024
77caaf7
组件易用性优化,category-search组件唤出textInput时,input框自动聚焦 (#1842) (#1848)
aolinpk May 26, 2024
36f3672
Chore: Update package.json
GreatZPP May 26, 2024
18e63c4
fix: build site vitepress当前版本暂不支持esm引入lodash (#1860)
GreatZPP May 27, 2024
3cdd771
ActionTimeline组件新增自定义icon位置的内容插槽。 #1846 (#1872)
aolinpk Jun 3, 2024
e767b09
feat(md): MarkdownMD 编辑器支持自定义配置工具栏 #1862 (#1874)
aolinpk Jun 3, 2024
ccebf06
fix(search): fix invalid clear icon (#1871)
fu050409 Jun 3, 2024
b4a3289
fix: 解决InputNumber数字输入框,去除掉值之后失焦/确认之后会变成数值0的问题。 (#1849)
aolinpk Jun 3, 2024
69917a5
chore: update package.json
GreatZPP Jun 3, 2024
926954c
Merge branch 'fix/mention-filter-trigger' of https://github.com/SWPUO…
fu050409 Jun 7, 2024
096d2d9
refactor(mention): refactor display logic like GitHub
fu050409 Jun 7, 2024
3feff35
fix(mention): fix focus state restore
fu050409 Jun 7, 2024
e87d7c8
refactor(mention): merge `arrowEnterDown` to `arrowKeyDown`
fu050409 Jun 8, 2024
1ff3025
fix(mention): fix suggestions wont close when router changed
fu050409 Jun 8, 2024
26424da
fix(mention): fix suggestions wont close if value is empty
fu050409 Jun 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions packages/devui-vue/devui/mention/src/mention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,46 @@ export default defineComponent({
const showSuggestions = ref<boolean>(false);
const currentIndex = ref<number>(0);
const suggestionsTop = ref<number>();
const suggestionsLeft = ref<number>();
const suggestions = ref<IMentionSuggestionItem[]>([]);
const filteredSuggestions = ref<IMentionSuggestionItem[]>([]);
const suggestionsDom = ref<HTMLDivElement>();
const loading = computed(() => props.loading);
const instance = getCurrentInstance();

const checkShouldShowSuggestions = (word: string) => {
if (word && props.trigger.includes(word[0])) {
// 需要以空格作为分割,单词尾部的 trigger 符号不生效
return word.length === 1 || !props.trigger.includes(word[word.length - 1]);
} else {
return false;
}
};

const handleUpdate = debounce((val: string) => {
if (props.trigger.includes(val[0])) {
const wordsWithoutSpace = val.split(' ');
const lastWordIndex = wordsWithoutSpace.length - 1;
const lastWord = wordsWithoutSpace[lastWordIndex];
const shouldBeActive = checkShouldShowSuggestions(lastWord);
if (shouldBeActive) {
showSuggestions.value = true;
if (props.position === 'top') {
setTimeout(() => {
const height = window.getComputedStyle(suggestionsDom.value as Element, null).height;
const element = window.getComputedStyle(suggestionsDom.value as Element, null);
const { height, width } = element;
suggestionsTop.value = -Number(height.replace('px', ''));
suggestionsLeft.value = Number(width.replace('px', ''));
}, 0);
}
filteredSuggestions.value = (suggestions.value as IMentionSuggestionItem[]).filter((item: IMentionSuggestionItem) =>
String(item[props.dmValueParse.value as keyof IMentionSuggestionItem])
.toLocaleLowerCase()
.includes(val.slice(1).toLocaleLowerCase())
.includes(lastWord.slice(1).toLocaleLowerCase())
);
} else {
showSuggestions.value = false;
}
emit('change', val.slice(1));
emit('change', lastWord.slice(1));
}, 300);

const handleBlur = (e: Event) => {
Expand All @@ -67,7 +83,12 @@ export default defineComponent({
e.stopPropagation();
e.preventDefault();
showSuggestions.value = false;
textContext.value = textContext.value.substring(0, 1) + item[props.dmValueParse.value as keyof IMentionSuggestionItem];
const wordsWithoutSpace = textContext.value.split(' ');
const lastWordIndex = wordsWithoutSpace.length - 1;
const lastWord = wordsWithoutSpace[lastWordIndex];
const selectedWord = item[props.dmValueParse.value as keyof IMentionSuggestionItem];
wordsWithoutSpace[lastWordIndex] = `${lastWord[0]}${selectedWord}`;
textContext.value = wordsWithoutSpace.join(' ');
};

const arrowKeyDown = (e: KeyboardEvent) => {
Expand Down Expand Up @@ -102,9 +123,12 @@ export default defineComponent({
e.stopPropagation();
e.preventDefault();
showSuggestions.value = false;
textContext.value =
textContext.value.substring(0, 1) +
filteredSuggestions.value[currentIndex.value][props.dmValueParse.value as keyof IMentionSuggestionItem];
const wordsWithoutSpace = textContext.value.split(' ');
CoderSerio marked this conversation as resolved.
Show resolved Hide resolved
const lastWordIndex = wordsWithoutSpace.length - 1;
const lastWord = wordsWithoutSpace[lastWordIndex];
const selectedWord = filteredSuggestions.value[currentIndex.value][props.dmValueParse.value as keyof IMentionSuggestionItem];
wordsWithoutSpace[lastWordIndex] = `${lastWord[0]}${selectedWord}`;
textContext.value = wordsWithoutSpace.join(' ');
emit('select', filteredSuggestions.value[currentIndex.value]);
}
}
Expand Down Expand Up @@ -149,6 +173,7 @@ export default defineComponent({
style={{
marginTop: props.position === 'top' ? '0px' : '-16px',
top: suggestionsTop.value ? suggestionsTop.value + 'px' : 'inherit',
left: suggestionsLeft.value ? suggestionsLeft.value + 'px' : 'inherit',
}}>
{filteredSuggestions.value.length > 0 ? (
filteredSuggestions.value?.map((item, index) => {
Expand Down
Loading