diff browser_side/richtext.py @ 355:9bb78c09e9fc

browser_side: wysiwyg edition handles the toolbar buttons
author souliane <souliane@mailoo.org>
date Thu, 13 Feb 2014 10:32:26 +0100
parents ddb909ab5cbc
children b95099a1d11b
line wrap: on
line diff
--- a/browser_side/richtext.py	Thu Feb 13 10:30:38 2014 +0100
+++ b/browser_side/richtext.py	Thu Feb 13 10:32:26 2014 +0100
@@ -25,6 +25,8 @@
 from pyjamas.ui.HTML import HTML
 from pyjamas.ui.FlexTable import FlexTable
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
+from pyjamas import Window
+from __pyjamas__ import doc
 
 from constants import Const
 from dialog import ConfirmDialog, InfoDialog
@@ -197,25 +199,42 @@
         button.addStyleName('richTextToolButton')
         self.toolbar.add(button)
 
-        def button_callback():
+        def buttonCb():
             """Generic callback for a toolbar button."""
             text = self.textarea.getText()
             cursor_pos = self.textarea.getCursorPos()
             selection_length = self.textarea.getSelectionLength()
-            infos = composition.RICH_SYNTAXES[syntax][key]
+            data = composition.RICH_SYNTAXES[syntax][key]
             if selection_length == 0:
-                middle_text = infos[1]
+                middle_text = data[1]
             else:
                 middle_text = text[cursor_pos:cursor_pos + selection_length]
             self.textarea.setText(text[:cursor_pos]
-                                  + infos[0]
+                                  + data[0]
                                   + middle_text
-                                  + infos[2]
+                                  + data[2]
                                   + text[cursor_pos + selection_length:])
-            self.textarea.setCursorPos(cursor_pos + len(infos[0]) + len(middle_text))
+            self.textarea.setCursorPos(cursor_pos + len(data[0]) + len(middle_text))
             self.textarea.setFocus(True)
 
-        button.addClickListener(button_callback)
+        def wysiwygCb():
+            """Callback for a toolbar button while wysiwyg mode is enabled."""
+            data = composition.COMMANDS[key]
+
+            def execCommand(command, arg):
+                self.display.setFocus(True)
+                doc().execCommand(command, False, arg.strip() if arg else '')
+            # use Window.prompt instead of dialog.PromptDialog to not loose the focus
+            prompt = lambda command, text: execCommand(command, Window.prompt(text))
+            if isinstance(data, tuple) or isinstance(data, list):
+                if data[1]:
+                    prompt(data[0], data[1])
+                else:
+                    execCommand(data[0], data[2])
+            else:
+                execCommand(data, False, '')
+
+        button.addClickListener(lambda: wysiwygCb() if self.wysiwyg else buttonCb())
 
     def getContent(self):
         assert(hasattr(self, 'textarea'))