sourceafFandocViewer::ViewFandoc.fan

using fwt::Dialog
using fwt::Event
using flux::CommandId
using flux::FluxCommand
using flux::FileResource
using fluxText::TextEditor
using afFandocViewer::Browser

**
** A [flux]`fandoc://flux` tool command that flips the view between browsing and editing - useful 
** for editing Fandocs. If dirty, the view's content is saved prior to switching. 
** 
** To use, copy this src file to '%FAN_HOME%/etc/flux/tools/'.
** 
** To bind this command to F12 modify '%FAN_HOME%/etc/flux/tools/keys.fog' and change the line:
** 
**   "tools.DebugDump": "F12",
** 
** to
** 
**   "tools.ViewFandoc": "F12",
** 
class ViewFandoc : FluxCommand {
    
    new make(Str id) : super(id) {}

    override Void invoked(Event? event) {
        if (frame.view.dirty)
            frame.command(CommandId.save).invoke(event)
        
        if (fandocUri != null)
            switchViews(fandocUri)
    }

    private Void switchViews(Uri fandocUri) {
        view := (fandocUri.query["view"] == TextEditor#.qname) ? Browser#.qname : TextEditor#.qname
        fandocUri = fandocUri.plusQuery(["view" : view])
        frame.load(fandocUri)
    }
    
    private Uri? fandocUri() {
        // check that current view tab is file resource
        r := frame.view.resource
        if (r isnot FileResource) {
            Dialog.openErr(frame, "Current view is not file resource: $r.typeof")
            return null
        }
        return r.uri
    }

}