# HG changeset patch # User souliane # Date 1394712948 -3600 # Node ID ea03f898067fcb3aeece52f07517818a04349026 # Parent 487dd238ab884b51e855c14d064277c53f9813d7 browser_side: LightTextEditor renamed to HTMLTextEditor, new class LightTextEditor based on TextArea diff -r 487dd238ab88 -r ea03f898067f browser_side/base_panels.py --- a/browser_side/base_panels.py Tue Mar 11 13:53:19 2014 +0100 +++ b/browser_side/base_panels.py Thu Mar 13 13:15:48 2014 +0100 @@ -24,6 +24,7 @@ from pyjamas.ui.HTMLPanel import HTMLPanel from pyjamas.ui.Button import Button from pyjamas.ui.HTML import HTML +from pyjamas.ui.SimplePanel import SimplePanel from pyjamas.ui.PopupPanel import PopupPanel from pyjamas.ui.StackPanel import StackPanel from pyjamas.ui.TextArea import TextArea @@ -435,8 +436,8 @@ self.edit_listeners.append(listener) -class LightTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler): - """Manage a simple text editor whith the HTML 5 "contenteditable" property.""" +class HTMLTextEditor(BaseTextEditor, HTML, FocusHandler, KeyboardHandler): + """Manage a simple text editor with the HTML 5 "contenteditable" property.""" def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None): """ @@ -509,3 +510,78 @@ def onLostFocus(self, sender): """Finish the edition when focus is lost""" self.edit(False) + + +class LightTextEditor(BaseTextEditor, SimplePanel, FocusHandler, KeyboardHandler): + """Manage a simple text editor with a TextArea for editing, HTML for display.""" + + def __init__(self, content=None, modifiedCb=None, afterEditCb=None, single_line=False, enhance_display=True, listen_focus=None): + """ + @param content + @param modifiedCb + @param afterEditCb + @param single_line: set to True to manage a single line editor. In that + case the edition will be terminated when or is pressed. + @param listen_focus: set to True to terminate the edition when the + focus is lost. Leave to None in order to use single_line's value. + @param enhance_display: if True, the display text will be enhanced with addURLToText + """ + SimplePanel.__init__(self) + self.__single_line = single_line + self.__enhance_display = enhance_display + self.__listen_focus = single_line if listen_focus is None else listen_focus + if self.__listen_focus: + FocusHandler.__init__(self) + KeyboardHandler.__init__(self) + strproc = lambda text: html_sanitize(html_strip(text)) if self.__single_line else html_strip(text) + BaseTextEditor.__init__(self, content, strproc, modifiedCb, afterEditCb) + self.textarea = TextArea() + self.display = HTML() + + def setContent(self, content=None): + BaseTextEditor.setContent(self, content) + + def getContent(self): + text = self.textarea.getText() + return {'text': self.strproc(text) if text else ''} + + def edit(self, edit, abort=False, sync=False): + if edit: + self.textarea.setText(self._original_content['text']) + self.setWidget(self.textarea if edit else self.display) + BaseTextEditor.edit(self, edit) + if edit: + if self.__listen_focus: + self.textarea.addFocusListener(self) + self.textarea.addKeyboardListener(self) + else: + self.setDisplayContent() + if self.__listen_focus: + if self in self._focusListeners: + self.textarea.removeFocusListener(self) + if self in self._keyboardListeners: + self.textarea.removeKeyboardListener(self) + + def setDisplayContent(self): + text = addURLToImage(self._original_content['text']) + self.display.setHTML(addURLToText(text) if self.__enhance_display else text) + + def setFocus(self, focus): + self.textarea.setFocus(focus) + + def onKeyDown(self, sender, keycode, modifiers): + for listener in self.edit_listeners: + listener(self.textarea, keycode) + + def onKeyPress(self, sender, keycode, modifiers): + if not self.__single_line: + return + # XXX: it seems that pyjamas never catches the escape key + if keycode in (KEY_ENTER, KEY_ESCAPE): # finish the edition + self.textarea.setFocus(False) + if not self.__listen_focus: + self.edit(False) + + def onLostFocus(self, sender): + """Finish the edition when focus is lost""" + self.edit(False) diff -r 487dd238ab88 -r ea03f898067f browser_side/panels.py --- a/browser_side/panels.py Tue Mar 11 13:53:19 2014 +0100 +++ b/browser_side/panels.py Thu Mar 13 13:15:48 2014 +0100 @@ -44,7 +44,7 @@ from jid import JID from html_tools import html_sanitize -from base_panels import ChatText, OccupantsList, PopupMenuPanel, BaseTextEditor, LightTextEditor +from base_panels import ChatText, OccupantsList, PopupMenuPanel, BaseTextEditor, LightTextEditor, HTMLTextEditor from card_game import CardPanel from radiocol import RadioColPanel from menu import Menu @@ -944,14 +944,14 @@ return False -class StatusPanel(LightTextEditor, ClickHandler): +class StatusPanel(HTMLTextEditor, ClickHandler): EMPTY_STATUS = '<click to set a status>' def __init__(self, host, status=''): self.host = host modifiedCb = lambda content: self.host.bridge.call('setStatus', None, self.host.status_panel.presence, content['text']) or True - LightTextEditor.__init__(self, {'text': status}, modifiedCb, None, single_line=True) + HTMLTextEditor.__init__(self, {'text': status}, modifiedCb, None, single_line=True) self.edit(False) self.setStyleName('statusPanel') ClickHandler.__init__(self) @@ -968,7 +968,7 @@ return content def getContent(self): - return self.__cleanContent(LightTextEditor.getContent(self)) + return self.__cleanContent(HTMLTextEditor.getContent(self)) def setContent(self, content): content = self.__cleanContent(content) diff -r 487dd238ab88 -r ea03f898067f browser_side/richtext.py --- a/browser_side/richtext.py Tue Mar 11 13:53:19 2014 +0100 +++ b/browser_side/richtext.py Thu Mar 13 13:15:48 2014 +0100 @@ -31,7 +31,7 @@ from constants import Const from dialog import ConfirmDialog, InfoDialog -from base_panels import TitlePanel, BaseTextEditor, LightTextEditor +from base_panels import TitlePanel, BaseTextEditor, HTMLTextEditor from list_manager import ListManager from html_tools import html_sanitize import panels @@ -107,7 +107,7 @@ if hasattr(self, 'toolbar'): self.toolbar.setVisible(False) if not hasattr(self, 'display'): - self.display = LightTextEditor(enhance_display=False) # for display mode + self.display = HTMLTextEditor(enhance_display=False) # for display mode for listener in self.edit_listeners: self.display.addEditListener(listener) if not self.read_only and not hasattr(self, 'textarea'): @@ -316,7 +316,7 @@ self.display.edit(False) def setDisplayContent(self): - """Set the content of the LightTextEditor which is used for display/wysiwyg""" + """Set the content of the HTMLTextEditor which is used for display/wysiwyg""" content = self._original_content text = content['text'] if 'title' in content and content['title']: