From 56eeb1fc6b03c4e8ed9405e301c7edc7be5fd5d9 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Fri, 1 Oct 2021 19:11:08 +0200 Subject: feat: basic preview feature --- components/Sidebar/index.tsx | 273 +++++++++++++++++++++++++++++++++++++++++++ components/contextmenu.tsx | 22 ++-- pages/index.tsx | 137 +++++++++++----------- pages/uniorg.tsx | 3 +- util/uniorg.tsx | 8 +- util/webSocketFunctions.ts | 25 ++++ 6 files changed, 384 insertions(+), 84 deletions(-) create mode 100644 components/Sidebar/index.tsx create mode 100644 util/webSocketFunctions.ts diff --git a/components/Sidebar/index.tsx b/components/Sidebar/index.tsx new file mode 100644 index 0000000..64436f2 --- /dev/null +++ b/components/Sidebar/index.tsx @@ -0,0 +1,273 @@ +import React, { useContext, useEffect, useState } from 'react' + +import { UniOrg } from "../../util/uniorg" +import { getOrgText } from "../../util/webSocketFunctions" + +import { + Button, + Slide, + VStack, + Flex, + Heading, + Box, + CloseButton, + Text, + Drawer, + DrawerOverlay, + DrawerHeader, + DrawerBody, + DrawerCloseButton, + DrawerContent, + DrawerFooter, + IconButton, +} from '@chakra-ui/react' +import { Scrollbars } from 'react-custom-scrollbars-2' +import { ChevronLeftIcon, ChevronRightIcon } from '@chakra-ui/icons' + +import { GraphData, NodeObject, LinkObject } from 'force-graph' +import { OrgRoamNode } from '../../api' +import {ThemeContext} from '../../util/themecontext' + +export interface SidebarProps { + isOpen: boolean + onClose: any + openNode: string + nodeById: any + previewNode: NodeObject, + orgText: string +} + +const Sidebar = (props: SidebarProps) => { + + const { isOpen, onClose, openNode, nodeById, previewNode, orgText } = props + + const {highlightColor}= useContext(ThemeContext) + + useEffect(() => { + (async () => { + if (!openNode) { + return + } + if (openNode === 'nil') { + return + } + })() + + const previewRoamNode = previewNode as OrgRoamNode + }, [previewNode]) + + //maybe want to close it when clicking outside, but not sure + //const outsideClickRef = useRef(); + return ( + + + } + colorScheme="white" + aria-label="Close file-viewer" + height={100} + variant="ghost" + marginRight={-2} + bg="alt.100" + onClick={onClose} + marginTop={20} + /> + + + {previewNode.title} + + ( + + )} + > + + + + + + + + + + ) +} + +export default Sidebar + + + + +/* + {previewNode.title} + */ diff --git a/components/contextmenu.tsx b/components/contextmenu.tsx index 39b1895..ff3dd81 100644 --- a/components/contextmenu.tsx +++ b/components/contextmenu.tsx @@ -43,6 +43,7 @@ import { } from '@chakra-ui/icons' import { OrgRoamGraphReponse, OrgRoamLink, OrgRoamNode } from '../api' +import { getOrgText, deleteNodeInEmacs, openNodeInEmacs, createNodeInEmacs } from "../util/webSocketFunctions" export default interface ContextMenuProps { background: Boolean @@ -50,12 +51,10 @@ export default interface ContextMenuProps { nodeType?: string coordinates: number[] handleLocal: (node: OrgRoamNode, add: string) => void - openNodeInEmacs: (node: OrgRoamNode) => void menuClose: () => void scope: { nodeIds: string[] } - deleteNodeInEmacs: (node: OrgRoamNode) => void - createNodeInEmacs: (node: OrgRoamNode) => void - getOrgText: any + webSocket: any + setPreviewNode: any, } export const ContextMenu = (props: ContextMenuProps) => { @@ -67,10 +66,8 @@ export const ContextMenu = (props: ContextMenuProps) => { handleLocal, menuClose, scope, - openNodeInEmacs, - deleteNodeInEmacs, - createNodeInEmacs, - getOrgText, + webSocket, + setPreviewNode, } = props const { isOpen, onOpen, onClose } = useDisclosure() const copyRef = useRef() @@ -104,11 +101,11 @@ export const ContextMenu = (props: ContextMenuProps) => { )} {!node?.properties.FILELESS ? ( - } onClick={() => openNodeInEmacs(node as OrgRoamNode)}> + } onClick={() => openNodeInEmacs(node as OrgRoamNode, webSocket)}> Open in Emacs ) : ( - } onClick={() => createNodeInEmacs(node)}> + } onClick={() => createNodeInEmacs(node, webSocket)}> Create node )} @@ -159,7 +156,8 @@ export const ContextMenu = (props: ContextMenuProps) => { Permenantly delete note )} - getOrgText(node)}>Preview + {getOrgText(node!, webSocket) + setPreviewNode(node)}}>Preview @@ -198,7 +196,7 @@ export const ContextMenu = (props: ContextMenuProps) => { ml={3} onClick={() => { console.log('aaaaa') - deleteNodeInEmacs(node!) + deleteNodeInEmacs(node!, webSocket) onClose() menuClose() }} diff --git a/pages/index.tsx b/pages/index.tsx index 5b3c35d..ad01741 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -17,11 +17,12 @@ import type { } from 'react-force-graph' import { OrgRoamGraphReponse, OrgRoamLink, OrgRoamNode } from '../api' import { GraphData, NodeObject, LinkObject } from 'force-graph' +import Sidebar from '../components/Sidebar' import { useWindowSize } from '@react-hook/window-size' import { useAnimation } from '@lilib/hooks' -import { Box, useDisclosure, useTheme } from '@chakra-ui/react' +import { Box, Flex, IconButton, useDisclosure, useTheme } from '@chakra-ui/react' import { initialPhysics, @@ -41,6 +42,8 @@ import SpriteText from 'three-spritetext' import wrap from 'word-wrap' import ReconnectingWebSocket from 'reconnecting-websocket' +import { getOrgText, deleteNodeInEmacs, openNodeInEmacs, createNodeInEmacs } from "../util/webSocketFunctions" +import { ChevronLeftIcon } from '@chakra-ui/icons' // react-force-graph fails on import when server-rendered // https://github.com/vasturiano/react-force-graph/issues/155 const ForceGraph2D = ( @@ -84,11 +87,9 @@ export function GraphPage() { const [emacsNodeId, setEmacsNodeId] = useState(null) const [behavior, setBehavior] = usePersistantState('behavior', initialBehavior) const [mouse, setMouse] = usePersistantState('mouse', initialMouse) - const [orgText, setOrgText] = useState('') - - useEffect(() => { - console.log(orgText) - }, [orgText]) + const [orgText, setOrgText] = useState("") + const [previewNode, setPreviewNode] = useState({}) + const { isOpen, onOpen, onClose } = useDisclosure() const nodeByIdRef = useRef({}) const linksByNodeIdRef = useRef({}) @@ -364,7 +365,7 @@ export function GraphPage() { case 'graphdata': return updateGraphData(message.data) case 'orgText': - return setOrgText(message.data) + return setOrgText(message.data) case 'theme': return setEmacsTheme(message.data) case 'command': @@ -414,25 +415,50 @@ export function GraphPage() { return ( - + + /> + + + {!isOpen && ( + } + height={100} + aria-label="Open org-viewer" + position="relative" + zIndex="overlay" + colorScheme="purple" + onClick={onOpen} + variant="ghost" + marginTop={10} + /> + )} + + @@ -473,9 +500,10 @@ export interface GraphProps { setScope: any webSocket: any tagColors: { [tag: string]: string } + setPreviewNode: any } -export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { +export const Graph = forwardRef(function(props: GraphProps, graphRef: any) { const { physics, graphData, @@ -491,6 +519,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { setScope, webSocket, tagColors, + setPreviewNode, } = props // react-force-graph does not track window size @@ -522,28 +551,6 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { return } - const sendMessageToEmacs = (command: string, data: {}) => { - webSocket.send(JSON.stringify({ command: command, data: data })) - } - - const getOrgText = (node: OrgRoamNode) => { - sendMessageToEmacs('getText', { id: node.id }) - } - - const openNodeInEmacs = (node: OrgRoamNode) => { - sendMessageToEmacs('open', { id: node.id }) - } - - const deleteNodeInEmacs = (node: OrgRoamNode) => { - if (node.level !== 0) { - return - } - sendMessageToEmacs('delete', { id: node.id, file: node.file }) - } - - const createNodeInEmacs = (node: OrgRoamNode) => { - sendMessageToEmacs('create', { id: node.id, title: node.title, ref: node.properties.ROAM_REFS }) - } const contextMenu = useDisclosure() @@ -560,7 +567,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { break } case mouse.follow: { - openNodeInEmacs(node) + openNodeInEmacs(node, webSocket) break } case mouse.context: { @@ -772,7 +779,7 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { ]) useEffect(() => { - ;(async () => { + ; (async () => { const fg = graphRef.current const d3 = await d3promise if (physics.gravityOn && !(scope.nodeIds.length && !physics.gravityLocal)) { @@ -934,15 +941,15 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { return needsHighlighting ? getThemeColor(visuals.citeNodeColor) : highlightColors[visuals.citeNodeColor][visuals.backgroundColor]( - visuals.highlightFade * opacity, - ) + visuals.highlightFade * opacity, + ) } if (visuals.refNodeColor && node.properties.ROAM_REFS) { return needsHighlighting ? getThemeColor(visuals.refNodeColor) : highlightColors[visuals.refNodeColor][visuals.backgroundColor]( - visuals.highlightFade * opacity, - ) + visuals.highlightFade * opacity, + ) } if (!needsHighlighting) { return highlightColors[getNodeColorById(node.id as string)][visuals.backgroundColor]( @@ -1090,20 +1097,20 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { if (visuals.refLinkColor && roamLink.type === 'ref') { return needsHighlighting && (visuals.refLinkHighlightColor || visuals.linkHighlight) ? highlightColors[visuals.refLinkColor][ - visuals.refLinkHighlightColor || visuals.linkHighlight - ](opacity) + visuals.refLinkHighlightColor || visuals.linkHighlight + ](opacity) : highlightColors[visuals.refLinkColor][visuals.backgroundColor]( - visuals.highlightFade * opacity, - ) + visuals.highlightFade * opacity, + ) } if (visuals.citeLinkColor && roamLink.type?.includes('cite')) { return needsHighlighting && (visuals.citeLinkHighlightColor || visuals.linkHighlight) ? highlightColors[visuals.citeLinkColor][ - visuals.citeLinkHighlightColor || visuals.linkHighlight - ](opacity) + visuals.citeLinkHighlightColor || visuals.linkHighlight + ](opacity) : highlightColors[visuals.citeLinkColor][visuals.backgroundColor]( - visuals.highlightFade * opacity, - ) + visuals.highlightFade * opacity, + ) } return getLinkColor(sourceId as string, targetId as string, needsHighlighting) @@ -1192,10 +1199,8 @@ export const Graph = forwardRef(function (props: GraphProps, graphRef: any) { coordinates={contextPos} handleLocal={handleLocal} menuClose={contextMenu.onClose.bind(contextMenu)} - openNodeInEmacs={openNodeInEmacs} - deleteNodeInEmacs={deleteNodeInEmacs} - createNodeInEmacs={createNodeInEmacs} - getOrgText={getOrgText} + webSocket={webSocket} + setPreviewNode={setPreviewNode} /> )} {threeDim ? ( diff --git a/pages/uniorg.tsx b/pages/uniorg.tsx index 65786c9..0c77cdb 100644 --- a/pages/uniorg.tsx +++ b/pages/uniorg.tsx @@ -11,7 +11,7 @@ export interface uniorgProps { orgText: string } -const UniOrg = (props: uniorgProps) => { +export const UniOrg = (props: uniorgProps) => { const { orgText } = props const processor = unified() .use(uniorgParse) @@ -22,4 +22,3 @@ const UniOrg = (props: uniorgProps) => { return
processor.processSync(orgText)
} -export default UniOrg diff --git a/util/uniorg.tsx b/util/uniorg.tsx index e4d8dfd..ba86a89 100644 --- a/util/uniorg.tsx +++ b/util/uniorg.tsx @@ -7,11 +7,11 @@ import katex from 'rehype-katex' import rehype2react from 'rehype-react' import React from 'react' -export interface uniorgProps { +export interface UniOrgProps { orgText: string } -const uniorg = (props: uniorgProps) => { +export const UniOrg = (props: UniOrgProps) => { const { orgText } = props const processor = unified() .use(uniorgParse) @@ -19,7 +19,7 @@ const uniorg = (props: uniorgProps) => { .use(katex) .use(rehype2react, { createElement: React.createElement }) - return processor.processSync(orgText) + console.log(processor.processSync(orgText)) + return
{processor.processSync(orgText).result}
} -export default uniorg diff --git a/util/webSocketFunctions.ts b/util/webSocketFunctions.ts new file mode 100644 index 0000000..8e2bd7e --- /dev/null +++ b/util/webSocketFunctions.ts @@ -0,0 +1,25 @@ +import { OrgRoamNode } from '../api' +import ReconnectingWebSocket from 'reconnecting-websocket' + +export function sendMessageToEmacs(command: string, data: {}, webSocket: ReconnectingWebSocket) { + webSocket.send(JSON.stringify({ command: command, data: data })) +} + +export function getOrgText(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('getText', { id: node.id }, webSocket) +} + +export function openNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('open', { id: node.id }, webSocket) +} + +export function deleteNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + if (node.level !== 0) { + return + } + sendMessageToEmacs('delete', { id: node.id, file: node.file }, webSocket) +} + +export function createNodeInEmacs(node: OrgRoamNode, webSocket: ReconnectingWebSocket) { + sendMessageToEmacs('create', { id: node.id, title: node.title, ref: node.properties.ROAM_REFS }, webSocket) +} -- cgit v1.2.3