File size: 9,522 Bytes
ccefd0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import CommonDialog from "./CommonDialog";
import { useState, useEffect, useRef } from "react";

const style = {
  padding: "5px 2px 2px 5px",
  margin: "2px 0",
};

export default function TextChartDialog({
  open,
  close,
  addAnnotation,
  deleteAnnotation,
  popupData,
}: {
  plotlyData: any;
  open: boolean;
  close: () => void;
  addAnnotation: (annotation: any) => void;
  updateAnnotation?: (annotation: any) => void;
  deleteAnnotation: (annotation: any) => void;
  popupData: any | null;
}) {
  // Prevent multiple renderings
  const hasLoaded = useRef(false);

  const defaultPopupData = {
    text: "",
    color: "#0088CC",
    size: 18,
    bordercolor: "#822661",
    arrowcolor: "#822661",
    bgcolor: "#000000",
    arrowsize: 1,
    arrowwidth: 2,
    yanchor: "above",
  };

  // Use a single state object to hold all form data
  const [formData, setFormData] = useState<any>(defaultPopupData);
  const [editMode, setEditMode] = useState(false);

  // Handle initialization when dialog opens
  useEffect(() => {
    if (open && popupData?.annotation && !hasLoaded.current) {
      const annotation = popupData.annotation;

      // Get properties from annotation for editing
      let data = {
        text: annotation.text || "",
        color: annotation.font?.color || defaultPopupData.color,
        size: annotation.font?.size || defaultPopupData.size,
        bordercolor: annotation.bordercolor || defaultPopupData.bordercolor,
        bgcolor: annotation.bgcolor || defaultPopupData.bgcolor,
        arrowcolor: annotation.arrowcolor || defaultPopupData.arrowcolor,
        arrowsize: annotation.arrowsize || defaultPopupData.arrowsize,
        arrowwidth: annotation.arrowwidth || defaultPopupData.arrowwidth,
        yanchor: "above",
      };

      // Determine position based on annotation coordinates
      if (annotation.y !== undefined && annotation.ay !== undefined) {
        data.yanchor = annotation.y < annotation.ay ? "above" : "below";
      }

      setFormData(data);
      setEditMode(true);
      hasLoaded.current = true;
    } else if (!open) {
      // Reset when dialog closes
      setFormData(defaultPopupData);
      setEditMode(false);
      hasLoaded.current = false;
    } else if (open && !popupData?.annotation) {
      // Reset for new annotations
      setFormData(defaultPopupData);
      setEditMode(false);
    }
  }, [open, popupData]);

  function onChange(e: any) {
    const name = e.target.id.replace("addtext_", "");
    let value = e.target.value;

    // Convert numeric values
    if (name === "size" || name === "arrowsize" || name === "arrowwidth") {
      value = parseFloat(value);
    }

    setFormData((prev: any) => ({
      ...prev,
      [name]: value
    }));
  }

  function onClose() {
    close();
  }

  function onSubmit() {
    if (formData.text) {
      const dataToSubmit = { ...formData };

      // Add the annotation reference for editing
      if (editMode && popupData?.annotation) {
        dataToSubmit.annotation = popupData.annotation;
      }

      addAnnotation(dataToSubmit);
      close();
    } else {
      if (document.getElementById("popup_textarea_warning")) {
        document.getElementById("popup_textarea_warning")!.style.display = "block";
      }
      if (document.getElementById("addtext_text")) {
        document.getElementById("addtext_text")!.style.border = "1px solid red";
      }
    }
  }

  function onDelete() {
    if (editMode && popupData) {
      deleteAnnotation(popupData);
    }
    close();
  }

  return (
    <CommonDialog
      title={editMode ? "Edit Annotation" : "Add Text to Chart"}
      description="Add or edit text annotations on the chart."
      open={open}
      close={onClose}
    >
      <div id="popup_title" className="popup_content">
        <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
          <div style={{ marginBottom: 20 }}>
            <label htmlFor="popup_text">
              <b>Text:</b>
              <div id="popup_textarea_warning" className="popup_warning" style={{display: "none"}}>
                Text is required
              </div>
            </label>
            <textarea
              id="addtext_text"
              style={{
                ...style,
                width: "100%",
                maxWidth: "100%",
                maxHeight: "200px",
                marginTop: "8px",
              }}
              rows={4}
              cols={50}
              placeholder="Enter text here"
              onChange={onChange}
              value={formData.text || ""}
            ></textarea>
          </div>

          <div
            style={{
              display: "grid",
              gridTemplateColumns: "1fr 1fr",
              gap: "12px",
              marginBottom: 20,
            }}
          >
            {/* Row 1 */}
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_color">
                <b>Font color</b>
              </label>
              <input
                type="color"
                id="addtext_color"
                style={{ margin: "2px 0" }}
                value={formData.color}
                onChange={onChange}
              />
            </div>

            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_size">
                <b>Font size</b>
              </label>
              <input
                style={{ width: "52px" }}
                type="number"
                id="addtext_size"
                value={formData.size}
                onChange={onChange}
              />
            </div>

            {/* Row 2 */}
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_bgcolor">
                <b>Background</b>
              </label>
              <input
                type="color"
                id="addtext_bgcolor"
                style={{ margin: "2px 0" }}
                value={formData.bgcolor}
                onChange={onChange}
              />
            </div>

            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_bordercolor">
                <b>Border color</b>
              </label>
              <input
                type="color"
                id="addtext_bordercolor"
                style={{ margin: "2px 0" }}
                value={formData.bordercolor}
                onChange={onChange}
              />
            </div>

            {/* Row 3 */}
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_arrowcolor">
                <b>Arrow color</b>
              </label>
              <input
                type="color"
                id="addtext_arrowcolor"
                style={{ margin: "2px 0" }}
                value={formData.arrowcolor}
                onChange={onChange}
              />
            </div>

            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_arrowsize">
                <b>Arrow size</b>
              </label>
              <input
                style={{ width: "52px" }}
                type="number"
                id="addtext_arrowsize"
                min="0.1"
                max="5"
                step="0.1"
                value={formData.arrowsize}
                onChange={onChange}
              />
            </div>

            {/* Row 4 */}
            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_arrowwidth">
                <b>Arrow width</b>
              </label>
              <input
                style={{ width: "52px" }}
                type="number"
                id="addtext_arrowwidth"
                min="1"
                max="10"
                value={formData.arrowwidth}
                onChange={onChange}
              />
            </div>

            <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
              <label htmlFor="addtext_yanchor">
                <b>Position</b>
              </label>
              <select
                id="addtext_yanchor"
                name="yanchor"
                style={{ width: "100px" }}
                value={formData.yanchor}
                onChange={onChange}
              >
                <option value="above">Above</option>
                <option value="below">Below</option>
              </select>
            </div>
          </div>
        </div>

        <div style={{ float: "right", marginTop: 20 }}>
          <button
            className="_btn-tertiary ph-capture"
            id="title_cancel"
            onClick={onClose}
          >
            Cancel
          </button>
          {editMode && (
            <button
              className="_btn ph-capture"
              id="title_delete"
              onClick={onDelete}
            >
              Delete
            </button>
          )}
          <button
            className="_btn ph-capture"
            id="title_submit"
            onClick={onSubmit}
          >
            Submit
          </button>
        </div>
      </div>
    </CommonDialog>
  );
}