OSDN Git Service

Changed to allow removal of hyperlinks in notes.
authorRandy Baumgarte <randy@fbn.cx>
Fri, 7 Jan 2011 17:03:27 +0000 (12:03 -0500)
committerRandy Baumgarte <randy@fbn.cx>
Mon, 10 Jan 2011 01:14:59 +0000 (20:14 -0500)
src/cx/fbn/nevernote/dialog/InsertLinkDialog.java
src/cx/fbn/nevernote/gui/BrowserWindow.java

index 8f5339b..3694191 100644 (file)
@@ -35,10 +35,11 @@ public class InsertLinkDialog extends QDialog {
        private final QPushButton ok;\r
        private String          urlText;\r
        private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
+       private final boolean insertHyperlink;\r
        \r
        \r
        // Constructor\r
-       public InsertLinkDialog() {\r
+       public InsertLinkDialog(boolean insert) {\r
                okPressed = false;\r
                setWindowTitle(tr("Insert Link"));\r
                setWindowIcon(new QIcon(iconPath+"link.png"));\r
@@ -46,6 +47,7 @@ public class InsertLinkDialog extends QDialog {
                QGridLayout input = new QGridLayout();\r
                QGridLayout button = new QGridLayout();\r
                setLayout(grid);\r
+               insertHyperlink = insert;\r
                \r
                \r
                url = new QLineEdit("");\r
@@ -85,7 +87,7 @@ public class InsertLinkDialog extends QDialog {
        @SuppressWarnings("unused")\r
        private void validateInput() {\r
                ok.setEnabled(true);\r
-               if (url.text().trim().equals("")) \r
+               if (url.text().trim().equals("") && insertHyperlink\r
                        ok.setEnabled(false);\r
        }\r
        \r
index 951c68a..4da0b3c 100644 (file)
@@ -234,7 +234,8 @@ public class BrowserWindow extends QWidget {
     SpellDictionary userDictionary;\r
     SpellChecker spellChecker;\r
     SuggestionListener spellListener;\r
-       private final HashMap<String,Integer> previewPageList; \r
+       private final HashMap<String,Integer> previewPageList;  \r
+       boolean insertHyperlink = true;\r
        \r
        \r
        public static class SuggestionListener implements SpellCheckListener {\r
@@ -1284,7 +1285,7 @@ public class BrowserWindow extends QWidget {
                if (text.trim().equalsIgnoreCase(""))\r
                        return;\r
 \r
-               InsertLinkDialog dialog = new InsertLinkDialog();\r
+               InsertLinkDialog dialog = new InsertLinkDialog(insertHyperlink);\r
                if (currentHyperlink != null && currentHyperlink != "") {\r
                        dialog.setUrl(currentHyperlink);\r
                }\r
@@ -1293,8 +1294,12 @@ public class BrowserWindow extends QWidget {
                        logger.log(logger.EXTREME, "Insert link canceled");\r
                        return;\r
                }\r
-               if (browser.insertLinkAction.text().equalsIgnoreCase("Insert Hyperlink")) {\r
+               \r
+               // Take care of inserting new links\r
+               if (insertHyperlink) {\r
                        String selectedText = browser.selectedText();\r
+                       if (dialog.getUrl().trim().equals(""))\r
+                               return;\r
                        logger.log(logger.EXTREME, "Inserting link on text "+selectedText);\r
                        logger.log(logger.EXTREME, "URL Link " +dialog.getUrl().trim());\r
                        String dUrl = StringUtils.replace(dialog.getUrl().trim(), "'", "\\'");\r
@@ -1304,25 +1309,55 @@ public class BrowserWindow extends QWidget {
                        String script = "document.execCommand('insertHtml', false, '"+url+"');";\r
                        browser.page().mainFrame().evaluateJavaScript(script);\r
                        return;\r
-               } else {\r
-                       String js = new String( "function getCursorPos() {"\r
-                                       +"var cursorPos;"\r
-                                       +"if (window.getSelection) {"\r
-                                       +"   var selObj = window.getSelection();"\r
-                                       +"   var selRange = selObj.getRangeAt(0);"\r
-                                       +"   var workingNode = window.getSelection().anchorNode.parentNode;"\r
-                                       +"   while(workingNode != null) { " \r
-                                       +"      if (workingNode.nodeName.toLowerCase()=='a') workingNode.setAttribute('href','" +dialog.getUrl() +"');"\r
-                                       +"      workingNode = workingNode.parentNode;"\r
-                                       +"   }"\r
-                                       +"}"\r
-                                       +"} getCursorPos();");\r
-                               browser.page().mainFrame().evaluateJavaScript(js);\r
-                               contentChanged();\r
                }\r
                \r
+               // Edit existing links\r
+               String js = new String( "function getCursorPos() {"\r
+                               +"var cursorPos;"\r
+                               +"if (window.getSelection) {"\r
+                               +"   var selObj = window.getSelection();"\r
+                               +"   var selRange = selObj.getRangeAt(0);"\r
+                               +"   var workingNode = window.getSelection().anchorNode.parentNode;"\r
+                               +"   while(workingNode != null) { " \r
+                               +"      if (workingNode.nodeName.toLowerCase()=='a') workingNode.setAttribute('href','" +dialog.getUrl() +"');"\r
+                               +"      workingNode = workingNode.parentNode;"\r
+                               +"   }"\r
+                               +"}"\r
+                               +"} getCursorPos();");\r
+                       browser.page().mainFrame().evaluateJavaScript(js);\r
+               \r
+               if (!dialog.getUrl().trim().equals("")) {\r
+                       contentChanged();\r
+                       return;\r
+               }\r
+               \r
+               // Remove URL\r
+               js = new String( "function getCursorPos() {"\r
+                               +"var cursorPos;"\r
+                               +"if (window.getSelection) {"\r
+                               +"   var selObj = window.getSelection();"\r
+                               +"   var selRange = selObj.getRangeAt(0);"\r
+                               +"   var workingNode = window.getSelection().anchorNode.parentNode;"\r
+                               +"   while(workingNode != null) { " \r
+                               +"      if (workingNode.nodeName.toLowerCase()=='a') { "\r
+                               +"         workingNode.removeAttribute('href');"\r
+                               +"         workingNode.removeAttribute('title');"\r
+                               +"         var text = document.createTextNode(workingNode.innerText);"\r
+                               +"         workingNode.parentNode.insertBefore(text, workingNode);"\r
+                               +"         workingNode.parentNode.removeChild(workingNode);"\r
+                               +"      }"\r
+                               +"      workingNode = workingNode.parentNode;"\r
+                               +"   }"\r
+                               +"}"\r
+                               +"} getCursorPos();");\r
+                       browser.page().mainFrame().evaluateJavaScript(js);\r
+                       \r
+                       contentChanged();\r
+\r
+               \r
        }\r
        \r
+       \r
        // Insert a table\r
        public void insertTable() {\r
                TableDialog dialog = new TableDialog();\r
@@ -2331,6 +2366,7 @@ public class BrowserWindow extends QWidget {
                browser.insertTableRowAction.setEnabled(false);\r
                browser.deleteTableRowAction.setEnabled(false);\r
                browser.insertLinkAction.setText(tr("Insert Hyperlink"));\r
+               insertHyperlink = true;\r
                currentHyperlink ="";\r
                insideList = false;\r
                forceTextPaste = false;\r
@@ -2426,6 +2462,7 @@ public class BrowserWindow extends QWidget {
        public void setInsideLink(String link) {\r
                browser.insertLinkAction.setText(tr("Edit Hyperlink"));\r
                currentHyperlink = link;\r
+               insertHyperlink = false;\r
        }\r
        \r
        public void italicActive() {\r