|
| 1 | +import { CSSResult } from "lit"; |
| 2 | + |
| 3 | +import { getReadTheDocsConfig } from "./readthedocs-config"; |
| 4 | +import * as notification from "./notification"; |
| 5 | +import * as analytics from "./analytics"; |
| 6 | +import * as search from "./search"; |
| 7 | +import * as docdiff from "./docdiff"; |
| 8 | +import * as flyout from "./flyout"; |
| 9 | +import * as ethicalads from "./ethicalads"; |
| 10 | +import * as hotkeys from "./hotkeys"; |
| 11 | +import * as linkpreviews from "./linkpreviews"; |
| 12 | +import * as filetreediff from "./filetreediff"; |
| 13 | +import * as customscript from "./customscript"; |
| 14 | +import * as application from "./application"; |
| 15 | +import { default as objectPath } from "object-path"; |
| 16 | +import { |
| 17 | + docTool, |
| 18 | + domReady, |
| 19 | + isEmbedded, |
| 20 | + IS_PRODUCTION, |
| 21 | + setupLogging, |
| 22 | + getMetadataValue, |
| 23 | +} from "./utils"; |
| 24 | + |
| 25 | +import doctoolsStyleSheet from "./doctools.css"; |
| 26 | +import { html, nothing, render, LitElement } from "lit"; |
| 27 | +import { ContextConsumer } from "@lit/context"; |
| 28 | +import { configContext } from "./context.js"; |
| 29 | + |
| 30 | +export class AddonsAppElement extends LitElement { |
| 31 | + static elementName = "readthedocs-application"; |
| 32 | + |
| 33 | + // `_config` is the context we are going to consume when it's updated. |
| 34 | + _config = new ContextConsumer(this, { |
| 35 | + context: configContext, |
| 36 | + subscribe: true, |
| 37 | + }); |
| 38 | + |
| 39 | + constructor() { |
| 40 | + super(); |
| 41 | + |
| 42 | + console.log( |
| 43 | + "Addons Application _config (from constructor() method)", |
| 44 | + this._config.value, |
| 45 | + ); |
| 46 | + |
| 47 | + this.addons = [ |
| 48 | + flyout.FlyoutAddon, |
| 49 | + notification.NotificationAddon, |
| 50 | + analytics.AnalyticsAddon, |
| 51 | + ethicalads.EthicalAdsAddon, |
| 52 | + search.SearchAddon, |
| 53 | + |
| 54 | + // HotKeys has to be initialized before DocDiff because when |
| 55 | + // `?readthedocs-diff=true` DocDiff triggers an event that HotKeys has to |
| 56 | + // listen to to update its internal state. |
| 57 | + hotkeys.HotKeysAddon, |
| 58 | + docdiff.DocDiffAddon, |
| 59 | + |
| 60 | + linkpreviews.LinkPreviewsAddon, |
| 61 | + filetreediff.FileTreeDiffAddon, |
| 62 | + customscript.CustomScriptAddon, |
| 63 | + ]; |
| 64 | + |
| 65 | + this.httpStatus = getMetadataValue("readthedocs-http-status"); |
| 66 | + |
| 67 | + setupLogging(); |
| 68 | + getReadTheDocsConfig(this.sendUrlParam()); // .then(() => console.log("Finished"));; |
| 69 | + } |
| 70 | + |
| 71 | + render() { |
| 72 | + console.log( |
| 73 | + "Addons Application _config (from render() method)", |
| 74 | + this._config.value, |
| 75 | + ); |
| 76 | + |
| 77 | + if (!this._config.value) { |
| 78 | + return nothing; |
| 79 | + } |
| 80 | + |
| 81 | + if (this._config.value && !this.loadWhenEmbedded()) { |
| 82 | + return nothing; |
| 83 | + } |
| 84 | + |
| 85 | + let promises = []; |
| 86 | + for (const addon of this.addons) { |
| 87 | + if (addon.isEnabled(this._config.value, this.httpStatus)) { |
| 88 | + promises.push( |
| 89 | + new Promise((resolve) => { |
| 90 | + resolve(new addon(this._config.value)); |
| 91 | + }), |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + Promise.all(promises).catch((err) => { |
| 97 | + console.error(err); |
| 98 | + }); |
| 99 | + |
| 100 | + return nothing; |
| 101 | + } |
| 102 | + |
| 103 | + loadWhenEmbedded() { |
| 104 | + const loadWhenEmbedded = objectPath.get( |
| 105 | + this._config.value, |
| 106 | + "addons.options.load_when_embedded", |
| 107 | + false, |
| 108 | + ); |
| 109 | + if (isEmbedded() && !loadWhenEmbedded) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + sendUrlParam() { |
| 116 | + for (const addon of this.addons) { |
| 117 | + if (addon.requiresUrlParam()) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + addDoctoolData() { |
| 125 | + // Apply fixes to variables for individual documentation tools |
| 126 | + const elementHtml = document.querySelector("html"); |
| 127 | + if (elementHtml) { |
| 128 | + // Inject styles at the parent DOM to set variables at :root |
| 129 | + let styleSheet = doctoolsStyleSheet; |
| 130 | + if (doctoolsStyleSheet instanceof CSSResult) { |
| 131 | + styleSheet = doctoolsStyleSheet.styleSheet; |
| 132 | + } |
| 133 | + document.adoptedStyleSheets = [styleSheet]; |
| 134 | + |
| 135 | + // If we detect a documentation tool, set attributes on :root to allow |
| 136 | + // for CSS selectors to utilize these values. |
| 137 | + if (docTool.documentationTool) { |
| 138 | + elementHtml.setAttribute( |
| 139 | + "data-readthedocs-tool", |
| 140 | + docTool.documentationTool, |
| 141 | + ); |
| 142 | + } |
| 143 | + if (docTool.documentationTheme) { |
| 144 | + elementHtml.setAttribute( |
| 145 | + "data-readthedocs-tool-theme", |
| 146 | + docTool.documentationTheme, |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +customElements.define("readthedocs-application", AddonsAppElement); |
| 154 | +render(new AddonsAppElement(), document.body); |
0 commit comments