Skip to content

Commit 367759c

Browse files
committed
Removable spans
1 parent bf8f14d commit 367759c

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

examples/introduction.ipynb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
},
1919
{
2020
"cell_type": "code",
21-
"execution_count": 2,
21+
"execution_count": 8,
2222
"metadata": {},
2323
"outputs": [
2424
{
2525
"data": {
2626
"application/vnd.jupyter.widget-view+json": {
27-
"model_id": "1829eb118f2144af87b08c70337954ba",
27+
"model_id": "5ef05f34ae604a9bb8e869e70da5feaf",
2828
"version_major": 2,
2929
"version_minor": 0
3030
},
@@ -50,17 +50,18 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 3,
53+
"execution_count": 10,
5454
"metadata": {},
5555
"outputs": [
5656
{
5757
"data": {
5858
"text/plain": [
59-
"[{'start': 6, 'end': 16, 'text': 'March 2014', 'label': 'date'},\n",
60-
" {'start': 43, 'end': 55, 'text': 'Jonathan Ive', 'label': 'person'}]"
59+
"[[{'start': 6, 'end': 16, 'text': 'March 2014', 'label': 'date'},\n",
60+
" {'start': 89, 'end': 94, 'text': 'Apple', 'label': 'org'},\n",
61+
" {'start': 28, 'end': 33, 'text': 'Apple', 'label': 'org'}]]"
6162
]
6263
},
63-
"execution_count": 3,
64+
"execution_count": 10,
6465
"metadata": {},
6566
"output_type": "execute_result"
6667
}
@@ -84,7 +85,7 @@
8485
{
8586
"data": {
8687
"application/vnd.jupyter.widget-view+json": {
87-
"model_id": "16084c39ab6848f299ceac47c2a55169",
88+
"model_id": "844d0d89496342618e5c5d4e6b592a1f",
8889
"version_major": 2,
8990
"version_minor": 0
9091
},
@@ -111,21 +112,8 @@
111112
]
112113
},
113114
{
114-
"cell_type": "code",
115-
"execution_count": 5,
115+
"cell_type": "raw",
116116
"metadata": {},
117-
"outputs": [
118-
{
119-
"data": {
120-
"text/plain": [
121-
"[]"
122-
]
123-
},
124-
"execution_count": 5,
125-
"metadata": {},
126-
"output_type": "execute_result"
127-
}
128-
],
129117
"source": [
130118
"other.spans"
131119
]

src/components/Annotate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export default function Annotate({
1919
const totalDocs = docs.length;
2020
const [selectedLabel, setSelectedLabel] = useState<string>("");
2121
const [docIndex, setDocIndex] = useState<number>(0);
22-
const [docSpans, setDocSpans] = useState<Span[][]>([]);
22+
const [docSpans, setDocSpans] = useState<Span[][]>(
23+
[...Array(totalDocs).keys()].map(() => [])
24+
);
2325

2426
const text = useMemo<string>(() => {
2527
return docs[docIndex];

src/components/Highlightable.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@ import { h, VNode } from "preact";
22
import { useEffect, useRef } from "preact/hooks";
33
import { Span } from "../annotate";
44

5-
const SpanLabel = ({ text, label }: { text: string; label: string }): VNode => {
6-
return h("span", { className: "span" }, [
5+
const SpanLabel = ({
6+
text,
7+
label,
8+
onClick,
9+
}: {
10+
text: string;
11+
label: string;
12+
onClick: () => void;
13+
}): VNode => {
14+
return h("span", { className: "span", onClick }, [
715
text,
816
h("span", { className: "spanLabel" }, label),
917
]);
1018
};
1119

12-
const getHighlightedText = (text: string, spans: Span[]): VNode[] => {
20+
const getHighlightedText = (
21+
text: string,
22+
spans: Span[],
23+
onRemoveSpan: (span: Span) => void
24+
): VNode[] => {
1325
const chunks: VNode[] = [];
1426
let prevOffset = 0;
1527

@@ -23,7 +35,13 @@ const getHighlightedText = (text: string, spans: Span[]): VNode[] => {
2335
text.slice(prevOffset, span.start)
2436
)
2537
);
26-
chunks.push(SpanLabel({ text: span.text, label: span.label }));
38+
chunks.push(
39+
SpanLabel({
40+
text: span.text,
41+
label: span.label,
42+
onClick: () => onRemoveSpan(span),
43+
})
44+
);
2745
prevOffset = span.end;
2846
});
2947
chunks.push(h("span", { "data-offset": prevOffset }, text.slice(prevOffset)));
@@ -76,6 +94,10 @@ const Highlightable = ({
7694
);
7795
}
7896

97+
const onRemoveSpan = (span: Span) => {
98+
onUpdate(spans.filter((s) => s.start !== span.start));
99+
};
100+
79101
useEffect(() => {
80102
const el: any = ref.current;
81103
if (el) {
@@ -86,7 +108,7 @@ const Highlightable = ({
86108
return h(
87109
"div",
88110
{ ref, className: "content" },
89-
getHighlightedText(text, spans)
111+
getHighlightedText(text, spans, onRemoveSpan)
90112
);
91113
};
92114

src/components/Nav.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const Nav = ({ docIndex, totalDocs, onChangeNav }: Props): VNode => {
1515
};
1616

1717
return h("div", { className: "nav" }, [
18-
h("div", { onClick: onPrev }, "<"),
18+
h("div", { className: "navLink", onClick: onPrev }, "<"),
1919
h("div", null, `${docIndex + 1} / ${totalDocs}`),
20-
h("div", { onClick: onNext }, ">"),
20+
h("div", { className: "navLink", onClick: onNext }, ">"),
2121
]);
2222
};
2323

0 commit comments

Comments
 (0)