Skip to content

Commit

Permalink
Update: Filter command list based on user query text
Browse files Browse the repository at this point in the history
  • Loading branch information
omjogani committed Oct 15, 2024
1 parent 499ef4d commit 0e158b6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions plugins/text-editor-resources/src/components/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import textEditor from '@hcengineering/text-editor'
import { type CompletionOptions } from '../Completion'
import MentionList from './MentionList.svelte'
import { SvelteRenderer } from './node-view'
import type { SuggestionKeyDownProps, SuggestionProps } from './extension/suggestion'
import type { SuggestionKeyDownProps, SuggestionOptions, SuggestionProps } from './extension/suggestion'
import InlineCommandsList from './InlineCommandsList.svelte'

export const mInsertTable = [
Expand Down Expand Up @@ -141,14 +141,29 @@ export function inlineCommandsConfig (
): Partial<CompletionOptions> {
return {
suggestion: {
items: () => {
return [
items: ({ query }: { query: string }) => {
const items = [
{ id: 'image', label: textEditor.string.Image, icon: view.icon.Image },
{ id: 'table', label: textEditor.string.Table, icon: view.icon.Table2 },
{ id: 'code-block', label: textEditor.string.CodeBlock, icon: view.icon.CodeBlock },
{ id: 'separator-line', label: textEditor.string.SeparatorLine, icon: view.icon.SeparatorLine },
{ id: 'todo-list', label: textEditor.string.TodoList, icon: view.icon.TodoList }
].filter(({ id }) => !excludedCommands.includes(id as InlineCommandId))

// to handle case of `todo-list` and `action-item` being the same
const searchableItems = items.map(item =>
item.id === 'todo-list'
? { ...item, searchLabels: ['action-item', textEditor.string.TodoList] }
: { ...item, searchLabels: [item.label] }
)

const filteredItems = searchableItems.filter(item =>
item.searchLabels.some(label =>
label.toLowerCase().includes(query.toLowerCase())
)
)

return filteredItems.length > 0 ? filteredItems : items
},
command: ({ editor, range, props }: { editor: Editor, range: Range, props: any }) => {
editor.commands.deleteRange(range)
Expand All @@ -169,6 +184,7 @@ export function inlineCommandsConfig (
element: document.body,
props: {
...props,
query: props.query,
close: () => {
component.destroy()
}
Expand Down

0 comments on commit 0e158b6

Please sign in to comment.