From cb3f39ba0ac1acb204830cf35a633ac772de942a Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Mon, 17 May 2021 17:51:31 +0100 Subject: [PATCH 1/3] Catch reserved keywords and display modal --- .../file-explorer/src/lib/file-explorer.tsx | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index d925bc3f0b..4ce0c9cace 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -584,15 +584,31 @@ export const FileExplorer = (props: FileExplorerProps) => { modal('Validation Error', 'Special characters are not allowed', 'OK', () => {}) } else { if (state.focusEdit.isNew) { - state.focusEdit.type === 'file' ? createNewFile(joinPath(parentFolder, content)) : createNewFolder(joinPath(parentFolder, content)) - removeInputField(parentFolder)(dispatch) + if ((content === props.name) || (content.indexOf('gist-') === 0)) { + removeInputField(parentFolder)(dispatch) + modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { + label: 'Close', + fn: () => {} + }, null) + } else { + state.focusEdit.type === 'file' ? createNewFile(joinPath(parentFolder, content)) : createNewFolder(joinPath(parentFolder, content)) + removeInputField(parentFolder)(dispatch) + } } else { - const oldPath: string = state.focusEdit.element - const oldName = extractNameFromKey(oldPath) - const newPath = oldPath.replace(oldName, content) + if ((content === props.name) || (content.indexOf('gist-') === 0)) { + editRef.current.textContent = state.focusEdit.lastEdit + modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { + label: 'Close', + fn: () => {} + }, null) + } else { + const oldPath: string = state.focusEdit.element + const oldName = extractNameFromKey(oldPath) + const newPath = oldPath.replace(oldName, content) - editRef.current.textContent = extractNameFromKey(oldPath) - renamePath(oldPath, newPath) + editRef.current.textContent = extractNameFromKey(oldPath) + renamePath(oldPath, newPath) + } } setState(prevState => { return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } } From ebb0c674bb4c4e77f9dc5f8239ccd3bbe576e7fd Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 18 May 2021 10:46:08 +0100 Subject: [PATCH 2/3] Optimised mode for managing reserved words --- .../remix-ui/file-explorer/src/lib/file-explorer.tsx | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index 4ce0c9cace..57eda46094 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -97,7 +97,8 @@ export const FileExplorer = (props: FileExplorerProps) => { modals: [], toasterMsg: '', mouseOverElement: null, - showContextMenu: false + showContextMenu: false, + reservedKeywords: [name, 'gist-'] }) const [fileSystem, dispatch] = useReducer(fileSystemReducer, fileSystemInitialState) const editRef = useRef(null) @@ -222,6 +223,11 @@ export const FileExplorer = (props: FileExplorerProps) => { return keyPath.join('/') } + const hasReservedKeyword = (content: string): boolean => { + if (state.reservedKeywords.findIndex(value => content.startsWith(value)) !== -1) return true + else return false + } + const createNewFile = async (newFilePath: string) => { const fileManager = state.fileManager @@ -584,7 +590,7 @@ export const FileExplorer = (props: FileExplorerProps) => { modal('Validation Error', 'Special characters are not allowed', 'OK', () => {}) } else { if (state.focusEdit.isNew) { - if ((content === props.name) || (content.indexOf('gist-') === 0)) { + if (hasReservedKeyword(content)) { removeInputField(parentFolder)(dispatch) modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { label: 'Close', @@ -595,7 +601,7 @@ export const FileExplorer = (props: FileExplorerProps) => { removeInputField(parentFolder)(dispatch) } } else { - if ((content === props.name) || (content.indexOf('gist-') === 0)) { + if (hasReservedKeyword(content)) { editRef.current.textContent = state.focusEdit.lastEdit modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { label: 'Close', From 6850d0cc46752dda2031887cc666c64c766cccd7 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 18 May 2021 11:56:42 +0100 Subject: [PATCH 3/3] Update modal parameters --- libs/remix-ui/file-explorer/src/lib/file-explorer.tsx | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index 57eda46094..c471449ac4 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -592,10 +592,7 @@ export const FileExplorer = (props: FileExplorerProps) => { if (state.focusEdit.isNew) { if (hasReservedKeyword(content)) { removeInputField(parentFolder)(dispatch) - modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { - label: 'Close', - fn: () => {} - }, null) + modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, 'Close', () => {}) } else { state.focusEdit.type === 'file' ? createNewFile(joinPath(parentFolder, content)) : createNewFolder(joinPath(parentFolder, content)) removeInputField(parentFolder)(dispatch) @@ -603,10 +600,7 @@ export const FileExplorer = (props: FileExplorerProps) => { } else { if (hasReservedKeyword(content)) { editRef.current.textContent = state.focusEdit.lastEdit - modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, { - label: 'Close', - fn: () => {} - }, null) + modal('Reserved Keyword', `File name contains remix reserved keywords. '${content}'`, 'Close', () => {}) } else { const oldPath: string = state.focusEdit.element const oldName = extractNameFromKey(oldPath)