AnnotationEditor: simplify plugin (#37463)

pull/38016/head
Leon Sorokin 4 years ago committed by GitHub
parent befdfedfb6
commit ecfa32c8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 63
      public/app/plugins/panel/timeseries/plugins/AnnotationEditorPlugin.tsx
  2. 2
      public/app/plugins/panel/timeseries/plugins/ContextMenuPlugin.tsx

@ -31,25 +31,21 @@ export const AnnotationEditorPlugin: React.FC<AnnotationEditorPluginProps> = ({
plotInstance.setSelect({ top: 0, left: 0, width: 0, height: 0 }); plotInstance.setSelect({ top: 0, left: 0, width: 0, height: 0 });
} }
setIsAddingAnnotation(false); setIsAddingAnnotation(false);
}, [setSelection, , setIsAddingAnnotation, plotCtx]); }, [plotCtx]);
useLayoutEffect(() => { useLayoutEffect(() => {
let annotating = false; let annotating = false;
let isClick = false;
const setSelect = (u: uPlot) => { const setSelect = (u: uPlot) => {
if (annotating) { if (annotating) {
setIsAddingAnnotation(true); setIsAddingAnnotation(true);
const min = u.posToVal(u.select.left, 'x');
const max = u.posToVal(u.select.left + u.select.width, 'x');
setSelection({ setSelection({
min, min: u.posToVal(u.select.left, 'x'),
max, max: u.posToVal(u.select.left + u.select.width, 'x'),
bbox: { bbox: {
left: u.select.left, left: u.select.left,
top: 0, top: 0,
height: u.bbox.height / window.devicePixelRatio, height: u.select.height,
width: u.select.width, width: u.select.width,
}, },
}); });
@ -61,19 +57,17 @@ export const AnnotationEditorPlugin: React.FC<AnnotationEditorPluginProps> = ({
config.addHook('init', (u) => { config.addHook('init', (u) => {
// Wrap all setSelect hooks to prevent them from firing if user is annotating // Wrap all setSelect hooks to prevent them from firing if user is annotating
const setSelectHooks = u.hooks['setSelect']; const setSelectHooks = u.hooks.setSelect;
if (setSelectHooks) { if (setSelectHooks) {
for (let i = 0; i < setSelectHooks.length; i++) { for (let i = 0; i < setSelectHooks.length; i++) {
const hook = setSelectHooks[i]; const hook = setSelectHooks[i];
if (hook === setSelect) {
continue;
}
setSelectHooks[i] = (...args) => { if (hook !== setSelect) {
if (!annotating) { setSelectHooks[i] = (...args) => {
hook!(...args); !annotating && hook!(...args);
} };
}; }
} }
} }
}); });
@ -81,40 +75,23 @@ export const AnnotationEditorPlugin: React.FC<AnnotationEditorPluginProps> = ({
config.setCursor({ config.setCursor({
bind: { bind: {
mousedown: (u, targ, handler) => (e) => { mousedown: (u, targ, handler) => (e) => {
if (e.button === 0) { annotating = e.button === 0 && (e.metaKey || e.ctrlKey);
handler(e); handler(e);
if (e.metaKey || e.ctrlKey) {
isClick = true;
annotating = true;
}
}
return null;
},
mousemove: (u, targ, handler) => (e) => {
if (e.button === 0) {
handler(e);
// handle cmd+drag
if (e.metaKey || e.ctrlKey) {
isClick = false;
annotating = true;
}
}
return null; return null;
}, },
mouseup: (u, targ, handler) => (e) => { mouseup: (u, targ, handler) => (e) => {
// handle cmd+click // uPlot will not fire setSelect hooks for 0-width && 0-height selections
if (isClick && u.cursor.left && e.button === 0 && (e.metaKey || e.ctrlKey)) { // so we force it to fire on single-point clicks by mutating left & height
u.setSelect({ left: u.cursor.left, width: 0, top: 0, height: 0 }); if (annotating && u.select.width === 0) {
annotating = true; u.select.left = u.cursor.left!;
u.select.height = u.bbox.height / window.devicePixelRatio;
} }
handler(e); handler(e);
return null; return null;
}, },
}, },
}); });
}, [config, setIsAddingAnnotation]); }, [config]);
const startAnnotating = useCallback<StartAnnotatingFn>( const startAnnotating = useCallback<StartAnnotatingFn>(
({ coords }) => { ({ coords }) => {
@ -146,7 +123,7 @@ export const AnnotationEditorPlugin: React.FC<AnnotationEditorPluginProps> = ({
}); });
setIsAddingAnnotation(true); setIsAddingAnnotation(true);
}, },
[plotCtx, setSelection, setIsAddingAnnotation] [plotCtx]
); );
return ( return (

@ -108,7 +108,7 @@ export const ContextMenuPlugin: React.FC<ContextMenuPluginProps> = ({
// TODO: remove listeners on unmount // TODO: remove listeners on unmount
plotCanvas.current?.addEventListener('mouseup', (e: MouseEvent) => { plotCanvas.current?.addEventListener('mouseup', (e: MouseEvent) => {
// ignore cmd+click, this is handled by annotation editor // ignore cmd+click, this is handled by annotation editor
if (!isClick || e.metaKey) { if (!isClick || e.metaKey || e.ctrlKey) {
setPoint(null); setPoint(null);
return; return;
} }

Loading…
Cancel
Save