Skip to content

Commit

Permalink
Added proper role to window entry in window menu; general refactoring…
Browse files Browse the repository at this point in the history
…; fixed a bug, which occurred if archiving file could not be accessed
  • Loading branch information
ransome1 committed Jan 11, 2024
1 parent a6c7509 commit 9cfef4c
Show file tree
Hide file tree
Showing 23 changed files with 208 additions and 232 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Contribution Guidelines

Please find sleek's contribution guidelines here: https://github.com/ransome1/sleek/wiki/Contributing
Click here to see sleek's [contributing guidelines](https://github.com/ransome1/sleek/wiki/Contributing-Guidelines).
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Robin Ahle
Copyright (c) 2024 Robin Ahle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
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.5" date="2024-01-06"/>
<release version="2.0.6" date="2024-01-11"/>
</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.5
Version=2.0.6
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.5",
"version": "2.0.6",
"main": "./src/main/main.tsx",
"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.5",
"version": "2.0.6",
"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.5"
version: "2.0.6"
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
28 changes: 14 additions & 14 deletions src/__tests__/__mock__/recurrence.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

2024-01-09 Line 1 rec:1d due:2024-01-10
2024-01-09 Line 1 rec:w due:2024-01-16
2024-01-09 Line 1 rec:2m due:2024-03-09
2024-01-09 Line 1 rec:+1d due:2024-01-11
2024-01-09 Line 1 rec:7w due:2024-02-27
2024-01-09 Line 1 due:2023-07-24 rec:+1b
2024-01-09 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-01-09 Water plants @home +quick due:2024-01-16 t:2024-01-06 rec:1w
2024-01-09 Line 1 rec:+1d t:2023-09-20
2024-01-09 Line 1 rec:1d pri:A due:2024-01-10
2024-01-09 (A) Do something rec:d t:2024-01-10 @SomeContext
2024-01-09 Do something rec:0d
2024-01-09 Do something rec:0d due:2024-01-09
2024-01-09 Do something rec:0d due:2024-01-09 t:2024-01-09
2024-01-11 Line 1 rec:1d due:2024-01-12
2024-01-11 Line 1 rec:w due:2024-01-18
2024-01-11 Line 1 rec:2m due:2024-03-11
2024-01-11 Line 1 rec:+1d due:2024-01-13
2024-01-11 Line 1 rec:7w due:2024-02-29
2024-01-11 Line 1 due:2023-07-24 rec:+1b
2024-01-11 taxes are due in one year t:2022-03-30 due:2022-04-30 rec:+1y
2024-01-11 Water plants @home +quick due:2024-01-18 t:2024-01-08 rec:1w
2024-01-11 Line 1 rec:+1d t:2023-09-20
2024-01-11 Line 1 rec:1d pri:A due:2024-01-12
2024-01-11 (A) Do something rec:d t:2024-01-12 @SomeContext
2024-01-11 Do something rec:0d
2024-01-11 Do something rec:0d due:2024-01-11
2024-01-11 Do something rec:0d due:2024-01-11 t:2024-01-11
18 changes: 10 additions & 8 deletions src/__tests__/main/Archive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ jest.mock('../../main/main', () => ({

jest.mock('../../main/config', () => ({
configStorage: {
get: jest.fn((key) => {
if(key === 'files') {
return [
{ active: false, todoFilePath: './src/__tests__/__mock__/test1.txt', todoFileName: 'test1.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
{ active: true, todoFilePath: './src/__tests__/__mock__/archiving.txt', todoFileName: 'archiving.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
{ active: false, todoFilePath: './src/__tests__/__mock__/test3.txt', todoFileName: 'test3.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
];
}
get: jest.fn(() => {
return [
{ active: false, todoFilePath: './src/__tests__/__mock__/test1.txt', todoFileName: 'test1.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
{ active: true, todoFilePath: './src/__tests__/__mock__/archiving.txt', todoFileName: 'archiving.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
{ active: false, todoFilePath: './src/__tests__/__mock__/test3.txt', todoFileName: 'test3.txt', todoFileBookmark: null, doneFilePath: './src/__tests__/__mock__/done.txt', doneFileBookmark: null },
];
}),
set: jest.fn(),
},
}));

jest.mock('../../main/modules/Menu', () => ({
createMenu: jest.fn(),
}));

describe('Archiving', () => {
beforeEach(async() => {
jest.clearAllMocks();
Expand Down
51 changes: 25 additions & 26 deletions src/main/modules/File/Archive.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { app } from 'electron';
import fs from 'fs/promises';
import { getActiveFile } from './Active';
import { replaceFileContent } from './Write';
import { readFileContent } from './File';
import { mainWindow } from '../../main';
import { createTodoObjects } from '../ProcessDataRequest/CreateTodoObjects';

async function replaceFileContent(string: string, filePath: string) {
await fs.writeFile(filePath, string, 'utf8');
}

function handleRequestArchive(): void {
const activeFile = getActiveFile();
mainWindow!.webContents.send('triggerArchiving', Boolean(activeFile?.doneFilePath));
}

async function extractTodoObjectsFromFile(filePath: string, complete: boolean | null, bookmark: string | null): Promise<string[]> {
let fileContent;

if(process.mas && bookmark) {
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark);
fileContent = await fs.readFile(filePath, 'utf8');
stopAccessingSecurityScopedResource()
async function readFilteredFileContent(filePath: string, bookmark: string | null, complete: boolean): Promise<string> {
const filterStrings = (fileContent: string, complete: boolean): string => {
const arrayOfStrings = fileContent.split('\n');
const filteredArrayOfStrings = arrayOfStrings.filter(string => {
return (complete && string.startsWith('x ')) || (!complete && !string.startsWith('x '));
});
return filteredArrayOfStrings.join('\n');
};
const fileContent: string | null = await readFileContent(filePath, bookmark);
if(fileContent) {
return filterStrings(fileContent, complete);
} else {
fileContent = await fs.readFile(filePath, 'utf8');
return '';
}

const todoObjects = await createTodoObjects(fileContent);

return todoObjects
.filter((todoObject) => todoObject && (complete === null || todoObject.complete === complete))
.map((todoObject) => (todoObject?.string ?? '').toString());
}

async function archiveTodos(): Promise<string> {
Expand All @@ -39,20 +39,19 @@ async function archiveTodos(): Promise<string> {
return 'Archiving file is not defined';
}

const completedTodos = await extractTodoObjectsFromFile(activeFile.todoFilePath, true, activeFile.todoFileBookmark);
const completedTodos: string = await readFilteredFileContent(activeFile.todoFilePath, activeFile.todoFileBookmark, true);

if(completedTodos.length === 0) {
if(!completedTodos) {
return 'No completed todos found';
}

const uncompletedTodos = await extractTodoObjectsFromFile(activeFile.todoFilePath, false, activeFile.todoFileBookmark);
const todosFromDoneFile = await extractTodoObjectsFromFile(activeFile.doneFilePath, true, activeFile.doneFileBookmark);

const stringDoneFile = todosFromDoneFile.length === 0 ? completedTodos.join('\n') : todosFromDoneFile.join('\n') + '\n' + completedTodos.join('\n');
const stringTodoFile = uncompletedTodos.join('\n');
const uncompletedTodos: string = await readFilteredFileContent(activeFile.todoFilePath, activeFile.todoFileBookmark, false);

const todosFromDoneFile: string | null = await readFileContent(activeFile.doneFilePath, activeFile.doneFileBookmark);

await replaceFileContent(stringDoneFile, activeFile.doneFilePath);
await replaceFileContent(stringTodoFile, activeFile.todoFilePath);
await replaceFileContent(todosFromDoneFile + '\n' + completedTodos, activeFile.doneFilePath);

await replaceFileContent(uncompletedTodos, activeFile.todoFilePath);

return 'Successfully archived';
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/modules/File/Write.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,4 @@ async function writeTodoObjectToFile(id: number, string: string): Promise<void>
}
}

async function replaceFileContent(string: string, filePath: string) {
await fs.writeFile(filePath, string, 'utf8');
}

export { writeTodoObjectToFile, replaceFileContent, removeLineFromFile };
export { writeTodoObjectToFile, removeLineFromFile };
1 change: 1 addition & 0 deletions src/main/modules/IpcMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function handleUpdateTodoObject(event: IpcMainEvent, index: number, string: stri

async function handleWriteTodoToFile(event: IpcMainEvent, index: number, string: string, state: boolean, attributeType: string, attributeValue: string): Promise<void> {
try {
console.log(string)
let todoObject;
if(attributeType && attributeValue) {
todoObject = createTodoObject(index, string, attributeType, attributeValue);
Expand Down
33 changes: 9 additions & 24 deletions src/main/modules/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function createMenu(files: FileObject[]) {
const template: Electron.MenuItemConstructorOptions[] = [
{
label: 'sleek',
type: 'submenu',
submenu: [
{
label: 'About',
Expand All @@ -26,7 +27,6 @@ function createMenu(files: FileObject[]) {
'Reveal configuration folder',
],
};

const buttonClicked = await dialog.showMessageBox(options);
if(buttonClicked.response === 1) {
const pathToReveal = app.getPath('userData');
Expand All @@ -45,19 +45,15 @@ function createMenu(files: FileObject[]) {
? [
{ type: 'separator' },
{
label: 'Hide',
accelerator: 'Cmd+H',
role: 'hide',
}
]
: []),
{ type: 'separator' },
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: () => {
app.quit();
},
role: 'quit',
accelerator: 'CmdOrCtrl+Q'
},
],
},
Expand Down Expand Up @@ -92,16 +88,7 @@ function createMenu(files: FileObject[]) {
],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
{ role: 'selectAll' },
],
role: 'editMenu'
},
{
label: 'View',
Expand Down Expand Up @@ -182,24 +169,19 @@ function createMenu(files: FileObject[]) {
handleRequestArchive();
},
},
{
role: 'reload',
visible: false,
},
],
}]
: []),
{
label: 'Window',
role: 'window',
submenu: [
{
label: 'Close window',
accelerator: 'CmdOrCtrl+W',
role: 'close',
},
{ type: 'separator' },
{
label: 'sleek',
label: 'Open window',
click: () => {
handleCreateWindow();
}
Expand Down Expand Up @@ -261,6 +243,9 @@ function createMenu(files: FileObject[]) {
role: 'toggleDevTools',
label: 'Developer tools'
},
{
role: 'reload',
},
],
},
];
Expand Down
1 change: 0 additions & 1 deletion src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ const App = () => {
<GridComponent
todoObjects={todoObjects}
setTodoObject={setTodoObject}
attributes={attributes}
filters={filters}
setDialogOpen={setDialogOpen}
setContextMenu={setContextMenu}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/Dialog/AutoSuggest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const AutoSuggest: React.FC<AutoSuggestProps> = ({
setPrefix('+');
return Object.keys(attributes?.projects).filter((key) => key.includes(match));
} else {
return;
return [];
}
};

Expand Down
17 changes: 9 additions & 8 deletions src/renderer/Drawer/Attributes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef, KeyboardEvent, memo } from 'react';
import React, { useState, useEffect, useRef, memo } from 'react';
import { Accordion, AccordionSummary, AccordionDetails, Box, Button, Badge } from '@mui/material';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
Expand All @@ -25,15 +25,16 @@ const DrawerAttributes: React.FC<Props> = memo(({
filters,
t,
}) => {
const mustNotify: boolean = (todoObjects: TodoObject[]) => todoObjects.some((todoObject) => todoObject.notify);
const mustNotify = (attributes: Attribute[]) => attributes.some((attribute) => attribute.notify);

const [isCtrlKeyPressed, setIsCtrlKeyPressed] = useState(false);
const firstTabbableElementRef = useRef<HTMLDivElement | null>(null);

const isAttributesEmpty = (attributes: Attributes | null) => {
return !attributes || Object.values(attributes).every((attribute) => !Object.keys(attribute).length);
};

const handleCtrlCmdDown = (event: KeyboardEvent) => {
const handleCtrlCmdDown = (event: globalThis.KeyboardEvent) => {
if(event.ctrlKey || event.metaKey) {
setIsCtrlKeyPressed(true);
}
Expand All @@ -59,12 +60,12 @@ const DrawerAttributes: React.FC<Props> = memo(({
handleFocusFirstTabbableElement();

document.addEventListener('keydown', (event) => handleCtrlCmdDown(event));
document.addEventListener('keyup', (event) => handleCtrlCmdUp(event));
window.addEventListener('focus', (event) => handleCtrlCmdUp(event));
document.addEventListener('keyup', handleCtrlCmdUp);
window.addEventListener('focus', handleCtrlCmdUp);
return () => {
document.removeEventListener('keydown', () => handleCtrlCmdDown);
document.removeEventListener('keyup', () => handleCtrlCmdUp);
window.removeEventListener('focus', () => handleCtrlCmdUp);
document.removeEventListener('keydown', handleCtrlCmdDown);
document.removeEventListener('keyup', handleCtrlCmdUp);
window.removeEventListener('focus', handleCtrlCmdUp);
};
}, []);

Expand Down
4 changes: 2 additions & 2 deletions src/renderer/Grid/Group.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { memo } from 'react';
import { ListItem, Box, Button, Divider } from '@mui/material';

interface Props {
interface GroupProps {
value: string;
group: string;
filters: Filters | null;
onClick: Function;
}

const Group: React.FC<Props> = memo(({
const Group: React.FC<GroupProps> = memo(({
value,
group,
filters,
Expand Down
Loading

0 comments on commit 9cfef4c

Please sign in to comment.