diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2023-01-19 04:10:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-19 04:10:57 +0100 |
commit | 5eeb2bbeb8c71ae813808ee0e8d09293c4abe1c1 (patch) | |
tree | bb62ab50a59cbeca06de6fd1eb5c75ff27bc5bd0 /examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js | |
parent | 1fa8552994fb292729eca8c0de6a1561380b008c (diff) | |
download | dokka-5eeb2bbeb8c71ae813808ee0e8d09293c4abe1c1.tar.gz dokka-5eeb2bbeb8c71ae813808ee0e8d09293c4abe1c1.tar.bz2 dokka-5eeb2bbeb8c71ae813808ee0e8d09293c4abe1c1.zip |
Revise README documentation for examples and plugins (#2736)
Diffstat (limited to 'examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js')
-rw-r--r-- | examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js b/examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js new file mode 100644 index 00000000..248d0ab0 --- /dev/null +++ b/examples/gradle/dokka-versioning-multimodule-example/previousDocVersions/0.9/scripts/symbol-parameters-wrapper_deferred.js @@ -0,0 +1,83 @@ +// helps with some corner cases where <wbr> starts working already, +// but the signature is not yet long enough to be wrapped +const leftPaddingPx = 60 + +const symbolResizeObserver = new ResizeObserver(entries => { + entries.forEach(entry => { + const symbolElement = entry.target + symbolResizeObserver.unobserve(symbolElement) // only need it once, otherwise will be executed multiple times + wrapSymbolParameters(symbolElement); + }) +}); + +const wrapAllSymbolParameters = () => { + document.querySelectorAll("div.symbol").forEach(symbol => wrapSymbolParameters(symbol)) +} + +const wrapSymbolParameters = (symbol) => { + let parametersBlock = symbol.querySelector("span.parameters") + if (parametersBlock == null) { + return // nothing to wrap + } + + let symbolBlockWidth = symbol.clientWidth + + // Even though the script is marked as `defer` and we wait for `DOMContentLoaded` event, + // it can happen that `symbolBlockWidth` is 0, indicating that something hasn't been loaded. + // In this case, just retry once all styles have been applied and it has been resized correctly. + if (symbolBlockWidth === 0) { + symbolResizeObserver.observe(symbol) + return + } + + let innerTextWidth = Array.from(symbol.children) + .filter(it => !it.classList.contains("block")) // blocks are usually on their own (like annotations), so ignore it + .map(it => it.getBoundingClientRect().width).reduce((a, b) => a + b, 0) + + // if signature text takes up more than a single line, wrap params for readability + let shouldWrapParams = innerTextWidth > (symbolBlockWidth - leftPaddingPx) + if (shouldWrapParams) { + parametersBlock.classList.add("wrapped") + parametersBlock.querySelectorAll("span.parameter").forEach(param => { + // has to be a physical indent so that it can be copied. styles like + // paddings and `::before { content: " " }` do not work for that + param.prepend(createNbspIndent()) + }) + } +} + +const createNbspIndent = () => { + let indent = document.createElement("span") + indent.append(document.createTextNode("\u00A0\u00A0\u00A0\u00A0")) + indent.classList.add("nbsp-indent") + return indent +} + +const resetAllSymbolParametersWrapping = () => { + document.querySelectorAll("div.symbol").forEach(symbol => resetSymbolParametersWrapping(symbol)) +} + +const resetSymbolParametersWrapping = (symbol) => { + let parameters = symbol.querySelector("span.parameters") + if (parameters != null) { + parameters.classList.remove("wrapped") + parameters.querySelectorAll("span.parameter").forEach(param => { + let indent = param.querySelector("span.nbsp-indent") + if (indent != null) indent.remove() + }) + } +} + +if (document.readyState === 'loading') { + window.addEventListener('DOMContentLoaded', () => { + wrapAllSymbolParameters() + }) +} else { + wrapAllSymbolParameters() +} + +window.onresize = event => { + // need to re-calculate if params need to be wrapped after resize + resetAllSymbolParametersWrapping() + wrapAllSymbolParameters() +} |