-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.html
52 lines (51 loc) · 1.88 KB
/
ui.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
<h2>DE Hyphenation</h2>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter">
<div style="text-align: justify">
<span id="loader" style="font-family: Inter;">I am used for loading</span>
<br>
<br>
<span id="content" style="font-family: Inter">Waiting for text...</span>
</div>
<script type="module">
window.onload = await function () {
document.getElementById("loader").innerHTML = "Font loaded";
}
import texLinebreak from 'https://esm.sh/[email protected]/';
import hypher from 'https://esm.sh/hyphenation.de';
const { createHyphenator, layoutText } = texLinebreak
const hyphenate = createHyphenator(hypher);
var result = "";
function measureText(cssFont, text) {
let measureCtx;
let canvas;
canvas = document.createElement('canvas');
measureCtx = canvas.getContext('2d');
measureCtx.font = cssFont;
console.log(measureCtx.font)
return measureCtx.measureText(text).width;
}
window.addEventListener('message', (event) => {
const measure = word => measureText('400 10px Inter', word)
document.getElementById("content").innerHTML = event.data.pluginMessage.text;
const { items, positions } = layoutText(event.data.pluginMessage.text, event.data.pluginMessage.width, measure, hyphenate)
// console.log(items, positions)
const stack = [];
for (let i = items.length - 1; i >= 0; i--) {
if (items[i].type == 'glue')
stack.push(i)
}
positions.forEach(pos => {
//console.log(pos)
if (pos.item == stack[stack.length - 1] + 1) {
result += items[stack[stack.length - 1]].text
stack.pop();
}
const item = items[pos.item]
//console.log(item)
const text = item.type === 'box' ? item.text : '-';
result += text;
// console.log(result)
});
parent.postMessage({ pluginMessage: { type: 'text', result, } }, '*')
}, false)
</script>