Skip to content

Commit

Permalink
Quill editor (#6159)
Browse files Browse the repository at this point in the history
* feat(Text Editor): Quill Editor

* fix: Add imageDrop module

- prevent default events

* refactor(Comment): Comment is now frappe control

- Use quill's bubble theme for comment control

* feat: Support @mentions in comment area

- Uses quill-mention package

* fix: Use setContents to fix autofocus bug

* fix: Spaces to Tabs

* fix: Missing semicolon

* fix: Fix style

* fix: Remove all of summernote

- Remove comment.js (use fieldtype: 'Comment')
- Add quill styles to webform.css

* fix: Replace color/background with indent/outdent
  • Loading branch information
netchampfaris authored and rmehta committed Sep 30, 2018
1 parent 8acdc06 commit 1053427
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 760 deletions.
6 changes: 2 additions & 4 deletions frappe/public/build.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"public/js/frappe/form/controls/text.js",
"public/js/frappe/form/controls/code.js",
"public/js/frappe/form/controls/text_editor.js",
"public/js/frappe/form/controls/comment.js",
"public/js/frappe/form/controls/check.js",
"public/js/frappe/form/controls/image.js",
"public/js/frappe/form/controls/attach.js",
Expand Down Expand Up @@ -91,7 +92,6 @@
"public/js/frappe/ui/dialog.js"
],
"css/desk.min.css": [
"public/js/lib/summernote/summernote.css",
"public/js/lib/leaflet/leaflet.css",
"public/js/lib/leaflet/leaflet.draw.css",
"public/js/lib/leaflet/L.Control.Locate.css",
Expand Down Expand Up @@ -124,7 +124,6 @@
"public/js/lib/awesomplete/awesomplete.min.js",
"public/js/lib/Sortable.min.js",
"public/js/lib/jquery/jquery.hotkeys.js",
"public/js/lib/summernote/summernote.js",
"public/js/lib/bootstrap.min.js",
"node_modules/moment/min/moment-with-locales.min.js",
"node_modules/moment-timezone/builds/moment-timezone-with-data.min.js",
Expand Down Expand Up @@ -352,14 +351,13 @@
"js/web_form.min.js": [
"public/js/frappe/misc/datetime.js",
"website/js/web_form.js",
"public/js/lib/summernote/summernote.js",
"public/js/lib/datepicker/datepicker.min.js",
"public/js/lib/datepicker/datepicker.en.js"
],
"css/web_form.css": [
"public/less/list.less",
"website/css/web_form.css",
"public/js/lib/summernote/summernote.css"
"public/less/quill.less"
],
"js/print_format_v3.min.js": [
"public/js/legacy/layout.js",
Expand Down
124 changes: 124 additions & 0 deletions frappe/public/js/frappe/form/controls/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'quill-mention';

frappe.ui.form.ControlComment = frappe.ui.form.ControlTextEditor.extend({
make_wrapper() {
this.comment_wrapper = !this.no_wrapper ? $(`
<div class="comment-input-wrapper">
<div class="comment-input-header">
<span class="small text-muted">${__("Add a comment")}</span>
<button class="btn btn-default btn-comment btn-xs pull-right">
${__("Comment")}
</button>
</div>
<div class="comment-input-container">
<div class="frappe-control"></div>
<div class="text-muted small">
${__("Ctrl+Enter to add comment")}
</div>
</div>
</div>
`) : $('<div class="frappe-control"></div>');

this.comment_wrapper.appendTo(this.parent);

// wrapper should point to frappe-control
this.$wrapper = !this.no_wrapper
? this.comment_wrapper.find('.frappe-control')
: this.comment_wrapper;

this.wrapper = this.$wrapper;

this.button = this.comment_wrapper.find('.btn-comment');
},

bind_events() {
this._super();

this.button.click(() => {
this.submit();
});

this.$wrapper.on('keydown', e => {
const key = frappe.ui.keys.get_key(e);
if (key === 'ctrl+enter') {
e.preventDefault();
this.submit();
}
});

this.quill.on('text-change', frappe.utils.debounce(() => {
this.update_state();
}, 300));
},

submit() {
this.on_submit && this.on_submit(this.get_value());
},

update_state() {
const value = this.get_value();
if (strip_html(value)) {
this.button.removeClass('btn-default').addClass('btn-primary');
} else {
this.button.addClass('btn-default').removeClass('btn-primary');
}
},

get_quill_options() {
const options = this._super();
return Object.assign(options, {
theme: 'bubble',
modules: Object.assign(options.modules, {
mention: this.get_mention_options()
})
});
},

get_mention_options() {
if (!(this.mentions && this.mentions.length)) {
return null;
}

const at_values = this.mentions.map((value, i) => {
return {
id: i,
value
};
});

return {
allowedChars: /^[A-Za-z0-9_]*$/,
mentionDenotationChars: ["@"],
isolateCharacter: true,
source: function(searchTerm, renderList, mentionChar) {
let values;

if (mentionChar === "@") {
values = at_values;
}

if (searchTerm.length === 0) {
renderList(values, searchTerm);
} else {
const matches = [];
for (let i = 0; i < values.length; i++) {
if (~values[i].value.toLowerCase().indexOf(searchTerm.toLowerCase())) {
matches.push(values[i]);
}
}
renderList(matches, searchTerm);
}
},
};
},

get_toolbar_options() {
return [
['bold', 'italic', 'underline'],
['blockquote', 'code-block'],
['link', 'image'],
[{ 'list': 'ordered' }, { 'list': 'bullet' }],
['clean']
];
},
});

0 comments on commit 1053427

Please sign in to comment.