2020-07-02 23:45:43 +02:00
|
|
|
import { useEffect, useCallback, useRef, useState } from 'react';
|
2020-06-06 17:00:20 +02:00
|
|
|
|
|
|
|
export function cursorPositionToTextOffset(cursorPos: any, body: string) {
|
|
|
|
if (!body) return 0;
|
|
|
|
|
|
|
|
const noteLines = body.split('\n');
|
|
|
|
|
|
|
|
let pos = 0;
|
|
|
|
for (let i = 0; i < noteLines.length; i++) {
|
|
|
|
if (i > 0) pos++; // Need to add the newline that's been removed in the split() call above
|
|
|
|
|
|
|
|
if (i === cursorPos.line) {
|
|
|
|
pos += cursorPos.ch;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
pos += noteLines[i].length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function usePrevious(value: any): any {
|
|
|
|
const ref = useRef();
|
|
|
|
useEffect(() => {
|
|
|
|
ref.current = value;
|
|
|
|
});
|
|
|
|
return ref.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useScrollHandler(editorRef: any, webviewRef: any, onScroll: Function) {
|
|
|
|
const ignoreNextEditorScrollEvent_ = useRef(false);
|
|
|
|
const scrollTimeoutId_ = useRef<any>(null);
|
|
|
|
|
|
|
|
const scheduleOnScroll = useCallback((event: any) => {
|
|
|
|
if (scrollTimeoutId_.current) {
|
|
|
|
clearTimeout(scrollTimeoutId_.current);
|
|
|
|
scrollTimeoutId_.current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
scrollTimeoutId_.current = setTimeout(() => {
|
|
|
|
scrollTimeoutId_.current = null;
|
|
|
|
onScroll(event);
|
|
|
|
}, 10);
|
|
|
|
}, [onScroll]);
|
|
|
|
|
|
|
|
const setEditorPercentScroll = useCallback((p: number) => {
|
|
|
|
ignoreNextEditorScrollEvent_.current = true;
|
|
|
|
|
|
|
|
if (editorRef.current) {
|
|
|
|
editorRef.current.setScrollPercent(p);
|
|
|
|
|
|
|
|
scheduleOnScroll({ percent: p });
|
|
|
|
}
|
|
|
|
}, [scheduleOnScroll]);
|
|
|
|
|
|
|
|
const setViewerPercentScroll = useCallback((p: number) => {
|
|
|
|
if (webviewRef.current) {
|
|
|
|
webviewRef.current.wrappedInstance.send('setPercentScroll', p);
|
|
|
|
scheduleOnScroll({ percent: p });
|
|
|
|
}
|
|
|
|
}, [scheduleOnScroll]);
|
|
|
|
|
|
|
|
const editor_scroll = useCallback(() => {
|
|
|
|
if (ignoreNextEditorScrollEvent_.current) {
|
|
|
|
ignoreNextEditorScrollEvent_.current = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editorRef.current) {
|
|
|
|
const percent = editorRef.current.getScrollPercent();
|
|
|
|
|
|
|
|
setViewerPercentScroll(percent);
|
|
|
|
}
|
|
|
|
}, [setViewerPercentScroll]);
|
|
|
|
|
|
|
|
const resetScroll = useCallback(() => {
|
|
|
|
if (editorRef.current) {
|
|
|
|
editorRef.current.setScrollPercent(0);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { resetScroll, setEditorPercentScroll, setViewerPercentScroll, editor_scroll };
|
|
|
|
}
|
2020-07-02 23:45:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
export function useRootSize(dependencies:any) {
|
|
|
|
const { rootRef } = dependencies;
|
|
|
|
|
|
|
|
const [rootSize, setRootSize] = useState({ width: 0, height: 0 });
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!rootRef.current) return;
|
|
|
|
|
|
|
|
const { width, height } = rootRef.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (rootSize.width !== width || rootSize.height !== height) {
|
|
|
|
setRootSize({ width: width, height: height });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return rootSize;
|
|
|
|
}
|