Bin
2025-12-17 262fecaa75b2909ad244f12c3b079ed3ff4ae329
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
135
136
137
138
139
140
141
142
143
144
145
146
147
import { observer } from "mobx-react";
import { types, getParent } from "mobx-state-tree";
 
import NormalizationMixin from "../mixins/Normalization";
import RegionsMixin from "../mixins/Regions";
import Registry from "../core/Registry";
// import { CustomTagModel } from "../tags/Custom";
import { guidGenerator } from "../core/Helpers";
 
import { HtxTextBox } from "../components/HtxTextBox/HtxTextBox";
import { cn } from "../utils/bem";
 
const Model = types
  .model("CustomRegionModel", {
    id: types.optional(types.identifier, guidGenerator),
    pid: types.optional(types.string, guidGenerator),
    type: "customregion",
 
    _value: types.frozen(),
    // states: types.array(types.union(ChoicesModel)),
  })
  .volatile(() => ({
    classification: true,
    perRegionTags: [],
    results: [],
    selected: false,
  }))
  .views((self) => ({
    get parent() {
      // Since getParentOfType might not work, we'll use getParent which should work
      // for any MobX parent-child relationship
      return getParent(self);
    },
    getRegionElement() {
      return document.querySelector(`#CustomRegion-${self.id}`);
    },
    getOneColor() {
      return null;
    },
  }))
  .actions((self) => ({
    setValue(val) {
      if (self._value === val || !self.parent.validateText(val)) return;
 
      self._value = val;
      self.parent.onChange();
    },
 
    updateValue(newValue) {
      // This is the sanctioned way to update the region's value
      // from an external component like the POC UI.
      self._value = newValue;
      // Also notify the parent about the change so it can update results
      if (self.parent.updateResult) {
        self.parent.updateResult();
      }
    },
 
    deleteRegion() {
      self.parent.remove(self);
    },
 
    selectRegion() {
      self.selected = true;
    },
 
    afterUnselectRegion() {
      self.selected = false;
    },
  }));
 
const CustomRegionModel = types.compose("CustomRegionModel", RegionsMixin, NormalizationMixin, Model);
 
const HtxCustomRegionView = ({ item, onFocus }) => {
  const classes = [styles.mark];
  const params = { onFocus: (e) => onFocus(e, item) };
  const { parent } = item;
  const { relationMode } = item.annotation;
  const editable = parent.isEditable && !item.isReadOnly();
  const deleteable = parent.isDeleteable && !item.isReadOnly();
 
  if (relationMode) {
    classes.push(styles.relation);
  }
 
  if (item.selected) {
    classes.push(styles.selected);
  } else if (item.highlighted) {
    classes.push(styles.highlighted);
  }
 
  if (editable || parent.transcription) {
    params.onChange = (str) => {
      item.setValue(str);
      item.parent.updateLeadTime();
    };
    params.onInput = () => {
      item.parent.countTime();
    };
  }
 
  params.onDelete = item.deleteRegion;
 
  let divAttrs = {};
 
  if (!parent.perregion) {
    divAttrs = {
      onMouseOver: () => {
        if (relationMode) {
          item.setHighlight(true);
        }
      },
      onMouseOut: () => {
        /* range.setHighlight(false); */
        if (relationMode) {
          item.setHighlight(false);
        }
      },
    };
  }
 
  const name = `${parent?.name ?? ""}:${item.id}`;
 
  return (
    <div {...divAttrs} className={cn("row").toString()} data-testid="custom-region">
      <HtxTextBox
        isEditable={editable}
        isDeleteable={deleteable}
        onlyEdit={parent.transcription}
        id={`CustomRegion-${item.id}`}
        name={name}
        className={classes.join(" ")}
        rows={parent.rows}
        text={item._value}
        {...params}
        ignoreShortcuts={true}
      />
    </div>
  );
};
 
const HtxCustomRegion = observer(HtxCustomRegionView);
 
Registry.addTag("customregion", CustomRegionModel, HtxCustomRegion);
Registry.addRegionType(CustomRegionModel, "custominterface");
 
export { CustomRegionModel, HtxCustomRegion };