Spaces:
Paused
Paused
File size: 88,054 Bytes
aceb1b2 | 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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 | /**
* Span Annotation Core - Positioning and Overlay Management
*
* This module provides unified text positioning for span annotations.
*/
// Debug logging utility - respects the debug setting from server config
function spanCoreDebugLog(...args) {
if (window.config && window.config.debug) {
console.log(...args);
}
}
function spanCoreDebugWarn(...args) {
if (window.config && window.config.debug) {
console.warn(...args);
}
}
/**
* Centralized Z-Index Management
* All overlay z-index values defined in one place for consistency.
* Higher values appear on top of lower values.
*/
const OVERLAY_Z_INDEX = {
// Base layers (defined in HTML template)
TEXT_CONTENT: 1, // #text-content
OVERLAY_CONTAINER: 2, // #span-overlays container
// Overlay types (higher = on top)
ADMIN_KEYWORD: 100, // Admin-defined keyword highlights (dashed border)
AI_KEYWORD: 110, // AI-suggested keyword highlights (solid border)
USER_SPAN: 120, // User-created span annotations (filled)
// Interactive elements (must be above overlays)
SPAN_CONTROLS: 200, // Label + delete button
TOOLTIP: 300 // Hover tooltips
};
/**
* Get font metrics for positioning calculations
*/
function getFontMetrics(container) {
const computedStyle = window.getComputedStyle(container);
const fontSize = parseFloat(computedStyle.fontSize);
const fontFamily = computedStyle.fontFamily;
const lineHeight = parseFloat(computedStyle.lineHeight) || fontSize * 1.2;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.font = `${fontSize}px ${fontFamily}`;
const testChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
const charWidths = {};
for (let char of testChars) {
charWidths[char] = ctx.measureText(char).width;
}
const totalWidth = Object.values(charWidths).reduce((sum, width) => sum + width, 0);
const averageCharWidth = totalWidth / Object.keys(charWidths).length;
return {
fontSize,
fontFamily,
lineHeight,
averageCharWidth,
charWidths,
containerPadding: {
top: parseFloat(computedStyle.paddingTop) || 0,
left: parseFloat(computedStyle.paddingLeft) || 0,
right: parseFloat(computedStyle.paddingRight) || 0,
bottom: parseFloat(computedStyle.paddingBottom) || 0
}
};
}
/**
* Unified Positioning Strategy for Text Span Annotation
*/
class UnifiedPositioningStrategy {
constructor(container) {
this.container = container;
this.fontMetrics = null;
this.canonicalText = null;
this.isInitialized = false;
}
async initialize() {
await this.waitForElements();
await this.waitForFontMetrics();
this.canonicalText = this.getCanonicalText();
this.fontMetrics = this.getFontMetrics();
this.isInitialized = true;
}
async waitForElements() {
return new Promise((resolve) => {
const check = () => {
if (this.container && this.container.textContent && this.container.textContent.trim()) {
resolve();
} else {
setTimeout(check, 50);
}
};
check();
});
}
async waitForFontMetrics() {
return new Promise((resolve) => {
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(() => {
setTimeout(resolve, 200);
});
} else {
setTimeout(resolve, 800);
}
});
}
getCanonicalText() {
if (this.container.hasAttribute('data-original-text')) {
const originalText = this.container.getAttribute('data-original-text');
const cleanText = originalText.replace(/<[^>]*>/g, '').trim();
return this.normalizeText(cleanText);
}
const textContent = this.container.textContent || '';
return this.normalizeText(textContent);
}
normalizeText(text) {
// For code displays (pre/code elements), preserve newlines for accurate positioning
// Check if we're inside a code display
if (this.container && (
this.container.closest('.code-display') ||
this.container.closest('.code-simple') ||
this.container.querySelector('pre')
)) {
// Only normalize multiple spaces to single space, keep newlines
return text.replace(/[ \t]+/g, ' ').trim();
}
// For regular text, normalize all whitespace
return text.replace(/\s+/g, ' ').trim();
}
getFontMetrics() {
return getFontMetrics(this.container);
}
createSpanFromSelection(selection, options = {}) {
if (!this.isInitialized) {
return null;
}
const selectedText = selection.toString().trim();
if (!selectedText) {
return null;
}
// Calculate offset directly from the selection range by walking the DOM
// This avoids text normalization mismatches between indexOf and DOM text
const offsets = this.getOffsetsFromSelection(selection);
if (!offsets) {
console.warn('[SpanCore] Could not calculate offsets from selection');
return null;
}
console.log('[SpanCore] createSpanFromSelection: container=' + (this.container ? this.container.id : 'NULL') + ' start=' + offsets.start + ' end=' + offsets.end + ' selected="' + selectedText + '"');
return this.createSpanWithAlgorithm(offsets.start, offsets.end, selectedText, options);
}
/**
* Calculate character offsets from a selection by walking the DOM tree.
* This gives us the exact position in the raw DOM text, avoiding normalization issues.
*/
getOffsetsFromSelection(selection) {
if (!selection.rangeCount) return null;
const range = selection.getRangeAt(0);
const startContainer = range.startContainer;
const endContainer = range.endContainer;
const startOffset = range.startOffset;
const endOffset = range.endOffset;
// Walk through text nodes to find cumulative offset
const textNodes = [];
let cumulativeOffset = 0;
const collectTextNodes = (node) => {
if (node.nodeType === Node.TEXT_NODE) {
textNodes.push({
node: node,
start: cumulativeOffset,
end: cumulativeOffset + node.textContent.length
});
cumulativeOffset += node.textContent.length;
} else if (node.nodeType === Node.ELEMENT_NODE) {
// Skip overlay containers - their label/button text must not affect offset calculations
if (node.id === 'span-overlays' || node.classList.contains('span-overlays-field')) {
return;
}
for (const child of node.childNodes) {
collectTextNodes(child);
}
}
};
collectTextNodes(this.container);
// Find the offset for start and end
let absoluteStart = null;
let absoluteEnd = null;
for (const tn of textNodes) {
if (tn.node === startContainer) {
absoluteStart = tn.start + startOffset;
}
if (tn.node === endContainer) {
absoluteEnd = tn.start + endOffset;
}
}
// Handle case where start/end containers are element nodes
if (absoluteStart === null && startContainer.nodeType === Node.ELEMENT_NODE) {
// startOffset is the index of the child node
if (startOffset < startContainer.childNodes.length) {
const childNode = startContainer.childNodes[startOffset];
for (const tn of textNodes) {
if (tn.node === childNode || tn.node.parentNode === childNode) {
absoluteStart = tn.start;
break;
}
}
}
}
if (absoluteEnd === null && endContainer.nodeType === Node.ELEMENT_NODE) {
if (endOffset > 0 && endOffset <= endContainer.childNodes.length) {
const childNode = endContainer.childNodes[endOffset - 1];
for (const tn of textNodes) {
if (tn.node === childNode || tn.node.parentNode === childNode) {
absoluteEnd = tn.end;
break;
}
}
}
}
if (absoluteStart === null || absoluteEnd === null) {
console.warn('[SpanCore] Could not find absolute offsets for selection', {
startContainer: startContainer.nodeName,
endContainer: endContainer.nodeName,
startOffset,
endOffset
});
return null;
}
return { start: absoluteStart, end: absoluteEnd };
}
createSpanWithAlgorithm(start, end, text, options = {}) {
if (!this.isInitialized) {
return null;
}
const canonicalText = this.getCanonicalText();
if (start < 0 || end > canonicalText.length || start >= end) {
console.warn('[SpanCore] Invalid span positions:', { start, end, textLength: canonicalText.length });
return null;
}
// Use getPositionsFromOffsets (offset-based) instead of getTextPositions (indexOf-based).
// getTextPositions ignores start/end and uses indexOf(text) on data-original-text,
// which can return wrong positions when data-original-text differs from DOM text.
const positions = this.getPositionsFromOffsets(start, end);
if (!positions || positions.length === 0) {
return null;
}
// Validate positions
const invalidPositions = positions.filter(p =>
p.x < -1000 || p.y < -1000 || p.width <= 0 || p.height <= 0 ||
p.x > 10000 || p.y > 10000 || p.width > 5000 || p.height > 500
);
if (invalidPositions.length > 0) {
console.warn('[SpanCore] Some positions look invalid:', invalidPositions);
}
const span = {
// Use a temporary ID - will be replaced with deterministic ID when label/schema are set
id: `span_${start}_${end}_${Date.now()}`,
start: start,
end: end,
text: text,
label: 'unknown',
color: null
};
// Store the positions for later ID generation
span._tempPositions = { start, end };
// Pass options (including color) to createOverlay
const overlay = this.createOverlay(span, positions, options);
if (!overlay) {
return null;
}
return { span, overlay, positions };
}
getTextPositions(start, end, text) {
// Use this.container (the element passed to the strategy constructor)
// instead of hardcoded #text-content, so multi-field mode works
const textElement = this.container || document.getElementById('text-content');
if (!textElement) {
return null;
}
let actualText = textElement.getAttribute('data-original-text');
const domTextContent = textElement.textContent || textElement.innerText || '';
if (!actualText) {
actualText = domTextContent;
}
const targetStart = actualText.indexOf(text);
if (targetStart === -1) {
return null;
}
const targetEnd = targetStart + text.length;
const range = document.createRange();
// Collect all text nodes with their cumulative offsets
const textNodes = [];
let cumulativeOffset = 0;
const collectTextNodes = (node) => {
if (node.nodeType === Node.TEXT_NODE) {
textNodes.push({
node: node,
text: node.textContent,
start: cumulativeOffset,
end: cumulativeOffset + node.textContent.length
});
cumulativeOffset += node.textContent.length;
} else if (node.nodeType === Node.ELEMENT_NODE) {
// Skip overlay containers - their label/button text must not affect offset calculations
if (node.id === 'span-overlays' || node.classList.contains('span-overlays-field')) {
return;
}
for (const child of node.childNodes) {
collectTextNodes(child);
}
}
};
collectTextNodes(textElement);
if (textNodes.length === 0) {
return null;
}
// Find the text nodes containing start and end positions
let startNode = null, startOffset = 0;
let endNode = null, endOffset = 0;
for (const tn of textNodes) {
if (startNode === null && targetStart >= tn.start && targetStart < tn.end) {
startNode = tn.node;
startOffset = targetStart - tn.start;
}
if (targetEnd > tn.start && targetEnd <= tn.end) {
endNode = tn.node;
endOffset = targetEnd - tn.start;
}
}
try {
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
} catch (error) {
console.error('[SpanCore] Error setting range:', error);
return null;
}
const rects = range.getClientRects();
if (rects.length === 0) {
return null;
}
// Use the text element itself for positioning reference
// This ensures consistency with overlay positioning which is inside the text element
const containerRect = textElement.getBoundingClientRect();
const positions = Array.from(rects).map((rect) => ({
x: rect.left - containerRect.left,
y: rect.top - containerRect.top,
width: rect.width,
height: rect.height
}));
return positions;
}
/**
* Get screen positions for text at specific character offsets.
* Unlike getTextPositions(), this uses the provided offsets directly
* instead of searching for the text with indexOf().
*
* @param {number} start - Start character offset in the text
* @param {number} end - End character offset in the text
* @returns {Array<{x, y, width, height}>|null} Screen positions relative to container, or null on error
*/
getPositionsFromOffsets(start, end) {
// Use this.container instead of hardcoded #text-content
const textElement = this.container || document.getElementById('text-content');
if (!textElement) {
console.warn('[SpanCore] getPositionsFromOffsets: text-content element not found');
return null;
}
// Collect text nodes with cumulative offsets
const textNodes = [];
let cumulativeOffset = 0;
const collectTextNodes = (node) => {
if (node.nodeType === Node.TEXT_NODE) {
textNodes.push({
node: node,
start: cumulativeOffset,
end: cumulativeOffset + node.textContent.length
});
cumulativeOffset += node.textContent.length;
} else if (node.nodeType === Node.ELEMENT_NODE) {
// Skip overlay containers - their label/button text must not affect offset calculations
if (node.id === 'span-overlays' || node.classList.contains('span-overlays-field')) {
return;
}
for (const child of node.childNodes) {
collectTextNodes(child);
}
}
};
collectTextNodes(textElement);
if (textNodes.length === 0) {
console.warn('[SpanCore] getPositionsFromOffsets: no text nodes found');
return null;
}
// Find nodes containing start and end positions
let startNode = null, startOffset = 0;
let endNode = null, endOffset = 0;
for (const tn of textNodes) {
// Find start node
if (startNode === null && start >= tn.start && start < tn.end) {
startNode = tn.node;
startOffset = start - tn.start;
}
// Find end node (can be same as start node)
if (end > tn.start && end <= tn.end) {
endNode = tn.node;
endOffset = end - tn.start;
}
}
if (!startNode || !endNode) {
console.warn('[SpanCore] getPositionsFromOffsets: could not find text nodes for offsets', { start, end, totalLength: cumulativeOffset });
return null;
}
// Create range and get bounding rectangles
const range = document.createRange();
try {
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
} catch (e) {
console.error('[SpanCore] getPositionsFromOffsets: error setting range:', e);
return null;
}
const rects = range.getClientRects();
if (rects.length === 0) {
console.warn('[SpanCore] getPositionsFromOffsets: no client rects returned');
return null;
}
// Convert to positions relative to the text element container itself
// This ensures consistency with overlay positioning which is also relative to text-content
const containerRect = textElement.getBoundingClientRect();
return Array.from(rects).map(rect => ({
x: rect.left - containerRect.left,
y: rect.top - containerRect.top,
width: rect.width,
height: rect.height
}));
}
createOverlay(span, positions, options = {}) {
if (!positions || positions.length === 0) return null;
const { isAiSpan = false, color = 'rgba(255, 255, 0, 0.3)' } = options;
// Padding values for visual breathing room around text
const HORIZONTAL_PADDING = 3; // px on each side
const VERTICAL_PADDING = 2; // px on top and bottom
const overlay = document.createElement('div');
overlay.className = isAiSpan ? 'span-overlay-ai' : 'span-overlay-pure';
overlay.dataset.annotationId = span.id;
overlay.dataset.start = span.start;
overlay.dataset.end = span.end;
overlay.dataset.label = span.label;
if (span.schema) {
overlay.dataset.schema = span.schema;
}
if (span.target_field) {
overlay.dataset.targetField = span.target_field;
}
if (isAiSpan) {
overlay.dataset.isAiSpan = 'true';
}
// Include entity linking data if present
if (span.kb_id && span.kb_source) {
overlay.dataset.kbId = span.kb_id;
overlay.dataset.kbSource = span.kb_source;
if (span.kb_label) {
overlay.dataset.kbLabel = span.kb_label;
}
overlay.classList.add('has-entity-link');
}
overlay.style.position = 'absolute';
// Anchor at top-left of #span-overlays. Without explicit top/left, the browser
// uses the "hypothetical static position" which is offset by any rendered whitespace
// inside #span-overlays (caused by white-space:pre-wrap inherited from #text-content).
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.pointerEvents = 'none';
overlay.style.zIndex = isAiSpan ? OVERLAY_Z_INDEX.AI_KEYWORD : OVERLAY_Z_INDEX.USER_SPAN;
positions.forEach((pos) => {
const segment = document.createElement('div');
segment.className = 'span-highlight-segment';
segment.style.position = 'absolute';
// Add padding: position starts earlier, dimensions are larger
segment.style.left = `${pos.x - HORIZONTAL_PADDING}px`;
segment.style.top = `${pos.y - VERTICAL_PADDING}px`;
segment.style.width = `${pos.width + 2 * HORIZONTAL_PADDING}px`;
segment.style.height = `${pos.height + 2 * VERTICAL_PADDING}px`;
if (isAiSpan) {
// AI spans use a box/border style instead of background highlight
segment.style.backgroundColor = 'transparent';
segment.style.border = `2px solid ${color}`;
segment.style.borderRadius = '3px';
segment.style.boxShadow = `0 0 4px ${color}`;
} else {
segment.style.backgroundColor = color;
segment.style.border = `1px solid ${color.replace('0.3', '0.8')}`;
segment.style.borderRadius = '4px';
}
segment.style.pointerEvents = 'none';
overlay.appendChild(segment);
});
if (!isAiSpan) {
// Regular spans get labels and delete buttons
// Position label and delete button together above the segment start
const segmentLeft = positions[0].x - HORIZONTAL_PADDING;
const segmentTop = positions[0].y - VERTICAL_PADDING;
// Create a container for label + delete button so they stay together
// Position above the highlight - allow negative values to go above container
const controlsContainer = document.createElement('div');
controlsContainer.className = 'span-controls';
controlsContainer.style.position = 'absolute';
controlsContainer.style.left = `${segmentLeft}px`;
// Position 22px above the segment top (controls are ~18px tall)
controlsContainer.style.top = `${segmentTop - 22}px`;
controlsContainer.style.display = 'flex';
controlsContainer.style.alignItems = 'center';
controlsContainer.style.gap = '4px';
controlsContainer.style.pointerEvents = 'auto';
controlsContainer.style.zIndex = '10';
// Check if span labels should be shown (config: show_span_labels)
const spanForm = document.querySelector('.annotation-form.span[data-show-span-labels="false"]');
const showLabels = !spanForm;
if (showLabels) {
const label = document.createElement('div');
label.className = 'span-label';
label.textContent = span.label;
// Override CSS positioning to work in flex container
label.style.position = 'static';
label.style.top = 'auto';
label.style.left = 'auto';
label.style.backgroundColor = 'rgba(0, 0, 0, 0.85)';
label.style.color = 'white';
label.style.padding = '2px 6px';
label.style.borderRadius = '3px';
label.style.fontSize = '11px';
label.style.fontWeight = '500';
label.style.whiteSpace = 'nowrap';
label.style.display = 'block';
controlsContainer.appendChild(label);
}
const deleteBtn = document.createElement('button');
deleteBtn.className = 'span-delete-btn';
deleteBtn.textContent = '×';
deleteBtn.style.backgroundColor = 'rgba(220, 53, 69, 0.9)';
deleteBtn.style.color = 'white';
deleteBtn.style.border = 'none';
deleteBtn.style.borderRadius = '50%';
deleteBtn.style.width = '16px';
deleteBtn.style.height = '16px';
deleteBtn.style.minWidth = '16px';
deleteBtn.style.minHeight = '16px';
deleteBtn.style.padding = '0';
deleteBtn.style.margin = '0';
deleteBtn.style.fontSize = '12px';
deleteBtn.style.fontFamily = 'Arial, sans-serif';
deleteBtn.style.fontWeight = 'bold';
deleteBtn.style.lineHeight = '1';
deleteBtn.style.textAlign = 'center';
deleteBtn.style.cursor = 'pointer';
deleteBtn.style.display = 'flex';
deleteBtn.style.alignItems = 'center';
deleteBtn.style.justifyContent = 'center';
deleteBtn.style.flexShrink = '0';
// Reset any CSS positioning that might interfere
deleteBtn.style.position = 'static';
deleteBtn.style.top = 'auto';
deleteBtn.style.right = 'auto';
deleteBtn.style.left = 'auto';
deleteBtn.onclick = (e) => {
e.stopPropagation();
// Immediately remove this overlay from DOM for instant visual feedback
const overlayElement = e.target.closest('.span-overlay-pure');
if (overlayElement) {
overlayElement.remove();
}
// Then process the server-side delete
if (window.spanManager) {
window.spanManager.deleteSpan(span.id);
}
};
controlsContainer.appendChild(deleteBtn);
overlay.appendChild(controlsContainer);
}
return overlay;
}
validateSpan(span) {
if (!this.isInitialized) return false;
const { start, end, text } = span;
if (start >= end || start < 0 || end > this.canonicalText.length) return false;
const coveredText = this.canonicalText.substring(start, end);
return coveredText === text;
}
}
/**
* Frontend Span Manager for Potato Annotation Platform
*/
class SpanManager {
constructor() {
this.annotations = { spans: [] };
this.colors = {};
this.selectedLabel = null;
this.selectedTargetField = '';
this.currentSchema = null;
this.isInitialized = false;
this.currentInstanceId = null;
this.lastKnownInstanceId = null;
this.positioningStrategy = null;
this.schemas = {};
// Multi-span support: per-field positioning strategies
this.fieldStrategies = {}; // { fieldKey: UnifiedPositioningStrategy }
// AI span state tracking
this.aiSpans = new Map(); // Map<annotationId, Array<overlayElement>>
// Admin keyword highlight state tracking
this.keywordHighlights = []; // Array of keyword highlight overlay elements
// Discontinuous span support: track active span being extended
this.discontinuousMode = false;
this.activeDiscontinuousSpan = null; // Span being extended with additional parts
}
// ==================== SCHEMA STATE MANAGEMENT ====================
/**
* Set the current schema with validation and logging.
* This is the ONLY place currentSchema should be modified.
*
* @param {string} schema - The schema name to set
* @param {string} source - Where this call originated (for debugging)
* @returns {boolean} - Whether the schema was set successfully
*/
setCurrentSchema(schema, source = 'unknown') {
if (!schema) {
console.warn(`[SpanManager] setCurrentSchema called with empty schema from ${source}`);
return false;
}
// Validate schema exists if we have schemas loaded
if (Object.keys(this.schemas).length > 0 && !this.schemas[schema]) {
console.warn(`[SpanManager] Schema '${schema}' not found in loaded schemas. Source: ${source}. Available: ${Object.keys(this.schemas).join(', ')}`);
// Still set it - might be valid but not yet loaded
}
const oldSchema = this.currentSchema;
this.currentSchema = schema;
if (oldSchema !== schema) {
console.debug(`[SpanManager] Schema changed: '${oldSchema}' -> '${schema}' (source: ${source})`);
}
return true;
}
// ==================== AI SPAN METHODS ====================
/**
* Insert AI-suggested span highlights for keywords
* @param {Array} keywords - Array of keyword objects with {label, start, end, text, reasoning}
* @param {string} annotationId - The annotation ID these AI spans belong to
*/
/**
* Insert AI keyword highlights with bordered (unfilled) overlays
* @param {Array} highlights - Array of {label, start, end, text, schema}
* @param {string} annotationId - The annotation ID
*/
insertAiKeywordHighlights(highlights, annotationId) {
console.log('[SpanManager] insertAiKeywordHighlights called:', { highlights, annotationId });
if (!highlights || !Array.isArray(highlights) || highlights.length === 0) {
console.log('[SpanManager] No highlights to insert');
return;
}
this.deleteOneAiSpan(annotationId);
const spanOverlays = document.getElementById('span-overlays');
if (!spanOverlays) {
console.log('[SpanManager] span-overlays element not found');
return;
}
const createdOverlays = [];
highlights.forEach((highlight) => {
const { label, start, end, text, schema } = highlight;
if (!this.positioningStrategy || !this.positioningStrategy.isInitialized) {
console.warn('[SpanManager] Positioning strategy not initialized');
return;
}
// Use getPositionsFromOffsets() which respects the provided offsets
// instead of getTextPositions() which does indexOf() and ignores offsets
const positions = this.positioningStrategy.getPositionsFromOffsets(start, end);
if (!positions || positions.length === 0) {
console.warn('[SpanManager] No positions found for highlight:', { start, end, text });
return;
}
// Get color for this label from schema colors
const color = this.getAiKeywordColor(label, schema);
const span = {
id: `ai_keyword_${annotationId}_${start}_${end}_${Date.now()}`,
start: start,
end: end,
text: text,
label: label
};
// Create bordered overlay (not filled)
const overlay = this.createBorderedOverlay(span, positions, color);
if (overlay) {
overlay.dataset.aiAnnotationId = annotationId;
overlay.title = `${label}: "${text}"`;
spanOverlays.appendChild(overlay);
createdOverlays.push(overlay);
}
});
if (createdOverlays.length > 0) {
this.aiSpans.set(annotationId, createdOverlays);
}
}
/**
* Create a bordered (unfilled) overlay for keyword highlighting.
* Used for AI keyword highlights and admin keyword highlights.
*/
createBorderedOverlay(span, positions, color) {
// Padding for visual breathing room
const HORIZONTAL_PADDING = 2;
const VERTICAL_PADDING = 1;
const overlay = document.createElement('div');
overlay.className = 'span-overlay ai-keyword-overlay';
overlay.dataset.spanId = span.id;
overlay.dataset.label = span.label;
overlay.style.position = 'absolute';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.pointerEvents = 'none';
overlay.style.zIndex = OVERLAY_Z_INDEX.AI_KEYWORD;
positions.forEach((pos) => {
const segment = document.createElement('div');
segment.className = 'span-segment ai-keyword-segment';
segment.style.position = 'absolute';
// FIX: Use correct property names (x, y not left, top)
segment.style.left = `${pos.x - HORIZONTAL_PADDING}px`;
segment.style.top = `${pos.y - VERTICAL_PADDING}px`;
segment.style.width = `${pos.width + 2 * HORIZONTAL_PADDING}px`;
segment.style.height = `${pos.height + 2 * VERTICAL_PADDING}px`;
segment.style.border = `2px solid ${color}`;
segment.style.borderRadius = '3px';
segment.style.backgroundColor = 'transparent';
segment.style.pointerEvents = 'none';
segment.style.boxSizing = 'border-box';
overlay.appendChild(segment);
});
return overlay;
}
/**
* Get color for AI keyword highlight based on label
*/
getAiKeywordColor(label, schemaName) {
// Try to get from loaded colors (with case-insensitive fallback)
if (this.colors && schemaName && this.colors[schemaName]) {
const schemaColors = this.colors[schemaName];
// Try exact match first
if (schemaColors[label]) {
const color = schemaColors[label];
if (color.startsWith('(')) {
return `rgba${color.replace(')', ', 0.8)')}`;
}
return color;
}
// Try case-insensitive match
const lowerLabel = label.toLowerCase();
for (const [key, color] of Object.entries(schemaColors)) {
if (key.toLowerCase() === lowerLabel) {
if (color.startsWith('(')) {
return `rgba${color.replace(')', ', 0.8)')}`;
}
return color;
}
}
}
// Fallback colors for common labels (case-insensitive)
const fallbackColors = {
'positive': 'rgba(34, 197, 94, 0.8)', // green
'negative': 'rgba(239, 68, 68, 0.8)', // red
'neutral': 'rgba(156, 163, 175, 0.8)', // gray
'yes': 'rgba(34, 197, 94, 0.8)', // green
'no': 'rgba(239, 68, 68, 0.8)', // red
'maybe': 'rgba(245, 158, 11, 0.8)', // amber
};
const lowerLabel = label.toLowerCase();
return fallbackColors[lowerLabel] || 'rgba(245, 158, 11, 0.8)';
}
insertAiSpans(keywords, annotationId) {
console.log('[SpanManager] insertAiSpans called:', { keywords, annotationId });
if (!keywords || !Array.isArray(keywords) || keywords.length === 0) {
console.log('[SpanManager] No keywords to insert, returning early');
return;
}
this.deleteOneAiSpan(annotationId);
const spanOverlays = document.getElementById('span-overlays');
if (!spanOverlays) {
console.log('[SpanManager] span-overlays element not found - keyword highlighting only works with span annotation type');
return;
}
const createdOverlays = [];
keywords.forEach((keyword) => {
const { label, start, end, text, reasoning } = keyword;
if (!this.positioningStrategy || !this.positioningStrategy.isInitialized) {
return;
}
const positions = this.positioningStrategy.getTextPositions(start, end, text);
if (!positions || positions.length === 0) {
return;
}
const span = {
id: `ai_span_${annotationId}_${start}_${end}_${Date.now()}`,
start: start,
end: end,
text: text,
label: label || 'keyword'
};
const aiColor = 'rgba(245, 158, 11, 0.8)';
const overlay = this.positioningStrategy.createOverlay(span, positions, {
isAiSpan: true,
color: aiColor
});
if (overlay) {
overlay.dataset.aiAnnotationId = annotationId;
overlay.title = reasoning || `AI suggested: "${text}"`;
spanOverlays.appendChild(overlay);
createdOverlays.push(overlay);
}
});
if (createdOverlays.length > 0) {
this.aiSpans.set(annotationId, createdOverlays);
}
}
/**
* Clear all AI span highlights
*/
clearAiSpans() {
const spanOverlays = document.getElementById('span-overlays');
if (spanOverlays) {
const aiOverlays = spanOverlays.querySelectorAll('.span-overlay-ai');
aiOverlays.forEach(overlay => overlay.remove());
}
this.aiSpans.clear();
}
/**
* Check if AI spans exist for a specific annotation
*/
inAiSpans(annotationId) {
return this.aiSpans.has(annotationId) && this.aiSpans.get(annotationId).length > 0;
}
/**
* Delete AI spans for a specific annotation
*/
deleteOneAiSpan(annotationId) {
const overlays = this.aiSpans.get(annotationId);
if (overlays && overlays.length > 0) {
overlays.forEach(overlay => {
if (overlay && overlay.parentNode) {
overlay.remove();
}
});
}
this.aiSpans.delete(annotationId);
}
// ==================== CORE SPAN MANAGER METHODS ====================
async fetchCurrentInstanceIdFromServer() {
try {
const response = await fetch('/api/current_instance');
if (!response.ok) {
throw new Error(`Failed to fetch current instance: ${response.status}`);
}
const data = await response.json();
const serverInstanceId = data.instance_id;
if (this.currentInstanceId !== serverInstanceId && this.currentInstanceId !== null) {
this.clearAllStateAndOverlays();
}
this.currentInstanceId = serverInstanceId;
this.lastKnownInstanceId = serverInstanceId;
return serverInstanceId;
} catch (error) {
console.error('[SpanManager] Error fetching current instance ID:', error);
return null;
}
}
async initialize() {
try {
const serverInstanceId = await this.fetchCurrentInstanceIdFromServer();
if (!serverInstanceId) {
console.error('[SpanManager] Failed to get server instance ID during initialization');
return false;
}
// Set up event listeners early (before async strategy init that might block)
this.setupEventListeners();
// Check for instance_display span target fields first
const spanTargetFields = document.querySelectorAll('.display-field[data-span-target="true"]');
console.log('[SpanManager] init: found', spanTargetFields.length, 'span target fields');
if (spanTargetFields.length > 0) {
// Multi-span / instance_display mode
for (const field of spanTargetFields) {
const fieldKey = field.dataset.fieldKey;
const textContent = field.querySelector('.text-content');
console.log('[SpanManager] init: field', fieldKey, 'textContent=', textContent?.id);
if (textContent && fieldKey) {
// Ensure text content has position: relative for overlay positioning
// This is critical: overlays are positioned relative to textContent itself
if (!textContent.style.position || textContent.style.position === 'static') {
textContent.style.position = 'relative';
}
// Create span-overlays container for this field if not present
// Append INSIDE textContent so positions are relative to the same container
let overlaysEl = textContent.querySelector('.span-overlays-field');
if (!overlaysEl) {
overlaysEl = document.createElement('div');
overlaysEl.className = 'span-overlays-field';
overlaysEl.id = `span-overlays-${fieldKey}`;
overlaysEl.style.cssText = 'position: absolute; top: 0; left: 0; right: 0; bottom: 0; pointer-events: none; z-index: 2;';
textContent.appendChild(overlaysEl);
}
const strategy = new UnifiedPositioningStrategy(textContent);
await strategy.initialize();
this.fieldStrategies[fieldKey] = strategy;
console.log('[SpanManager] init: strategy ready for', fieldKey);
}
}
// Use the first field as the default positioning strategy
const firstKey = Object.keys(this.fieldStrategies)[0];
if (firstKey) {
this.positioningStrategy = this.fieldStrategies[firstKey];
}
} else {
// Legacy single-field mode
const textContent = document.getElementById('text-content');
if (textContent) {
this.positioningStrategy = new UnifiedPositioningStrategy(textContent);
await this.positioningStrategy.initialize();
}
}
await this.loadSchemas();
await this.loadColors();
this.setupResizeHandler();
this.setupOverlayInteractions();
await this.loadAnnotations(serverInstanceId);
// Auto-select the label if a span schema has exactly one label
this.autoSelectSingleLabel();
this.isInitialized = true;
return true;
} catch (error) {
console.error('[SpanManager] Initialization failed:', error);
this.isInitialized = false;
return false;
}
}
async loadColors() {
try {
const response = await fetch('/api/colors');
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
this.colors = await response.json();
} catch (error) {
console.warn('[SpanManager] Error loading colors, using defaults:', error.message);
// Fallback colors - visible purple for better visibility
const defaultColors = {
'positive': 'rgba(110, 86, 207, 0.15)', // Purple
'negative': 'rgba(239, 68, 68, 0.15)', // Red
'neutral': 'rgba(113, 113, 122, 0.15)', // Gray
'span': 'rgba(110, 86, 207, 0.15)' // Purple
};
// Structure as { schemaName: { labelName: color } } to match server format
this.colors = {
'sentiment': defaultColors,
'emotion': defaultColors,
'entity': defaultColors,
'topic': defaultColors,
'span': defaultColors
};
}
}
async loadSchemas() {
try {
const response = await fetch('/api/schemas');
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
this.schemas = await response.json();
if (!this.currentSchema && Object.keys(this.schemas).length > 0) {
this.setCurrentSchema(Object.keys(this.schemas)[0], 'loadSchemas');
}
return this.schemas;
} catch (error) {
console.warn('[SpanManager] Error loading schemas:', error.message);
return this.extractSchemaFromForms();
}
}
extractSchemaFromForms() {
const spanForms = document.querySelectorAll('.annotation-form.span');
if (spanForms.length > 0) {
const firstSpanForm = spanForms[0];
const fieldset = firstSpanForm.querySelector('fieldset');
if (fieldset && fieldset.getAttribute('schema')) {
return fieldset.getAttribute('schema');
}
}
return null;
}
setupEventListeners() {
const textContainer = document.getElementById('instance-text');
const textContent = document.getElementById('text-content');
if (textContainer) {
textContainer.addEventListener('mouseup', (e) => this.handleTextSelection(e));
textContainer.addEventListener('keyup', (e) => this.handleTextSelection(e));
}
if (textContent) {
textContent.addEventListener('mouseup', (e) => this.handleTextSelection(e));
textContent.addEventListener('keyup', (e) => this.handleTextSelection(e));
}
// Also listen on instance_display span target fields
const spanTargetFields = document.querySelectorAll('.display-field[data-span-target="true"]');
for (const field of spanTargetFields) {
field.addEventListener('mouseup', (e) => this.handleTextSelection(e));
field.addEventListener('keyup', (e) => this.handleTextSelection(e));
}
// Listen for clicks outside spans to clear discontinuous mode
document.addEventListener('click', (e) => {
// If clicking outside span annotation areas, clear discontinuous span
if (!e.target.closest('.annotation-form.span') &&
!e.target.closest('#instance-text') &&
!e.target.closest('.display-field[data-span-target="true"]')) {
this.clearActiveDiscontinuousSpan();
}
});
document.addEventListener('click', (e) => {
if (e.target.classList.contains('span-delete')) {
e.stopPropagation();
const annotationId = e.target.closest('.span-highlight').dataset.annotationId;
this.deleteSpan(annotationId);
}
});
}
selectLabel(label, schema = null, targetField = null) {
this.selectedLabel = label;
this.selectedTargetField = targetField || '';
if (schema) {
this.setCurrentSchema(schema, 'selectLabel');
}
}
/**
* If a span schema has exactly one label, auto-select it so the user
* can immediately start highlighting text without clicking the checkbox first.
*/
autoSelectSingleLabel() {
const spanForms = document.querySelectorAll('.annotation-form.span');
for (const form of spanForms) {
const checkboxes = form.querySelectorAll('input[type="checkbox"][for_span="true"]');
if (checkboxes.length === 1 && !checkboxes[0].checked) {
checkboxes[0].click(); // triggers changeSpanLabel via onclick
}
}
}
getSelectedLabel() {
// FIRST: Use the label set by selectLabel() if available
// This is set by changeSpanLabel() when a checkbox is clicked,
// and is more reliable than querying the DOM
if (this.selectedLabel && this.currentSchema) {
return this.selectedLabel;
}
// FALLBACK: Try to find checked span checkbox (for legacy code paths)
const checkedCheckbox = document.querySelector('.annotation-form.span input[type="checkbox"]:checked');
if (checkedCheckbox) {
const checkboxId = checkedCheckbox.id;
const parts = checkboxId.split('_');
if (parts.length >= 2) {
// ID format is "schemaName_labelName" - extract both
const label = parts[parts.length - 1];
// Schema name is everything before the last underscore
// (handles multi-word schema names like "emotion_spans")
const schemaName = parts.slice(0, -1).join('_');
// Update currentSchema to match the selected checkbox's schema
if (schemaName) {
this.setCurrentSchema(schemaName, 'getSelectedLabel');
}
return label;
}
return checkedCheckbox.value;
}
return this.selectedLabel;
}
async loadAnnotations(instanceId) {
try {
const serverInstanceId = await this.fetchCurrentInstanceIdFromServer();
if (serverInstanceId !== instanceId) {
instanceId = serverInstanceId;
}
// Find the text content element - may be legacy #text-content or instance_display fields
let textContent = document.getElementById('text-content');
if (!textContent || textContent.closest('[style*="display: none"]')) {
// Try instance_display span target fields
const firstField = Object.keys(this.fieldStrategies)[0];
if (firstField) {
textContent = document.getElementById(`text-content-${firstField}`);
}
}
const existingSpans = textContent ? textContent.querySelectorAll('.span-highlight') : [];
const hasServerRenderedSpans = existingSpans.length > 0;
const response = await fetch(`/api/spans/${instanceId}`);
if (!response.ok) {
if (response.status === 404) {
this.annotations = { spans: [] };
if (!hasServerRenderedSpans) {
this.clearAllStateAndOverlays();
this.renderSpans();
}
return Promise.resolve();
}
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
// Get response text first to debug JSON parsing issues
const responseText = await response.text();
let responseData;
try {
responseData = JSON.parse(responseText);
} catch (jsonError) {
console.error('[SpanManager] JSON parse error for /api/spans/', instanceId);
console.error('[SpanManager] Response text (first 500 chars):', responseText.substring(0, 500));
throw new Error(`JSON parse error: ${jsonError.message}`);
}
this.annotations = responseData;
// Only update data-original-text on legacy #text-content, not on per-field elements
// Per-field elements already have correct data-original-text from server rendering
if (this.annotations && this.annotations.text && textContent) {
const hasFieldStrategies = Object.keys(this.fieldStrategies).length > 0;
if (!hasFieldStrategies) {
const plainText = this.annotations.text.replace(/<[^>]*>/g, '').trim();
textContent.setAttribute('data-original-text', plainText);
}
}
if (this.annotations.spans && this.annotations.spans.length > 0) {
const firstSpan = this.annotations.spans[0];
if (firstSpan.schema && !this.currentSchema) {
this.setCurrentSchema(firstSpan.schema, 'loadAnnotations');
}
}
this.renderSpans();
// Load admin keyword highlights after span annotations
await this.loadKeywordHighlights(instanceId);
} catch (error) {
console.error('[SpanManager] Error loading annotations:', error);
throw error;
}
}
getSpans() {
return this.annotations?.spans || [];
}
getSpanColor(label) {
// Diagnostic logging for color lookup failures
if (!this.currentSchema) {
console.warn(`[SpanManager] getSpanColor: No currentSchema set for label '${label}'. Using fallback color.`);
return 'rgba(110, 86, 207, 0.15)';
}
if (!this.colors || Object.keys(this.colors).length === 0) {
console.warn(`[SpanManager] getSpanColor: Colors not loaded for label '${label}'. Using fallback color.`);
return 'rgba(110, 86, 207, 0.15)';
}
if (!this.colors[this.currentSchema]) {
console.warn(`[SpanManager] getSpanColor: Schema '${this.currentSchema}' not found in colors. Available schemas: ${Object.keys(this.colors).join(', ')}. Using fallback color.`);
return 'rgba(110, 86, 207, 0.15)';
}
const schemaColors = this.colors[this.currentSchema];
if (!schemaColors[label]) {
console.warn(`[SpanManager] getSpanColor: Label '${label}' not found in schema '${this.currentSchema}'. Available labels: ${Object.keys(schemaColors).join(', ')}. Using fallback color.`);
return 'rgba(110, 86, 207, 0.15)';
}
const color = schemaColors[label];
if (color.startsWith('(')) {
return `rgba${color.replace(')', ', 0.15)')}`;
}
return color;
}
clearAllStateAndOverlays() {
// Clear legacy overlay container
const spanOverlays = document.getElementById('span-overlays');
if (spanOverlays) {
const regularOverlays = spanOverlays.querySelectorAll('.span-overlay-pure:not(.span-overlay-ai)');
regularOverlays.forEach(overlay => overlay.remove());
}
// Clear per-field overlay containers (multi-span mode)
for (const fieldKey of Object.keys(this.fieldStrategies)) {
const fieldOverlays = document.getElementById(`span-overlays-${fieldKey}`);
if (fieldOverlays) {
const regularOverlays = fieldOverlays.querySelectorAll('.span-overlay-pure:not(.span-overlay-ai)');
regularOverlays.forEach(overlay => overlay.remove());
}
}
this.annotations = { spans: [] };
// Don't clear currentSchema - keep it for consistency
}
renderSpans() {
const hasFieldStrategies = Object.keys(this.fieldStrategies).length > 0;
if (hasFieldStrategies) {
// Multi-field mode: clear and render per-field
for (const fieldKey of Object.keys(this.fieldStrategies)) {
const overlaysEl = document.getElementById(`span-overlays-${fieldKey}`);
if (overlaysEl) {
const regularOverlays = overlaysEl.querySelectorAll('.span-overlay-pure:not(.span-overlay-ai)');
regularOverlays.forEach(overlay => overlay.remove());
}
}
const spans = this.getSpans();
if (!spans || spans.length === 0) return;
const sortedSpans = [...spans].sort((a, b) => a.start - b.start);
sortedSpans.forEach((span, index) => {
const fieldKey = span.target_field || '';
const strategy = this.fieldStrategies[fieldKey] || this.positioningStrategy;
const overlaysEl = document.getElementById(`span-overlays-${fieldKey}`) || document.getElementById('span-overlays');
if (strategy && overlaysEl) {
this.renderSpanOverlay(span, index, null, overlaysEl, strategy);
}
});
} else {
// Legacy single-field mode
const textContent = document.getElementById('text-content');
const spanOverlays = document.getElementById('span-overlays');
if (!textContent || !spanOverlays) {
return;
}
// Clear existing regular overlays (preserve AI spans)
const regularOverlays = spanOverlays.querySelectorAll('.span-overlay-pure:not(.span-overlay-ai)');
regularOverlays.forEach(overlay => overlay.remove());
const spans = this.getSpans();
if (!spans || spans.length === 0) {
return;
}
const sortedSpans = [...spans].sort((a, b) => a.start - b.start);
sortedSpans.forEach((span, index) => {
this.renderSpanOverlay(span, index, textContent, spanOverlays);
});
}
}
renderSpanOverlay(span, layerIndex, textContent, spanOverlays, strategy = null) {
const activeStrategy = strategy || this.positioningStrategy;
if (!activeStrategy || !activeStrategy.isInitialized) {
return;
}
// Use getPositionsFromOffsets (offset-based) instead of getTextPositions (text-search-based)
// This is critical for multi-field mode where the API might return text from the wrong field
const positions = activeStrategy.getPositionsFromOffsets(span.start, span.end);
if (!positions || positions.length === 0) {
return;
}
const color = this.getSpanColor(span.label);
const overlay = activeStrategy.createOverlay(span, positions, {
isAiSpan: false,
color: color
});
if (overlay) {
spanOverlays.appendChild(overlay);
}
}
handleTextSelection(event) {
const selection = window.getSelection();
console.warn('[SpanManager] handleTextSelection ENTRY: rangeCount=' + selection.rangeCount + ' isCollapsed=' + selection.isCollapsed + ' selectedLabel=' + this.selectedLabel + ' currentSchema=' + this.currentSchema);
if (!selection.rangeCount || selection.isCollapsed) {
return;
}
const selectedLabel = this.getSelectedLabel();
if (!selectedLabel) {
console.warn('[SpanManager] handleTextSelection: no selectedLabel, returning');
return;
}
// Check if Ctrl/Cmd key is held for discontinuous span mode
const isDiscontinuousMode = event && (event.ctrlKey || event.metaKey);
// Check if discontinuous spans are allowed for current schema
const schemaForm = document.querySelector(`.annotation-form.span[data-allow-discontinuous="true"]`);
const allowDiscontinuous = schemaForm !== null;
// Detect which field the selection is in and pick the right positioning strategy
let targetField = this.selectedTargetField || '';
let strategy = this.positioningStrategy;
let overlaysContainer = document.getElementById('span-overlays');
const hasFieldStrategies = Object.keys(this.fieldStrategies).length > 0;
console.warn('[SpanManager] handleTextSelection: fieldStrategies=', Object.keys(this.fieldStrategies), 'selectedLabel=', selectedLabel);
if (selection.rangeCount > 0) {
const startNode = selection.getRangeAt(0).startContainer;
const el = startNode.nodeType === Node.TEXT_NODE ? startNode.parentElement : startNode;
const textContentEl = el ? el.closest('[id^="text-content-"]') : null;
console.warn('[SpanManager] handleTextSelection: textContentEl=', textContentEl?.id, 'el=', el?.id || el?.className);
if (textContentEl) {
const fieldKey = textContentEl.id.replace('text-content-', '');
if (fieldKey && this.fieldStrategies[fieldKey]) {
targetField = fieldKey;
strategy = this.fieldStrategies[fieldKey];
overlaysContainer = document.getElementById(`span-overlays-${fieldKey}`);
}
}
}
if (!strategy || !strategy.isInitialized) {
console.warn('[SpanManager] handleTextSelection: strategy not ready, targetField=' + targetField);
return;
}
console.warn('[SpanManager] handleTextSelection: using strategy for field=' + targetField + ' container=' + (strategy.container ? strategy.container.id : 'NULL') + ' overlays=' + (overlaysContainer ? overlaysContainer.id : 'NULL'));
// Handle discontinuous span extension (Ctrl/Cmd+click with existing active span)
if (isDiscontinuousMode && allowDiscontinuous && this.activeDiscontinuousSpan) {
// Only allow extending if same label and schema
if (this.activeDiscontinuousSpan.label === selectedLabel &&
this.activeDiscontinuousSpan.schema === this.currentSchema) {
this.addDiscontinuousPart(selection, strategy, overlaysContainer);
selection.removeAllRanges();
return;
}
}
// Get color BEFORE creating the overlay so it's created with the correct color
const color = this.getSpanColor(selectedLabel);
// Pass color to createSpanFromSelection so overlay is created with correct color
const result = strategy.createSpanFromSelection(selection, {
color: color
});
if (!result) {
console.warn('[SpanManager] handleTextSelection: createSpanFromSelection returned null for field=' + targetField);
return;
}
const { span, overlay } = result;
span.label = selectedLabel;
span.schema = this.currentSchema;
span.target_field = targetField;
span.additional_parts = []; // Initialize for discontinuous support
// Generate deterministic ID matching server format: {schema}_{label}_{start}_{end}
// This ensures span IDs are stable across page reloads
const oldId = span.id;
span.id = `${this.currentSchema}_${selectedLabel}_${span.start}_${span.end}`;
console.log(`[SpanManager] Generated deterministic span ID: ${span.id} (was ${oldId})`);
if (overlay) {
// Update overlay's data-annotation-id to match the new deterministic ID
overlay.dataset.annotationId = span.id;
// Update the label text to match the selected label
const label = overlay.querySelector('.span-label');
if (label) {
label.textContent = selectedLabel;
}
// Add discontinuous indicator if enabled
if (allowDiscontinuous) {
overlay.classList.add('discontinuous-enabled');
overlay.dataset.discontinuousEnabled = 'true';
}
// Append the overlay to the correct container
if (overlaysContainer) {
overlaysContainer.appendChild(overlay);
console.warn('[SpanManager] handleTextSelection: overlay appended to ' + overlaysContainer.id + ' for field=' + targetField);
} else {
console.warn('[SpanManager] handleTextSelection: no overlaysContainer for field=' + targetField);
}
}
// Add span to local state
if (!this.annotations.spans) {
this.annotations.spans = [];
}
this.annotations.spans.push(span);
// If discontinuous mode is enabled, set this as the active span for extension
if (allowDiscontinuous && isDiscontinuousMode) {
this.activeDiscontinuousSpan = span;
console.log('[SpanManager] Set active discontinuous span:', span.id);
} else {
// Clear active span if not in discontinuous mode
this.activeDiscontinuousSpan = null;
}
this.saveSpan(span);
selection.removeAllRanges();
}
/**
* Add an additional part to the active discontinuous span.
* @param {Selection} selection - The current text selection
* @param {UnifiedPositioningStrategy} strategy - The positioning strategy to use
* @param {Element} overlaysContainer - The container for span overlays
*/
addDiscontinuousPart(selection, strategy, overlaysContainer) {
if (!this.activeDiscontinuousSpan) {
console.warn('[SpanManager] addDiscontinuousPart: no active discontinuous span');
return;
}
// Get the offsets for the new selection
const offsets = strategy.getOffsetsFromSelection(selection);
if (!offsets) {
console.warn('[SpanManager] addDiscontinuousPart: could not get offsets');
return;
}
const selectedText = selection.toString().trim();
const newPart = {
start: offsets.start,
end: offsets.end,
text: selectedText
};
// Check for overlap with existing parts
const allParts = [
{ start: this.activeDiscontinuousSpan.start, end: this.activeDiscontinuousSpan.end },
...(this.activeDiscontinuousSpan.additional_parts || [])
];
for (const part of allParts) {
if (newPart.start < part.end && newPart.end > part.start) {
console.warn('[SpanManager] addDiscontinuousPart: new part overlaps with existing part');
return;
}
}
// Add the new part
if (!this.activeDiscontinuousSpan.additional_parts) {
this.activeDiscontinuousSpan.additional_parts = [];
}
this.activeDiscontinuousSpan.additional_parts.push(newPart);
// Sort parts by start position
this.activeDiscontinuousSpan.additional_parts.sort((a, b) => a.start - b.start);
console.log('[SpanManager] Added discontinuous part:', newPart, 'to span:', this.activeDiscontinuousSpan.id);
// Create overlay for the new part
const color = this.getSpanColor(this.activeDiscontinuousSpan.label);
const positions = strategy.getPositionsFromOffsets(newPart.start, newPart.end);
if (positions && positions.length > 0) {
const partOverlay = this.createDiscontinuousPartOverlay(
this.activeDiscontinuousSpan,
newPart,
positions,
color
);
if (partOverlay && overlaysContainer) {
overlaysContainer.appendChild(partOverlay);
}
}
// Save the updated span
this.saveSpanWithParts(this.activeDiscontinuousSpan);
}
/**
* Create an overlay for a discontinuous span part.
* Uses a dashed border to visually connect it to the parent span.
*/
createDiscontinuousPartOverlay(parentSpan, part, positions, color) {
const HORIZONTAL_PADDING = 3;
const VERTICAL_PADDING = 2;
const overlay = document.createElement('div');
overlay.className = 'span-overlay-pure discontinuous-part';
overlay.dataset.annotationId = parentSpan.id;
overlay.dataset.parentSpanId = parentSpan.id;
overlay.dataset.partStart = part.start;
overlay.dataset.partEnd = part.end;
overlay.dataset.label = parentSpan.label;
overlay.dataset.isDiscontinuousPart = 'true';
overlay.style.position = 'absolute';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.pointerEvents = 'none';
overlay.style.zIndex = OVERLAY_Z_INDEX.USER_SPAN;
positions.forEach((pos) => {
const segment = document.createElement('div');
segment.className = 'span-highlight-segment discontinuous-segment';
segment.style.position = 'absolute';
segment.style.left = `${pos.x - HORIZONTAL_PADDING}px`;
segment.style.top = `${pos.y - VERTICAL_PADDING}px`;
segment.style.width = `${pos.width + 2 * HORIZONTAL_PADDING}px`;
segment.style.height = `${pos.height + 2 * VERTICAL_PADDING}px`;
segment.style.backgroundColor = color;
segment.style.border = `2px dashed ${color.replace('0.15', '0.8').replace('0.3', '0.8')}`;
segment.style.borderRadius = '4px';
segment.style.pointerEvents = 'none';
overlay.appendChild(segment);
});
// Add a small label indicating this is part of a discontinuous span
if (positions.length > 0) {
const label = document.createElement('div');
label.className = 'span-label discontinuous-label';
label.textContent = `+ ${parentSpan.label}`;
label.style.position = 'absolute';
label.style.left = `${positions[0].x - HORIZONTAL_PADDING}px`;
label.style.top = `${Math.max(0, positions[0].y - VERTICAL_PADDING - 18)}px`;
label.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
label.style.color = 'white';
label.style.padding = '1px 4px';
label.style.borderRadius = '3px';
label.style.fontSize = '10px';
label.style.fontWeight = '500';
label.style.whiteSpace = 'nowrap';
label.style.pointerEvents = 'auto';
overlay.appendChild(label);
}
return overlay;
}
/**
* Save a span with additional parts (discontinuous span).
*/
async saveSpanWithParts(span) {
try {
const postData = {
type: "span",
schema: span.schema || this.currentSchema,
state: [{
name: span.label,
start: span.start,
end: span.end,
title: span.label,
value: 1,
target_field: span.target_field || '',
additional_parts: span.additional_parts || [],
span_id: span.id // Send the deterministic ID to server
}],
instance_id: this.currentInstanceId
};
const response = await fetch('/updateinstance', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(postData)
});
if (response.ok) {
console.log('[SpanManager] Saved discontinuous span with parts:', span.additional_parts);
// Don't reload annotations immediately to preserve UI state during discontinuous mode
} else {
console.error('[SpanManager] Failed to save discontinuous span:', await response.text());
}
} catch (error) {
console.error('[SpanManager] Error saving discontinuous span:', error);
}
}
/**
* Clear the active discontinuous span (e.g., when clicking elsewhere or selecting a different label).
*/
clearActiveDiscontinuousSpan() {
if (this.activeDiscontinuousSpan) {
console.log('[SpanManager] Clearing active discontinuous span:', this.activeDiscontinuousSpan.id);
this.activeDiscontinuousSpan = null;
}
}
async saveSpan(span) {
try {
const spanState = {
name: span.label,
start: span.start,
end: span.end,
title: span.label,
value: 1,
target_field: span.target_field || '',
span_id: span.id // Send the deterministic ID to server
};
// Include additional_parts for discontinuous spans
if (span.additional_parts && span.additional_parts.length > 0) {
spanState.additional_parts = span.additional_parts;
}
const postData = {
type: "span",
schema: span.schema || this.currentSchema,
state: [spanState],
instance_id: this.currentInstanceId
};
const response = await fetch('/updateinstance', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(postData)
});
if (response.ok) {
await this.loadAnnotations(this.currentInstanceId);
} else {
console.error('[SpanManager] Failed to save span:', await response.text());
}
} catch (error) {
console.error('[SpanManager] Error saving span:', error);
}
}
async deleteSpan(spanId) {
const span = this.annotations.spans?.find(s => s.id === spanId);
// Immediately remove the overlay from DOM for instant visual feedback
// This ensures overlay is removed even if server reload has issues
// Search in all possible overlay containers (main and field-specific)
const selector = `.span-overlay-pure[data-annotation-id="${spanId}"]`;
// Remove from main span-overlays container
const spanOverlays = document.getElementById('span-overlays');
if (spanOverlays) {
const overlayToRemove = spanOverlays.querySelector(selector);
if (overlayToRemove) {
overlayToRemove.remove();
}
}
// Also search in field-specific overlay containers
document.querySelectorAll('[id^="span-overlays-"]').forEach(container => {
const overlayToRemove = container.querySelector(selector);
if (overlayToRemove) {
overlayToRemove.remove();
}
});
// Also search anywhere in the document as a fallback
const anyOverlay = document.querySelector(selector);
if (anyOverlay) {
anyOverlay.remove();
}
// Also remove server-rendered inline span-highlight elements
// These are rendered by render_span_annotations() on the server
const inlineSelector = `.span-highlight[data-annotation-id="${spanId}"]`;
document.querySelectorAll(inlineSelector).forEach(inlineSpan => {
// Unwrap the span: replace it with its text content
const parent = inlineSpan.parentNode;
while (inlineSpan.firstChild) {
parent.insertBefore(inlineSpan.firstChild, inlineSpan);
}
inlineSpan.remove();
});
// Also remove from local state immediately
if (this.annotations.spans) {
this.annotations.spans = this.annotations.spans.filter(s => s.id !== spanId);
}
if (!span) {
// Even if span not found in state, we already removed the overlay visually
return;
}
try {
const postData = {
type: "span",
schema: span.schema || this.currentSchema,
state: [{
name: span.label,
start: span.start,
end: span.end,
title: span.label,
value: null
}],
instance_id: this.currentInstanceId
};
const response = await fetch('/updateinstance', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(postData)
});
if (response.ok) {
// Reload to sync with server state
await this.loadAnnotations(this.currentInstanceId);
} else {
console.error('[SpanManager] Failed to delete span:', await response.text());
// Reload anyway to get correct state from server
await this.loadAnnotations(this.currentInstanceId);
}
} catch (error) {
console.error('[SpanManager] Error deleting span:', error);
// Reload to ensure UI matches server state
await this.loadAnnotations(this.currentInstanceId);
}
}
onInstanceChange(newInstanceId) {
// Clear AI spans and keyword highlights on instance change
this.clearAiSpans();
this.clearKeywordHighlights();
if (newInstanceId && newInstanceId !== this.currentInstanceId) {
this.clearAllStateAndOverlays();
this.currentInstanceId = newInstanceId;
this.loadAnnotations(newInstanceId);
}
}
calculateOverlapDepths(spans) {
if (!spans || spans.length === 0) return [];
const result = spans.map(span => ({
span,
depth: 0,
heightMultiplier: 1.0
}));
for (let i = 0; i < spans.length; i++) {
let maxOverlap = 0;
for (let j = 0; j < i; j++) {
if (spans[j].end > spans[i].start && spans[j].start < spans[i].end) {
maxOverlap = Math.max(maxOverlap, result[j].depth + 1);
}
}
result[i].depth = maxOverlap;
result[i].heightMultiplier = 1.0 / (maxOverlap + 1);
}
return result;
}
applyOverlapStyling(spanElements, overlapData) {
spanElements.forEach((element, index) => {
if (overlapData[index]) {
const { depth, heightMultiplier } = overlapData[index];
element.style.setProperty('--overlap-depth', depth);
element.style.setProperty('--height-multiplier', heightMultiplier);
}
});
}
// ==================== KEYWORD HIGHLIGHT METHODS ====================
/**
* Load admin-defined keyword highlights for the current instance.
* These are displayed using the same visual system as AI keyword suggestions
* (bounding boxes around keywords).
*/
async loadKeywordHighlights(instanceId) {
try {
const response = await fetch(`/api/keyword_highlights/${instanceId}`);
if (!response.ok) {
if (response.status === 404) {
return;
}
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
const keywords = data.keywords || [];
if (keywords.length === 0) {
return;
}
this.insertKeywordHighlights(keywords);
} catch (error) {
console.error('[SpanManager] Error loading keyword highlights:', error);
}
}
/**
* Insert admin keyword highlights using the same visual system as AI spans.
* @param {Array} keywords - Array of keyword objects with {label, start, end, text, reasoning, schema, color}
*/
insertKeywordHighlights(keywords) {
if (!keywords || !Array.isArray(keywords) || keywords.length === 0) {
return;
}
// Clear any existing admin keyword highlights
this.clearKeywordHighlights();
const spanOverlays = document.getElementById('span-overlays');
if (!spanOverlays) {
return;
}
const createdOverlays = [];
keywords.forEach((keyword) => {
const { label, start, end, text, reasoning, schema, color } = keyword;
if (!this.positioningStrategy || !this.positioningStrategy.isInitialized) {
return;
}
// Use getPositionsFromOffsets() which respects the provided offsets
const positions = this.positioningStrategy.getPositionsFromOffsets(start, end);
if (!positions || positions.length === 0) {
console.warn('[SpanManager] No positions found for admin keyword:', { start, end, text });
return;
}
const span = {
id: `keyword_${start}_${end}_${Date.now()}`,
start: start,
end: end,
text: text,
label: label || 'keyword'
};
const keywordColor = color || 'rgba(245, 158, 11, 0.8)';
const overlay = this.positioningStrategy.createOverlay(span, positions, {
isAiSpan: true, // Use bordered style
color: keywordColor
});
if (overlay) {
overlay.dataset.keywordHighlight = 'true';
overlay.dataset.schema = schema || '';
overlay.dataset.label = label || '';
overlay.title = reasoning || `Keyword: "${text}" → ${label}`;
overlay.classList.add('keyword-highlight-overlay');
// Admin keywords use a lower z-index than AI keywords
overlay.style.zIndex = OVERLAY_Z_INDEX.ADMIN_KEYWORD;
spanOverlays.appendChild(overlay);
createdOverlays.push(overlay);
}
});
this.keywordHighlights = createdOverlays;
}
/**
* Clear all admin keyword highlight overlays.
*/
clearKeywordHighlights() {
const spanOverlays = document.getElementById('span-overlays');
if (spanOverlays) {
const keywordOverlays = spanOverlays.querySelectorAll('.keyword-highlight-overlay');
keywordOverlays.forEach(overlay => overlay.remove());
}
this.keywordHighlights = [];
}
// ==================== RESIZE HANDLING ====================
/**
* Setup resize handler to reposition overlays when window resizes.
* Uses debouncing to avoid performance issues during resize.
*/
setupResizeHandler() {
let resizeTimeout = null;
const DEBOUNCE_MS = 150;
const handleResize = () => {
if (resizeTimeout) {
clearTimeout(resizeTimeout);
}
resizeTimeout = setTimeout(() => {
this.repositionAllOverlays();
}, DEBOUNCE_MS);
};
window.addEventListener('resize', handleResize);
// Also observe container size changes (for dynamic layouts)
if (typeof ResizeObserver !== 'undefined') {
const instanceText = document.getElementById('instance-text');
if (instanceText) {
const resizeObserver = new ResizeObserver(handleResize);
resizeObserver.observe(instanceText);
}
}
spanCoreDebugLog('[SpanManager] Resize handler initialized');
}
/**
* Reposition all overlays based on current text positions.
* Called after resize to ensure overlays stay aligned with text.
*/
repositionAllOverlays() {
spanCoreDebugLog('[SpanManager] Repositioning all overlays');
// Re-render user span overlays
this.renderSpans();
// Re-render admin keyword highlights
if (this.currentInstanceId) {
this.loadKeywordHighlights(this.currentInstanceId);
}
// Clear AI keyword overlays on resize since they're temporary
// User can re-click the keyword button to regenerate
this.clearAiSpans();
}
// ==================== UNIFIED OVERLAY INTERACTIONS ====================
/**
* Setup unified interaction handlers for all overlay types.
* Provides consistent hover effects and click behavior.
*/
setupOverlayInteractions() {
const spanOverlays = document.getElementById('span-overlays');
if (!spanOverlays) return;
// Use event delegation for hover effects
spanOverlays.addEventListener('mouseenter', (e) => {
const segment = e.target.closest('.span-highlight-segment, .span-segment');
if (segment) {
this.handleSegmentHover(segment, true);
}
}, true);
spanOverlays.addEventListener('mouseleave', (e) => {
const segment = e.target.closest('.span-highlight-segment, .span-segment');
if (segment) {
this.handleSegmentHover(segment, false);
}
}, true);
spanCoreDebugLog('[SpanManager] Overlay interactions initialized');
}
/**
* Handle hover state for overlay segments.
* @param {Element} segment - The segment element
* @param {boolean} isHovering - Whether mouse is entering or leaving
*/
handleSegmentHover(segment, isHovering) {
const overlay = segment.closest('.span-overlay, .span-overlay-pure, .span-overlay-ai, .ai-keyword-overlay, .keyword-highlight-overlay');
if (!overlay) return;
if (isHovering) {
// Highlight all segments of the same overlay
overlay.querySelectorAll('.span-highlight-segment, .span-segment').forEach(seg => {
seg.style.filter = 'brightness(0.85)';
});
// Show tooltip if available (for AI/keyword overlays without controls)
const tooltipText = overlay.title || overlay.dataset.label;
const hasControls = overlay.querySelector('.span-controls');
if (tooltipText && !hasControls) {
this.showOverlayTooltip(segment, tooltipText);
}
} else {
// Remove highlight
overlay.querySelectorAll('.span-highlight-segment, .span-segment').forEach(seg => {
seg.style.filter = '';
});
this.hideOverlayTooltip();
}
}
/**
* Show tooltip near a segment.
* @param {Element} segment - The segment to position tooltip near
* @param {string} text - The tooltip text
*/
showOverlayTooltip(segment, text) {
const spanOverlays = document.getElementById('span-overlays');
if (!spanOverlays) return;
let tooltip = document.getElementById('overlay-tooltip');
if (!tooltip) {
tooltip = document.createElement('div');
tooltip.id = 'overlay-tooltip';
tooltip.className = 'overlay-tooltip';
tooltip.style.cssText = `
position: absolute;
background: rgba(0, 0, 0, 0.85);
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
pointer-events: none;
z-index: ${OVERLAY_Z_INDEX.TOOLTIP};
white-space: nowrap;
max-width: 300px;
overflow: hidden;
text-overflow: ellipsis;
`;
spanOverlays.appendChild(tooltip);
}
const rect = segment.getBoundingClientRect();
const containerRect = document.getElementById('instance-text').getBoundingClientRect();
tooltip.textContent = text;
tooltip.style.left = `${rect.left - containerRect.left}px`;
tooltip.style.top = `${Math.max(0, rect.top - containerRect.top - 28)}px`;
tooltip.style.display = 'block';
}
/**
* Hide the overlay tooltip.
*/
hideOverlayTooltip() {
const tooltip = document.getElementById('overlay-tooltip');
if (tooltip) {
tooltip.style.display = 'none';
}
}
}
// Initialize global span manager
window.spanManager = new SpanManager();
/**
* Initialize the span manager.
* Called once when DOM is ready, with a single retry fallback.
*/
function initializeSpanManager() {
if (window.spanManager && !window.spanManager.isInitialized) {
window.spanManager.initialize().catch((error) => {
console.error('[SpanManager] Initialization failed:', error);
});
}
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initializeSpanManager);
} else {
// DOM already loaded, initialize immediately
initializeSpanManager();
}
// Single retry fallback after 1 second
// Handles edge cases where text-content isn't populated yet on initial load
// (e.g., content loaded via AJAX after DOMContentLoaded)
setTimeout(() => {
if (window.spanManager && !window.spanManager.isInitialized) {
console.debug('[SpanManager] Retry initialization after 1s timeout');
initializeSpanManager();
}
}, 1000);
// Export for testing
if (typeof module !== 'undefined' && module.exports) {
module.exports = { SpanManager, UnifiedPositioningStrategy, getFontMetrics };
}
|