mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
keep popover open on hover
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { FC, useRef, useState, MouseEvent, useLayoutEffect } from "react";
|
import React, { FC, useRef, useState, MouseEvent, useLayoutEffect, useReducer, Dispatch } from "react";
|
||||||
import { Person, Repository } from "@scm-manager/ui-types";
|
import { Person, Repository } from "@scm-manager/ui-types";
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -32,6 +32,7 @@ import styled from "styled-components";
|
|||||||
import DateShort from "./DateShort";
|
import DateShort from "./DateShort";
|
||||||
import { SingleContributor } from "./repos/changesets";
|
import { SingleContributor } from "./repos/changesets";
|
||||||
import DateFromNow from "./DateFromNow";
|
import DateFromNow from "./DateFromNow";
|
||||||
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
// TODO move types to ui-types
|
// TODO move types to ui-types
|
||||||
|
|
||||||
@@ -98,7 +99,8 @@ const PopoverContainer = styled.div`
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
left: 2.25em;
|
left: 2.25em;
|
||||||
z-index: 100;
|
z-index: 100;
|
||||||
width: 35em;
|
width: 30em;
|
||||||
|
display: block;
|
||||||
|
|
||||||
&:before {
|
&:before {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -156,16 +158,17 @@ type LineProps = {
|
|||||||
annotation: AnnotatedLine;
|
annotation: AnnotatedLine;
|
||||||
showAnnotation: boolean;
|
showAnnotation: boolean;
|
||||||
nr: number;
|
nr: number;
|
||||||
repository: Repository;
|
dispatch: Dispatch<Action>;
|
||||||
setPopOver: (popover: React.ReactNode) => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type PopoverProps = {
|
type PopoverProps = {
|
||||||
annotation: AnnotatedLine;
|
annotation: AnnotatedLine;
|
||||||
offsetTop?: number;
|
offsetTop?: number;
|
||||||
|
dispatch: Dispatch<Action>;
|
||||||
|
repository: Repository;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Popover: FC<PopoverProps> = ({ annotation, offsetTop }) => {
|
const Popover: FC<PopoverProps> = ({ annotation, offsetTop, repository, dispatch }) => {
|
||||||
const [height, setHeight] = useState(125);
|
const [height, setHeight] = useState(125);
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
@@ -174,15 +177,38 @@ const Popover: FC<PopoverProps> = ({ annotation, offsetTop }) => {
|
|||||||
}
|
}
|
||||||
}, [ref]);
|
}, [ref]);
|
||||||
|
|
||||||
|
const onMouseEnter = (e: MouseEvent) => {
|
||||||
|
dispatch({
|
||||||
|
type: "enter-popover"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const OnMouseLeave = (e: MouseEvent) => {
|
||||||
|
dispatch({
|
||||||
|
type: "leave-popover"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const top = (offsetTop || 0) - height - 5;
|
const top = (offsetTop || 0) - height - 5;
|
||||||
return (
|
return (
|
||||||
<PopoverContainer ref={ref} className="box changeset-details is-family-primary" style={{ top: `${top}px` }}>
|
<PopoverContainer
|
||||||
|
ref={ref}
|
||||||
|
onMouseEnter={onMouseEnter}
|
||||||
|
onMouseLeave={OnMouseLeave}
|
||||||
|
className="box changeset-details is-family-primary"
|
||||||
|
style={{ top: `${top}px` }}
|
||||||
|
>
|
||||||
<PopoverHeading className="is-clearfix">
|
<PopoverHeading className="is-clearfix">
|
||||||
<SingleContributor className="is-pulled-left" person={annotation.author} />
|
<SingleContributor className="is-pulled-left" person={annotation.author} />
|
||||||
<DateFromNow className="is-pulled-right" date={annotation.when} />
|
<DateFromNow className="is-pulled-right" date={annotation.when} />
|
||||||
</PopoverHeading>
|
</PopoverHeading>
|
||||||
<SmallHr />
|
<SmallHr />
|
||||||
<p className="has-text-info">Changeset {shortRevision(annotation.revision)}</p>
|
<p>
|
||||||
|
Changeset{" "}
|
||||||
|
<Link to={`/repo/${repository.namespace}/${repository.name}/code/changeset/${annotation.revision}`}>
|
||||||
|
{shortRevision(annotation.revision)}
|
||||||
|
</Link>
|
||||||
|
</p>
|
||||||
<PopoverDescription className="content">{annotation.description}</PopoverDescription>
|
<PopoverDescription className="content">{annotation.description}</PopoverDescription>
|
||||||
</PopoverContainer>
|
</PopoverContainer>
|
||||||
);
|
);
|
||||||
@@ -196,18 +222,30 @@ const EmptyMetadata = styled(LineElement)`
|
|||||||
width: 16.7em;
|
width: 16.7em;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const AnnotateLine: FC<LineProps> = ({ annotation, showAnnotation, setPopOver, nr, children }) => {
|
const dispatchDeferred = (dispatch: Dispatch<Action>, action: Action) => {
|
||||||
|
setTimeout(() => dispatch(action), 250);
|
||||||
|
};
|
||||||
|
|
||||||
|
const AnnotateLine: FC<LineProps> = ({ annotation, showAnnotation, dispatch, nr, children }) => {
|
||||||
const link = useRef<HTMLDivElement>(null);
|
const link = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
const onMouseEnter = (e: MouseEvent) => {
|
const onMouseEnter = (e: MouseEvent) => {
|
||||||
if (showAnnotation) {
|
if (showAnnotation) {
|
||||||
setPopOver(<Popover annotation={annotation} offsetTop={link.current?.offsetTop} />);
|
dispatchDeferred(dispatch, {
|
||||||
|
annotation,
|
||||||
|
line: nr,
|
||||||
|
offset: link.current!.offsetTop,
|
||||||
|
type: "enter-line"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const OnMouseLeave = (e: MouseEvent) => {
|
const OnMouseLeave = (e: MouseEvent) => {
|
||||||
if (showAnnotation) {
|
if (showAnnotation) {
|
||||||
setPopOver(null);
|
dispatchDeferred(dispatch, {
|
||||||
|
line: nr,
|
||||||
|
type: "leave-line"
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -233,8 +271,84 @@ const AnnotateLine: FC<LineProps> = ({ annotation, showAnnotation, setPopOver, n
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
annotation?: AnnotatedLine;
|
||||||
|
offset?: number;
|
||||||
|
line?: number;
|
||||||
|
onPopover: boolean;
|
||||||
|
onLine: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type EnterLine = {
|
||||||
|
annotation: AnnotatedLine;
|
||||||
|
offset: number;
|
||||||
|
line: number;
|
||||||
|
type: "enter-line";
|
||||||
|
};
|
||||||
|
|
||||||
|
type LeaveLine = {
|
||||||
|
line: number;
|
||||||
|
type: "leave-line";
|
||||||
|
};
|
||||||
|
|
||||||
|
type EnterPopover = {
|
||||||
|
type: "enter-popover";
|
||||||
|
};
|
||||||
|
|
||||||
|
type LeavePopover = {
|
||||||
|
type: "leave-popover";
|
||||||
|
};
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
onPopover: false,
|
||||||
|
onLine: false
|
||||||
|
};
|
||||||
|
|
||||||
|
type Action = EnterLine | LeaveLine | EnterPopover | LeavePopover;
|
||||||
|
|
||||||
|
const reducer = (state: State, action: Action): State => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "enter-line": {
|
||||||
|
if (state.onPopover) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
annotation: action.annotation,
|
||||||
|
offset: action.offset,
|
||||||
|
line: action.line,
|
||||||
|
onLine: true,
|
||||||
|
onPopover: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "leave-line": {
|
||||||
|
if (state.onPopover) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onLine: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return initialState;
|
||||||
|
}
|
||||||
|
case "enter-popover": {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onPopover: true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "leave-popover": {
|
||||||
|
if (state.onLine) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onPopover: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return initialState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const Annotate: FC<Props> = ({ source, repository }) => {
|
const Annotate: FC<Props> = ({ source, repository }) => {
|
||||||
const [popOver, setPopOver] = useState<React.ReactNode | null>(null);
|
const [state, dispatch] = useReducer(reducer, initialState);
|
||||||
|
|
||||||
const defaultRenderer = ({ rows, stylesheet, useInlineStyles }: any) => {
|
const defaultRenderer = ({ rows, stylesheet, useInlineStyles }: any) => {
|
||||||
let lastRevision = "";
|
let lastRevision = "";
|
||||||
@@ -251,13 +365,7 @@ const Annotate: FC<Props> = ({ source, repository }) => {
|
|||||||
const newAnnotation = annotation.revision !== lastRevision;
|
const newAnnotation = annotation.revision !== lastRevision;
|
||||||
lastRevision = annotation.revision;
|
lastRevision = annotation.revision;
|
||||||
return (
|
return (
|
||||||
<AnnotateLine
|
<AnnotateLine dispatch={dispatch} annotation={annotation} showAnnotation={newAnnotation} nr={i + 1}>
|
||||||
setPopOver={setPopOver}
|
|
||||||
annotation={annotation}
|
|
||||||
showAnnotation={newAnnotation}
|
|
||||||
nr={i + 1}
|
|
||||||
repository={repository}
|
|
||||||
>
|
|
||||||
{line}
|
{line}
|
||||||
</AnnotateLine>
|
</AnnotateLine>
|
||||||
);
|
);
|
||||||
@@ -267,6 +375,13 @@ const Annotate: FC<Props> = ({ source, repository }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let popover = null;
|
||||||
|
if ((state.onPopover || state.onLine) && state.annotation) {
|
||||||
|
popover = (
|
||||||
|
<Popover annotation={state.annotation} dispatch={dispatch} offsetTop={state.offset} repository={repository} />
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const code = source.lines.reduce((content, line) => {
|
const code = source.lines.reduce((content, line) => {
|
||||||
content += line.code + "\n";
|
content += line.code + "\n";
|
||||||
return content;
|
return content;
|
||||||
@@ -274,7 +389,7 @@ const Annotate: FC<Props> = ({ source, repository }) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ position: "relative" }}>
|
<div style={{ position: "relative" }}>
|
||||||
{popOver}
|
{popover}
|
||||||
<ReactSyntaxHighlighter
|
<ReactSyntaxHighlighter
|
||||||
showLineNumbers={false}
|
showLineNumbers={false}
|
||||||
language={source.language}
|
language={source.language}
|
||||||
|
|||||||
Reference in New Issue
Block a user