Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 486d53d

Browse files
hlomzikniklubnick-skriabin
authored
Fix showLabels param and related settings (#360)
* [fix] don't show null as label value * [fix] showLabels=false takes precedence * [fix] always show labels in NER until !showLabels previously NER labels were always shown despite the settings; but with showLabels=false labels depend on settings, so that was not `false` actually. * [fix] regions with and without label same height * [fix] showLabels unset by default; fix conditions very important thing: `label` is an array * [fix] settings couldn't affect explicit showLabels * [fix] ctrl+z with text regions' labels * comments about weird label type: array|string|null * [fix] copy .htx-no-label class from global.scss * [fix] add .htx-no-label to the generated styles * [fix] toggle labels also in every iframe Co-authored-by: niklub <[email protected]> Co-authored-by: Nick Skriabin <[email protected]>
1 parent 0acf05c commit 486d53d

File tree

5 files changed

+46
-23
lines changed

5 files changed

+46
-23
lines changed

src/mixins/HighlightMixin.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { getRoot, types } from "mobx-state-tree";
1+
import { types } from "mobx-state-tree";
22

33
import Utils from "../utils";
44
import { guidGenerator } from "../utils/unique";
55
import Constants, { defaultStyle } from "../core/Constants";
6+
import { isDefined } from "../utils/utilities";
67

78
export const HighlightMixin = types
89
.model()
@@ -47,10 +48,20 @@ export const HighlightMixin = types
4748
const labelColor = self.getLabelColor();
4849
const identifier = guidGenerator(5);
4950
const stylesheet = createSpanStylesheet(root.ownerDocument, identifier, labelColor);
51+
const classNames = ["htx-highlight", stylesheet.className];
52+
53+
if (!(self.parent.showlabels ?? self.store.settings.showLabels)) {
54+
classNames.push("htx-no-label");
55+
}
56+
57+
// in this case labels presence can't be changed from settings — manual mode
58+
if (isDefined(self.parent.showlabels)) {
59+
classNames.push("htx-manual-label");
60+
}
5061

5162
self._stylesheet = stylesheet;
5263
self._spans = Utils.Selection.highlightRange(range, {
53-
classNames: ["htx-highlight", stylesheet.className],
64+
classNames,
5465
label: self.getLabels(),
5566
});
5667

@@ -79,8 +90,11 @@ export const HighlightMixin = types
7990
updateSpans() {
8091
if (self._hasSpans) {
8192
const lastSpan = self._spans[self._spans.length - 1];
93+
const label = self.getLabels();
8294

83-
lastSpan.setAttribute("data-label", self.getLabels());
95+
// label is array, string or null, so check for length
96+
if (!label?.length) lastSpan.removeAttribute("data-label");
97+
else lastSpan.setAttribute("data-label", label);
8498
}
8599
},
86100

@@ -100,7 +114,7 @@ export const HighlightMixin = types
100114
const lastSpan = self._spans[self._spans.length - 1];
101115

102116
self._stylesheet.setColor(self.getLabelColor());
103-
Utils.Selection.applySpanStyles(lastSpan, { label: "" });
117+
Utils.Selection.applySpanStyles(lastSpan, { label: self.getLabels() });
104118
},
105119

106120
/**
@@ -166,10 +180,6 @@ export const HighlightMixin = types
166180
},
167181

168182
getLabels() {
169-
const settings = getRoot(self).settings;
170-
171-
if (!self.parent.showlabels && !settings.showLabels) return null;
172-
173183
return self.labeling?.mainValue ?? [];
174184
},
175185

@@ -228,6 +238,7 @@ const stateClass = {
228238
highlighted: "__highlighted",
229239
collapsed: "__collapsed",
230240
hidden: "__hidden",
241+
noLabel: "htx-no-label",
231242
};
232243

233244
/**
@@ -267,6 +278,7 @@ const createSpanStylesheet = (document, identifier, color) => {
267278
font-family: Monaco;
268279
vertical-align: super;
269280
content: attr(data-label);
281+
line-height: 0;
270282
`,
271283
[classNames.active]: `
272284
color: ${Utils.Colors.contrastColor(initialActiveColor)};
@@ -287,6 +299,9 @@ const createSpanStylesheet = (document, identifier, color) => {
287299
[`${className}.${stateClass.hidden}::after`]: `
288300
display: none
289301
`,
302+
[`${className}.${stateClass.noLabel}::after`]: `
303+
display: none
304+
`,
290305
};
291306

292307
const styleTag = document.createElement("style");

src/tags/object/RichText/RichText.styl

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@
3030
justify-content: center;
3131
align-items: center;
3232
background: rgba(125,125,125,.15);
33-
font-size: 24px;
33+
font-size: 24px;
34+
35+
// global.scss is not included to final build, so define it here
36+
:global(.htx-no-label)::after {
37+
display: none;
38+
}

src/tags/object/RichText/model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const TagAttrs = types.model("RichTextModel", {
6565

6666
highlightcolor: types.maybeNull(customTypes.color),
6767

68-
showlabels: types.optional(types.boolean, true),
68+
showlabels: types.maybeNull(types.boolean),
6969

7070
encoding: types.optional(types.enumeration(["none", "base64", "base64unicode"]), "none"),
7171

src/utils/html.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ import insertAfter from "insert-after";
22
import * as Checkers from "./utilities";
33
import Canvas from "./canvas";
44

5+
// fast way to change labels visibility for all text regions
56
function toggleLabelsAndScores(show) {
6-
const els = document.getElementsByClassName("htx-highlight");
7+
const toggleInDocument = document => {
8+
const els = document.getElementsByClassName("htx-highlight");
79

8-
Array.from(els).forEach(el => {
9-
let foundCls = null;
10+
Array.from(els).forEach(el => {
11+
// labels presence controlled by explicit `showLabels` in the config
12+
if (el.classList.contains("htx-manual-label")) return;
1013

11-
Array.from(el.classList).forEach(cls => {
12-
if (cls.indexOf("htx-label-") !== -1) foundCls = cls;
13-
});
14-
15-
if (foundCls !== null) {
1614
if (show) el.classList.remove("htx-no-label");
1715
else el.classList.add("htx-no-label");
18-
}
19-
});
16+
});
17+
};
18+
19+
toggleInDocument(document);
20+
document.querySelectorAll("iframe.lsf-htx-richtext")
21+
.forEach(iframe => toggleInDocument(iframe.contentWindow.document));
2022
}
2123

2224
const labelWithCSS = (function() {

src/utils/selection-tools.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export const highlightRange = (range, { label, classNames }) => {
362362

363363
const lastLabel = highlights[highlights.length - 1];
364364

365-
if (lastLabel) lastLabel.setAttribute("data-label", label);
365+
if (lastLabel) lastLabel.setAttribute("data-label", label ?? "");
366366

367367
return highlights;
368368
};
@@ -454,8 +454,9 @@ export const applySpanStyles = (spanNode, { classNames, label }) => {
454454
spanNode.classList.add(...classNames);
455455
}
456456

457-
if (label === null) spanNode.removeAttribute("data-label");
458-
else if (label) spanNode.setAttribute("data-label", label);
457+
// label is array, string or null, so check for length
458+
if (!label?.length) spanNode.removeAttribute("data-label");
459+
else spanNode.setAttribute("data-label", label);
459460
};
460461

461462
/**

0 commit comments

Comments
 (0)