diff --git a/web/assets/base.css b/web/assets/base.css index a24c307..f178350 100644 --- a/web/assets/base.css +++ b/web/assets/base.css @@ -61,6 +61,8 @@ kbd { font-family: var(--font-mono); font-size: 10.5px; padding: 1px 5px; border /* Code. */ mark.hit { background: oklch(var(--hit)); color: inherit; border-radius: 2px; padding: 0 1px; } +/* Search field hover card (dark tip): matches keep readable ink on the hit colour. */ +.field-tip mark.hit { color: oklch(var(--ink)); } .code-line { display: flex; } .code-line.sel { background: oklch(var(--live) / 0.08); box-shadow: inset 2px 0 oklch(var(--live)); } .tok-k { color: oklch(0.55 0.17 300); } diff --git a/web/assets/field.js b/web/assets/field.js index 766fa0d..27d3af0 100644 --- a/web/assets/field.js +++ b/web/assets/field.js @@ -44,12 +44,18 @@ var f = self.pick(e); canvas.style.cursor = f && f.href ? 'pointer' : ''; var key = f ? f.key : null; - if (key !== self.hoverKey) { self.hoverKey = key; self.focus(key); } + if (key !== self.hoverKey) { self.hoverKey = key; self.focus(key); self.card(f && f.tip ? f.tip() : '', key); } }); canvas.addEventListener('mouseleave', function () { canvas.style.cursor = ''; if (self.hoverKey) { self.hoverKey = null; self.focus(null); } + self.card(''); }); + // The card is placed against the cluster; scrolling moves the cluster away. + window.addEventListener('scroll', function () { + if (self.hoverKey) { self.hoverKey = null; self.focus(null); } + self.card(''); + }, true); canvas.addEventListener('click', function (e) { var f = self.pick(e); if (!f || !f.href) return; @@ -58,6 +64,45 @@ }); } + // The hover card of a result: the site's dark tip with an arrow, placed + // below the result's dot cluster (above when there is no room below), + // with the arrow left of the tip's centre, kept inside the window; the arrow points at the + // cluster's centre. Empty html hides it. + Field.prototype.card = function (html, key) { + var idxs = key && this.files[key]; + if (!html || !idxs || !idxs.length) { if (this.cardEl) this.cardEl.style.visibility = 'hidden'; return; } + if (!this.cardEl) { + var el = document.createElement('div'); + el.setAttribute('role', 'tooltip'); + // The site's tip style (app.js data-tip), sized for a few code lines. + el.className = 'pointer-events-none fixed z-[60] w-[440px] max-w-[calc(100vw-16px)] rounded-control bg-ink px-3 py-2.5 text-[12.5px] leading-snug text-paper shadow-lg'; + el.innerHTML = '
'; + document.body.appendChild(el); + this.cardEl = el; + } + var el = this.cardEl, arrow = el.querySelector('[data-arrow]'); + el.querySelector('[data-body]').innerHTML = html; + el.style.visibility = 'hidden'; el.style.left = '0px'; el.style.top = '0px'; + // The cluster's box in window coordinates. + var c = this.c.getBoundingClientRect(), x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity; + for (var k = 0; k < idxs.length; k++) { + var i = idxs[k]; + x0 = Math.min(x0, this.x[i]); x1 = Math.max(x1, this.x[i]); y0 = Math.min(y0, this.y[i]); y1 = Math.max(y1, this.y[i]); + } + var cx = c.left + (x0 + x1) / 2, top = c.top + y0 - 4, bottom = c.top + y1 + 4, gap = 9; + var w = el.offsetWidth, h = el.offsetHeight; + // The arrow sits left of the tip's centre (a quarter of its width), not mid-way. + var left = Math.min(Math.max(8, cx - Math.min(w / 4, 96)), window.innerWidth - w - 8); + var above = bottom + gap + h > window.innerHeight - 8 && top - gap - h >= 8, y = above ? top - gap - h : bottom + gap; + el.style.left = left + 'px'; el.style.top = y + 'px'; + // The arrow follows the cluster even when the card is shifted to stay on screen. + var ax = Math.min(Math.max(12, cx - left), w - 12); + arrow.style.left = (ax - 5) + 'px'; + arrow.style.top = above ? '' : '-5px'; + arrow.style.bottom = above ? '-5px' : ''; + el.style.visibility = 'visible'; + }; + // The settled file under the pointer: its label, or the hit cluster nearest to it. Field.prototype.pick = function (e) { if (this.mode !== 'settled' || !this.lastFiles || !this.n) return null; @@ -107,6 +152,7 @@ this.hit = new Float32Array(this.n); // 0 = no hit, otherwise reveal time (ms) this.owner = new Int32Array(this.n).fill(-1); this.hitDim = new Uint8Array(this.n); + this.shared = new Uint8Array(this.n); // dots claimed by more than one result cluster for (var i = 0; i < this.n; i++) this.phase[i] = rand(i, 7) * Math.PI * 2; if (this.opts.onLayout) this.opts.onLayout(this.n, this.shards); if (this.mode === 'settled') this.placeHits(this.lastFiles || []); @@ -130,6 +176,7 @@ Field.prototype.candidates = function () { return Math.round(this.frac * this.n); }; Field.prototype.scan = function () { + this.card(''); this.labels = []; this.labelBoxes = []; this.mode = 'scan'; this.pending = null; @@ -158,6 +205,7 @@ var rows = this.rows, cols = Math.floor(this.n / rows); this.hit.fill(0); this.hitDim.fill(0); + this.shared.fill(0); this.owner.fill(-1); this.files = {}; this.labels = []; this.labelBoxes = []; @@ -173,16 +221,27 @@ idxs.push(i); self.hit[i] = now + 40 + Math.abs(self.x[i] - originX) * 0.12 + fi * stagger + j * 10; self.hitDim[i] = j >= lit ? 1 : 0; + if (self.owner[i] >= 0 && self.owner[i] !== fi) self.shared[i] = 1; self.owner[i] = fi; } self.files[f.key] = idxs; if (idxs.length) self.labels.push({ key: f.key, text: f.label || '', i: idxs[idxs.length - 1], at: self.hit[idxs[0]] + 180 }); }); + // A crossing shows inside the overlap only: its edge (dots with a 4-neighbour + // outside the overlap) is drawn as an ordinary lit hit. + var edge = []; + for (var i = 0; i < this.n; i++) { + if (!this.shared[i]) continue; + var r = i % rows, near = [r > 0 ? i - 1 : -1, r < rows - 1 ? i + 1 : -1, i - rows, i + rows]; + for (var q = 0; q < 4; q++) { var nb = near[q]; if (nb >= 0 && nb < this.n && !this.shared[nb]) { edge.push(i); break; } } + } + for (var e = 0; e < edge.length; e++) { this.shared[edge[e]] = 0; this.hitDim[edge[e]] = 0; } }; Field.prototype.focus = function (key) { this.focusKey = key; }; Field.prototype.reset = function () { + this.card(''); this.labels = []; this.pending = null; this.labelBoxes = []; this.mode = 'idle'; this.frac = 0; this.hit.fill(0); this.owner.fill(-1); this.scanned.fill(0); this.focusKey = null; this.lastFiles = []; @@ -242,6 +301,8 @@ if (h && now >= h) { var pop = Math.min(1, (now - h) / 260); ink = (this.hitDim[i] ? 0.12 + 0.2 * pop : 0.3 + 0.7 * pop); + // Where clusters cross: normal size, between a lit hit and a background dot. + if (this.shared[i]) ink = 0.15 + 0.35 * pop; if (focusSet && focusSet[i]) { blue = 1; ink = 0; } } } @@ -252,6 +313,7 @@ if (blue > 0.02) blueBins[Math.round(Math.min(1, blue) * LEVELS)].push(i); } + this.now = now; ctx.clearRect(0, 0, w, this.h); this.drawBins(inkBins, this.inkColor, R); this.drawBins(blueBins, this.liveColor, R + 0.25); @@ -300,7 +362,9 @@ ctx.globalAlpha = lv / LEVELS; ctx.beginPath(); for (var j = 0; j < list.length; j++) { - var i = list[j], rr = r + (this.hit[i] && !this.hitDim[i] && lv > LEVELS * 0.8 ? 0.7 : 0); + // A result dot has one size whatever its colour (hover only recolours): + // lit hits grow once they have popped in; dim and crossing dots stay at R. + var i = list[j], rr = this.hit[i] && this.now >= this.hit[i] ? R + (!this.hitDim[i] && !this.shared[i] && this.now - this.hit[i] >= 180 ? 0.7 : 0) : r; ctx.moveTo(this.x[i] + rr, this.y[i]); ctx.arc(this.x[i], this.y[i], rr, 0, Math.PI * 2); } diff --git a/web/assets/search-page.js b/web/assets/search-page.js index a5f89fa..97bd615 100644 --- a/web/assets/search-page.js +++ b/web/assets/search-page.js @@ -155,6 +155,17 @@ async function prepare() { $('#retry').onclick=()=>schedule(true); }catch(e){if(seq===state.seq)errorNotice(e);} } +// The hover card of a result in the field: path, match count with line +// numbers, and the first matching lines (highlighted like the result list). +function hitCard(f,filesOnly){ + const slash=f.path.lastIndexOf('/'), nums=f.lines.map(l=>l.LineNumber), shown=nums.slice(0,12); + const count=filesOnly?t('命中文件','matching file'):!f.lines.length?t('路径命中','path match'):t(f.count+' 处',f.count+(f.count>1?' matches':' match')); + const where=shown.length?' · '+t('行 ','lines ')+shown.join(', ')+(nums.length>shown.length?t(' 等 '+nums.length+' 行',' and '+(nums.length-shown.length)+' more'):''):''; + const lines=f.lines.slice(0,3).map(l=>'
'+l.LineNumber+''+highlightBytes(l.Line,l.LineFragments).replace(/\r?\n$/,'')+'
').join(''); + return '

'+esc(f.path.slice(0,slash+1))+''+esc(f.path.slice(slash+1))+'

'+ + '

'+count+where+'

'+ + (lines?'
'+lines+'
':'')+'
'; +} function renderResults() { const out=state.result;if(!out){$('#results').innerHTML='';return;} const commit=out.meta.Commit, repo=out.meta.Repository; @@ -193,7 +204,8 @@ async function search(push=false) { state.result=normalizeSearch(out,repo,revision);state.roundtrip=Math.round(performance.now()-start);state.showAll=false; if(!FULL_SHA.test(revision)){const mains=(row(repo)?.policy?.branches||[]).filter(b=>b==='main'||b==='master');state.branch=revision||row(repo)?.default_branch||(mains.length===1?mains[0]:'');} state.revision=state.result.meta.Commit;scope();queryURL(false,q); - field.settle(state.result.files.map(f=>{const line=f.lines[0]?.LineNumber;return {key:f.path,n:f.count,label:f.path.split('/').at(-1)+(line?':'+line:''),href:codeURL(repo,state.result.meta.Commit,f.path,line?[line,line]:null,state.branch)};}));metrics(state.result);setState(out.Meta.Partial||out.Meta.Truncated?'partial':state.result.files.length?'done':'empty'); + const filesOnly=state.result.meta.FilesOnly; + field.settle(state.result.files.map(f=>{const line=f.lines[0]?.LineNumber;return {key:f.path,n:f.count,label:f.path.split('/').at(-1)+(line?':'+line:''),href:codeURL(repo,state.result.meta.Commit,f.path,line?[line,line]:null,state.branch),tip:()=>hitCard(f,filesOnly)};}));metrics(state.result);setState(out.Meta.Partial||out.Meta.Truncated?'partial':state.result.files.length?'done':'empty'); if(q.pattern===input.value&&document.startViewTransition&&!matchMedia('(prefers-reduced-motion: reduce)').matches)document.startViewTransition(renderResults);else renderResults(); }catch(e){if(seq===state.seq&&e.name!=='AbortError')errorNotice(e);}finally{if(seq===state.seq)$('#progress').classList.remove('on');} } diff --git a/web/index.html b/web/index.html index f9153a7..e51a019 100644 --- a/web/index.html +++ b/web/index.html @@ -5,12 +5,12 @@ {{brand}} - + - +