comparison browser_side/richtext.py @ 353:ddb909ab5cbc

browser_side: wysiwyg edtion, first steps (do not use the buttons yet)
author souliane <souliane@mailoo.org>
date Thu, 13 Feb 2014 01:33:24 +0100
parents 2610443b05a2
children 9bb78c09e9fc
comparison
equal deleted inserted replaced
352:2610443b05a2 353:ddb909ab5cbc
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from pyjamas.ui.TextArea import TextArea 20 from pyjamas.ui.TextArea import TextArea
21 from pyjamas.ui.Button import Button 21 from pyjamas.ui.Button import Button
22 from pyjamas.ui.CheckBox import CheckBox
22 from pyjamas.ui.DialogBox import DialogBox 23 from pyjamas.ui.DialogBox import DialogBox
23 from pyjamas.ui.Label import Label 24 from pyjamas.ui.Label import Label
24 from pyjamas.ui.HTML import HTML 25 from pyjamas.ui.HTML import HTML
25 from pyjamas.ui.FlexTable import FlexTable 26 from pyjamas.ui.FlexTable import FlexTable
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel 27 from pyjamas.ui.HorizontalPanel import HorizontalPanel
138 for key in composition.RICH_SYNTAXES[syntax].keys(): 139 for key in composition.RICH_SYNTAXES[syntax].keys():
139 self.addToolbarButton(syntax, key) 140 self.addToolbarButton(syntax, key)
140 label = Label(_("Syntax: %s") % syntax) 141 label = Label(_("Syntax: %s") % syntax)
141 label.addStyleName("richTextSyntaxLabel") 142 label.addStyleName("richTextSyntaxLabel")
142 self.toolbar.add(label) 143 self.toolbar.add(label)
144 self.wysiwyg_button = CheckBox('Wysiwyg edition')
145 wysiywgCb = lambda sender: self.setWysiwyg(sender.getChecked())
146 self.wysiwyg_button.addClickListener(wysiywgCb)
147 self.toolbar.add(self.wysiwyg_button)
143 self.getFlexCellFormatter().setColSpan(self.toolbar_offset + count, 0, 2) 148 self.getFlexCellFormatter().setColSpan(self.toolbar_offset + count, 0, 2)
144 self.setWidget(self.toolbar_offset + count, 0, self.toolbar) 149 self.setWidget(self.toolbar_offset + count, 0, self.toolbar)
145 count += 1 150 count += 1
151
152 def setWysiwyg(self, wysiwyg, init=False):
153 """Toggle the edition mode between rich content syntax and wysiwyg.
154 @param wysiwyg: boolean value
155 @param init: set to True to re-init without switching the widgets."""
156 def setWysiwyg():
157 self.wysiwyg = wysiwyg
158 if hasattr(self, 'wysiwyg_button'):
159 self.wysiwyg_button.setChecked(wysiwyg)
160 if not wysiwyg:
161 self.display.removeStyleName('richTextWysiwyg')
162
163 if init:
164 setWysiwyg()
165 return
166
167 self.getFlexCellFormatter().setColSpan(self.content_offset, 0, 2)
168 if wysiwyg:
169 def syntaxConvertCb(text):
170 self.display.setContent({'text': text})
171 self.textarea.removeFromParent() # XXX: force as it is not always done...
172 self.setWidget(self.content_offset, 0, self.display)
173 self.display.addStyleName('richTextWysiwyg')
174 self.display.edit(True)
175 content = self.getContent()
176 if content['text'] and content['syntax'] != Const.SYNTAX_XHTML:
177 self.host.bridge.call('syntaxConvert', syntaxConvertCb, content['text'], content['syntax'], Const.SYNTAX_XHTML)
178 else:
179 syntaxConvertCb(content['text'])
180 else:
181 syntaxConvertCb = lambda text: self.textarea.setText(text)
182 text = self.display.getContent()['text']
183 if text and self.toolbar.syntax != Const.SYNTAX_XHTML:
184 self.host.bridge.call('syntaxConvert', syntaxConvertCb, text)
185 else:
186 syntaxConvertCb(text)
187 self.setWidget(self.content_offset, 0, self.textarea)
188 self.textarea.setWidth('100%') # CSS width doesn't do it, don't know why
189
190 setWysiwyg() # do it in the end because it affects self.getContent
146 191
147 def addToolbarButton(self, syntax, key): 192 def addToolbarButton(self, syntax, key):
148 """Add a button with the defined parameters.""" 193 """Add a button with the defined parameters."""
149 button = Button('<img src="%s" class="richTextIcon" />' % 194 button = Button('<img src="%s" class="richTextIcon" />' %
150 composition.RICH_BUTTONS[key]["icon"]) 195 composition.RICH_BUTTONS[key]["icon"])
173 button.addClickListener(button_callback) 218 button.addClickListener(button_callback)
174 219
175 def getContent(self): 220 def getContent(self):
176 assert(hasattr(self, 'textarea')) 221 assert(hasattr(self, 'textarea'))
177 assert(hasattr(self, 'toolbar')) 222 assert(hasattr(self, 'toolbar'))
178 content = {'text': self.strproc(self.textarea.getText()), 'syntax': self.toolbar.syntax} 223 if self.wysiwyg:
224 content = {'text': self.display.getContent()['text'], 'syntax': Const.SYNTAX_XHTML}
225 else:
226 content = {'text': self.strproc(self.textarea.getText()), 'syntax': self.toolbar.syntax}
179 if hasattr(self, 'title_panel'): 227 if hasattr(self, 'title_panel'):
180 content.update({'title': self.strproc(self.title_panel.getText())}) 228 content.update({'title': self.strproc(self.title_panel.getText())})
181 return content 229 return content
182 230
183 def edit(self, edit=False, abort=False, sync=False): 231 def edit(self, edit=False, abort=False, sync=False):
189 confirmation. When edit is False and abort is True, abortion is actually done. 237 confirmation. When edit is False and abort is True, abortion is actually done.
190 @param sync: set to True to cancel the edition after the content has been saved somewhere else 238 @param sync: set to True to cancel the edition after the content has been saved somewhere else
191 """ 239 """
192 self.refresh(edit) 240 self.refresh(edit)
193 BaseTextEditor.edit(self, edit, abort, sync) 241 BaseTextEditor.edit(self, edit, abort, sync)
194 if (edit and abort) or sync: 242 if (edit and abort):
195 return 243 return
196 244 self.setWysiwyg(False, init=True) # /!\ it affects self.getContent
245 if sync:
246 return
197 # the following must NOT be done at each UI refresh! 247 # the following must NOT be done at each UI refresh!
198 content = self._original_content 248 content = self._original_content
199 if edit: 249 if edit:
200 def getParamCb(syntax): 250 def getParamCb(syntax):
201 # set the editable text in the current user-selected syntax 251 # set the editable text in the current user-selected syntax