Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LayoutTree: use different hideCaret name in table #2775

Merged

Conversation

ethan-james
Copy link
Collaborator

Fixes #2707

The calculated indent value for a thought increases when the thought isTableCol2, but its integer indentDepth stays the same. Since moving the cursor into a table hides all thoughts beneath the table, it is straightforward to add 1 to column 2's indentDepth in order to correctly hide the cursor during the transform animation.

Copy link
Contributor

@raineorshine raineorshine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works!

Just a recommendation on the implementation.

Comment on lines 911 to 916
const cursor = useSelector(state => state.cursor)
const spaceAboveLast = useRef(spaceAboveExtended)
const treeThought = treeThoughts.find(thought => equalPath(thought.path, cursor))
// When the cursor is in a table, all thoughts beneath the table are hidden,
// so there is no concern about animation name conflicts with subsequent (deeper) thoughts.
const tableDepth = treeThought?.isTableCol2 ? 1 : 0
Copy link
Contributor

@raineorshine raineorshine Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would avoid treeThoughts here and just determine tableDepth directly from the Redux state in a useSelector. You can use attributeEquals(state, head(rootedParentOf(state, state.cursor)), '=view', 'Table') to determine if the cursor in table col2. This makes all the logic a single selector and avoids the O(n) traversal of treeThoughts.

Separately, there is a performance issue to be aware of. const cursor = useSelector(state => state.cursor) causes the render function get called every time the cursor ref changes, which is not necessary when we are only using it to determine tableDepth. In general, any logic that reduces the range of the value should go in the useSelector. More simply: Aim for "thick" Redux selectors.

(It won't do any harm in this case, because LayoutTree always re-renders when the cursor changes, but it's better to avoid the habit. The LayoutTree is also the heaviest render function in the entire app, so it is highly sensitive to performance issues.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks! I'll try to get my head around the rootedParentOf bit.

Copy link
Contributor

@raineorshine raineorshine Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's basically just path.slice(0, -1) if that helps :)

It handles a few edge cases in the root.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set it up, and it actually does the opposite of what I wanted, returning 1 for column 1 & 0 for column 2, resulting in "C" getting hideCaret2 and "D" getting hideCaret1. But it still works because both values have a greater indent level than "B", which has hideCaret0.

The linearizeTree function, where attributeEquals(state, head(rootedParentOf(state, state.cursor)), '=view', 'Table') appears, is getting childPath passed into it to use as its basePath. childPath is calculated in a fairly complex way, but long story short, if I do this in the selector:

rootedParentOf(state, rootedParentOf(state, state.cursor))

then it appears to work the way I would expect, where column 2 return 1 and column 1 returns 0. Do you think it would be better for me to change it?

Copy link
Contributor

@raineorshine raineorshine Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, that makes sense. A path is in col2 when its grandparent has =view/Table.

You should be able to come up with a selector that gives you 1 under the same circumstances as your previous logic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is using rootedParentOf twice a good solution, or is there a cleverer utility?

Copy link
Contributor

@raineorshine raineorshine Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's relatively safe to use parentOf in this case, since root children cannot be in col2 (as they are directly in the root) and root grandchildren are unlikely to be in col2 (as the root itself would have to be a table). However, double rootedParentOf is also fine.

You can always make a grandparentOf helper, or separate out the parent-of-the-parent in a grandparent variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context. I opted to add rootedGrandparentOf to clean up the selector a bit.

Comment on lines 26 to 28
/** Calls rootedParentOf twice to get the rooted grandparent of a thought. */
export const rootedGrandparentOf = <T extends Context | Path>(state: State, thoughts: T): T =>
rootedParentOf(state, rootedParentOf(state, thoughts))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a convention on this project, we generally only allow a single, default export that matches the file name. This makes it easier to know where functions live in the project, and is facilitated by the quick file picker that most editors provide. Reducers/Action Creators are an exception, and a few others, but we try to avoid this when possible.

Could move this into its own file in the src/selectors directory?

@@ -909,7 +909,11 @@ const LayoutTree = () => {
])

const spaceAboveLast = useRef(spaceAboveExtended)

// When the cursor is in a table, all thoughts beneath the table are hidden,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be worth mentioning in this comment the reason that this is needed. i.e. Why is col2 a special case for the hideCaret depth?

@raineorshine raineorshine merged commit b6e7ca2 into cybersemics:main Jan 18, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caret drifts moving between table columns
2 participants