-
Notifications
You must be signed in to change notification settings - Fork 4
/
websocket_text_iink_search.html
173 lines (156 loc) · 5.75 KB
/
websocket_text_iink_search.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="HandheldFriendly" content="true" />
<title>Text search</title>
<link rel="stylesheet" href="../assets/style/reset.css" />
<link rel="stylesheet" href="../assets/style/components.css" />
<link rel="stylesheet" href="../assets/style/examples.css" />
<script type="text/javascript" src="../../dist/iink.min.js"></script>
<style>
#editor {
height: calc(100vh - 62px);
}
@media only screen and (max-width: 509px) {
#editor {
height: calc(100vh - 110px);
}
}
@media only screen and (max-width: 397px) {
#editor {
height: calc(100vh - 157px);
}
.input-field {
width: 100%;
}
}
</style>
</head>
<body>
<nav class="flex-container wrap between gap">
<div class="flex-container wrap between gap">
<input id="searchInput" type="search" class="input-field" />
<button id="searchBtn" class="classic-btn">Search</button>
</div>
<button class="classic-btn" id="convert" disabled>Convert</button>
</nav>
<div id="editor" touch-action="none"></div>
<script>
const editorElement = document.getElementById("editor");
const convertElement = document.getElementById("convert");
const searchInput = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn");
let lastJiix = undefined;
/**
* we expose the editor for use in the integration test
*/
let editor;
async function loadEditor() {
const res = await fetch("../server-configuration.json");
const conf = await res.json();
const options = {
configuration: {
server: {
...conf,
protocol: "WEBSOCKET"
},
recognition: {
type: "TEXT",
export: {
jiix: {
"bounding-box": true
}
}
},
rendering: {
smartGuide: {
enable: false
}
}
}
};
/**
* Instanciate editor
* @param {Element} The DOM element to attach the ink paper
* @param {Object} The Editor parameters
*/
editor = new iink.Editor(editorElement, options);
/**
* async initialize editor behaviors
*/
await editor.initialize();
// Utility function transforming millimeters to pixel as iinkSDK is millimeters only
function mmToPixel(mm) {
return (96 * mm) / 25.4;
}
// Highlight a series of words (jiix structure) by creating a div with a lower z-index
function highlightWords(words) {
words.forEach((word) => {
const boundingBox = word["bounding-box"];
const newElement = document.createElement("div");
newElement.style.position = "absolute";
newElement.style.left = mmToPixel(boundingBox.x) + "px";
newElement.style.top = mmToPixel(boundingBox.y) + "px";
newElement.style.height = mmToPixel(boundingBox.height) + "px";
newElement.style.width = mmToPixel(boundingBox.width) + "px";
newElement.classList.add("highlight");
newElement.style.backgroundColor = "yellow";
editorElement.appendChild(newElement);
});
}
// Perform the search by iterating the last jiix received and ignoring the carriage returns and blank in it.
function performSearch() {
document.querySelectorAll(".highlight").forEach((elem) => elem.parentNode.removeChild(elem));
if (searchInput.value && lastJiix) {
searchWords = searchInput.value.split(" ");
let posUnderEvaluation = 0;
let wordsToHighlight = [];
lastJiix.words.forEach((word) => {
const searchWord = searchWords[posUnderEvaluation];
if (word.label.trim() !== "") {
if (word.label.toUpperCase() === searchWord.toUpperCase()) {
wordsToHighlight.push(word);
if (posUnderEvaluation === searchWords.length - 1) {
highlightWords(wordsToHighlight);
posUnderEvaluation = 0;
wordsToHighlight = [];
} else {
posUnderEvaluation++;
}
} else {
posUnderEvaluation = 0;
wordsToHighlight = [];
}
}
});
}
}
searchBtn.addEventListener("click", performSearch);
editor.events.addEventListener("exported", (evt) => {
const exports = evt.detail;
if (exports) {
convertElement.disabled = false;
if (exports["application/vnd.myscript.jiix"]) {
lastJiix = exports["application/vnd.myscript.jiix"];
performSearch();
}
}
});
editor.events.addEventListener("changed", () => {
document.querySelectorAll(".highlight").forEach((elem) => elem.parentNode.removeChild(elem));
});
convertElement.addEventListener("click", () => {
editor.convert();
});
window.addEventListener("resize", () => {
editor.resize();
});
};
loadEditor().catch(error => console.error(error));
</script>
</body>
</html>