-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummernote-footnotes.js
90 lines (80 loc) · 4.02 KB
/
summernote-footnotes.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
(function ($) {
$.extend(true, $.summernote.plugins, {
'footnotes': function (context) {
var self = this;
var ui = $.summernote.ui,
$note = context.layoutInfo.note,
$editable = context.layoutInfo.editable,
options = context.options,
lang = options.langInfo;
var footnoteCounter = 1;
context.memo('button.footnotes', function () {
var button = ui.button({
contents: '<i class="fa fa-superscript"></i>',
tooltip: 'insert footnote',
click: function () {
self.showFootnoteModal();
}
});
return button.render();
});
this.showFootnoteModal = function () {
var modal = `
<div class="modal fade" id="footnoteModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Insert Footnote</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<textarea id="footnoteText" class="form-control" placeholder="Enter footnote text"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="insertFootnote">Insert</button>
</div>
</div>
</div>
</div>
`;
if ($('#footnoteModal').length === 0) {
$('body').append(modal);
}
$('#footnoteModal').modal('show');
self.handleFootnoteEvents();
};
this.handleFootnoteEvents = function () {
$('#insertFootnote').off('click').on('click', function () {
var footnoteText = $('#footnoteText').val();
if (footnoteText) {
var footnoteId = 'footnote-' + footnoteCounter;
var footnoteMarkup = `<sup><a href="#${footnoteId}" id="ref-${footnoteId}">[${footnoteCounter}]</a></sup>` + '<span></span>';
var footnoteEntry = `
<p id="${footnoteId}">
<a href="#ref-${footnoteId}"><sup>[${footnoteCounter}]</sup></a> ${footnoteText}
</p>
`;
$note.summernote('editor.restoreRange');
var node = document.createElement("span");
node.innerHTML = footnoteMarkup;
$note.summernote('editor.insertNode', node);
var $footnotes = $editable.find('#footnotes');
if ($footnotes.length === 0) {
$editable.append('<div id="footnotes"><hr/></div>');
$footnotes = $editable.find('#footnotes');
}
$footnotes.append(footnoteEntry);
// Synchronize Summernote's content
$note.summernote('code', $editable.html());
$('#footnoteModal').modal('hide');
footnoteCounter++;
}
});
$('#footnoteModal').on('show.bs.modal', function () {
$('#footnoteText').val('');
});
};
}
});
})(jQuery);