Implementing UNDO

David Epstein dfepstein at comcast.net
Fri May 21 14:32:50 EDT 2021


LC’s built in undo does not appear to undo anything done by something I have scripted.
My approach is to use a global array variable “u” to store information needed to undo the most recent action.  On each action, an “undoInit” puts empty into u, after (if necessary) cleaning up anything u’s values indicate is left over from the prior action (e.g., actually deleting an object that had been only “pseudo-deleted” (hidden) so as to make the user’s choice of deletion undoable).  Then the appropriate new values are loaded into u.  The main elements I use are

# u["cardID"] = short id of the card where the undoable action was taken
# u["stackName"] = short name of the stack where the undoable action was taken
# u["changed"], list of objects being changed, each line has:  propName,objectID
# u[propName,objectID] holds prior value of that property of that object (one key/value for each line of u["changed"]
# u["removed"], list of objectID,cardID,stackName for objects being pseudo-removed
# u["added"], list of objectID,cardID,stackName for objects being added
# u["cardsRemoved"], list of cards being pseudo-removed, each line has:  cardID,cardNumber
# u["cardsAdded"], list of cards being added, each line has:  cardID,cardNumber

The “Undo” command makes sure we’re on card id u[“cardID”] of stack u[“stackName”}, then checks for non-empty values of u[“changed”],u[“removed”],u[“added”],u{“cardsRemoved”], and u[“cardsAdded”], and uses values stored in u to restore the prior state of things, while reloading u with values reporting the state of things now being reversed.

For text, I load u[“changed”] with “htmlText”,<myFieldID>, and load u[“htmlText”,<myFieldID>] with the htmlText of that field before the latest editing.

David Epstein



More information about the use-livecode mailing list