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
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
const assert = require("assert");
 
Feature("Shortcuts functional").config({ waitForAction: 16 });
 
const createConfig = ({ rows = "1" }) => {
  return `<View>
  <TextArea name="text" toName="audio" editable="true" rows="${rows}">
    <Shortcut alias="[-]" value="-" hotkey="1" />
    <Shortcut alias="[ + ]" value=" + " hotkey="2" />
    <Shortcut alias="[!]" value="!" hotkey="3" />
    <Shortcut alias="[make a ninja]" value="‍👤" hotkey="4" />
  </TextArea>
  <Audio name="audio" value="$audio"/>
  <Labels name="labels" toName="audio" allowempty="true">
    <Label value="Label1"/>
    <Label value="Label2"/>
  </Labels>
</View>
`;
};
 
const configParams = new DataTable(["inline"]);
 
[true, false].forEach((inline) => {
  configParams.add([inline]);
});
 
const AUDIO_URL = "/public/files/barradeen-emotional.mp3";
 
const TEXT_SELECTOR = "[name='text']";
 
Data(configParams).Scenario(
  "Should keep the focus and cursor position.",
  async ({ I, LabelStudio, AtOutliner, current }) => {
    const { inline } = current;
    const config = createConfig({
      rows: inline ? "1" : "3",
    });
 
    const params = {
      config,
      data: { audio: AUDIO_URL },
    };
 
    I.amOnPage("/");
    LabelStudio.init(params);
    AtOutliner.seeRegions(0);
 
    // Check if there is right input element
    I.seeElement((inline ? "input" : "textarea") + TEXT_SELECTOR);
 
    // Input something there
    I.fillField(TEXT_SELECTOR, "A B");
 
    // Try to use shortcuts
    // A B
    I.click(TEXT_SELECTOR);
    // A B|
    // Shortcut by pressing hotkey (the cursor is at the end)
    I.pressKey("3");
    // A B!|
    I.pressKey("ArrowLeft");
    // A B|!
    I.pressKey("ArrowLeft");
    // A |B!
    // Select space
    I.pressKeyDown("Shift");
    I.pressKey("ArrowLeft");
    I.pressKeyUp("Shift");
    // A| |B!
    // Shortcut by clicking button (the cursor is in the middle)
    I.click(`${locate(".ant-tag").toXPath()}[contains(text(), '[ + ]')]`);
    // A + |B!
    I.pressKey("ArrowLeft");
    // A +| B!
    I.pressKey("ArrowLeft");
    // A |+ B!
    I.pressKey("ArrowLeft");
    // A| + B!
    I.pressKey("ArrowLeft");
    // |A + B!
    // Shortcut by pressing hotkey (the cursor is at the begin)
    I.pressKey("1");
    // -|A + B!
    // Commit
    I.pressKey(["Shift", "Enter"]);
 
    // If we got an expected result then we didn't lost focus.
    const result = await LabelStudio.serialize();
    assert.equal(result.length, 1);
    assert.equal(result[0].value.text[0], "-A + B!");
  },
);
 
Data(configParams).Scenario("Should work with emoji.", async ({ I, LabelStudio, AtOutliner, current }) => {
  const { inline } = current;
  const config = createConfig({
    rows: inline ? "1" : "3",
  });
 
  const params = {
    config,
    data: { audio: AUDIO_URL },
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  AtOutliner.seeRegions(0);
 
  // Check if there is right input element
  I.seeElement((inline ? "input" : "textarea") + TEXT_SELECTOR);
 
  // Try to use shortcuts with emoji
  // Input some cats
  I.fillField(TEXT_SELECTOR, "🐱🐱🐱");
  // 🐱🐱🐱
  I.click(TEXT_SELECTOR);
  // 🐱🐱🐱|
  // Move cursor to the end of the second cat emoji
  I.pressKey("ArrowLeft");
  // 🐱🐱|🐱
  // Make the cat a ninja cat
  I.pressKey("4");
  // 🐱🐱‍👤|🐱
  // Commit
  I.pressKey(["Shift", "Enter"]);
 
  // If we got an expected result then we didn't lost focus.
  const result = await LabelStudio.serialize();
  assert.equal(result.length, 1);
  assert.equal(result[0].value.text[0], "🐱🐱‍👤🐱");
});
 
Data(configParams).Scenario("Should work with existent regions.", async ({ I, LabelStudio, AtOutliner, current }) => {
  const { inline } = current;
  const config = createConfig({
    rows: inline ? "1" : "3",
  });
 
  const params = {
    config,
    data: { audio: AUDIO_URL },
    annotations: [
      {
        id: "Test",
        result: [
          {
            value: { text: ["A B"] },
            id: "floE-bRC8E",
            from_name: "text",
            to_name: "audio",
            type: "textarea",
          },
        ],
      },
    ],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  // Text regions will not be displayed at outliner
  AtOutliner.seeRegions(0);
  LabelStudio.waitForObjectsReady();
 
  const initialResult = await LabelStudio.serialize();
  assert.equal(initialResult.length, 1);
 
  // Start editing
  I.click('[aria-label="Edit Region"]');
 
  // Try to use shortcuts
  // A B|
  // Shortcut by pressing hotkey (the cursor is at the end)
  I.pressKey("3");
  // A B!|
  I.pressKey("ArrowLeft");
  // A B|!
  I.pressKey("ArrowLeft");
  // A |B!
  // Select space
  I.pressKeyDown("Shift");
  I.pressKey("ArrowLeft");
  I.pressKeyUp("Shift");
  // A| |B!
  // Shortcut by clicking button (the cursor is in the middle)
  I.click(`${locate(".ant-tag").toXPath()}[contains(text(), '[ + ]')]`);
  // A + |B!
  I.pressKey("ArrowLeft");
  // A +| B!
  I.pressKey("ArrowLeft");
  // A |+ B!
  I.pressKey("ArrowLeft");
  // A| + B!
  I.pressKey("ArrowLeft");
  // |A + B!
  // Shortcut by pressing hotkey (the cursor is at the begin)
  I.pressKey("1");
  // -|A + B!
  // Commit
  I.pressKey(["Shift", "Enter"]);
 
  // If we got an expected result then we didn't lost focus.
  const result = await LabelStudio.serialize();
  assert.equal(result.length, 1);
  assert.equal(result[0].value.text[0], "-A + B!");
});
 
{
  const paramsTable = new DataTable(["isInline", "isPerRegion"]);
 
  for (const isPerRegion of [true, false]) {
    for (const isInline of [true, false]) {
      paramsTable.add([isInline, isPerRegion]);
    }
  }
 
  const createConfig = ({ rows = "1" }) => {
    return `<View>
  <Text name="text" value="$text" />
  <TextArea name="comment" toName="text" editable="true" rows="${rows}">
    <Shortcut value="Shortcut" hotkey="Alt+V" />
  </TextArea>
</View>
`;
  };
  const createPerRegionConfig = ({ rows = "1" }) => {
    return `<View>
  <Labels name="label" toName="text">
    <Label value="region" />
  </Labels>
  <Text name="text" value="$text" />
  <TextArea name="comment" toName="text" editable="true" rows="${rows}">
    <Shortcut value="Shortcut" hotkey="Alt+V" />
  </TextArea>
</View>
`;
  };
 
  Data(paramsTable).Scenario("Initial focus", async ({ I, LabelStudio, AtOutliner, current }) => {
    const { isInline, isPerRegion } = current;
    const config = (isPerRegion ? createPerRegionConfig : createConfig)({
      rows: isInline ? "1" : "3",
    });
 
    I.amOnPage("/");
 
    LabelStudio.init({
      config,
      data: { text: "A simple text" },
      annotations: [
        {
          id: "Test",
          result: isPerRegion
            ? [
                {
                  value: {
                    start: 9,
                    end: 13,
                    text: "text",
                    labels: ["region"],
                  },
                  id: "Yk0XNP_zRJ",
                  from_name: "label",
                  to_name: "text",
                  type: "labels",
                  origin: "manual",
                },
              ]
            : [],
        },
      ],
    });
 
    if (isPerRegion) {
      I.say("Select region");
      AtOutliner.clickRegion(1);
    }
    I.say("Try to use shortcut at the start");
    I.click(locate(".ant-tag").withText("Shortcut"));
    // eslint-disable-next-line
    // pause();
 
    I.waitForValue('[name="comment"]', "Shortcut");
 
    I.say("Commit text");
    I.pressKey(["Shift", "Enter"]);
 
    I.say("Check that shortcuts still work in the edit mode");
    I.click('[aria-label="Edit Region"]');
    I.click(locate(".ant-tag").withText("Shortcut"));
    I.waitForValue('[name^="comment:"]', "ShortcutShortcut");
 
    I.say("Commit changes");
    I.pressKey(["Shift", "Enter"]);
 
    I.say("Check that shortcuts work with textarea again");
    I.click(locate(".ant-tag").withText("Shortcut"));
    I.waitForValue('[name="comment"]', "Shortcut");
  });
}