From 140f858dfd7fb7941790fcc1ad338cb150f972c0 Mon Sep 17 00:00:00 2001 From: "cmanske%netscape.com" Date: Thu, 4 May 2000 14:02:03 +0000 Subject: [PATCH] HTML Soure editor is working. Tweaked some dialog xul. Removed some unused stuff from EditorCommands.js git-svn-id: svn://10.0.0.236/trunk@68234 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/editor/base/nsEditorShell.cpp | 57 ++++++--- mozilla/editor/base/nsHTMLEditor.cpp | 11 ++ mozilla/editor/base/nsHTMLEditor.h | 1 + mozilla/editor/composer/src/nsEditorShell.cpp | 57 ++++++--- .../editor/libeditor/html/nsHTMLEditor.cpp | 11 ++ mozilla/editor/libeditor/html/nsHTMLEditor.h | 1 + mozilla/editor/public/nsIHTMLEditor.h | 4 + .../ui/composer/content/EditorCommands.js | 117 ++++++++++-------- .../composer/content/TextEditorAppShell.xul | 8 +- mozilla/editor/ui/composer/content/editor.xul | 71 +++++------ .../ui/composer/content/editorOverlay.xul | 7 +- mozilla/editor/ui/composer/skin/editor.css | 12 ++ .../ui/dialogs/content/EdHLineProps.xul | 10 +- .../editor/ui/dialogs/skin/EditorDialog.css | 3 +- 14 files changed, 238 insertions(+), 132 deletions(-) diff --git a/mozilla/editor/base/nsEditorShell.cpp b/mozilla/editor/base/nsEditorShell.cpp index 40631fdfc1e..a6bf9f2ca4a 100644 --- a/mozilla/editor/base/nsEditorShell.cpp +++ b/mozilla/editor/base/nsEditorShell.cpp @@ -168,13 +168,15 @@ GetDocument(nsIDocShell *aDocShell, nsIDocument **aDoc ) return res; } -// Utility to set and attribute of an element (used for throbber) +// Utility get a UI element static nsresult -SetChromeAttribute( nsIDocShell *shell, const char *id, - const char *name, const nsString &value ) +GetChromeElement(nsIDocShell *aShell, const char *aID, nsIDOMElement **aElement) { + if (!aElement) return NS_ERROR_NULL_POINTER; + *aElement = nsnull; + nsCOMPtr doc; - nsresult rv = GetDocument( shell, getter_AddRefs(doc) ); + nsresult rv = GetDocument( aShell, getter_AddRefs(doc) ); if(NS_SUCCEEDED(rv) && doc) { // Up-cast. @@ -183,15 +185,43 @@ SetChromeAttribute( nsIDocShell *shell, const char *id, { // Find specified element. nsCOMPtr elem; - rv = xulDoc->GetElementById( NS_ConvertASCIItoUCS2(id), getter_AddRefs(elem) ); - if ( elem ) - // Set the text attribute. - rv = elem->SetAttribute( NS_ConvertASCIItoUCS2(name), value ); + rv = xulDoc->GetElementById( NS_ConvertASCIItoUCS2(aID), getter_AddRefs(elem) ); + if (elem) + { + *aElement = elem.get(); + NS_ADDREF(*aElement); + } } } return rv; } +// Utility to set and attribute of a UI element +static nsresult +SetChromeAttribute(nsIDocShell *aShell, const char *aID, + const char *aName, const nsString &aValue) +{ + nsCOMPtr elem; + nsresult rv = GetChromeElement(aShell, aID, getter_AddRefs(elem)); + if (NS_SUCCEEDED(rv) && elem) + // Set the text attribute. + rv = elem->SetAttribute( NS_ConvertASCIItoUCS2(aName), aValue); + + return rv; +} + +static nsresult +RemoveChromeAttribute(nsIDocShell *aShell, const char *aID, const char *aName) +{ + nsCOMPtr elem; + nsresult rv = GetChromeElement(aShell, aID, getter_AddRefs(elem)); + if (NS_SUCCEEDED(rv) && elem) + // Set the text attribute. + rv = elem->RemoveAttribute(NS_ConvertASCIItoUCS2(aName)); + + return rv; +} + // Utility to get the treeOwner for a docShell static nsresult GetTreeOwner(nsIDocShell* aDocShell, nsIBaseWindow** aBaseWindow) @@ -484,10 +514,9 @@ nsEditorShell::PrepareDocumentForEditing(nsIDocumentLoader* aLoader, nsIURI *aUr if (!mMailCompose) { mContentWindow->Focus(); - // turn on caret - nsCOMPtr selCon; - editor->GetSelectionController(getter_AddRefs(selCon)); - if (selCon) selCon->SetCaretEnabled(PR_TRUE); + // Collapse the selection to the begining of the document + // (this also turns on the caret) + mEditor->SetCaretToDocumentStart(); } return NS_OK; } @@ -1023,11 +1052,11 @@ nsEditorShell::SetDisplayMode(PRInt32 aDisplayMode) { nsresult res = NS_OK; + // Reqesting SourceMode and we are already doing that if (aDisplayMode == eDisplayModeSource && mDisplayMode == eDisplayModeSource) return NS_OK; - nsAutoString indexVal = NS_ConvertASCIItoUCS2((aDisplayMode == eDisplayModeSource) ? "1" : "0"); - SetChromeAttribute( mDocShell, "ContentWindowDeck", "index", indexVal ); + // The rest of the HTML Source display work is in EditorCommand.js nsCOMPtr styleSheets = do_QueryInterface(mEditor); if (!styleSheets) return NS_NOINTERFACE; diff --git a/mozilla/editor/base/nsHTMLEditor.cpp b/mozilla/editor/base/nsHTMLEditor.cpp index aed62ff2f16..fcd8394b5f5 100644 --- a/mozilla/editor/base/nsHTMLEditor.cpp +++ b/mozilla/editor/base/nsHTMLEditor.cpp @@ -2142,6 +2142,17 @@ nsresult nsHTMLEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, return result; } +NS_IMETHODIMP +nsHTMLEditor::SetCaretToDocumentStart() +{ + nsCOMPtr bodyElement; + nsresult res = nsEditor::GetBodyElement(getter_AddRefs(bodyElement)); + if (NS_FAILED(res)) return res; + if (!bodyElement) return NS_ERROR_NULL_POINTER; + nsCOMPtr bodyNode = do_QueryInterface(bodyElement); + return CollapseSelectionToDeepestNonTableFirstChild(nsnull, bodyNode); +} + nsresult nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsIDOMSelection *aSelection, nsIDOMNode *aNode) { diff --git a/mozilla/editor/base/nsHTMLEditor.h b/mozilla/editor/base/nsHTMLEditor.h index 822eca27682..49719c6c8f6 100644 --- a/mozilla/editor/base/nsHTMLEditor.h +++ b/mozilla/editor/base/nsHTMLEditor.h @@ -120,6 +120,7 @@ public: NS_IMETHOD DeleteSelectionAndCreateNode(const nsString& aTag, nsIDOMNode ** aNewNode); NS_IMETHOD SelectElement(nsIDOMElement* aElement); NS_IMETHOD SetCaretAfterElement(nsIDOMElement* aElement); + NS_IMETHOD SetCaretToDocumentStart(); NS_IMETHOD SetParagraphFormat(const nsString& aParagraphFormat); diff --git a/mozilla/editor/composer/src/nsEditorShell.cpp b/mozilla/editor/composer/src/nsEditorShell.cpp index 40631fdfc1e..a6bf9f2ca4a 100644 --- a/mozilla/editor/composer/src/nsEditorShell.cpp +++ b/mozilla/editor/composer/src/nsEditorShell.cpp @@ -168,13 +168,15 @@ GetDocument(nsIDocShell *aDocShell, nsIDocument **aDoc ) return res; } -// Utility to set and attribute of an element (used for throbber) +// Utility get a UI element static nsresult -SetChromeAttribute( nsIDocShell *shell, const char *id, - const char *name, const nsString &value ) +GetChromeElement(nsIDocShell *aShell, const char *aID, nsIDOMElement **aElement) { + if (!aElement) return NS_ERROR_NULL_POINTER; + *aElement = nsnull; + nsCOMPtr doc; - nsresult rv = GetDocument( shell, getter_AddRefs(doc) ); + nsresult rv = GetDocument( aShell, getter_AddRefs(doc) ); if(NS_SUCCEEDED(rv) && doc) { // Up-cast. @@ -183,15 +185,43 @@ SetChromeAttribute( nsIDocShell *shell, const char *id, { // Find specified element. nsCOMPtr elem; - rv = xulDoc->GetElementById( NS_ConvertASCIItoUCS2(id), getter_AddRefs(elem) ); - if ( elem ) - // Set the text attribute. - rv = elem->SetAttribute( NS_ConvertASCIItoUCS2(name), value ); + rv = xulDoc->GetElementById( NS_ConvertASCIItoUCS2(aID), getter_AddRefs(elem) ); + if (elem) + { + *aElement = elem.get(); + NS_ADDREF(*aElement); + } } } return rv; } +// Utility to set and attribute of a UI element +static nsresult +SetChromeAttribute(nsIDocShell *aShell, const char *aID, + const char *aName, const nsString &aValue) +{ + nsCOMPtr elem; + nsresult rv = GetChromeElement(aShell, aID, getter_AddRefs(elem)); + if (NS_SUCCEEDED(rv) && elem) + // Set the text attribute. + rv = elem->SetAttribute( NS_ConvertASCIItoUCS2(aName), aValue); + + return rv; +} + +static nsresult +RemoveChromeAttribute(nsIDocShell *aShell, const char *aID, const char *aName) +{ + nsCOMPtr elem; + nsresult rv = GetChromeElement(aShell, aID, getter_AddRefs(elem)); + if (NS_SUCCEEDED(rv) && elem) + // Set the text attribute. + rv = elem->RemoveAttribute(NS_ConvertASCIItoUCS2(aName)); + + return rv; +} + // Utility to get the treeOwner for a docShell static nsresult GetTreeOwner(nsIDocShell* aDocShell, nsIBaseWindow** aBaseWindow) @@ -484,10 +514,9 @@ nsEditorShell::PrepareDocumentForEditing(nsIDocumentLoader* aLoader, nsIURI *aUr if (!mMailCompose) { mContentWindow->Focus(); - // turn on caret - nsCOMPtr selCon; - editor->GetSelectionController(getter_AddRefs(selCon)); - if (selCon) selCon->SetCaretEnabled(PR_TRUE); + // Collapse the selection to the begining of the document + // (this also turns on the caret) + mEditor->SetCaretToDocumentStart(); } return NS_OK; } @@ -1023,11 +1052,11 @@ nsEditorShell::SetDisplayMode(PRInt32 aDisplayMode) { nsresult res = NS_OK; + // Reqesting SourceMode and we are already doing that if (aDisplayMode == eDisplayModeSource && mDisplayMode == eDisplayModeSource) return NS_OK; - nsAutoString indexVal = NS_ConvertASCIItoUCS2((aDisplayMode == eDisplayModeSource) ? "1" : "0"); - SetChromeAttribute( mDocShell, "ContentWindowDeck", "index", indexVal ); + // The rest of the HTML Source display work is in EditorCommand.js nsCOMPtr styleSheets = do_QueryInterface(mEditor); if (!styleSheets) return NS_NOINTERFACE; diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp index aed62ff2f16..fcd8394b5f5 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.cpp @@ -2142,6 +2142,17 @@ nsresult nsHTMLEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode, return result; } +NS_IMETHODIMP +nsHTMLEditor::SetCaretToDocumentStart() +{ + nsCOMPtr bodyElement; + nsresult res = nsEditor::GetBodyElement(getter_AddRefs(bodyElement)); + if (NS_FAILED(res)) return res; + if (!bodyElement) return NS_ERROR_NULL_POINTER; + nsCOMPtr bodyNode = do_QueryInterface(bodyElement); + return CollapseSelectionToDeepestNonTableFirstChild(nsnull, bodyNode); +} + nsresult nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsIDOMSelection *aSelection, nsIDOMNode *aNode) { diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.h b/mozilla/editor/libeditor/html/nsHTMLEditor.h index 822eca27682..49719c6c8f6 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.h +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.h @@ -120,6 +120,7 @@ public: NS_IMETHOD DeleteSelectionAndCreateNode(const nsString& aTag, nsIDOMNode ** aNewNode); NS_IMETHOD SelectElement(nsIDOMElement* aElement); NS_IMETHOD SetCaretAfterElement(nsIDOMElement* aElement); + NS_IMETHOD SetCaretToDocumentStart(); NS_IMETHOD SetParagraphFormat(const nsString& aParagraphFormat); diff --git a/mozilla/editor/public/nsIHTMLEditor.h b/mozilla/editor/public/nsIHTMLEditor.h index 4a80c6887c8..2fc0b4b76af 100644 --- a/mozilla/editor/public/nsIHTMLEditor.h +++ b/mozilla/editor/public/nsIHTMLEditor.h @@ -269,6 +269,10 @@ public: */ NS_IMETHOD SetCaretAfterElement(nsIDOMElement* aElement)=0; + /** + * Set selection to start of document + */ + NS_IMETHOD SetCaretToDocumentStart()=0; /** * Document me! diff --git a/mozilla/editor/ui/composer/content/EditorCommands.js b/mozilla/editor/ui/composer/content/EditorCommands.js index c2086006616..d480ff8409f 100644 --- a/mozilla/editor/ui/composer/content/EditorCommands.js +++ b/mozilla/editor/ui/composer/content/EditorCommands.js @@ -43,20 +43,22 @@ var docWasModified = false; // Check if clean document, if clean then unload wh var contentWindow = 0; var sourceContentWindow = 0; var ContentWindowDeck; -var EditorMenuAndToolbars; +var EditorToolbox; +// Bummer! Can't get at enums from nsIDocumentEncoder.h +var gOutputSelectionOnly = 1; +var gOutputFormatted = 2; +var gOutputNoDoctype = 4; +var gOutputBodyOnly = 8; +var gOutputPreformatted = 16; +var gOutputWrap = 32; +var gOutputFormatFlowed = 64; +var gOutputAbsoluteLinks = 128; +var gOutputEncodeEntities = 256; var gPrefs; // These must be kept in synch with the XUL lists -var gParagraphTagNames = new Array("","P","H1","H2","H3","H4","H5","H6","BLOCKQUOTE","ADDRESS","PRE","DT","DD"); -var gFontFaceNames = new Array("","tt","Arial, Helvetica","Times","Courier"); var gFontSizeNames = new Array("xx-small","x-small","small","medium","large","x-large","xx-large"); -var gStyleTags = { - "bold" : "b", - "italic" : "i", - "underline" : "u" - }; - const nsIFilePicker = Components.interfaces.nsIFilePicker; function EditorOnLoad() @@ -124,11 +126,11 @@ var DocumentStateListener = function EditorStartup(editorType, editorElement) { contentWindow = window.content; - sourceContentWindow = document.getElementById("HTMLSourceWindow"); + sourceContentWindow = document.getElementById("content-source"); // XUL elements we use when switching from normal editor to edit source ContentWindowDeck = document.getElementById("ContentWindowDeck"); - EditorMenuAndToolbars = document.getElementById("EditorMenuAndToolbars"); + EditorToolbox = document.getElementById("EditorToolbox"); // store the editor shell in the window, so that child windows can get to it. editorShell = editorElement.editorShell; @@ -476,32 +478,6 @@ function onParagraphFormatChange(commandID) // dump(" ==== onParagraphFormatChange was called. state="+state+"|\n"); return; //TODO: REWRITE THIS - var menulist = document.getElementById("ParagraphSelect"); - if (menulist) - { - // If we don't match anything, set to "normal" - var newIndex = 0; - var format = select.getAttribute("format"); - if ( format == "mixed") - { -// dump("Mixed paragraph format *******\n"); - // No single type selected - newIndex = -1; - } - else - { - for( var i = 0; i < gParagraphTagNames.length; i++) - { - if( gParagraphTagNames[i] == format ) - { - newIndex = i; - break; - } - } - } - if (menulist.selectedIndex != newIndex) - menulist.selectedIndex = newIndex; - } } function EditorSetParagraphFormat(commandID, paraFormat) @@ -890,20 +866,51 @@ function EditorAlign(commandID, alignType) function SetEditMode(mode) { - SetDisplayMode(mode); + var bodyNode = editorShell.editorDocument.getElementsByTagName("body").item(0); + if (!bodyNode) + { + dump("SetEditMode: We don't have a body node!\n"); + return; + } + // Switch the UI mode before inserting contents + // so user can't type in source window while new window is being filled + var previousMode = EditorDisplayMode; + if (!SetDisplayMode(mode)) + return; if (mode == DisplayModeSource) { - // Get the current doc and output into the SourceWindow - sourceContentWindow.value = "this is sample text. We will insert current page source here"; - - contentWindow.blur(); - sourceContentWindow.focus(); + // Get the current contents and output into the SourceWindow + if (bodyNode) + { + var childCount = bodyNode.childNodes.length; + if( childCount) + { + // KLUDGE until we have an output flag that strips out and for us + var sourceContent = editorShell.GetContentsAs("text/html", gOutputBodyOnly); + sourceContentWindow.value = sourceContent.replace(//,"").replace(/<\/body>/,""); + sourceContentWindow.focus(); + setTimeout("sourceContentWindow.focus()", 10); + return; + } + } + // If we fall through, revert to previous node + SetDisplayMode(PreviousNonSourceDisplayMode); } - else + else if (previousMode == DisplayModeSource) { - // Get the Source Window contents and paste into the HTML editor + // We are comming from edit source mode, + // so transfer that back into the document + editorShell.SelectAll(); + editorShell.InsertSource(sourceContentWindow.value); + // Clear out the source editor buffer + sourceContentWindow.value = ""; + // reset selection to top of doc (wish we could preserve it!) + if (bodyNode) + editorShell.editorSelection.collapse(bodyNode, 0); + contentWindow.focus(); + setTimeout("contentWindow.focus()", 10); } } @@ -916,6 +923,11 @@ function CancelSourceEditing() function SetDisplayMode(mode) { + // Already in requested mode: + // return false to indicate we didn't switch + if (mode == EditorDisplayMode) + return false; + EditorDisplayMode = mode; // Save the last non-source mode so we can cancel source editing easily @@ -940,16 +952,16 @@ function SetDisplayMode(mode) // Switch to the sourceWindow (second in the deck) ContentWindowDeck.setAttribute("index","1"); - // Get the current doc and output into the SourceWindow - // Hide normal chrome - EditorMenuAndToolbars.setAttribute("style", "display:none"); + // BUG IN CSS/BOXES MAKES THIS ASSERT LIKE MAD! + //EditorToolbox.setAttribute("style", "visibility:collapse"); // THIS DOESN'T WORK!? - //EditorMenuAndToolbars.setAttribute("collapsed", "true"); + //EditorToolbox.setAttribute("collapsed", "true"); // TODO: WE MUST DISABLE ALL KEYBOARD COMMANDS! - contentWindow.blur(); + + // THIS DOESN'T WORK! sourceContentWindow.focus(); } else @@ -958,13 +970,14 @@ function SetDisplayMode(mode) ContentWindowDeck.setAttribute("index","0"); // Show normal chrome - EditorMenuAndToolbars.setAttribute("style", "display:inherit"); - //EditorMenuAndToolbars.removeAttribute("collapsed"); + // BUG IN CSS/BOXES MAKES THIS ASSERT LIKE MAD! + //EditorToolbox.setAttribute("style","visibility:inherit"); // TODO: WE MUST ENABLE ALL KEYBOARD COMMANDS! contentWindow.focus(); } + return true; } function EditorToggleParagraphMarks() diff --git a/mozilla/editor/ui/composer/content/TextEditorAppShell.xul b/mozilla/editor/ui/composer/content/TextEditorAppShell.xul index e787375d8fc..cd665917f7c 100644 --- a/mozilla/editor/ui/composer/content/TextEditorAppShell.xul +++ b/mozilla/editor/ui/composer/content/TextEditorAppShell.xul @@ -45,10 +45,10 @@ persist="screenX screenY width height" > - - - - +