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

Add support for duplicating todo items #719

Open
wants to merge 1 commit into
base: 2.0.14
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
9 changes: 8 additions & 1 deletion src/__tests__/main/Write.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { prepareContentForWriting, removeLineFromFile } from '../../main/modules/File/Write';
import { prepareContentForWriting, removeLineFromFile, duplicateRecord } from '../../main/modules/File/Write';
import { config } from '../../main/config';
import dayjs from 'dayjs';

Expand Down Expand Up @@ -157,4 +157,11 @@ describe('Writing to file', () => {
expect(fileContent).toEqual(`Line 1\nEdited line\nMulti line 1Multi line 2Multi line 3\nUpdated line\nAppend 1\nAppend 2\nNew line with relative threshold date t:June 3rd, 2005\nLine4\nLine5\nLine6\nMulti line 1Multi line 2Multi line 3`);
config.get = originalGet;
});

test('should duplicate the second line and add it to the end of the file', () => {
duplicateRecord(1);
const fileContent = fs.readFileSync('./src/__tests__/__mock__/test.txt', 'utf8');
expect(fileContent).toEqual(`Line 1\nEdited line\nMulti line 1Multi line 2Multi line 3\nUpdated line\nAppend 1\nAppend 2\nNew line with relative threshold date t:June 3rd, 2005\nLine4\nLine5\nLine6\nMulti line 1Multi line 2Multi line 3\nEdited line`);
});

});
1 change: 1 addition & 0 deletions src/locales/cs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Kopírovat",
"duplicate": "Duplikovat",
"delete": "Smazat",
"cancel": "Zrušit",
"openFile": "Otevřít soubor",
Expand Down
1 change: 1 addition & 0 deletions src/locales/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Kopieren",
"duplicate": "Doppelter",
"delete": "Löschen",
"cancel": "Abbrechen",
"openFile": "Datei öffnen",
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Copy",
"duplicate": "Duplicate",
"delete": "Delete",
"cancel": "Cancel",
"openFile": "Open file",
Expand Down
1 change: 1 addition & 0 deletions src/locales/es.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Copiar",
"duplicate": "Duplicado",
"delete": "Eliminar",
"cancel": "Cancelar",
"openFile": "Abrir archivo",
Expand Down
1 change: 1 addition & 0 deletions src/locales/fr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Copier",
"duplicate": "Dupliquer",
"delete": "Supprimer",
"cancel": "Annuler",
"openFile": "Ouvrir un fichier",
Expand Down
1 change: 1 addition & 0 deletions src/locales/hi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "कॉपी करें",
"duplicate": "डुप्लिकेट",
"delete": "हटाएं",
"cancel": "रद्द करें",
"openFile": "फ़ाइल खोलें",
Expand Down
1 change: 1 addition & 0 deletions src/locales/hu.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Másolás",
"duplicate": "Megkettőzés",
"delete": "Törlés",
"cancel": "Mégse",
"openFile": "Fájl megnyitása",
Expand Down
1 change: 1 addition & 0 deletions src/locales/it.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Copia",
"duplicate": "Duplicare",
"delete": "Elimina",
"cancel": "Annulla",
"openFile": "Apri file",
Expand Down
1 change: 1 addition & 0 deletions src/locales/jp.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "コピー",
"duplicate": "複製",
"delete": "削除",
"cancel": "キャンセル",
"openFile": "ファイルを開く",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ko.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "복사",
"duplicate": "복제하다",
"delete": "삭제",
"cancel": "취소",
"openFile": "파일 열기",
Expand Down
1 change: 1 addition & 0 deletions src/locales/pl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Kopiuj",
"duplicate": "Duplikować",
"delete": "Usuń",
"cancel": "Anuluj",
"openFile": "Otwórz plik",
Expand Down
1 change: 1 addition & 0 deletions src/locales/pt.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Copiar",
"duplicate": "Duplicado",
"delete": "Eliminar",
"cancel": "Cancelar",
"openFile": "Abrir ficheiro",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ru.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Копировать",
"duplicate": "Дубликат",
"delete": "Удалить",
"cancel": "Отменить",
"openFile": "Открыть файл",
Expand Down
1 change: 1 addition & 0 deletions src/locales/tr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "Kopyala",
"duplicate": "Kopyalamak",
"delete": "Sil",
"cancel": "İptal",
"openFile": "Dosyasını aç",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"copy": "复制",
"duplicate": "重复",
"delete": "删除",
"cancel": "取消",
"openFile": "打开 文件",
Expand Down
12 changes: 11 additions & 1 deletion src/main/modules/File/Write.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function removeLineFromFile(lineNumber: number) {
}
}

function duplicateRecord(lineNumber: number) {
let activeFile: FileObject | null = getActiveFile();
if(!activeFile) {
throw new Error('No active file found');
} else if(lineNumber >= 0 && lineNumber < linesInFile.length) {
linesInFile.push(linesInFile[lineNumber]);
writeToFile(linesInFile.join('\n'), activeFile.todoFilePath, activeFile.todoFileBookmark);
}
}

function prepareContentForWriting(lineNumber: number, string: string) {
const activeFile: FileObject | null = getActiveFile();
if(!activeFile) {
Expand Down Expand Up @@ -63,4 +73,4 @@ function prepareContentForWriting(lineNumber: number, string: string) {
writeToFile(linesInFile.join('\n'), activeFile.todoFilePath, activeFile.todoFileBookmark);
}

export { prepareContentForWriting, removeLineFromFile, writeToFile };
export { prepareContentForWriting, removeLineFromFile, duplicateRecord, writeToFile };
12 changes: 11 additions & 1 deletion src/main/modules/IpcMain.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ipcMain, app, IpcMainEvent, clipboard, shell } from 'electron';
import { dataRequest } from './DataRequest/DataRequest';
import { changeCompleteState } from './DataRequest/ChangeCompleteState';
import { prepareContentForWriting, removeLineFromFile } from './File/Write';
import { prepareContentForWriting, removeLineFromFile, duplicateRecord } from './File/Write';
import { archiveTodos, handleRequestArchive } from './File/Archive';
import { config, filter, notifiedTodoObjectsStorage } from '../config';
import { handleError } from '../util';
Expand Down Expand Up @@ -156,6 +156,14 @@ function handleRemoveLineFromFile(event: IpcMainEvent, index: number) {
}
}

function handleDuplicateRecord(event: IpcMainEvent, index: number) {
try {
duplicateRecord(index);
} catch(error: any) {
handleError(error);
}
}

function handleArchiveTodos(event: IpcMainEvent): void {
try {
const archivingResult = archiveTodos();
Expand Down Expand Up @@ -202,6 +210,7 @@ function removeEventListeners(): void {
ipcMain.off('saveToClipboard', handleSaveToClipboard);
ipcMain.off('revealInFileManager', handleRevealInFileManager);
ipcMain.off('removeLineFromFile', handleRemoveLineFromFile);
ipcMain.off('duplicateRecord', handleDuplicateRecord);
ipcMain.off('updateTodoObject', handleUpdateTodoObject);
ipcMain.off('requestArchive', handleRequestArchive);
}
Expand All @@ -227,5 +236,6 @@ ipcMain.on('droppedFile', handleDroppedFile);
ipcMain.on('saveToClipboard', handleSaveToClipboard);
ipcMain.on('revealInFileManager', handleRevealInFileManager);
ipcMain.on('removeLineFromFile', handleRemoveLineFromFile);
ipcMain.on('duplicateRecord', handleDuplicateRecord);
ipcMain.on('updateTodoObject', handleUpdateTodoObject);
ipcMain.on('requestArchive', handleRequestArchive);
8 changes: 8 additions & 0 deletions src/renderer/Grid/Row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const Row: React.FC<Props> = memo(({
const handleSaveToClipboard = () => {
if(todoObject) ipcRenderer.send('saveToClipboard', todoObject?.string);
};
const handleDuplicateRecord = () => {
if(todoObject) ipcRenderer.send('duplicateRecord', todoObject?.lineNumber);
};

const handleContextMenu = (event: React.MouseEvent, todoString: string) => {
setContextMenu({
Expand All @@ -52,6 +55,11 @@ const Row: React.FC<Props> = memo(({
label: t('copy'),
function: handleSaveToClipboard,
},
{
id: 'duplicate',
label: t('duplicate'),
function: handleDuplicateRecord,
},
{
id: 'delete',
label: t('delete'),
Expand Down