Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state bugs. #427

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions web/blueprint/src/lib/components/datasetView/SpanHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ import SpanHoverTooltip, {type SpanHoverNamedValue} from './SpanHoverTooltip.sve

export interface SpanHoverInfo {
namedValues: SpanHoverNamedValue[];
spansHovered: string[];
spansHovered: Set<string>;
isHovered: boolean;
itemScrollContainer: HTMLDivElement | null;
}
export function spanHover(element: HTMLSpanElement, spanHoverInfo: SpanHoverInfo) {
let tooltipComponent: SvelteComponent | undefined;
let curSpanHoverInfo = spanHoverInfo;
const itemScrollListener = () => destroyHoverElement();
showSpan();
if (curSpanHoverInfo.isHovered) {
showSpan();
}
function showSpan() {
if (!curSpanHoverInfo.isHovered) {
return;
}
curSpanHoverInfo.namedValues = spanHoverInfo.namedValues.filter(namedValue =>
curSpanHoverInfo.spansHovered.some(path =>
const namedValues = curSpanHoverInfo.namedValues.filter(namedValue =>
Array.from(curSpanHoverInfo.spansHovered).some(path =>
pathIsMatching(deserializePath(namedValue.spanPath), deserializePath(path))
)
);
if (curSpanHoverInfo.namedValues.length === 0) {
return;
}
if (curSpanHoverInfo.itemScrollContainer != null) {
curSpanHoverInfo.itemScrollContainer.addEventListener('scroll', itemScrollListener);
}
Expand All @@ -39,7 +35,7 @@ export function spanHover(element: HTMLSpanElement, spanHoverInfo: SpanHoverInfo

tooltipComponent = new SpanHoverTooltip({
props: {
namedValues: curSpanHoverInfo.namedValues,
namedValues,
x,
y: boundingRect.top
},
Expand All @@ -59,12 +55,11 @@ export function spanHover(element: HTMLSpanElement, spanHoverInfo: SpanHoverInfo
update(spanHoverInfo: SpanHoverInfo) {
curSpanHoverInfo = spanHoverInfo;

if (!spanHoverInfo.isHovered) {
if (!curSpanHoverInfo.isHovered) {
destroyHoverElement();
} else {
showSpan();
}
tooltipComponent?.$set({namedValues: curSpanHoverInfo.namedValues});
},
destroy() {
destroyHoverElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@
use:spanHover={{
namedValues: renderSpan.hoverInfo,
isHovered: renderSpan.isFirstHover,
spansHovered: Array.from(pathsHovered),
spansHovered: pathsHovered,
itemScrollContainer: $itemScrollContainer
}}
use:spanClick={{
Expand Down
41 changes: 20 additions & 21 deletions web/blueprint/src/lib/view_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,20 @@ export interface MergedSpan {
}

// Split a merged span up into smaller chunks to help with snippeting.
function splitMergedSpan(mergedSpan: MergedSpan): MergedSpan[] {
function chunkText(text: string): MergedSpan[] {
const splitBy = '\n';
const splits = mergedSpan.text.split(splitBy);
const splits = text.split(splitBy);
const splitSpans: MergedSpan[] = [];
let lastEnd = mergedSpan.span?.start || 0;
let lastEnd = 0;
for (let i = 0; i < splits.length; i++) {
const text = splits[i] + (i < splits.length - 1 ? splitBy : '');
const end = lastEnd + text.length;
const span = {start: lastEnd, end};
splitSpans.push({
text,
span,
originalSpans: mergedSpan.originalSpans,
paths: mergedSpan.paths
originalSpans: {},
paths: []
});
lastEnd = end;
}
Expand Down Expand Up @@ -260,6 +260,9 @@ export function mergeSpans(
inputSpanSets: {[spanSet: string]: LilacValueNodeCasted<'string_span'>[]}
): MergedSpan[] {
const spanSetKeys = Object.keys(inputSpanSets);
if (spanSetKeys.length === 0) {
return chunkText(text);
}
const textLength = text.length;

// Maps a span set to the index of the spans we're currently processing for each span set.
Expand Down Expand Up @@ -329,14 +332,12 @@ export function mergeSpans(
.map(span => L.path(span as LilacValueNode))
.map(path => serializePath(path!));

mergedSpans.push(
...splitMergedSpan({
text: text.slice(curStartIdx, curEndIndex),
span: {start: curStartIdx, end: curEndIndex},
originalSpans: spansInRange,
paths
})
);
mergedSpans.push({
text: text.slice(curStartIdx, curEndIndex),
span: {start: curStartIdx, end: curEndIndex},
originalSpans: spansInRange,
paths
});

// Advance the spans that have the span end index.
for (const spanSet of Object.keys(spanSetIndices)) {
Expand Down Expand Up @@ -364,14 +365,12 @@ export function mergeSpans(

// If the text has more characters than spans, emit a final empty span.
if (curStartIdx < text.length) {
mergedSpans.push(
...splitMergedSpan({
text: text.slice(curStartIdx, text.length),
span: {start: curStartIdx, end: text.length},
originalSpans: {},
paths: []
})
);
mergedSpans.push({
text: text.slice(curStartIdx, text.length),
span: {start: curStartIdx, end: text.length},
originalSpans: {},
paths: []
});
}

return mergedSpans;
Expand Down