Skip to content

Commit 2097527

Browse files
author
himon
committed
Change order of annotation tools new property, annotationToolsIds, also persisting the selection of defaultToolId
1 parent 3d198dd commit 2097527

File tree

10 files changed

+1873
-1748
lines changed

10 files changed

+1873
-1748
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,20 @@ import VanillaFilerobotImageEditor from 'filerobot-image-editor';
435435
const { TABS, TOOLS } = VanillaFilerobotImageEditor;
436436
```
437437

438+
#### `annotationToolsIds`
439+
440+
<u>Type:</u> `string[]`
441+
442+
<u>Supported version:</u> +v4.0.0
443+
444+
<u>Default:</u> `[]`
445+
446+
The provided tools in annotation will be shown to the user, if empty array provided or left by default all tools in annotation will be shown.
447+
448+
```
449+
['Text', 'Image', 'Rect', 'Ellipse', 'Polygon', 'Pen', 'Line', 'Arrow']
450+
```
451+
438452
#### `defaultTabId`
439453

440454
<u>Type:</u> `string`

packages/react-filerobot-image-editor/src/actions/selectTab.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const selectTab = (state, payload) =>
99
: {
1010
...state,
1111
tabId: payload.tabId,
12-
toolId: TABS_TOOLS[payload.tabId][0],
12+
toolId: TABS_TOOLS[payload.tabId][payload.toolId],
1313
selectionsIds: [],
1414
pointerCssIcon:
1515
payload.tabId === TABS_IDS.ANNOTATE
1616
? POINTER_ICONS.DRAW
1717
: POINTER_ICONS.DEFAULT,
18-
};
18+
};
1919

2020
export default selectTab;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export const SELECT_TOOL = 'SELECT_TOOL';
22

3-
const selectTool = (state, payload) =>
4-
state.toolId === payload.toolId
3+
const selectTool = (state, payload) => {
4+
console.log('selectTool', {payload, state});
5+
return state.toolId === payload.toolId
56
? state
67
: {
78
...state,
89
toolId: payload.toolId,
910
selectionsIds: payload.keepSelections ? state.selectionsIds : [],
1011
};
12+
}
1113

1214
export default selectTool;

packages/react-filerobot-image-editor/src/components/Tabs/index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,27 @@ import { useStore } from 'hooks';
88
import { SELECT_TAB } from 'actions';
99
import TabItem from './TabItem';
1010
import { AVAILABLE_TABS } from './Tabs.constants';
11+
import { TABS_TOOLS } from 'components/tools/tools.constants';
1112

1213
const Tabs = ({ toggleMainMenu, isDrawer }) => {
1314
const {
1415
t,
1516
tabId = null,
17+
toolId,
1618
dispatch,
1719
config: { defaultTabId, tabsIds, useCloudimage },
1820
} = useStore();
1921

2022
const currentTabId = tabId || defaultTabId;
2123

2224
const selectTab = useCallback((newTabId) => {
25+
const annotateToolIdKey = defaultTabId === newTabId ? TABS_TOOLS.Annotate.indexOf(toolId) : 0;
26+
2327
dispatch({
2428
type: SELECT_TAB,
2529
payload: {
2630
tabId: newTabId,
31+
toolId: annotateToolIdKey
2732
},
2833
});
2934

packages/react-filerobot-image-editor/src/components/ToolsBar/index.jsx

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import PropTypes from 'prop-types';
44

55
/** Internal Depepdencneis */
66
import { SELECT_TOOL } from 'actions';
7-
import { TABS_TOOLS, TOOLS_ITEMS } from 'components/tools/tools.constants';
8-
import { TABS_IDS } from 'utils/constants';
7+
import { TABS_TOOLS, TOOLS_ITEMS, AVAILABLE_ANNOTATIONS_TOOLS } from 'components/tools/tools.constants';
8+
import { TABS_IDS, TOOLS_IDS } from 'utils/constants';
99
import { useStore } from 'hooks';
1010
import Carousel from 'components/common/Carousel';
1111
import { StyledToolsBar, StyledToolsBarItems } from './ToolsBar.styled';
@@ -21,7 +21,7 @@ const ToolsBar = ({ isPhoneScreen }) => {
2121
toolId,
2222
annotations,
2323
selectionsIds = [],
24-
config: { defaultTabId, defaultToolId, useCloudimage },
24+
config: { annotationToolsIds, defaultTabId, defaultToolId, useCloudimage },
2525
} = useStore();
2626
const currentTabId = tabId || defaultTabId;
2727
const currentToolId =
@@ -41,25 +41,46 @@ const ToolsBar = ({ isPhoneScreen }) => {
4141
});
4242
}, []);
4343

44-
const items = useMemo(
45-
() =>
46-
tabTools.map((id) => {
47-
const { Item, hideFn } = TOOLS_ITEMS[id];
44+
const items = useMemo(() => {
45+
console.log({TABS_IDS, TABS_TOOLS: TABS_TOOLS.Annotate, tabTools});
46+
const shouldShowTool = (id) =>
47+
!TOOLS_ITEMS[id]?.hideFn || !TOOLS_ITEMS[id].hideFn({ useCloudimage });
48+
49+
const renderItem = (id) => {
50+
const { Item, hideFn } = TOOLS_ITEMS[id];
51+
if (!Item || (hideFn && hideFn({ useCloudimage }))) return null;
52+
53+
return (
54+
<Item
55+
key={id}
56+
selectTool={selectTool}
57+
t={t}
58+
isSelected={currentToolId === id}
59+
/>
60+
);
61+
};
4862

49-
return (
50-
Item &&
51-
(!hideFn || !hideFn({ useCloudimage })) && (
52-
<Item
53-
key={id}
54-
selectTool={selectTool}
55-
t={t}
56-
isSelected={currentToolId === id}
57-
/>
58-
)
59-
);
60-
}),
61-
[tabTools, currentToolId],
62-
);
63+
if (currentTabId === TABS_IDS.ANNOTATE) {
64+
let orderedToolIds = [];
65+
66+
if (annotationToolsIds.length > 0) {
67+
orderedToolIds = TABS_TOOLS.Annotate.reduce((acc, id) => {
68+
const index = annotationToolsIds.indexOf(id);
69+
if (index !== -1) acc[index] = id;
70+
return acc;
71+
}, []);
72+
console.log({orderedToolIds});
73+
} else {
74+
orderedToolIds = [...TABS_TOOLS.Annotate];
75+
}
76+
77+
return (orderedToolIds.length ? orderedToolIds : TABS_TOOLS.Annotate)
78+
.filter(shouldShowTool)
79+
.map(renderItem);
80+
}
81+
82+
return tabTools.map(renderItem);
83+
}, [currentTabId, tabTools, annotationToolsIds, currentToolId, useCloudimage]);
6384

6485
const ToolOptionsComponent = useMemo(() => {
6586
if (!currentToolId) {

packages/react-filerobot-image-editor/src/context/defaultConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default {
9292
componentType: 'slider', // slider | buttons
9393
},
9494
tabsIds: [],
95+
annotationToolsIds: [],
9596
defaultTabId: TABS_IDS.ADJUST,
9697
defaultToolId: TOOLS_IDS.CROP,
9798
onClose: undefined,

packages/react-filerobot-image-editor/src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ export interface FilerobotImageEditorConfig {
292292
};
293293
// TABS_IDS
294294
tabsIds?: availableTabs[] | [];
295+
annotationToolsIds?: availableTools[] | [];
295296
defaultTabId?: availableTabs;
296297
defaultToolId?: availableTools;
297298
onBeforeSave?: (savedImageData: savedImageData) => void | boolean;

public/demo-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ const config = {
351351
// forceToPngInEllipticalCrop: false, // in case the develop wants to force the saved image to be PNG if there is elliptical crop is done otherwise the provided savedImageType would be used.
352352
// onClose: () => console.log('Act closing 👅'), // if we have value then close button will be shown unless showBackButton is true then if onClose has value the back button will be shown otherwise nothing will be shown.
353353
// tabsIds: [TABS.ADJUST, TABS.WATERMARK],
354+
// annotationToolsIds: [],
354355
// savingPixelRatio: 4,
355356
previewPixelRatio: window.devicePixelRatio * 4,
356357
// defaultTabId: TABS.ADJUST,

public/init.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const copyButtons = document.querySelectorAll('.copy-button');
2727
const accordions = document.querySelectorAll('[data-accordion]');
2828

2929
let useCloudimage = false;
30-
const { TABS } = FilerobotImageEditor;
30+
const { TABS, TOOLS } = FilerobotImageEditor;
3131

3232
const EXAMPLE_CODE_TABS = {
3333
'js-code-tab': jsCodeWrapper,
@@ -50,6 +50,17 @@ const selectedTabs = [
5050
TABS.RESIZE,
5151
];
5252

53+
const selectedAnnotationTools = [
54+
TOOLS.TEXT,
55+
TOOLS.IMAGE,
56+
TOOLS.RECT,
57+
TOOLS.ELLIPSE,
58+
TOOLS.POLYGON,
59+
TOOLS.PEN,
60+
TOOLS.LINE,
61+
TOOLS.ARROW,
62+
];
63+
5364
const IMG_EDITOR_TABS = {
5465
adjust: TABS.ADJUST,
5566
finetune: TABS.FINETUNE,
@@ -63,6 +74,7 @@ const pluginConfig = {
6374
...config,
6475
source: 'https://scaleflex.cloudimg.io/v7/demo/river.png',
6576
tabsIds: selectedTabs,
77+
annotationToolsIds: selectedAnnotationTools,
6678
defaultTabId: TABS.ADJUST,
6779
defaultToolId: null,
6880
observePluginContainerSize: true,
@@ -207,7 +219,6 @@ function changeModeHandler() {
207219

208220
useCloudimage = false;
209221
}
210-
211222
filerobotImageEditor.render({ useCloudimage, tabsIds: [...selectedTabs] });
212223
}
213224

0 commit comments

Comments
 (0)