Mercurial > libervia-web
diff browser_side/base_panels.py @ 401:ea03f898067f
browser_side: LightTextEditor renamed to HTMLTextEditor, new class LightTextEditor based on TextArea
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 13 Mar 2014 13:15:48 +0100 |
parents | 487dd238ab88 |
children | d7e78cb78dfc |
line wrap: on
line diff
--- 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 <enter> or <escape> 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)