Skip to content

Commit 8c7c776

Browse files
authored
Update index.html
- Add color selection (WIP)
1 parent 4c004d1 commit 8c7c776

1 file changed

Lines changed: 72 additions & 4 deletions

File tree

index.html

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
88
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
9+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
910
<title>Hello, world!</title>
1011
</head>
1112

@@ -35,6 +36,19 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
3536
<div class="mb-3">
3637
<button type="button" class="btn btn-outline-primary btn-block" onclick="process()">Process</button>
3738
<button type="button" class="btn btn-outline-success btn-block" onclick="copy()">Copy to clipboard</button>
39+
40+
41+
42+
<div class="btn-group">
43+
<button type="button" class="btn btn-outline-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
44+
Option: <span class="selection">Option 1</span><span class="caret"></span>
45+
</button>
46+
<ul class="dropdown-menu" id="color-dropdown">
47+
<li><a class="dropdown-item" href="#">All colors</a></li>
48+
</ul>
49+
</div>
50+
51+
3852
</div>
3953
</div>
4054
</div>
@@ -71,8 +85,6 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
7185
[String.raw `&#236;`, `i`],
7286
[String.raw `&#8220;`, ``],
7387
[String.raw `&#8221;`, ``],
74-
[String.raw `&#236;`, ``],
75-
[String.raw `&#236;`, ``],
7688
[String.raw ` `, ` `],
7789
[String.raw ` `, ` `],
7890
];
@@ -86,6 +98,11 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
8698
selectField("output");
8799
}
88100

101+
$(".dropdown-menu ul li a").click(function(){
102+
$(this).parents(".btn-group").find('.selection').text($(this).text());
103+
$(this).parents(".btn-group").find('.selection').val($(this).text());
104+
});
105+
89106
document.getElementById('input-file').addEventListener('change', getFile)
90107

91108
function getFile(event) {
@@ -94,7 +111,10 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
94111
document.getElementById("input").value = " "
95112
document.getElementById("output").value = " "
96113
placeFileContent(document.getElementById('input'), input.files[0]);
97-
selectField("input");
114+
setTimeout(function(){
115+
selectField("input");
116+
setColorFromInput();
117+
}, 500);
98118
}
99119
}
100120

@@ -110,7 +130,7 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
110130
reader.onload = event => resolve(event.target.result)
111131
reader.onerror = error => reject(error)
112132
reader.readAsText(file)
113-
})
133+
})
114134
}
115135

116136
function copy() {
@@ -125,6 +145,54 @@ <h1>Lithium Ebook Highlighted Text Extractor</h1>
125145
field.setSelectionRange(0, 99999); /* For mobile devices */
126146
}
127147

148+
function addColorDropdownItem(changeColor, colorItem) {
149+
var list = document.getElementById("color-dropdown");
150+
var li = document.createElement("li");
151+
var link = document.createElement("a");
152+
link.className = "dropdown-item"
153+
if (changeColor) {
154+
link.setAttribute("style", "color: " + colorItem + " ");
155+
}
156+
var text = document.createTextNode(colorItem);
157+
//alert(colorItem)
158+
link.appendChild(text);
159+
link.href = "#";
160+
li.appendChild(link);
161+
list.appendChild(li);
162+
}
163+
164+
function setColorFromInput() {
165+
var colors = [];
166+
let input = document.getElementById("input").value
167+
input = input.replace(new RegExp(String.raw `^(?!<div class='color' style='background-color).+`, "mg"), ``);
168+
input = input.replaceAll(String.raw `<div class='color' style='background-color: `,``)
169+
input = input.replaceAll(String.raw `'></div>`,``)
170+
input = input.replace(new RegExp(String.raw `^(?:[\t ]*(?:\r?\n|\r))+`, "mg"), ``);
171+
var char = '\n';
172+
var i = j = 0;
173+
while ((j = input.indexOf(char, i)) !== -1) {
174+
colors.push(input.substring(i, j).trim());
175+
i = j + 1;
176+
}
177+
let colorsUniq = remove_duplicates(colors)
178+
addColorDropdownItem(false,"All colors")
179+
for (var i = 0; i < colorsUniq.length; i++){
180+
addColorDropdownItem(true,colorsUniq[i])
181+
}
182+
}
183+
184+
function remove_duplicates(arr) {
185+
var obj = {};
186+
var ret_arr = [];
187+
for (var i = 0; i < arr.length; i++) {
188+
obj[arr[i]] = true;
189+
}
190+
for (var key in obj) {
191+
ret_arr.push(key);
192+
}
193+
return ret_arr;
194+
}
195+
128196
</script>
129197
</body>
130198

0 commit comments

Comments
 (0)