Notify the index when an item's class changes (#2330)
This commit is contained in:
@@ -131,6 +131,12 @@ class Index:
|
|||||||
self._novelExtra = extra
|
self._novelExtra = extra
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def setItemClass(self, tHandle: str, itemClass: nwItemClass) -> None:
|
||||||
|
"""Update the class for all tags of a handle."""
|
||||||
|
logger.info("Updating class for '%s'", tHandle)
|
||||||
|
self._tagsIndex.updateClass(tHandle, itemClass.name)
|
||||||
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
# Public Methods
|
# Public Methods
|
||||||
##
|
##
|
||||||
@@ -854,6 +860,15 @@ class TagsIndex:
|
|||||||
x.get("name", "") for x in self._tags.values() if x.get("class", "") == className
|
x.get("name", "") for x in self._tags.values() if x.get("class", "") == className
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def updateClass(self, tHandle: str, className: str) -> None:
|
||||||
|
"""Update the class name of an item. This must be called when a
|
||||||
|
document moves to another class.
|
||||||
|
"""
|
||||||
|
for entry in self._tags.values():
|
||||||
|
if entry.get("handle") == tHandle:
|
||||||
|
entry["class"] = className
|
||||||
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
# Pack/Unpack
|
# Pack/Unpack
|
||||||
##
|
##
|
||||||
|
|||||||
@@ -435,7 +435,11 @@ class NWItem:
|
|||||||
"""
|
"""
|
||||||
if self._parent is not None:
|
if self._parent is not None:
|
||||||
# Only update for child items
|
# Only update for child items
|
||||||
self.setClass(itemClass)
|
if itemClass != self._class:
|
||||||
|
self.setClass(itemClass)
|
||||||
|
if self._type == nwItemType.FILE:
|
||||||
|
# Notify the index of the class change
|
||||||
|
self._project.index.setItemClass(self._handle, itemClass)
|
||||||
|
|
||||||
if self._layout == nwItemLayout.NO_LAYOUT:
|
if self._layout == nwItemLayout.NO_LAYOUT:
|
||||||
# If no layout is set, pick one
|
# If no layout is set, pick one
|
||||||
|
|||||||
@@ -1049,6 +1049,12 @@ def testCoreIndex_TagsIndex():
|
|||||||
assert tagsIndex.tagClass("Tag3") == nwItemClass.PLOT.name
|
assert tagsIndex.tagClass("Tag3") == nwItemClass.PLOT.name
|
||||||
assert tagsIndex.tagClass("Tag4") is None
|
assert tagsIndex.tagClass("Tag4") is None
|
||||||
|
|
||||||
|
# Change class of item
|
||||||
|
tagsIndex.updateClass("0000000000003", nwItemClass.WORLD.name)
|
||||||
|
assert tagsIndex.tagClass("Tag3") == nwItemClass.WORLD.name
|
||||||
|
tagsIndex.updateClass("0000000000003", nwItemClass.PLOT.name)
|
||||||
|
assert tagsIndex.tagClass("Tag3") == nwItemClass.PLOT.name
|
||||||
|
|
||||||
# Pack Data
|
# Pack Data
|
||||||
assert tagsIndex.packData() == content
|
assert tagsIndex.packData() == content
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user