From 855dceaafe05762d9d0bd2959918098d244655e9 Mon Sep 17 00:00:00 2001 From: Neelesh Date: Tue, 4 Aug 2020 00:26:31 +0530 Subject: [PATCH 1/5] Order files alphabetically in sidebar and nested folders. This commit solves issue #704. I added a function to get sorted children of the parent file when ever a file is created of renamed. This function is only called on the parent of the file that is create or renamed. --- client/modules/IDE/reducers/files.js | 35 ++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 7710074d..9c07cbbc 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -45,7 +45,7 @@ const initialState = () => { name: 'root', id: r, _id: r, - children: [a, b, c], + children: [b, a, c], fileType: 'folder', content: '' }, @@ -110,6 +110,12 @@ function deleteMany(state, ids) { return newState; } +function sortedChildrenId(state, children) { + const childrenArray = state.filter(file => children.includes(file.id)); + childrenArray.sort((a, b) => (a.name > b.name ? 1 : -1)); + return childrenArray.map(child => child.id); +} + const files = (state, action) => { if (state === undefined) { state = initialState(); // eslint-disable-line @@ -138,7 +144,7 @@ const files = (state, action) => { return initialState(); case ActionTypes.CREATE_FILE: // eslint-disable-line { - const newState = state.map((file) => { + let newState = state.map((file) => { if (file.id === action.parentId) { const newFile = Object.assign({}, file); newFile.children = [...newFile.children, action.id]; @@ -146,7 +152,8 @@ const files = (state, action) => { } return file; }); - return [...newState, + newState = [ + ...newState, { name: action.name, id: action.id, @@ -154,17 +161,31 @@ const files = (state, action) => { content: action.content, url: action.url, children: action.children, - fileType: action.fileType || 'file' - }]; + fileType: action.fileType || 'file', + }, + ]; + return newState.map((file) => { + if (file.id === action.parentId) { + file.children = sortedChildrenId(newState, file.children); + } + return file; + }); } case ActionTypes.UPDATE_FILE_NAME: - return state.map((file) => { + { + const newState = state.map((file) => { if (file.id !== action.id) { return file; } - return Object.assign({}, file, { name: action.name }); }); + return newState.map((file) => { + if (file.children.includes(action.id)) { + file.children = sortedChildrenId(newState, file.children); + } + return file; + }); + } case ActionTypes.DELETE_FILE: { const newState = deleteMany(state, [action.id, ...getAllDescendantIds(state, action.id)]); From 4b022dd0e861ce312e6eb48ec0190e7362cd03d3 Mon Sep 17 00:00:00 2001 From: Neelesh Date: Tue, 4 Aug 2020 10:28:21 +0530 Subject: [PATCH 2/5] Refactored CREATE_FILE and UPDATE_FILE_NAME case into smaller functions --- client/modules/IDE/reducers/files.js | 44 ++++++++++++++++------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 9c07cbbc..1f03e8de 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -116,6 +116,26 @@ function sortedChildrenId(state, children) { return childrenArray.map(child => child.id); } +function udpateParent(state, action) { + return state.map((file) => { + if (file.id === action.parentId) { + const newFile = Object.assign({}, file); + newFile.children = [...newFile.children, action.id]; + return newFile; + } + return file; + }); +} + +function renameFile(state, action) { + return state.map((file) => { + if (file.id !== action.id) { + return file; + } + return Object.assign({}, file, { name: action.name }); + }); +} + const files = (state, action) => { if (state === undefined) { state = initialState(); // eslint-disable-line @@ -144,16 +164,8 @@ const files = (state, action) => { return initialState(); case ActionTypes.CREATE_FILE: // eslint-disable-line { - let newState = state.map((file) => { - if (file.id === action.parentId) { - const newFile = Object.assign({}, file); - newFile.children = [...newFile.children, action.id]; - return newFile; - } - return file; - }); - newState = [ - ...newState, + const newState = [ + ...udpateParent(state, action), { name: action.name, id: action.id, @@ -161,9 +173,8 @@ const files = (state, action) => { content: action.content, url: action.url, children: action.children, - fileType: action.fileType || 'file', - }, - ]; + fileType: action.fileType || 'file' + }]; return newState.map((file) => { if (file.id === action.parentId) { file.children = sortedChildrenId(newState, file.children); @@ -173,12 +184,7 @@ const files = (state, action) => { } case ActionTypes.UPDATE_FILE_NAME: { - const newState = state.map((file) => { - if (file.id !== action.id) { - return file; - } - return Object.assign({}, file, { name: action.name }); - }); + const newState = renameFile(state, action); return newState.map((file) => { if (file.children.includes(action.id)) { file.children = sortedChildrenId(newState, file.children); From 932f24cd9e2b43ca8374e299169719614e2d4c5d Mon Sep 17 00:00:00 2001 From: Neelesh Date: Tue, 4 Aug 2020 21:54:45 +0530 Subject: [PATCH 3/5] corrected a typo --- client/modules/IDE/reducers/files.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 1f03e8de..9be4667e 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -116,7 +116,7 @@ function sortedChildrenId(state, children) { return childrenArray.map(child => child.id); } -function udpateParent(state, action) { +function updateParent(state, action) { return state.map((file) => { if (file.id === action.parentId) { const newFile = Object.assign({}, file); @@ -165,7 +165,7 @@ const files = (state, action) => { case ActionTypes.CREATE_FILE: // eslint-disable-line { const newState = [ - ...udpateParent(state, action), + ...updateParent(state, action), { name: action.name, id: action.id, From 1c02d45fd72f73e66f3585fa586e1b31138a953b Mon Sep 17 00:00:00 2001 From: Neelesh Singh Date: Tue, 4 Aug 2020 23:46:25 +0530 Subject: [PATCH 4/5] sort existing files before displaying --- client/modules/IDE/reducers/files.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 9be4667e..404a1aee 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -227,7 +227,12 @@ const files = (state, action) => { return file; }); default: - return state; + return state.map((file) => { + if (file.name === 'root') { + file.children = sortedChildrenId(state, file.children); + } + return file; + }); } }; From 9c8a8a411053472b48f5d02057c5688b96438b62 Mon Sep 17 00:00:00 2001 From: Neelesh Singh Date: Wed, 5 Aug 2020 00:32:02 +0530 Subject: [PATCH 5/5] Sort nested folders in existing sketch --- client/modules/IDE/reducers/files.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index 404a1aee..80557fb3 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -228,9 +228,7 @@ const files = (state, action) => { }); default: return state.map((file) => { - if (file.name === 'root') { - file.children = sortedChildrenId(state, file.children); - } + file.children = sortedChildrenId(state, file.children); return file; }); }