Update src/frontend/HighlightedTextbox.svelte
Browse files
src/frontend/HighlightedTextbox.svelte
CHANGED
|
@@ -56,25 +56,33 @@
|
|
| 56 |
_color_map = correct_color_map(current_color_map, browser, ctx);
|
| 57 |
}
|
| 58 |
|
| 59 |
-
function
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
$: set_color_map();
|
| 80 |
$: set_text_from_value(true);
|
|
@@ -108,42 +116,66 @@
|
|
| 108 |
});
|
| 109 |
|
| 110 |
function set_value_from_marked_span(): void {
|
| 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 |
|
| 148 |
function handle_remove_tags(): void {
|
| 149 |
marked_el_text = el_text;
|
|
|
|
| 56 |
_color_map = correct_color_map(current_color_map, browser, ctx);
|
| 57 |
}
|
| 58 |
|
| 59 |
+
function escapeHtml(text: string): string {
|
| 60 |
+
const div = document.createElement('div');
|
| 61 |
+
div.textContent = text;
|
| 62 |
+
return div.innerHTML;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function set_text_from_value(as_output: boolean): void {
|
| 66 |
+
if (value.length > 0 && as_output) {
|
| 67 |
+
el_text = value.map(([text, _]) => text).join("");
|
| 68 |
+
marked_el_text = value.map(([text, category]) => {
|
| 69 |
+
// Escape HTML entities in the text
|
| 70 |
+
const escapedText = escapeHtml(text);
|
| 71 |
+
if (category !== null) {
|
| 72 |
+
return `<mark class="hl ${category}" style="background-color:${_color_map[category].secondary}">${escapedText}</mark>`;
|
| 73 |
+
} else {
|
| 74 |
+
return escapedText;
|
| 75 |
+
}
|
| 76 |
+
}).join("");
|
| 77 |
+
tagged_text = value.map(([text, category]) => {
|
| 78 |
+
if (category !== null) {
|
| 79 |
+
return `<${category}>${text}</${category}>`;
|
| 80 |
+
} else {
|
| 81 |
+
return text;
|
| 82 |
+
}
|
| 83 |
+
}).join("");
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
|
| 87 |
$: set_color_map();
|
| 88 |
$: set_text_from_value(true);
|
|
|
|
| 116 |
});
|
| 117 |
|
| 118 |
function set_value_from_marked_span(): void {
|
| 119 |
+
let new_value: [string, string | null][] = [];
|
| 120 |
+
let text = "";
|
| 121 |
+
let category = null;
|
| 122 |
+
|
| 123 |
+
// Create a temporary container to parse the HTML properly
|
| 124 |
+
const tempDiv = document.createElement('div');
|
| 125 |
+
tempDiv.innerHTML = marked_el_text;
|
| 126 |
+
|
| 127 |
+
// Function to recursively process nodes
|
| 128 |
+
function processNode(node: Node) {
|
| 129 |
+
if (node.nodeType === Node.TEXT_NODE) {
|
| 130 |
+
// Text node - add to current text
|
| 131 |
+
text += node.textContent || '';
|
| 132 |
+
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
| 133 |
+
const element = node as HTMLElement;
|
| 134 |
+
|
| 135 |
+
if (element.tagName.toLowerCase() === 'mark') {
|
| 136 |
+
// Save any accumulated text before the mark
|
| 137 |
+
if (text) {
|
| 138 |
+
new_value.push([text, category]);
|
| 139 |
+
text = "";
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Extract category from mark class
|
| 143 |
+
const classList = element.className.split(' ');
|
| 144 |
+
const hlIndex = classList.indexOf('hl');
|
| 145 |
+
category = hlIndex >= 0 && hlIndex + 1 < classList.length ? classList[hlIndex + 1] : null;
|
| 146 |
+
|
| 147 |
+
// Process mark content
|
| 148 |
+
for (let child of element.childNodes) {
|
| 149 |
+
processNode(child);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Save marked text
|
| 153 |
+
if (text) {
|
| 154 |
+
new_value.push([text, category]);
|
| 155 |
+
text = "";
|
| 156 |
+
category = null;
|
| 157 |
+
}
|
| 158 |
+
} else {
|
| 159 |
+
// Other elements - process children
|
| 160 |
+
for (let child of element.childNodes) {
|
| 161 |
+
processNode(child);
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
// Process all nodes
|
| 168 |
+
for (let node of tempDiv.childNodes) {
|
| 169 |
+
processNode(node);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
// Add any remaining text
|
| 173 |
+
if (text) {
|
| 174 |
+
new_value.push([text, category]);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
value = new_value;
|
| 178 |
+
}
|
| 179 |
|
| 180 |
function handle_remove_tags(): void {
|
| 181 |
marked_el_text = el_text;
|