From 211b327cc27e96da42fd189cd25c9e053237544b Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 10 Jan 2024 14:39:38 +0100 Subject: [PATCH] Add support to content tabs --- book.toml | 1 + src/rust-sdk/01-getting-started.md | 4 +- src/rust-sdk/02-installation.md | 57 ++++++++- src/rust-sdk/04-constructors.md | 18 +++ tabs.js | 180 +++++++++++++++++++++++++++++ 5 files changed, 256 insertions(+), 4 deletions(-) create mode 100644 tabs.js diff --git a/book.toml b/book.toml index f9929e2..90ba733 100644 --- a/book.toml +++ b/book.toml @@ -6,5 +6,6 @@ language = "en" multilingual = false [output.html] +additional-js = ["tabs.js"] git-repository-url = "https://github.com/smartvaults/book" edit-url-template = "https://github.com/smartvaults/book/edit/master/{path}" diff --git a/src/rust-sdk/01-getting-started.md b/src/rust-sdk/01-getting-started.md index 7955ea7..dd1c716 100644 --- a/src/rust-sdk/01-getting-started.md +++ b/src/rust-sdk/01-getting-started.md @@ -1,5 +1,3 @@ # Rust SDK -This section will show you how to use the Rust SDK. - -TODO: add UniFFI docs next to the Rust docs (use content tabs) \ No newline at end of file +This section will show you how to use the Rust SDK. \ No newline at end of file diff --git a/src/rust-sdk/02-installation.md b/src/rust-sdk/02-installation.md index 96337b0..249d66a 100644 --- a/src/rust-sdk/02-installation.md +++ b/src/rust-sdk/02-installation.md @@ -1,5 +1,9 @@ # Installing the library + +
Rust
+
+ Add the `smartvaults-sdk` dependency in your `Cargo.toml` file: ```toml @@ -23,4 +27,55 @@ Use a specific commit: ```toml [dependencies] smartvaults-sdk = { git = "https://github.com/smartvaults/smartvaults", rev = "383c186cb1df3ab5906978d6b313aed86d2698b1" } -``` \ No newline at end of file +``` + +
+ +
Kotlin
+
+ +To use the Kotlin language bindings for `smartvaults-sdk` in your Android project add the following to your gradle dependencies: + +```kotlin +repositories { + mavenCentral() +} + +dependencies { + implementation("io.smartvaults:smartvaults-sdk:") +} +``` + +Import the library in your code: + +```kotlin +import io.smartvaults.sdk.* +``` + +To import nostr or nostr-sdk: +```kotlin +import rust.nostr.protocol.* +import rust.nostr.sdk.* +``` + +## Known issues + +### JNA dependency + +Depending on the JVM version you use, you might not have the JNA dependency on your classpath. The exception thrown will be + +```bash +class file for com.sun.jna.Pointer not found +``` + +The solution is to add JNA as a dependency like so: + +```kotlin +dependencies { + // ... + implementation("net.java.dev.jna:jna:5.12.1") +} +``` + +
+
\ No newline at end of file diff --git a/src/rust-sdk/04-constructors.md b/src/rust-sdk/04-constructors.md index 1d64b11..61135dd 100644 --- a/src/rust-sdk/04-constructors.md +++ b/src/rust-sdk/04-constructors.md @@ -10,6 +10,10 @@ All these when constructed return a ready to use client. This constructor generate a random [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic and store it in a local [encrypted keychain](./keychain/01-index.md). + +
Rust
+
+ ```rust,no_run use smartvaults_sdk::prelude::*; @@ -28,6 +32,20 @@ let client = SmartVaults::generate( # } ``` +
+ +
Kotlin
+
+ +```kotlin +import io.smartvaults.sdk.* + +// TODO +``` +
+ +
+ ## Restore This constructor restore a [BIP39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic and store diff --git a/tabs.js b/tabs.js new file mode 100644 index 0000000..16ea403 --- /dev/null +++ b/tabs.js @@ -0,0 +1,180 @@ +(function () { + 'use strict'; + + let selected_ = null; + + customElements.define('custom-tabs', class extends HTMLElement { + + constructor() { + super(); // always call super() first in the ctor. + + // Create shadow DOM for the component. + let shadowRoot = this.attachShadow({ mode: 'open' }); + shadowRoot.innerHTML = ` + +
+ +
+
+ +
+ `; + } + + get selected() { + return selected_; + } + + set selected(idx) { + selected_ = idx; + this._selectTab(idx); + this.setAttribute('selected', idx); + } + + connectedCallback() { + this.setAttribute('role', 'tablist'); + + const tabsSlot = this.shadowRoot.querySelector('#tabsSlot'); + const panelsSlot = this.shadowRoot.querySelector('#panelsSlot'); + + this.tabs = tabsSlot.assignedNodes({ flatten: true }); + this.panels = panelsSlot.assignedNodes({ flatten: true }).filter(el => { + return el.nodeType === Node.ELEMENT_NODE; + }); + + // Save refer to we can remove listeners later. + this._boundOnTitleClick = this._onTitleClick.bind(this); + + tabsSlot.addEventListener('click', this._boundOnTitleClick); + document.addEventListener('mdbook-category-changed', this._onSiblingCategoryChanged.bind(this)); + this.selected = this._findFirstSelectedTab() || this._findStoredSelectedTab() || 0; + } + + disconnectedCallback() { + const tabsSlot = this.shadowRoot.querySelector('#tabsSlot'); + tabsSlot.removeEventListener('click', this._boundOnTitleClick); + document.removeEventListener('mdbook-category-changed', this._onSiblingCategoryChanged.bind(this)); + } + + _onTitleClick(e) { + if (e.target.slot === 'title') { + this.selected = this.tabs.indexOf(e.target); + e.target.focus(); + } + } + + _findFirstSelectedTab() { + let selectedIdx; + for (let [i, tab] of this.tabs.entries()) { + tab.setAttribute('role', 'tab'); + if (tab.hasAttribute('selected')) { + selectedIdx = i; + } + } + return selectedIdx; + } + + _findStoredSelectedTab() { + let selectedIdx; + if (this.getAttribute("category")) { + let selectedText; + try { selectedText = localStorage.getItem('mdbook-tabs-' + this.getAttribute("category")); } catch (e) { } + if (selectedText) { + for (let [i, tab] of this.tabs.entries()) { + if (tab.textContent === selectedText) { + selectedIdx = i; + break; + } + } + } + } + return selectedIdx; + } + + _selectTab(idx = null, propagate = true) { + let category = this.getAttribute("category"); + for (let i = 0, tab; tab = this.tabs[i]; ++i) { + let select = i === idx; + tab.setAttribute('tabindex', select ? 0 : -1); + tab.setAttribute('aria-selected', select); + this.panels[i].setAttribute('aria-hidden', !select); + if (select && category && tab.textContent) { + try { localStorage.setItem('mdbook-tabs-' + category, tab.textContent); } catch (e) { } + } + } + + if (propagate) { + document.dispatchEvent(new CustomEvent( + 'mdbook-category-changed', + { detail: { category: category, idx: idx }} + )); + } + } + + _onSiblingCategoryChanged(e) { + let category = this.getAttribute("category") + if (category === e.detail.category) { + this._selectTab(e.detail.idx, false); + this.setAttribute('selected', e.detail.idx); + } + } + }); + +})(); \ No newline at end of file