Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { useMemo, useState } from "react";
import { inject, observer } from "mobx-react";
 
import { useWindowSize } from "../../common/Utils/useWindowSize";
import { cn } from "../../utils/bem";
import { isDefined } from "../../utils/utilities";
import { Tool } from "./Tool";
import { ToolbarProvider } from "./ToolbarContext";
 
import "./FlyoutMenu.scss";
import "./Tool.scss";
import "./Toolbar.scss";
 
export const Toolbar = inject("store")(
  observer(({ store, tools, expanded }) => {
    const [toolbar, setToolbar] = useState(null);
    const windowSize = useWindowSize();
 
    const alignment = useMemo(() => {
      if (!isDefined(toolbar)) return "right";
 
      const bbox = toolbar.getBoundingClientRect();
 
      if (bbox.left < 200) {
        return "right";
      }
      if (windowSize.width - bbox.right < 200) {
        return "left";
      }
 
      return "right";
    }, [toolbar, windowSize]);
 
    const toolGroups = tools
      .filter((t) => !t.dynamic)
      .reduce((res, tool) => {
        const group = res[tool.group] ?? [];
 
        group.push(tool);
        res[tool.group] = group;
        return res;
      }, {});
 
    const smartTools = tools.filter((t) => t.dynamic);
 
    return (
      <ToolbarProvider value={{ expanded, alignment }}>
        <div ref={(el) => setToolbar(el)} className={cn("toolbar").mod({ alignment, expanded }).toClassName()}>
          {Object.entries(toolGroups).map(([name, tools], i) => {
            const visibleTools = tools.filter((t) => t.viewClass);
 
            return visibleTools.length ? (
              <div className={cn("toolbar").elem("group").toClassName()} key={`toolset-${name}-${i}`}>
                {visibleTools
                  .sort((a, b) => a.index - b.index)
                  .map((tool, i) => {
                    const ToolComponent = tool.viewClass;
 
                    return <ToolComponent key={`${tool.toolName}-${i}`} />;
                  })}
              </div>
            ) : null;
          })}
          {store.autoAnnotation && <SmartTools tools={smartTools} />}
        </div>
      </ToolbarProvider>
    );
  }),
);
 
const SmartTools = observer(({ tools }) => {
  const [selectedIndex, setSelectedIndex] = useState(
    Math.max(
      tools.findIndex((t) => t.selected),
      0,
    ),
  );
 
  const selected = useMemo(() => tools[selectedIndex], [selectedIndex]);
 
  const hasSelected = tools.some((t) => t.selected);
 
  return (
    tools.length > 0 && (
      <div className={cn("toolbar").elem("group").toClassName()}>
        <Tool
          smart
          label="Auto-Detect"
          active={hasSelected}
          icon={selected.iconClass}
          shortcut="tool:auto-detect"
          extra={
            tools.length > 1 ? (
              <div className={cn("toolbar").elem("smart").toClassName()}>
                {tools.map((t, i) => {
                  const ToolView = t.viewClass;
 
                  return (
                    <div
                      key={`${i}`}
                      onClickCapture={(e) => {
                        e.preventDefault();
                        setSelectedIndex(i);
                        t.manager.selectTool(t, true);
                      }}
                    >
                      <ToolView />
                    </div>
                  );
                })}
              </div>
            ) : null
          }
          controls={selected.controls}
          onClick={(e) => {
            let nextIndex = selectedIndex + 1;
 
            // if that's a smart button in extra block, it's already selected
            // if it's a hotkey handler, there are no `e` event
            if (e?.target?.closest(`.${cn("tool").elem("extra")}`)) return;
 
            if (!hasSelected) nextIndex = 0;
            else if (nextIndex >= tools.length) nextIndex = 0;
 
            const nextTool = tools[nextIndex];
 
            setSelectedIndex(nextIndex);
            nextTool.manager.selectTool(nextTool, true);
          }}
        />
      </div>
    )
  );
});