-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjquery.custom-highlight.js
More file actions
147 lines (116 loc) · 3.83 KB
/
Copy pathjquery.custom-highlight.js
File metadata and controls
147 lines (116 loc) · 3.83 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;(function ($, window, document, undefined) {
"use strict";
const pluginName = "customHighlight",
defaults = {
actions: [{"test_action": "Test action"}],
position: "left",
textcolor: "on",
backgroundcolor: "off"
};
class Plugin {
constructor(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
init() {
let mouseX;
let mouseY;
$(document).mousemove(e => {
mouseX = e.pageX;
mouseY = e.pageY - 10;
});
$(this.element).mouseup(e => this.handleMouseUp(e));
}
handleMouseUp(e) {
const selectedText = this.getSelectedText();
if (selectedText != "") {
const curr_span_id = this.add_cs_class(selectedText);
const $currSpanElement = $("#" + curr_span_id);
this.applyTextcolor($currSpanElement);
this.applyBackgroundcolor($currSpanElement, $currSpanElement.css('color'));
this.applyActionsToElement($currSpanElement);
}
}
getSelectedText() {
return (document.all) ? document.selection.createRange().text : document.getSelection();
}
applyTextcolor($element) {
if (this.settings.textcolor == "on") {
const r_color = '#' + Math.random().toString(16).slice(2, 8);
$element.css('color', r_color);
}
}
applyBackgroundcolor($element, r_color) {
if (this.settings.backgroundcolor == "on") {
let b_color = '#' + Math.random().toString(16).slice(2, 8);
if (r_color == b_color) {
b_color = '#' + Math.random().toString(16).slice(2, 8);
}
$element.addClass(".light").css("color", "#3A393C");
$element.addClass(".dark").css("color", "#FBFBFB");
$element.css('background-color', b_color);
$element.colourBrightness();
}
}
applyActionsToElement($element) {
const all_actions = this.settings.actions;
let all_buttons = "";
if (all_actions.length > 0) {
all_buttons = this.getButtonsFromActions(all_actions);
}
$element.tooltipster({
content: all_buttons,
multiple: true,
position: this.settings.position,
delay: 100,
maxWidth: 500,
speed: 300,
interactive: true,
animation: 'grow',
trigger: 'hover',
contentAsHTML: true
});
}
getButtonsFromActions(actions) {
const first = Object.keys(actions[0])[0];
let buttons = "";
if (first == "test_action") {
buttons = "<button type='button' class='btn' onclick=test_action('" + curr_span_id_for_use + "')>Test Action</button>";
} else {
for (let action of actions) {
for (let key in action) {
buttons += "<button type='button' class='btn' onclick=" + key + "('" + curr_span_id_for_use + "')>" + action[key] + "</button> ";
}
}
}
return buttons;
}
add_cs_class(s_text) {
const selection = s_text;
const selection_text = selection.toString();
const span = document.createElement('SPAN');
span.textContent = " + " + selection_text + " + ";
span.classList.add("cs-editing");
span.classList.add("tooltip");
const range = selection.getRangeAt(0);
range.deleteContents();
range.insertNode(span);
const custom_hash_1 = Math.random().toString(36).slice(2);
span.id = "cs-editing-" + custom_hash_1;
return span.id;
}
test_action() {
alert("Here's a Test Action!");
}
}
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);