|
4 | 4 |
|
5 | 5 | // helps with some corner cases where <wbr> starts working already,
|
6 | 6 | // but the signature is not yet long enough to be wrapped
|
7 |
| -const leftPaddingPx = 60 |
| 7 | +(function() { |
| 8 | + const leftPaddingPx = 60; |
8 | 9 |
|
9 |
| -const symbolResizeObserver = new ResizeObserver(entries => { |
10 |
| - entries.forEach(entry => { |
11 |
| - const symbolElement = entry.target |
12 |
| - symbolResizeObserver.unobserve(symbolElement) // only need it once, otherwise will be executed multiple times |
13 |
| - wrapSymbolParameters(symbolElement); |
14 |
| - }) |
15 |
| -}); |
16 |
| - |
17 |
| -const wrapAllSymbolParameters = () => { |
18 |
| - document.querySelectorAll("div.symbol").forEach(symbol => wrapSymbolParameters(symbol)) |
19 |
| -} |
20 |
| - |
21 |
| -const wrapSymbolParameters = (symbol) => { |
22 |
| - let parametersBlock = symbol.querySelector("span.parameters") |
23 |
| - if (parametersBlock == null) { |
24 |
| - return // nothing to wrap |
| 10 | + function createNbspIndent() { |
| 11 | + let indent = document.createElement("span"); |
| 12 | + indent.append(document.createTextNode("\u00A0\u00A0\u00A0\u00A0")); |
| 13 | + indent.classList.add("nbsp-indent"); |
| 14 | + return indent; |
25 | 15 | }
|
26 | 16 |
|
27 |
| - let symbolBlockWidth = symbol.clientWidth |
| 17 | + function wrapSymbolParameters(entry) { |
| 18 | + const symbol = entry.target; |
| 19 | + const symbolBlockWidth = entry.borderBoxSize && entry.borderBoxSize[0] && entry.borderBoxSize[0].inlineSize; |
28 | 20 |
|
29 |
| - // Even though the script is marked as `defer` and we wait for `DOMContentLoaded` event, |
30 |
| - // it can happen that `symbolBlockWidth` is 0, indicating that something hasn't been loaded. |
31 |
| - // In this case, just retry once all styles have been applied and it has been resized correctly. |
32 |
| - if (symbolBlockWidth === 0) { |
33 |
| - symbolResizeObserver.observe(symbol) |
34 |
| - return |
35 |
| - } |
| 21 | + // Even though the script is marked as `defer` and we wait for `DOMContentLoaded` event, |
| 22 | + // or if this block is a part of hidden tab, it can happen that `symbolBlockWidth` is 0, |
| 23 | + // indicating that something hasn't been loaded. |
| 24 | + // In this case, observer will be triggered onсe again when it will be ready. |
| 25 | + if (symbolBlockWidth > 0) { |
| 26 | + const node = symbol.querySelector(".parameters"); |
36 | 27 |
|
37 |
| - let innerTextWidth = Array.from(symbol.children) |
38 |
| - .filter(it => !it.classList.contains("block")) // blocks are usually on their own (like annotations), so ignore it |
39 |
| - .map(it => it.getBoundingClientRect().width).reduce((a, b) => a + b, 0) |
| 28 | + if (node) { |
| 29 | + // if window resize happened and observer was triggered, reset previously wrapped |
| 30 | + // parameters as they might not need wrapping anymore, and check again |
| 31 | + node.classList.remove("wrapped"); |
| 32 | + node.querySelectorAll(".parameter .nbsp-indent") |
| 33 | + .forEach(indent => indent.remove()); |
40 | 34 |
|
41 |
| - // if signature text takes up more than a single line, wrap params for readability |
42 |
| - let shouldWrapParams = innerTextWidth > (symbolBlockWidth - leftPaddingPx) |
43 |
| - if (shouldWrapParams) { |
44 |
| - parametersBlock.classList.add("wrapped") |
45 |
| - parametersBlock.querySelectorAll("span.parameter").forEach(param => { |
46 |
| - // has to be a physical indent so that it can be copied. styles like |
47 |
| - // paddings and `::before { content: " " }` do not work for that |
48 |
| - param.prepend(createNbspIndent()) |
49 |
| - }) |
50 |
| - } |
51 |
| -} |
| 35 | + const innerTextWidth = Array.from(symbol.children) |
| 36 | + .filter(it => !it.classList.contains("block")) // blocks are usually on their own (like annotations), so ignore it |
| 37 | + .map(it => it.getBoundingClientRect().width) |
| 38 | + .reduce((a, b) => a + b, 0); |
52 | 39 |
|
53 |
| -const createNbspIndent = () => { |
54 |
| - let indent = document.createElement("span") |
55 |
| - indent.append(document.createTextNode("\u00A0\u00A0\u00A0\u00A0")) |
56 |
| - indent.classList.add("nbsp-indent") |
57 |
| - return indent |
58 |
| -} |
| 40 | + // if signature text takes up more than a single line, wrap params for readability |
| 41 | + if (innerTextWidth > (symbolBlockWidth - leftPaddingPx)) { |
| 42 | + node.classList.add("wrapped"); |
| 43 | + node.querySelectorAll(".parameter").forEach(param => { |
| 44 | + // has to be a physical indent so that it can be copied. styles like |
| 45 | + // paddings and `::before { content: " " }` do not work for that |
| 46 | + param.prepend(createNbspIndent()); |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
59 | 52 |
|
60 |
| -const resetAllSymbolParametersWrapping = () => { |
61 |
| - document.querySelectorAll("div.symbol").forEach(symbol => resetSymbolParametersWrapping(symbol)) |
62 |
| -} |
| 53 | + const symbolsObserver = new ResizeObserver(entries => entries.forEach(wrapSymbolParameters)); |
63 | 54 |
|
64 |
| -const resetSymbolParametersWrapping = (symbol) => { |
65 |
| - let parameters = symbol.querySelector("span.parameters") |
66 |
| - if (parameters != null) { |
67 |
| - parameters.classList.remove("wrapped") |
68 |
| - parameters.querySelectorAll("span.parameter").forEach(param => { |
69 |
| - let indent = param.querySelector("span.nbsp-indent") |
70 |
| - if (indent != null) indent.remove() |
71 |
| - }) |
| 55 | + function initHandlers() { |
| 56 | + document.querySelectorAll("div.symbol").forEach(symbol => symbolsObserver.observe(symbol)); |
72 | 57 | }
|
73 |
| -} |
74 | 58 |
|
75 |
| -if (document.readyState === 'loading') { |
76 |
| - window.addEventListener('DOMContentLoaded', () => { |
77 |
| - wrapAllSymbolParameters() |
78 |
| - }) |
79 |
| -} else { |
80 |
| - wrapAllSymbolParameters() |
81 |
| -} |
| 59 | + if (document.readyState === 'loading') window.addEventListener('DOMContentLoaded', initHandlers); |
| 60 | + else initHandlers(); |
82 | 61 |
|
83 |
| -window.onresize = event => { |
84 |
| - // need to re-calculate if params need to be wrapped after resize |
85 |
| - resetAllSymbolParametersWrapping() |
86 |
| - wrapAllSymbolParameters() |
87 |
| -} |
| 62 | + // ToDo: Add `unobserve` if dokka will be SPA-like: |
| 63 | + // https://github.com/w3c/csswg-drafts/issues/5155 |
| 64 | +})(); |
0 commit comments