Skip to content

Commit 92db0b9

Browse files
committed
improve newline insertion logic for tooltips
1 parent 245da7a commit 92db0b9

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/js/dom-on-load.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import {clamp, debounce, t, tryURL} from './util';
1313

1414
const SPLIT_BTN_MENU = '.split-btn-menu';
1515
const tooltips = new WeakMap();
16+
/** Strips html tags but allows <invalid-html-tags> which we use to emphasize stuff */
17+
const rxTag = /(\n\s*)?<\/?[a-z]+(?:\s+[a-z]+=[^>]*)?>/g;
18+
const rxLong1 = /([.?!]\s+|[]\s*|.{55,70},)\s+/gu;
19+
const rxLong2 = /(.{55,70}(?=.{50,}))\s+/gu;
20+
1621
splitLongTooltips();
1722
addTooltipsToEllipsized();
1823
window.on('mousedown', suppressFocusRingOnClick, {passive: true});
@@ -224,20 +229,18 @@ function splitLongTooltips() {
224229
if (tooltips.has(el))
225230
continue;
226231
const old = el.title;
227-
// Strip html tags but allow <invalid-html-tags> which we use to emphasize stuff
228-
const title = old.replace(/(\n\s*)?<\/?[a-z]+(\s+[a-z]+=[^>]*?)?>(\n\s*)?/g, ' ');
229232
tooltips.set(el, old);
230-
let tmp = '';
231-
if (title.length < 50) {
232-
tmp = title;
233-
} else {
234-
for (const s of title.split(/\n+/)) {
235-
tmp += s.replace(/([.?!]\s+|[]\s*|.{50,60},)\s+/gu, '$1\n')
236-
.replace(/(.{50,80}(?=.{40,}))\s+/gu, '$1\n');
237-
tmp += '\n\n';
233+
let res = old.includes('<') ? old.replace(rxTag, '$1') : old;
234+
if (res.length > 60) {
235+
for (let arr = res.split(/\n+/), a = res = '', b, cut, i = 0; i < arr.length; i++) {
236+
a = arr[i];
237+
b = a.length <= 60 ? a : a.replace(rxLong1, '$1\n').replace(rxLong2, '$1\n');
238+
res += cut || i && b !== a ? '\n\n' : '\n';
239+
res += b;
240+
cut = b !== a;
238241
}
239242
}
240-
if (tmp !== old)
241-
el.title = tmp;
243+
if (res !== old)
244+
el.title = res;
242245
}
243246
}

src/options/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ for (const el of $$('[data-clickable]')) {
7979
this.dispatchEvent(new Event('change'));
8080
};
8181
note.title = `<table>${
82-
note.title.replace(/<([^>]+)>\s*([^<\n]+)/g, (_, a, b) =>
82+
note.title.replace(/<([^>]+)>([^<\n]+)/g, (_, a, b) =>
8383
`<tr><td><code>${a}</code></td><td>${b}</td></tr>`)
8484
}</table>`;
8585
for (const el of $$('[show-if]')) {

0 commit comments

Comments
 (0)