-
Notifications
You must be signed in to change notification settings - Fork 393
highlight the currently open notebook in NotebookList #1420
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -238,14 +238,14 @@ export class NotebookList extends Disposable { | |
helpIconButton([], "https://polynote.org/latest/docs/notebooks-list/"), | ||
]), | ||
span(['right-buttons'], [ | ||
iconButton(['create-notebook'], 'Create new notebook', 'plus-circle', 'New').click(evt => { | ||
evt.stopPropagation(); | ||
dispatcher.createNotebook() | ||
}), | ||
iconButton(['highlight-notebook'], 'Highlight opened notebook', 'plus-circle', 'Highlight').click(evt => { | ||
iconButton(['highlight-notebook'], 'Find opened notebook', 'crosshairs', 'Highlight').click(evt => { | ||
evt.stopPropagation(); | ||
this.tree.highlightPath(this.currentNotebook, 0); | ||
}), | ||
iconButton(['create-notebook'], 'Create new notebook', 'plus-circle', 'New').click(evt => { | ||
evt.stopPropagation(); | ||
dispatcher.createNotebook() | ||
}) | ||
]) | ||
]); | ||
|
||
|
@@ -254,7 +254,7 @@ export class NotebookList extends Disposable { | |
value: "", | ||
lastSaved: 0, | ||
children: {}, | ||
isOrHasCurrentNotebook: false | ||
isOrHasCurrentNotebook: true | ||
}); | ||
this.tree = new BranchEl(dispatcher, treeState); | ||
|
||
|
@@ -408,6 +408,7 @@ export class BranchHandler extends ObjectStateHandler<Branch> { | |
if (!currentState || !isBranch(currentState) || !currentState.children[piece]) { | ||
currentUpdate.fullPath = currentPath; | ||
currentUpdate.value = piece; | ||
currentUpdate.isOrHasCurrentNotebook = currentNotebook === path; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just stepping through in devtools, I'm seeing some instances where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under what situation do you see that addPath gets called after updateCurrentNotebook? I wasn't able to reproduce. The last step of the process to create a new notebook is to select it so I think things should happen in the order I mentioned earlier: https://github.com/polynote/polynote/blob/megan/highlight-current-notebook/polynote-frontend/polynote/messaging/dispatcher.ts#L379 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was able to kind of repro when renaming a notebook, I think. But it's hard to tell because we have a bug with that 😅 #1422 If that's the only way to repro I think we should fix that problem: in general, selecting the current notebook should be the very last thing that happens; and notebook shouldn't be selected before it "exists" if that makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like when reopening the current notebooks (e.g. create a notebook and refresh the page with the notebook still open), the frontend is loading the notebooks and then also getting a message from the server (of type |
||
currentState = undefined; | ||
} else { | ||
currentState = currentState.children[piece]; | ||
|
@@ -449,16 +450,18 @@ export class BranchHandler extends ObjectStateHandler<Branch> { | |
this.update(state => go(path, state)) | ||
} | ||
|
||
// traverses the tree to update isOrHasCurrentNotebook for each Node so that elements | ||
// can display the proper styling to highlight themselves | ||
updateCurrentNotebook(currentNotebookPath: string) { | ||
function go(fullPath: string, sliceIndex: number, parent: Branch): UpdatePartial<Branch> { | ||
return { | ||
children: Object.keys(parent.children).reduce((acc, key) => { | ||
const branchOrLeaf = parent.children[key]; | ||
const currPath = getUpToNthOccurrence(fullPath, sliceIndex, "/"); | ||
const currPath = getUpToNthOccurrence(fullPath, sliceIndex + 1, "/"); | ||
if ("children" in branchOrLeaf) { | ||
acc[key] = { | ||
...go(fullPath, sliceIndex + 1, branchOrLeaf), | ||
isOrHasCurrentNotebook: setValue(branchOrLeaf["fullPath"] === currPath) | ||
isOrHasCurrentNotebook: setValue(branchOrLeaf["fullPath"] === currPath) // is this branch part of the path to the current notebook? | ||
} // 'tis a branch | ||
} else { | ||
acc[key] = { | ||
|
@@ -655,17 +658,17 @@ export class BranchEl extends Disposable { | |
* Recursively expand folders until the leaf node at the desired path is reached | ||
*/ | ||
highlightPath(path: string, index: number) { | ||
const currPath = getUpToNthOccurrence(path, index, "/"); | ||
const currPath = getUpToNthOccurrence(path, index + 1, "/"); | ||
const child = this.children.find(c => c.path === currPath); | ||
if (child === null) { | ||
return | ||
} | ||
this.expanded = true; // expand the current folder | ||
this.expanded = index != 0; // expand the current folder, but not at the root level | ||
if (child instanceof BranchEl) { | ||
child.highlightPath(path, index + 1); | ||
} | ||
else if (child instanceof LeafEl) { // scroll to this notebook | ||
child.focus(); | ||
else if (child instanceof LeafEl) { | ||
child.focus(); // scroll to this notebook | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,26 +354,23 @@ export function positionIn(str: string, line: number, column: number): number { | |
} | ||
|
||
/** | ||
* Helper method for grabbing everything in a string up to the (n + 1)th occurrence of the pattern. | ||
* Zero-indexed - n = 0 will return everything up to the first occurrence, n = 1 will return everything up to | ||
* the second, including the first occurrence in the return string | ||
* Helper method for grabbing everything in a string up to the nth occurrence of the pattern. | ||
* Will only return a nonempty string for n >= 1. | ||
* E.g. if called with "hello/world" where n = 1 and pattern = "/", will return "hello" | ||
*/ | ||
export function getUpToNthOccurrence(str: string, n: number, pattern: string): string { | ||
|
||
let count = 0; | ||
const pieces = str.split(pattern); | ||
if (n > pieces.length) { | ||
return str; | ||
} | ||
let i = 0; | ||
while (i < str.length) { | ||
if (str[i] === pattern) { | ||
count++; | ||
i += pattern.length; | ||
} | ||
else { | ||
i++; | ||
} | ||
if (n < count) { | ||
return str.slice(0, i - 1); | ||
} | ||
let res = ""; | ||
while (i < pieces.length && i < n) { | ||
res += pieces[i]; | ||
res += i < n - 1 ? pattern : ""; | ||
i++; | ||
} | ||
return str; | ||
return res; | ||
} | ||
|
||
//**************** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary?