home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 August / PCpro_2005_08.ISO / files / firefox / ScrapBook.xpi / chrome / scrapbook.jar / content / edit.js < prev    next >
Encoding:
Text File  |  2005-06-04  |  19.8 KB  |  692 lines

  1. /**************************************************
  2. // edit.js
  3. // Implementation file for edit.xul
  4. // 
  5. // Description: 
  6. // Author: Gomita
  7. // Contributors: Bob Chao
  8. // 
  9. // Version: 
  10. // License: see LICENSE.txt
  11. **************************************************/
  12.  
  13.  
  14.  
  15. const DEFAULT_INTERVAL = 3;
  16.  
  17. var gID;
  18. var gRes;
  19. var gWindow;
  20. var gFrames;
  21. var gChanged;
  22. var gTimerID;
  23. var gShowHeader;
  24. var SBstring;
  25. var SBheader;
  26. var SBbrowser;
  27. var SBcomment;
  28. var SBcommentML;
  29. var SBcommentIL;
  30.  
  31. var IFLASHER;
  32. try {
  33.     IFLASHER = Components.classes['@mozilla.org/inspector/flasher;1'].getService(Components.interfaces.inIFlasher);
  34.     IFLASHER.thickness = 2;
  35. } catch(ex) {
  36. }
  37.  
  38.  
  39.  
  40. function SB_initEdit()
  41. {
  42.     SBstring    = document.getElementById("ScrapBookString");
  43.     SBbrowser   = document.getElementById("ScrapBookBrowser");
  44.     SBcommentIL = document.getElementById("ScrapBookInlineTextbox");
  45.     gShowHeader = nsPreferences.getBoolPref("scrapbook.edit.showheader", false);
  46.     if ( gShowHeader ) SBheader.hidden = false;
  47.     SBRDF.init();
  48.     gID = document.location.href.match(/\?id\=(\d{14})$/);
  49.     gID = RegExp.$1;
  50.     SB_initBrowser();
  51.     gChanged = false;
  52.     SB_setIntervalConfirmSave();
  53. }
  54.  
  55.  
  56. function SB_setIntervalConfirmSave()
  57. {
  58.     var sec = nsPreferences.getIntPref("scrapbook.edit.confirmsave", DEFAULT_INTERVAL);
  59.     if ( sec > 0 ) gTimerID = window.setInterval(SB_checkShouldConfirmSave, sec * 1000 * 60);
  60. }
  61.  
  62.  
  63. function SB_initBrowser()
  64. {
  65.     if ( !gID ) return;
  66.     var myDir = SBcommon.getContentDir(gID);
  67.     var myDirPath = SBservice.IO.newFileURI(myDir).spec;
  68.     SBbrowser.setAttribute("src", myDirPath + "index.html");
  69. }
  70.  
  71.  
  72. function SB_getFrameList(aWindow)
  73. {
  74.     for ( var f=0; f<aWindow.frames.length; f++ )
  75.     {
  76.         gFrames.push(aWindow.frames[f]);
  77.         SB_getFrameList(aWindow.frames[f]);
  78.     }
  79. }
  80.  
  81.  
  82. function SB_browserOnload(aEvent)
  83. {
  84.     aEvent.preventBubble();
  85.     aEvent.stopPropagation();
  86.     gWindow = document.getElementById("ScrapBookBrowser").contentWindow;
  87.     if ( gWindow.location.href == "about:blank" ) return;
  88.     gFrames = [gWindow];
  89.     SB_getFrameList(gWindow);
  90.  
  91.     if ( gWindow.location.href.match(/^file:/) && gWindow.location.href.match(/\/data\/(\d{14})\/index\.html$/) )
  92.     {
  93.         gID = RegExp.$1;
  94.         SBeditor.disable(false);
  95.         SBeditor.init(gID);
  96.     }
  97.     else
  98.     {
  99.         window.location.href = gWindow.location.href;
  100.     }
  101.     gChanged = false;
  102.  
  103.     SBeditStyle.apply("scrapbook-inline-comment-style", "");
  104.     for ( var f=0; f<gFrames.length; f++ )
  105.     {
  106.         var aDocument = gFrames[f].document;
  107.         aDocument.removeEventListener("click", SBinlineComment.clickToEdit, true);
  108.         aDocument.addEventListener("click"   , SBinlineComment.clickToEdit, true);
  109.     }
  110.     document.getElementById("ScrapBookBlockHide").setAttribute("checked", false); 
  111.  
  112.     var myBrowser = SBservice.WM.getMostRecentWindow("navigator:browser").getBrowser();
  113.     if ( myBrowser.selectedBrowser.contentWindow.gID == gID )
  114.     {
  115.         myBrowser.selectedTab.label = SBeditor.item.title;
  116.         myBrowser.selectedTab.setAttribute("image", SBeditor.item.icon);
  117.     }
  118. }
  119.  
  120.  
  121. function SB_exitEditingMode()
  122. {
  123.     if ( SBeditor.eraser ) SBeditor.toggleEraser();
  124.     SB_checkShouldConfirmSave();
  125.     window.location.href = gWindow.location.href;
  126. }
  127.  
  128.  
  129. function SB_checkShouldConfirmSave()
  130. {
  131.     if ( gChanged ) SB_confirmSave();
  132.     gChanged = false;
  133. }
  134.  
  135.  
  136. function SB_confirmSave()
  137. {
  138.     const PROMPT = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService);
  139.     var target = SBeditor.item.title;
  140.     var button = PROMPT.BUTTON_TITLE_SAVE      * PROMPT.BUTTON_POS_0
  141.                + PROMPT.BUTTON_TITLE_DONT_SAVE * PROMPT.BUTTON_POS_1;
  142.     var result = PROMPT.confirmEx(window, "ScrapBook", SBstring.getFormattedString("EDIT_SAVE_CHANGES", [target]), button, null, null, null, null, {});
  143.     if ( result == 0 ) SBeditor.save();
  144. }
  145.  
  146.  
  147.  
  148. function SE_getSelection()
  149. {
  150.     var myWindow = document.commandDispatcher.focusedWindow;
  151.     if ( !myWindow || myWindow == window ) myWindow = window._content;
  152.     var selectedText = myWindow.getSelection();
  153.     var mySelection = selectedText.QueryInterface(Components.interfaces.nsISelectionPrivate);
  154.     var isSelected = false;
  155.     try {
  156.         isSelected = ( mySelection.anchorNode.isSameNode(mySelection.focusNode) && mySelection.anchorOffset == mySelection.focusOffset ) ? false : true;
  157.     } catch(ex) {
  158.         isSelected = false;
  159.     }
  160.     return isSelected ? mySelection : false;
  161. }
  162.  
  163.  
  164. function SE_cutter(aEvent)
  165. {
  166.     var mySelection = SE_getSelection();
  167.     if ( !mySelection ) return;
  168.     mySelection.deleteFromDocument();
  169.     gChanged = true;
  170.     SBeditDOMEraser.allowUndo(false);
  171. }
  172.  
  173.  
  174. function SE_initMarker()
  175. {
  176.     var col = nsPreferences.copyUnicharPref("scrapbook.editor.marker", "#FFFF00");
  177.     var colList = { "#FFFF00" : "Y", "#90EE90" : "G", "#ADD8E6" : "B", "#FFB6C1" : "P" };
  178.     document.getElementById("ScrapEditorMarker" + colList[col]).setAttribute("checked", true);
  179. }
  180.  
  181.  
  182. function SE_marker(bgcol)
  183. {
  184.     if ( !bgcol ) bgcol = nsPreferences.copyUnicharPref("scrapbook.editor.marker", "#FFFF00");
  185.     nsPreferences.setUnicharPref("scrapbook.editor.marker", bgcol);
  186.     var mySelection = SE_getSelection();
  187.     if ( !mySelection ) return;
  188.     lmSetMarker(mySelection, "linemarker-marked-line", "background-color: " + bgcol + "; color: #000000;");
  189.     gChanged = true;
  190.     SBeditDOMEraser.allowUndo(false);
  191. }
  192.  
  193.  
  194. function SE_removeAllSpan(aClass)
  195. {
  196.     for ( var f=0; f<gFrames.length; f++ )
  197.     {
  198.         var spanElems = gFrames[f].document.getElementsByTagName("span");
  199.         for ( var i = 0; i < spanElems.length; i++ )
  200.         {
  201.             if ( spanElems[i].getAttribute("class") == aClass )
  202.             {
  203.                 spanElems[i].removeAttribute("style");
  204.                 spanElems[i].removeAttribute("class");
  205.                 spanElems[i].removeAttribute("title");
  206.             }
  207.         }
  208.     }
  209.     gChanged = true;
  210.     SBeditDOMEraser.allowUndo(false);
  211. }
  212.  
  213.  
  214. function SE_removeElementsByTagName(aTag)
  215. {
  216.     var shouldSave = false;
  217.     for ( var f = gFrames.length - 1; f >= 0; f-- )
  218.     {
  219.         var elems = gFrames[f].document.getElementsByTagName(aTag);
  220.         if ( elems.length < 1 ) continue;
  221.         for ( var i = elems.length - 1; i >= 0; i-- )
  222.         {
  223.             SBhtmlDocUtil.removeNodeFromParent(elems[i]);
  224.         }
  225.         shouldSave = true;
  226.     }
  227.     if ( shouldSave )
  228.     {
  229.         gChanged = true;
  230.         SBeditDOMEraser.allowUndo(false);
  231.         SB_confirmSave();
  232.     }
  233. }
  234.  
  235.  
  236.  
  237.  
  238. var SBeditDOMEraser = {
  239.  
  240.     parent   : null,
  241.     child    : null,
  242.     refChild : null,
  243.  
  244.     handleEvent : function(aEvent)
  245.     {
  246.         aEvent.preventDefault();
  247.         var targetElement = aEvent.originalTarget;
  248.         var tagName = targetElement.localName.toUpperCase();
  249.         try {
  250.             document.getElementById("ScrapEditorFlasher").value = tagName;
  251.         } catch(ex) {}
  252.         if ( aEvent.type != "keypress" && (tagName == "HTML" || tagName == "BODY") ) return;
  253.         var onMarkerLine = ( tagName == "SPAN" && targetElement.getAttribute("class") == "linemarker-marked-line" );
  254.  
  255.         switch ( aEvent.type )
  256.         {
  257.             case "mouseover" :
  258.                 document.getElementById("ScrapBookTooltip").label = onMarkerLine ? SBstring.getString("EDIT_REMOVE_HIGHLIGHT") : tagName;
  259.                 document.getElementById("ScrapBookTooltip").showPopup(SBbrowser, aEvent.clientX + SBbrowser.boxObject.screenX, aEvent.clientY + SBbrowser.boxObject.screenY);
  260.                 if ( IFLASHER ) {
  261.                     IFLASHER.color = onMarkerLine ? "#3333FF" : "#FF3333";
  262.                     IFLASHER.drawElementOutline(targetElement);
  263.                 }
  264.                 break;
  265.             case "mouseout" :
  266.                 if ( IFLASHER ) {
  267.                     IFLASHER.repaintElement(targetElement);
  268.                 }
  269.                 break;
  270.             case "click" :
  271.                 if ( aEvent.button == 0 )
  272.                 {
  273.                     if ( tagName == "SPAN" && targetElement.getAttribute("class") == "linemarker-marked-line" ) {
  274.                         targetElement.removeAttribute("class");
  275.                         targetElement.removeAttribute("style");
  276.                         this.allowUndo(false);
  277.                     } else {
  278.                         this.parent   = targetElement.parentNode;
  279.                         this.refChild = targetElement.nextSibling;
  280.                         this.child    = this.parent.removeChild(targetElement);
  281.                         this.allowUndo(true);
  282.                     }
  283.                     gChanged = true;
  284.                 }
  285.                 break;
  286.             case "keypress" :
  287.                 this.undo();
  288.                 break;
  289.         }
  290.     },
  291.  
  292.     allowUndo : function(aBool)
  293.     {
  294.         SBundoButton.hidden = !aBool;
  295.         document.getElementById("ScrapEditorRestore").hidden = aBool;
  296.     },
  297.  
  298.     undo : function()
  299.     {
  300.         this.parent.insertBefore(this.child, this.refChild);
  301.         this.allowUndo(false);
  302.     },
  303.  
  304. };
  305.  
  306.  
  307.  
  308. var SBeditor = {
  309.  
  310.     id   : "",
  311.     res  : null,
  312.     item : null,
  313.     eraser  : false,
  314.     comment : false,
  315.  
  316.     init : function(aID)
  317.     {
  318.         if ( !SBcomment ) return;
  319.         this.id   = aID;
  320.         this.res  = gRes = SBservice.RDF.GetResource("urn:scrapbook:item" + aID);
  321.         this.item = new ScrapBookItem(aID);
  322.         for ( var prop in this.item )
  323.         {
  324.             this.item[prop] = SBRDF.getProperty(prop, this.res);
  325.         }
  326.         document.getElementById("ScrapEditorIcon").src       = this.item.icon ? this.item.icon : SBcommon.getDefaultIcon(this.item.type);
  327.         document.getElementById("ScrapEditorTitle").value    = this.item.title;
  328.         document.getElementById("ScrapEditorEraser").checked = false;
  329.         document.getElementById("ScrapEditorFlasher").value  = "";
  330.         SBcomment.value   = this.item.comment.replace(/ __BR__ /g, "\t");
  331.         SBcommentML.value = this.item.comment.replace(/ __BR__ /g, "\n");
  332.         this.eraser = false;
  333.         SBeditDOMEraser.allowUndo(false);
  334.         SE_initMarker();
  335.         if ( gShowHeader ) SBheader.firstChild.value  = this.item.source;
  336.     },
  337.  
  338.     toggleEraser : function()
  339.     {
  340.         this.eraser = !this.eraser;
  341.         gFrames = [gWindow];
  342.         SB_getFrameList(gWindow);
  343.         for ( var f=0; f<gFrames.length; f++ )
  344.         {
  345.             gFrames[f].document.removeEventListener("mouseover", SBeditDOMEraser, true);
  346.             gFrames[f].document.removeEventListener("mouseout" , SBeditDOMEraser, true);
  347.             gFrames[f].document.removeEventListener("click"    , SBeditDOMEraser, true);
  348.             gFrames[f].document.removeEventListener("keypress" , SBeditDOMEraser, true);
  349.         }
  350.         SBeditStyle.remove("scrapbook-eraser-style");
  351.         if ( this.eraser ) {
  352.             for ( var f=0; f<gFrames.length; f++ )
  353.             {
  354.                 gFrames[f].document.addEventListener("mouseover", SBeditDOMEraser, true);
  355.                 gFrames[f].document.addEventListener("mouseout" , SBeditDOMEraser, true);
  356.                 gFrames[f].document.addEventListener("click"    , SBeditDOMEraser, true);
  357.                 gFrames[f].document.addEventListener("keypress" , SBeditDOMEraser, true);
  358.             }
  359.             SBeditStyle.apply("scrapbook-eraser-style", "* { cursor: crosshair; }");
  360.         }
  361.         document.getElementById("ScrapEditorInline").disabled = this.eraser;
  362.         document.getElementById("ScrapEditorCutter").disabled = this.eraser;
  363.         document.getElementById("ScrapEditorMarker").disabled = this.eraser;
  364.         document.getElementById("ScrapEditorFlasher").value = "";
  365.     },
  366.  
  367.     toggleCommentXUL : function(inverse)
  368.     {
  369.         this.comment = inverse ? !this.comment : nsPreferences.getBoolPref("scrapbook.edit.multilines", false);
  370.         SBcomment.disabled = this.comment;
  371.         SBcommentML.hidden = !this.comment;
  372.         SBcomment.setAttribute("style", this.comment ? "visibility:hidden;" : "padding:2px;");
  373.         SBcomment.value = this.comment ? SBcomment.value.replace(/\t/g, "\n") : SBcommentML.value.replace(/\n/g, "\t");
  374.         (this.comment ? SBcommentML : SBcomment).focus();
  375.         nsPreferences.setBoolPref("scrapbook.edit.multilines", this.comment);
  376.     },
  377.  
  378.  
  379.  
  380.     restore : function()
  381.     {
  382.         document.getElementById("ScrapBookBrowser").removeAttribute("src");
  383.         SB_initBrowser();
  384.     },
  385.  
  386.     save : function()
  387.     {
  388.         gFrames = [gWindow];
  389.         SB_getFrameList(gWindow);
  390.  
  391.         SBeditor.disable(true);
  392.  
  393.         SBeditStyle.removeAll();
  394.  
  395.         for ( var f=0; f<gFrames.length; f++ )
  396.         {
  397.             var aDocument = gFrames[f].document;
  398.  
  399.             var rootNode  = aDocument.getElementsByTagName("html")[0];
  400.             var mySrc = "";
  401.             mySrc = SBhtmlDocUtil.surroundByTags(rootNode, rootNode.innerHTML);
  402.             mySrc = SBhtmlDocUtil.doctypeToString(aDocument.doctype) + mySrc;
  403.  
  404.             mySrc = mySrc.replace(/ -moz-background-clip: initial; -moz-background-origin: initial; -moz-background-inline-policy: initial;\">/g, '">');
  405.             mySrc = mySrc.replace(/<span>([^<]*)<\/span>/gm, "$1");
  406.  
  407.             var myFile = SBcommon.getContentDir(this.item.id).clone();
  408.             myFile.append(SBcommon.getFileName(aDocument.location.href));
  409.             SBcommon.writeFile(myFile, mySrc, aDocument.characterSet);
  410.         }
  411.  
  412.         SBeditor.saveResource();
  413.         SBbrowser.reload();
  414.         gChanged = false;
  415.         window.setTimeout(function() { SBbrowser.stop(); SBeditor.disable(false); }, 500);
  416.     },
  417.  
  418.     saveResource : function()
  419.     {
  420.         var newTitle   = document.getElementById("ScrapEditorTitle").value;
  421.         var newComment = (this.comment ? SBcommentML : SBcomment).value.replace(/\t|\n/g, " __BR__ ");
  422.         if ( newTitle != this.item.title || newComment != this.item.comment )
  423.         {
  424.             SBRDF.updateItem(this.res, "title",   newTitle);
  425.             SBRDF.updateItem(this.res, "comment", newComment);
  426.             SBRDF.flush();
  427.             this.item.title   = newTitle;
  428.             this.item.comment = newComment;
  429.             SBcommon.writeIndexDat(this.item);
  430.             this.disableTemporary(500);
  431.         }
  432.     },
  433.  
  434.     disableTemporary : function(msec)
  435.     {
  436.         window.setTimeout(function() { SBeditor.disable(true);  }, 0);
  437.         window.setTimeout(function() { SBeditor.disable(false); }, msec);
  438.     },
  439.  
  440.     disable : function(aBool)
  441.     {
  442.         var editorXULs = document.getElementById("ScrapEditor").childNodes;
  443.         for ( var i = 0; i < editorXULs.length; i++ )
  444.         {
  445.             if ( editorXULs[i].id != "ScrapEditorUndo" ) editorXULs[i].disabled = aBool;
  446.         }
  447.         SBcommentML.disabled = aBool;
  448.     }
  449.  
  450. };
  451.  
  452.  
  453.  
  454. var SBinlineComment = {
  455.  
  456.     selection  : null,
  457.     targetSpan : null,
  458.  
  459.     showXUL : function(show)
  460.     {
  461.         if ( SBeditor.comment ) SBcommentML.hidden = show;
  462.         document.getElementById("ScrapEditor").hidden = show;
  463.         document.getElementById("ScrapBookInline").hidden = !show;
  464.         document.getElementById("ScrapBookInlineHeader").hidden = !show;
  465.     },
  466.  
  467.     init : function(target)
  468.     {
  469.         if ( !target ) this.selection = SE_getSelection();
  470.         if ( !target && !this.selection ) return;
  471.         this.targetSpan = target ? target : null;
  472.         this.showXUL(true);
  473.         SBcommentIL.value = target ? target.getAttribute("title") : "";
  474.         SBcommentIL.select();
  475.         var label = target ? target.innerHTML : this.selection.toString();
  476.         label = label.length > 72 ? label.substring(0,72) + "..." : label;
  477.         document.getElementById("ScrapBookInlineHeader").firstChild.value = SBstring.getFormattedString("EDIT_INLINE_COMMENT", [label]);
  478.     },
  479.  
  480.     save : function()
  481.     {
  482.         if ( this.targetSpan )
  483.         {
  484.             if ( !SBcommentIL.value ) {
  485.                 this.targetSpan.removeAttribute("class");
  486.                 this.targetSpan.removeAttribute("style");
  487.                 this.targetSpan.removeAttribute("title");
  488.             } else {
  489.                 this.targetSpan.setAttribute("title", SBcommentIL.value);
  490.             }
  491.         }
  492.         else
  493.         {
  494.             if ( !SBcommentIL.value ) return;
  495.             lmSetMarker(
  496.                 this.selection,
  497.                 "scrapbook-inline-comment",
  498.                 "border-bottom: 2px dotted #FF3333; cursor: help;",
  499.                 SBcommentIL.value
  500.             );
  501.         }
  502.         gChanged = true;
  503.         this.exit();
  504.     },
  505.  
  506.     exit : function()
  507.     {
  508.         this.showXUL(false);
  509.     },
  510.  
  511.     remove : function ()
  512.     {
  513.         SBcommentIL.value = "";
  514.         this.save();
  515.     },
  516.  
  517.     keypress : function(aEvent)
  518.     {
  519.         if      ( aEvent.keyCode == 13 ) this.save();
  520.         else if ( aEvent.keyCode == 27 ) this.exit();
  521.     },
  522.  
  523.     clickToEdit : function(aEvent)
  524.     {
  525.         var tName  = aEvent.originalTarget.localName.toUpperCase();
  526.         var tClass = aEvent.originalTarget.getAttribute("class");
  527.         if      ( tName == "SPAN" && tClass == "scrapbook-inline-comment" ) SBinlineComment.init(aEvent.originalTarget);
  528.         else if ( tName == "DIV"  && tClass == "scrapbook-block-comment"  ) SBblockComment.edit(aEvent.originalTarget);
  529.     },
  530.  
  531. };
  532.  
  533.  
  534. var SBblockComment = {
  535.  
  536.     get DEFAULT_STYLE()
  537.     {
  538.         return "font-size: 0.9em !important; font-weight: normal !important;\n"
  539.              + "text-decoration: none !important;\n"
  540.              + "line-height: 1.5em !important;\n"
  541.              + "color: #000000 !important;\n\n"
  542.              + "border: 1px solid #0FA9E5 !important;\n"
  543.              + "background-color: #E7F4FC !important;\n\n"
  544.              + "margin: 10px !important;\n"
  545.              + "padding: 10px !important;\n\n"
  546.              + "white-space:normal !important;\n"
  547.              + "cursor: pointer !important;\n"
  548.              + "";
  549.     },
  550.  
  551.     add : function()
  552.     {
  553.         var myWindow = SBcommon.getFocusedWindow();
  554.         if ( !myWindow.location.href.match(/^file:\/\//) ) return;
  555.         var selectedText = myWindow.getSelection();
  556.         var mySelection = selectedText.QueryInterface(Components.interfaces.nsISelectionPrivate);
  557.         var targetNode = mySelection.anchorNode;
  558.         if ( !targetNode ) return;
  559.         if ( targetNode.nodeName == "#text" ) targetNode = targetNode.parentNode;
  560.         targetNode.appendChild(this.duplicate());
  561.         targetNode.lastChild.firstChild.firstChild.focus();
  562.         this.change();
  563.     },
  564.  
  565.     edit : function(oldElement)
  566.     {
  567.         var newElement = this.duplicate();
  568.         newElement.firstChild.firstChild.appendChild(document.createTextNode(oldElement.firstChild.data));
  569.         oldElement.parentNode.replaceChild(newElement, oldElement);
  570.         newElement.firstChild.firstChild.focus();
  571.         this.change();
  572.     },
  573.  
  574.     duplicate : function()
  575.     {
  576.         var divElement = document.getElementById("ScrapBookBlock").cloneNode(true);
  577.         divElement.setAttribute("style", nsPreferences.copyUnicharPref("scrapbook.editor.blockstyle", this.DEFAULT_STYLE));
  578.         divElement.removeAttribute("id");
  579.         return divElement;
  580.     },
  581.  
  582.     customizeCSS : function(init)
  583.     {
  584.         if ( SBeditor.comment ) SBcommentML.hidden = init;
  585.         document.getElementById("ScrapEditor").hidden = init;
  586.         document.getElementById("ScrapBookBlockStyle").hidden = !init;
  587.         document.getElementById("ScrapBookBlockHeader").hidden = !init;
  588.         if ( init ) {
  589.             document.getElementById("ScrapBookBlockStyle").value = nsPreferences.copyUnicharPref("scrapbook.editor.blockstyle", this.DEFAULT_STYLE);
  590.             document.getElementById("ScrapBookBlockStyle").focus();
  591.         } else {
  592.             nsPreferences.setUnicharPref("scrapbook.editor.blockstyle", document.getElementById("ScrapBookBlockStyle").value);
  593.         }
  594.     },
  595.  
  596.     toggleHide : function()
  597.     {
  598.         if ( document.getElementById("ScrapBookBlockHide").getAttribute("checked") ) {
  599.             SBeditStyle.apply("scrapbook-hide-block-comment", ".scrapbook-block-comment { display: none; }");
  600.         } else {
  601.             SBeditStyle.remove("scrapbook-hide-block-comment");
  602.         }
  603.     },
  604.  
  605.     change : function()
  606.     {
  607.         gChanged = true;
  608.         SBeditDOMEraser.allowUndo(false);
  609.     },
  610.  
  611. };
  612.  
  613.  
  614. var SBhtmlDocUtil = {
  615.  
  616.     removeNodeFromParent : function(aNode)
  617.     {
  618.         var newNode = document.createTextNode("");
  619.         aNode.parentNode.replaceChild(newNode, aNode);
  620.         aNode = newNode;
  621.         return aNode;
  622.     },
  623.     surroundByTags : function(aNode, aContent)
  624.     {
  625.         var tag = "<" + aNode.nodeName.toLowerCase();
  626.         for ( var i=0; i<aNode.attributes.length; i++ )
  627.         {
  628.             tag += ' ' + aNode.attributes[i].name + '="' + aNode.attributes[i].value + '"';
  629.         }
  630.         tag += ">\n";
  631.         return tag + aContent + "</" + aNode.nodeName.toLowerCase() + ">\n";
  632.     },
  633.     doctypeToString : function(aDoctype)
  634.     {
  635.         if ( !aDoctype ) return "";
  636.         var ret = "<!DOCTYPE " + aDoctype.name;
  637.         if ( aDoctype.publicId ) ret += ' PUBLIC "' + aDoctype.publicId + '"';
  638.         if ( aDoctype.systemId ) ret += ' "'        + aDoctype.systemId + '"';
  639.         ret += ">\n";
  640.         return ret;
  641.     },
  642. };
  643.  
  644.  
  645.  
  646.  
  647. var SBeditStyle = {
  648.  
  649.     apply : function(aStyleID, aStyleString)
  650.     {
  651.         for ( var f=0; f<gFrames.length; f++ )
  652.         {
  653.             var newStyleNode = gFrames[f].document.createElement("style");
  654.             newStyleNode.setAttribute("media", "screen");
  655.             newStyleNode.setAttribute("type", "text/css");
  656.             newStyleNode.setAttribute("id", aStyleID);
  657.             newStyleNode.appendChild(gFrames[f].document.createTextNode(aStyleString));
  658.             var headNode = gFrames[f].document.getElementsByTagName("head")[0];
  659.             headNode.appendChild(newStyleNode);
  660.         }
  661.     },
  662.  
  663.     remove : function(aStyleID)
  664.     {
  665.         for ( var f=0; f<gFrames.length; f++ )
  666.         {
  667.             try {
  668.                 SBhtmlDocUtil.removeNodeFromParent(gFrames[f].document.getElementById(aStyleID));
  669.             } catch(ex) {
  670.             }
  671.         }
  672.     },
  673.  
  674.     removeAll : function()
  675.     {
  676.         for ( var f=0; f<gFrames.length; f++ )
  677.         {
  678.             try {
  679.                 var styleNodes = gFrames[f].document.getElementsByTagName("style");
  680.             } catch(ex) {
  681.             }
  682.             for ( var i = styleNodes.length - 1; i >= 0 ; i-- )
  683.             {
  684.                 if ( styleNodes[i].id.substring(0,10) == "scrapbook-" ) SBhtmlDocUtil.removeNodeFromParent(styleNodes[i]);
  685.             }
  686.         }
  687.     },
  688.  
  689. };
  690.  
  691.  
  692.