Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Add extension enabled prop (#1893)

Stanimir73a1182440a8
diff --git a/app/components/dialogs/settings/SettingsExtensions.tsx b/app/components/dialogs/settings/SettingsExtensions.tsx
index a7d02a009..c83d5395f 100644
--- a/app/components/dialogs/settings/SettingsExtensions.tsx
+++ b/app/components/dialogs/settings/SettingsExtensions.tsx
@@ -16,7 +16,7 @@
*
*/
-import React, { useRef, useState } from 'react';
+import React, { ChangeEvent, useRef, useState } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Accordion, AccordionDetails, AccordionSummary } from '@mui/material';
@@ -27,6 +27,7 @@ import Box from '@mui/material/Box';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import IconButton from '@mui/material/IconButton';
+import Switch from '@mui/material/Switch';
import Button from '@mui/material/Button';
import DeleteIcon from '@mui/icons-material/Delete';
import { Progress } from 'aws-sdk/clients/s3';
@@ -35,7 +36,6 @@ import { actions as AppActions, getExtensions } from '-/reducers/app';
import IOActions from '-/reducers/io-actions';
import { TS } from '-/tagspaces.namespace';
import i18n from '-/services/i18n';
-import PlatformFacade from '-/services/platform-facade';
import PlatformIO from '-/services/platform-facade';
import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
@@ -43,7 +43,9 @@ interface Props {
extension: Array<TS.Extension>;
isDevMode: boolean;
removeExtension: (extensionId: string) => void;
+ updateExtension: (extension: TS.Extension) => void;
removeSupportedFileTypes: (extensionId: string) => void;
+ enableExtension: (extensionId: string, enabled: boolean) => void;
resetProgress: () => void;
onUploadProgress: (progress: Progress, response: any) => void;
toggleUploadDialog: () => void;
@@ -64,29 +66,29 @@ function SettingsExtensions(props: Props) {
function handleFileInputChange(selection: any) {
const files: File[] = Array.from(selection.currentTarget.files);
- PlatformFacade.getUserDataDir().then(dataDir => {
+ PlatformIO.getUserDataDir().then(dataDir => {
props.resetProgress();
props.toggleUploadDialog();
PlatformIO.disableObjectStoreSupport();
PlatformIO.disableWebdavSupport();
const destinationPath =
- dataDir + PlatformFacade.getDirSeparator() + 'tsplugins';
+ dataDir + PlatformIO.getDirSeparator() + 'tsplugins';
props
.uploadFilesAPI(files, destinationPath, props.onUploadProgress, false)
.then(fsEntries => {
const targetPath =
destinationPath +
- PlatformFacade.getDirSeparator() +
+ PlatformIO.getDirSeparator() +
'@tagspaces' +
- PlatformFacade.getDirSeparator() +
+ PlatformIO.getDirSeparator() +
'extensions';
const promises = fsEntries.map(fsEntry =>
- PlatformFacade.unZip(fsEntry.path, targetPath)
+ PlatformIO.unZip(fsEntry.path, targetPath)
);
// fsEntries.name.substring(0, fsEntries.extension.length + 1);
return Promise.all(promises).then(paths => {
- PlatformFacade.loadExtensions();
- paths.forEach(path => PlatformFacade.deleteFilePromise(path));
+ PlatformIO.loadExtensions();
+ paths.forEach(path => PlatformIO.deleteFilePromise(path));
return props.switchCurrentLocationType();
});
})
@@ -136,6 +138,29 @@ function SettingsExtensions(props: Props) {
.map(ext => (
<ListItem key={ext.extensionId} disablePadding>
{ext.extensionName} ({ext.version})
+ <Switch
+ data-tid="enableExtensionTID"
+ name="enableExtension"
+ checked={ext.extensionEnabled}
+ onChange={(event: ChangeEvent<HTMLInputElement>) => {
+ if (event.target.checked) {
+ props.updateExtension({
+ ...ext,
+ extensionEnabled: true
+ });
+ props.enableExtension(ext.extensionId, true);
+ //reload default file types
+ PlatformIO.loadExtensions();
+ } else {
+ props.updateExtension({
+ ...ext,
+ extensionEnabled: false
+ });
+ props.enableExtension(ext.extensionId, false);
+ props.removeSupportedFileTypes(ext.extensionId);
+ }
+ }}
+ />
<Tooltip title={i18n.t('core:removeExtension')}>
<IconButton
aria-label={i18n.t('core:delete')}
@@ -166,9 +191,7 @@ function SettingsExtensions(props: Props) {
props.removeSupportedFileTypes(
removeExtDialogOpened.extensionId
);
- PlatformFacade.removeExtension(
- removeExtDialogOpened.extensionId
- );
+ PlatformIO.removeExtension(removeExtDialogOpened.extensionId);
}
}}
cancelDialogTID="cancelRemoveExtDialogTID"
@@ -214,7 +237,9 @@ function mapActionCreatorsToProps(dispatch) {
return bindActionCreators(
{
removeExtension: AppActions.removeExtension,
+ updateExtension: AppActions.updateExtension,
removeSupportedFileTypes: SettingsActions.removeSupportedFileTypes,
+ enableExtension: SettingsActions.enableExtension,
onUploadProgress: AppActions.onUploadProgress,
uploadFilesAPI: IOActions.uploadFilesAPI,
toggleUploadDialog: AppActions.toggleUploadDialog,
diff --git a/app/components/dialogs/settings/SettingsFileTypes.tsx b/app/components/dialogs/settings/SettingsFileTypes.tsx
index 9e68a79de..fce8fe2f1 100644
--- a/app/components/dialogs/settings/SettingsFileTypes.tsx
+++ b/app/components/dialogs/settings/SettingsFileTypes.tsx
@@ -55,6 +55,7 @@ import ConfirmDialog from '-/components/dialogs/ConfirmDialog';
import PlatformFacade from '-/services/platform-facade';
import { getExtensions } from '-/reducers/app';
import { supportedFileTypes } from '-/extension-config';
+import useFirstRender from '-/utils/useFirstRender';
const styles: any = (theme: any) => ({
fileTypeColorDialog: {
@@ -95,16 +96,21 @@ function SettingsFileTypes(props: Props) {
const settingsFileTypeRef = useRef<TableVirtuosoHandle>(null);
const [ignored, forceUpdate] = useReducer(x => x + 1, 0);
+ const firstRender = useFirstRender();
useEffect(() => {
- items.current = props.supportedFileTypes;
- isValidationInProgress.current = false;
- const timer = scrollToItem(selectedItem.current);
- return () => {
- if (timer) {
- clearTimeout(timer);
+ if (!firstRender) {
+ items.current = props.supportedFileTypes;
+ isValidationInProgress.current = false;
+ if (selectedItem.current !== undefined) {
+ const timer = scrollToItem(selectedItem.current);
+ return () => {
+ if (timer) {
+ clearTimeout(timer);
+ }
+ };
}
- };
+ }
}, [props.supportedFileTypes]);
type ScrollToIndexArgs = {
diff --git a/app/reducers/app.ts b/app/reducers/app.ts
index 24a4edf9e..d2fd4d417 100644
--- a/app/reducers/app.ts
+++ b/app/reducers/app.ts
@@ -156,9 +156,9 @@ export const types = {
UPDATE_CURRENTDIR_ENTRIES: 'APP/UPDATE_CURRENTDIR_ENTRIES',
REFLECT_EDITED_ENTRY_PATHS: 'APP/REFLECT_EDITED_ENTRY_PATHS',
SET_ISLOADING: 'APP/SET_ISLOADING',
- ADD_EXTENSION: 'APP/ADD_EXTENSION',
ADD_EXTENSIONS: 'APP/ADD_EXTENSIONS',
- REMOVE_EXTENSIONS: 'APP/REMOVE_EXTENSIONS'
+ REMOVE_EXTENSIONS: 'APP/REMOVE_EXTENSIONS',
+ UPDATE_EXTENSION: 'APP/UPDATE_EXTENSION'
};
export const NotificationTypes = {
@@ -933,16 +933,22 @@ export default (state: any = initialState, action: any) => {
};
}
case types.ADD_EXTENSIONS: {
+ const extensions = mergeByProp(
+ state.extensions,
+ action.extensions,
+ 'extensionId'
+ );
return {
...state,
- extensions: mergeByProp(
- state.extensions,
- action.extensions,
- 'extensionId'
- )
+ extensions: extensions.map(ext => {
+ if (action.enabledExtensions.includes(ext.extensionId)) {
+ return { ...ext, extensionEnabled: true };
+ }
+ return ext;
+ })
};
}
- case types.ADD_EXTENSION: {
+ case types.UPDATE_EXTENSION: {
return {
...state,
extensions: mergeByProp(
@@ -980,18 +986,29 @@ function disableBackGestureMac() {
}
export const actions = {
- addExtension: (extension: TS.Extension) => ({
- type: types.ADD_EXTENSION,
- extension
- }),
- addExtensions: (extensions: Array<TS.Extension>) => ({
+ addExtensions: (extensions: Array<TS.Extension>) => (
+ dispatch: (action) => void,
+ getState: () => any
+ ) => {
+ const { settings } = getState();
+ dispatch(actions.addExtensionsInt(extensions, settings.enabledExtensions));
+ },
+ addExtensionsInt: (
+ extensions: Array<TS.Extension>,
+ enabledExtensions: Array<string>
+ ) => ({
type: types.ADD_EXTENSIONS,
- extensions
+ extensions,
+ enabledExtensions
}),
removeExtension: (extensionId: string) => ({
type: types.REMOVE_EXTENSIONS,
extensionId
}),
+ updateExtension: (extension: TS.Extension) => ({
+ type: types.UPDATE_EXTENSION,
+ extension
+ }),
setLastBackgroundImageChange: (folderPath, lastBackgroundImageChange) => ({
type: types.LAST_BACKGROUND_IMAGE_CHANGE,
folderPath,
diff --git a/app/reducers/settings-default.ts b/app/reducers/settings-default.ts
index 0382df162..83eefc1d8 100644
--- a/app/reducers/settings-default.ts
+++ b/app/reducers/settings-default.ts
@@ -220,6 +220,7 @@ export default {
],
keyBindings: keyBindings(AppConfig.isMacLike),
supportedFileTypes: supportedFileTypes,
+ enabledExtensions: [],
mapTileServers: [
{
uuid: getUuid(),
diff --git a/app/reducers/settings.ts b/app/reducers/settings.ts
index 45ec7f926..5fb7ca9d1 100644
--- a/app/reducers/settings.ts
+++ b/app/reducers/settings.ts
@@ -83,6 +83,7 @@ export const types = {
SET_SUPPORTED_FILE_TYPES: 'SETTINGS/SET_SUPPORTED_FILE_TYPES',
ADD_SUPPORTED_FILE_TYPES: 'SETTINGS/ADD_SUPPORTED_FILE_TYPES',
REMOVE_SUPPORTED_FILE_TYPES: 'SETTINGS/REMOVE_SUPPORTED_FILE_TYPES',
+ ENABLE_EXTENSION: 'SETTINGS/ENABLE_EXTENSION',
SET_LAST_PUBLISHED_VERSION: 'SETTINGS/SET_LAST_PUBLISHED_VERSION',
SET_ENTRY_PROPERTIES_SPLIT_SIZE: 'SETTINGS/SET_ENTRY_PROPERTIES_SPLIT_SIZE',
SET_MAIN_VSPLIT_SIZE: 'SETTINGS/SET_MAIN_VSPLIT_SIZE',
@@ -337,6 +338,34 @@ export default (state: any = defaultSettings, action: any) => {
)
};
}
+ case types.ENABLE_EXTENSION: {
+ let enabledExtensions;
+ let supportedFileTypes;
+ if (action.enabled) {
+ if (!state.enabledExtensions.includes(action.extensionId)) {
+ enabledExtensions = [...state.enabledExtensions, action.extensionId];
+ } else {
+ enabledExtensions = [...state.enabledExtensions];
+ }
+ } else {
+ enabledExtensions = state.enabledExtensions.filter(
+ extensionId => extensionId === action.extensionId
+ );
+ supportedFileTypes = state.supportedFileTypes.filter(
+ fType =>
+ !(
+ fType.viewer === action.extensionId &&
+ fType.editor === action.extensionId
+ )
+ );
+ }
+
+ return {
+ ...state,
+ enabledExtensions: enabledExtensions,
+ ...(supportedFileTypes && { supportedFileTypes: supportedFileTypes })
+ };
+ }
case types.REMOVE_SUPPORTED_FILE_TYPES: {
const supportedFileTypes = state.supportedFileTypes.map(
(fType: TS.FileTypes) => ({
@@ -653,14 +682,23 @@ export const actions = {
type: types.SET_GLOBAL_KEYBINDING,
enableGlobalKeyboardShortcuts
}),
- addSupportedFileTypes: (supportedFileTypes: []) => ({
+ addSupportedFileTypes: (
+ supportedFileTypes: Array<TS.FileTypes>,
+ override = false
+ ) => ({
type: types.ADD_SUPPORTED_FILE_TYPES,
- supportedFileTypes
+ supportedFileTypes,
+ override
}),
removeSupportedFileTypes: (extensionId: string) => ({
type: types.REMOVE_SUPPORTED_FILE_TYPES,
extensionId
}),
+ enableExtension: (extensionId: string, enabled: boolean) => ({
+ type: types.ENABLE_EXTENSION,
+ extensionId,
+ enabled
+ }),
setSupportedFileTypes: (supportedFileTypes: []) => ({
type: types.SET_SUPPORTED_FILE_TYPES,
supportedFileTypes
diff --git a/app/tagspaces.namespace.d.ts b/app/tagspaces.namespace.d.ts
index 8c589d8d1..0e1dd121a 100644
--- a/app/tagspaces.namespace.d.ts
+++ b/app/tagspaces.namespace.d.ts
@@ -52,6 +52,7 @@ export namespace TS {
extensionName: string,
extensionTypes: Array<string>,
extensionExternal?: boolean,
+ extensionEnabled: boolean,
color?: string,
version: string,
}
diff --git a/app/utils/extension-utils.js b/app/utils/extension-utils.js
index b314c23d6..490e45895 100644
--- a/app/utils/extension-utils.js
+++ b/app/utils/extension-utils.js
@@ -46,6 +46,7 @@ function processDirs(directoryPath, dirs, isExternal = false) {
color,
fileTypes,
buildFolder,
+ enabled,
...props
} = packageJsonObj['tsextension'];
@@ -81,45 +82,12 @@ function processDirs(directoryPath, dirs, isExternal = false) {
}
});
}
- /*if (fileTypesEdit) {
- fileTypesEdit.forEach(fileType => {
- const existingItemIndex = supportedFileTypes.findIndex(
- item => item.type === fileType
- );
- if (existingItemIndex !== -1) {
- // If an item with the same id already exists, update its properties
- supportedFileTypes[existingItemIndex].editor = extensionId;
- } else {
- // If the item doesn't exist, add it to the array
- supportedFileTypes.push({
- type: fileType,
- editor: extensionId
- });
- }
- });
- }
- if (fileTypesView) {
- fileTypesView.forEach(fileType => {
- const existingItemIndex = supportedFileTypes.findIndex(
- item => item.type === fileType
- );
- if (existingItemIndex !== -1) {
- // If an item with the same id already exists, update its properties
- supportedFileTypes[existingItemIndex].viewer = extensionId;
- } else {
- // If the item doesn't exist, add it to the array
- supportedFileTypes.push({
- type: fileType,
- viewer: extensionId
- });
- }
- });
- }*/
return {
extensionId: extensionId,
extensionName: name,
extensionTypes: types,
...(isExternal && { extensionExternal: true }),
+ extensionEnabled: enabled !== undefined ? enabled : !isExternal,
version: version,
...props
};