Skip to content

Commit

Permalink
Suggestion container won't open any longer on input focus, if only on…
Browse files Browse the repository at this point in the history
…e suggestion is available, it will be inserted on select
  • Loading branch information
ransome1 committed Nov 26, 2023
1 parent 1f09f5f commit aabae11
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<developer_name>Robin Ahle</developer_name>
<content_rating type="oars-1.1"/>
<releases>
<release version="2.0.3-rc.2" date="2023-11-26"/>
<release version="2.0.3-rc.3" date="2023-11-26"/>
</releases>
<url type="homepage">https://github.com/ransome1/sleek</url>
<url type="contact">https://github.com/ransome1/sleek/issues</url>
Expand Down
2 changes: 1 addition & 1 deletion flatpak/com.github.ransome1.sleek.desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Desktop Entry]
Version=2.0.3-rc.2
Version=2.0.3-rc.3
Name=sleek
Exec=sleek
Type=Application
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.3-rc.2",
"version": "2.0.3-rc.3",
"main": "./src/main/main.ts",
"scripts": {
"build": "concurrently \"yarn run peggy\" \"yarn run build:main\" \"yarn run build:renderer\"",
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sleek",
"version": "2.0.3-rc.2",
"version": "2.0.3-rc.3",
"description": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"synopsis": "todo.txt manager for Linux, Windows and MacOS, free and open-source (FOSS)",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sleek
base: core20
version: "2.0.3-rc.2"
version: "2.0.3-rc.3"
summary: todo.txt manager for Linux, free and open-source (FOSS)
description: |
sleek is an open-source (FOSS) todo manager based on the todo.txt syntax. Stripped down to only the most necessary features, and with a clean and simple interface, sleek aims to help you focus on getting things done.
Expand Down
8 changes: 5 additions & 3 deletions src/main/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ if (!fs.existsSync(customStylesPath)) {
fs.writeFileSync(customStylesPath, '');
}

const handleConfigChange = async (key: string, newValue: any) => {
const handleConfigChange = async () => {
// TODO: If there was a search string before it will be ignored here, needs fix
const [todoObjects, attributes, headers, filters] = await processDataRequest('');
mainWindow!.webContents.send('requestData', todoObjects, attributes, headers, filters);
if (todoObjects) {
mainWindow!.webContents.send('requestData', todoObjects, attributes, headers, filters);
}
};

filterStorage.onDidAnyChange((newValue, oldValue) => {
Expand All @@ -111,7 +114,6 @@ configStorage.onDidAnyChange((newValue, oldValue) => {
handleConfigChange();
});


configStorage.onDidChange('files', async (files: File[] | undefined) => {
if (files) {

Expand Down
51 changes: 30 additions & 21 deletions src/renderer/TodoDialog/AutoSuggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,22 @@ const AutoSuggest: React.FC<Props> = ({
textFieldRef,
}) => {
const [suggestions, setSuggestions] = useState<string[]>([]);
const [selectedSuggestionIndex, setSelectedSuggestionIndex] = useState<number>(
-1
);
const [selectedSuggestionIndex, setSelectedSuggestionIndex] = useState<number>(-1);
const [prefix, setPrefix] = useState<string | null>(null);
const [matchPosition, setMatchPosition] = useState<{ start: number; end: number }>(
{ start: -1, end: -1 }
);
const [multilineTextField, setMultilineTextField] = useState<boolean>(
store.get('multilineTextField')
);
const [matchPosition, setMatchPosition] = useState<{ start: number; end: number }>({ start: -1, end: -1 });
const [multilineTextField, setMultilineTextField] = useState<boolean>(store.get('multilineTextField'));

const handleSetMultilineTextField = () => {
setMultilineTextField((prevMultilineTextField) => !prevMultilineTextField);
};

const handleSuggestionsFetchRequested = ({ value }: { value: string }) => {

let content = value.replaceAll('\n', ' ').replaceAll(String.fromCharCode(16), ' ');
if (!content) return false;

if (!content) return;
const cursorPosition = textFieldRef.current?.selectionStart;
if (!cursorPosition) return;

setSuggestions([]);
if (!cursorPosition) return false;

let match;
while ((match = regex.exec(content)) !== null) {
Expand All @@ -75,7 +68,6 @@ const AutoSuggest: React.FC<Props> = ({
_event: React.SyntheticEvent,
{ suggestion }: { suggestion: string }
) => {

if (!textFieldValue) return;
const createNewValue = (string: string, a: number, b: number) => {
return `${textFieldValue.slice(0, a)}${prefix}${string} ${textFieldValue.slice(
Expand All @@ -96,6 +88,13 @@ const AutoSuggest: React.FC<Props> = ({
setTextFieldValue(newValue);
};

const handleShouldRenderSuggestions = (value, reason) => {
if(reason === 'input-focused') {
return false;
}
return true;
}

const getSuggestions = (trigger: string, match: string): string[] => {
if (trigger === '@') {
setPrefix('@');
Expand All @@ -107,7 +106,7 @@ const AutoSuggest: React.FC<Props> = ({
return [];
};

const renderSuggestion = (
const handleRenderSuggestion = (
suggestion: string,
{ isHighlighted }: { isHighlighted: boolean }
) => (
Expand Down Expand Up @@ -136,8 +135,8 @@ const AutoSuggest: React.FC<Props> = ({
string: string
) => {
if (suggestions.length > 0) {
if (event.key === 'Enter') {
if (suggestions.length > 0 && selectedSuggestionIndex !== -1) {
if (suggestions.length === 1 || event.key === 'Enter') {
if (selectedSuggestionIndex !== -1) {
event.stopPropagation();
const selectedSuggestion = suggestions[selectedSuggestionIndex];
handleSuggestionSelected(null, { suggestion: selectedSuggestion });
Expand All @@ -161,6 +160,12 @@ const AutoSuggest: React.FC<Props> = ({
}
};

const handleSuggestionHighlighted = (suggestion) => {
if(suggestions.length === 1) {
handleSuggestionSelected(null, { suggestion: suggestions[0] });
}
}

const value = () => {
return multilineTextField
? textFieldValue.replaceAll(String.fromCharCode(16), '\n')
Expand All @@ -172,8 +177,7 @@ const AutoSuggest: React.FC<Props> = ({
value: value(),
onChange: handleChange,
inputRef: textFieldRef,
onKeyDown: (event: React.KeyboardEvent) =>
handleKeyDown(event, todoObject?.id, textFieldValue),
onKeyDown: (event: React.KeyboardEvent) => handleKeyDown(event, todoObject?.id, textFieldValue),
};

useEffect(() => {
Expand Down Expand Up @@ -201,11 +205,16 @@ const AutoSuggest: React.FC<Props> = ({
</Box>
)}
suggestions={suggestions}

shouldRenderSuggestions={handleShouldRenderSuggestions}


onSuggestionsFetchRequested={handleSuggestionsFetchRequested}
onSuggestionsClearRequested={handleSuggestionsClearRequested}
getSuggestionValue={(suggestion: any) => suggestion}
renderSuggestion={renderSuggestion}
getSuggestionValue={(suggestion: string) => suggestion}
renderSuggestion={handleRenderSuggestion}
onSuggestionSelected={handleSuggestionSelected}
onSuggestionHighlighted={handleSuggestionHighlighted}
inputProps={inputProps}
/>
<InputAdornment className="resize" position="end">
Expand Down

0 comments on commit aabae11

Please sign in to comment.