Sorry, we don't support your browser.  Install a modern browser

Editing Notebook Tab using Index from Get replaced previous Tab#398

I’m using the index from Notes.getNotebookTabs() to update the last notebook tab, and instead of updating that one, it replaces the next to last with the new contents

6 years ago

Provide the code plz

6 years ago

so i wrote this function which takes in json, but can be any arbitrary string and attempts to replace the contents of the tab called “Game Config”

function updateNotebook(json)
    for _,data in pairs(Notes.getNotebookTabs()) do
        if data.title == "Game Config" then
            Notes.editNotebookTab({
                index = data.index,
                body = json,
            })
            break
        end
    end
end
6 years ago

Works perfect for me.
Try to check if indexes are not ordered.

print( data.index, " => ", _ )

P.S. Whitespace is a thing by using tabs via copy-paste

6 years ago

data.index is 0 indexed while _ is 1 indexed, but i think that’s the be expected? the indexes are in the correct order though

6 years ago

Overall, I said that this is not a permanent bug. That is, if you create a new simple table (as I did) with a single button and use your function, then everything will be fine. Therefore, until you find the reason (steps to reproduce), the devs are unlikely to be able to fix the bug.

6 years ago

So I compared the TabStates part of json file from a fresh save to the one I had, and turns out it was missing an id 0 tab called “Rules”. after adding that in the code works now, but in the absence of that tab existing i feel like the code should still work?

6 years ago

The following code demonstrates some of the weirdness.

function onLoad(saved_data)
    do
        local tabs = Notes.getNotebookTabs()
        for i = #tabs, 1, -1 do
            Notes.removeNotebookTab(tabs[i].index)
        end
    end
    Notes.addNotebookTab({title = "Tab 1", body = "Body 1"})
    Notes.addNotebookTab({title = "Tab 2", body = "Body 2"})
    Notes.addNotebookTab({title = "Tab 3", body = "Body 3"})
    Notes.addNotebookTab({title = "Tab 4", body = "Body 4"})
    log(Notes.getNotebookTabs())
    for _, data in pairs(Notes.getNotebookTabs()) do
        if data.title == "Tab 2" then
            Notes.removeNotebookTab(data.index)
        end
    end
    log(Notes.getNotebookTabs())
    for _, data in pairs(Notes.getNotebookTabs()) do
        if data.title == "Tab 4" then
            Notes.editNotebookTab({
                index = data.index,
                body = "New body 4",
            })
        end
    end
end

The end result is 3 tabs:

  • Tab 1: Body 1
  • Tab 4: New body 4
  • Tab 4: Body 4

Looking at the code, it appears that this happens because tts keeps both a list and dictionary of tabs, and is consitent in which one it uses to index.
For example, editNotebookTab gets the current state from the list of tabs, but then updates the tab corresponding to the dictionary of tabs.

Similarly,

function onLoad(saved_data)
    for _, data in pairs(Notes.getNotebookTabs()) do
        log(data.title)
        Notes.removeNotebookTab(data.index)
    end
    for _, data in pairs(Notes.getNotebookTabs()) do
        log(data.title)
    end
end

deletes the even positioned tabs, since it delets based on index in the list of tabs, not the dictionary of tabs.

5 years ago

It looks like this behavior is preserved over saves, as the order of tabs in the save file determines the order of tabs, but the dictionary index is preserved in the save file, but doesn’t impact the order of tabs.

5 years ago

Also encountered that problem.
When inspecting the save file, this is what the notebooks section looks like:

    "0": {
      "title": "Options",
      "body": "",
      "id": 0
    },
    "1": {
      "title": "White",
      "body": "",
      "id": 1
    },
    "3": {
      "title": "Red",
      "body": "",
      "id": 3
    },
    "6": {
      "title": "Green",
      "body": "",
      "id": 6
    }

If I wanted to change the notebook “Red”, the index getNotebookTabs() returns is 2. Using that values doesn’t work though. Using the id value of the notebook (3) works and changes the content. However there’s no way to get the id of the notebook via scripts.

5 years ago
1
Changed the status to
Awaiting Release
10 days ago