Skip to content

Commit

Permalink
fix folder mentions in autocomplete input
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiya1155 committed Dec 12, 2023
1 parent 2fe8d79 commit 82d4ce3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
28 changes: 3 additions & 25 deletions client/src/Project/CurrentTabContent/ChatTab/Input/InputCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ import {
import { schema as basicSchema } from 'prosemirror-schema-basic';
import * as icons from 'file-icons-js';
import { useTranslation } from 'react-i18next';
import {
InputEditorContent,
ParsedQueryType,
ParsedQueryTypeEnum,
} from '../../../../types/general';
import { InputEditorContent, ParsedQueryType } from '../../../../types/general';
import { getFileExtensionForLang } from '../../../../utils';
import { blurInput } from '../../../../utils/domUtils';
import { getMentionsPlugin } from './mentionPlugin';
import { addMentionNodes } from './utils';
import { addMentionNodes, mapEditorContentToInputValue } from './utils';
import { placeholderPlugin } from './placeholderPlugin';

const schema = new Schema({
Expand Down Expand Up @@ -142,25 +138,7 @@ const InputCore = ({
if (!parts) {
return false;
}
onSubmit?.({
parsed:
parts?.map((s: InputEditorContent) =>
s.type === 'mention'
? {
type:
s.attrs.type === 'lang'
? ParsedQueryTypeEnum.LANG
: ParsedQueryTypeEnum.PATH,
text: s.attrs.id,
}
: { type: ParsedQueryTypeEnum.TEXT, text: s.text },
) || [],
plain: parts
?.map((s: InputEditorContent) =>
s.type === 'mention' ? `${s.attrs.type}:${s.attrs.id}` : s.text,
)
.join(''),
});
onSubmit?.(mapEditorContentToInputValue(parts));
return true;
},
'Ctrl-Enter': baseKeymap.Enter,
Expand Down
22 changes: 2 additions & 20 deletions client/src/Project/CurrentTabContent/ChatTab/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import useKeyboardNavigation from '../../../../hooks/useKeyboardNavigation';
import KeyboardHint from '../../../../components/KeyboardHint';
import { focusInput } from '../../../../utils/domUtils';
import InputCore from './InputCore';
import { mapEditorContentToInputValue } from './utils';

type Props = {
value?: { parsed: ParsedQueryType[]; plain: string };
Expand Down Expand Up @@ -122,26 +123,7 @@ const ConversationInput = ({
);

const onChangeInput = useCallback((inputState: InputEditorContent[]) => {
const newValue = inputState
.map((s) =>
s.type === 'mention' ? `${s.attrs.type}:${s.attrs.id}` : s.text,
)
.join('');
const newValueParsed = inputState.map((s) =>
s.type === 'mention'
? {
type:
s.attrs.type === 'lang'
? ParsedQueryTypeEnum.LANG
: ParsedQueryTypeEnum.PATH,
text: s.attrs.id,
}
: { type: ParsedQueryTypeEnum.TEXT, text: s.text },
);
setInputValue({
plain: newValue,
parsed: newValueParsed,
});
setInputValue(mapEditorContentToInputValue(inputState));
}, []);

const onSubmitButtonClicked = useCallback(() => {
Expand Down
8 changes: 6 additions & 2 deletions client/src/Project/CurrentTabContent/ChatTab/Input/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const mentionNode: NodeSpec = {
draggable: false,

toDOM: (node) => {
const isDir =
node.attrs.type === 'dir' ||
node.attrs.display.endsWith('/') ||
node.attrs.display.endsWith('\\');
const folderIcon = document.createElement('span');
folderIcon.innerHTML = `<svg viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
Expand All @@ -36,7 +40,7 @@ export const mentionNode: NodeSpec = {
class:
'prosemirror-tag-node inline-flex gap-1 h-[22px] items-center align-bottom bg-bg-base border border-bg-border rounded px-1',
},
node.attrs.type === 'dir'
isDir
? folderIcon
: [
'span',
Expand All @@ -55,7 +59,7 @@ export const mentionNode: NodeSpec = {
],
node.attrs.type === 'lang'
? node.attrs.display
: node.attrs.type === 'dir'
: isDir
? splitPath(node.attrs.display).slice(-2)[0]
: splitPath(node.attrs.display).pop(),
];
Expand Down
32 changes: 32 additions & 0 deletions client/src/Project/CurrentTabContent/ChatTab/Input/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
import OrderedMap from 'orderedmap';
import { type NodeSpec } from 'prosemirror-model';
import {
InputEditorContent,
ParsedQueryTypeEnum,
} from '../../../../types/general';
import { mentionNode } from './nodes';

export function addMentionNodes(nodes: OrderedMap<NodeSpec>) {
return nodes.append({
mention: mentionNode,
});
}

export const mapEditorContentToInputValue = (
inputState: InputEditorContent[],
) => {
const getType = (type: string) => (type === 'lang' ? 'lang' : 'path');
const newValue = inputState
.map((s) =>
s.type === 'mention'
? `${getType(s.attrs.type)}:${s.attrs.id}`
: s.text.replace(new RegExp(String.fromCharCode(160), 'g'), ' '),
)
.join('');
const newValueParsed = inputState.map((s) =>
s.type === 'mention'
? {
type:
s.attrs.type === 'lang'
? ParsedQueryTypeEnum.LANG
: ParsedQueryTypeEnum.PATH,
text: s.attrs.id,
}
: { type: ParsedQueryTypeEnum.TEXT, text: s.text },
);
return {
plain: newValue,
parsed: newValueParsed,
};
};

0 comments on commit 82d4ce3

Please sign in to comment.