From d23b965d2a219a7aea38243d9da719462ac3a112 Mon Sep 17 00:00:00 2001 From: Bijay Shrestha Date: Thu, 16 Apr 2020 18:08:08 +0545 Subject: [PATCH] refactored flatArray function added setCursor for test purpose recreated content editable and selection icons added distant thought animation --- src/components/FlatTreeRenderer.js | 29 ++++++++++++++++--------- src/util/flatRender.js | 35 ++++++++++++++++-------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/components/FlatTreeRenderer.js b/src/components/FlatTreeRenderer.js index 384363f7137..76c85cdec1d 100644 --- a/src/components/FlatTreeRenderer.js +++ b/src/components/FlatTreeRenderer.js @@ -2,6 +2,8 @@ import React, { useRef } from 'react' import { connect } from 'react-redux' import { useTransition, useSpring, animated } from 'react-spring' import useMeasure from '../hooks/useMeasure.js' +import { store } from '../store.js' +import ContentEditable from 'react-contenteditable' // util import { treeToFlatArray } from '../util' @@ -10,7 +12,7 @@ import { treeToFlatArray } from '../util' // constant // import { RANKED_ROOT } from '../constants' -const TreeNode = ({ style, value, item, springKey, phase }) => { +const TreeNode = ({ style, value, item, springKey, phase, rotation, selectionOpacity }) => { const [bind, { height: viewHeight }] = useMeasure() @@ -21,9 +23,18 @@ const TreeNode = ({ style, value, item, springKey, phase }) => { }) return ( - - - {value} + { + store.dispatch({ type: 'setCursor', thoughtsRanked: item.path, cursorHistoryClear: true, editing: true }) + }}> + +
+ `rgba(255,255,255,${o})`) }}> + `rotate(${r}deg)`), fontSize: '0.94rem' }}> + { item.hasChildren ? '▸' : '•' } + + + +
) @@ -33,19 +44,17 @@ const TreeAnimation = ({ flatArray, visibleDepth }) => { const transitions = useTransition(flatArray, node => node.key, { unique: true, - from: item => ({ opacity: 0 }), - enter: item => ({ opacity: 1, display: 'block', x: (item.path.length - visibleDepth) * 1.2 }), + from: item => ({ opacity: 0, }), + enter: item => ({ opacity: item.isDistantThought ? 0.45 : 1, display: 'block', x: (item.path.length - visibleDepth) * 1.2, rotation: item.expanded ? 90 : 0, selectionOpacity: item.isSelected ? 0.3 : 0 }), leave: item => ({ opacity: 0 }), - update: item => ({ x: (item.path.length - visibleDepth) * 1.2 }), - onDestroyed: () => { - } + update: item => ({ x: (item.path.length - visibleDepth) * 1.2, opacity: item.isDistantThought ? 0.45 : 1, rotation: item.expanded ? 90 : 0, selectionOpacity: item.isSelected ? 0.3 : 0 }), }) return ( { transitions.map(({ item, key, props, phase }) => { - return `translateX(${x}rem)`) }} value={item.value} phase={phase}/> + return `translateX(${x}rem)`) }} value={item.value} phase={phase} rotation={props.rotation} selectionOpacity={props.selectionOpacity}/> }) } diff --git a/src/util/flatRender.js b/src/util/flatRender.js index 6413733948b..b45a0fd54b5 100644 --- a/src/util/flatRender.js +++ b/src/util/flatRender.js @@ -40,7 +40,8 @@ const getFlatArray = (startingPath, cursor, isLeaf, isParentCursorAncestor = tru else { // stop deeper recursion at certain depth where any descendant of cursor has more than one subthought - const stop = (addDistantAncestorAndStop || (isCursorChildren && subThoughts.length > 1)) + // stop further deeper recursion if max depth is reached + const stop = (addDistantAncestorAndStop || (isCursorChildren && subThoughts.length > 1)) || childPath.length - cursor.length === MAX_DEPTH_FROM_CURSOR const distanceFromCursor = cursor.length - childPath.length @@ -52,22 +53,24 @@ const getFlatArray = (startingPath, cursor, isLeaf, isParentCursorAncestor = tru ) && !isCursor + const deeperFlatArray = stop ? [] : getFlatArray(childPath, cursor, isLeaf, isCursorAncestor, isCursorChildren || isCursor) + // limit depth from the cursor - return (childPath.length - cursor.length >= MAX_DEPTH_FROM_CURSOR) - ? acc - : acc.concat([ - { ...child, - path: childPath, - isSelected: isCursor, - key: `${parentNode.value}-${parentNode.rank}-${child.value}-${child.rank}-${childPathLength}`, - isDistantThought, - isCursorChildren, - noAnimationExit: (checkIfContextOfCursor && isLeaf) || isCursorChildren, - isCursorAncestor, - }, - // isCursorChildren is used to prevent cursor descendants to call isDescendant everytime - ...stop ? [] : getFlatArray(childPath, cursor, isLeaf, isCursorAncestor, isCursorChildren || isCursor) - ]) + return acc.concat([ + { ...child, + path: childPath, + isSelected: isCursor, + key: `${parentNode.value}-${parentNode.rank}-${child.value}-${child.rank}-${childPathLength}`, + isDistantThought, + isCursorChildren, + noAnimationExit: (checkIfContextOfCursor && isLeaf) || isCursorChildren, + isCursorAncestor, + hasChildren: getThoughtsRanked(childPath).length > 0, + expanded: deeperFlatArray.length > 0, + }, + // isCursorChildren is used to prevent cursor descendants to call isDescendant everytime + ...deeperFlatArray + ]) } }, []) }