Source

wmd / apiExample.html

Full commit
<!DOCTYPE html>
<html>
  <head>
    <title>WMD API example</title>
  </head>
  <body>
    <p>You can disable WMD's auto-start behavior and take charge of creating editors yourself.</p>
    <p>Do "view source" to see the code for this example.</p>
    
    <p>
      <a href="javascript:createInstance()">Create</a> -
      <a href="javascript:destroyInstance()">Destroy</a>
    </p>
    
    <form id="myForm"></form>
    
    <script type="text/javascript">
        /***** Tell WMD not to start up automatically *****/
        // Set this *before* you load wmd.js
        // Otherwise you'll end up with two editors on top of each other!
        wmd_options = { autostart: false };
    
        var instances = [];
        
        function createInstance() {
        
            /***** Make sure WMD has finished loading *****/
            if (!Attacklab || !Attacklab.wmd) {
                alert("WMD hasn't finished loading!");
                return;
            }
        
            // build the dom elements
            var form = document.getElementById("myForm");
    
            var textarea = document.createElement("textarea");
            textarea.style.width = "500px";
            textarea.value = "Hi.";
            form.appendChild(textarea);
    
            var previewDiv = document.createElement("div");
            form.appendChild(previewDiv);
        
            /***** build the preview manager *****/
            var panes = {input:textarea, preview:previewDiv, output:null};
            var previewManager = new Attacklab.wmd.previewManager(panes);
        
            /***** build the editor and tell it to refresh the preview after commands *****/
            var editor = new Attacklab.wmd.editor(textarea,previewManager.refresh);
        
            // save everything so we can destroy it all later
            instances.push({ta:textarea, div:previewDiv, ed:editor, pm:previewManager});
        }
        
        function destroyInstance() {
            var inst = instances.pop();
        
            if (inst) {
        
                /***** destroy the editor and preview manager *****/
                inst.pm.destroy();
                inst.ed.destroy();
            
                // remove the dom elements
                inst.ta.parentNode.removeChild(inst.ta);
                inst.div.parentNode.removeChild(inst.div);
        
            }
        }
        
        
    </script>
    
    <script type="text/javascript" src="wmd/wmd.js""></script>
  </body>
</html>