File size: 22,578 Bytes
e75de29 115fba1 e75de29 ae56e3f e75de29 97b4c47 58965e9 1ec71a6 583fb42 97b4c47 583fb42 115fba1 d9dc9ac 115fba1 d9dc9ac 3e2cbc6 d9dc9ac 583fb42 115fba1 d9dc9ac 583fb42 d9dc9ac 583fb42 d9dc9ac 583fb42 97b4c47 583fb42 d9dc9ac e75de29 583fb42 d9dc9ac 583fb42 e75de29 | 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 | /* Inserted into each isolated srcdoc by template.py. */
(async function () {
const root = '__RENDERER_ROOT__';
const status = document.getElementById('status');
const diagnostics = window.visualizerDiagnostics = {
parserURL: '__PARSER_URL__', stage: 'loading', messages: []
};
let manualSize = false;
const frame = window.frameElement;
const handle = document.getElementById('resize-handle');
function sizePanel(width, height) {
if (!frame) return;
manualSize = true;
const available = frame.parentElement.clientWidth;
frame.style.width = Math.min(available, Math.max(Math.min(320, available), width)) + 'px';
frame.style.height = Math.max(parseFloat(getComputedStyle(frame).minHeight) || 280, height) + 'px';
}
let drag = null;
handle.addEventListener('pointerdown', event => {
if (!frame || event.button !== 0) return;
event.preventDefault();
const box = frame.getBoundingClientRect();
drag = {x:event.screenX, y:event.screenY, width:box.width, height:box.height};
handle.setPointerCapture(event.pointerId);
});
handle.addEventListener('pointermove', event => {
if (drag) sizePanel(drag.width + event.screenX - drag.x, drag.height + event.screenY - drag.y);
});
handle.addEventListener('pointerup', () => { drag = null; });
handle.addEventListener('pointercancel', () => { drag = null; });
handle.addEventListener('keydown', event => {
if (!frame || !['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) return;
event.preventDefault();
const box = frame.getBoundingClientRect();
sizePanel(box.width + (event.key === 'ArrowRight' ? 20 : event.key === 'ArrowLeft' ? -20 : 0),
box.height + (event.key === 'ArrowDown' ? 20 : event.key === 'ArrowUp' ? -20 : 0));
});
handle.addEventListener('dblclick', () => {
manualSize = false;
if (frame) {
frame.style.width = '100%';
frame.style.height = Math.ceil(document.body.getBoundingClientRect().height + 2) + 'px';
}
});
function fail(error) {
diagnostics.stage = 'error';
diagnostics.error = error.message;
status.textContent = 'Visualization failed: ' + error.message;
document.querySelectorAll('button').forEach(button => button.disabled = true);
}
function loadScript(url) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
const timer = setTimeout(() => reject(new Error('Timed out loading ' + url)), 15000);
script.src = url;
script.onload = () => { clearTimeout(timer); resolve(); };
script.onerror = () => { clearTimeout(timer); reject(new Error('Could not load ' + url)); };
document.head.appendChild(script);
});
}
try {
const paths = [
'lib/ext/jquery.min.js', 'lib/ext/jquery.svg.min.js',
'lib/ext/jquery.svgdom.min.js', 'lib/brat/configuration.js',
'lib/brat/util.js', 'lib/brat/annotation_log.js', 'lib/ext/webfont.js',
'lib/brat/dispatcher.js', 'lib/brat/url_monitor.js',
'lib/brat/visualizer.js', 'lib/local/config.js'
];
for (const path of paths) await loadScript(root + path);
await loadScript(diagnostics.parserURL);
if (!window.ConllU || !window.Util || !window.Config) {
throw new Error('A required library API is unavailable.');
}
diagnostics.stage = 'parsing';
const doc = new window.ConllU.Document();
const logger = message => diagnostics.messages.push(message);
// This pinned parser flags the second trailing newline of standard
// UDPipe output as an empty sentence. Keep exactly one terminator.
const input = document.getElementById('input').value.replace(/\r\n/g, '\n').replace(/\n+$/, '\n');
doc.parse(input, logger);
const data = doc.toBrat(logger);
diagnostics.data = data;
if (doc.error || data.error) throw new Error('Invalid CoNLL-U: ' + diagnostics.messages.join('; '));
if (!data.entities.length) throw new Error('No tokens were produced.');
// WebFont custom URLs must serve CSS, not the demo's raw TTF URLs.
// Embed the same font bytes in downloaded SVGs for portable PNG export.
const webFontURLs = [];
window.embeddedFontCSS = '';
for (const [family, filename] of [
['PT Sans Caption', 'PT_Sans-Caption-Web-Regular.ttf'],
['Liberation Sans', 'Liberation_Sans-Regular.ttf']
]) {
const response = await fetch(root + 'static/fonts/' + filename, {signal: AbortSignal.timeout(15000)});
if (!response.ok) throw new Error('Could not load font ' + filename);
const bytes = new Uint8Array(await response.arrayBuffer());
let binary = '';
for (let offset = 0; offset < bytes.length; offset += 8192) {
binary += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
}
const css = '@font-face{font-family:"' + family + '";src:url(data:font/ttf;base64,' + btoa(binary) + ') format("truetype");}';
window.embeddedFontCSS += css;
webFontURLs.push('data:text/css;charset=utf-8,' + encodeURIComponent(css));
}
diagnostics.stage = 'rendering';
const dispatcher = new Dispatcher();
let commentPopup = document.getElementById('commentpopup');
if (!commentPopup) {
commentPopup = document.createElement('div');
commentPopup.id = 'commentpopup';
document.body.appendChild(commentPopup);
}
let commentTimer = null;
let isCommentVisible = false;
function adjustCommentPosition(evt) {
if (!commentPopup) return;
const popupRect = commentPopup.getBoundingClientRect();
const offset = 12;
let x = evt.clientX + offset;
let y = evt.clientY + offset;
if (x + popupRect.width > window.innerWidth - 8) {
x = evt.clientX - popupRect.width - offset;
}
if (y + popupRect.height > window.innerHeight - 8) {
y = evt.clientY - popupRect.height - offset;
}
commentPopup.style.left = Math.max(8, x) + 'px';
commentPopup.style.top = Math.max(8, y) + 'px';
}
dispatcher.on('displaySpanComment', function(evt, target, spanId, spanType, mods, spanText, commentText, commentType) {
clearTimeout(commentTimer);
let html = '<div class="comment_text">' + Util.escapeHTML(spanText) + '</div>';
if (spanType) {
html += '<div class="comment_type">' + Util.escapeHTML(spanType) + '</div>';
}
if (mods && mods.length) {
html += '<div class="comment_mods">';
mods.forEach(function(mod) {
html += '<div class="comment_mod_item">' + Util.escapeHTML(mod) + '</div>';
});
html += '</div>';
}
if (commentText) {
html += '<div class="comment_notes">' + Util.escapeHTMLwithNewlines(commentText) + '</div>';
}
commentPopup.innerHTML = html;
commentPopup.style.display = 'block';
commentPopup.style.opacity = '0';
adjustCommentPosition(evt);
commentTimer = setTimeout(function() {
commentPopup.style.transition = 'opacity 0.15s ease';
commentPopup.style.opacity = '1';
isCommentVisible = true;
}, 100);
});
dispatcher.on('hideComment', function() {
clearTimeout(commentTimer);
if (isCommentVisible) {
commentPopup.style.transition = 'opacity 0.15s ease';
commentPopup.style.opacity = '0';
commentTimer = setTimeout(function() {
commentPopup.style.display = 'none';
isCommentVisible = false;
}, 150);
} else {
commentPopup.style.display = 'none';
}
});
window.addEventListener('mousemove', function(evt) {
if (isCommentVisible) adjustCommentPosition(evt);
});
await new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('Tree rendering timed out.')), 15000);
dispatcher.on('doneRendering', function () {
const svg = document.querySelector('#vis svg');
if (!svg || !svg.querySelector('text') || svg.getBBox().width <= 0) return;
svg.querySelectorAll('.sentnum text').forEach((element, index) => {
if (data.sentlabels[index]) element.textContent = data.sentlabels[index];
});
clearTimeout(timer);
resolve();
});
dispatcher.on('renderError: Fatal', function () {
clearTimeout(timer);
reject(new Error('The tree renderer reported an error.'));
});
const labelWidth = Math.max(20, ...data.sentlabels.map(label => label ? label.length * 10 : 0));
Util.embed('vis', Config.bratCollData, data, webFontURLs, dispatcher, {sentNumMargin: labelWidth});
});
await document.fonts.ready;
diagnostics.stage = 'ready';
status.textContent = '';
document.getElementById('download-svg').addEventListener('click', downloadSVG);
document.getElementById('download-png').addEventListener('click', downloadPNG);
document.querySelectorAll('button').forEach(button => button.disabled = false);
// SVG paint order determines layering: draw arcs before POS groups.
for (const arcs of document.querySelectorAll('#vis .arcs')) {
arcs.parentNode.insertBefore(arcs, arcs.parentNode.firstChild);
}
// Match brat's configured POS padding around word backgrounds,
// including temporary backgrounds inserted by its hover handler.
const padding = Configuration.visual.margin;
function padWordBackgrounds() {
document.querySelectorAll('#vis svg > g.highlight > rect').forEach(rect => {
if (rect.dataset.wordPadded && !rect.dataset.wordChunk) return;
const originalX = Number(rect.getAttribute('x'));
const originalY = Number(rect.getAttribute('y'));
const word = Array.from(document.querySelectorAll('#vis tspan[data-chunk-id]')).find(word => {
const box = word.getBBox();
return rect.dataset.wordChunk ? word.dataset.chunkId === rect.dataset.wordChunk :
Math.abs(box.x - originalX) < 1 && Math.abs(box.y - originalY) < 4;
});
if (word) rect.dataset.wordChunk = word.dataset.chunkId;
const box = word ? word.getBBox() : {
x:originalX, y:originalY,
width:Number(rect.getAttribute('width')),
height:Number(rect.getAttribute('height'))
};
const wordWidth = word ? word.getSubStringLength(0, word.textContent.trimEnd().length) : box.width;
rect.setAttribute('x', box.x - padding.x);
rect.setAttribute('y', box.y - padding.y);
rect.setAttribute('width', wordWidth + padding.x * 2);
rect.setAttribute('height', box.height + padding.y * 2);
rect.dataset.wordPadded = 'true';
});
}
padWordBackgrounds();
const wordBackgrounds = document.querySelector('#vis svg > g.highlight');
if (wordBackgrounds) {
new MutationObserver(padWordBackgrounds).observe(wordBackgrounds, {childList:true});
}
// Highlight the syntactic head, rather than all connected tokens.
const heads = new Map();
for (const sentence of doc.sentences) {
for (const element of sentence.elements) {
if (element.isWord() && element.head !== '0' && element.head !== '_') {
heads.set(sentence.id + '-T' + element.id, sentence.id + '-T' + element.head);
}
}
}
const vis = document.getElementById('vis');
function highlightHead(target) {
vis.querySelectorAll('.hover-head, .hover-head-word').forEach(rect => rect.classList.remove('hover-head', 'hover-head-word'));
if (!(target instanceof Element)) return;
let token = target.closest('.span')?.querySelector('rect[data-span-id]');
const word = target.closest('tspan[data-chunk-id]');
if (!token && word) {
const box = word.getBoundingClientRect();
const center = (box.left + box.right) / 2;
token = Array.from(vis.querySelectorAll('rect[data-span-id]')).filter(rect => {
const pos = rect.getBoundingClientRect();
return center >= pos.left - 4 && center <= pos.right + 4 &&
box.top >= pos.bottom && box.top - pos.bottom < box.height * 3;
}).sort((a, b) => b.getBoundingClientRect().bottom - a.getBoundingClientRect().bottom)[0];
}
const head = token && heads.get(token.dataset.spanId);
if (head) {
vis.querySelectorAll('rect[data-span-id]').forEach(rect => {
if (rect.dataset.spanId !== head) return;
rect.classList.add('hover-head');
const pos = rect.getBoundingClientRect();
const center = (pos.left + pos.right) / 2;
// brat already draws a background behind each word.
// Color that rectangle without altering the text or layout.
vis.querySelectorAll('svg > g.highlight > rect').forEach(background => {
const box = background.getBoundingClientRect();
if (center >= box.left - 4 && center <= box.right + 4 &&
box.top >= pos.bottom && box.top - pos.bottom < box.height * 3) {
background.classList.add('hover-head-word');
}
});
});
}
}
vis.addEventListener('mouseover', event => highlightHead(event.target));
vis.addEventListener('mouseout', event => highlightHead(event.relatedTarget));
vis.addEventListener('mouseleave', () => highlightHead(null));
// Each brat arc segment starts at the label and ends at the token.
// Suppress the label marker when the two marker anchors crowd each
// other (14 native SVG units, independent of the current view zoom).
for (const path of document.querySelectorAll('#vis .arcs path')) {
const style = getComputedStyle(path);
if (style.markerStart === 'none' || style.markerEnd === 'none') continue;
const start = path.getPointAtLength(0);
const end = path.getPointAtLength(path.getTotalLength());
const gap = Math.hypot(end.x - start.x, end.y - start.y);
if (gap < 14) {
path.style.setProperty('marker-start', 'none', 'important');
path.style.setProperty('--inactive-marker-start', 'none');
path.dataset.labelMarkerSuppressed = 'true';
// brat offsets the label-side start by the marker width.
// Restore that space as a straight segment, leaving one unit
// before the label. Retain the rest of the curve unchanged.
const markerMatch = style.markerStart.match(/#([^"')]+)["']?\)/);
const marker = markerMatch && document.getElementById(markerMatch[1]);
const markerWidth = marker ? marker.markerWidth.baseVal.value : 5;
const next = path.getPointAtLength(Math.min(1, path.getTotalLength()));
const direction = Math.sign(next.x - start.x);
const extension = Math.max(0, markerWidth - 1);
if (direction && extension) {
const original = path.getAttribute('d');
const initialMove = /^\s*M\s*[-+\d.eE]+[\s,]+[-+\d.eE]+/;
if (initialMove.test(original)) {
const newX = start.x - direction * extension;
path.setAttribute('d', 'M' + newX + ',' + start.y +
' L' + start.x + ',' + start.y + original.replace(initialMove, ''));
path.dataset.labelLineExtension = String(extension);
path.dataset.originalLabelX = String(start.x);
path.dataset.originalTip = end.x + ',' + end.y;
}
}
}
}
// brat shares markers across arcs. Give inactive arcs gray copies
// rather than recoloring the shared definitions used by active arcs.
const markerCopies = new Map();
for (const path of document.querySelectorAll('#vis .arcs path')) {
for (const position of ['start', 'end']) {
const property = 'marker-' + position;
const reference = getComputedStyle(path).getPropertyValue(property);
const match = reference.match(/#([^"')]+)["']?\)/);
if (!match) continue;
const original = document.getElementById(match[1]);
if (!original) continue;
let copy = markerCopies.get(original.id);
if (!copy) {
copy = original.cloneNode(true);
copy.id = original.id + '-inactive';
original.querySelectorAll('path, polygon, polyline, circle, rect').forEach((shape, index) => {
const clone = copy.querySelectorAll('path, polygon, polyline, circle, rect')[index];
const style = getComputedStyle(shape);
if (style.fill !== 'none') clone.style.fill = '#c4cbd3';
if (style.stroke !== 'none') clone.style.stroke = '#c4cbd3';
});
original.parentNode.appendChild(copy);
markerCopies.set(original.id, copy);
}
path.style.setProperty('--inactive-marker-' + position, 'url(#' + copy.id + ')');
}
}
// Zoom changes SVG viewport dimensions, preserving its original fonts.
const svg = document.querySelector('#vis svg');
const width = svg.width.baseVal.value, height = svg.height.baseVal.value + padding.y * 2 + 2;
svg.dataset.nativeWidth = width;
svg.dataset.nativeHeight = height;
// Measure tree marks and words on every rendered row, including wrapped
// lines and later sentences, so zoom and exports retain all text.
const bounds = svg.getBoundingClientRect();
const marks = Array.from(svg.querySelectorAll('.span rect, .span path, .span text, .arcs path, .arcs text, svg > g.text > text tspan, .sentnum path, .sentnum text'));
const contentWidth = Math.min(width, Math.max(...marks.map(mark => mark.getBoundingClientRect().right - bounds.left)) + 12);
svg.dataset.contentWidth = contentWidth;
svg.dataset.contentHeight = height;
svg.setAttribute('viewBox', '0 0 ' + contentWidth + ' ' + height);
const resize = () => {
if (window.frameElement && !manualSize && !document.fullscreenElement) {
window.frameElement.style.height = Math.ceil(document.body.getBoundingClientRect().height + 2) + 'px';
}
};
let zoom = 1.4;
const out = document.getElementById('zoom-out');
const into = document.getElementById('zoom-in');
const reset = document.getElementById('zoom-reset');
function setZoom(value) {
zoom = Math.max(.5, Math.min(3, Math.round(value * 100) / 100));
svg.style.width = contentWidth * zoom + 'px';
svg.style.height = height * zoom + 'px';
padWordBackgrounds();
diagnostics.zoom = zoom;
out.disabled = zoom <= .5;
into.disabled = zoom >= 3;
reset.disabled = zoom === 1.4;
reset.title = 'Reset zoom to 140% (current: ' + Math.round(zoom * 100) + '%)';
resize();
}
out.addEventListener('click', () => setZoom(zoom - .2));
into.addEventListener('click', () => setZoom(zoom + .2));
reset.addEventListener('click', () => setZoom(1.4));
setZoom(1.4);
const toggle = document.getElementById('toggle-view-controls');
const controls = document.getElementById('zoom-controls');
toggle.addEventListener('click', () => {
controls.hidden = !controls.hidden;
toggle.setAttribute('aria-expanded', String(!controls.hidden));
const label = controls.hidden ? 'Show view controls' : 'Hide view controls';
toggle.setAttribute('aria-label', label);
toggle.title = label;
resize();
});
const fullscreen = document.getElementById('fullscreen');
fullscreen.disabled = !document.fullscreenEnabled;
if (!document.fullscreenEnabled) fullscreen.title = 'Fullscreen unavailable in this embedding';
fullscreen.addEventListener('click', async () => {
try {
if (document.fullscreenElement) await document.exitFullscreen();
else await document.getElementById('tree-view').requestFullscreen();
} catch (error) { status.textContent = 'Fullscreen unavailable: ' + error.message; }
});
document.addEventListener('fullscreenchange', () => {
const label = document.fullscreenElement ? 'Exit fullscreen' : 'Enter fullscreen';
fullscreen.setAttribute('aria-label', label);
fullscreen.title = label;
resize();
});
resize();
new ResizeObserver(resize).observe(document.body);
} catch (error) {
fail(error);
}
})();
|