From 6dcf1f422cab3a2cc811c36ddad1ac3307d099f3 Mon Sep 17 00:00:00 2001 From: Marcus Ortiz Date: Mon, 25 Oct 2021 14:28:57 -0700 Subject: [PATCH] Add support for `variantOverrides` Render JSON key. --- src/components/DocumentationTopic.vue | 30 +- .../Summary/LanguageSwitcher.vue | 56 ++- .../Summary/LanguageSwitcherLink.vue | 2 +- src/styles/core/colors/_dark.scss | 1 + src/styles/core/colors/_light.scss | 1 + src/utils/data.js | 4 + src/utils/json-patch.js | 333 ++++++++++++++++++ src/utils/json-pointer.js | 63 ++++ src/views/DocumentationTopic.vue | 36 +- .../components/DocumentationTopic.spec.js | 23 +- .../Summary/LanguageSwitcher.spec.js | 53 ++- .../Summary/LanguageSwitcherLink.spec.js | 12 +- tests/unit/utils/json-patch.spec.js | 225 ++++++++++++ tests/unit/utils/json-pointer.spec.js | 35 ++ tests/unit/views/DocumentationTopic.spec.js | 37 ++ 15 files changed, 849 insertions(+), 62 deletions(-) create mode 100644 src/utils/json-patch.js create mode 100644 src/utils/json-pointer.js create mode 100644 tests/unit/utils/json-patch.spec.js create mode 100644 tests/unit/utils/json-pointer.spec.js diff --git a/src/components/DocumentationTopic.vue b/src/components/DocumentationTopic.vue index 4b1a0eb4e..d52d7bdbb 100644 --- a/src/components/DocumentationTopic.vue +++ b/src/components/DocumentationTopic.vue @@ -98,6 +98,7 @@ import { getSetting } from 'docc-render/utils/theme-settings'; import Aside from 'docc-render/components/ContentNode/Aside.vue'; import DocumentationNav from 'theme/components/DocumentationTopic/DocumentationNav.vue'; import BetaLegalText from 'theme/components/DocumentationTopic/BetaLegalText.vue'; +import LanguageSwitcher from 'theme/components/DocumentationTopic/Summary/LanguageSwitcher.vue'; import Abstract from './DocumentationTopic/Description/Abstract.vue'; import ContentNode from './DocumentationTopic/ContentNode.vue'; import CallToActionButton from './CallToActionButton.vue'; @@ -113,7 +114,6 @@ import SeeAlso from './DocumentationTopic/SeeAlso.vue'; import Summary from './DocumentationTopic/Summary.vue'; import Title from './DocumentationTopic/Title.vue'; import Topics from './DocumentationTopic/Topics.vue'; -import LanguageSwitcher from './DocumentationTopic/Summary/LanguageSwitcher.vue'; export default { name: 'DocumentationTopic', @@ -307,25 +307,33 @@ export default { // hierarchy/breadcrumb for a given topic. We choose to render only the // first one. parentTopicIdentifiers: ({ hierarchy: { paths: [ids = []] = [] } }) => ids, - shouldShowLanguageSwitcher: ({ isTargetIDE, objcPath, swiftPath }) => ( - isTargetIDE && objcPath && swiftPath - ), + shouldShowLanguageSwitcher: ({ objcPath, swiftPath }) => objcPath && swiftPath, hideSummary: () => getSetting(['features', 'docs', 'summary', 'hide'], false), }, + methods: { + normalizePath(path) { + // Sometimes `paths` data from `variants` are prefixed with a leading + // slash and sometimes they aren't + return path.startsWith('/') ? path : `/${path}`; + }, + }, created() { // Switch to the Objective-C variant of a page if the query parameter is not // present in the URL _but_ the user has previously selected Objective-C // using the navigation toggle (indicating a semi-global preference/mode) if (this.topicState.preferredLanguage === Language.objectiveC.key.url && this.interfaceLanguage !== Language.objectiveC.key.api - && this.objcPath) { + && this.objcPath && this.$route.query.language !== Language.objectiveC.key.url) { const { query } = this.$route; - this.$router.replace({ - path: `/${this.objcPath}`, - query: { - ...query, - language: Language.objectiveC.key.url, - }, + + this.$nextTick().then(() => { + this.$router.replace({ + path: this.normalizePath(this.objcPath), + query: { + ...query, + language: Language.objectiveC.key.url, + }, + }); }); } diff --git a/src/components/DocumentationTopic/Summary/LanguageSwitcher.vue b/src/components/DocumentationTopic/Summary/LanguageSwitcher.vue index b92508ed0..bc65a1ed8 100644 --- a/src/components/DocumentationTopic/Summary/LanguageSwitcher.vue +++ b/src/components/DocumentationTopic/Summary/LanguageSwitcher.vue @@ -15,16 +15,19 @@ class="language-option swift" :class="{ active: swift.active }" :url="swift.active ? null : swift.url" + @click="chooseLanguage(swift)" >{{swift.name}} {{objc.name}}