comparison 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
comparison
equal deleted inserted replaced
354:c417a7acfe44 355:9bb78c09e9fc
23 from pyjamas.ui.DialogBox import DialogBox 23 from pyjamas.ui.DialogBox import DialogBox
24 from pyjamas.ui.Label import Label 24 from pyjamas.ui.Label import Label
25 from pyjamas.ui.HTML import HTML 25 from pyjamas.ui.HTML import HTML
26 from pyjamas.ui.FlexTable import FlexTable 26 from pyjamas.ui.FlexTable import FlexTable
27 from pyjamas.ui.HorizontalPanel import HorizontalPanel 27 from pyjamas.ui.HorizontalPanel import HorizontalPanel
28 from pyjamas import Window
29 from __pyjamas__ import doc
28 30
29 from constants import Const 31 from constants import Const
30 from dialog import ConfirmDialog, InfoDialog 32 from dialog import ConfirmDialog, InfoDialog
31 from base_panels import TitlePanel, BaseTextEditor, LightTextEditor 33 from base_panels import TitlePanel, BaseTextEditor, LightTextEditor
32 from list_manager import ListManager 34 from list_manager import ListManager
195 composition.RICH_BUTTONS[key]["icon"]) 197 composition.RICH_BUTTONS[key]["icon"])
196 button.setTitle(composition.RICH_BUTTONS[key]["tip"]) 198 button.setTitle(composition.RICH_BUTTONS[key]["tip"])
197 button.addStyleName('richTextToolButton') 199 button.addStyleName('richTextToolButton')
198 self.toolbar.add(button) 200 self.toolbar.add(button)
199 201
200 def button_callback(): 202 def buttonCb():
201 """Generic callback for a toolbar button.""" 203 """Generic callback for a toolbar button."""
202 text = self.textarea.getText() 204 text = self.textarea.getText()
203 cursor_pos = self.textarea.getCursorPos() 205 cursor_pos = self.textarea.getCursorPos()
204 selection_length = self.textarea.getSelectionLength() 206 selection_length = self.textarea.getSelectionLength()
205 infos = composition.RICH_SYNTAXES[syntax][key] 207 data = composition.RICH_SYNTAXES[syntax][key]
206 if selection_length == 0: 208 if selection_length == 0:
207 middle_text = infos[1] 209 middle_text = data[1]
208 else: 210 else:
209 middle_text = text[cursor_pos:cursor_pos + selection_length] 211 middle_text = text[cursor_pos:cursor_pos + selection_length]
210 self.textarea.setText(text[:cursor_pos] 212 self.textarea.setText(text[:cursor_pos]
211 + infos[0] 213 + data[0]
212 + middle_text 214 + middle_text
213 + infos[2] 215 + data[2]
214 + text[cursor_pos + selection_length:]) 216 + text[cursor_pos + selection_length:])
215 self.textarea.setCursorPos(cursor_pos + len(infos[0]) + len(middle_text)) 217 self.textarea.setCursorPos(cursor_pos + len(data[0]) + len(middle_text))
216 self.textarea.setFocus(True) 218 self.textarea.setFocus(True)
217 219
218 button.addClickListener(button_callback) 220 def wysiwygCb():
221 """Callback for a toolbar button while wysiwyg mode is enabled."""
222 data = composition.COMMANDS[key]
223
224 def execCommand(command, arg):
225 self.display.setFocus(True)
226 doc().execCommand(command, False, arg.strip() if arg else '')
227 # use Window.prompt instead of dialog.PromptDialog to not loose the focus
228 prompt = lambda command, text: execCommand(command, Window.prompt(text))
229 if isinstance(data, tuple) or isinstance(data, list):
230 if data[1]:
231 prompt(data[0], data[1])
232 else:
233 execCommand(data[0], data[2])
234 else:
235 execCommand(data, False, '')
236
237 button.addClickListener(lambda: wysiwygCb() if self.wysiwyg else buttonCb())
219 238
220 def getContent(self): 239 def getContent(self):
221 assert(hasattr(self, 'textarea')) 240 assert(hasattr(self, 'textarea'))
222 assert(hasattr(self, 'toolbar')) 241 assert(hasattr(self, 'toolbar'))
223 if self.wysiwyg: 242 if self.wysiwyg: