Update custom view display text

I have a custom view that should change its display text based on how it was opened. I can only create the final string when onOpen() is called, so I can’t hard-code the string in getDisplayText(). Is there a way to programmatically update the display text or re-trigger getDisplayText()?

class WorkspaceView extends ItemView {

  viewTitle = "Default view title"
		
  getDisplayText() {
    return this.viewTitle
  }

  onOpen() {
    // Some custom logic
    this.viewTitle = ...
  }
}

So you just want to pass in a different display value on onOpen, or do you want to change the display text after it has already been opened?


If it’s the former, I think you could just set the view state like this inside your Plugin class:
leaf.setViewState({ type: "view-name", active: true, state: { viewTitle : "different title"} });

And then add this inside your ItemView class:

override async setState(
    state
): Promise<void> {
    this.viewTitle = state.viewTitle;
    return super.setState(state);
}

This will enforce the change of viewTitle to “different title” before the getDisplayText() function is even ran for this specific view. So you’d do the custom logic to calculate the title either in your Plugin class before setViewState is called, or you can do it in this setState function.


If it’s the later, I think you’ll have to do rerun setViewState(), I’m not sure of any way to change the display title directly without getting into editing the html. And I’m also not sure if there’s any implications of changing it on the fly like that.

I just checked, you can change the ariaLabel from within the ItemView by this.leaf.tabHeaderEl.ariaLabel = "new display Text";

However I’m not sure if this would cause any issues, and there is a chance it gets rendered back to its original value.