[IMP] web_widget_text_markdown: black, isort, prettier

pull/2136/head
Olga Marco 2022-02-02 09:34:43 +01:00
parent 1f4a981159
commit ef99af6cf1
9 changed files with 192 additions and 176 deletions

View File

@ -0,0 +1 @@
../../../../web_widget_text_markdown

View File

@ -0,0 +1,2 @@
[bdist_wheel]
universal=1

View File

@ -0,0 +1,6 @@
import setuptools
setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

View File

@ -3,29 +3,21 @@
# #
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{ {
'name': 'Web Widget Text Markdown', "name": "Web Widget Text Markdown",
'version': '11.0.1.0.0', "version": "11.0.1.0.0",
"author": "Alexandre Díaz, " "author": "Alexandre Díaz, "
"Komit, " "Komit, "
"Sudokeys, " "Sudokeys, "
"Odoo Community Association (OCA)", "Odoo Community Association (OCA)",
'category': 'Web', "category": "Web",
'license': 'AGPL-3', "license": "AGPL-3",
'website': 'https://github.com/OCA/web', "website": "https://github.com/OCA/web",
'summary': 'Widget to text fields that adds markdown support', "summary": "Widget to text fields that adds markdown support",
'depends': [ "depends": ["web"],
'web' "demo": ["demo/bootstrap_markdown.xml"],
], "data": ["views/assets.xml"],
'demo': [ "qweb": ["static/src/xml/bootstrap_markdown.xml"],
"demo/bootstrap_markdown.xml", "installable": True,
], "auto_install": False,
'data': [ "application": False,
'views/assets.xml',
],
"qweb": [
"static/src/xml/bootstrap_markdown.xml",
],
'installable': True,
'auto_install': False,
'application': False
} }

View File

@ -4,29 +4,26 @@
* Copyright 2019 Alexandre Díaz - <dev@redneboa.es> * Copyright 2019 Alexandre Díaz - <dev@redneboa.es>
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define("web_widget_text_markdown.FieldTextMarkDown", function(require) { odoo.define("web_widget_text_markdown.FieldTextMarkDown", function(require) {
'use strict'; "use strict";
var basic_fields = require('web.basic_fields'); var basic_fields = require("web.basic_fields");
var field_registry = require('web.field_registry'); var field_registry = require("web.field_registry");
var core = require('web.core'); var core = require("web.core");
var _t = core._t; var _t = core._t;
var LIBS_PATH = '/web_widget_text_markdown/static/src/lib/'; var LIBS_PATH = "/web_widget_text_markdown/static/src/lib/";
var FieldTextMarkDown = basic_fields.FieldText.extend({ var FieldTextMarkDown = basic_fields.FieldText.extend({
className: [ className: [
basic_fields.FieldText.prototype.className, basic_fields.FieldText.prototype.className,
'o_field_text_markdown', "o_field_text_markdown",
].join(' '), ].join(" "),
jsLibs: [ jsLibs: [
LIBS_PATH + 'marked.js', LIBS_PATH + "marked.js",
LIBS_PATH + 'dropzone.js', LIBS_PATH + "dropzone.js",
LIBS_PATH + 'bootstrap-markdown.js', LIBS_PATH + "bootstrap-markdown.js",
],
cssLibs: [
LIBS_PATH + 'bootstrap-markdown.min.css',
], ],
cssLibs: [LIBS_PATH + "bootstrap-markdown.min.css"],
_getValue: function() { _getValue: function() {
return this.$markdown.getContent(); return this.$markdown.getContent();
@ -34,14 +31,18 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
_prepareInput: function() { _prepareInput: function() {
var $input = this._super.apply(this, arguments); var $input = this._super.apply(this, arguments);
_.defer(function ($elm) { _.defer(
function($elm) {
$input.removeClass(this.className); $input.removeClass(this.className);
$input.wrap( $input.wrap(
_.str.sprintf("<div class='%s'></div>", this.className)); _.str.sprintf("<div class='%s'></div>", this.className)
);
$elm.markdown(this._getMarkdownOptions()); $elm.markdown(this._getMarkdownOptions());
this.$markdown = $elm.data("markdown"); this.$markdown = $elm.data("markdown");
this.$markdown.setContent(this.value || ""); this.$markdown.setContent(this.value || "");
}.bind(this), $input); }.bind(this),
$input
);
return $input; return $input;
}, },
@ -49,7 +50,6 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
this.$el.html(marked(this._formatValue(this.value))); this.$el.html(marked(this._formatValue(this.value)));
}, },
_getMarkdownOptions: function() { _getMarkdownOptions: function() {
var markdownOpts = { var markdownOpts = {
autofocus: false, autofocus: false,
@ -61,14 +61,14 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
if (this.res_id) { if (this.res_id) {
var self = this; var self = this;
markdownOpts.dropZoneOptions = { markdownOpts.dropZoneOptions = {
paramName: 'ufile', paramName: "ufile",
url: '/web/binary/upload_attachment', url: "/web/binary/upload_attachment",
acceptedFiles: 'image/*', acceptedFiles: "image/*",
width: 'o_field_text_markdown', width: "o_field_text_markdown",
params: { params: {
csrf_token: core.csrf_token, csrf_token: core.csrf_token,
session_id: this.getSession().override_session, session_id: this.getSession().override_session,
callback: '', callback: "",
model: this.model, model: this.model,
id: this.res_id, id: this.res_id,
}, },
@ -85,15 +85,19 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
if (_t.database.multi_lang && this.field.translate) { if (_t.database.multi_lang && this.field.translate) {
markdownOpts.additionalButtons = [ markdownOpts.additionalButtons = [
[{ [
name: 'oTranslate', {
data: [{ name: "oTranslate",
name: 'cmdTranslate', data: [
title: _t('Translate'), {
icon: {glyph: 'glyphicon glyphicon-flag'}, name: "cmdTranslate",
title: _t("Translate"),
icon: {glyph: "glyphicon glyphicon-flag"},
callback: this._markdownTranslate, callback: this._markdownTranslate,
}], },
}], ],
},
],
]; ];
} }
} }
@ -113,26 +117,28 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
var self = this; var self = this;
var caretPos = 0; var caretPos = 0;
var $textarea = null; var $textarea = null;
markdown.on('drop', function (e) { markdown.on("drop", function(e) {
$textarea = $(e.target); $textarea = $(e.target);
caretPos = $textarea.prop('selectionStart'); caretPos = $textarea.prop("selectionStart");
}); });
markdown.on('success', function (file, response) { markdown.on("success", function(file, response) {
var text = $textarea.val(); var text = $textarea.val();
var attachment_id = self._getAttachmentId(response); var attachment_id = self._getAttachmentId(response);
if (attachment_id) { if (attachment_id) {
var ftext = text.substring(0, caretPos) + '\n![' + var ftext =
_t('description') + text.substring(0, caretPos) +
'](/web/image/' + attachment_id + ')\n' + "\n![" +
_t("description") +
"](/web/image/" +
attachment_id +
")\n" +
text.substring(caretPos); text.substring(caretPos);
$textarea.val(ftext); $textarea.val(ftext);
} else { } else {
self.do_warn( self.do_warn(_t("Error"), _t("Can't create the attachment."));
_t('Error'),
_t("Can't create the attachment."));
} }
}); });
markdown.on('error', function (file, error) { markdown.on("error", function(file, error) {
console.warn(error); console.warn(error);
}); });
}, },
@ -140,13 +146,11 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
_markdownDropZoneUploadSuccess: function() { _markdownDropZoneUploadSuccess: function() {
this.isDirty = true; this.isDirty = true;
this._doDebouncedAction(); this._doDebouncedAction();
this.$markdown.$editor.find(".dz-error-mark:last") this.$markdown.$editor.find(".dz-error-mark:last").css("display", "none");
.css("display", "none");
}, },
_markdownDropZoneUploadError: function() { _markdownDropZoneUploadError: function() {
this.$markdown.$editor.find(".dz-success-mark:last") this.$markdown.$editor.find(".dz-success-mark:last").css("display", "none");
.css("display", "none");
}, },
_markdownTranslate: function() { _markdownTranslate: function() {
@ -154,7 +158,6 @@ odoo.define("web_widget_text_markdown.FieldTextMarkDown", function (require) {
}, },
}); });
field_registry.add("bootstrap_markdown", FieldTextMarkDown);
field_registry.add('bootstrap_markdown', FieldTextMarkDown);
return FieldTextMarkDown; return FieldTextMarkDown;
}); });

View File

@ -2,33 +2,35 @@
/* Copyright 2019 Alexandre Díaz - <dev@redneboa.es> /* Copyright 2019 Alexandre Díaz - <dev@redneboa.es>
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('web_widget_text_markdown.test', function (require) { odoo.define("web_widget_text_markdown.test", function(require) {
'use strict'; "use strict";
var FormView = require('web.FormView'); var FormView = require("web.FormView");
var testUtils = require('web.test_utils'); var testUtils = require("web.test_utils");
var createAsyncView = testUtils.createAsyncView; var createAsyncView = testUtils.createAsyncView;
QUnit.module(
QUnit.module('web_widget_text_markdown', { "web_widget_text_markdown",
{
beforeEach: function() { beforeEach: function() {
this.data = { this.data = {
partner: { partner: {
fields: {comment: {string: "Comment", type: "text"}}, fields: {comment: {string: "Comment", type: "text"}},
records: [ records: [{id: 1, comment: "This is a test\n**Hello**"}],
{id: 1, comment: "This is a test\n**Hello**"},
],
}, },
}; };
this.arch = '<form><sheet>' + this.arch =
"<form><sheet>" +
'<field name="comment"' + '<field name="comment"' +
' widget="bootstrap_markdown" />' + ' widget="bootstrap_markdown" />' +
'</sheet></form>'; "</sheet></form>";
}, },
}, function () { },
QUnit.module('FieldTextMarkDown'); function() {
QUnit.test('bootstrap markdown widget are correctly rendered (preview)', QUnit.module("FieldTextMarkDown");
QUnit.test(
"bootstrap markdown widget are correctly rendered (preview)",
function(assert) { function(assert) {
assert.expect(1); assert.expect(1);
@ -36,20 +38,21 @@ odoo.define('web_widget_text_markdown.test', function (require) {
createAsyncView({ createAsyncView({
View: FormView, View: FormView,
model: 'partner', model: "partner",
data: this.data, data: this.data,
arch: this.arch, arch: this.arch,
res_id: 1, res_id: 1,
}).then(function(form) { }).then(function(form) {
_.defer(function() { _.defer(function() {
assert.strictEqual(form.$('.md-editor').length, 0); assert.strictEqual(form.$(".md-editor").length, 0);
form.destroy(); form.destroy();
done(); done();
}); });
}); });
} }
); );
QUnit.test('bootstrap markdown widget are correctly rendered (edit)', QUnit.test(
"bootstrap markdown widget are correctly rendered (edit)",
function(assert) { function(assert) {
assert.expect(1); assert.expect(1);
@ -57,20 +60,20 @@ odoo.define('web_widget_text_markdown.test', function (require) {
createAsyncView({ createAsyncView({
View: FormView, View: FormView,
model: 'partner', model: "partner",
data: this.data, data: this.data,
arch: this.arch, arch: this.arch,
res_id: 1, res_id: 1,
viewOptions: {mode: 'edit'}, viewOptions: {mode: "edit"},
}).then(function(form) { }).then(function(form) {
_.defer(function() { _.defer(function() {
assert.strictEqual(form.$('.md-editor').length, 1); assert.strictEqual(form.$(".md-editor").length, 1);
form.destroy(); form.destroy();
done(); done();
}); });
}); });
} }
); );
}); }
);
}); });

View File

@ -4,17 +4,26 @@
* Copyright 2019 Alexandre Díaz - <dev@redneboa.es> * Copyright 2019 Alexandre Díaz - <dev@redneboa.es>
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).--> * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).-->
<odoo> <odoo>
<template id="assets_backend" name="web_widget_text_markdown_assets" inherit_id="web.assets_backend"> <template
id="assets_backend"
name="web_widget_text_markdown_assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<link rel='stylesheet' <link
href='/web_widget_text_markdown/static/src/less/web_widget_text_markdown.less'/> rel='stylesheet'
<script type="text/javascript" href='/web_widget_text_markdown/static/src/less/web_widget_text_markdown.less'
src="/web_widget_text_markdown/static/src/js/web_widget_text_markdown.js"/> />
<script
type="text/javascript"
src="/web_widget_text_markdown/static/src/js/web_widget_text_markdown.js"
/>
</xpath> </xpath>
</template> </template>
<template id="qunit_suite" inherit_id="web.qunit_suite"> <template id="qunit_suite" inherit_id="web.qunit_suite">
<xpath expr="//t[@t-set='head']" position="inside"> <xpath expr="//t[@t-set='head']" position="inside">
<script type="application/javascript" <script
type="application/javascript"
src="/web_widget_text_markdown/static/tests/js/web_widget_text_markdown.js" src="/web_widget_text_markdown/static/tests/js/web_widget_text_markdown.js"
/> />
</xpath> </xpath>