I thought I had found the last remaining oddity with the "Save" button in the Raven/RavenPlus editor 3 months ago. Unfortunately, I found another one recently.
I am a bit paranoid while composing my blog post. So, not wanting to lose my work, I have this habit of regularly saving my post after every couple of minutes while editing. I also have a need to dive into the "XHTML" tab from time to time; to insert an Amazon affiliate ad, for example. So this means me regularly switching between the "Design" and "XHTML" tabs while composing my blog post.
Did I Just Modify My Content?
I got confused when I first encountered this flaw in Raven/RavenPlus: The "Save" button in the editor became enabled when I switched from the "Design" tab to the "XHTML" tab. It won't happen the first time round when I made the flip. But it definitely will from the second time onwards, even when there is NO content in either the "Design" or "XHTML" tab. This can't be right.
The Fix
I have to make 2 tries before I eventually got it right. In my first attempt, I modified 2 functions, added 2 variables and then inserted something like 15 lines of code. Testing it out over a couple of days, I later realised that it does not work. I blame this squarely on the complexity of event-driven programming. It is hard to trace where and when an event is being fired. And the "Change" event in the "XHTML" editor got fired at least 3 times when I flip over to the "XHTML" tab. I could only speculate on where this event was fired. My best guess is when the content in the "XHTML" editor was cleared before loading the updated post that was made in the "Design" tab.
By tracing and examining it closer (thanks to all those print statements that I have inserted while debugging), I finally came to realised that to fix this problem, all that was needed was just one line of code - to set the "contentLoaded" variable to "False" (in the function "setValue") after switching to the "XHTML" tab and before (re)loading the XHTML/HTML scripts in this tab. This will prevent the "fireContentModifiedEvent()" in the "onChange" event handler from being fired (since there was no modification of any kind) and thus preventing the "Save" button from being enabled by mistake.
The Source Code
Only one files needs to be modified
"zoundry\appframework\ui\widgets\controls\advanced\stc\stceditor.py".
And there is only one class in this file: ZStyledXhtmlEditControl.
To fix this problem, the code in RED below are the new lines that needs to be inserted into the "setValue" function.
====== BEGIN ======
def setValue(self, value): # Chuah TC 20th April 2015 # # We need to set the variable "contentLoaded" to False because prior to calling # "_internalSetValue", onChange will get fired at least 3 times. This will then cause # "self._fireContentModifiedEvent()" in onChange to get fired unintentionally, which # will cause the "Save" button to be enabled, even though nothing has changed, # when we flip between the "Design" and "XHTML" tab continuously. # self.contentLoaded = False # # Chuah TC 20th April 2015 self.clearValidation() # we need to reset the undo buffer otherwise the last undo will remove the text. self._getStcControl().EmptyUndoBuffer() self._internalSetValue(value) # end setValue
====== END ======
0 comments:
Post a Comment