forked from tylerecouture/summernote-table-headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummernote-table-headers.js
104 lines (90 loc) · 4.08 KB
/
summernote-table-headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* https://github.com/tylerecouture/summernote-lists */
(function (factory) {
/* global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(window.jQuery);
}
}(function ($) {
// Extends plugins for emoji plugin.
$.extend($.summernote.plugins, {
'tableHeaders': function (context) {
var self = this,
ui = $.summernote.ui,
options = context.options,
$editor = context.layoutInfo.editor,
$editable = context.layoutInfo.editable;
editable = $editable[0];
context.memo('button.tableHeaders', function () {
return ui.buttonGroup([
ui.button({
contents: '<b>H<b>', //ui.icon(options.icons.bold),
tooltip: 'Toggle table header',
click:function (e) {
self.toggleTableHeader();
}
}),
]).render();
});
this.toggleTableHeader = function () {
const rng = context.invoke('createRange', $editable);
const dom = $.summernote.dom;
if (rng.isCollapsed() && rng.isOnCell()) {
context.invoke('beforeCommand');
var table = dom.ancestor(rng.commonAncestor(), dom.isTable)
var $table = $(table);
var $thead = $table.find('thead');
if ($thead[0]) {
// thead found, so convert to a regular row. When a header
// exists and user tries to add a new row below
// the header, Summernote actually adds another tr within the
// thead so need to capture all and move them into tbody
self.observer.disconnect(); // see below
self.replaceTags($thead.find('th'), 'td')
var $theadRows = $thead.find('tr');
$table.prepend($theadRows);
$thead.remove();
}
else { // thead not found, so convert top row to header row
var $topRow = $table.find('tr')[0];
$topRow.remove();
var $thead = $("<thead>");
$thead.prependTo($table);
$thead.append($topRow);
self.replaceTags($thead.find('td'), 'th')
// Detect changes to the table dom so we can fix the header
// after rows or cols are added. Summernote creates td's only
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
// Options for the observer (which mutations to observe)
var config = { childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
for(var mutation of mutationsList) {
self.replaceTags($(mutation.target).find('td'), 'th')
}
};
// Create an observer instance linked to the callback function
self.observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
self.observer.observe($thead[0], config);
self.destroy = function () {
self.observer.disconnect();
};
} // else
context.invoke('afterCommand');
}
};
this.replaceTags = function($nodes, newTag) {
$nodes.replaceWith(function() {
return $("<" + newTag + " />", {html: $(this).html()});
});
}
}
});
}));