diff options
| author | Robot Odoo <xmo+roboto@odoo.com> | 2019-01-17 19:17:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-17 19:17:22 +0100 |
| commit | 22efdfbecc1e8c688000be98903a5a355d1ece8a (patch) | |
| tree | e097625171b2b67ed5f5288ced67446608b9f633 /addons/web_editor/controllers | |
| parent | f50fbc6d168f0d17fa1e932b4ecc968a92b02b65 (diff) | |
| parent | eb0ea8efbe932c140302e9f8da33a839bcd73146 (diff) | |
[IMP] web_editor,*: Refactoring the wysiwyg editor and 'html' field
_Task #1878502_
- The refactoring allows for the update of the _Summernote_ library.
- A new structure was created by transforming all the plugins in the library using the odoo inheritance system. Plugins are easier to implement with the `AbstractPlugin` to add Odoo behaviors.
- The cleanup allowed us to remove some of the code that was in `web_editor` but only used in `website` or in `mass_mailing`. Some parts are still in `web_editor` but will be moved at a later time.
- The refactoring also had to be done in order to create a set of unit tests. Each behavior can be tested, including the behaviors performed as a consequence of keyboard interactions (for instance: Enter, Tab...).
- From now on, the methods of the library (in this case _Summernote_) can no longer be called by other modules or files. Only the `wysiwyg` widgets can access it, to simplify the updating process. The `wysiwyg` object serves as an interface. A lot of tests are done, ensuring the consistency of outside behavior.
- Depending on the options the snippets will be loaded or not, the editor will be in an iframe or not... all of this is transparent from the outside.
- Regarding iframes, all controllers related to editing have been removed: the new API no longer needs them. This speeds up loading, eases testing and removes complexity for the same features.
**PUBLIC FEATURES**
There are several public methods on the `Wysiwyg` class:
- `Wysiwyg.prepare (WidgetParent)` which returns a deferred resolved when the library (xml, lazy, assets...) is loaded.
- `Wysiwyg.getRange (DOM)` returns the range (selection in the dom)
- `Wysiwyg.setRange (startNode, startOffset, endNode, endOffset)` which creates a range (selection in the dom)
- `Wysiwyg.setRangeFromNode (DOM, options)` that creates a range from an element (option available to select all, start or end)
A jQuery selector was added: `:o_editable`, which indicates whether the current element is editable. That is, if it is contained in a tag with the attribute `contentEditable = "true"` or in a tag with the class `o_editable`.
Several methods are also present:
- `focusIn`: makes a focus and places the cursor at the beginning of the element
- `focusInEnd`: makes a focus and places the cursor at the end of the element
- `selectContent`: makes a focus and selects the content
**HTML FIELD**
The HTML field can receive different options:
- `style-inline:` {boolean} transforms a class into an inline style when saving and vice versa when reading.
- `no-attachment`: {boolean} prevents the use of attachments (in media dialog)
- `cssEdit`: {xml_id} to use a template containing the css to loaded in an iframe when editing
- `cssReadonly`: {xml_id} to use a template containing the css to load into an iframe when viewing in readonly
- `snippets`: {xml_id} snippets template (can be used with or without `cssEdit`)
- `wrapper`: {template} qweb static template (containing a tag: `id = "wrapper"`) that will include the content during editing (removed on save)
**MASS MAILING**
A widget was created for `mass mailing`. There are now two fields: `body_html` and `body_arch`.
`body_arch` contains the code with the class without conversion into inline style, useful when editing and one with the inline style that is visible in readonly mode and sent by email.
Advantage: no spreading errors, able to update css/theme, able to do more changes when converting to inline style so that a maximum of mail clients have an impeccable rendering.
**REMAINING KNOW ISSUES**
These will be fixed in a subsequent commit.
- A few committed changes made during the development of this refactoring were lost in the process and should be reapplied.
- Some minor (mostly range) issues remain with Firefox.
--
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr
closes odoo/odoo#29775
Diffstat (limited to 'addons/web_editor/controllers')
| -rw-r--r-- | addons/web_editor/controllers/main.py | 171 |
1 files changed, 101 insertions, 70 deletions
diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index 1b3f03966f3..bd267c5f32d 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -20,54 +20,6 @@ logger = logging.getLogger(__name__) class Web_Editor(http.Controller): #------------------------------------------------------ - # Backend snippet - #------------------------------------------------------ - @http.route('/web_editor/snippets', type='json', auth="user") - def snippets(self, **kwargs): - return request.env.ref('web_editor.snippets').render(None) - - #------------------------------------------------------ - # Backend html field - #------------------------------------------------------ - @http.route('/web_editor/field/html', type='http', auth="user") - def FieldTextHtml(self, model=None, res_id=None, field=None, callback=None, **kwargs): - kwargs.update( - model=model, - res_id=res_id, - field=field, - datarecord=json.loads(kwargs['datarecord']), - debug=request.debug) - - for k in kwargs: - if isinstance(kwargs[k], str) and kwargs[k].isdigit(): - kwargs[k] = int(kwargs[k]) - - trans = dict( - lang=kwargs.get('lang', request.env.context.get('lang')), - translatable=kwargs.get('translatable'), - edit_translations=kwargs.get('edit_translations'), - editable=kwargs.get('enable_editor')) - - kwargs.update(trans) - - record = None - if model and kwargs.get('res_id'): - record = request.env[model].with_context(trans).browse(kwargs.get('res_id')) - - kwargs.update(content=record and getattr(record, field) or "") - - return request.render(kwargs.get("template") or "web_editor.FieldTextHtml", kwargs, uid=request.uid) - - #------------------------------------------------------ - # Backend html field in inline mode - #------------------------------------------------------ - @http.route('/web_editor/field/html/inline', type='http', auth="user") - def FieldTextHtmlInline(self, model=None, res_id=None, field=None, callback=None, **kwargs): - kwargs['inline_mode'] = True - kwargs['dont_load_assets'] = not kwargs.get('enable_editor') and not kwargs.get('edit_translations') - return self.FieldTextHtml(model, res_id, field, callback, **kwargs) - - #------------------------------------------------------ # convert font into picture #------------------------------------------------------ @http.route([ @@ -137,6 +89,106 @@ class Web_Editor(http.Controller): return response #------------------------------------------------------ + # Update a checklist in the editor on check/uncheck + #------------------------------------------------------ + @http.route('/web_editor/checklist', type='json', auth='user') + def update_checklist(self, res_model, res_id, filename, checklistId, checked, **kwargs): + record = request.env[res_model].browse(res_id) + value = getattr(record, filename, False) + htmlelem = etree.fromstring("<div>%s</div>" % value, etree.HTMLParser()) + checked = bool(checked) + + li = htmlelem.find(".//li[@id='checklist-id-" + str(checklistId) + "']") + + if not self._update_checklist_recursive(li, checked, children=True, ancestors=True): + return value + + value = etree.tostring(htmlelem[0][0], encoding='utf-8', method='html')[5:-6] + record.write({filename: value}) + + return value + + def _update_checklist_recursive (self, li, checked, children=False, ancestors=False): + if 'checklist-id-' not in li.get('id', ''): + return False + + classname = li.get('class', '') + if ('o_checked' in classname) == checked: + return False + + # check / uncheck + if checked: + classname = '%s o_checked' % classname + else: + classname = re.sub(r"\s?o_checked\s?", '', classname) + li.set('class', classname) + + # propagate to children + if children: + node = li.getnext() + ul = None + if node is not None: + if node.tag == 'ul': + ul = node + if node.tag == 'li' and len(node.getchildren()) == 1 and node.getchildren()[0].tag == 'ul': + ul = node.getchildren()[0] + + if ul is not None: + for child in ul.getchildren(): + if child.tag == 'li': + self._update_checklist_recursive(child, checked, children=True) + + # propagate to ancestors + if ancestors: + allSelected = True + ul = li.getparent() + if ul.tag == 'li': + ul = ul.getparent() + + for child in ul.getchildren(): + if child.tag == 'li' and 'o_checked' not in child.get('class', ''): + allSelected = False + + node = ul.getprevious() + if node is not None and node.tag == 'li': + self._update_checklist_recursive(node, allSelected, ancestors=True) + + return True + + #------------------------------------------------------ + # upload an image as base64 + #------------------------------------------------------ + @http.route('/web_editor/add_image_base64', type='json', auth='user', methods=['POST'], website=True) + def add_image_base64(self, res_model, res_id, image_base64, filename, disable_optimization=None, **kwargs): + data = base64.b64decode(image_base64) + attachment = self._image_to_attachment(res_model, res_id, data, filename, filename, disable_optimization=disable_optimization) + return attachment.read(['name', 'mimetype', 'checksum', 'url', 'res_id', 'res_model', 'access_token'])[0] + + def _image_to_attachment(self, res_model, res_id, data, name, datas_fname, disable_optimization=None): + Attachments = request.env['ir.attachment'] + try: + image = Image.open(io.BytesIO(data)) + w, h = image.size + if w*h > 42e6: # Nokia Lumia 1020 photo resolution + raise ValueError( + u"Image size excessive, uploaded images must be smaller " + u"than 42 million pixel") + if not disable_optimization and image.format in ('PNG', 'JPEG'): + data = tools.image_save_for_web(image) + except IOError: + pass + attachment = Attachments.create({ + 'name': name, + 'datas_fname': datas_fname, + 'datas': base64.b64encode(data), + 'public': res_model == 'ir.ui.view', + 'res_id': res_id, + 'res_model': res_model, + }) + attachment.generate_access_token() + return attachment + + #------------------------------------------------------ # add attachment (images or link) #------------------------------------------------------ @http.route('/web_editor/attachment/add', type='http', auth='user', methods=['POST'], website=True) @@ -175,32 +227,11 @@ class Web_Editor(http.Controller): attachments = request.env['ir.attachment'] for c_file in request.httprequest.files.getlist('upload'): data = c_file.read() - try: - image = Image.open(io.BytesIO(data)) - w, h = image.size - if w*h > 42e6: # Nokia Lumia 1020 photo resolution - raise ValueError( - u"Image size excessive, uploaded images must be smaller " - u"than 42 million pixel") - if not disable_optimization and image.format in ('PNG', 'JPEG'): - data = tools.image_save_for_web(image) - except IOError as e: - pass - name = c_file.filename datas_fname = name if filters: datas_fname = filters + '_' + datas_fname - attachment = Attachments.create({ - 'name': name, - 'datas': base64.b64encode(data), - 'datas_fname': datas_fname, - 'public': res_model == 'ir.ui.view', - 'res_id': res_id, - 'res_model': res_model, - }) - attachment.generate_access_token() - attachments += attachment + attachments += self._image_to_attachment(res_model, res_id, data, name, datas_fname, disable_optimization=disable_optimization) uploads += attachments.read(['name', 'mimetype', 'checksum', 'url', 'res_id', 'res_model', 'access_token']) except Exception as e: logger.exception("Failed to upload image to attachment") |
