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

Feature - Enhanced Text Editing Feature: Single Backspace to Delete Entire Mentioned String #133

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/components/mention-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const MentionInput: FC<MentionInputProps> = (

onSelectionChange,

deleteFullMentioned,

...textInputProps
},
) => {
Expand All @@ -54,7 +56,7 @@ const MentionInput: FC<MentionInputProps> = (
* @param changedText
*/
const onChangeInput = (changedText: string) => {
onChange(generateValueFromPartsAndChangedText(parts, plainText, changedText));
onChange(generateValueFromPartsAndChangedText(parts, plainText, changedText, deleteFullMentioned));
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ type MentionInputProps = Omit<TextInputProps, 'onChange'> & {
inputRef?: Ref<TextInput>;

containerStyle?: StyleProp<ViewStyle>;

deleteFullMentioned?: boolean;
};

export type {
Expand Down
20 changes: 16 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getPartIndexByCursor = (parts: Part[], cursor: number, isIncludeEnd?: bool
* @param cursor current cursor position
* @param count count of characters that didn't change
*/
const getPartsInterval = (parts: Part[], cursor: number, count: number): Part[] => {
const getPartsInterval = (parts: Part[], cursor: number, count: number, deleteFullMentioned: boolean): Part[] => {
const newCursor = cursor + count;

const currentPartIndex = getPartIndexByCursor(parts, cursor);
Expand All @@ -69,6 +69,12 @@ const getPartsInterval = (parts: Part[], cursor: number, count: number): Part[]
// Push whole first affected part or sub-part of the first affected part
if (currentPart.position.start === cursor && currentPart.position.end <= newCursor) {
partsInterval.push(currentPart);
} else if (deleteFullMentioned && currentPart.data?.trigger === "@") {
partsInterval.push(
generatePlainTextPart(
currentPart.text.substring(0, -(cursor - currentPart.position.start))
)
);
} else {
partsInterval.push(generatePlainTextPart(currentPart.text.substr(cursor - currentPart.position.start, count)));
}
Expand All @@ -80,6 +86,12 @@ const getPartsInterval = (parts: Part[], cursor: number, count: number): Part[]
// Push whole last affected part or sub-part of the last affected part
if (newPart.position.end === newCursor && newPart.position.start >= cursor) {
partsInterval.push(newPart);
} else if (deleteFullMentioned && newPart.data?.trigger === "@") {
partsInterval.push(
generatePlainTextPart(
newPart.text.substring(0, -(newCursor - newPart.position.start))
)
);
} else {
partsInterval.push(generatePlainTextPart(newPart.text.substr(0, newCursor - newPart.position.start)));
}
Expand Down Expand Up @@ -185,7 +197,7 @@ const getMentionPartSuggestionKeywords = (
* @param originalText original plain text
* @param changedText changed plain text
*/
const generateValueFromPartsAndChangedText = (parts: Part[], originalText: string, changedText: string) => {
const generateValueFromPartsAndChangedText = (parts: Part[], originalText: string, changedText: string, deleteFullMentioned = false) => {
const changes = diffChars(originalText, changedText) as CharactersDiffChange[];

let newParts: Part[] = [];
Expand Down Expand Up @@ -221,8 +233,8 @@ const generateValueFromPartsAndChangedText = (parts: Part[], originalText: strin
*/
default: {
if (change.count !== 0) {
newParts = newParts.concat(getPartsInterval(parts, cursor, change.count));

newParts = newParts.concat(getPartsInterval(parts, cursor, change.count, deleteFullMentioned));
cursor += change.count;
}

Expand Down